文檔 https://orator-orm.com/docs/0.9/collections.html 支援36個函數
all avg chunk collapse contains count diff each every
filter first flatten forget for_page get implode is_empty
last map merge pluck pop prepend pull push put reduce
reject reverse serialize shift sort sum take to_json
transform unique where zip
安裝
pip install backpack
導入子產品
# 如果安裝了 orator可以使用
from orator import Collection
# 或者
from backpack import Collection
# 測試使用的data資料
data = [
{'name': 'Tom', 'age': 23},
{'name': 'Jack', 'age': 25}
]
1、簡單的CURD
# 所有資料
print(Collection([1, 2, 3]).all())
[1, 2, 3]
# 取第一個值
print(Collection([1, 2, 3]).first())
1
# 帶條件取值
print(Collection([1, 2, 3]).first(lambda item: item > 1))
2
# 取最後一個值
print(Collection([1, 2, 3]).last())
3
# 分頁取值
print(Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]).for_page(2, 3).all())
[4, 5, 6]
# 間隔取數
print(Collection([1, 2, 3]).every(2).all())
[1, 3]
# 前往後取值
print(Collection([2, 1, 3]).take(2).all())
[2, 1]
# 後往前取值
print(Collection([2, 1, 3]).take(-1).all())
[3]
# 切片取值
print(Collection([1, 2, 3, 4, 5, 6])[1:4:2].all())
# [2, 4]
# 擷取值
print(Collection([1, 2, 3]).get(4, 'default'))
# default
# 根據鍵設定值
print(Collection([1, 2, 3]).put(0, 5).all())
[5, 2, 3]
c = Collection([1, 2, 3])
c[0] = 5
print(c.all())
[5, 2, 3]
# 移除資料,不傳回值
print(Collection([1, 2, 3]).forget(1).all())
[1, 3]
# 傳回移除
print(Collection([1, 2, 3]).pull(0))
1
# 前部插入
print(Collection([1, 2, 3]).prepend(0).all())
[0, 1, 2, 3]
# 彈出第一個值
print(Collection([1, 2, 3]).shift())
1
# 尾部插入
print(Collection([1, 2, 3]).push(4).all())
[1, 2, 3, 4]
print(Collection([1, 2, 3]).append(4).all())
[1, 2, 3, 4]
# 彈出
print(Collection([1, 2, 3]).pop(1))
# 條件取值
print(Collection(data).where('age', 23).all())
[{'name': 'Tom', 'age': 23}]
2、判斷操作
```python
# 空值測試
print(Collection([]).is_empty())
True
# 包含
print(Collection([1, 2, 3]).contains(1))
# True
print(1 in Collection([1, 2, 3]))
# True
print(Collection([1, 2, 3]).contains(lambda item: item > 1))
# True
print(Collection(data).contains('name', 'Simon'))
# False
3、資料變換
# 反轉
print(Collection([1, 2, 3]).reverse().all())
[3, 2, 1]
# 排序
print(Collection([2, 1, 3]).sort().all())
[1, 2, 3]
# 取唯一值
print(Collection([2, 1, 3, 3]).unique().all())
[2, 1, 3]
# 變換資料,修改自身
print(Collection([2, 1, 3]).transform(lambda item: item * 2).all())
[4, 2, 6]
# 僅疊代,不修改原對象
print(Collection([1, 2, 3]).each(lambda x: x + 1).all())
[1, 2, 3]
# 過濾
print(Collection([1, 2, 3]).filter(lambda item: item > 2).all())
[3]
# 映射
print(Collection([1, 2, 3]).map(lambda x: x + 1).all())
[2, 3, 4]
# 移除滿足條件的值
print(Collection([1, 2, 3, 4]).reject(lambda item: item > 3).all())
[1, 2, 3]
# 拆分
print(Collection([1, 2, 3]).chunk(size=2).serialize())
[[1, 2], [3]]
# 塌陷
print(Collection([[1, 2], [3, 4]]).collapse().all())
[1, 2, 3, 4]
# 壓平資料,保留值
print(Collection([1, 2, [3, 4, 5, {'foo': 'bar'}]]).flatten().all())
[1, 2, 3, 4, 5, 'bar']
# 取字典值
print(Collection(data).pluck('name').all())
['Tom', 'Jack']
print(Collection(data).pluck('name', 'age'))
{23: 'Tom', 25: 'Jack'}
4、兩個集合操作
# 差異比較
print(Collection([1, 2, 3]).diff([2, 3, 4]).all())
[1]
# 合并
print(Collection([1, 2, 3]).merge([1, 2, 3]).all())
[1, 2, 3, 1, 2, 3]
# 合并序列
print(Collection([1, 2, 3]).zip([4, 5, 6]).all())
[(1, 4), (2, 5), (3, 6)]
5、計算操作
# 計數
print(Collection([1, 2, 3]).count())
3
# 計數
print(len(Collection([1, 2, 3])))
3
# 平均數
print(Collection([1, 2, 3]).avg())
2.0
print(Collection(data).avg('age'))
24.0
# 求和
print(Collection([2, 1, 3]).sum())
6
print(Collection(data).sum('age'))
48
# 累積計算
print(Collection([1, 2, 3]).reduce(lambda result, item: result + item, 0))
6
6、序列化
# 取值拼接
print(Collection(data).implode('name', ', '))
# Tom, Jack
print(Collection(['foo', 'bar', 'baz']).implode('-'))
# foo-bar-baz
# 轉字元串
print(Collection(data).serialize())
[{'name': 'Tom', 'age': 23}, {'name': 'Jack', 'age': 25}]
# 轉json
print(Collection(data).to_json())
[{"name": "Tom", "age": 23}, {"name": "Jack", "age": 25}]