Metadata-Version: 2.1
Name: aspectify
Version: 0.0.2
Summary: Apply Aspect Oriented Programming to your Python code
Home-page: https://github.com/ruescog/aspectify
Author: ruescog
Author-email: ruescog@unirioja.es
License: Apache Software License 2.0
Keywords: nbdev jupyter notebook python
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Provides-Extra: dev

aspectify
================

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Aspect Oriented Programming is a programming paradigm that allows the
user to separate some cross-cutting content from the main code, such as
the logging or a database connection.

As it may be known, other programming languages has some functionallity
(implemented or plugged in) to use this paradigm as an additional
abstract layer to the core application. AspectJ (used in Java), can
sound familiar to the reader.

In order to bring this amazing and powerful functionality to Python
(which, in addition, will allow us to add it dynamically instead of
using a weaver –as it is done in Java–), we have defined `Aspectify`, a
Python library to manage AOP.

## Background concepts

Before introducing the library, it is important to define some concepts
used in AOP. Those are: -
[`Aspect`](https://ruescog.github.io/aspectify/aop.html#aspect)
(*what*): a cross-cutting concept. In fact, an
[`Aspect`](https://ruescog.github.io/aspectify/aop.html#aspect) will
group some functionalies. These, which will modify the natural behaviour
of a method, are called `Advice`s. - `PointCut` (*when*): the time at
which the new behaviuor must occur. Originally, only three points in
time were defined (`before`, `around` and `after`), but nowadays new
points in time are defined, such as “after throwing an exception”. -
`Advice` (*what to do*): as mentioned above, the code fragment to
execute when the `PointCut` occurs.

## Installation

In order to install the library, it is only needed to execute the pypi
comand that follows:

``` sh
pip install aspectify
```

> **ADVICE**: You should use a virtual environment to install the
> packages associated with your proyect.

## How to use Aspectify

Once the background is defined and the library installed, we can start
to create the AOP layer to our projects.

### The core project

In order to use the library, we need a project. For example, here is a
`Calculator` program, that will apply the basic operators to two values
entered by keyboard.

``` python
class Calculator():
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def add(self):
        return self.a + self.b

    def subtract(self):
        return self.a - self.b

    def multiply(self):
        return self.a * self.b

    def divide(self):
        return self.a / self.b
```

Now, we can use it to operate some integers.

``` python
a = 10
b = 20
operator = "+"
calculator = Calculator(a, b)

if operator == "+":
    print(calculator.add())
elif operator == "-":
    print(calculator.subtract())
elif operator == "*":
    print(calculator.multiply())
elif operator == "/":
    print(calculator.divide())
```

    30

A bit rudimentary, but it works.

Now, let us suppose that this `Calculator` program is a library. In
addition, let us suppose that we did not write it, and we did not have
the access to they parts: we just know that a `Calculator` class exists
and it has four methods: `add`, `subtract`, `multiply` and `divide`.

### AOP: definition

At this moment, we want to modify, let us say, the `add` method
behaviour to add the following message: “Adding some numbers”. We can
use the library to create an
[`Aspect`](https://ruescog.github.io/aspectify/aop.html#aspect).

> ADVICE: Trying to be as clean as possible, we strongly recomend you to
> create a **new** `.py` file and add the
> [`Aspect`](https://ruescog.github.io/aspectify/aop.html#aspect)s to
> this file.

First of all, we need to import to the
[`Aspect`](https://ruescog.github.io/aspectify/aop.html#aspect)s file
(`aspects.py` from now on) the libraries and modules that will be used.
In this case, the `Calculator`.

``` python
# aspects.py file

# import Calculator
from aspectify.aop import Aspect, is_detectable
```

First of all, we need to define a method to gather all the classes
loaded in the `aspects.py` file, in order to modify their behaviour
dynamically. This method will be always be the same:

``` python
def get_classes():
    return [element for element in list(globals().items()) if is_detectable(*element)]
```

At this point, we can define our first
[`Aspect`](https://ruescog.github.io/aspectify/aop.html#aspect). We will
call it “add-message”, and it will show a message `before` the addition
is done.

``` python
add_message_aspect = Aspect("add-message")
add_message_aspect.set_before(lambda: print("Adding some numbers"))
```

Finally, we will create a `PointCut` that will define which methods will
show this message. We will use the regular expression `.*add`.

``` python
add_message_aspect.create_pointcut(get_classes(), ".*add")
```

    AttributeError: 'ZMQExitAutocall' object has no attribute '__name__'


