Skip to main content

Apply Aspect Oriented Programming to your Python code

Project description

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.

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

aspectify-1.0.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

aspectify-1.0.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file aspectify-1.0.0.tar.gz.

File metadata

  • Download URL: aspectify-1.0.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for aspectify-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5ca33906992ab99bdf33b4921a3a897ff73f24cd3e3ca081158f005947d15797
MD5 9444e3abce59bebafec422cbff37ef44
BLAKE2b-256 90a27ab312dbe9dcc01fcd2a334a85147d317631e13e91d05e9b374e03741955

See more details on using hashes here.

File details

Details for the file aspectify-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: aspectify-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for aspectify-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 878b9428ea561335118cce3235baabee97a22ab0405e0e125a5e1afa82fb7838
MD5 a5c993f714d7669d616e401cdcf3fbf9
BLAKE2b-256 e5007b8d7ae79899a1a46184ed7929ab8125fe6e477581b9587cc11a38a02bd0

See more details on using hashes here.

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