Skip to content

Commit

Permalink
added parentheses around expressions by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjaaron committed Jun 20, 2018
1 parent f78e1ce commit ab84a50
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ usage
expression [expression ...]
positional arguments:
``expression`` expression(s) to be executed. If multiple expression
arguments are given, and --exec is not used, the value
of the previous expression is available as 'x' in the
following expression. if --exec is used, all
assignment must be explicit.
expression expression(s) to be executed. If multiple expression
arguments are given, and --exec is not used, the value
of the previous expression is available as 'x' in the
following expression. if --exec is used, all
assignment must be explicit.

optional arguments:
-h, --help show this help message and exit
Expand All @@ -77,6 +77,8 @@ optional arguments:
-q, --quiet suppress automatic printing. doesn't affect --post
-j, --json load stdin as json into object 'j'; If used with
--loop, treat each line of stdin as a new object
-J, --real-dict-json like -j, but creates real dictionaries instead of the
wrapper that allows dot syntax.
-o, --force-oneline-json
outside of loops and iterators, objects serialzed to
json print with two-space indent. this forces this
Expand Down
15 changes: 12 additions & 3 deletions pyfil.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def run(expressions, args, namespace={}):
if not (args.quiet or args.exec):
if args.join is not None and isinstance(value, collections.Iterable):
print(ast.literal_eval("'''" + args.join.replace("'", r"\'") +
"'''").join(map(str, value)))
"'''").join(map(str, value)))
elif value is None:
pass
elif isinstance(value, collections.Iterator):
Expand Down Expand Up @@ -196,6 +196,10 @@ def main():
help="load stdin as json into object 'j'; If used with "
'--loop, treat each line of stdin as a new object')

ap.add_argument('-J', '--real-dict-json', action='store_true',
help='like -j, but creates real dictionaries instead of '
'the wrapper that allows dot syntax.')

ap.add_argument('-o', '--force-oneline-json', action='store_true',
help='outside of loops and iterators, objects serialzed '
'to json print with two-space indent. this forces '
Expand Down Expand Up @@ -237,7 +241,9 @@ def main():
a = ap.parse_args()

func = 'exec' if a.exec else 'eval'
expressions = [compile(e, '<string>', func) for e in a.expression]
expressions = [compile(
e if a.exec else "(%s)" % e,
'<string>', func) for e in a.expression]
user_env = os.environ['HOME'] + '/.config/pyfil-env.py'

namespace = NameSpace(__builtins__)
Expand All @@ -247,6 +253,9 @@ def main():

if a.json:
jdecode = json.JSONDecoder(object_hook=LazyDict).decode
elif a.real_dict_json:
jdecode = json.loads
a.json = True

if a.post or a.split or a.field_sep:
a.loop = True
Expand All @@ -272,7 +281,7 @@ def main():
if a.post:
if a.quiet or a.exec:
a.loop, a.quiet, a.exec = None, None, None
run((a.post,), a, namespace)
run(('(%s)' % a.post,), a, namespace)

else:
if a.pre:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='pyfil',
version='1.5',
version='1.7',
author='Aaron Christianson',
license='BSD',
author_email='ninjaaron@gmail.com',
Expand Down

0 comments on commit ab84a50

Please sign in to comment.