libtbx - low-level utilities and infrastructure for CCTBX

libtbx is the only pure-Python module in CCTBX, and is required by all other modules. Among many other functions, it handles all of the environment and build system configuration.

Submodules

Basic functionality

The base libtbx module contains several convenience tools for simplifying other modules.

class libtbx.AutoType

Class for creating the Auto instance, which mimics the behavior of None with respect to the ‘is’ and ‘==’ operators; this is used throughout CCTBX to indicate parameters that should be determined automatically.

Examples

>>> def f(optional=libtbx.Auto)
...    if optional is libtbx.Auto:
...        optional = 5
...    return optional
...
>>> print(f())
5
>>> print(f(optional=10))
10
libtbx.adopt_init_args(obj, args, exclude=(), hide=False)

Adopts the initial arguments passed to an object, allowing developers to skip the tedious task of assigning each attribute of an instance in its __init__ method.

Parameters:

Examples

>>> class foo(object):
...     def __init__(self, x, y=1, z=None):
...         adopt_init_args(self, locals())
...
>>> a = foo('a', z=10)
>>> assert a.x == 'a'
>>> assert a.y == 1
>>> assert a.z == 10
libtbx.adopt_optional_init_args(obj, kwds)

Easy management of long list of arguments with default value passed to __init__.

Parameters:

Examples

>>> class foo(object):
...     z = 1
...     def __init__(self, **kwds):
...       libtbx.adopt_optional_init_args(self, kwds)
...
>>> a = foo()
>>> assert a.z == 1
>>> a = foo(z=10)
>>> assert a.z == 10
class libtbx.dict_with_default_0
class libtbx.group_args(**keyword_arguments)

Class to build an arbitrary object from a list of keyword arguments.

Examples

>>> from libtbx import group_args
>>> obj = group_args(a=1, b=2, c=3)
>>> print(obj.a, obj.b, obj.c)
1 2 3

Once stop_dynamic_attributes is called, adding new attributes won’t be possible, that is this:

obj.tmp=10

will fail.

add_if_missing(other, add_if_self_is_none=False)

takes values from other only if not present at all in self Optionally add if value in self is None

copy()

produce shallow copy of self by converting to dict and back

merge(other)

To merge other group_args into self. Overwrites matching fields!!!

class libtbx.mutable(value)
class libtbx.slots_getstate_setstate

Implements getstate and setstate for classes with __slots__ defined. Allows an object to easily pickle only certain attributes.

Examples

>>> class sym_pair(libtbx.slots_getstate_setstate):
...     __slots__ = ["i_seq", "j_seq"]
...     def __init__(self, i_seq, j_seq):
...         self.i_seq = i_seq
...         self.j_seq = j_seq
...
class libtbx.slots_getstate_setstate_default_initializer(**kwds)

Merges together functionality from slots_getstate_setstate with adopt_optional_init_args.

Examples

>>> class sym_pair(libtbx.slots_getstate_setstate_default_initializer):
...     __slots__ = ["i_seq", "j_seq"]
...
>>> svm_pair(i_seq=1, j_seq=2)
>>> print(svm_pair.i_seq)
1
class libtbx.unpicklable

An inheritable class that raises a runtime exception that an object is unpicklable.