>>> from hydro_class import HYDRO >>> hyd = HYDRO() >>> hyd.ncells = 20 # This actually changes state # in the C++ code # by using __setattr__ >>> hyd.initialize() >>> hyd.cycle() >>> hyd.density()
def fact(n): if n == 0: return 1 else: return n*fact(n-1)
#DECLARE long fact(long)
class AnyClass: """ All data in an instance of this class is contained in a single attribute ("data") -- this may be as complex as it needs to be; for example a python dictionary which contains dictionaries and other native types""" def __init__(self): execdict = {} execfile('some_file', globals(), execdict) self.data = execdict['data'] def save(self): file = open('some_file', 'w') file.write('data = %s' % 'self.data') file.close() delf __repr__(self): return 'self.data'
# A class may have more than one data attribute, and # these need not be python native types, but could be # instances of developer created classes similar to the # sample 'AnyClass' presented above class OtherClass: def __init__(self, data1, data2): execdict = {} execfile('file', globals(), execdict) self.data1 == data1 self.data2 = AnyClass(data2) def save(self, file_name) file = open(file_name, 'w') file.write('instance= OtherClass(%s, %s)' % ('data1', 'data2')) file.close() def load_OtherClass_instance(file_name): execdict = {} execfile(file_name, globals(), execdict) return execdict['instance']
def execute_command(command_line): words = split(command_line) command_name = words[0] args = words[1:] execdict = {'arguments': args } execfile(command_name + '.py', globals(), execdict)
def execute_command(line_list): execdict = {} for line in line_list: words = split(line) command_name = words[0] execdict['arguments'] = words[1:] execfile(command_name + '.py', globals(), execdict)
# Code from an auxiliary ("command") file. This will # receive "arguments" in its local variable dictionary # when exectuted. There may also be other variables # in this dictionary. Let us assume that whatever this # command does, it requires that a data object named # AnyObject (an instance perhaps of our example # class AnyClass) is available for word in arguments: execdict = {'Object': AnyObject} execfile(word + '.py', globals(), execdict)
# The code below is from a command implementation file # This command expects "arguments" and a loaded-up # AnyClass object named "AnyObject". The function # of this command is to add a "hook" so that when some # other command attempts to manipulate the object in # some way, an extra check will be made. AnyObject.add_hook('if self.some_method() : return 0') class AnyClass: # remember this class houses a single dictionary # attribute named "data" def add_hook(self, code): self.data['hook'].append(code) def some_processing_function(self): exec self.data['hook'] ...some AnyClass functionality... return 1
double MyVar -- MyVar_get(), MyVar_set()