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]¶ -
append
(obj)¶
-
as_dict
()¶ Return stored objects in a dictionary with object names as keys.
-
extend
(objs)¶ Extend the container items by the sequence objs.
-
get
(ii, default=None, msg_if_none=None)¶ Get an item from Container - a wrapper around Container.__getitem__() with defaults and custom error message.
Parameters: 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.
-
get_names
()¶
-
has_key
(ii)¶
-
insert
(ii, obj)¶
-
iteritems
()¶
-
iterkeys
()¶
-
itervalues
()¶
-
print_names
()¶
-
remove_name
(name)¶
-
update
(objs=None)¶
-
-
class
sfepy.base.base.
OneTypeList
(item_class, seq=None)[source]¶ -
find
(name, ret_indx=False)¶
-
get_names
()¶
-
print_names
()¶
-
-
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
-
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: 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)¶ Make a (deep) copy of self.
Parameters:
- 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)¶ A dict-like get() for Struct attributes.
-
set_default
(key, default=None)¶ Behaves like dict.setdefault().
-
str_all
()¶
-
str_class
()¶ As __str__(), but for class attributes.
-
to_dict
()¶
-
update
(other, **kwargs)¶ A dict-like update for Struct attributes.
-
-
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 :function:`output()` function using output_log_name and output_screen attributes of options.
Parameters: 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.
-
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_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.
Returns: 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.
Parameters: 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.
Returns: 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.
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:
- args : dict
- 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: 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.
Returns: di : dict
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.
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.
Examples
>>> 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]
-
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.
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: 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.
Returns: 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.
Parameters: 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.
Returns: dst : dict
The destination dictionary.