microconst 0.1.0__tar.gz

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.
@@ -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
+ [![pypi](https://img.shields.io/pypi/v/microconst)](https://pypi.org/project/microconst/)
26
+ ![python](https://img.shields.io/badge/python-3.12+-blue?logo=python)
27
+ ![typed](https://img.shields.io/badge/typed-yes-blue)
28
+ ![tests](https://img.shields.io/badge/tests-manual-green)
29
+ ![license](https://img.shields.io/badge/license-MIT-blue)
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,64 @@
1
+ # microconst
2
+ [![pypi](https://img.shields.io/pypi/v/microconst)](https://pypi.org/project/microconst/)
3
+ ![python](https://img.shields.io/badge/python-3.12+-blue?logo=python)
4
+ ![typed](https://img.shields.io/badge/typed-yes-blue)
5
+ ![tests](https://img.shields.io/badge/tests-manual-green)
6
+ ![license](https://img.shields.io/badge/license-MIT-blue)
7
+
8
+ Replace long constant names with 2 ASCII keys
9
+
10
+ ## Installation
11
+ pip:
12
+ ```
13
+ pip install microconst
14
+ ```
15
+ poetry:
16
+ ```
17
+ poetry add microconst
18
+ ```
19
+
20
+ ## Features
21
+ #### Constants
22
+ 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>
23
+ After reaching both of caps, next call of function will throw `OverflowError`. Current maximum count of keys is 3844.
24
+
25
+ #### Typing
26
+ `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.
27
+
28
+ ## Examples
29
+ 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>
30
+ #### Flag usage:
31
+ ```python
32
+ from microconst import flag
33
+
34
+ # Autogenerates unique pairs of characters
35
+ class Status:
36
+ PENDING = flag()
37
+ APPROVED = flag()
38
+ REJECTED = flag()
39
+
40
+ assert Status.PENDING == "aa"
41
+ assert Status.APPROVED == "ba"
42
+ assert Status.REJECTED == "ca"
43
+ ```
44
+
45
+ #### Key usage:
46
+ ```python
47
+ from microconst import key, parse_entry
48
+
49
+ USER = key(str)
50
+
51
+ # You can call keys to create key-value pair (acts same as concat)
52
+ username = USER("name")
53
+ # Getting value
54
+ value = USER.parse_entry(username)
55
+
56
+ assert username == "aaname"
57
+ assert value == "name"
58
+
59
+ # You can use seperate function or method. Method doesnt require type argument
60
+ assert parse_entry(username, str) == value
61
+ ```
62
+
63
+ ## License
64
+ MIT
@@ -0,0 +1,6 @@
1
+ from microconst.entry import parse_entry
2
+ from microconst.members import Key
3
+ from microconst.members import create_flag as flag
4
+ from microconst.members import create_key as key
5
+
6
+ __all__ = ["Key", "flag", "key", "parse_entry"]
@@ -0,0 +1,6 @@
1
+ from collections.abc import Callable
2
+
3
+
4
+ def parse_entry[T](data: str, value_type: Callable[[str], T]) -> T:
5
+ str_value = data[2:]
6
+ return value_type(str_value)
@@ -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()
@@ -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)
@@ -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
+ [![pypi](https://img.shields.io/pypi/v/microconst)](https://pypi.org/project/microconst/)
26
+ ![python](https://img.shields.io/badge/python-3.12+-blue?logo=python)
27
+ ![typed](https://img.shields.io/badge/typed-yes-blue)
28
+ ![tests](https://img.shields.io/badge/tests-manual-green)
29
+ ![license](https://img.shields.io/badge/license-MIT-blue)
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,12 @@
1
+ README.md
2
+ pyproject.toml
3
+ microconst/__init__.py
4
+ microconst/entry.py
5
+ microconst/key.py
6
+ microconst/members.py
7
+ microconst/py.typed
8
+ microconst.egg-info/PKG-INFO
9
+ microconst.egg-info/SOURCES.txt
10
+ microconst.egg-info/dependency_links.txt
11
+ microconst.egg-info/top_level.txt
12
+ tests/test_all.py
@@ -0,0 +1 @@
1
+ microconst
@@ -0,0 +1,75 @@
1
+ [tool.ruff]
2
+ line-length = 120
3
+
4
+ [tool.ruff.lint]
5
+ select = ["ALL"]
6
+ ignore = [
7
+ "D", # Docstring warnings
8
+ "INP001" # Implicit namespace package
9
+ ]
10
+
11
+ [tool.ruff.lint.per-file-ignores]
12
+ "tests/**/*.py" = [
13
+ "S101", # Assert used
14
+ "PLR2004", # Magic value
15
+ ]
16
+
17
+
18
+ [tool.mypy]
19
+ exclude = ["example/"]
20
+ strict = true
21
+
22
+ disallow_untyped_defs = true
23
+ disallow_untyped_calls = true
24
+ check_untyped_defs = true
25
+
26
+ warn_return_any = true
27
+ disallow_any_explicit = true
28
+ disallow_any_unimported = true
29
+ disallow_any_decorated = true
30
+ disallow_any_expr = true
31
+ disallow_any_generics = true
32
+
33
+ no_implicit_optional = true
34
+ strict_equality = true
35
+ strict_concatenate = true
36
+ local_partial_types = true
37
+
38
+ no_implicit_reexport = true
39
+
40
+
41
+ [build-system]
42
+ requires = ["setuptools>=61.0"]
43
+ build-backend = "setuptools.build_meta"
44
+
45
+
46
+ [project]
47
+ name = "microconst"
48
+ version = "0.1.0"
49
+ description = "Library for optimizing enum constant values"
50
+
51
+ requires-python = ">=3.12"
52
+
53
+ readme = "README.md"
54
+ license = {text = "MIT"}
55
+ authors = [{name = "triple-raze", email = "weebucks2@gmail.com"}]
56
+
57
+ classifiers = [
58
+ "Development Status :: 4 - Beta",
59
+ "Intended Audience :: Developers",
60
+ "License :: OSI Approved :: MIT License",
61
+ "Programming Language :: Python :: 3.12",
62
+ "Programming Language :: Python :: 3.13",
63
+ "Programming Language :: Python :: 3.14",
64
+ "Operating System :: OS Independent",
65
+ "Topic :: Software Development :: Libraries :: Python Modules",
66
+ "Topic :: Utilities",
67
+ "Typing :: Typed"
68
+ ]
69
+ keywords = ["enum", "compact"]
70
+
71
+
72
+ [project.urls]
73
+ "Homepage" = "https://github.com/triple-raze/microconst"
74
+ "Repository" = "https://github.com/triple-raze/microconst.git"
75
+ "Issues" = "https://github.com/triple-raze/microconst/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,21 @@
1
+ from microconst import flag, key, parse_entry
2
+
3
+
4
+ def test_member_creation() -> None:
5
+ assert key(int)(10) == "aa10"
6
+
7
+ assert key(str)("hello") == "bahello"
8
+
9
+ assert key(float)(14.1) == "ca14.1"
10
+
11
+ assert flag() == "da"
12
+ assert flag() == "ea"
13
+
14
+
15
+ def test_value_extraction() -> None:
16
+ int_key = key(int)
17
+ data = int_key(123)
18
+
19
+ assert int_key.parse_entry(data) == 123
20
+
21
+ assert parse_entry("aa1.2", float) == 1.2