def test_locally_defined_class(): ########################################################################### # Testing for locally defined class: if garlicsim.__version_info__ <= (0, 6, 3): raise nose.SkipTest("This test doesn't pass in `garlicsim` version " "0.6.3 and below, because `describe` doesn't " "support nested classes yet.") result = describe(A.B) assert result == prefix + 'A.B' assert resolve(result) is A.B result = describe(A.C.D.deeper_method) assert result == prefix + 'A.C.D.deeper_method' assert resolve(result) == A.C.D.deeper_method result = describe(A.C.D.deeper_method, root=A.C) assert result == 'C.D.deeper_method' assert resolve(result, root=A.C) == A.C.D.deeper_method result = describe(A.C.D.deeper_method, root='A.C.D') assert result == 'D.deeper_method' assert resolve(result, root='A.C.D') == A.C.D.deeper_method
def test_garlicsim(): '''Test `resolve` on `garlicsim` modules.''' result = resolve('garlicsim.general_misc') import garlicsim assert garlicsim.general_misc is result result = resolve('garlicsim.general_misc.persistent.' 'cross_process_persistent.' 'CrossProcessPersistent.personality') result2 = \ resolve('general_misc.persistent.CrossProcessPersistent.personality', namespace=garlicsim) result3 = resolve('persistent.CrossProcessPersistent.personality', root=garlicsim.general_misc.persistent, namespace='email') assert result is result2 is result3 is garlicsim.general_misc.persistent.\ cross_process_persistent.CrossProcessPersistent.personality result = resolve('data_structures.end.End', root=garlicsim.data_structures) result2 = resolve('data_structures.End', root=garlicsim.data_structures) result3 = resolve('data_structures.End', namespace='garlicsim') assert result is result2 is garlicsim.data_structures.end.End import email assert resolve('garlicsim', namespace={'e': email})
def test_local_modules(): '''Test `describe` on local, relatively-imported modules.''' import garlicsim from .sample_module_tree import w z = resolve('w.x.y.z', root=w) result = describe(z, root=w) assert result == 'w.x.y.z' result = describe(z, shorten=True, root=w) assert result == 'w.y.z' result = describe(z, shorten=True, root=w) assert result == 'w.y.z' result = describe(z, shorten=True, root=w, namespace='email') assert result == 'w.y.z' result = describe(z, shorten=True, root=garlicsim, namespace=w) assert result == 'y.z' result = describe(z, shorten=True, root=w.x) assert result == 'x.y.z'
def __init__(self, function): ''' Construct the `TempFunctionCallCounter`. For `function`, you may pass in either a function object, or a `(parent_object, function_name)` pair, or a `(getter, setter)` pair. ''' if cute_iter_tools.is_iterable(function): first, second = function if isinstance(second, basestring): actual_function = getattr(first, second) else: assert callable(first) and callable(second) actual_function = first( ) # `first` is the getter in this case. else: # not cute_iter_tools.is_iterable(function) assert callable(function) actual_function = function try: address = address_tools.object_to_string.get_address(function) parent_object_address, function_name = address.rsplit('.', 1) parent_object = address_tools.resolve(parent_object_address) except Exception: raise Exception("Couldn't obtain parent/name pair from " "function; supply one manually or " "alternatively supply a getter/setter pair.") first, second = parent_object, function_name self.call_counting_function = count_calls(actual_function) TempValueSetter.__init__(self, (first, second), value=self.call_counting_function)
def resolve(self, address): '''Resolve `address` into a Python object.''' return address_tools.resolve( address, root=self.simpack, namespace=self.gui_project.namespace )
def update_simpack_list(self): '''Update the list of available simpacks.''' self.list_of_simpacks = [] for path, package_prefix in garlicsim_wx.simpack_places: if path not in sys.path: sys.path.append(path) if package_prefix: assert package_prefix[-1] == '.' package = address_tools.resolve(package_prefix[:-1]) path_to_search = path_tools.get_path_of_package(package) else: # not package_prefix path_to_search = path list_of_simpacks_in_simpack_place = [ (package_prefix + package_name[1:]) for package_name in package_finder.get_packages(path_to_search, self_in_name=False) ] list_of_simpacks_in_simpack_place.sort(cmp=underscore_hating_cmp) self.list_of_simpacks += list_of_simpacks_in_simpack_place self.list_box.SetItems(self.list_of_simpacks)
def test_garlicsim(): '''Test `describe` for various `garlicsim` modules.''' import garlicsim result = describe(garlicsim.data_structures.state.State) assert result == 'garlicsim.data_structures.state.State' assert resolve(result) is garlicsim.data_structures.state.State result = describe(garlicsim.data_structures.state.State, shorten=True) assert result == 'garlicsim.data_structures.State' assert resolve(result) is garlicsim.data_structures.state.State result = describe(garlicsim.Project, shorten=True) assert result == 'garlicsim.Project' assert resolve(result) is garlicsim.Project # When a root or namespace is given, it's top priority to use it, even if # it prevents shorterning and results in an overall longer address: result = describe(garlicsim.Project, shorten=True, root=garlicsim.asynchronous_crunching) assert result == 'asynchronous_crunching.Project' assert resolve(result, root=garlicsim.asynchronous_crunching) is \ garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace=garlicsim) assert result == 'Project' assert resolve(result, namespace=garlicsim) is garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace=garlicsim.__dict__) assert result == 'Project' assert resolve(result, namespace=garlicsim.__dict__) is \ garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace='garlicsim') assert result == 'Project' assert resolve(result, namespace='garlicsim') is garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace='garlicsim.__dict__') assert result == 'Project' assert resolve(result, namespace='garlicsim.__dict__') is \ garlicsim.Project result = describe(garlicsim.data_structures.state.State, root=garlicsim) assert result == 'garlicsim.data_structures.state.State' assert resolve(result, root=garlicsim) is \ garlicsim.data_structures.state.State
def test_address_in_expression(): result = resolve('[object, email.encoders, marshal]') import email, marshal, garlicsim assert result == [object, email.encoders, marshal] assert resolve('[email.encoders, 7, (1, 3), marshal]') == \ [email.encoders, 7, (1, 3), marshal] result = resolve('{email: marshal, object: 7, garlicsim: garlicsim}') import garlicsim assert result == {email: marshal, object: 7, garlicsim: garlicsim} assert resolve('{email: marshal, object: 7, garlicsim: garlicsim}') == \ {email: marshal, object: 7, garlicsim: garlicsim} assert resolve('{Project: simulate}', namespace=garlicsim) == \ {garlicsim.Project: garlicsim.simulate} assert resolve('{asynchronous_crunching.Project: simulate}', root=garlicsim.asynchronous_crunching, namespace=garlicsim) == \ {garlicsim.asynchronous_crunching.Project: garlicsim.simulate} assert resolve('garlicsim if 4 else e', namespace={'e': email}) is \ garlicsim
def test_stdlib(): '''Test `describe` for various stdlib modules.''' import email.encoders result = describe(email.encoders) assert result == 'email.encoders' assert resolve(result) is email.encoders result = describe(email.encoders, root=email.encoders) assert result == 'encoders' assert resolve(result, root=email.encoders) is email.encoders result = describe(email.encoders, namespace=email) assert result == 'encoders' assert resolve(result, namespace=email) is email.encoders result = describe(email.encoders, root=email.encoders, namespace=email) assert result == 'encoders' assert resolve(result, root=email.encoders, namespace=email) is \ email.encoders
def _guess_simpack(self): '''`try` to guess the simpack that this step profile belongs to.''' try: module = \ address_tools.resolve(self.step_function.__module__) except Exception: return None else: if hasattr(module, 'State'): if issubclass(module.State, garlicsim.data_structures.State): return garlicsim.misc.simpack_tools.\ _get_from_state_class(module.State)
def test_function_in_main(): '''Test that a function defined in `__main__` is well-described.''' ########################################################################### # We can't really define a function in `__main__` in this test, so we # emulate it: with TempValueSetter((globals(), '__name__'), '__main__'): def f(x): pass assert f.__module__ == '__main__' import __main__ __main__.f = f del __main__ # ########################################################################### assert describe(f) == '__main__.f' assert resolve(describe(f)) is f
def __init__(self, function): ''' Construct the `TempFunctionCallCounter`. For `function`, you may pass in either a function object, or a `(parent_object, function_name)` pair, or a `(getter, setter)` pair. ''' if isinstance(function, collections.Iterable): first, second = function if isinstance(second, str): actual_function = getattr(first, second) else: assert isinstance(first, collections.Callable) and \ isinstance(second, collections.Callable) actual_function = first() # `first` is the getter in this case. else: # not isinstance(function, collections.Iterable) assert isinstance(function, collections.Callable) actual_function = function try: address = address_tools.object_to_string.get_address(function) parent_object_address, function_name = address.rsplit('.', 1) parent_object = address_tools.resolve(parent_object_address) except Exception: raise Exception("Couldn't obtain parent/name pair from " "function; supply one manually or " "alternatively supply a getter/setter pair.") first, second = parent_object, function_name self.call_counting_function = count_calls(actual_function) TempValueSetter.__init__( self, (first, second), value=self.call_counting_function )
def test_garlicsim(): '''Test `describe` for various `garlicsim` modules.''' import garlicsim result = describe(garlicsim.data_structures.state.State) assert result == 'garlicsim.data_structures.state.State' assert resolve(result) is garlicsim.data_structures.state.State result = describe(garlicsim.data_structures.state.State, shorten=True) assert result == 'garlicsim.data_structures.State' assert resolve(result) is garlicsim.data_structures.state.State result = describe(garlicsim.Project, shorten=True) assert result == 'garlicsim.Project' assert resolve(result) is garlicsim.Project # When a root or namespace is given, it's top priority to use it, even if # it prevents shorterning and results in an overall longer address: result = describe(garlicsim.Project, shorten=True, root=garlicsim.asynchronous_crunching) assert result == 'asynchronous_crunching.Project' assert resolve(result, root=garlicsim.asynchronous_crunching) is \ garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace=garlicsim) assert result == 'Project' assert resolve(result, namespace=garlicsim) is garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace=garlicsim.__dict__) assert result == 'Project' assert resolve(result, namespace=garlicsim.__dict__) is \ garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace='garlicsim') assert result == 'Project' assert resolve(result, namespace='garlicsim') is garlicsim.Project result = describe(garlicsim.Project, shorten=True, namespace='garlicsim.__dict__') assert result == 'Project' assert resolve(result, namespace='garlicsim.__dict__') is \ garlicsim.Project result = describe(garlicsim.data_structures.state.State, root=garlicsim) assert result == 'garlicsim.data_structures.state.State' assert resolve(result, root=garlicsim) is \ garlicsim.data_structures.state.State import garlicsim_lib.simpacks.life result = describe(garlicsim_lib.simpacks.life.state.State.step) assert result == 'garlicsim_lib.simpacks.life.state.State.step' result = describe(garlicsim_lib.simpacks.life.state.State.step, shorten=True) assert result == 'garlicsim_lib.simpacks.life.State.step' result = describe(garlicsim_lib.simpacks.life.state.State.step, root=garlicsim_lib.simpacks.life) assert result == 'life.state.State.step' result = describe(garlicsim_lib.simpacks.life.state.State.step, namespace=garlicsim_lib.simpacks) assert result == 'life.state.State.step' result = describe(garlicsim_lib.simpacks.life.state.State.step, root=garlicsim_lib.simpacks.life, shorten=True) assert result == 'life.State.step'
def test_stdlib(): '''Test `resolve` on stdlib modules.''' result = resolve('email') import email import marshal assert result is email assert resolve('email') is \ resolve('email.email') is \ resolve('email.email.email') is \ resolve('email.email.email.email') is email result = resolve('email.base64mime.a2b_base64') assert result is email.base64mime.a2b_base64 result = resolve('email.email.encoders.base64.b32decode') assert result is email.encoders.base64.b32decode result = resolve('base64.b32decode', root='email.email.encoders.base64') assert result is email.encoders.base64.b32decode result = resolve('base64.b32decode', namespace='email.email.encoders') assert result is email.encoders.base64.b32decode result = resolve('base64.b32decode', root=marshal, namespace='email.email.encoders') assert result is email.encoders.base64.b32decode assert resolve('object') is object
def test_locally_defined_class(): '''Test `resolve` on a locally defined class tree.''' assert resolve(prefix + 'A') is A assert resolve(prefix + 'A.B') is A.B assert resolve(prefix + 'A.method') == A.method assert resolve('method', namespace=A) == A.method assert resolve(prefix + 'A.B.deep_method') == A.B.deep_method assert resolve('B.deep_method', namespace=A) == A.B.deep_method assert resolve(prefix + 'A.C.D') is A.C.D assert resolve(prefix + 'A.C.D.deeper_method') == \ A.C.D.deeper_method assert resolve('D.deeper_method', root=(prefix + 'A.C.D')) == \ A.C.D.deeper_method assert resolve('D.deeper_method', root=A.C.D, namespace='email') == \ A.C.D.deeper_method assert resolve('A', root=A) == A
def resolve(self, address): '''Resolve `address` into a Python object.''' return address_tools.resolve(address, root=self.simpack, namespace=self.gui_project.namespace)
def test_stdlib(): '''Test `resolve` on stdlib modules.''' result = resolve('email') import email email.email = email # That's how it's like in Python 2.x. import marshal assert result is email assert resolve('email') is \ resolve('email.email') is \ resolve('email.email.email') is \ resolve('email.email.email.email') is email result = resolve('email.base64mime.a2b_base64') assert result is email.base64mime.a2b_base64 result = resolve('email.email.encoders.encode_base64') assert result is email.encoders.encode_base64 result = resolve('base64mime.b2a_base64', root='email.email') assert result is email.base64mime.b2a_base64 result = resolve('base64.b32decode', namespace='email.email.encoders') import base64 assert result is base64.b32decode result = resolve('base64.b32decode', root=marshal, namespace='email.email.encoders') assert result is base64.b32decode assert resolve('object') is object