Skip to content

Commit

Permalink
Remove Property.from_string and Property.to_string (#1529)
Browse files Browse the repository at this point in the history
This PR is to close issue #280.
  • Loading branch information
luca-patrignani authored Feb 25, 2024
1 parent 213e3ce commit 2614ef0
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 94 deletions.
73 changes: 1 addition & 72 deletions dace/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,6 @@
###############################################################################


def set_property_from_string(prop, obj, string, sdfg=None, from_json=False):
""" Interface function that guarantees that a property will always be
correctly set, if possible, by accepting all possible input arguments to
from_string. """

# If the property is a string (property name), obtain it from the object
if isinstance(prop, str):
prop = type(obj).__properties__[prop]

if isinstance(prop, CodeProperty):
if from_json:
val = prop.from_json(string)
else:
val = prop.from_string(string, obj.language)
elif isinstance(prop, (ReferenceProperty, DataProperty)):
if sdfg is None:
raise ValueError("You cannot pass sdfg=None when editing a ReferenceProperty!")
if from_json:
val = prop.from_json(string, sdfg)
else:
val = prop.from_string(string, sdfg)
else:
if from_json:
val = prop.from_json(string, sdfg)
else:
val = prop.from_string(string)
setattr(obj, prop.attr_name, val)


###############################################################################
# Property base implementation
###############################################################################
Expand All @@ -74,8 +45,6 @@ def __init__(
setter=None,
dtype: Type[T] = None,
default=None,
from_string=None,
to_string=None,
from_json=None,
to_json=None,
meta_to_json=None,
Expand Down Expand Up @@ -114,35 +83,8 @@ def __init__(
if not isinstance(choice, dtype):
raise TypeError("All choices must be an instance of dtype")

if from_string is not None:
self._from_string = from_string
elif choices is not None:
self._from_string = lambda s: choices[s]
else:
self._from_string = self.dtype

if to_string is not None:
self._to_string = to_string
elif choices is not None:
self._to_string = lambda val: val.__name__
else:
self._to_string = str

if from_json is None:
if self._from_string is not None:

def fs(obj, *args, **kwargs):
if isinstance(obj, str):
# The serializer does not know about this property, so if
# we can convert using our to_string method, do that here
return self._from_string(obj)
# Otherwise ship off to the serializer, telling it which type
# it's dealing with as a sanity check
return dace.serialize.from_json(obj, *args, known_type=dtype, **kwargs)

self._from_json = fs
else:
self._from_json = lambda *args, **kwargs: dace.serialize.from_json(*args, known_type=dtype, **kwargs)
self._from_json = lambda *args, **kwargs: dace.serialize.from_json(*args, known_type=dtype, **kwargs)
else:
self._from_json = from_json
if self.from_json != from_json:
Expand Down Expand Up @@ -226,7 +168,6 @@ def __set__(self, obj, val):
if (self.dtype is not None and not isinstance(val, self.dtype) and not (val is None and self.allow_none)):
if isinstance(val, str):
raise TypeError("Received str for property {} of type {}. Use "
"dace.properties.set_property_from_string or the "
"from_string method of the property.".format(self.attr_name, self.dtype))
raise TypeError("Invalid type \"{}\" for property {}: expected {}".format(
type(val).__name__, self.attr_name, self.dtype.__name__))
Expand Down Expand Up @@ -296,14 +237,6 @@ def allow_none(self):
def desc(self):
return self._desc

@property
def from_string(self):
return self._from_string

@property
def to_string(self):
return self._to_string

@property
def from_json(self):
return self._from_json
Expand Down Expand Up @@ -853,8 +786,6 @@ def __init__(
getter=None,
setter=None,
default=None,
from_string=None,
to_string=None,
from_json=None,
to_json=None,
unmapped=False, # Don't enforce 1:1 mapping with a member variable
Expand All @@ -867,8 +798,6 @@ def __init__(
setter=setter,
dtype=set,
default=default,
from_string=from_string,
to_string=to_string,
from_json=from_json,
to_json=to_json,
choices=None,
Expand Down
23 changes: 1 addition & 22 deletions dace/sdfg/sdfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,6 @@ def _replace_dict_values(d, old, new):
d[k] = new


def _assignments_from_string(astr):
""" Returns a dictionary of assignments from a semicolon-delimited
string of expressions. """

result = {}
for aitem in astr.split(';'):
aitem = aitem.strip()
m = re.search(r'([^=\s]+)\s*=\s*([^=]+)', aitem)
result[m.group(1)] = m.group(2)

return result


def _assignments_to_string(assdict):
""" Returns a semicolon-delimited string from a dictionary of assignment
expressions. """
return '; '.join(['%s=%s' % (k, v) for k, v in assdict.items()])


def memlets_in_ast(node: ast.AST, arrays: Dict[str, dt.Data]) -> List[mm.Memlet]:
"""
Generates a list of memlets from each of the subscripts that appear in the Python AST.
Expand Down Expand Up @@ -199,9 +180,7 @@ class InterstateEdge(object):
"""

assignments = Property(dtype=dict,
desc="Assignments to perform upon transition (e.g., 'x=x+1; y = 0')",
from_string=_assignments_from_string,
to_string=_assignments_to_string)
desc="Assignments to perform upon transition (e.g., 'x=x+1; y = 0')")
condition = CodeProperty(desc="Transition condition", default=CodeBlock("1"))

def __init__(self, condition: CodeBlock = None, assignments=None):
Expand Down

0 comments on commit 2614ef0

Please sign in to comment.