Skip to content

Commit

Permalink
@dynamic decorator for methods used in pattern constraints and simu…
Browse files Browse the repository at this point in the history
…lation
  • Loading branch information
johnsekar committed Feb 7, 2019
1 parent bf0ade0 commit 3d80ce6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class X(BaseClass):
pi = PositiveIntegerAttribute()
s = StringAttribute()

@dynamic
def product(f,i,factor=1):
return f*i*factor


class TestAttributes(unittest.TestCase):

Expand All @@ -22,3 +26,12 @@ def test_attribute_defaults(self):
self.assertEqual(x1.i,None)
self.assertEqual(x1.pi,None)
self.assertEqual(x1.s,None)

def test_dynamic_decorator(self):
x1 = X(id='idx1',b=True,f=0.5,i=100)

self.assertTrue(x1.product._isdynamic)
self.assertEqual(x1.product(),50)
self.assertEqual(x1.product(factor=3),150)
self.assertEqual(x1.product(f=0.25,factor=3),75)
self.assertEqual(x1.product(f=0.25,i=10,factor=3),7.5)
13 changes: 13 additions & 0 deletions wc_rules/attributes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from obj_model import core
from obj_model import bio
from functools import wraps

# scalar attributes
class IdAttribute(core.StringAttribute):
Expand Down Expand Up @@ -45,3 +46,15 @@ class ManyToManyAttribute(core.ManyToManyAttribute):pass
class BioSeqAttribute(bio.BioSeqAttribute):
def __init__(self):
super().__init__(default=None)

# wrapper for methods that can be called for setting pattern constraints & during simulation
def dynamic(fn):
fnvars = fn.__code__.co_varnames
@wraps(fn)
def outer(self_obj,**kwargs):
populate_this = [x for x in fnvars if x in self_obj.get_literal_attrs() and x not in kwargs]
for var in populate_this:
kwargs[var] = getattr(self_obj,var)
return fn(**kwargs)
outer._isdynamic = True
return outer
1 change: 0 additions & 1 deletion wc_rules/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def finalize(self):
# get reference & defined dependencies
compiled_deps = self.validate_dependencies()


# here we build a networkx graph to evaluate symmetry
g,helper = build_simple_graph(self)

Expand Down

0 comments on commit 3d80ce6

Please sign in to comment.