privex.helpers.common

Common functions and classes that don’t fit into a specific category

Copyright:

    +===================================================+
    |                 © 2019 Privex Inc.                |
    |               https://www.privex.io               |
    +===================================================+
    |                                                   |
    |        Originally Developed by Privex Inc.        |
    |        License: X11 / MIT                         |
    |                                                   |
    |        Core Developer(s):                         |
    |                                                   |
    |          (+)  Chris (@someguy123) [Privex]        |
    |          (+)  Kale (@kryogenic) [Privex]          |
    |                                                   |
    +===================================================+

Copyright 2019     Privex Inc.   ( https://www.privex.io )

Attributes

Functions

almost(compare, *numbers[, tolerance])

Compare two or more numbers, returning True if all numbers are no more than tolerance greater or smaller than than compare - otherwise False.

byteify(data[, encoding, if_none])

Convert a piece of data into bytes if it isn’t already.

call_sys(proc, *args[, write])

A small wrapper around subprocess.Popen which allows executing processes, while optionally piping data (write) into the process’s stdin, then finally returning the process’s output and error results.

camel_to_snake(name)

Convert name from camel case (HelloWorld) to snake case (hello_world).

chunked(iterable, n)

Split iterable into n iterables of similar size

construct_dict(cls, kwargs[, args, …])

Removes keys from the passed dict data which don’t exist on cls (thus would get rejected as kwargs) using get_function_params().

dec_round(amount[, dp, rounding])

Round a Decimal to x decimal places using quantize (dp must be >= 1 and the default dp is 2)

empty(v[, zero, itr])

Quickly check if a variable is empty or not.

empty_if(v[, is_empty, not_empty])

Syntactic sugar for x if empty(y) else z.

env_bool(env_key[, env_default])

Obtains an environment variable env_key, if it’s empty or not set, env_default will be returned.

env_cast(env_key, cast[, env_default])

Obtains an environment variable env_key, if it’s empty or not set, env_default will be returned.

env_csv(env_key[, env_default, csvsplit])

Quick n’ dirty parsing of simple CSV formatted environment variables, with fallback to user specified env_default (defaults to None)

env_decimal(env_key[, env_default])

Alias for env_cast() with Decimal casting

env_int(env_key[, env_default])

Alias for env_cast() with int casting

env_keyval(env_key[, env_default, valsplit, …])

Parses an environment variable containing key:val,key:val into a list of tuples [(key,val), (key,val)]

extract_settings(prefix[, _settings, …])

Extract prefixed settings from a given module, dictionary, class, or instance.

filter_form(form, *keys[, cast])

Extract the keys keys from the dict-like form if they exist and return a dictionary containing the keys and values found.

get_function_params(obj[, check_parents])

Extracts a function/method’s signature (or class constructor signature if a class is passed), and returns it as a dictionary.

human_name(class_name)

This function converts a class/function name into a Title Case name.

inject_items(items, dest_list, position)

Inject a list items after a certain element in dest_list.

io_tail(f[, nlines, bsz])

NOTE: If you’re only loading a small amount of lines, e.g. less than 1MB, consider using the much easier tail()

is_false(v[, chk_none])

Warning: Unless you specifically need to verify a value is Falsey, it’s usually safer to check for truth is_true() and invert the result, i.e.

is_true(v)

Check if a given bool/str/int value is some form of True:

parse_csv(line[, csvsplit])

Quick n’ dirty parsing of a simple comma separated line, with automatic whitespace stripping of both the line itself, and the values within the commas.

parse_keyval(line[, valsplit, csvsplit])

Parses a csv with key:value pairs such as.

random_str([size, chars])

Generate a random string of arbitrary length using a given character set (string / list / tuple).

reverse_io(f[, blocksize])

Read file as series of blocks from end of file to start.

shell_quote(*args)

Takes command line arguments as positional args, and properly quotes each argument to make it safe to pass on the command line.

stringify(data[, encoding, if_none])

Convert a piece of data into a string (from bytes) if it isn’t already.

tail(filename[, nlines, bsz])

Pure python equivalent of the UNIX tail command.

Classes

ErrHelpParser([prog, usage, description, …])

ErrHelpParser - Use this instead of argparse.ArgumentParser to automatically get full help output as well as the error message when arguments are invalid, instead of just an error message.