# Smarter PYthon Dicts (SPYD)

SPYD introduces a new SmartDict object, which has several features that the builtin python dict needs

---

## Table of Contents

1. [Installation](#installation)
2. [Usage](#usage)
3. [License](https://github.com/Mazurex/SPYD/blob/index/LICENSE)

---

## Installation

You can install the library via `pip`:

```bash
pip install spyd
```

You can also find the source code [here](https://github.com/Mazurex/SPYD/tree/index/SPYD)

---

## Usage

After importing the library:

```py
from spyd import SmartDict
```

You can just create a SmartDict as such:

```py
my_dict = SmartDict()
```

If you wish to convert an existing dict into a SmartDict, or you wish to assign values during creation, you can do it as such:

```py
my_dict = SmartDict({"name": "Jeff"})
```

or:

```py
normal_dict = {"name": "Jeff"}
my_dict = SmartDict(normal_dict)
```

One awesome feature of SmartDicts is `dot notation`, it works the same as bracket notation, but easier!
Fortunately, SmartDict accepts both:

```py
my_dict = SmartDict()
# bracket notation
my_dict["hello"] = "world"
# dot notation
my_dict.hello = "world"
```

SmartDicts also automatically create nested SmartDicts when using dot notation:

```py
my_dict = SmartDict()
my_dict.data.name = "Jeff"
```

In the above example, "data" would be created as a SmartDict within the original SmartDict.
Returns:

```py
SmartDict{"data": SmartDict{"name": "Jeff"}}
```

With SmartDicts you can also easily convert to and from JSON and YAML!
Converting a SmartDict to a JSON format is easy:

```py
my_dict = SmartDict()
my_dict.data.name = "test"
my_dict.to_json()
```

The JSON formatted SmartDict can be applied to a JSON like this:

```py
my_dict = SmartDict()
my_dict.data.name = "test"
with open("data.json", "w") as file:
    file.write(my_dict.to_json())
```

The above examples also apply to YAML.

You can also convert JSON and YAML data into SmartDict:

```py
my_dict = SmartDict.from_json()
```

Here you don't have to create a new instance of SmartDict as you're only accessing a static method.
`from_json()` only parameter is formatted JSON or YAML data, which you can get like this:

```py
with open("my_file.json", "r") as file:
    my_dict = SmartDict.from_json(file.read())
```

Once again, the above examples also count for YAML files.

You can also merge a dict into an existing SmartDict:

```py
my_dict = SmartDict()
my_dict.name = "Jeff"
my_dict.hello = "World!"

other_dict = {"hello": "world"}

my_dict.merge(other_dict, overwrite = False)
```

merge simply appends all values from `other_dict` into the existing SmartDict, if the `overwrite` paramter is true, any same key value from the existing SmartDict will be replaced with the new dict's value.

You can also flatten a SmartDict, essentially removing any nesting:

```py
my_dict = SmartDict()
my_dict.data.name = "test"
my_dict.other.address = "321 idk street"
print(my_dict.flatten())
```

Returns:

```py
{'data.name': 'test', 'other.address': '321 idk street'}
```

You can also find keys by looking up values:

```py
my_dict = SmartDict()
my_dict.name = "Jeff"
my_dict.hello = "World!"

print(my_dict.reverse_lookup("World!"))
```

The above print would return "hello" as it is the first occuring key with the value "World!"
