Skip to main content

camelCase to snake_case or snake_case to camelCase for Python dict object

Project description

# camel-and-snake

## 🐫 and 🐍 ???🤣

本项目旨在解决 camelCase 和 snake_case 相互转换的问题

**支持 python3 以上版本**


## 应用场景

* 生成对 JavaScript 更友好的 `camelCase` 命名规范的 JSON 响应数据
* 在 JSON 序列化之前将 `snake_case` 的字典转为 `camelCase`
* 在 JSON 反序列化之后将 `camelCase` 的字典转回 `snake_case`


## 如何使用?

### 安装

你可以通过 `pip` 来安装 `camel_and_snake`
> 推荐使用 `pipenv` 在虚拟环境下安装

```shell
$ pip3 install camel_and_snake
```


### 使用

```python
from camel_and_snake import Converter
```

**snake_case -> camelCase**

```python
dict_obj = {
'org_id': 123,
'org_name': 'ShadowCoder',
'member_list': [
{'member_id': 1, 'member_name': 'Cphayim'},
{'member_id': 2, 'member_name': 'Hoyoe'}
]
}

# 转换过程中将对深层的每一个字典进行递归替换 key 值,并返回一个新的对象
json.dumps(Converter.camelify(dict_obj), indent=2)

"""
{
"orgId": 123,
"orgName": "ShadowCoder",
"memberList": [
{
"memberId": 1,
"memberName": "Cphayim"
},
{
"memberId": 2,
"memberName": "Hoyoe"
}
]
}
"""
```


**camelCase -> snake_case**

```python
json_str = '{"orgId": 123, "orgName": "ShadowCoder", "memberList": [{"memberId": 1, "memberName": "Cphayim"}, {"memberId": 2, "memberName": "Hoyoe"}]}'

dict_obj = Converter.snakeify(json.loads(json_str))
# {'org_id': 123, 'org_name': 'ShadowCoder', 'member_list': [{'member_id': 1, 'member_name': 'Cphayim'}, {'member_id': 2, 'member_name': 'Hoyoe'}]}
```

## License

[MIT](https://opensource.org/licenses/MIT)

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

camel_and_snake-0.1.2.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

camel_and_snake-0.1.2-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file camel_and_snake-0.1.2.tar.gz.

File metadata

File hashes

Hashes for camel_and_snake-0.1.2.tar.gz
Algorithm Hash digest
SHA256 39cc75d4a1699f9aa9d862c44fe3d2f38f104ed5df75df3871f1ed4300cbf1d4
MD5 d35119ed0e466f2e46ebd26296461113
BLAKE2b-256 093887f4607357902e9e591830ec1036b696654941acea33af0ba1a2cc826fba

See more details on using hashes here.

File details

Details for the file camel_and_snake-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for camel_and_snake-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e47326af53111f5722ff193b25ed2a38ab4aacac03371d62af7de36502e75aed
MD5 298852914136077c22fffb69273907ff
BLAKE2b-256 ac780f6c41e4e2a4a0c9f973b16d4d0f8c5bfb8b355e88b4d553fc5facad8d8b

See more details on using hashes here.

Supported by

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