pytornado.fileio package

Submodules

pytornado.fileio.utils module

Utils for reading and writing files

Developed at Airinnova AB, Stockholm, Sweden.

class pytornado.fileio.utils.NDArrayEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.encoder.JSONEncoder

Serialise numpy arrays

Use with: json.dump(obj, fp, cls=NDArrayEncoder)

default(obj)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

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)
pytornado.fileio.utils.dump_pretty_json(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=<class 'pytornado.fileio.utils.NDArrayEncoder'>, indent=4, separators=(', ', ': '), default=None, sort_keys=False, **kw)

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object).

If skipkeys is true then dict keys that are not basic types (str, int, float, bool, None) will be skipped instead of raising a TypeError.

If ensure_ascii is false, then the strings written to fp can contain non-ASCII characters if they appear in strings contained in obj. Otherwise, all such characters are escaped in JSON strings.

If check_circular is false, then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse).

If allow_nan is false, then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity).

If indent is 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. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') 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 JSONEncoder subclass (e.g. one that overrides the .default() method to serialize additional types), specify it with the cls kwarg; otherwise JSONEncoder is used.

pytornado.fileio.utils.parse_str(entry, allow_char='-_')

Strip undesired characters from a string

Note:
  • Alphanumeric characters always accepted: [a…z], [A…Z], [0…9]
Args:
entry:(string) Input string
allow_char:(string) Additional accepted characters
allow_none:(bool) Detect when input is None
allow_bool:(bool) Detect when input is True or False
Returns:
parsed_string:Parsed string

Module contents

The fileio package contains the modules dedicated to the input and output of data to and from file. Each module is itself associated with a given file type.