https://github.com/zerointensity/snakedict
Keep your dict keys PEP 8 compliant.
https://github.com/zerointensity/snakedict
Last synced: 30 days ago
JSON representation
Keep your dict keys PEP 8 compliant.
- Host: GitHub
- URL: https://github.com/zerointensity/snakedict
- Owner: ZeroIntensity
- License: mit
- Created: 2022-07-19T14:55:27.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-19T15:08:55.000Z (over 3 years ago)
- Last Synced: 2025-03-10T14:50:17.739Z (11 months ago)
- Language: Python
- Homepage: https://pypi.org/project/snakedict
- Size: 7.81 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Snake Dict
## Keep your dictionary keys PEP 8 compliant
### Quick Example
```py
from snakedict import convert
my_dict = {
"helloWorld": "test",
"thisIsCamelCase": "hello",
"this_is_snake_case": "world",
}
my_dict = convert(a)
print(my_dict["hello_world"]) # test
```
### Installation
#### Linux/macOS
```
python3 -m pip install -U snakedict
```
#### Windows
```
py -3 -m pip install -U snakedict
```
### Usage
There are 2 main functions, `convert` and `auto`.
`convert` does what was shown above, it takes in a dictionary and converts keys that are in camelCase to snake_case:
```py
from snakedict import convert
my_dict = {
"something": "test",
(1, 2, 3): "hello" # you can use any key you want, snakedict won't touch it unless its in camelcase
}
print(convert(my_dict)) # no camelcase found, nothing happens
```
On the other hand, `auto` is a decorator that converts the response to snake case:
```py
from snakedict import auto
@auto()
def fetch_something():
return { # lets pretend this is a call to some api
"accountName": "someone1234",
"joinDate": "7/19/22",
}
print(fetch_something()["account_name"])
```
`auto` has a parameter called `execute_maybe` that won't call `convert` if the response wasn't a `dict`:
If you want an error to be raised if the function returns `None`, set it to `False`:
```py
@auto(execute_maybe = False)
def fetch_something():
return None
fetch_something() # TypeError!
```