# Attrsx
**`attrsx` β A Lightweight, Playful Extension to `attrs`**
π§ **What is `attrsx`?**
`attrsx` is a minimal, lightweight extension of the popular `attrs` library, designed to seamlessly add logging capabilities to your `attrs` classes.
---
### **β¨ What It Does:**
- π οΈ **Extends `attrs.define`** β Behaves exactly like `attrs.define`, but with automatic logger initialization.
- ποΈ **Built-in Logging Fields** β Adds fields for logger name, level, and format β all customizable per class or instance.
- π **Non-Intrusive** β If you donβt need logging, `attrsx` functions just like plain old `attrs`.
---
### **π― Why `attrsx`?**
- π You sometimes just want **simple logging** without writing the same boilerplate over and over.
- π `attrsx` lets you keep things clean β adding just a little "magic" to `attrs`-based classes.
- π± Itβs `attrs`, but with room for playful future extensions.
---
### **π§ Features at a Glance:**
- β
**Fully compatible** with `attrs` 22.2.0 and later.
- π§© Supports **all `attrs.define` parameters** β like `slots`, `frozen`, and more.
- π§ Logger defaults to the class name but can be **overridden effortlessly**.
---
β¨ Itβs `attrs`, but with just a little... **extra**. π
```python
import attrsx
import attrs
```
## Usage
### 1. Built-in logger
One of the primary extensions in `attrsx` is `automatic logging`. It can be accessed via `self.logger` in any `attrsx`-decorated class.
#### Example: Basic Logger Usage
```python
@attrsx.define
class ProcessData:
data: str = attrs.field(default=None)
def run(self):
self.logger.info("Running data processing...")
self.logger.debug(f"Processing data: {self.data}")
return f"Processed: {self.data}"
```
```python
ProcessData(data = "data").run()
```
INFO:ProcessData:Running data processing...
'Processed: data'
#### Logger Configuration
The logging behavior can be customized using the following optional attributes:
- `loggerLvl` : Sets the log level (from `logging`), defaults to `logging.INFO`.
- `logger_name` : Specifies the logger name; defaults to the class name.
- `logger_format` : Sets the logging message format; defaults to `%(levelname)s:%(name)s:%(message)s`.
`self.logger` becomes available starting from `__attrs_post_init__`.
```python
import logging
@attrsx.define
class ProcessData2:
data: str = attrs.field(default=None)
# optional attributes
loggerLvl: int = attrs.field(default=logging.DEBUG)
logger_name : str = attrs.field(default="ProcessData")
logger_format : str = attrs.field(default="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
def __attrs_post_init__(self):
self.logger.info("Custom post-init logic running!")
self.data = "DATA"
def run(self):
self.logger.info("Running data processing...")
self.logger.debug(f"Processing data: {self.data}")
return f"Processed: {self.data}"
```
```python
ProcessData2(data = "data").run()
```
2025-01-02 23:26:02,710 - ProcessData - INFO - Custom post-init logic running!
2025-01-02 23:26:02,711 - ProcessData - INFO - Running data processing...
2025-01-02 23:26:02,711 - ProcessData - DEBUG - Processing data: DATA
'Processed: DATA'
#### Using External Loggers
An external, pre-initialized logger can also be provided to the class using the `logger` attribute.
```python
ProcessData2(
data = "data",
logger = ProcessData().logger
).run()
```
INFO:ProcessData:Custom post-init logic running!
INFO:ProcessData:Running data processing...
'Processed: DATA'
Raw data
{
"_id": null,
"home_page": "https://kiril-mordan.github.io/reusables/attrsx/",
"name": "attrsx",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "aa-paa-tool",
"author": "Kyrylo Mordan",
"author_email": "parachute.repo@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/66/9d/085470ba89ed0f362efab31b59560ee2acf3a230dcd4252e37a3a3c34a6e/attrsx-0.0.1.tar.gz",
"platform": null,
"description": "# Attrsx\n\n**`attrsx` \u2013 A Lightweight, Playful Extension to `attrs`** \n\n\ud83d\udd27 **What is `attrsx`?** \n`attrsx` is a minimal, lightweight extension of the popular `attrs` library, designed to seamlessly add logging capabilities to your `attrs` classes. \n\n---\n\n### **\u2728 What It Does:** \n- \ud83d\udee0\ufe0f **Extends `attrs.define`** \u2013 Behaves exactly like `attrs.define`, but with automatic logger initialization. \n- \ud83c\udf9b\ufe0f **Built-in Logging Fields** \u2013 Adds fields for logger name, level, and format \u2013 all customizable per class or instance. \n- \ud83d\udd0d **Non-Intrusive** \u2013 If you don\u2019t need logging, `attrsx` functions just like plain old `attrs`. \n\n---\n\n### **\ud83c\udfaf Why `attrsx`?** \n- \ud83d\ude80 You sometimes just want **simple logging** without writing the same boilerplate over and over. \n- \ud83d\ude0c `attrsx` lets you keep things clean \u2013 adding just a little \"magic\" to `attrs`-based classes. \n- \ud83c\udf31 It\u2019s `attrs`, but with room for playful future extensions. \n\n---\n\n### **\ud83d\udea7 Features at a Glance:** \n- \u2705 **Fully compatible** with `attrs` 22.2.0 and later. \n- \ud83e\udde9 Supports **all `attrs.define` parameters** \u2013 like `slots`, `frozen`, and more. \n- \ud83d\udd27 Logger defaults to the class name but can be **overridden effortlessly**. \n\n---\n\n\u2728 It\u2019s `attrs`, but with just a little... **extra**. \ud83d\ude04\n\n```python\nimport attrsx\nimport attrs\n```\n\n## Usage\n\n### 1. Built-in logger\n\nOne of the primary extensions in `attrsx` is `automatic logging`. It can be accessed via `self.logger` in any `attrsx`-decorated class.\n\n#### Example: Basic Logger Usage\n\n\n```python\n@attrsx.define\nclass ProcessData:\n data: str = attrs.field(default=None)\n\n def run(self):\n self.logger.info(\"Running data processing...\")\n self.logger.debug(f\"Processing data: {self.data}\")\n return f\"Processed: {self.data}\"\n\n```\n\n\n```python\nProcessData(data = \"data\").run()\n```\n\n INFO:ProcessData:Running data processing...\n\n\n\n\n\n 'Processed: data'\n\n\n\n#### Logger Configuration\n\nThe logging behavior can be customized using the following optional attributes:\n\n- `loggerLvl` : Sets the log level (from `logging`), defaults to `logging.INFO`.\n- `logger_name` : Specifies the logger name; defaults to the class name.\n- `logger_format` : Sets the logging message format; defaults to `%(levelname)s:%(name)s:%(message)s`.\n\n`self.logger` becomes available starting from `__attrs_post_init__`.\n\n\n```python\nimport logging\n\n@attrsx.define\nclass ProcessData2:\n data: str = attrs.field(default=None)\n \n # optional attributes\n loggerLvl: int = attrs.field(default=logging.DEBUG) \n logger_name : str = attrs.field(default=\"ProcessData\")\n logger_format : str = attrs.field(default=\"%(asctime)s - %(name)s - %(levelname)s - %(message)s\")\n\n def __attrs_post_init__(self):\n self.logger.info(\"Custom post-init logic running!\")\n self.data = \"DATA\"\n\n def run(self):\n self.logger.info(\"Running data processing...\")\n self.logger.debug(f\"Processing data: {self.data}\")\n return f\"Processed: {self.data}\"\n```\n\n\n```python\nProcessData2(data = \"data\").run()\n```\n\n 2025-01-02 23:26:02,710 - ProcessData - INFO - Custom post-init logic running!\n 2025-01-02 23:26:02,711 - ProcessData - INFO - Running data processing...\n 2025-01-02 23:26:02,711 - ProcessData - DEBUG - Processing data: DATA\n\n\n\n\n\n 'Processed: DATA'\n\n\n\n#### Using External Loggers\n\nAn external, pre-initialized logger can also be provided to the class using the `logger` attribute.\n\n\n```python\nProcessData2(\n data = \"data\",\n logger = ProcessData().logger\n).run()\n```\n\n INFO:ProcessData:Custom post-init logic running!\n INFO:ProcessData:Running data processing...\n\n\n\n\n\n 'Processed: DATA'\n\n\n",
"bugtrack_url": null,
"license": "mit",
"summary": "A lightweight extension of attrs that adds extras like logging.",
"version": "0.0.1",
"project_urls": {
"Homepage": "https://kiril-mordan.github.io/reusables/attrsx/"
},
"split_keywords": [
"aa-paa-tool"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "952406f10aa119d602413d95a718f89019f76debaecd74390ff107f098e6ad05",
"md5": "d01f6de5d67d70835445975992040c6a",
"sha256": "a1def6b84d9202e14bcc175c85a5aa157a959576ad625916227eeda7b356c203"
},
"downloads": -1,
"filename": "attrsx-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d01f6de5d67d70835445975992040c6a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 624472,
"upload_time": "2025-01-02T22:38:37",
"upload_time_iso_8601": "2025-01-02T22:38:37.316583Z",
"url": "https://files.pythonhosted.org/packages/95/24/06f10aa119d602413d95a718f89019f76debaecd74390ff107f098e6ad05/attrsx-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "669d085470ba89ed0f362efab31b59560ee2acf3a230dcd4252e37a3a3c34a6e",
"md5": "5a666ea8b52e33cb85c821bbb708fbe7",
"sha256": "5b0b567451c35268c1410484182f7d013d93d687c9c1dd1848447730d34ebd12"
},
"downloads": -1,
"filename": "attrsx-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "5a666ea8b52e33cb85c821bbb708fbe7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 586058,
"upload_time": "2025-01-02T22:38:41",
"upload_time_iso_8601": "2025-01-02T22:38:41.103552Z",
"url": "https://files.pythonhosted.org/packages/66/9d/085470ba89ed0f362efab31b59560ee2acf3a230dcd4252e37a3a3c34a6e/attrsx-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-02 22:38:41",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "attrsx"
}