Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

pyopm.core

ep12 edited this page Mar 29, 2020 · 2 revisions

pyopm.core

This module contains the following:

classes:

  • ObjectPattern
  • ObjectPatternMatch
  • ObjectMultiPattern
  • NoMatchingPatternError
  • AmbiguityError

functions:

  • break_attr_path (internal function)
  • _start_block (internal function)
  • _calculate_actions (internal function)

Public Classes and Functions

class ObjectPattern

This class represents a pattern that can be applied to objects using the match method. If the object matches the pattern, an instance of ObjectPatternMatch is returned, else match returns None.

ObjectPattern.__init__:
ObjectPatterns are initialised with a dict:

p = ObjectPattern({
    'obj': {
        'eval': [lambda o: callable(o) or o is None]
    },
    'obj.__class__': {
        'eval': [lambda o: isinstance(o, type)],
        'bind': {'obj_type': None, 'obj_type_str': str}
    },
    'obj.__doc__': {
        'bind': {'docstring': None}
    }
})

Each key represents an attribute that the object should have where obj is the object itself. Each value specifies further restrictions (eval key) and bindings (bind key).
Let's start with the eval checks: The value of the eval item must be a list of callables [f1, f2, ...]. They are called with the attribute object (roughly o = eval(key)): f1(o). They should return a bool value: True if and only if the object obj or rather the attribute object o matches the requirements.
The bind dict is a mapping of desired variable names (key) and a way to calculate them (value):

  • If the value is a callable get_value_to_bind, then get_value_to_bind(o) will be the value that can be bound using a with block.
  • If the value is a string s, the value to bind will roughly be eval(s). Please avoid using this functionality.
  • If the value is None (or anything else), the attribute object o itself will be bound to the variable name (later). Convention: Please use None.

This is fairly abstract. Consider

m = p.match(list)

All tests pass and m is an ObjectPatternMatch object (not None). Let's have a look at m.bound:

{'obj_type': type,
 'obj_type_str': "<class 'type'>",
 'docstring': 'Built-in mutable sequence.\n\nIf no argument is given, the constructor creates a new empty list.\nThe argument must be an iterable if specified.'}

Compare that with list.__class__, str(list.__class__) and list.__doc__, respectively:

type
"<class 'type'>"
'Built-in mutable sequence.\n\nIf no argument is given, the constructor creates a new empty list.\nThe argument must be an iterable if specified.'

See? If we now do

with m:
    print(obj_type)
    print(docstring)

obj_type and docstring can be used as usual. Note that we never explicitly defined them. The values were bound when entering the with block and they will be removed or restored to the values they might have had before when leaving the with block.

The exact behaviour is configurable using the config dict passed to ObjectPattern.__init__. See pyopm.core.CONFIG for the default configuration.

Warning: The default configuration pyopm.core.CONFIG might change with upcoming releases!

Clone this wiki locally