A Python Implementation of the Reflectivity API from the Pharo language
Project description
Reflectivipy is an API inspired by Reflectivity in Pharo. Reflectivity allows you to deal with partial behavioral reflection in Python by letting you install MetaLink directly on method AST nodes. Moreover, Reflectivity provides object-centric capabilities and let you install a modified behavior on a dedicated object.
Let see how to install a link on a method AST towards a meta-object:
import reflectivipy
# We define a new meta-object that will act as a logger
# each time a dedicated AST node will be "visited/executed"
class MetaLogger(object):
def log_me(self):
print("I'm here")
# Here is the class we will instrument
class ExampleClass(object):
def foo(self):
print('Executing foo')
# We create a link ('control' is 'before' by default)
link = reflectivipy.MetaLink(MetaLogger(), selector='log_me', control='before')
# We get the method AST we want to instrument
rf_ast = reflectivipy.reflective_ast_for_method(ExampleClass, 'foo')
# We select the node that we want to install the link on
# Here we selected the "print 'Executing foo'" AST node.
node = rf_ast.body[0].body[0]
# We install the link on the node
reflectivipy.link(link, node)
a = ExampleClass()
a.foo()
# When we don't need it anymore, we remove it
print('Uninstall Metalink')
link.uninstall()
a.foo()
# Produces:
#
# I'm here
# Executing foo
# Uninstall Metalink
# Executing foo
This small code example uses the two main Reflectivipy concepts:
the meta-object definition, i.e: the object that will own the behavior to add
the MetaLink in itself which link the meta-object to the AST node that must be modified.
The MetaLink link is used to install a new behavior before the code associated to the AST node on which it will be installed. The method AST is then gathered using the reflective_ast_for_method function. The desired AST node is gathered (here it’s the print node). Finally, the node and the meta-behavior are linked together using the link function. Once the new meta-behavior is not required anymore, the uninstall method of the created link is called. This call uninstall the link from every node it could be installed on.
On top of that, meta-behavior can be installed for a dedicated instance instead of a class. To do that, it’s just a matter of asking for the reflective_ast_for_method of the instance instead of the one from the class. The code remains then exactly the same.
Installation
Currently, Reflectivity is now on pypi, so you can install it using pip. It is recommanded to install it in the virtualenv.
$ pip install reflectivipy
Quick Start
Contributors
Steven Costiou (@StevenCostiou), main author of Reflectivipy
Vincent Aranega (@aranega)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file reflectivipy-0.1.1.tar.gz
.
File metadata
- Download URL: reflectivipy-0.1.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | db3f85f28845fe3ef977ff07c6501f26a5c697015c0dd194cf7b8c8831485ae3 |
|
MD5 | b82e2e2e73ec717ce26e8a05ae67a589 |
|
BLAKE2b-256 | 1e71fa571718677f557037dd5890457e9b72bf43e28116e904d93fb07ae71ead |
File details
Details for the file reflectivipy-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: reflectivipy-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08fc7ec6b76186e06ddc4058e9f30066977cd9ef2951fc224f48988fc415813a |
|
MD5 | 056f23607d234cb8faf143db4ee30d9d |
|
BLAKE2b-256 | 14e1b1ef3b9857b57d3ba266753342c0748a96fc28ff3c489acf969e17e5ca8f |