Skip to content

Commit

Permalink
add Group() function to nested symbol table
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Sep 10, 2024
1 parent 2cad627 commit 6a218da
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions asteval/astutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,6 @@ class Group(dict):
"""
Group: a container of objects that can be accessed either as an object attributes
or dictionary key/value. Attribute names must follow Python naming conventions.
"""
def __init__(self, name=None, searchgroups=None, **kws):
if name is None:
Expand All @@ -408,9 +406,10 @@ def __getattr__(self, name, default=None):
raise KeyError(f"no attribute named '{name}'")

def __setitem__(self, name, value):
if not valid_varname(name):
raise SyntaxError(f"invalid attribute name '{name}'")
dict.__setitem__(self, name, value)
if valid_varname(name):
dict.__setitem__(self, name, value)
else: # raise SyntaxError(f"invalid attribute name '{name}'")
return setattr(self, name, value)

def get(self, key, default=None):
val = self.__getattr__(key, ReturnedNone)
Expand Down Expand Up @@ -470,7 +469,7 @@ def make_symbol_table(use_numpy=True, nested=False, top=True, **kws):
name = '_main'
if 'name' in kws:
name = kws.pop('name')
symtable = Group(name=name)
symtable = Group(name=name, Group=Group)
else:
symtable = {}

Expand All @@ -483,6 +482,7 @@ def make_symbol_table(use_numpy=True, nested=False, top=True, **kws):

if nested:
symtable['math'] = Group(name='math', **math_functions)
symtable['Group'] = Group
symtable._searchgroups = ('math',)
else:
symtable.update(math_functions)
Expand Down

0 comments on commit 6a218da

Please sign in to comment.