Python:orator/backpack内置数据操作类Collection
  TEZNKK3IfmPf 2023年11月15日 31 0

文档

支持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}]
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   24天前   21   0   0 json
  TEZNKK3IfmPf   2024年04月19日   19   0   0 jsonjava
TEZNKK3IfmPf