Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
ep12 edited this page Mar 29, 2020 · 1 revision

Welcome to the PyOPM wiki!

This is the place where you can find what you want to know about PyOPM.

Installation

To get started with PyOPM, you can install it via pip:

$ pip3 install -U pyopm

If necessary, consider using the --user flag if your user account does not have write access to the site-packages folder of your Python 3 installation.

Using PyOPM

Here is a simple example of how to use PyOPM:

from pyopm import ObjectPattern

p = ObjectPattern({
    'obj': {'eval': [lambda o: isinstance(o, dict)]},
    'obj.keys': {'eval': [lambda k: all(isinstance(x, (int, str)) for x in k())],
                 'bind': {'keys': lambda o: o()}},
    'obj.values': {'bind': {'values': lambda o: o()}},
    'obj.items': {'eval': [lambda i: all(isinstance(y, float if isinstance(x, int) else int)
                                         for x, y in i())],
                  'bind': {'items': lambda i: list(i())}},
})

m = p.match({0, 1, 2})  # not a dict -> m is None
print(type(m))
m = p.match({0: 0, 'one': 1})  # 0: 0 does not match the rules -> m is None
print(type(m))
m = p.match({0: 0.2, 'one': 1})  # match!
print(type(m))

with m:  # magic: use the objects bound to the names specified above
    print(keys)
    print(values)
    print(list(zip(keys, values)))  # should be the same as...
    print(items)  # ...this!

This snippet should print

<class 'NoneType'>
<class 'NoneType'>
<class 'pyopm.core.ObjectPatternMatch'>
dict_keys([0, 'one'])
dict_values([0.2, 1])
[(0, 0.2), ('one', 1)]
[(0, 0.2), ('one', 1)]
Clone this wiki locally