2009-07-06 61 views
54

是否有任何可以自动查询XML文件和/或RDBMS表的Python类似于LINQ的项目?该语法不一定非常像C#中的LINQ,但希望以pythonic的方式关闭。LINQ in Python

+1

请注意,有[pyquery](http:// pypi .python.org/pypi/pyquery),一个像Python一样的jquery-syntax /功能包。 – user712092 2011-08-30 16:27:45

+0

py-enumerable/py_linq https://github.com/viralogic/py-enumerable可以帮助 – scls 2017-07-01 21:16:44

回答

5

如果你正在寻找一个ORM再有就是SQLAlchemy

+2

Elixir是SQLAlchemy之上的声明层,使用起来非常简单方便。 http://elixir.ematia.de/ – sunqiang 2009-07-06 04:26:05

+0

Elixir的断开链接 – scls 2017-07-01 21:22:50

31

Pynq实现表达式树:

http://wiki.github.com/heynemann/pynq

微软创建使用表达式树的LINQ(语言集成查询),这是一种关于如何将操作解析到树中的数学概念,以便您可以独立于结果分析操作。

Pynq是表达式树理论和一些提供者的Python实现。逐渐会有更多的提供商,但Pynq将努力使自己的提供商尽可能地容易编写。

+3

声音很酷。 8年未触及。 – Robino 2017-03-15 11:14:51

1

你可能想看看kalamar:http://dyko.org/api/kalamar.html

它是利用语法或多或少类似于LINQ访问存储在不同格式数据的统一数据访问库(XML,MP3,...)上异构后端(文件,sql,...)。

6

我还没有使用它,但是这表明承诺:

asq是一个简单的实施Python中的LINQ风格的API,它工作在Python的iterables,包括在以下方面实现并行版本的Python标准库多处理模块。 API体育功能与对象的LINQ等同,100%声明测试覆盖率和comprehensive documentation

+0

2012年在Google Code上打开了一个问题,2015年7月迁移到GH,未于2017年7月解决https://github.com/sixty-north/asq/issues/5 – scls 2017-07-01 21:21:27

+0

@scls rob-smallshire为您提供了答案并关闭了问题 – 2017-09-07 16:59:38

4

我觉得Pony ORM应该在这里提到。对我来说,这 LINQ:

q = select((p.name, p.price) for p in Product) 
q2 = q.filter(lambda n, p: n.name.startswith(“A”) and p > 100) 

或直接从他们的着陆页:

select(c for c in Customer 
     if sum(c.orders.price) > 1000) 
4

只是为了好玩我创建了以下代码。

"""This module provides linq-like extensions for some common data structures""" 
import __builtin__ 

class extlist(list): 
    """subclass of list""" 
    def where(self, condition): 
     return extlist(filter(condition, self)) 
    def aggregate(self, condition): 
     return extlist(reduce(condition, self)) 
    def select(self, condition=lambda x: x): 
     return extlist(map(condition, self)) 

__builtin__.list = extlist 


class extstr(str): 
    """subclass of str""" 
    def where(self, condition): 
     return extstr(filter(condition, self)) 
    def aggregate(self, condition): 
     return extstr(reduce(condition, self)) 
    def select(self, condition=lambda x: x): 
     return extstr(map(condition, self)) 

__builtin__.str = extstr 

使用

a = list('apple') 
a.where(lambda u: u == 'p').select(lambda x: x.upper()) 

[ 'P', 'P']

2

我们推出PythonQL,这基本上是LINQ的Python类固醇(好吧,也许维生素)。它是Python语法的扩展,但是(drumrolls)你可以通过pip来安装它,它不会破坏任何现有的代码:)检查出来:www.pythonql.org