11# -*- coding: utf-8 -*-
2- # Wrappers around _moose.so classes.
3-
42from __future__ import print_function , division , absolute_import
53
6- __author__ = "Dilawar Singh"
7- __copyright__ = "Copyright 2019-, Dilawar Singh"
8- __maintainer__ = "Dilawar Singh"
9- 4+ # Wrappers around _moose.so classes.
5+
6+ __author__ = "Dilawar Singh"
7+ __copyright__ = "Copyright 2019-, Dilawar Singh"
8+ __maintainer__ = "Dilawar Singh"
9+ 1010
11- import sys
1211import difflib
1312import moose ._moose as _moose
1413import pprint
@@ -35,14 +34,16 @@ def _didYouMean(v, options):
3534 """
3635 return ' or ' .join (difflib .get_close_matches (v , list (options ), n = 2 ))
3736
37+
3838def _eval (expr ):
3939 try :
40- if isinstance (expr , (int ,float )):
40+ if isinstance (expr , (int , float )):
4141 return expr
4242 return eval (str (expr ))
4343 except Exception :
4444 return expr
4545
46+
4647def _prettifyExpr (expr ):
4748 global sympyFound_
4849 if not sympyFound_ :
@@ -55,19 +56,23 @@ def _prettifyExpr(expr):
5556
5657# Generic wrapper around moose.Neutral
5758class Neutral (_moose .Neutral ):
58-
59- def __init__ (self , path , n = 1 , g = 0 , dtype = 'Neutral' ):
59+ def __init__ (self , path , n = 1 , g = 0 , dtype = 'Neutral' , ** kwargs ):
6060 super (_moose .Neutral , self ).__init__ (path , n , g , dtype )
61+ for k in kwargs :
62+ try :
63+ setattr (self , k , kwargs [k ])
64+ except AttributeError :
65+ logging .warn ("Attribute %s is not found. Ignoring..." % k )
6166
62- def __new__ (cls , pathOrObject , n = 1 , g = 0 , dtype = 'Neutral' ):
67+ def __new__ (cls , pathOrObject , n = 1 , g = 0 , dtype = 'Neutral' , ** kwargs ):
6368 path = pathOrObject
6469 if not isinstance (pathOrObject , str ):
65- path = pathOrObject .path ;
70+ path = pathOrObject .path
6671 if _moose .exists (path ):
6772 # logger.info("%s already exists. Returning old element."%path)
6873 return _moose .element (path )
69- return super (_moose .Neutral , cls ).__new__ (cls , pathOrObject , n , g , dtype )
70-
74+ return super (_moose .Neutral , cls ).__new__ (cls , pathOrObject , n , g ,
75+ dtype )
7176
7277 def connect (self , srcField , dest , destField ):
7378 """Wrapper around moose.connect.
@@ -82,7 +87,8 @@ def connect(self, srcField, dest, destField):
8287 if dym :
8388 logger .warn ("\t Did you mean %s?" % dym )
8489 else :
85- logger .warn (': Available options: %s' % ', ' .join (allSrcFields ))
90+ logger .warn (': Available options: %s' %
91+ ', ' .join (allSrcFields ))
8692 raise NameError ("Failed to connect" )
8793 if destField not in allDestFields :
8894 logger .error ("Could not find '{0}' in {1} destFields." .format (
@@ -91,7 +97,8 @@ def connect(self, srcField, dest, destField):
9197 if dym :
9298 logger .warn ("\t Did you mean %s?" % dym )
9399 else :
94- logger .warn (': Available options: %s' % ', ' .join (allDestFields ))
100+ logger .warn (': Available options: %s' %
101+ ', ' .join (allDestFields ))
95102 raise NameError ("Failed to connect" )
96103
97104 # Everything looks fine. Connect now.
@@ -108,11 +115,13 @@ class Function(_moose.Function):
108115 def __init__ (self , path , n = 1 , g = 0 , dtype = 'Function' , ** kwargs ):
109116 super (_moose .Function , self ).__init__ (path , n , g , dtype )
110117 for k in kwargs :
111- setattr (self , k , kwargs [k ])
118+ try :
119+ setattr (self , k , kwargs [k ])
120+ except AttributeError :
121+ logging .warn ("Attribute %s is not found. Ignoring..." % k )
112122
113123 def __getitem__ (self , key ):
114- """Override [] operator.
115- FIXME: This function is tricky. Fix it later.
124+ """Override [] operator. It returns the linked variable by name.
116125 """
117126 assert self .numVars > 0
118127 return self .x [self .xindex [key ]]
@@ -122,11 +131,13 @@ def compile(self, expr, constants={}, variables=[], mode=0, **kwargs):
122131 """
123132 __expr = expr
124133 # Replace constants.
125- constants = { k : v for k , v in
126- sorted (constants .items (), key = lambda x : len (x )) }
134+ constants = {
135+ k : v
136+ for k , v in sorted (constants .items (), key = lambda x : len (x ))
137+ }
127138 for i , constName in enumerate (constants ):
128139 # replace constName with 'c%d' % i
129- mooseConstName = 'c%d' % i
140+ mooseConstName = 'c%d' % i
130141 expr = expr .replace (constName , mooseConstName )
131142 self .c [mooseConstName ] = _eval (constants [constName ])
132143
@@ -141,7 +152,7 @@ def compile(self, expr, constants={}, variables=[], mode=0, **kwargs):
141152 msg += _prettifyExpr (__expr )
142153 msg += "\n to, \n "
143154 msg += _prettifyExpr (expr )
144- logging .warn ( msg .replace ('\n ' , '\n ﹅ ' ))
155+ logging .warn (msg .replace ('\n ' , '\n ﹅ ' ))
145156
146157 # alias
147158 setExpr = compile
@@ -155,3 +166,15 @@ def printUsingSympy(self):
155166 """
156167 import sympy
157168 sympy .pprint (self .sympyExpr ())
169+
170+
171+ class StimulusTable (Neutral ):
172+ """StimulusTable
173+ """
174+ def __init__ (self , path , n = 1 , g = 0 , dtype = 'StimulusTable' , ** kwargs ):
175+ super (Neutral , self ).__init__ (path , n , g , dtype )
176+ for k in kwargs :
177+ try :
178+ setattr (self , k , kwargs [k ])
179+ except AttributeError :
180+ logging .warn ("Attribute %s is not found. Ignoring..." % k )
0 commit comments