schemabind 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.
schemabind/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from schemabind.core import bind
|
schemabind/core.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
class BoundRow:
|
|
2
|
+
def __init__(self, data):
|
|
3
|
+
self._data = data
|
|
4
|
+
self._normalized_keys = {}
|
|
5
|
+
|
|
6
|
+
for key in data.keys():
|
|
7
|
+
normalized_key = key.lower().replace(" ", "_")
|
|
8
|
+
if normalized_key in self._normalized_keys:
|
|
9
|
+
raise ValueError(
|
|
10
|
+
f"Duplicate normalized key '{normalized_key}' for original keys "
|
|
11
|
+
f"'{self._normalized_keys[normalized_key]}' and '{key}'"
|
|
12
|
+
)
|
|
13
|
+
self._normalized_keys[normalized_key] = key
|
|
14
|
+
|
|
15
|
+
def __getattr__(self, name):
|
|
16
|
+
if name not in self._normalized_keys:
|
|
17
|
+
available_fields = ", ".join(self._data.keys())
|
|
18
|
+
raise AttributeError(
|
|
19
|
+
f"Field '{name}' not found. " f"Available fields: {available_fields}"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
original_key = self._normalized_keys[name]
|
|
23
|
+
return self._data[original_key]
|
|
24
|
+
|
|
25
|
+
def __repr__(self):
|
|
26
|
+
return f"BoundRow(keys={list(self._data.keys())})"
|
|
27
|
+
|
|
28
|
+
def keys(self):
|
|
29
|
+
return self._data.keys()
|
|
30
|
+
|
|
31
|
+
def values(self):
|
|
32
|
+
return self._data.values()
|
|
33
|
+
|
|
34
|
+
def items(self):
|
|
35
|
+
return self._data.items()
|
|
36
|
+
|
|
37
|
+
def get(self, key, default=None):
|
|
38
|
+
return self._data.get(key, default)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def bind(data):
|
|
42
|
+
if not isinstance(data, dict):
|
|
43
|
+
raise TypeError(f"Expected dict, got {type(data).__name__}")
|
|
44
|
+
|
|
45
|
+
return BoundRow(data)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: schemabind
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Expose existing dictionary fields as readable Python attributes.
|
|
5
|
+
Author: Adam
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/adamhx2/SchemaBind
|
|
8
|
+
Project-URL: Issues, https://github.com/adamhx2/SchemaBind/issues
|
|
9
|
+
Keywords: dictionary,attributes,csv,schema,developer-tools
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# SchemaBind
|
|
20
|
+
|
|
21
|
+
SchemaBind reduces repetitive dictionary lookup boilerplate by exposing existing fields as readable Python attributes.
|
|
22
|
+
|
|
23
|
+
Instead of:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
first_name = row["first_name"]
|
|
27
|
+
email = row["email_address"]
|
|
28
|
+
phone = row.get("phone_number")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Use:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from schemabind import bind
|
|
35
|
+
|
|
36
|
+
customer = bind(row)
|
|
37
|
+
|
|
38
|
+
customer.first_name
|
|
39
|
+
customer.email_address
|
|
40
|
+
customer.phone_number
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
SchemaBind is currently focused on dictionaries, including rows produced by `csv.DictReader`.
|
|
44
|
+
|
|
45
|
+
It does not create schemas, validate data, transform values, or infer new fields. It simply provides a cleaner way to access fields that already exist.
|
|
46
|
+
|
|
47
|
+
## Example
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
import csv
|
|
51
|
+
from schemabind import bind
|
|
52
|
+
|
|
53
|
+
with open("samples/csv/customer.csv") as f:
|
|
54
|
+
rows = list(csv.DictReader(f))
|
|
55
|
+
|
|
56
|
+
customer = bind(rows[0])
|
|
57
|
+
|
|
58
|
+
print(customer.first_name)
|
|
59
|
+
print(customer.email_address)
|
|
60
|
+
print(customer.phone_number)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Missing attributes raise `AttributeError` instead of silently returning `None`, which helps catch typos in field names.
|
|
64
|
+
|
|
65
|
+
## Field Normalization
|
|
66
|
+
|
|
67
|
+
SchemaBind supports simple field-name normalization for attribute access.
|
|
68
|
+
|
|
69
|
+
For example:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
customer = bind({"First Name": "Kaladin"})
|
|
73
|
+
|
|
74
|
+
print(customer.first_name)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The original dictionary keys are preserved for dictionary-style helpers:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
customer.keys()
|
|
81
|
+
customer.get("First Name")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
If multiple fields normalize to the same attribute name, SchemaBind raises `ValueError` instead of guessing which field to use.
|
|
85
|
+
|
|
86
|
+
For example:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
bind({
|
|
90
|
+
"First Name": "Dalinar",
|
|
91
|
+
"first_name": "Shallan",
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Both fields would normalize to `first_name`, so the binding is rejected as ambiguous.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
schemabind/__init__.py,sha256=CGMdtSwFIF0554pzUDHjvlVbS4UZ_UPCAMPC3ojLZVE,34
|
|
2
|
+
schemabind/core.py,sha256=4wrsaYA-oCBgAVkTj6eqV26rMX1D95jiEbqQgg0-DlQ,1421
|
|
3
|
+
schemabind-0.1.0.dist-info/METADATA,sha256=mQzWK3BVaD5XHW1X5Uj_ypALd2QmflXoXT73A1jkjBw,2464
|
|
4
|
+
schemabind-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
schemabind-0.1.0.dist-info/top_level.txt,sha256=ZOSVrFNBAbOQL6ub6H2Ngzpch4X-GZ3PjADGFzB6LYw,11
|
|
6
|
+
schemabind-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
schemabind
|