All Projects → cjrh → autoslot

cjrh / autoslot

Licence: Apache-2.0 license
Automatic __slots__ for your Python classes

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to autoslot

Capslock Plus
An efficiency tool that provides various functions by enhancing the Caps Lock key into a modifier key.
Stars: ✭ 650 (+1126.42%)
Mutual labels:  efficiency
Deadline
A simple TODO with countdown timer
Stars: ✭ 134 (+152.83%)
Mutual labels:  efficiency
Pathfinder.vim
Vim plugin to suggest better movements
Stars: ✭ 228 (+330.19%)
Mutual labels:  efficiency
Ostrio Analytics
📊 Visitor's analytics tracking code for ostr.io service
Stars: ✭ 9 (-83.02%)
Mutual labels:  efficiency
Xseries
Library for cross-version Minecraft Bukkit support and various efficient API methods.
Stars: ✭ 109 (+105.66%)
Mutual labels:  efficiency
Ghostskb
Smart input method switcher like a ghost
Stars: ✭ 186 (+250.94%)
Mutual labels:  efficiency
Python Mss
An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.
Stars: ✭ 582 (+998.11%)
Mutual labels:  efficiency
ember-named-yields
Named Yields for Ember Components
Stars: ✭ 17 (-67.92%)
Mutual labels:  slots
Strcf
Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking (CVPR 2018)
Stars: ✭ 127 (+139.62%)
Mutual labels:  efficiency
Paddle
PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
Stars: ✭ 17,232 (+32413.21%)
Mutual labels:  efficiency
Go Benchmark App
Application for HTTP benchmarking via different rules and configs
Stars: ✭ 21 (-60.38%)
Mutual labels:  efficiency
Server Tech Tree
服务端软件技术树:服务端主流技术九大分类和全景图
Stars: ✭ 106 (+100%)
Mutual labels:  efficiency
Ybmodelfile
根据 JSON 自动生成 Model 文件(数据模型)
Stars: ✭ 209 (+294.34%)
Mutual labels:  efficiency
Void
terminal-based personal organizer
Stars: ✭ 831 (+1467.92%)
Mutual labels:  efficiency
Spvnas
[ECCV 2020] Searching Efficient 3D Architectures with Sparse Point-Voxel Convolution
Stars: ✭ 239 (+350.94%)
Mutual labels:  efficiency
Steward
A command launcher for Chrome
Stars: ✭ 617 (+1064.15%)
Mutual labels:  efficiency
Eemeter
An open source python package for implementing and developing standard methods for calculating normalized metered energy consumption and avoided energy use.
Stars: ✭ 134 (+152.83%)
Mutual labels:  efficiency
cs signal
Thread aware Signal/Slot library
Stars: ✭ 58 (+9.43%)
Mutual labels:  slots
mIRC-Twitch-Scripts
Various scripts and games to use with a mIRC bot designed for Twitch.tv
Stars: ✭ 30 (-43.4%)
Mutual labels:  slots
Linqit
Extend python lists with .NET's LINQ syntax for clean and fast coding. Also known as PINQ.
Stars: ✭ 222 (+318.87%)
Mutual labels:  efficiency
https://coveralls.io/repos/github/cjrh/autoslot/badge.svg?branch=master

autoslot

Automatic "__slots__".

Demo

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:

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:

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:

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].