sfepy.base.base module

sfepy.base.base.debug(frame=None, frames_back=1)

Start debugger on line where it is called, roughly equivalent to:

import pdb; pdb.set_trace()

First, this function tries to start an IPython-enabled debugger using the IPython API.

When this fails, the plain old pdb is used instead.

With IPython, one can say in what frame the debugger can stop.

class sfepy.base.base.Container(objs=None, **kwargs)[source]

Return stored objects in a dictionary with object names as keys.


Extend the container items by the sequence objs.

get(ii, default=None, msg_if_none=None)[source]

Get an item from Container - a wrapper around Container.__getitem__() with defaults and custom error message.

ii : int or str

The index or name of the item.

default : any, optional

The default value returned in case the item ii does not exist.

msg_if_none : str, optional

If not None, and if default is None and the item ii does not exist, raise ValueError with this message.

insert(ii, obj)[source]
class sfepy.base.base.IndexedStruct(**kwargs)[source]
class sfepy.base.base.OneTypeList(item_class, seq=None)[source]
find(name, ret_indx=False)[source]
class sfepy.base.base.Output(prefix, filename=None, quiet=False, combined=False, append=False, **kwargs)[source]

Factory class providing output (print) functions. All SfePy printing should be accomplished by this class.


>>> from sfepy.base.base import Output
>>> output = Output('sfepy:')
>>> output(1, 2, 3, 'hello')
sfepy: 1 2 3 hello
>>> output.prefix = 'my_cool_app:'
>>> output(1, 2, 3, 'hello')
my_cool_app: 1 2 3 hello
set_output(filename=None, quiet=False, combined=False, append=False)[source]

Set the output mode.

If quiet is True, no messages are printed to screen. If simultaneously filename is not None, the messages are logged into the specified file.

If quiet is False, more combinations are possible. If filename is None, output is to screen only, otherwise it is to the specified file. Moreover, if combined is True, both the ways are used.

filename : str or file object

Print messages into the specified file.

quiet : bool

Do not print anything to screen.

combined : bool

Print both on screen and into the specified file.

append : bool

Append to an existing file instead of overwriting it. Use with filename.

class sfepy.base.base.Struct(**kwargs)[source]
copy(deep=False, name=None)[source]

Make a (deep) copy of self.


deep : bool
Make a deep copy.
name : str
Name of the copy, with default self.name + ‘_copy’.
get(key, default=None, msg_if_none=None)[source]

A dict-like get() for Struct attributes.

set_default(key, default=None)[source]

Behaves like dict.setdefault().


As __str__(), but for class attributes.

update(other, **kwargs)[source]

A dict-like update for Struct attributes.


Try to cast val to Python float, and if this fails, to Python complex type.

sfepy.base.base.assert_(condition, msg=’assertion failed!’)[source]
sfepy.base.base.check_names(names1, names2, msg)[source]

Check if all names in names1 are in names2, otherwise raise IndexError with the provided message msg.


Configure the standard output() function using output_log_name and output_screen attributes of options.

options : Struct or dict

The options with output_screen and output_log_name items. Defaults are provided if missing.

sfepy.base.base.debug(frame=None, frames_back=1)

Start debugger on line where it is called, roughly equivalent to:

import pdb; pdb.set_trace()

First, this function tries to start an IPython-enabled debugger using the IPython API.

When this fails, the plain old pdb is used instead.

With IPython, one can say in what frame the debugger can stop.


Start debugger at the line where an exception was raised.

sfepy.base.base.dict_extend(d1, d2)[source]
sfepy.base.base.dict_from_keys_init(keys, seq_class=None)[source]

Convert a dictionary of nD arrays of the same shapes with non-negative integer keys to a single (n+1)D array.

sfepy.base.base.dict_to_struct(*args, **kwargs)[source]

Convert a dict instance to a Struct instance.

sfepy.base.base.edit_dict_strings(str_dict, old, new, recur=False)[source]

Replace substrings old with new in string values of dictionary str_dict. Both old and new can be lists of the same length - items in old are replaced by items in new with the same index.

str_dict : dict

The dictionary with string values or tuples containing strings.

old : str or list of str

The old substring or list of substrings.

new : str or list of str

The new substring or list of substrings.

recur : bool

If True, edit tuple values recursively.

new_dict : dict

