xoutil.types
- Names for built-in types and extensions.¶
-
xoutil.types.
is_iterable
(maybe)[source]¶ Returns True if maybe is an iterable object (e.g. implements the __iter__ method):
>>> is_iterable('all strings are iterable') True # Numbers are not >>> is_iterable(1) False >>> from xoutil.eight import range >>> is_iterable(range(1)) True >>> is_iterable({}) True >>> is_iterable(tuple()) True >>> is_iterable(set()) True
-
xoutil.types.
is_collection
(maybe)[source]¶ Test maybe to see if it is a tuple, a list, a set or a generator function.
It returns False for dictionaries and strings:
>>> is_collection('all strings are iterable') False # Numbers are not >>> is_collection(1) False >>> from xoutil.eight import range >>> is_collection(range(1)) True >>> is_collection({}) False >>> is_collection(tuple()) True >>> is_collection(set()) True >>> is_collection(a for a in range(100)) True
Changed in version 1.5.5: UserList are collections.
-
xoutil.types.
is_scalar
(maybe)[source]¶ Returns True if maybe is a string, an int, or some other scalar type (i.e not an iterable.)
-
xoutil.types.
is_classmethod
(desc, name=Unset)[source]¶ Returns true if a method is a class method.
Parameters: - desc –
This may be the method descriptor or the class that holds the method, in the second case you must provide the name of the method.
Note
Notice that in the first case what is needed is the method descriptor, i.e, taken from the class’ __dict__ attribute. If instead you pass something like
cls.methodname
, this method will return False whilstis_instancemethod()
will return True. - name – The name of the method, if the first argument is the class.
- desc –
-
xoutil.types.
is_staticmethod
(desc, name=Unset)[source]¶ Returns true if a method is a static method.
This function takes the same arguments as
is_classmethod()
.
-
xoutil.types.
is_instancemethod
(desc, name=Unset)[source]¶ Returns true if a given method is neither a static method nor a class method.
This function takes the same arguments as
is_classmethod()
.
-
xoutil.types.
is_slotwrapper
(desc, name=Unset)[source]¶ Returns True if a given method is a slot wrapper (i.e. a method that is builtin in the object base class).
This function takes the same arguments as
is_classmethod()
.
-
xoutil.types.
are_instances
(*subjects, types)[source]¶ Return True if every subject is an instance of (any) types.
Parameters: - subjects – All but last positional arguments. Are the objects required to be instances of types.
- types – The last positional argument. Either a single type or a
sequence of types. This must meet the conditions on the last argument
of
isinstance()
.
Returns: True or False. True if for every subject,
isinstance(subject, types)
is True. Otherwise, False.If no subjects are provided return True:
>>> are_instances(int) True
See also
The function
no_instances()
allows to test for subjects not being instances of types.
-
xoutil.types.
no_instances
(*subjects, types)[source]¶ Return True if every subject is not an instance of (neither) types.
Parameters: - subjects – All but last positional arguments. Are the objects required not to be instances of types.
- types – The last positional argument. Either a single type or a
sequence of types. This must meet the conditions on the last argument
of
isinstance()
.
Returns: True or False. True if for every subject,
isinstance(subject, types)
is False. Otherwise, False.If no subjects are provided return True:
>>> no_instances(int) True
Note
This is not the same as
not are_instances(...)
.This function requires that no subject is an instance of types. Negating
are_instances()
would be True if any subject is not an instance of types.
-
xoutil.types.
new_class
(name, bases=(), kwds=None, exec_body=None)[source]¶ Create a class object dynamically using the appropriate metaclass.
New in version 1.5.5.
-
xoutil.types.
prepare_class
(name, bases=(), kwds=None)[source]¶ Call the __prepare__ method of the appropriate metaclass.
Returns (metaclass, namespace, kwds) as a 3-tuple
metaclass is the appropriate metaclass namespace is the prepared class namespace kwds is an updated copy of the passed in kwds argument with any ‘metaclass’ entry removed. If no kwds argument is passed in, this will be an empty dict.
New in version 1.5.5.
-
xoutil.types.
UnsetType
¶ alias of
Logical
-
xoutil.types.
SlotWrapperType
¶ alias of
wrapper_descriptor
-
class
xoutil.types.
Required
(*args, **kwargs)[source]¶ A type for required fields in scenarios where a default is not possible.
-
class
xoutil.types.
mro_dict
(target)[source]¶ Utility behaving like a read-only dict of target MRO attributes.
For example:
>>> class A(object): ... x = 12 ... y = 34 >>> class B(A): ... y = 56 ... z = 78 >>> d = mro_dict(B) >>> d['x'] 12 >>> d['y'] 56 >>> d['z'] 78
-
class
xoutil.types.
MappingProxyType
¶ New in version 1.5.5.
Read-only proxy of a mapping. It provides a dynamic view on the mapping’s entries, which means that when the mapping changes, the view reflects these changes.
Note
In Python 3.3+ this is an alias for
types.MappingProxyType
in the standard library.
-
class
xoutil.types.
SimpleNamespace
[source]¶ New in version 1.5.5.
A simple
object
subclass that provides attribute access to its namespace, as well as a meaningful repr.Unlike
object
, withSimpleNamespace
you can add and remove attributes. If aSimpleNamespace
object is initialized with keyword arguments, those are directly added to the underlying namespace.The type is roughly equivalent to the following code:
class SimpleNamespace(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) def __repr__(self): keys = sorted(self.__dict__) items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): return self.__dict__ == other.__dict__
SimpleNamespace
may be useful as a replacement forclass NS: pass
. However, for a structured record type usenamedtuple()
instead.Note
In Python 3.4+ this is an alias to
types.SimpleNamespace
.
-
class
xoutil.types.
DynamicClassAttribute
(fget=None, fset=None, fdel=None, doc=None)[source]¶ Route attribute access on a class to
__getattr__()
.This is a descriptor, used to define attributes that act differently when accessed through an instance and through a class. Instance access remains normal, but access to an attribute through a class will be routed to the class’s
__getattr__()
method; this is done by raising AttributeError.This allows one to have properties active on an instance, and have virtual attributes on the class with the same name (see
Enum
for an example).New in version 1.5.5.
Note
The class Enum mentioned has not yet been backported.
Note
In Python 3.4+ this is an alias to
types.DynamicClassAttribute
.
Importing Unset and ignored¶
Warning
Removed in 1.5.0
Deprecated since version 1.4.0: These imports are removed in version 1.5.0.
The values Unset and ignored are not types neither functions that test for types. They are values and are moved out of this module. Nevertheless, they will remain importable from this module up to version 1.5.0.
-
xoutil.types.
ignored
¶ To be used in arguments that are currently ignored cause they are being deprecated. The only valid reason to use ignored is to signal ignored arguments in method’s/function’s signature.