Markups


AttrDict

 AttrDict (*args, given_input_to_ad=None, **kwargs)

*Utility class to interact with a dictionary as if it were an object. AD is an alias to this class

FEATURES: 0. Access and modify keys (including nested keys) as if they were object attributes, supporting tab-completion. Example: self.key1.key2[0].key3 1. Keys and values are recursively converted to AttrDict instances. 2. Pretty-print the dictionary using print. 3. Convert the entire structure to a regular dictionary at any time using self.to_dict() / self.dict(). 3. Recursively remove keys using self.drop(key) from a JSON object. 4. Apply a function to all values at all levels using map.

GOTCHAS: 1. All integer keys are implicitly converted to strings due to the enforced self.key format. 2. You can still use self[int], but this internally converts the integer to a string.

METHODS: - items(): Return the items of the AttrDict as key-value pairs. - keys(): Return the keys of the AttrDict. - values(): Return the values of the AttrDict. - update(dict): Update the AttrDict with key-value pairs from another dictionary. - get(key, default=None): Get the value associated with a key, with an optional default value. - __iter__(): Allow iteration over the keys of the AttrDict. - __len__(): Return the number of keys in the AttrDict. - __repr__(): Return a string representation of the AttrDict. - __dir__(): List the keys of the AttrDict as attributes. - __contains__(key): Check if a key exists in the AttrDict, use ‘a.b.c’ notation to directly check for a nested attribute. - __delitem__(key): Delete a key from the AttrDict. - map(func): Apply a function to all values in the AttrDict. - drop(key): Recursively remove a key and its values from the AttrDict. - to_dict(): Convert the AttrDict and its nested structure to a regular dictionary. - pretty(print_with_logger=False, *args, **kwargs): Pretty-print the AttrDict as JSON. - __eq__(other): Compare the AttrDict with another dictionary for equality. - find_address(key, current_path=""): Find and return all addresses (paths) of a given key in the AttrDict. - summary(current_path='', summary_str='', depth=0, sep=' '): Generate a summary of the structure and values in the AttrDict. - write_summary(to, **kwargs): Write the summary to a file or stream. - fetch(addr): Retrieve a value at a specified address (path).

PARAMETERS: - data (dict, optional): Initial data to populate the AttrDict.

USAGE: - Create an AttrDict instance by providing an optional initial dictionary, and then access and manipulate its contents as if they were object attributes.

EXAMPLE:

my_dict = {'name': 'John', 'age': 30, 'address': {'city': 'New York', 'zip': '10001'}}
attr_dict = AttrDict(my_dict)
print(attr_dict.name)  # Access values like attributes
attr_dict.address.city = 'Los Angeles'  # Modify nested values
```*


::: {#bfb4146f-90bf-42bc-bf40-69fb6d612ea2 .cell tags='[]' execution_count=5}
``` {.python .cell-code}
xx = AttrDict({"a": 1, "b": [{"c": 2, "d": 4}, {"e": 3}], "f": {"g": {"h": 20}}})
print(type(xx.b[0]))
print(type(xx.to_dict()["b"][0]))
print("f.g.h" in xx)
xx.pretty()
<class 'torch_snippets.markup2.AttrDict'>
<class 'dict'>
True
{
    "a": 1,
    "b": [
        {
            "c": 2,
            "d": 4
        },
        {
            "e": 3
        }
    ],
    "f": {
        "g": {
            "h": 20
        }
    }
}

:::

x = {"abc": {"b": 10, "c": 11}, "d": {"e": {"f": [2, {"abc": "pqrs"}, 2.234]}}}

y = AttrDict(x)

assert y.abc.b == 10
assert y.d.e.f == [2, {"abc": "pqrs"}, 2.234]

y.d.e.g = 11

# del y.abc.c
# OR
del y["abc"]["c"]

assert y.to_dict() == {
    "abc": {"b": 10},
    "d": {"e": {"f": [2, {"abc": "pqrs"}, 2.234], "g": 11}},
}

y.pretty(indent=2)

assert "abc" in y
assert "def" not in y
print("e" in y.d)
{
  "abc": {
    "b": 10
  },
  "d": {
    "e": {
      "f": [
        2,
        {
          "abc": "pqrs"
        },
        2.234
      ],
      "g": 11
    }
  }
}
True
x = {"abc": {"b": 10, "c": 11}, "d": {"e": {"f": [2, {"abc": "pqrs"}, 2.234]}}}
y = AttrDict(x)
y.abc[["b", "c"]]

```↯ AttrDict ↯
b - 10 (🏷️ int)
c - 11 (🏷️ int)

```

write_json

 write_json (obj, fpath, silent=False)

read_json

 read_json (fpath)
try:
    import torch

    d = AD(a=torch.Tensor([1, 2, 3]), b="hello")
    write_json(d, "/tmp/tmp.json")
    print("\n".join(readlines("/tmp/tmp.json")))
except ModuleNotFoundError:
    ...
[10/02/24 18:38:56] INFO     loaded 8 lines                                                                                        d=580741;file://<ipython-input-1-e6d68859b80d>:6\<ipython-input-1-e6d68859b80d>;;\:d=455875;file://<ipython-input-1-e6d68859b80d>:6#<module>:6\<module>:6;;\
{
"a": [
1.0,
2.0,
3.0
],
"b": "hello"
}
d = [1, {1: 1, 2: 2}, 3]

pretty_json({1: 1, 2: 2})
pretty_json(d)

f = write_json(d, "/tmp/test.json")
print(f)
read_json(f)
/tmp/test.json
[1, {'1': 1, '2': 2}, 3]

read_jsonl

 read_jsonl (file)

write_jsonl

 write_jsonl (items, dest, mode='a')

write_yaml

 write_yaml (content, fpath)

read_yaml

 read_yaml (file)

write_xml

 write_xml (data:Union[torch_snippets.markup2.AttrDict,dict],
            file_path:Union[str,pathlib.Path])

read_xml

 read_xml (file_path:Union[str,pathlib.Path])

Read xml data as a dictionary

y.to_dict()
{'abc': {'b': 10, 'c': 11}, 'd': {'e': {'f': [2, {'abc': 'pqrs'}, 2.234]}}}
y

```↯ AttrDict ↯
abc
  b - 10 (🏷️ int)
  c - 11 (🏷️ int)
d
  e
    f
      0 - 2 (🏷️ int)
      1
        abc - pqrs (🏷️ str)
      2 - 2.234 (🏷️ float)

```