类似于django的python对象和字典查询api

iterable_orm的Python项目详细描述


Latest PyPI versionLatest Travis CI build status

iterable允许您使用django orm提供的similer api过滤、排除和排序数据。数据必须是对象或字典的列表。支持Python2&3。

iterable提供以下api

filter-返回与给定查找参数匹配的对象或词典的新列表

exclude-返回与给定查找参数不匹配的对象或词典的新列表

get-如果有两个匹配项,则返回单个对象或字典返回异常

order_by-返回按列表排序的对象或词典

first-返回筛选或排除数据的第一个对象或字典

last-返回筛选或排除数据的第一个对象或字典

count-返回筛选或exlcude或字典的长度

请注意,iterable\u orm不支持django orm提供的类q对象,但通过传递匿名函数来筛选或排除函数(例如manager.filter(a g e=lambda x:x>;=20和x<;=30))提供了一种解决方法

基本用法

将对象或字典列表传递给queryset

fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)

过滤和排除

可以按值或查找筛选和排除数据,例如gt、gte、lt、lte、startswith、istartswith、endswith、contains、icontains、value-in、value-not-in、value-range、date-range(需要datetime对象)或匿名函数。

iterable\u orm还允许您使用标准的双下划线符号来分隔相关字段来筛选相关对象,例如manager.filter(parent\u name='john'),此筛选器按parent.child='john'。

所有的过滤和exlcuding都是惰性的,因此您可以构造任意数量的过滤,它只在迭代、调用count、first、last和order_by时计算。

下面是过滤和排除的代码示例,

fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Filter accounts with age greater than 25 and exclude if gender is maledata=manager.filter(age__gt=20).exclude(gender='male')# Filter using lamdadata=manager.filter(age=lambdax:x>=20andx<=30).exclude(gender='male')# Filter accounts with the name starting with letter 's' and gender is femaledata=manager.filter(name__istartswith='s').exclude(gender='female')# Filter accounts who have registred from 2014 till 2016 of current date and who are a femaledata=manager.filter(registered__date_range=(datetime.today().replace(year=2014),datetime.today().replace(year=2016))).exclude(gender='female')# Filter accounts who have registred from 01-01-2015 till 2016 and who are a female if date is string objectdata=manager.filter(registered__date_range=('01-01-2015','01-01-2016')).exclude(gender='female')

过滤

可以按值或查找(如gt、gte等)筛选数据。

下面是过滤的代码示例,

fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Filter accounts with age greater that 25data=manager.filter(age__gt=20)# Filter accounts with age less that 25 and who are a maledata=manager.filter(age__lt=20,gender='male')# Get number of accounts with age 20 and who are a femaledata=manager.filter(age__gt=20,gender='female').count()# Filter accounts with name starting with letter 's'data=manager.filter(name__istartswith='s')# Filter accounts who have registred from 01-01-2015 till 2016data=manager.filter(registered__date_range=('01-01-2015','01-01-2016'))# Filter accounts who have friends who are a maledata=manager.filter(friends__gender='male')# Filter accounts with date rangedata=manager.filter(registered__value_range=('2015-11-15','2015-11-16')# chain filter e.gdata=manager.filter(name__istartswith='s').filter(gender='male')

不包括

可以按值或查找(如gt、gte等)排除数据。 下面是exlcude函数的代码示例:

fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Exclude accounts with age greater that 25data=manager.exclude(age__gt=20)# Exclude accounts with age less then 25 and who are a maledata=manager.exclude(age__lt=20,gender='male')# Exclude accounts with name starting with letter 's'data=manager.filter(name__istartswith='s')# Exclude accounts who have registred from 01-01-2015 till 2016data=manager.exclude(registered__date_range=('01-01-2015','01-01-2016'))# Exclude accounts who have friends who are a maledata=manager.filter(friends__gender='male')# Chain exclude e.g.data=manager.exclude(name__istartswith='s').exclude(gender='male')

订购

您可以按对象或字典的任何值排序数据:

fromiterable_ormimportQuerySetAccounts=[Alistofaccountobjectsthathaveattrubtessuchasname,email,age,genderect]manager=Queryset(Accounts)# Order by namedata=manager.order_by('name)# Order name by descendingdata=manager.order_by('-name)# Ordering by related lookup of friends namedata=manager.order_by('friends__name')# Ordering by related lookup of friends name descendingdata=manager.order_by('-friends__name')

单元测试

单元测试包括api的完整示例用法

运行单元测试:

pythontest.py

安装

使用以下命令安装最新版本:

pip install iterable_orm

兼容性

python 2.7、3.0到3.5

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在数据库中存储密码的java加密方法   java正则表达式否定整个正则表达式   java为什么要得到这个Hashmap输出?   看不到玻璃鱼爪哇SE 6   类对象包装器中的Java基本数据字段   java从文本文件中读取整数并存储到单独的变量中?(扫描仪)   优化大型Java数据阵列的处理和管理   如何使用Java XML包装类创建对象   java为ExecutorService invokeAll()创建包装器   java如何在Android Studio 1.0.0中设置Facebook SDK?获取SDK位置未找到错误   java在尝试从线程启动动画时调用了FromErrorThreadException   java根据哈希确认文件内容   通过java在neo4j中获取索引值相同的所有节点?   java为什么我的Validare邮政编码(布尔)程序返回false?   java会话自动从servlet/jsp生成,尽管存在以下条件:<%@page session=“false”%>   创建新LANsocket时拒绝java连接   java如何多线程更新由sql代码更新的数据库?   安卓 Java使用类作为集合来添加项   安卓为什么我的清单文件不声明java包?