Skip to main content

Wrapper class so that you can access `dict` and `list` as easily as in Javascript

Project description

python_js_json

Wrapper class so that you can access dict and list as easily as in Javascript

Installation

python3 -m pip install real_json

usage

```python
import real_json
import json

data = {
    "a": 1,
    "b": 2,
    "c": {
        "d": 3,
        "e": 4,
    },
    "f": [5, 6, 7],
}
wrapped = real_json.ify(data)

# Test attribute access
assert wrapped.a == 1
assert wrapped.b == 2
assert wrapped.c.d == 3
assert wrapped.c.e == 4
assert wrapped.f[0] == 5
assert wrapped.f[1] == 6
assert wrapped.f[2] == 7
assert wrapped.f[3] == None
assert wrapped.g == None
assert wrapped.c.f == None

# Test item access
assert wrapped["a"] == 1
assert wrapped["b"] == 2
assert wrapped["c"]["d"] == 3
assert wrapped["c"]["e"] == 4
assert wrapped["f"][0] == 5
assert wrapped["f"][1] == 6
assert wrapped["f"][2] == 7
assert wrapped["g"] == None
assert wrapped["c"]["f"] == None

# Test set attribute
wrapped.a = 10
assert wrapped.a == 10
wrapped.g = 20
assert wrapped.g == 20

# Test set item
wrapped["b"] = 20
assert wrapped["b"] == 20
wrapped["h"] = 30
assert wrapped["h"] == 30

# Test str and repr
assert str(wrapped) == str(data)
assert repr(wrapped) == repr(data)

# Test len
print("wrapped:", wrapped)
assert len(wrapped) == 6
assert len(wrapped.c) == 2
assert len(wrapped.f) == 3

# Test bool
assert bool(wrapped) == True
assert bool(wrapped.g) == True
assert bool(wrapped.c) == True
assert bool(wrapped.f)

json.dump(wrapped.__dict__["_data"], "wrapped.json")

data = {
    "name": "John",
    "age": 30,
    "cars": [
        {"model": "Ford", "year": 2020},
        {"model": "BMW", "year": 2019}
    ]
}

wrapped_data = real_json.ify(data)

# Accessing values using dot notation
print(wrapped_data.name)  # Output: "John"
print(wrapped_data.age)  # Output: 30
# Output: [{"model": "Ford", "year": 2020}, {"model": "BMW", "year": 2019}]
print(wrapped_data.cars)

# Accessing values using square brackets notation
print(wrapped_data['name'])  # Output: "John"
print(wrapped_data['age'])  # Output: 30
# Output: [{"model": "Ford", "year": 2020}, {"model": "BMW", "year": 2019}]
print(wrapped_data['cars'])

# Accessing values that are not present in the data
print(wrapped_data.address)  # Output: None
print(wrapped_data['address'])  # Output: None

# Accessing elements of the list using index notation
print(wrapped_data.cars[0])  # Output: {"model": "Ford", "year": 2020}
print(wrapped_data['cars'][0])  # Output: {"model": "Ford", "year": 2020}

# Accessing elements of the list using index notation
print(wrapped_data.cars[0].model)  # Output: "Ford"
print(wrapped_data['cars'][0]['model'])  # Output: "Ford"

# Accessing elements of the list using index notation
print(wrapped_data.cars[2])  # Output: None
print(wrapped_data['cars'][2])  # Output: None

# Accessing elements of the list using index notation
print(wrapped_data.cars[0].color)  # Output: None
print(wrapped_data['cars'][0]['color'])  # Output: None

json.dump(wrapped_data.__dict__["_data"], "data.json")
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

real_json-1.0.3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

real_json-1.0.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file real_json-1.0.3.tar.gz.

File metadata

  • Download URL: real_json-1.0.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for real_json-1.0.3.tar.gz
Algorithm Hash digest
SHA256 5e6fdac076f815f8bb5294e597ac1ff60d8fa7bc37088f10a20824f4ba684dbc
MD5 0e9e5cd78a1e058a64090e1779ba3ac9
BLAKE2b-256 9c447a38e2834d629634af3e2db143b2e1b6e4ce8c557e93199a3061eef19feb

See more details on using hashes here.

File details

Details for the file real_json-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: real_json-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for real_json-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 580f27969668adcd9b0d5c4ecb17ff994869a4d83eb74b6ccc7759e8dd8dc4f6
MD5 18358b6432c0e599fcafdd6f6eb64dae
BLAKE2b-256 d74cb817a99b69c197a1a3aa28dfe79f53615c7b025eea99b70687e1fdd376b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page