The dictionary with edited strings.

sfepy.base.base.edit_tuple_strings(str_tuple, old, new, recur=False)[source]

Replace substrings old with new in items of tuple str_tuple. Non-string items are just copied to the new tuple.

str_tuple : tuple

The tuple with string values.

old : str

The old substring.

new : str

The new substring.

recur : bool

If True, edit items that are tuples recursively.

new_tuple : tuple

The tuple with edited strings.

sfepy.base.base.find_subclasses(context, classes, omit_unnamed=False, name_attr=’name’)[source]

Find subclasses of the given classes in the given context.


>>> solver_table = find_subclasses(vars().items(),
                                   [LinearSolver, NonlinearSolver,
                                    TimeSteppingSolver, EigenvalueSolver,

Get a calling function’s arguments.


args : dict
The calling function’s arguments.

Utility function providing debug() function.

sfepy.base.base.get_default(arg, default, msg_if_none=None)[source]
sfepy.base.base.get_default_attr(obj, attr, default, msg_if_none=None)[source]
sfepy.base.base.get_subdict(adict, keys)[source]

Get a sub-dictionary of adict with given keys.

sfepy.base.base.import_file(filename, package_name=None, can_reload=True)[source]

Import a file as a module. The module is explicitly reloaded to prevent undesirable interactions.

sfepy.base.base.insert_as_static_method(cls, name, function)[source]
sfepy.base.base.insert_method(instance, function)[source]
sfepy.base.base.insert_static_method(cls, function)[source]
sfepy.base.base.invert_dict(d, is_val_tuple=False, unique=True)[source]

Invert a dictionary by making its values keys and vice versa.

d : dict

The input dictionary.

is_val_tuple : bool

If True, the d values are tuples and new keys are the tuple items.

unique : bool

If True, the d values are unique and so the mapping is one to one. If False, the d values (possibly) repeat, so the inverted dictionary will have as items lists of corresponding keys.

di : dict

The inverted dictionary.

sfepy.base.base.is_derived_class(cls, parent)[source]
sfepy.base.base.iter_dict_of_lists(dol, return_keys=False)[source]
sfepy.base.base.load_classes(filenames, classes, package_name=None, ignore_errors=False, name_attr=’name’)[source]

For each filename in filenames, load all subclasses of classes listed.

sfepy.base.base.mark_time(times, msg=None)[source]

Time measurement utility.

Measures times of execution between subsequent calls using time.clock(). The time is printed if the msg argument is not None.


>>> times = []
>>> mark_time(times)
... do something
>>> mark_time(times, 'elapsed')
elapsed 0.1
... do something else
>>> mark_time(times, 'elapsed again')
elapsed again 0.05
>>> times
[0.10000000000000001, 0.050000000000000003]

Prints the line number and waits for a keypress.

If you press: “q” …………. it will call sys.exit() any other key … it will continue execution of the program

This is useful for debugging.


Print Struct instances in a container, works recursively. Debugging utility function.

sfepy.base.base.remap_dict(d, map)[source]

Utility function to remap state dict keys according to var_map.

sfepy.base.base.select_by_names(objs_all, names, replace=None, simple=True)[source]
sfepy.base.base.set_defaults(dict_, defaults)[source]

Embed an IPython (if available) or regular Python shell in the given frame.


Waits for a keypress.

If you press: “q” …………. it will call sys.exit() any other key … it will continue execution of the program

This is useful for debugging. This function is called from pause().


Convert a (nested) dict obj into a (nested) Struct.

sfepy.base.base.try_imports(imports, fail_msg=None)[source]

Try import statements until one succeeds.

imports : list

The list of import statements.

fail_msg : str

If not None and no statement succeeds, a ValueError is raised with the given message, appended to all failed messages.

locals : dict

The dictionary of imported modules.

sfepy.base.base.update_dict_recursively(dst, src, tuples_too=False, overwrite_by_none=True)[source]

Update dst dictionary recursively using items in src dictionary.

dst : dict

The destination dictionary.

src : dict

The source dictionary.

tuples_too : bool

If True, recurse also into dictionaries that are members of tuples.

overwrite_by_none : bool

If False, do not overwrite destination dictionary values by None.

dst : dict

The destination dictionary.

sfepy.base.base.use_method_with_name(instance, method, new_name)[source]