def get_default_args_dict(function): ''' Get ordered dict from arguments which have a default to their default. Example: >>> def f(a, b, c=1, d='meow'): pass >>> get_default_args_dict(f) OrderedDict([('c', 1), ('d', 'meow')]) ''' arg_spec = cute_inspect.getargspec(function) (s_args, s_star_args, s_star_kwargs, s_defaults) = arg_spec # `getargspec` has a weird policy, when inspecting a function with no # defaults, to give a `defaults` of `None` instead of the more consistent # `()`. We fix that here: if s_defaults is None: s_defaults = () # The number of args which have default values: n_defaultful_args = len(s_defaults) defaultful_args = s_args[-n_defaultful_args:] if n_defaultful_args \ else [] return OrderedDict(zip(defaultful_args, s_defaults))
def __init__(self, containing_dict, function, *args, **kwargs): ''' Construct the `SleekCallArgs`. `containing_dict` is the `dict` we'll try to remove ourselves from when one of our sleekrefs dies. `function` is the function for which we calculate call args from `*args` and `**kwargs`. ''' self.containing_dict = containing_dict ''' `dict` we'll try to remove ourselves from when 1 of our sleekrefs dies. ''' args_spec = cute_inspect.getargspec(function) star_args_name, star_kwargs_name = \ args_spec.varargs, args_spec.keywords call_args = cute_inspect.getcallargs(function, *args, **kwargs) del args, kwargs self.star_args_refs = [] '''Sleekrefs to star-args.''' if star_args_name: star_args = call_args.pop(star_args_name, None) if star_args: self.star_args_refs = [SleekRef(star_arg, self.destroy) for star_arg in star_args] self.star_kwargs_refs = {} '''Sleerefs to star-kwargs.''' if star_kwargs_name: star_kwargs = call_args.pop(star_kwargs_name, {}) if star_kwargs: self.star_kwargs_refs = CuteSleekValueDict(self.destroy, star_kwargs) self.args_refs = CuteSleekValueDict(self.destroy, call_args) '''Mapping from argument name to value, sleek-style.''' # In the future the `.args`, `.star_args` and `.star_kwargs` attributes # may change, so we must record the hash now: self._hash = cheat_hashing.cheat_hash( ( self.args, self.star_args, self.star_kwargs ) )
def __init__(self, containing_dict, function, *args, **kwargs): ''' Construct the `SleekCallArgs`. `containing_dict` is the `dict` we'll try to remove ourselves from when one of our sleekrefs dies. `function` is the function for which we calculate call args from `*args` and `**kwargs`. ''' self.containing_dict = containing_dict ''' `dict` we'll try to remove ourselves from when 1 of our sleekrefs dies. ''' args_spec = cute_inspect.getargspec(function) star_args_name, star_kwargs_name = \ args_spec.varargs, args_spec.keywords call_args = cute_inspect.getcallargs(function, *args, **kwargs) del args, kwargs self.star_args_refs = [] '''Sleekrefs to star-args.''' if star_args_name: star_args = call_args.pop(star_args_name, None) if star_args: self.star_args_refs = [ SleekRef(star_arg, self.destroy) for star_arg in star_args ] self.star_kwargs_refs = {} '''Sleerefs to star-kwargs.''' if star_kwargs_name: star_kwargs = call_args.pop(star_kwargs_name, {}) if star_kwargs: self.star_kwargs_refs = CuteSleekValueDict( self.destroy, star_kwargs) self.args_refs = CuteSleekValueDict(self.destroy, call_args) '''Mapping from argument name to value, sleek-style.''' # In the future the `.args`, `.star_args` and `.star_kwargs` attributes # may change, so we must record the hash now: self._hash = cheat_hashing.cheat_hash( (self.args, self.star_args, self.star_kwargs))
def assert_same_signature(*callables): '''Assert that all the `callables` have the same function signature.''' arg_specs = [cute_inspect.getargspec(callable_) for callable_ in callables] if not logic_tools.all_equivalent(arg_specs, assume_transitive=False): raise Failure('Not all the callables have the same signature.')