Skip to content

Commit

Permalink
chore: update item docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
shengchenyang committed Nov 4, 2024
1 parent b55fc05 commit d97709b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 9 additions & 6 deletions ayugespidertools/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,25 @@ class AyuItem(MutableMapping, metaclass=ItemMeta):
>>> # 获取字段
>>> item["_table"]
'ta'
>>>
>>> # 添加 / 修改字段,不存在则创建,存在则修改
>>> item["_table"] = "tab"
>>> item["title"] = "tit"
>>> # 也可通过 add_field 添加字段,但不能重复添加相同字段
>>> item.add_field("num", 10)
>>> [ item["_table"], item["title"], item["num"] ]
['tab', 'tit', 10]
>>> item.asdict()
{'title': 'tit', '_table': 'tab', 'num': 10}
>>> type(item.asitem())
<class 'ayugespidertools.items.ScrapyItem'>
>>> # 转换为 dict
>>> item.asdict() == {'title': 'tit', '_table': 'tab', 'num': 10}
True
>>> # 转换为 scrapy item
>>> item.asitem().__class__.__name__ == "ScrapyItem"
True
>>> # 删除字段
>>> item.pop("num")
10
>>> del item["title"]
>>> item
{'_table': 'tab', 'num': 10}
{'_table': 'tab'}
"""

def __init__(
Expand Down
5 changes: 3 additions & 2 deletions docs/topics/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ item = AyuItem(_table="ta")

```shell
>>> # 删除字段:
>>>
>>> item.pop("num")
10
>>> del item["title"]
>>> item
{'_table': 'tab', 'num': 10}
{'_table': 'tab'}
```

## 使用示例
Expand Down

0 comments on commit d97709b

Please sign in to comment.