tokio.common module

Common convenience routines used throughout pytokio

exception tokio.common.ConfigError[source]

Bases: exceptions.RuntimeError

class tokio.common.JSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)[source]

Bases: json.encoder.JSONEncoder

Convert common pytokio data types into serializable formats

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 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)
tokio.common.humanize_bytes(bytect, base10=False, fmt='%.1f %s')[source]

Converts bytes into human-readable units

Parameters:
  • bytect (int) – Number of bytes
  • base10 (bool) – Convert to base-10 units (MB, GB, etc) if True
  • fmt (str or None) – Format of string to return; must contain %f/%d and %s for the quantity and units, respectively.
Returns:

Quantity and units expressed in a human-readable quantity

Return type:

str

tokio.common.isstr(obj)[source]

Determine if an object is a string or string-derivative

Provided for Python2/3 compatibility

Parameters:obj – object to be tested for stringiness
Returns:is it string-like?
Return type:bool
tokio.common.recast_string(value)[source]

Converts a string to some type of number or True/False if possible

Parameters:value (str) – A string that may represent an int or float
Returns:The most precise numerical or boolean representation of value if value is a valid string-encoded version of that type. Returns the unchanged string otherwise.
Return type:int, float, bool, str, or None
tokio.common.to_epoch(datetime_obj, astype=<type 'int'>)[source]

Convert datetime.datetime into epoch seconds

Currently assumes input datetime is expressed in localtime. Does not handle timezones very well. Once Python2 support is dropped from pytokio, this will be replaced by Python3’s datetime.datetime.timestamp() method.

Parameters:
  • datetime_obj (datetime.datetime) – Datetime to convert to seconds-since-epoch
  • astype – Whether you want the resulting timestamp as an int or float
Returns:

Seconds since epoch

Return type:

int or float