link.utils
stable
  • Tutorial
  • API documentation
    • link.utils package
      • Submodules
      • link.utils.filter module
      • link.utils.log module
      • link.utils.grammar module
      • Module contents
link.utils
  • Docs »
  • API documentation »
  • link.utils package
  • Edit on GitHub

link.utils package¶

Submodules¶

link.utils.filter module¶

class link.utils.filter.Filter(rule, *args, **kwargs)[source]¶

Bases: object

Apply MongoDB filter rule on dictionary.

handle_all_field(key, rule, obj)[source]¶

Handle $all operator.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if field match all values

Return type:

boolean

handle_and(key, rule, obj)[source]¶

Handle $and operator.

Parameters:
  • key (str) – key to check in dictionary (unused)
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if dictionary match all sub-filters

Return type:

boolean

handle_field(key, rule, obj)[source]¶

Handle filter on field.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if dictionary match

Return type:

boolean

handle_field_cond(key, rule, obj, cond)[source]¶

Handle comparison operators.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
  • cond (callable) – comparison operator
Returns:

True if field match

Return type:

boolean

handle_field_exists(key, rule, obj)[source]¶

Handle $exists operator.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if field is in dictionary

Return type:

boolean

handle_field_regex(key, pattern, obj, opts=None)[source]¶

Handle $regex operator.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if field match regex

Return type:

boolean

handle_field_rule(key, rule, obj)[source]¶

Handle other operators.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if field match

Return type:

boolean

handle_in_field(key, rule, obj)[source]¶

Handle $in operator.

Parameters:
  • key (str) – key to check in dictionary
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if field match at least one value

Return type:

boolean

handle_nor(key, rule, obj)[source]¶

Handle $nor operator.

Parameters:
  • key (str) – key to check in dictionary (unused)
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if dictionary doesn’t match any sub-filters

Return type:

boolean

handle_or(key, rule, obj)[source]¶

Handle $or operator.

Parameters:
  • key (str) – key to check in dictionary (unused)
  • rule (dict) – MongoDB sub-filter
  • obj (dict) – dictionary to check
Returns:

True if dictionary match at least one sub-filter

Return type:

boolean

match(obj)[source]¶

Check if dictionary match the MongoDB filter.

Parameters:obj (dict) – dictionary to check
Returns:True if dictionary match
Return type:boolean
class link.utils.filter.Mangle(rule, *args, **kwargs)[source]¶

Bases: object

Apply MongoDB update spec on dictionary.

addToSet(rule, obj)[source]¶

Handle $addToSet operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
bit(rule, obj)[source]¶

Handle $bit operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
currentDate(rule, obj)[source]¶

Handle $currentDate operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
inc(rule, obj)[source]¶

Handle $inc operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
max(rule, obj)[source]¶

Handle $max operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
min(rule, obj)[source]¶

Handle $min operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
mul(rule, obj)[source]¶

Handle $mul operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
pop(rule, obj)[source]¶

Handle $pop operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
pull(rule, obj)[source]¶

Handle $pull operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
pullAll(rule, obj)[source]¶

Handle $pullAll operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
push(rule, obj)[source]¶

Handle $push operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
rename(rule, obj)[source]¶

Handle $rename operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
set(rule, obj)[source]¶

Handle $set operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
unset(rule, obj)[source]¶

Handle $unset operator.

Parameters:
  • rule (dict) – sub-spec
  • obj (dict) – dictionary to apply sub-rule on
link.utils.filter.del_field(key, obj)[source]¶

Remove field from dictionary.

Parameters:
  • key (str) – path to field in dictionary
  • obj (dict) – dictionary used for search
link.utils.filter.get_field(key, obj)[source]¶

Get field in dictionary.

Parameters:
  • key (str) – path to field in dictionary
  • obj (dict) – dictionary used for search
Returns:

field value

link.utils.filter.set_field(key, obj, val)[source]¶

et field in dictionary.

Parameters:
  • key (str) – path to field in dictionary
  • obj (dict) – dictionary used for search
  • val – value to set

link.utils.log module¶

class link.utils.log.ConfigurableLogger(name)[source]¶

Bases: logging.Logger

Configurable logger.

log_filter¶
log_format¶
log_level¶
class link.utils.log.LogFilter(name='')[source]¶

Bases: logging.Filter

Filter log records using link.utils.filter.Filter.

filter(record)[source]¶
log_filter¶
link.utils.log.logrecord_to_dict(record)[source]¶

Transform a LogRecord object into a dict.

Parameters:record (LogRecord) – record to transform
Returns:record as a JSON serializable dict
Return type:dict

link.utils.grammar module¶

link.utils.grammar.codegenerator(modname, prefix, grammar)[source]¶

Parse grammar model and generate Python code allowing to parse it.

Example:

with open('grammar.bnf') as f:
    module = codegenerator('mydsl', 'MyDSL', f.read())

assert module.__name__ == 'mydsl'
parser = module.MyDSLParser()
Parameters:
  • modname (str) – Name of the generated Python module
  • prefix (str) – Prefix used to name the parser
  • grammar (str) – Grammar describing the language to parse
Returns:

Generated Python module

Return type:

module

link.utils.grammar.find_ancestor(node, classname)[source]¶

Find first node’s ancestor which match class’ name.

Parameters:
  • node (grako.model.Node) – Grako Node
  • classname (str) – Class’ name
Returns:

Node’s ancestor or None if not found

Return type:

grako.model.Node

Module contents¶

Previous

© Copyright 2016, David Delassus. Revision f2f41725.

Built with Sphinx using a theme provided by Read the Docs.