module documentation

Helper functions for commonly used utilities.

Function parse_unique_urlencoded Parses unique key-value parameters from urlencoded content.
Function positional A decorator to declare that only the first N arguments may be positional.
Function update_query_params Updates a URI with new query parameters.
Constant POSITIONAL_EXCEPTION Undocumented
Constant POSITIONAL_IGNORE Undocumented
Constant POSITIONAL_SET Undocumented
Constant POSITIONAL_WARNING Undocumented
Variable logger Undocumented
Function _add_query_parameter Adds a query parameter to a url.
Constant _IS_DIR_MESSAGE Undocumented
Constant _MISSING_FILE_MESSAGE Undocumented
Constant _SYM_LINK_MESSAGE Undocumented
def parse_unique_urlencoded(content):

Parses unique key-value parameters from urlencoded content.

Parameters
contentstring, URL-encoded key-value pairs.
Returns
dict, The key-value pairs from content.
Raises
ValueErrorif one of the keys is repeated.
def positional(max_positional_args):

A decorator to declare that only the first N arguments may be positional.

This decorator makes it easy to support Python 3 style keyword-only parameters. For example, in Python 3 it is possible to write:

def fn(pos1, *, kwonly1=None, kwonly2=None):
    ...

All named parameters after * must be a keyword:

fn(10, 'kw1', 'kw2')  # Raises exception.
fn(10, kwonly1='kw1')  # Ok.

Example

To define a function like above, do:

@positional(1)
def fn(pos1, kwonly1=None, kwonly2=None):
    ...

If no default value is provided to a keyword argument, it becomes a required keyword argument:

@positional(0)
def fn(required_kw):
    ...

This must be called with the keyword parameter:

fn()  # Raises exception.
fn(10)  # Raises exception.
fn(required_kw=10)  # Ok.

When defining instance or class methods always remember to account for self and cls:

class MyClass(object):

    @positional(2)
    def my_method(self, pos1, kwonly1=None):
        ...

    @classmethod
    @positional(2)
    def my_method(cls, pos1, kwonly1=None):
        ...

The positional decorator behavior is controlled by _helpers.positional_parameters_enforcement, which may be set to POSITIONAL_EXCEPTION, POSITIONAL_WARNING or POSITIONAL_IGNORE to raise an exception, log a warning, or do nothing, respectively, if a declaration is violated.

Parameters
max_positional_argsUndocumented
max_positional_argumentsMaximum number of positional arguments. All parameters after this index must be keyword only.
Returns
A decorator that prevents using arguments after max_positional_args from being used as positional parameters.
Raises
TypeErrorif a keyword-only argument is provided as a positional parameter, but only if _helpers.positional_parameters_enforcement is set to POSITIONAL_EXCEPTION.
def update_query_params(uri, params):

Updates a URI with new query parameters.

If a given key from params is repeated in the uri, then the URI will be considered invalid and an error will occur.

If the URI is valid, then each value from params will replace the corresponding value in the query parameters (if it exists).

Parameters
uristring, A valid URI, with potential existing query parameters.
paramsdict, A dictionary of query parameters.
Returns
The same URI but with the new query parameters added.
POSITIONAL_EXCEPTION: str =

Undocumented

Value
'EXCEPTION'
POSITIONAL_IGNORE: str =

Undocumented

Value
'IGNORE'
POSITIONAL_SET =

Undocumented

Value
frozenset([POSITIONAL_WARNING, POSITIONAL_EXCEPTION, POSITIONAL_IGNORE])
POSITIONAL_WARNING: str =

Undocumented

Value
'WARNING'
logger =

Undocumented

def _add_query_parameter(url, name, value):

Adds a query parameter to a url.

Replaces the current value if it already exists in the URL.

Parameters
urlstring, url to add the query parameter to.
namestring, query parameter name.
valuestring, query parameter value.
Returns
Updated query parameter. Does not update the url if value is None.
_IS_DIR_MESSAGE: str =

Undocumented

Value
'{0}: Is a directory'
_MISSING_FILE_MESSAGE: str =

Undocumented

Value
'Cannot access {0}: No such file or directory'
_SYM_LINK_MESSAGE: str =

Undocumented

Value
'File: {0}: Is a symbolic link.'