Python module for implement object with syntax from JavaScript. Implementing access to keys by obj.path.to.your.key
notation as in JavaScript
pip install asjs
or like this:
curl https://github.com/tankalxat34/pyasjs/raw/main/asjs.py -o asjs.py
Recommended import statement:
from asjs import ObjectNotation
Using dictionary:
obj = ObjectNotation({"key1": "value1", "key2": "value2", "key3": {"a": "b", "lst": [14, 5, 6, 12]}})
Using arguments:
obj = ObjectNotation(key1="value1", key2="value2", key3={"a": "b", "lst": [14, 5, 6, 12]})
>>> obj[0]
value1
>>> obj.key2
value2
>>> obj["key3"]
[14, 5, 6, 12]
obj.year = 2023
or:
>>> year = 2023
>>> obj.set(year)
or:
>>> obj["year"] = 2023
>>> obj.fibonachi = lambda n: 1 if n <= 2 else obj.fibonachi(n - 1) + obj.fibonachi(n - 2)
>>> obj.fibonachi(7)
13
>>> del obj.year
or:
>>> del obj[-1]
or:
>>> del obj["year"]
>>> obj.key1 = 123
or:
>>> obj["key1"] = 123
Indexes:
>>> for i in range(len(obj)):
... print(i)
0
1
2
Keys:
>>> for k in obj:
... print(k)
key1
key2
key3
fibonachi
Values:
>>> for v in obj.values():
... print(v)
123
value2
JSObject(a: 'b', lst: JSObject(0: 14, 1: 5, 2: 6, 3: 12))
<function <lambda> at 0x0000020189F19480>
>>> "key3" in obj
True
>>> "fake_key" in obj
False
>>> obj.isArray()
False
>>> obj.key3.lst.isArray()
True
>>> obj.toList()
Traceback (most recent call last):
...
TypeError: Object is not be able to convert to list
>>> obj.key3.lst.toList()
[14, 5, 6, 12]
>>> obj[0:3]
('value1', 'value2', <asjs.ObjectNotation object at 0x000001D7195A1990>)
>>> obj.key3.lst[0:3]
(14, 5, 6)