-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on 2021/4/8 11:32 上午 | ||
--------- | ||
@summary: | ||
--------- | ||
@author: Boris | ||
@email: [email protected] | ||
""" | ||
|
||
|
||
class PerfectDict(dict): | ||
""" | ||
>>> from feapder.utils.perfect_dict import PerfectDict | ||
>>> data = PerfectDict(id=1, url="xxx") | ||
>>> data | ||
{'id': 1, 'url': 'xxx'} | ||
>>> data.id | ||
1 | ||
>>> data.get("id") | ||
1 | ||
>>> data["id"] | ||
1 | ||
>>> id, url = data | ||
>>> id | ||
1 | ||
>>> url | ||
'xxx' | ||
>>> data[0] | ||
1 | ||
>>> data[1] | ||
'xxx' | ||
>>> data = PerfectDict({"id":1, "url":"xxx"}) | ||
>>> data | ||
{'id': 1, 'url': 'xxx'} | ||
""" | ||
|
||
def __init__(self, _dict: dict = None, _values: list = None, **kwargs): | ||
self.__dict__ = _dict or kwargs or {} | ||
super().__init__(self.__dict__, **kwargs) | ||
self.__values__ = _values or list(self.__dict__.values()) | ||
|
||
def __getitem__(self, key): | ||
if isinstance(key, int): | ||
return self.__values__[key] | ||
else: | ||
return self.__dict__[key] | ||
|
||
def __iter__(self, *args, **kwargs): | ||
for value in self.__values__: | ||
yield value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on 2021/4/8 1:06 下午 | ||
--------- | ||
@summary: | ||
--------- | ||
@author: Boris | ||
@email: [email protected] | ||
""" | ||
|
||
from feapder.utils.perfect_dict import PerfectDict | ||
|
||
|
||
task_key = ["id", "url"] | ||
task = [1, "http://www.badu.com"] | ||
task = Task(_dict=dict(zip(task_key, task)), _values=task) | ||
|
||
task = Task(id=1, url="http://www.badu.com") | ||
task = Task({"id":"1", "url":"http://www.badu.com"}) | ||
|
||
print(task) | ||
id, url = task | ||
print(id, url) | ||
print(task[0], task[1]) | ||
print(task.id, task.url) | ||
print(task["id"], task["url"]) | ||
print(task.get("id"), task.get("url")) |