Skip to main content

Library for optimizing enum constant values

Project description

microconst

pypi python typed tests license

Replace long constant names with 2 ASCII characters

Installation

pip:

pip install microconst

poetry:

poetry add microconst

Features

Constants

Every call of flag or key generates 2-character constant. This constant is represented as left and right indexes of f"{ascii_letters}{digits}" str. Each time the left index reaches cap, it resets to 0 and right index increments.
After reaching both of caps, next call of function will throw OverflowError. Current maximum count of constants is 3844.

Typing

key function requires value_type argument, such as str, int etc. This type used in both static analysis and type conversion with Key.parse_entry function.

Examples

Main purpose of this library is making telegram bot's "callback_data" much more compact because of 64-byte limit. You can see more realistic example here

Flag usage

Before:

from enum import StrEnum, auto

# Some very strict limit
MAX_LEN: int = 4

class Status(StrEnum):
    PENDING = auto()
    APPROVED = auto()
    REJECTED = auto()

assert Status.PENDING == "pending"
assert len(Status.PENDING) > MAX_LEN  # Too long

After:

from microconst import flag

MAX_LEN: int = 4

# Autogenerates unique pairs of characters
class Status:
    PENDING = flag()
    APPROVED = flag()
    REJECTED = flag()

assert Status.PENDING == "aa"
assert len(Status.PENDING) < MAX_LEN 

Key usage

Before:

from enum import StrEnum, auto

class Data(StrEnum):
    USERNAME = auto()
    ORDER = auto()

# You should use separator because of varying character count in key
username = Data.USERNAME + ":" + "name"
assert username == "username:name"  # Too long

value = username.split(":")[1]
assert value == "name"

order = Data.ORDER + ":" + str(1337)
assert order == "order:1337"

# Order doesnt contain its type anywhere, so you should convert types each time
assert int(order.split(":")[1]) == 1337

After:

from microconst import key, parse_entry

class Data:
    USERNAME = key(str)
    ORDER = key(int)

# You can call keys to create key-value pair (acts same as concat)
username = Data.USERNAME("name")
assert username == "aaname"

# Getting value
value = Data.USERNAME.parse_entry(username)
assert value == "name"

order = Data.ORDER(1337)
assert order == "ba1337"

# You can use separate function or method. Method doesnt require type argument
assert parse_entry(order, int) == 1337
assert Data.ORDER.parse_entry(order) == 1337

License

MIT

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

microconst-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

microconst-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file microconst-0.1.1.tar.gz.

File metadata

  • Download URL: microconst-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for microconst-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5ddeebc702b14fb469725d8ad8cbb5ed81a296970e9156f7eec2b8dab78b7f0a
MD5 67965c2f5a4f2c30d30f1b6626d8ee52
BLAKE2b-256 0f99a9c172fa1df6fd210f6b2904e288eb6a849edd8024cce584b1f2092999f2

See more details on using hashes here.

File details

Details for the file microconst-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: microconst-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for microconst-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0fa7224fcec6fdf514fe5e795350284bd496153abc47eb2e57672740664970e0
MD5 ac7383da1c7444cf66845145de9b3872
BLAKE2b-256 88f61b87663b1e4f7f2f713ff215ead6c8ccad9a2c0a31030655ecf0d81b3174

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page