py-prim 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.
primitives/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ from .core import Primitive, Integer, Float, String, Boolean, Bytes
2
+
3
+ __all__ = [
4
+ "Primitive",
5
+ "Integer",
6
+ "Float",
7
+ "String",
8
+ "Boolean",
9
+ "Bytes"
10
+ ]
primitives/core.py ADDED
@@ -0,0 +1,129 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import Any
3
+
4
+ class Primitive(ABC):
5
+ def __init__(self, value: Any = None):
6
+ self._value = value
7
+ # Run initial validation if a value is provided
8
+ if value is not None:
9
+ self.validate()
10
+
11
+ @property
12
+ def value(self) -> Any:
13
+ return self._value
14
+
15
+ @value.setter
16
+ def value(self, new_value: Any):
17
+ self._value = new_value
18
+ self.validate()
19
+
20
+ # --- Abstract Methods (Must be overridden) ---
21
+
22
+ @abstractmethod
23
+ def default_value(self) -> Any:
24
+ """Return the default value for the type."""
25
+ pass
26
+
27
+ @abstractmethod
28
+ def is_empty(self) -> bool:
29
+ """Determine if the value is functionally empty."""
30
+ pass
31
+
32
+ @abstractmethod
33
+ def is_valid(self) -> bool:
34
+ """Return True if the value meets all type constraints."""
35
+ pass
36
+
37
+ # --- Generic Methods (Inherited by all) ---
38
+
39
+ def is_null(self) -> bool:
40
+ return self._value is None
41
+
42
+ def validate(self) -> None:
43
+ if not self.is_null() and not self.is_valid():
44
+ raise ValueError(f"Invalid value for {self.__class__.__name__}: {self._value}")
45
+
46
+ def reset(self) -> None:
47
+ self.value = self.default_value()
48
+
49
+ # --- Python Magic Methods ---
50
+
51
+ def __str__(self) -> str:
52
+ return str(self.default_value() if self.is_null() else self._value)
53
+
54
+ def __eq__(self, other: Any) -> bool:
55
+ # Allow comparison with raw primitive types or other Enhanced primitives
56
+ if isinstance(other, Primitive):
57
+ return self._value == other.value
58
+ return self._value == other
59
+
60
+ def __bool__(self) -> bool:
61
+ return not self.is_empty() and not self.is_null()
62
+
63
+
64
+ # ==========================================
65
+ # Derived Classes
66
+ # ==========================================
67
+
68
+ class Integer(Primitive):
69
+ def default_value(self) -> int:
70
+ return 0
71
+
72
+ def is_empty(self) -> bool:
73
+ return self._value == 0
74
+
75
+ def is_valid(self) -> bool:
76
+ # Explicitly exclude booleans since bool is a subclass of int in Python
77
+ return isinstance(self._value, int) and not isinstance(self._value, bool)
78
+
79
+
80
+ class Float(Primitive):
81
+ def default_value(self) -> float:
82
+ return 0.0
83
+
84
+ def is_empty(self) -> bool:
85
+ return self._value == 0.0
86
+
87
+ def is_valid(self) -> bool:
88
+ return isinstance(self._value, (float, int)) and not isinstance(self._value, bool)
89
+
90
+ # Optional override: ensure the value is always stored as a float internally
91
+ @Primitive.value.setter
92
+ def value(self, new_value):
93
+ if isinstance(new_value, int) and not isinstance(new_value, bool):
94
+ new_value = float(new_value)
95
+ super(Float, type(self)).value.fset(self, new_value)
96
+
97
+
98
+ class String(Primitive):
99
+ def default_value(self) -> str:
100
+ return ""
101
+
102
+ def is_empty(self) -> bool:
103
+ # Empty if it is length 0 or contains only whitespace
104
+ return self._value == "" or str(self._value).isspace()
105
+
106
+ def is_valid(self) -> bool:
107
+ return isinstance(self._value, str)
108
+
109
+
110
+ class Boolean(Primitive):
111
+ def default_value(self) -> bool:
112
+ return False
113
+
114
+ def is_empty(self) -> bool:
115
+ return self._value is False
116
+
117
+ def is_valid(self) -> bool:
118
+ return isinstance(self._value, bool)
119
+
120
+
121
+ class Bytes(Primitive):
122
+ def default_value(self) -> bytes:
123
+ return b""
124
+
125
+ def is_empty(self) -> bool:
126
+ return self._value == b""
127
+
128
+ def is_valid(self) -> bool:
129
+ return isinstance(self._value, bytes)
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.4
2
+ Name: py-prim
3
+ Version: 0.1.0
4
+ Summary: Enhanced and validated base primitive data types for Python.
5
+ Author: Salik Tariq
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.7
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Dynamic: license-file
13
+
14
+ # py-prim
15
+ A robust, validated, and state-aware base class architecture for standard Python primitive data types.
16
+
17
+ In standard Python, primitives like int and str are loose and allow for silent state changes or type mixing. This package provides wrapper classes (Integer, String, etc.) that enforce strict type validation, provide standardized empty/null checks, and offer a consistent API across all basic data types.
18
+
19
+ ## Features
20
+ Strict Type Validation: Fails fast if you try to assign a string to an integer, or mix up booleans and ints.
21
+
22
+ Standardized API: Every type implements .is_empty(), .is_null(), and .reset().
23
+
24
+ Magic Methods Support: Compares cleanly with standard Python types using == and integrates seamlessly with if statements.
25
+
26
+ Zero Dependencies: Built entirely on the Python standard library.
27
+
28
+ ## Installation
29
+ You can install this package directly from GitHub using either pip or uv.
30
+
31
+ Using pip:
32
+
33
+ pip install git+[https://github.com/saliktariq/Py-Prim.git](https://github.com/saliktariq/Py-Prim.git)
34
+
35
+ Using uv:
36
+
37
+ uv pip install git+[https://github.com/saliktariq/Py-Prim.git](https://github.com/saliktariq/Py-Prim.git)
38
+
39
+
40
+ ## Available Types
41
+ All types inherit from the Primitive abstract base class:
42
+
43
+ - Integer
44
+ - Float
45
+ - String
46
+ - Boolean
47
+ - Bytes
48
+
49
+ ## Usage:
50
+
51
+ Access and mutate the underlying data using the .value property.
52
+
53
+ from primitives import Integer, String
54
+
55
+ ### Initialize with a value
56
+ age = Integer(25)
57
+ print(age.value) # 25
58
+
59
+ ### Change the value
60
+ age.value = 26
61
+ Strict Type Validation
62
+ The classes automatically validate types upon initialization and assignment, preventing accidental type mixing.
63
+
64
+ from primitives import Integer
65
+
66
+ score = Integer(100)
67
+
68
+ #### This will raise a ValueError: "Invalid value for Integer: 100.5"
69
+ score.value = 100.5
70
+
71
+ ### This library strictly isolates Booleans from Integers.
72
+ score.value = True # Raises ValueError
73
+ State Checks and Resets
74
+ Every type knows what its "empty" and "default" states are.
75
+
76
+ from primitives import String, Float
77
+
78
+ name = String("")
79
+ print(name.is_empty()) # True
80
+
81
+ name.value = "Alice"
82
+ print(name.is_empty()) # False
83
+
84
+ ### Reset back to the type's default value
85
+ name.reset()
86
+ print(name.value) # ""
87
+
88
+ ### Null checking
89
+ balance = Float(None)
90
+ print(balance.is_null()) # True
91
+
92
+ ## License
93
+ MIT License
@@ -0,0 +1,7 @@
1
+ primitives/__init__.py,sha256=gUjrwcrXCTcyRHpiHCfrvFm0EhNPaNX5i0ear33wei4,168
2
+ primitives/core.py,sha256=QWk0JX-mGHCsA8mr_qHOsZ7JrSQ0Epe3qxkWc_X08u0,3574
3
+ py_prim-0.1.0.dist-info/licenses/LICENSE,sha256=BqUwR7qRj5_bx2Z1dm46e4P2jf1hWzSX9t26tqX1WyY,1062
4
+ py_prim-0.1.0.dist-info/METADATA,sha256=I7bv1i121ZPuzM1L42yld68aqcxyzbelnKMYs_JSBHc,2678
5
+ py_prim-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ py_prim-0.1.0.dist-info/top_level.txt,sha256=0EMScDdk-RvtAeywyRqTFUggbUI03UGJxIgsVcPjPnA,11
7
+ py_prim-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Salik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ primitives