In [7]:
%pylab inline
#import pysd
#print pysd.__version__
#print pysd.__file__
#import pandas as pd
#import os
import xarray as xr
import numpy as np
In [1]:
class Components(object):
"""
This class implements the 'components' namespace for a Model object,
which allows the model to have a namespace of its own with no additional
functions that may conflict with model names.
Implementing this as a class rather than a module allows this components
namespace to have access to its containing namespaces to access things
like the universal time, or functions defined externally
"""
def __init__(self, py_model_file):
from functions import cache # this will be used in the call to exec
import functions
#component_ns = dict()
#func_globals = globals().copy()
#func_globals.update(locals())
with open(py_model_file) as f:
code = compile(f.read(), py_model_file, 'exec')
#exec(code, func_globals, component_ns)
exec(code, globals(), locals())
#exec(code)
self.__dict__.update(locals())
In [2]:
my_ns = {}
In [56]:
def f():
return 5
a = lambda: f
In [59]:
a()
Out[59]:
In [8]:
def g():
print 5
In [9]:
def f():
exec('g()', globals(), my_ns)
In [11]:
a = {'hi':5}
b = {'number':1}
In [53]:
my_ns = dict()
code_str = """
def f1():
return self.f2()
def f2():
return 5
"""
code = compile(code_str, 'dummy', 'exec')
exec (code, globals(), my_ns)
In [54]:
class Bunch:
def __init__(self, ns):
self.__dict__.update(ns)
a = Bunch(my_ns)
In [55]:
a.f1()
In [47]:
my_ns['f1']()
In [14]:
z = a.copy()
z.update(b)
z
Out[14]:
In [1]:
in_file = "tests/test-models/tests/macro_expression/expression_macro.py"
my_ns = dict()
with open(in_file) as f:
code = compile(f.read(), in_file, 'exec')
exec(code, globals(), my_ns)
In [13]:
! cat $in_file
In [6]:
my_ns['expression_macro']()
In [7]:
class Bunch:
def __init__(self, ns):
self.__dict__.update(ns)
In [11]:
_t = 5
In [8]:
a = Bunch(my_ns)
In [12]:
a.time()
Out[12]:
In [1]:
import pysd
In [2]:
pysd.__version__
Out[2]:
In [3]:
model = pysd.read_vensim('tests/test-models/samples/teacup/teacup.mdl')
In [4]:
model.doc()
Out[4]:
In [5]:
def myfunc():
return [func() for func in myfunc.subfuncs]
myfunc.subfuncs = []
myfunc.subfuncs.append(lambda: 5)
myfunc.subfuncs.append(lambda: 6)
In [6]:
myfunc()
Out[6]:
In [13]:
a = [xr.DataArray(range(3)), xr.DataArray(range(5))]
a
Out[13]:
In [14]:
b = np.array(a)
b
Out[14]:
In [16]:
c = b*2
c
Out[16]:
In [ ]:
order = 3
init =
In [18]:
d = np.roll(b, 1)
d[0] = xr.DataArray(range(2))
d
Out[18]:
In [19]:
getattr(a, 'hi')
In [20]:
a.update()
In [ ]: