-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make variables command only show user-defined variables #10
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ def __init__(self, autoPrint=False, splitLines=True, separator=None, | |
self._functions = {} | ||
self._special = {} | ||
self._variables = {} | ||
self._userVariables = [] | ||
|
||
self.addSpecialCases() | ||
addSpecialFunctions(self) | ||
|
@@ -546,6 +547,7 @@ def _tryEvalExec(self, command, modifiers, count): | |
value = EngNumber(command) | ||
except decimal.InvalidOperation: | ||
try: | ||
variables_before = set(self._variables.keys()) | ||
exec(command, globals(), self._variables) | ||
except BaseException as e: | ||
err = str(e) | ||
|
@@ -561,6 +563,13 @@ def _tryEvalExec(self, command, modifiers, count): | |
'whitespace in a command line?') | ||
raise CalculatorError(*errors) | ||
else: | ||
# If we have new variables, add them to the user variables | ||
# list | ||
variables_after = set(self._variables.keys()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for |
||
new_variables = variables_after.difference(variables_before) | ||
for var in new_variables: | ||
self._userVariables.append(var) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are the user variables a |
||
|
||
self.debug('exec(%r) worked.' % command) | ||
return True, self.NO_VALUE | ||
else: | ||
|
@@ -661,26 +670,6 @@ def _findWithArgs(self, command, description, predicate, defaultArgCount, | |
(command, stackLen, '' if stackLen == 1 else 's')) | ||
|
||
if modifiers.reverse: | ||
item = self.stack[-1] | ||
|
||
if not predicate(item): | ||
raise StackError('Top stack item (%r) is not %s' % | ||
(item, description)) | ||
|
||
if count is None: | ||
count = (stackLen - 1 if modifiers.all else | ||
defaultArgCount(item)) | ||
|
||
nargsAvail = stackLen - 1 | ||
if nargsAvail < count: | ||
raise StackError( | ||
'Cannot run %r with %d argument%s ' | ||
'(stack has only %d item%s available)' % | ||
(command, count, '' if count == 1 else 's', | ||
nargsAvail, '' if nargsAvail == 1 else 's')) | ||
|
||
args = self.stack[-(count + 1):-1] | ||
else: | ||
if count is None: | ||
if modifiers.all: | ||
item = self.stack[0] | ||
|
@@ -708,6 +697,26 @@ def _findWithArgs(self, command, description, predicate, defaultArgCount, | |
item, description)) | ||
|
||
args = self.stack[-count:] | ||
else: | ||
item = self.stack[-1] | ||
|
||
if not predicate(item): | ||
raise StackError('Top stack item (%r) is not %s' % | ||
(item, description)) | ||
|
||
if count is None: | ||
count = (stackLen - 1 if modifiers.all else | ||
defaultArgCount(item)) | ||
|
||
nargsAvail = stackLen - 1 | ||
if nargsAvail < count: | ||
raise StackError( | ||
'Cannot run %r with %d argument%s ' | ||
'(stack has only %d item%s available)' % | ||
(command, count, '' if count == 1 else 's', | ||
nargsAvail, '' if nargsAvail == 1 else 's')) | ||
|
||
args = self.stack[-(count + 1):-1] | ||
|
||
return item, self.convertStackArgs(args) | ||
|
||
|
@@ -738,11 +747,12 @@ def defaultArgCount(x): | |
return self._findWithArgs(command, 'a string', predicate, | ||
defaultArgCount, modifiers, count) | ||
|
||
def setVariable(self, variable, value): | ||
def setUserVariable(self, variable, value): | ||
""" | ||
Set the value of a variable. | ||
Set the value of a user-defined variable. | ||
|
||
@param variable: The C{str} variable name. | ||
@param value: The value to give the variable. | ||
""" | ||
self._variables[variable] = value | ||
self._userVariables.append(variable) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ def stack(calc, modifiers, count): | |
stack.names = ('stack', 's', 'f') | ||
|
||
|
||
def variables(calc, modifiers, count): | ||
def variables_all(calc, modifiers, count): | ||
"""Show all variables. | ||
|
||
@param calc: A C{Calculator} instance. | ||
|
@@ -67,8 +67,21 @@ def variables(calc, modifiers, count): | |
return calc.NO_VALUE | ||
|
||
|
||
variables.names = ('variables',) | ||
variables_all.names = ('variables_all',) | ||
|
||
def variables_user(calc, modifiers, count): | ||
"""Show user-defined variables. | ||
|
||
@param calc: A C{Calculator} instance. | ||
@param modifiers: A C{Modifiers} instance. | ||
@param count: An C{int} count of the number of arguments to pass. | ||
""" | ||
for name, value in sorted(calc._variables.items()): | ||
if name in calc._userVariables: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like |
||
calc.report('%s: %r' % (name, value)) | ||
return calc.NO_VALUE | ||
|
||
variables_user.names = ('variables',) | ||
|
||
def clear(calc, modifiers, count): | ||
""" | ||
|
@@ -318,7 +331,7 @@ def store(calc, modifiers, count): | |
else: | ||
value = args | ||
|
||
calc.setVariable(variable, value) | ||
calc.setUserVariable(variable, value) | ||
calc._finalize(None, nPop=len(args) + 1, modifiers=modifiers, noValue=True) | ||
return calc.NO_VALUE | ||
|
||
|
@@ -369,12 +382,13 @@ def map_(calc, modifiers, count): | |
store, | ||
swap, | ||
undo, | ||
variables, | ||
variables_all, | ||
variables_user, | ||
) | ||
|
||
|
||
def addSpecialFunctions(calc): | ||
"""Add functions defined above | ||
"""Add functions defined above. | ||
|
||
@param calc: A C{Calculator} instance. | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the
.keys()
here. Thedict
is being iterated, so you get the keys as a result.Also, could you use camelCase? I know it's a tiny/silly issue, but I'd prefer to keep the code consistent in that respect. Sorry!