Skip to main content

A Python Implementation of the Reflectivity API from the Pharo language

Project description

Reflectivity: A Python Implementation of the Reflectivity API from the Pharo language

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:

.. code-block:: python

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, node)

a = ExampleClass()

# When we don't need it anymore, we remove it
print 'Uninstall Metalink'

# 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.


Currently, Reflectivity is not yet on ``pypi``, so you can install it using
``pip``. It is recommanded to install it in the virtualenv.

.. code-block:: bash
$ pip install -e .

Quick Start


* Steven Costiou (`@StevenCostiou <>`_), main author of Reflectivipy
* Vincent Aranega (`@aranega <>`_)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reflectivipy-0.0.1.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

reflectivipy-0.0.1-py2-none-any.whl (13.0 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page