Skip to content

Commit

Permalink
Chore: Add get_nested function (#7)
Browse files Browse the repository at this point in the history
* Fix Readme

* Add `get_nested`function
  • Loading branch information
yezz123 authored Dec 7, 2021
1 parent 1cebe1b commit 41e7b02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Pyngo

![Pyngo](https://user-images.githubusercontent.com/52716203/145123561-ef58bc47-b5a9-4aaf-b13f-be881cbe6539.png)

<p align="center">
<em>Utils to help integrate pydantic into Django projects</em>
</p>

[![codecov](https://codecov.io/gh/yezz123/pyngo-utils/branch/main/graph/badge.svg?token=QAGGVJ8URH)](https://codecov.io/gh/yezz123/pyngo-utils)
[![PyPI version](https://badge.fury.io/py/pyngo.svg)](https://badge.fury.io/py/pyngo)
[![Downloads](https://pepy.tech/badge/pyngo/month)](https://pepy.tech/project/pyngo)
[![License](https://img.shields.io/badge/License-MIT-green?style)](https://opensource.org/licenses/MIT)
[![Language](https://img.shields.io/badge/Language-Python-green?style)](https://github.com/yezz123)
[![framework](https://img.shields.io/badge/Framework-Django-green?style)](https://fastapi.tiangolo.com/)
[![framework](https://img.shields.io/badge/Framework-Django-green?style)](https://www.djangoproject.com/)
[![Pypi](https://img.shields.io/pypi/pyversions/pyngo.svg?color=%2334D058)](https://pypi.org/project/pyngo)

---
Expand Down
16 changes: 16 additions & 0 deletions pyngo/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ def set_nested(data: Dict[str, Any], keys: Sequence[str], value: Any) -> None:
for key in keys[:-1]:
data = data.setdefault(str(key), {})
data[keys[-1]] = value


def get_nested(data: Dict[str, Any], keys: Sequence[str]) -> Any:
"""
Get a value from a nested dictionary.
Args:
data (Dict[str, Any]): The dictionary to get the value from.
keys (Sequence[str]): The keys to get the value at.
Returns:
Any: The value.
"""
for key in keys[:-1]:
data = data[key]
return data[keys[-1]]

0 comments on commit 41e7b02

Please sign in to comment.