microconst 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- microconst/__init__.py +6 -0
- microconst/entry.py +6 -0
- microconst/key.py +29 -0
- microconst/members.py +32 -0
- microconst/py.typed +1 -0
- microconst-0.1.0.dist-info/METADATA +87 -0
- microconst-0.1.0.dist-info/RECORD +9 -0
- microconst-0.1.0.dist-info/WHEEL +5 -0
- microconst-0.1.0.dist-info/top_level.txt +1 -0
microconst/__init__.py
ADDED
microconst/entry.py
ADDED
microconst/key.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from collections.abc import Generator
|
|
2
|
+
from string import ascii_letters, digits
|
|
3
|
+
|
|
4
|
+
CHARACTERS = f"{ascii_letters}{digits}"
|
|
5
|
+
|
|
6
|
+
MAX_IDX = len(CHARACTERS) - 1
|
|
7
|
+
MAX_KEYS = len(CHARACTERS) ** 2
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def create_unique_key_generator() -> Generator[str, None, None]:
|
|
11
|
+
left_idx: int = 0
|
|
12
|
+
right_idx: int = 0
|
|
13
|
+
|
|
14
|
+
while right_idx != MAX_IDX + 1:
|
|
15
|
+
pair: str = CHARACTERS[left_idx] + CHARACTERS[right_idx]
|
|
16
|
+
|
|
17
|
+
if left_idx == MAX_IDX:
|
|
18
|
+
left_idx = 0
|
|
19
|
+
right_idx += 1
|
|
20
|
+
else:
|
|
21
|
+
left_idx += 1
|
|
22
|
+
|
|
23
|
+
yield pair
|
|
24
|
+
|
|
25
|
+
msg = f"cannot create more than {MAX_KEYS} keys"
|
|
26
|
+
raise OverflowError(msg)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
unique_key_generator = create_unique_key_generator()
|
microconst/members.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from typing import Self
|
|
3
|
+
|
|
4
|
+
from microconst.entry import parse_entry
|
|
5
|
+
from microconst.key import unique_key_generator
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Key[T](str):
|
|
9
|
+
# Subclasses of str should define __slots__
|
|
10
|
+
__slots__ = ("type",)
|
|
11
|
+
# We'll assign to this in __new__
|
|
12
|
+
type: Callable[[str], T]
|
|
13
|
+
|
|
14
|
+
def __new__(cls, name: str, value_type: Callable[[str], T]) -> Self:
|
|
15
|
+
instance = super().__new__(cls, name)
|
|
16
|
+
instance.type = value_type
|
|
17
|
+
return instance
|
|
18
|
+
|
|
19
|
+
def __call__(self, value: T) -> str:
|
|
20
|
+
return f"{self}{value}"
|
|
21
|
+
|
|
22
|
+
def parse_entry(self, data: str) -> T:
|
|
23
|
+
return parse_entry(data, self.type)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def create_flag() -> str:
|
|
27
|
+
return next(unique_key_generator)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def create_key[T](value_type: type[T]) -> Key[T]:
|
|
31
|
+
name = next(unique_key_generator)
|
|
32
|
+
return Key(name, value_type)
|
microconst/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This file serves as a type annotation marker.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: microconst
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Library for optimizing enum constant values
|
|
5
|
+
Author-email: triple-raze <weebucks2@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/triple-raze/microconst
|
|
8
|
+
Project-URL: Repository, https://github.com/triple-raze/microconst.git
|
|
9
|
+
Project-URL: Issues, https://github.com/triple-raze/microconst/issues
|
|
10
|
+
Keywords: enum,compact
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# microconst
|
|
25
|
+
[](https://pypi.org/project/microconst/)
|
|
26
|
+

|
|
27
|
+

|
|
28
|
+

|
|
29
|
+

|
|
30
|
+
|
|
31
|
+
Replace long constant names with 2 ASCII keys
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
pip:
|
|
35
|
+
```
|
|
36
|
+
pip install microconst
|
|
37
|
+
```
|
|
38
|
+
poetry:
|
|
39
|
+
```
|
|
40
|
+
poetry add microconst
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
#### Constants
|
|
45
|
+
Every call of `flag` or `key` generates 2-character key. This key 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.<br>
|
|
46
|
+
After reaching both of caps, next call of function will throw `OverflowError`. Current maximum count of keys is 3844.
|
|
47
|
+
|
|
48
|
+
#### Typing
|
|
49
|
+
`key` function requires `value_type` argument, such as `str`, `int` etc. This type used in both static analysis and type convertion with `Key.parse_entry(data)` function.
|
|
50
|
+
|
|
51
|
+
## Examples
|
|
52
|
+
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](example)<br><br>
|
|
53
|
+
#### Flag usage:
|
|
54
|
+
```python
|
|
55
|
+
from microconst import flag
|
|
56
|
+
|
|
57
|
+
# Autogenerates unique pairs of characters
|
|
58
|
+
class Status:
|
|
59
|
+
PENDING = flag()
|
|
60
|
+
APPROVED = flag()
|
|
61
|
+
REJECTED = flag()
|
|
62
|
+
|
|
63
|
+
assert Status.PENDING == "aa"
|
|
64
|
+
assert Status.APPROVED == "ba"
|
|
65
|
+
assert Status.REJECTED == "ca"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### Key usage:
|
|
69
|
+
```python
|
|
70
|
+
from microconst import key, parse_entry
|
|
71
|
+
|
|
72
|
+
USER = key(str)
|
|
73
|
+
|
|
74
|
+
# You can call keys to create key-value pair (acts same as concat)
|
|
75
|
+
username = USER("name")
|
|
76
|
+
# Getting value
|
|
77
|
+
value = USER.parse_entry(username)
|
|
78
|
+
|
|
79
|
+
assert username == "aaname"
|
|
80
|
+
assert value == "name"
|
|
81
|
+
|
|
82
|
+
# You can use seperate function or method. Method doesnt require type argument
|
|
83
|
+
assert parse_entry(username, str) == value
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
microconst/__init__.py,sha256=TSa3h7q5xtLLglUtUdBJvv395YYoAzYvB1ehj5UdiiE,231
|
|
2
|
+
microconst/entry.py,sha256=9oEKshzIcOgwhj1ZkTnHiZ2ejeVhXFiXDGChHfOhdqQ,171
|
|
3
|
+
microconst/key.py,sha256=nhNeyP8gDulWcgGo1UwUNTEszBB2p3ZxX4HUdlznjKI,709
|
|
4
|
+
microconst/members.py,sha256=m_FWujh3drGconuyNNh77KdUuwWZ-7IJhw8EMlsmQaI,875
|
|
5
|
+
microconst/py.typed,sha256=TYyiMxTQNcIkn-rpK_EarmVnGNXcBohGJy2b1REexQo,45
|
|
6
|
+
microconst-0.1.0.dist-info/METADATA,sha256=8-k0Ubgy1qi_gynnfdtWru5ApqP75u38zV5R2KC_1Hg,2947
|
|
7
|
+
microconst-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
8
|
+
microconst-0.1.0.dist-info/top_level.txt,sha256=XVBEqhpR9o0B51aKQ-3Bxn1AqJTHZ1IMQMcKRx8h0KY,11
|
|
9
|
+
microconst-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
microconst
|