| |
- builtins.ValueError(builtins.Exception)
-
- ArgDefError
- builtins.object
-
- Arg
- ArgExtra
- ArgList
- SequenceOf
-
- ListOf
- TupleOf
- Wrapped
class Arg(builtins.object) |
|
Arg(name=None, index=None, type=None, default=<object object at 0x109341e60>, optional=None, description=None)
Arg: represents one argument in an ArgList.
Arg(name=..., index=...,
type=..., default=..., optional=...,
description=...) -- constructor
All of these values are optional. Note that if you construct an
ArgList with Args in it, their index or name values will be set
automatically.
If you specify a default, that implies optional=True. It also implies
type=infer_type(defaultvalue). However, both of these may be
overridden by explicit values.
Static methods:
from_node() -- construct an Arg from an S-expression
Fields: (any of these may be None to indicate an unspecified value)
name -- str or unicode
index -- int
type -- a type object (int, str, etc.), or a class object (Sample or
Agent), or a Wrapped instance.
optional -- bool
description -- str or unicode
default -- anything
hasdefault -- bool; this exists to distinguish a None default from
an unspecified one.
Public methods:
clone() -- construct an Arg identical to this one
to_node() -- construct an S-expression from this Arg
absorb() -- merge the attributes of another Arg into this one |
|
Methods defined here:
- __init__(self, name=None, index=None, type=None, default=<object object>, optional=None, description=None)
- Initialize self. See help(type(self)) for accurate signature.
- __repr__(self)
- Return repr(self).
- absorb(self, arg)
- absorb(arg) -> None
Merge the attributes of another Arg into this one. This modifies
self, but not the arg argument.
The merge algorithm is somewhat ad-hoc. It is intended for a
particular purpose: merging the _args ArgList of an Agent
class with the from_argspec() ArgList.
If both the Args have a name attribute, they must be identical;
otherwise ArgDefError is raised. The same goes for index.
The optional attribute is always taken from the second Arg.
For all other attributes, if the two Args conflict, the first one's
attribute takes precedence.
- clone(self)
- clone() -> Arg
Construct an Arg identical to this one.
- to_node(self)
- to_node() -> Tree
Construct an S-expression from this Arg.
Static methods defined here:
- from_node(node)
- from_node(node) -> Arg
Construct an Arg from an S-expression. The Tree passed in should
be one generated by the to_node() method.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class ArgDefError(builtins.ValueError) |
|
ArgDefError: represents an error constructing an ArgList. |
|
- Method resolution order:
- ArgDefError
- builtins.ValueError
- builtins.Exception
- builtins.BaseException
- builtins.object
Data descriptors defined here:
- __weakref__
- list of weak references to the object (if defined)
Methods inherited from builtins.ValueError:
- __init__(self, /, *args, **kwargs)
- Initialize self. See help(type(self)) for accurate signature.
Static methods inherited from builtins.ValueError:
- __new__(*args, **kwargs) from builtins.type
- Create and return a new object. See help(type) for accurate signature.
Methods inherited from builtins.BaseException:
- __delattr__(self, name, /)
- Implement delattr(self, name).
- __getattribute__(self, name, /)
- Return getattr(self, name).
- __reduce__(...)
- Helper for pickle.
- __repr__(self, /)
- Return repr(self).
- __setattr__(self, name, value, /)
- Implement setattr(self, name, value).
- __setstate__(...)
- __str__(self, /)
- Return str(self).
- with_traceback(...)
- Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.
Data descriptors inherited from builtins.BaseException:
- __cause__
- exception cause
- __context__
- exception context
- __dict__
- __suppress_context__
- __traceback__
- args
|
class ArgExtra(builtins.object) |
|
ArgExtra(type=<class 'list'>)
ArgExtra: represents extra positional arguments, when constructing
an ArgList. |
|
Methods defined here:
- __init__(self, type=<class 'list'>)
- Initialize self. See help(type(self)) for accurate signature.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class ArgList(builtins.object) |
|
ArgList(*ls, **dic)
ArgList: represents the argument structure of a function. This
includes the number of arguments, their names, and their types.
Actually, arguments can be specified by position *or* by name,
although both is more common. The ArgList can also specify
extra positional arguments -- describing the f(*ls) Python
syntax. (It cannot currently describe the f(**dic) syntax.)
ArgList(...) -- constructor
You can construct the ArgList with positional arguments:
ArgList(Arg(...), Arg(...), ...)
...or with named arguments:
ArgList(x=Arg(...), y=Arg(...), ...)
The former is equivalent to specifying the index= value in the Arg
constructor; the latter, the name= value.
You can also include an ArgExtra object in the arguments:
ArgList(ArgExtra(...))
This defines the type of extra positional arguments. Note that the
ArgExtra must be included before any named arguments.
Static methods:
from_argspec() -- construct an ArgList from a Python function
merge() -- construct an ArgList by merging two ArgLists
from_node() -- construct an ArgList from an S-expression
Internal methods:
sort_args() -- finish constructing the ArgList
Public methods:
get_index() -- return the Arg with the given index number
get_name() -- return the Arg with the given name
clone() -- construct an ArgList identical to this one
to_node() -- construct an S-expression from this ArgList
dump() -- print out the ArgList
max_accepted() -- return the maximum number of positional arguments
min_accepted() -- return the minimum number of positional arguments
resolve() -- match a Tree of argument values against the ArgList |
|
Methods defined here:
- __bool__(self)
- __init__(self, *ls, **dic)
- Initialize self. See help(type(self)) for accurate signature.
- __len__(self)
- __repr__(self)
- Return repr(self).
- clone(self)
- clone() -> ArgList
Construct an ArgList identical to this one.
- dump(self, fl=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)
- dump(fl=sys.stdout) -> None
Print out the ArgList to stdout, or another stream. This method
prints in a human-readable form; it is intended for debugging.
- get_index(self, val)
- get_index(val) -> Arg
Return the Arg with the given index number. If there isn't one,
returns None.
- get_name(self, val)
- get_name(val) -> Arg
Return the Arg with the given name. If there isn't one,
returns None.
- max_accepted(self)
- max_accepted() -> int
Return the maximum number of positional arguments accepted by
the ArgList. If it accepts extra positional arguments, this
returns None.
### this could take listtype.max into account. necessary?
- min_accepted(self)
- min_accepted() -> int
Return the minimum number of positional arguments accepted by
the ArgList.
- resolve(self, node)
- resolve(node) -> (list, dict)
Match a Tree of argument values against the ArgList. Convert each
value to the appropriate type, fill in any necessary default
values, and return the values needed to call the function that
the ArgList represents.
The Tree must be a List with at least one (positional) value.
This first value is ignored -- it is assumed to be the name
of the function that this ArgList came from.
Raises ArgDefError if the arguments fail to match in any way.
(Too few, too many, can't be converted to the right type...)
The return values are a list and dict, such as you might expect
to use in the form f(*ls, **dic). However, you wouldn't actually
do that, because the contents of the list and dict are wrapped
values. (See the ArgWrapper class.) You could use the resolve()
function this way:
(ls, dic) = arglist.resolve(tree)
clas = ArgClassWrapper(f, ls, dic)
clas()
- sort_args(self)
- sort_args() -> None
Finish constructing the ArgList. This puts the arguments in a
consistent order. It raises ArgDefError if there are any
inconsistencies (like two arguments with the same name).
This is an internal method. It should only be called by methods
that construct ArgLists.
- to_node(self)
- to_node() -> Tree
Construct an S-expression from this ArgList.
Static methods defined here:
- from_argspec(args, varargs, varkw, defaults)
- from_argspec(args, varargs, varkw, defaults) -> ArgList
Construct an ArgList from a Python function. The four arguments
are those returned by inspect.getargspec() -- see the inspect module
in the Python standard library.
This uses the names and positions of the function arguments
to define an ArgList. If an argument has a default value, its
value and type are used as well.
If the function has extra positional arguments -- f(*ls) --
they are taken to be an arbitrary number of untyped arguments.
If the function has extra named arguments -- f(**dic) --
then ArgDefError is raised.
- from_node(node)
- from_node(node) -> ArgList
Construct an ArgList from an S-expression. The Tree passed in should
be one generated by the to_node() method.
- merge(arglist1, arglist2=None)
- merge(arglist1, arglist2=None) -> ArgList
Construct an ArgList by merging two ArgLists. This does not
modify the arguments; it creates a new ArgList.
The merge algorithm is somewhat ad-hoc. It is intended for a
particular purpose: merging the _args ArgList of an Agent
class with the from_argspec() ArgList.
If the second list is None, this returns (a clone of) the first list.
Otherwise, each argument in the second list is considered. If it
exists in the first list (same index or name), their characteristics
are merged. If not, the argument is appended to the first list.
When arguments are merged, the characteristics in the first list
take precedence -- except for the optional flag, which is always
taken from the second list. (It makes sense, honest.)
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class ListOf(SequenceOf) |
|
ListOf(*types, **opts)
ListOf: represents a list type, with additional information about
the types of the list elements.
You can use a ListOf instance (not the class!) as a type value in
an Arg.
ListOf(type, type, ..., min=0, max=None, repeat=...) -- constructor
The common case is to give one type: e.g., ListOf(int). This represents
a list of integers -- any length, from zero on up.
You can give several types: ListOf(int, str, bool). The types in
the list will be required to repeat, in that order. A list satisfying
this example might be [5, 'x', True, -5, 'y'].
If you simply say ListOf(), this is equivalent to ListOf(None): a
list of values of unspecified type and length. These are both
equivalent to simply specifying list as a type argument.
The optional min and max arguments constrain the length of the list.
(The default max=None means a list with no upper limit.)
If you specify a repeat argument, it determines how many of the
types in the list will repeat (if more values are given than the
ListOf has types). The default is "all of them". A type specified
as ListOf(int, str, bool, repeat=1) would be satisfied by
[1, 'x', True, False, True].
Public method:
to_node() -- construct an S-expression from this ListOf |
|
- Method resolution order:
- ListOf
- SequenceOf
- builtins.object
Methods defined here:
- default_setup(self, notypes)
Data and other attributes defined here:
- classname = 'ListOf'
Methods inherited from SequenceOf:
- __init__(self, *types, **opts)
- Initialize self. See help(type(self)) for accurate signature.
- __repr__(self)
- Return repr(self).
- to_node(self)
- to_node() -> Tree
Construct an S-expression from this type object.
Data descriptors inherited from SequenceOf:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class SequenceOf(builtins.object) |
|
SequenceOf(*types, **opts)
SequenceOf: base class for ListOf and TupleOf.
You cannot instantiate this class directly. |
|
Methods defined here:
- __init__(self, *types, **opts)
- Initialize self. See help(type(self)) for accurate signature.
- __repr__(self)
- Return repr(self).
- to_node(self)
- to_node() -> Tree
Construct an S-expression from this type object.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes defined here:
- classname = None
|
class TupleOf(SequenceOf) |
|
TupleOf(*types, **opts)
TupleOf: represents a tuple type, with additional information about
the types of the tuple elements.
You can use a TupleOf instance (not the class!) as a type value in
an Arg.
TupleOf(type, type, ..., min=..., max=..., repeat=...) -- constructor
The common case is to give several types: e.g., TupleOf(int, str, bool).
This represents a tuple of the given types, in that order.
You can give several types: TupleOf(int, str, bool). The types in
the list will be required to repeat, in that order. A list satisfying
this example might be [5, 'x', True, -5, 'y'].
If you simply say TupleOf(), this is a tuple of values of unspecified
type and length. This is equivalent to simply specifying tuple as a
type argument. (It is not, however, equivalent to TupleOf(None) --
that means exactly one value of unspecified type.)
The optional min and max arguments constrain the length of the tuple.
(The default is that both min and max are equal to the number of types
given.)
If you specify a repeat argument, it determines how many of the
types in the tuple will repeat (if more values are given than the
TupleOf has types). The default is "all of them". A type specified
as TupleOf(int, str, bool, repeat=1, max=5) would be satisfied by
(1, 'x', True, False, True).
Public method:
to_node() -- construct an S-expression from this TupleOf |
|
- Method resolution order:
- TupleOf
- SequenceOf
- builtins.object
Methods defined here:
- default_setup(self, notypes)
Data and other attributes defined here:
- classname = 'TupleOf'
Methods inherited from SequenceOf:
- __init__(self, *types, **opts)
- Initialize self. See help(type(self)) for accurate signature.
- __repr__(self)
- Return repr(self).
- to_node(self)
- to_node() -> Tree
Construct an S-expression from this type object.
Data descriptors inherited from SequenceOf:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class Wrapped(builtins.object) |
|
Wrapped(type)
Wrapped: represents a type which needs to be instantiated lazily.
Whenever you are creating an ArgList, you can use Wrapped(type)
instead of type.
The difference is that when the ArgList is resolved, values that
correspond with a type are instantiated immediately. Values that
correspond with a Wrapped type are left as placeholders, and can
be instantiated later -- as many times as desired.
Let me explain further. The usual pattern for resolving an ArgList
looks like:
(ls, dic) = arglist.resolve(tree)
clas = ArgClassWrapper(f, ls, dic)
clas()
Plain types are instantiated in the resolve() method. Wrapped types
are instantiated in the clas() invocation. If clas() is invoked
a second time, the wrapped types get a second instantiation,
independent of the first.
For immutable types (int, float, str, tuple) this makes no difference.
You never need to wrap those types. But for mutable types, the
difference is important. If the ArgList contains an Agent type,
an Agent instance will be created at resolve() time, and *shared*
between all clas() calls. If it instead contains a Wrapped(Agent)
type, each clas() call will get a *new* Agent instance.
Wrapped(type) -- constructor
The type must be a plain type (int, float, str, bool), or one of
Boodler's core classes (Agent or Sample), or a sequence placeholder
(a ListOf or TupleOf instance). Using long or unicode is equivalent
to int or str, respectively. Using list is equivalent to ListOf();
tuple is equivalent to TupleOf(). None indicates an unconstrained
value or list of values. Any other type argument will raise
ArgDefError. |
|
Methods defined here:
- __init__(self, type)
- Initialize self. See help(type(self)) for accurate signature.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
| |