Last updated:
0 purchases
aspectify 1.0.0
aspectify
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
(what): a cross-cutting concept. In fact, an
Aspect will
group some functionalies. These, which will modify the natural behaviour
of a method, are called Advices. - PointCut (when): a fragment of
code where the
Aspect is
defined. Can be multiple PointCuts for each
Aspect (indeed,
it will). - Advice (when and what to do): The code fragment to
execute when the PointCut occurs and the moment when the new behaviuor
must occur. Originally, only three moments were defined (before,
around –instead of– and after), but nowadays new moments are
defined, such as “after throwing an exception” (after_throwing) or
“after NOT throwing an exception” (after_returning).
Installation
In order to install the library, it is only needed to execute the pypi
comand that follows:
pip install aspectify
ADVICE: You should use a virtual environment to install the
packages associated with your proyect.
Why do we need Aspectify
Once the background is defined and the library is 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, we will use
the random library for Python.
from random import Random
Now, we can use it to generate some integers.
r = Random()
r.randint(5, 10)
9
As you can see in its documentation, randint (called with parameters
a and b) can generate the b value itself (it is a closed range
[5, 10]).
If we want to change this behaviour to the normal random functions
behaviour (the range is closed-opened [5, 10)), you will need to
redefine it. Furthermore, if other functions or library use this method,
they will not use yours.
How can we solve it? Using AOP.
During this introduction, we have seen the background concepts and how
to install the Aspectify library. In the next section we will explain
how to use it with a simple example.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.