Name | autoslot JSON |
Version |
2024.12.1
JSON |
| download |
home_page | http://pythonomicon.com |
Summary | Classes and metaclasses for easier ``__slots__`` handling. |
upload_time | 2024-12-06 14:42:53 |
maintainer | None |
docs_url | None |
author | Caleb Hattingh |
requires_python | None |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
.. image:: https://img.shields.io/badge/stdlib--only-yes-green.svg
:target: https://img.shields.io/badge/stdlib--only-yes-green.svg
.. image:: https://coveralls.io/repos/github/cjrh/autoslot/badge.svg?branch=master
:target: https://coveralls.io/github/cjrh/autoslot?branch=master
.. image:: https://img.shields.io/pypi/pyversions/autoslot.svg
:target: https://pypi.python.org/pypi/autoslot
.. image:: https://img.shields.io/github/tag/cjrh/autoslot.svg
:target: https://img.shields.io/github/tag/cjrh/autoslot.svg
.. image:: https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg
:target: https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg
.. image:: https://img.shields.io/pypi/v/autoslot.svg
:target: https://img.shields.io/pypi/v/autoslot.svg
.. image:: https://img.shields.io/badge/calver-YYYY.MM.MINOR-22bfda.svg
:target: http://calver.org/
autoslot
========
Automatic "__slots__".
Demo
----
.. code-block:: python
from autoslot import Slots
class Compact(Slots):
def __init__(self, a, b):
self.x = a
self.y = b
This produces *exactly* the same class as if you had done:
.. code-block:: python
class Compact:
__slots__ = {'x', 'y'}
def __init__(self, a, b):
self.x = a
self.y = b
Simply: the code inside ``__init__()`` is scanned to find all assignments
to attributes on ``self``, and these are added as ``__slots__``.
The benefit of using ``autoslot.Slots`` over a manual slots declaration is
that you can modify the
code inside the ``__init__()`` method to add more attributes, and those
changes will *automatically* be reflected in the ``__slots__`` definition.
You can also have the best of both worlds: slots for fields you expect,
**as well as** a ``__dict__`` for those you don't:
.. code-block:: python
from autoslot import SlotsPlusDict
class SemiCompact(SlotsPlusDict):
def __init__(self, a, b):
self.x = a
self.y = b
inst = SemiCompact(1, 2)
inst.z = 123 # <-- This won't fail!
Attributes ``x`` and ``y`` will be stored in slots, while all other
dynamically-assigned attributes will go into the usual ``__dict__`` instance
inside the class. If most of your class's attributes appear in the ``__init__()``
method (these will become slots), then the space bloat caused by dictionary
hash-table expansion will be contained to only the dynamically-assigned
attributes.
How does it work?
-----------------
See for yourself! The code is tiny.
In words: the metaclass finds the ``__init__()`` method, if present, and
accesses its bytecode. It looks for all assignments to attributes of
``self``, and considers those to be desired ``__slots__`` entries. Then the
metaclass injects ``__slots__`` into the namespace of the class definition
and thereafter allows class creation to proceed as normal.
Weakref
-------
When ``__slots__`` are used, weak references (e.g. using the weakref_
standard library module) won't work. If you need weak references, just
set it up on a new ``__slots__`` class variable as you would normally
do without using ``autoslot``:
.. code-block:: python
from autoslot import Slots
class Compact(Slots):
__slots__ = ['__weakref__']
def __init__(self, a, b):
self.x = a
self.y = b
Everything else will still work, and instances of ``Compact`` will now
also play nicely with the weakref_ module.
.. _weakref: https://docs.python.org/3/library/weakref.html?highlight=weakref#module-weakref
Raw data
{
"_id": null,
"home_page": "http://pythonomicon.com",
"name": "autoslot",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Caleb Hattingh",
"author_email": "caleb.hattingh@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d4/2b/d62d12200bac293891a5dd9f3cae605f04aad0c440a5fecced80e9ce99cb/autoslot-2024.12.1.tar.gz",
"platform": null,
"description": ".. image:: https://img.shields.io/badge/stdlib--only-yes-green.svg\n :target: https://img.shields.io/badge/stdlib--only-yes-green.svg\n\n.. image:: https://coveralls.io/repos/github/cjrh/autoslot/badge.svg?branch=master\n :target: https://coveralls.io/github/cjrh/autoslot?branch=master\n\n.. image:: https://img.shields.io/pypi/pyversions/autoslot.svg\n :target: https://pypi.python.org/pypi/autoslot\n\n.. image:: https://img.shields.io/github/tag/cjrh/autoslot.svg\n :target: https://img.shields.io/github/tag/cjrh/autoslot.svg\n\n.. image:: https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg\n :target: https://img.shields.io/badge/install-pip%20install%20autoslot-ff69b4.svg\n\n.. image:: https://img.shields.io/pypi/v/autoslot.svg\n :target: https://img.shields.io/pypi/v/autoslot.svg\n\n.. image:: https://img.shields.io/badge/calver-YYYY.MM.MINOR-22bfda.svg\n :target: http://calver.org/\n\nautoslot\n========\n\nAutomatic \"__slots__\".\n\nDemo\n----\n\n.. code-block:: python\n\n from autoslot import Slots\n\n class Compact(Slots):\n def __init__(self, a, b):\n self.x = a\n self.y = b\n\nThis produces *exactly* the same class as if you had done:\n\n.. code-block:: python\n\n class Compact:\n __slots__ = {'x', 'y'}\n def __init__(self, a, b):\n self.x = a\n self.y = b\n\nSimply: the code inside ``__init__()`` is scanned to find all assignments\nto attributes on ``self``, and these are added as ``__slots__``.\n\nThe benefit of using ``autoslot.Slots`` over a manual slots declaration is\nthat you can modify the\ncode inside the ``__init__()`` method to add more attributes, and those\nchanges will *automatically* be reflected in the ``__slots__`` definition.\n\nYou can also have the best of both worlds: slots for fields you expect,\n**as well as** a ``__dict__`` for those you don't:\n\n.. code-block:: python\n\n from autoslot import SlotsPlusDict\n\n class SemiCompact(SlotsPlusDict):\n def __init__(self, a, b):\n self.x = a\n self.y = b\n\n inst = SemiCompact(1, 2)\n inst.z = 123 # <-- This won't fail!\n\nAttributes ``x`` and ``y`` will be stored in slots, while all other\ndynamically-assigned attributes will go into the usual ``__dict__`` instance\ninside the class. If most of your class's attributes appear in the ``__init__()``\nmethod (these will become slots), then the space bloat caused by dictionary\nhash-table expansion will be contained to only the dynamically-assigned\nattributes.\n\nHow does it work?\n-----------------\n\nSee for yourself! The code is tiny.\n\nIn words: the metaclass finds the ``__init__()`` method, if present, and\naccesses its bytecode. It looks for all assignments to attributes of\n``self``, and considers those to be desired ``__slots__`` entries. Then the\nmetaclass injects ``__slots__`` into the namespace of the class definition\nand thereafter allows class creation to proceed as normal.\n\nWeakref\n-------\n\nWhen ``__slots__`` are used, weak references (e.g. using the weakref_\nstandard library module) won't work. If you need weak references, just\nset it up on a new ``__slots__`` class variable as you would normally\ndo without using ``autoslot``:\n\n.. code-block:: python\n\n from autoslot import Slots\n\n class Compact(Slots):\n __slots__ = ['__weakref__']\n\n def __init__(self, a, b):\n self.x = a\n self.y = b\n\nEverything else will still work, and instances of ``Compact`` will now\nalso play nicely with the weakref_ module.\n\n.. _weakref: https://docs.python.org/3/library/weakref.html?highlight=weakref#module-weakref\n",
"bugtrack_url": null,
"license": null,
"summary": "Classes and metaclasses for easier ``__slots__`` handling. ",
"version": "2024.12.1",
"project_urls": {
"Homepage": "http://pythonomicon.com"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "051a39b527bdd3220d490f63960efd361afa611d3ca314cfbf389cc2c7dd7df7",
"md5": "cd309887bdcaf8e5e097c6df348904ad",
"sha256": "b50098372bf99caf1f135e4f1bbd9f20d56cb23a046e8a3bf402a20399526bfe"
},
"downloads": -1,
"filename": "autoslot-2024.12.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "cd309887bdcaf8e5e097c6df348904ad",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 7858,
"upload_time": "2024-12-06T14:42:51",
"upload_time_iso_8601": "2024-12-06T14:42:51.046270Z",
"url": "https://files.pythonhosted.org/packages/05/1a/39b527bdd3220d490f63960efd361afa611d3ca314cfbf389cc2c7dd7df7/autoslot-2024.12.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d42bd62d12200bac293891a5dd9f3cae605f04aad0c440a5fecced80e9ce99cb",
"md5": "6db56c7c556ee56d4e49ea2d35fb5488",
"sha256": "e52e3ce5b98d5fb2d7dbd5c7773627bee552a9cfb49a53b2bddb689adf2a3a5a"
},
"downloads": -1,
"filename": "autoslot-2024.12.1.tar.gz",
"has_sig": false,
"md5_digest": "6db56c7c556ee56d4e49ea2d35fb5488",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11010,
"upload_time": "2024-12-06T14:42:53",
"upload_time_iso_8601": "2024-12-06T14:42:53.317556Z",
"url": "https://files.pythonhosted.org/packages/d4/2b/d62d12200bac293891a5dd9f3cae605f04aad0c440a5fecced80e9ce99cb/autoslot-2024.12.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-06 14:42:53",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "autoslot"
}