Skip to main content
# Concerns

[Python](https://www.python.org/) implementation of the
[Ruby](https://www.ruby-lang.org/)’s
[ActiveSupport::Concern](https://api.rubyonrails.org/v5.1/classes/ActiveSupport/Concern.html)
behavior.

## Use examples

Note: the class context is the fourth argument of the
[metaclass](https://www.python.org/doc/essays/metaclasses/) `__init__` method,
formally “namespace”:

```python
def __init__(cls, name: str, bases: tuple, namespace: dict) -> None:
...
```

It is a dictionary representation of the class body.

### Using a class as mix-in

If the aspect is a class, its context will be merged to the target context on
its factoring.

```python
from typing import NamedTuple
from concerns import concern


class SerialMixin:

def serialize(self) -> str:
return json.dumps(self._asdict(), sort_keys=True)

@classmethod
def deserialize(cls, data: str):
return cls(**json.loads(data))


class PersonBase(NamedTuple):
name: str
surname: str


class Person(PersonBase):
concern(SerialMixin)
```

### Using a mapping as default values map

If the aspect is a mapping (a dictionary, by example), it itself will be merged
to the target context on its factoring.

```python
from concerns import concern

class Animal:
# Too simple use, avoid doing it
concern({voice='grrr'})

def __init__(self, *, voice: str = None):
if voice:
self.voice = voice

def say(self) -> None:
print(self.voice)
```

### Function to change behavior

If the aspect is a callable, the target class context will be passed as
parameter to the aspect for processing before its factoring.

```python
from concerns import concern
from collections.abc import MutableMapping
from datetime import datetime

def make_timed(context: MutableMapping) -> None:
"""
Reusable aspect function to add creating time to objects
"""

def set_created_attr(attr: str) -> None:
context[attr] = None
def new(cls, name: str, bases: tuple, _dict: dict):
self = super().__new__(cls, name, bases, _dict)
setattr(self, attr) = datetime.now()
return self
context['__new__'] = new
context['set_created_attr'] = set_created_attr


class Register:
concern(make_timed)

... # more class attributes

set_created_attr('created_at')
```


Release files for concerns 1.0

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

Built distribution (wheel)

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

Release files / concerns-1.0-py3-none-any.whl

Download URL concerns-1.0-py3-none-any.whl
Size 2.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
11c8621e42adb2f263a7bfb8c6151c4478df7c78246406936e618d486a350df0
BLAKE2b-256 checksum
How to use checksums
91b5968af51879e395fb61703ceaaed932f45c881c88355fcddacf69849b55de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

Release history Release notifications | RSS feed

This release

1.0 This release

1 release file

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