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]¶
-
- 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.
- Parameters:
- iiint or str
The index or name of the item.
- defaultany, optional
The default value returned in case the item ii does not exist.
- msg_if_nonestr, optional
If not None, and if default is None and the item ii does not exist, raise ValueError with this message.
- 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.
Examples
>>> 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
- property prefix¶
- 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.
- Parameters:
- filenamestr or file object
Print messages into the specified file.
- quietbool
Do not print anything to screen.
- combinedbool
Print both on screen and into the specified file.
- appendbool
Append to an existing file instead of overwriting it. Use with filename.
- class sfepy.base.base.Struct(**kwargs)[source]¶
- sfepy.base.base.as_float_or_complex(val)[source]¶
Try to cast val to Python float, and if this fails, to Python complex type.
- 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.
- sfepy.base.base.configure_output(options)[source]¶
Configure the standard
output()
function using output_log_name and output_screen attributes of options.- Parameters:
- optionsStruct 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.
- sfepy.base.base.dict_to_array(adict)[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.
- Parameters:
- str_dictdict
The dictionary with string values or tuples containing strings.
- oldstr or list of str
The old substring or list of substrings.
- newstr or list of str
The new substring or list of substrings.
- recurbool
If True, edit tuple values recursively.
- Returns:
- new_dictdict
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.
- Parameters:
- str_tupletuple
The tuple with string values.
- oldstr
The old substring.
- newstr
The new substring.
- recurbool
If True, edit items that are tuples recursively.
- Returns:
- new_tupletuple
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.
Examples
>>> solver_table = find_subclasses(vars().items(), [LinearSolver, NonlinearSolver, TimeSteppingSolver, EigenvalueSolver, OptimizationSolver])
- sfepy.base.base.get_arguments(omit=None)[source]¶
Get a calling function’s arguments.
Returns:
- argsdict
The calling function’s arguments.
- 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.invert_dict(d, is_val_tuple=False, unique=True)[source]¶
Invert a dictionary by making its values keys and vice versa.
- Parameters:
- ddict
The input dictionary.
- is_val_tuplebool
If True, the d values are tuples and new keys are the tuple items.
- uniquebool
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.
- Returns:
- didict
The inverted dictionary.
- 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.pause(msg=None)[source]¶
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.
- sfepy.base.base.print_structs(objs)[source]¶
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.shell(frame=0)[source]¶
Embed an IPython (if available) or regular Python shell in the given frame.
- sfepy.base.base.spause(msg=None)[source]¶
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().
- sfepy.base.base.try_imports(imports, fail_msg=None)[source]¶
Try import statements until one succeeds.
- Parameters:
- importslist
The list of import statements.
- fail_msgstr
If not None and no statement succeeds, a ValueError is raised with the given message, appended to all failed messages.
- Returns:
- localsdict
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.
- Parameters:
- dstdict
The destination dictionary.
- srcdict
The source dictionary.
- tuples_toobool
If True, recurse also into dictionaries that are members of tuples.
- overwrite_by_nonebool
If False, do not overwrite destination dictionary values by None.
- Returns:
- dstdict
The destination dictionary.