Skip to content

Commit d3c1528

Browse files
committed
various updates and fixes to talk examples
1 parent 3c22263 commit d3c1528

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

PyOhio_2019/examples/arg_decorators.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self):
1616
fsize_parser = cmd2.Cmd2ArgumentParser(description='Obtain the size of a file')
1717
fsize_parser.add_argument('-c', '--comma', action='store_true',
1818
help='add comma for thousands separator')
19+
fsize_parser.add_argument('-u', '--unit', choices=['MB', 'KB'], help='unit to display size in')
1920
fsize_parser.add_argument('file_path', help='path of file',
2021
completer_method=cmd2.Cmd.path_complete)
2122

@@ -30,9 +31,17 @@ def do_fsize(self, args: argparse.Namespace) -> None:
3031
self.perror("Error retrieving size: {}".format(ex))
3132
return
3233

34+
if args.unit == 'KB':
35+
size /= 1024
36+
elif args.unit == 'MB':
37+
size /= 1024 * 1024
38+
else:
39+
args.unit = 'bytes'
40+
size = round(size, 2)
41+
3342
if args.comma:
3443
size = '{:,}'.format(size)
35-
self.poutput('{} bytes'.format(size))
44+
self.poutput('{} {}'.format(size, args.unit))
3645

3746
# do_pow parser
3847
pow_parser = argparse.ArgumentParser()

PyOhio_2019/examples/pyscript_example.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self):
2222
def _set_prompt(self):
2323
"""Set prompt so it displays the current working directory."""
2424
self._cwd = os.getcwd()
25-
self.prompt = style('{!r} $ '.format(self.cwd), fg='cyan')
25+
self.prompt = style('{!r} $ '.format(self._cwd), fg='cyan')
2626

2727
def postcmd(self, stop: bool, line: str) -> bool:
2828
"""Hook method executed just after a command dispatch is finished.
@@ -35,7 +35,6 @@ def postcmd(self, stop: bool, line: str) -> bool:
3535
self._set_prompt()
3636
return stop
3737

38-
@property
3938
def cwd(self):
4039
"""Read-only property used by the pyscript to obtain cwd"""
4140
return self._cwd
@@ -94,7 +93,7 @@ def do_dir(self, args, unknown):
9493
return
9594

9695
# Get the contents as a list
97-
contents = os.listdir(self.cwd)
96+
contents = os.listdir(self._cwd)
9897

9998
fmt = '{} '
10099
if args.long:

PyOhio_2019/examples/scripts/conditional_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
directory = 'foobar'
2222
print('Using default directory: {!r}'.format(directory))
2323

24-
# Keep track of where we started
25-
original_dir = cwd
24+
# Keep track of where we started - NOTE: cwd was added to self.py_locals dictionary so we can access it
25+
original_dir = cwd()
2626

2727
# Try to change to the specified directory
2828
cd_result = app('cd {}'.format(directory))

0 commit comments

Comments
 (0)