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.

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.


>>> def f(optional=libtbx.Auto)
...    if optional is libtbx.Auto:
...        optional = 5
...    return optional
>>> print f()
>>> print f(optional=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.


obj : object

args : list

exclude : list of str

hide : bool, optional


>>> 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__.


obj : object

kwds : dict


>>> 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.group_args(**keyword_arguments)

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


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



Raises an exception if sequence contains anything other than one element.

Parameters:sequence : list

Syntactic sugar for defining class properties for those poor souls who must stay compatible with older versions of Python which do not feature the @property decorator.


>>> class foo(object):
...     class bar(
...         'documentation of the property'
...         # In the following, self is the object featuring the property.
...         def fget(self): # getter
...         def fset(self, value): # setter
...         def fdel(self): # deleter
class libtbx.slots_getstate_setstate

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


>>> 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.


>>> 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
class libtbx.unpicklable

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

Table Of Contents

Previous topic

CCTBX development history

Next topic

libtbx.phil - Python-based hierarchical interchange language

This Page