jashin
Assorted Python utilities.
jashin.dictattr module
jashin.dictattr provides ItemAttr and DictModel to define class that wraps dictionary objects.
To wrap a dictionary like this,
userdict = {
"name": "test user",
"age": 20
}
You can use ItemAttr and DictModel as follows.
from jashin.dictattr import ItemAttr, DictModel
from dateutil.parser import parse as dateparse
class User(DictModel):
name = ItemAttr()
age = ItemAttr()
user = User(userdict)
print(user.name, user.created)
user.age = 30 # updates userdict
pritn(userdict['age']) # prints 30
ItemAttr supports nested objects.
companydict = {
"CEO": {
name: "A CEO",
age: "21",
}
"COO": {
name: "A COO",
age: "31",
}
}
To wrap a dictionary above, you can provide Company class.
class Company(DictModel):
CEO = ItemAttr(User)
COO = ItemAttr(User)
company = Company(companydict)
print(company.CEO.name) # prints 'A CEO'
DictModel class is not mandatory, but is provied to avoid boilerplate code. ItemAttr works any classes with __dictattr_get__ method.
class User:
name = ItemAttr()
age = ItemAttr()
def __init__(self, record):
self._recdict = record
def __dictattr_get__(self):
"Called by ItemAttr object to get dictionary"
return self._recdict
Type annotation is supported.
class User(DictModel):
name = ItemAttr[str]() # Explicity specify type
age = ItemAttr(int) # Inferred from `int` function.
user.name = "name" # OK
user.age = "30" # Incompatible types in assignment
# The right hand side expression has type "str",
# but 'age' attribute has type "int".
jashin.elapsed module
The jashin.elapsed measures elapsed time of arbitrary sections.
Sections can be specified by with block.
>>> from jashin.elapsed import Elapsed
>>> e = Elapsed()
>>> def test():
... a = 1
... for i in range(10):
... with e("section 1"):
... a += 1
...
... with e("section 2"):
... a += 1
...
>>> test()
>>> e.print()
section 1: n:10 sum:0.00002 ave:0.00000
section 2: n:10 sum:0.00002 ave:0.00000
Or by pair of begin(name) and end() methods.
>>> from jashin.elapsed import Elapsed
>>> e = Elapsed()
>>> def test2():
... a = 1
... for i in range(10):
... e.begin("section A"):
... a += 1
... e.end()
...
... e.begin("section B"):
... a += 1
... e.end()
...
>>> test2()
>>> e.print()
section A: n:10 sum:0.00002 ave:0.00000
section B: n:10 sum:0.00002 ave:0.00000
jashin.jsondefault module
To serialize arbitrary object into JSON, you should define default function.
def converter(obj):
if isinstance(obj, set):
return list[obj]
if isinstance(obj, datetime):
return obj.isoformat()
...
print(json.dumps(obj, default=converter))
This is tedious. The jashin.jsondefault.common provides common functionary to make popular types of objects JSON serializable.
from jashin import jsondefault
repo = jsondefault.common()
print(json.dumps(obj, default=repo)
Since jashin.jsondefault.common is a single-dispatch generic function, you can extend it to convert your custom objects.
from jashin import jsondefault
@dataclass
def Foo:
attr1:int = 100
repo = jsondefault.common()
@repo.register(Foo)
def conv_foo(obj):
return {'attr1': obj.foo}
print(json.dumps(object, default=repo)
Metadata
Release files for jashin 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jashin-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for jashin-0.0.7-py3-none-any.whl |
Release files / jashin-0.0.7-py3-none-any.whl
| Download URL | jashin-0.0.7-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c51dd3486f4984fe23142314183f739cf7d2eecc556abd4014c33a1c07d7cb5d
|
|
BLAKE2b-256 checksum How to use checksums |
a51db0ddc7082ccaba3bdb3227f353c4eb2f8f24f0afc68c0d17d4d063955515
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
|
Security for jashin 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.