Git Product home page Git Product logo

gqylpy-dict's Introduction

LOGO Release Python Versions License Downloads

gqylpy-dict

English | 中文

gqylpy-dict is based on the built-in dict and serves as an enhancement to it. It can do everything the built-in dict can do, and even more. (Designed specifically for the neurologically diverse)

pip3 install gqylpy_dict

>>> from gqylpy_dict import gdict

>>> gdict == dict
True

>>> gdict is dict
False

>>> x = {'a': [{'b': 'B'}]}
>>> x = gdict(x)
>>> x.a[0].b
'B'

>>> x.deepget('a[0].b')
'B'

Let's delve deeper into the functionalities and usages of the gdict class.

Firstly, the gdict class is a custom dictionary class that inherits from Python's built-in dict class. A special feature of gdict is its support for accessing and modifying key-value pairs in the dictionary using dot notation (.). This means we can access dictionary values as if they were object attributes. For instance, given a dictionary {'name': 'Tom', 'age': 25}, we can define a gdict object as follows:

my_dict = gdict({'name': 'Tom', 'age': 25})

Using dot notation, we can access the dictionary values:

my_dict.name  # 'Tom'
my_dict.age  # 25

We can even modify the dictionary values:

my_dict.name = 'Jerry'

At this point, the value associated with the 'name' key in the dictionary has been changed to 'Jerry'.

Additionally, gdict supports multi-level nested data structures, meaning the keys and values stored in a gdict object can also be dictionaries. For example:

my_dict = gdict({
    'personal_info': {'name': 'Tom', 'age': 25},
    'work_info': {'company': 'ABC Inc.', 'position': 'Engineer'}
})

We can access and modify values in nested dictionaries:

my_dict.personal_info.name  # 'Tom'
my_dict.work_info.position = 'Manager'

Aside from dot notation, we can also access and modify values in a gdict object using traditional dictionary access methods:

my_dict['personal_info']['name']  # 'Tom'
my_dict['work_info']['position'] = 'Manager'

Lastly, the gdict class supports various input formats during instantiation:

my_dict = gdict({'name': 'Tom', 'age': 25})
my_dict = gdict(name='Tom', age=25)
my_dict = gdict([('name', 'Tom'), ('age', 25)])

In summary, the design and implementation of the gdict class provide a convenient and extensible data structure that allows for more flexible manipulation of Python dictionary objects.

gqylpy-dict's People

Contributors

2018-11-27 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gqylpy-dict's Issues

字典对象不能使用 `set` 去重

这是大的问题,在一些场景中我们必须对字典对象去重,这就得引入或现写非哈希的去重算法,极为不便。如果能直接使用set去重,将大幅提高代码开发效率及可读性。我们能否做到让 gdict 对象使用 set 去重?

调用 `copy.copy` 得到深度拷贝的结果!

经分析,确认原因在于 gdict 类初始化的逻辑与常规类不同,gdict 类初始化过程可以说是一个(不完全的)深度拷贝。
copy.copy 调用过程就是直接创建一个相同类的对象,这样就像如上所述得到深度拷贝的结果。

`deepsetdefault` 执行后应该返回原值还是转为 `gdict` 实例后的值

gdict.deepsetdefaultdict.setdefault 功能相似,值不存在则设置值并返回值。若设置的值是一个 dict 实例且不存在,我们会先将这个 dict 实例转为 gdict 实例再执行设置,从而保证 gdict 实例的正确性(gdict 实例内层绝不包含 dict 实例,这是我们对 gdict 的理念,也是原则)。那么在返回值时,我们应该返回原 dict 实例还是转换后的 gdict 实例呢,问题的关键在于此。

`gdict` 类无法被继承

被继承时引发异常:

AttributeError: instance of "type" must define "__masquerade_class__" attribute, use to specify the class to disguise.

仅希望获取某几个值

在某些情况下我们希望从字典中取到的值是某几个特定值中的其中一个,如果不是,将返回默认特定值。

通过值解压的方式实例化得到的不是一个字典对象

内置 dict 通过值解压的方式实例化得到的是一个字典对象,而 gdict 通过值解压的方式实例化得到的是一个列表对象。且看下面的代码运行结果:

>>> dict((x, y) for x, y in [('a', 'A'),  ('b', 'B')])
{'a': 'A', 'b': 'B'}

>>> gdict((x, y) for x, y in [('a', 'A'), ('b', 'B')])
[['a', 'A'], ['b', 'B']]

我们希望它们得到同样的结果,无论在任何情况下。gdict 得到列表对象肯定是错误的,这是一个严重的问题,它使我们对 gdict 的理念产生了偏移,我们希望 gdict 在任何时候都有着与 dict 相同的特性。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.