Skip to main content

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.

Metadata

Release files for aspectify 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aspectify 1.0.0
File Size Uploaded
aspectify-1.0.0.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aspectify 1.0.0
File Interpreter ABI Platform
aspectify-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / aspectify-1.0.0.tar.gz

Download URL aspectify-1.0.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5ca33906992ab99bdf33b4921a3a897ff73f24cd3e3ca081158f005947d15797
BLAKE2b-256 checksum
How to use checksums
90a27ab312dbe9dcc01fcd2a334a85147d317631e13e91d05e9b374e03741955
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release files / aspectify-1.0.0-py3-none-any.whl

Download URL aspectify-1.0.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
878b9428ea561335118cce3235baabee97a22ab0405e0e125a5e1afa82fb7838
BLAKE2b-256 checksum
How to use checksums
e5007b8d7ae79899a1a46184ed7929ab8125fe6e477581b9587cc11a38a02bd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page