dtlibs.core

This module contains an assortment of useful functions and classes and can be seen as an extension to the python builtin functions.

Functions

dtlibs.core.true(*args, **kwargs)

Always returns True, regardless of input. This is useful in functions which require a callable (e.g. filter) and is the same as lambda *args, **kwargs: True, but reads cleaner.

dtlibs.core.false(*args, **kwargs)

Always returns False, regardless of input. This is useful in functions which require a callable (e.g. filter) and is the same as lambda *args, **kwargs: False, but reads cleaner.

dtlibs.core.none(*args, **kwargs)

Always returns None, regardless of input. This is useful in functions which require a callable (e.g. filter) and is the same as lambda *args, **kwargs: None, but reads cleaner.

dtlibs.core.isnumeric(value)

Return true if value can be converted to any number type, e.g.:

>>> isnumeric('-3e2+4j')
True
>>> isnumeric('string')
False
dtlibs.core.iscallable(value)

Return True if value has a __call__ member. This includes functions, methods, lambdas, etc.

dtlibs.core.float2(s[, default=0.0])

Convert any value to a float, or return default if the conversion fails.

Parameters:
  • s – Value to convert to a float.
  • default – This will be returned if s could not be converted.

default may be anything, it does not have to be a float. E.g.:

>>> print(float2('not a number', None))
None
dtlibs.core.int2(s[, default=0])

Convert any value to an int, or return default if the conversion fails.

Parameters:
  • s – Value to convert to an int.
  • default – This will be returned if s could not be converted.

default may be anything, it does not have to be an int. E.g.:

>>> print(int2('not a number', None))
None

Metaclasses

dtlibs.core.singleton(*classes)

Create a singleton-type metaclass which inherits from all metaclasses of classes. Providing arguments to this is only necessary when the singleton class inherits from a non-type base. For example, if Object is a special class who’s metaclass is not type, then a singleton subclass can be created thus:

class SingletonSubclass(Object, metaclass=singleton(Object)):
...

Subclasses will inherit this metaclass, so use with care at the top of a hierarchy. However, subclasses are handled independently, so

>>> class A(metaclass=singleton()):
...     pass
>>> class B(A):
...     pass
>>> A() is B()
False
dtlibs.core.uniqueinstance(*classes[, call_init=False])

Create a metaclass which only alows unique instances.

Parameters:
  • classes – Classes which the new uniqueinstance metaclass should inherit. They should all be of type type.
  • call_init – Sets whether __init__ is called for instances which have already been created.
Returns:

A metaclass object.

The returned metaclass ensures that each instance of the class it creates is unique, based on the initialisation parameters (excluding self), which should be hashable.

The actual value used to compare may be set by using callable annotations on the __init__ method. The callables should take a single argument (the value passed through the parameter) and return a hashable value which will be used as the key.

Variables arguments (e.g. *args or **kwargs) are handled as a single argument. For example, the key for the argument *args:type will always be tuple, and the key for the arguments described by __init__(self, name, *args) and called as __init__('spam', 'eggs', 42) would be ``('spam', ('eggs', 42)) An annotation should always be used with variable keyword arguments since a dict is not hashable.

Here is a simple example:

>>> class A(metaclass=uniqueinstance()):
...     def __init__(self, id, other_arg:type):
...         pass
>>> A(1, 2) is A(1, 3)
True
>>> A(1, 2) is A('not 1', 2)
False
>>> A(1, 2) is A(1, 'other arg')
False

Decorators & Function Tools

@dtlibs.core.deprecated(version, msg)

A decorator which displays a deprecated warning every call of a callable.

This decorator accepts string arguments as the version and message, and returns the callable with its docstring updated to include a deprecated message. It should not be used on a class since it will result in incorrect name binding, meaning that isinstance checks will fail. Instead, use it on the class’ __init__ method.

For example:

>>> @deprecated('1.3', 'use a lumberjack instead')
... def barber(count):
...     "I'm a barber"
...     return count
>>> barber(4)
4
>>> help(barber)  
Help of function barber

barber(*args, **kwargs)
    .. deprecated:: 1.3
       use a lumberjack instead

    I'm a barber
@dtlibs.core.info(**kwargs)

This decorator creates an __info__ property to a function, and stores all keyword arguments passed into it. For example

>>> @info(internal_data='Spam', more_data=['spam', 'eggs'])
... def spam_function(spam_count):
...     print(spam_count)
>>> spam_function.__info__
{'internal_data': 'Spam', 'more_data': ['spam', 'eggs']}

This has no effect on the function itself, it is merely a way to associate data with a function definition.

dtlibs.core.hasinfo(obj[, name=None])

Return True if obj has name in its __info__ dict.

If name is None, then True if returned if object has an __info__ dict. In either case, if object does not have an __info__ dict, False is returned.

Note

This (and the related getinfo and setinfo) can be used on any objects, not just functions. However, since the info is stored in the __info__ attribute of the object, use with objects which already make use of this name could cause expected behaviour.

dtlibs.core.setinfo(obj[, **kwargs])

Set info on a function using info().

This is the similar to obj = info(**kwargs)(obj), except that it updates __info__ if it already exists.

dtlibs.core.getinfo(obj, name)

Get the info set on a function (e.g. by info).

This is the same as obj.__info__[name]. If this does not exist, an exception is raised.