commonlibs.fileio package¶
Submodules¶
commonlibs.fileio.json module¶
JSON
-
class
commonlibs.fileio.json.NDArrayEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶ Bases:
json.encoder.JSONEncoderSerialise Numpy arrays
Use like this: json.dump(obj, fp, cls=NDArrayEncoder)
-
default(obj)[source]¶ Implement this method in a subclass such that it returns a serializable object for
o, or calls the base implementation (to raise aTypeError).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
-
-
commonlibs.fileio.json.dump_pretty_json(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=<class 'commonlibs.fileio.json.NDArrayEncoder'>, indent=4, separators=(', ', ': '), default=None, sort_keys=False, **kw)¶ Serialize
objas a JSON formatted stream tofp(a.write()-supporting file-like object).If
skipkeysis true thendictkeys that are not basic types (str,int,float,bool,None) will be skipped instead of raising aTypeError.If
ensure_asciiis false, then the strings written tofpcan contain non-ASCII characters if they appear in strings contained inobj. Otherwise, all such characters are escaped in JSON strings.If
check_circularis false, then the circular reference check for container types will be skipped and a circular reference will result in anOverflowError(or worse).If
allow_nanis false, then it will be aValueErrorto serialize out of rangefloatvalues (nan,inf,-inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN,Infinity,-Infinity).If
indentis a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines.Noneis the most compact representation.If specified,
separatorsshould be an(item_separator, key_separator)tuple. The default is(', ', ': ')if indent isNoneand(',', ': ')otherwise. To get the most compact JSON representation, you should specify(',', ':')to eliminate whitespace.default(obj)is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.If sort_keys is true (default:
False), then the output of dictionaries will be sorted by key.To use a custom
JSONEncodersubclass (e.g. one that overrides the.default()method to serialize additional types), specify it with theclskwarg; otherwiseJSONEncoderis used.
commonlibs.fileio.paths module¶
Better file path handling
-
class
commonlibs.fileio.paths.ProjectPaths(root_dir)[source]¶ Bases:
object-
FORMATTER_COUNTER= 'counter'¶
-
UID_ROOT= 'root'¶
-
add_path(uid, path, uid_groups=None, is_absolute=False)[source]¶ Add a path
- Args:
uid: Unique identifier for the path path: Path string or Path() object uid_groups: Optional UID(s) to identify files by groups is_absolute: Flag indicating if given ‘path’ is absolute
-
add_subpath(uid_parent, uid, path, uid_groups=None)[source]¶ Add a child path to an existing parent path
- Args:
uid_parent: UID of the parent directory uid: UID of the new path path: relative path to add uid_groups: Optional UID(s) to identify files by groups
-
add_suffix(uid, new_suffix)[source]¶ Add a suffix to a path
- Args:
uid: Unique identifier for the path new_suffix: String with the new suffix
-
change_suffix(uid, new_suffix)[source]¶ Modify the suffix of a path
- Args:
uid: Unique identifier for the path new_suffix: String with the new suffix
-
counter¶
-
iter_group_paths(uid_groups, make_dirs=False, is_dir=False)[source]¶ Return a generator with paths belong to group with given UID
- Args:
uid_groups: Optional UID(s) to identify files by groups
-
make_dirs_for_groups(uid_groups, is_dir=True)[source]¶ Create directories for all paths belonging to specified group(s)
- Args:
uid_groups: Optional UID(s) to identify files by groups
-
remove_path(uid)[source]¶ Remove a path from the bookkeeping
- Args:
uid: Unique identifier for the path
-
rm_dirs_for_groups(uid_groups)[source]¶ TODO
- Args:
uid_groups: Optional UID(s) to identify files by groups
-
root¶ Return the Path() object for the project root directory
-
Module contents¶
-
commonlibs.fileio.dump_pretty_json(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=<class 'commonlibs.fileio.json.NDArrayEncoder'>, indent=4, separators=(', ', ': '), default=None, sort_keys=False, **kw)¶ Serialize
objas a JSON formatted stream tofp(a.write()-supporting file-like object).If
skipkeysis true thendictkeys that are not basic types (str,int,float,bool,None) will be skipped instead of raising aTypeError.If
ensure_asciiis false, then the strings written tofpcan contain non-ASCII characters if they appear in strings contained inobj. Otherwise, all such characters are escaped in JSON strings.If
check_circularis false, then the circular reference check for container types will be skipped and a circular reference will result in anOverflowError(or worse).If
allow_nanis false, then it will be aValueErrorto serialize out of rangefloatvalues (nan,inf,-inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN,Infinity,-Infinity).If
indentis a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines.Noneis the most compact representation.If specified,
separatorsshould be an(item_separator, key_separator)tuple. The default is(', ', ': ')if indent isNoneand(',', ': ')otherwise. To get the most compact JSON representation, you should specify(',', ':')to eliminate whitespace.default(obj)is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.If sort_keys is true (default:
False), then the output of dictionaries will be sorted by key.To use a custom
JSONEncodersubclass (e.g. one that overrides the.default()method to serialize additional types), specify it with theclskwarg; otherwiseJSONEncoderis used.