iterables的sql风格连接。
join的Python项目详细描述
python iterables的sql风格连接。
>>>fromjoinimportjoin,merge,tuple_join>>>dogs=[...Dog('gatsby','Rruff!',16),...Dog('ein','wruf!',9),...]>>>cats=[...Cat('pleo','mreeeoww',16),...Cat('xena','mreow',12),...Cat('gatsby','rowr',15),...]>>>catdogs=merge(cats,dogs,key='name')>>>catdogs[CatDog({'right':Dog(name='gatsby',woof='Rruff!',weight=16),'name':'gatsby','weight':15,'meow':'rowr','woof':'Rruff!','left':Cat(name='gatsby',meow='rowr',weight=15)})]>>>catdogs[0].meow'rowr'>>>catdogs[0].woof'Rruff!'
安装
使用pip安装:
pip install join
使用
join完成了将iterable项关联在一起的工作,但为您提供了所有定制功能,允许您提供自己的连接函数、左右iterable的单独键,甚至允许您使用函数而不是属性名。
^例如,上面使用的{tt3}$就是join使用对象联合来联接匹配的对象。您可以使用元组联接,这是join:
>>>join(cats,dogs,key='name',join_fn=tuple_join)[(Cat(name='gatsby',meow='rowr',weight=15),Dog(name='gatsby',woof='Rruff!',weight=16))]
提供自己的连接功能很简单:
>>>defweight_sum(left,right):...returnleft.weight+right.weight...>>>join(cats,dogs,key='name',join_fn=weight_sum)[31]
使用单独的按键功能也很简单:
>>>defcat_key(cat):...returncat.weight%3==0# weight divisible by 3...>>>defdog_key(dog):...returndog.weight%4==0# weight divisible by 4...>>>defname_join(left,right):...returnleft.name+'-'+right.name...>>>join(cats,dogs,left_key=cat_key,right_key=dog_key,join_fn=name_join)['pleo-ein','xena-gatsby','gatsby-gatsby']
贡献
欢迎提出请求和问题!使用sh run_tests.sh从项目根目录运行测试。