# Python Dictionary Representation
## Description
The `DictRepr` class is an abstract base class that enables any subclass to define its own dictionary representation. This makes it possible to do:
```python
dict_repr = dict(arbitrary_object)
```
The subclass only needs to do two things:
- Override the `keys()` instance method to return a list of strings
- Have the keys map to variables or properties on that object
## Example
```python
lass MyArbitraryClass(DictRepr):
_keys = ["uppercase", "length"]
def __init__(self, value: str):
self._value = value
@property
def uppercase(self):
return self._value.upper()
@property
def length(self):
return len(self._value)
def keys(self):
return self._keys
if __name__ == "__main__":
myobj = MyArbitraryClass("Hello World")
mydict = dict(myobj)
print("mydict type: {}".format(type(mydict)))
pprint(mydict)
```
```console
$ python3 examples/dict_repr_example.py
mydict type: <class 'dict'>
{'length': 11, 'uppercase': 'HELLO WORLD'}
```
## Mapping Keys to Object Attributes
It may be the case that a desired dicitonary key isn't the name of the corresponding attribute. For example, a string may have spaces or other characters that, while perfectly suitable as a dictionary key, make it illegal as an object attribute name.
In this case, the `KeyTuple` class provides a mapping between key string and attribute name. Simply include a KeyTuple object in place of the key string:
```Python
_keys = [
KeyTuple("string with spaces 1", "no_spaces_1"),
KeyTuple("string with spaces 2", "no_spaces_2")
]
```
### KeyTuple Example
Let's modify the previous example:
```python
from py_dict_repr import DictRepr, KeyTuple
class MyArbitraryClass(DictRepr):
_keys = [
KeyTuple("uppercase verion", "uppercase"),
KeyTuple("string length", "length"),
# KeyTuple may be mixed with regular key strings
"lowercase"
]
def __init__(self, value: str):
self._value = value
@property
def uppercase(self):
return self._value.upper()
@property
def lowercase(self):
return self._value.lower()
@property
def length(self):
return len(self._value)
def keys(self):
return self._keys
if __name__ == "__main__":
myobj = MyArbitraryClass("Hello World")
mydict = dict(myobj)
print("mydict type: {}".format(type(mydict)))
pprint(mydict)
```
```console
$ python3 examples/dict_repr_example.py
python3 ./examples/dict_repr_keytuple_example.py
mydict type: <class 'dict'>
{string length: 11, uppercase verion: 'HELLO WORLD', 'lowercase': 'hello world'}
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源分类:Python库 所属语言:Python 资源全名:py-dict-repr-0.1.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
py-dict-repr-0.1.1.tar.gz (13个子文件)
py-dict-repr-0.1.1
PKG-INFO 3KB
py_dict_repr.egg-info
PKG-INFO 3KB
SOURCES.txt 274B
top_level.txt 13B
dependency_links.txt 1B
LICENSE 1KB
py_dict_repr
__about__.py 470B
__init__.py 204B
py_dict_repr.py 3KB
version.py 187B
setup.cfg 38B
setup.py 622B
README.md 3KB
共 13 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Vue.js快速构建python桌面应用程序的模板项目源码+运行教程(支持打包为可执行文件).zip
- 防护具检测57-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 视频下载-b站视频下载器
- CSV数据操作的工具包-含合并CSV文件、Excel转CSV、CSV转XLSX、统计CSV行数、重命名表头、选择和重排CSV列等功能.zip
- App商店优化(ASO)权威指南:提高App可见度与转化率的技术策略
- TomVPN_3.0.7.apk
- AEC论文解读 - ACOUSTIC ECHO CANCELLATION WITH THE DUAL-SIGNAL TRANSFORMATION LSTM NETWORK
- Vegetation Studio 1.5.3
- 阀门检测49-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 非常好的SqlServer大量源代码和教程资料100%好用.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功