simudo.util package

Submodules

simudo.util.attrproxy module

class simudo.util.attrproxy.DictAttrProxy(mapping)[source]

Bases: object

class simudo.util.attrproxy.AttrPrefixProxy(object, prefix)[source]

Bases: object

simudo.util.base32_token module

simudo.util.base32_token.generate_base32_token(length)[source]

simudo.util.cartesian_product module

simudo.util.cartesian_product.cartesian_product(arrays)[source]

simudo.util.latex module

simudo.util.latex.sci_to_latex1(string, suppress_mantissa_eq_1=True)[source]

Convert a number in scientific notation to reasonable LaTeX code.

Do NOT modify this method! Copy-paste it and change the name. Other code may rely on its exact behavior (bug-for-bug compatibility).

simudo.util.logging module

class simudo.util.logging.NameLevelFilter(name_levelno_rules, *args, **kwargs)[source]

Bases: logging.Filter

filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

class simudo.util.logging.TypicalLoggingSetup(**kwargs)[source]

Bases: simudo.util.setattr_init_mixin.SetattrInitMixin

console_formatter
debug_filename
dolfin = True
ensure_parent_dir(filename)[source]
info_filename
logfile_formatter
setup()[source]
setup_dolfin_loglevel()[source]
setup_filters()[source]
setup_handlers()[source]
setup_logging()[source]
stream_console
stream_debug
stream_info

simudo.util.name_dict module

class simudo.util.name_dict.NameDict(iterable=None)[source]

Bases: object

add(obj, existing_raise=True)[source]
copy()[source]
items()[source]
keys()[source]
mapping
obj_eq(obj0, obj1)[source]
obj_to_key(obj)[source]
replace(obj)[source]
update(data, existing_raise=True)[source]
values()[source]

simudo.util.os module

simudo.util.os.fullsplit(path)[source]

fully split path into components

simudo.util.os.mkdirp(path)[source]
simudo.util.os.dir_as_prefix(path)[source]

Add the final path separator (“/”) if necessary.

For example:

"abc/def" -> "abc/def/"
"abc/def/" -> "abc/def/"
simudo.util.os.outdir_path_helper(path)[source]

Calls mkdirp(), then returns dir_as_prefix() applied on path.

simudo.util.os.xlistdir(path, both=False)[source]

like listdir, but returns full paths or tuples (basename, fullpath)

simudo.util.os.parse_kv(string, as_list=False)[source]

simudo.util.pint module

class simudo.util.pint.XUnitRegistry(filename='', force_ndarray=False, default_as_delta=True, autoconvert_offset_to_baseunit=False, on_redefinition='warn', system=None, auto_reduce_dimensions=False)[source]

Bases: pint.registry.UnitRegistry

custom_file_open(path)[source]
load_definitions(file, is_resource=False)[source]

Add units and prefixes defined in a definition text file.

Parameters:
  • file – can be a filename or a line iterable.
  • is_resource – used to indicate that the file is a resource file and therefore should be loaded from the package.
simudo.util.pint.make_unit_registry(extra_definitions=())[source]

simudo.util.raise_from module

simudo.util.raise_from.raise_from(new, old)[source]

simudo.util.raise_from_py3 module

simudo.util.setattr_init_mixin module

class simudo.util.setattr_init_mixin.SetattrInitMixin(**kwargs)[source]

Bases: object

simudo.util.string_system module

simudo.util.string_system.make_string_system(strings, min_length=6, score=<function default_score>)[source]
simudo.util.string_system.format_string_system(string_system_output, replacements=None)[source]

simudo.util.with_default_kwargs module

simudo.util.with_default_kwargs.with_default_kwargs()[source]

Python doesn’t like it if we pass the same argument by an explicit kwarg and through **kwargs, e.g., f(y=3, **{'x': 4, 'y': 5}).

This decorator transforms a function so that it receives a default dict of kwargs through its first argument, then remaining kwargs normally (latter taking precedence over the former).

Assuming f was decorated with this wrapper, the call above becomes f({'x': 4, 'y':5}, y=3).

You can (ab)use this function to be lazy and pass all the local variables to a function as kwargs, and still have the option of overriding some of them, e.g., f(locals(), y=3).

simudo.util.xtimeit module

simudo.util.xtimeit.xtimeit(thunk, timer=<built-in function perf_counter>, overall_time=1.0, warmup_iterations=3)[source]

simudo.util.xcsv module

This (standalone) module implements a Pandas CSV reader-writer pair that allows data types to survive a round-trip (where they wouldn’t using plain pandas to_csv). It achieves this by saving some column metadata to JSON, and by prefixing string values with a “:” character so that they cannot be confused with NaN values (which are also allowed in string columns, creating unresolvable ambiguity in the written data).

See to_csv() and read_csv() for more info.

These methods are available as simple functions, so you can do:

>>> to_xcsv(df, "hello.csv")
>>> df2, meta = from_xcsv(df, "hello.csv")
class simudo.util.xcsv.XCSV[source]

Bases: object

classmethod read_csv(path, json_path=None, read_csv_kwargs={})[source]

Opposite of to_csv().

reader_class

alias of XCSVReader

classmethod to_csv(df, path, json_path=None, to_csv_kwargs={})[source]

Basically the same as pandas.DataFrame.to_csv(), but with proper escaping for strings to prevent them from being accidentally parsed as numbers or nan, and with column dtypes being written to an accompanying json file.

If the csv filename is "a.csv", then the file name containing the metadata will be called "a.csv_meta.json".

“XCSV” pronounced “excessive”.

Warning: mixed-type (“object”) columns are assumed to be string columns. So make sure those don’t contain anything other than strings or NaN, or your else your data might not survive the roundtrip test.

What’s definitely safe:

  • Columns with floats/ints and nans.
  • Columns with strings and nans.
  • Columns with booleans (no nans allowed!).
writer_class

alias of XCSVWriter

class simudo.util.xcsv.XCSVBase[source]

Bases: object

is_string_column(df, dtypes_dict, column_name)[source]
json_path
json_path_from_xcsv_path(path)[source]
class simudo.util.xcsv.XCSVWriter[source]

Bases: simudo.util.xcsv.XCSVBase

compute_meta()[source]
dtypes_dict
meta
string_columns
string_prefix
to_csv()[source]
write_json_meta()[source]
class simudo.util.xcsv.XCSVReader[source]

Bases: simudo.util.xcsv.XCSVBase

dtypes_dict
load_json_meta()[source]
meta
process_string_column(df, column_name, series)[source]
read_csv()[source]
string_columns
string_prefix
string_prefix_re

Module contents