需要帮助在python中选择简单数据库中的项目

时间:2021-05-16 11:15:24

I'm trying to write a circuit schematic drawing tool in Python. I'm creating a simple database based on dictionaries which holds all the components and their properties. I'm also trying to create a simple query language where you can select, for example, all resistors with value>100ohms or footprint='0402'

我正在尝试用Python编写电路原理图绘制工具。我正在创建一个基于字典的简单数据库,它包含所有组件及其属性。我还试图创建一种简单的查询语言,您可以选择,例如,所有电阻值> 100欧姆或占位面积='0402'

So far, I can select things using some primitive predicate search and then union and intersection for more complex things.

到目前为止,我可以使用一些原始谓词搜索来选择事物,然后使用联合和交集来处理更复杂的事情。

However, I'm having trouble defining what the semantics ought to be for a purely negative search. For instance, something like

但是,我很难定义纯粹否定搜索的语义应该是什么。例如,像

footprint != '0402'

足迹!='0402'

should select all items with footprint not equal to 0402. But doing this with only intersection gives me a blank result. For this to work I need to select all components, then intersect with "not 0402" to remove the ones I don't want.

应该选择占地面积不等于0402的所有项目。但仅使用交叉点进行此操作会给我一个空白结果。为此,我需要选择所有组件,然后与“not 0402”相交以删除我不想要的组件。

But this seems kind of brute force and seems like a fishy solution. I'm not interested in using a "real" database and query language, so please don't suggest that. I'm looking for the proper engineering rationale here, not necessarily a workaround to the problem.

但这似乎是一种蛮力,似乎是一种可疑的解决方案。我对使用“真正的”数据库和查询语言不感兴趣,所以请不要这么做。我在这里寻找合适的工程原理,不一定是解决问题的方法。

This problem was covered to some degree in SICP book, but I got very confused, since I think they were also using continuations and stuff, which I don't get yet.

这个问题在SICP的书中有所涉及,但我很困惑,因为我认为他们也使用了延续和东西,我还没有得到。

Can someone please explain what the 'proper' usage ought to be for negatively selecting stuff. I tried this in a commercial cad tool and it worked as expected, but then I've seen some SQL query examples (I think), which selected things first, then removed the unwanted ones.

有人可以解释一下“正确”用法应该是什么,以消极地选择东西。我在商业CAD工具中尝试了这个,它按预期工作,但后来我看到了一些SQL查询示例(我认为),首先选择了一些东西,然后删除了不需要的东西。

thanks
michael

3 个解决方案

#1


You'll be a lot happier with a proper database.

使用合适的数据库会让您更开心。

You have SQLite with your Python distribution. Simply define tables instead of dictionaries and use SQL.

你的Python发行版有SQLite。只需定义表而不是字典并使用SQL。

If you need more power and sophistication, you can add SQLAlchemy (or SQLObject) and you won't struggle with these problems.

如果您需要更多的功能和复杂性,可以添加SQLAlchemy(或SQLObject),您将不会遇到这些问题。

#2


It really depends on how you've implemented this "simple database based on dictionaries" as to why straight negation isn't working - you'll have to give us some more clues.

这实际上取决于你如何实现这个“基于字典的简单数据库”,为什么直接否定不起作用 - 你必须给我们一些更多的线索。

There's no reason why a straightforward negation match shouldn't work in Python, e.g.:

没有理由为什么直接的否定匹配不适用于Python,例如:

components = [
    { 'name': 'resistor', 'footprint': '0402', },
    { 'name': 'LED', 'footprint': '0100', },
    { 'name': 'speaker', 'footprint': '2000', },
]

[comp for comp in components if comp['footprint'] != '0402']
# [{'footprint': '0100', 'name': 'LED'}, {'footprint': '2000', 'name': 'speaker'}]

At a stretch, this is a simple database based on dictionaries - the specific capabilities are really going to depend on your actual implementation.

在一段时间内,这是一个基于字典的简单数据库 - 具体功能实际上取决于您的实际实现。

I'm resisting the temptation to suggest use of a real database and query language as I assume this is a learning exercise. It is a learning exercise, right? :)

我正在抵制建议使用真实数据库和查询语言的诱惑,因为我认为这是一个学习练习。这是一次学习练习,对吧? :)

#3


I agree with S.Lott that you would be happier with a real DB. SQLite is really light and fast, almost no drawbacks on using it.

我同意S.Lott的说法,你会对真正的数据库更满意。 SQLite非常简单快速,使用它几乎没有任何缺点。

if you really need to expose a simpler, yet complete, query language to the user, check JSONPath or JSONQuery (the second is a superset of the first). The exact syntax is (obviously) thought for JavaScript; but it should give you some ideas.

如果你真的需要向用户公开一种更简单但更完整的查询语言,请检查JSONPath或JSONQuery(第二个是第一个的超集)。精确的语法(显然)是为JavaScript考虑的;但它应该给你一些想法。

also, try to check similarities and differences with XPath and XQuery. it helps to see what is useful and what's culture-specific.

另外,尝试检查与XPath和XQuery的相似之处和不同之处。它有助于了解什么是有用的,什么是文化特定的。

#1


You'll be a lot happier with a proper database.

使用合适的数据库会让您更开心。

You have SQLite with your Python distribution. Simply define tables instead of dictionaries and use SQL.

你的Python发行版有SQLite。只需定义表而不是字典并使用SQL。

If you need more power and sophistication, you can add SQLAlchemy (or SQLObject) and you won't struggle with these problems.

如果您需要更多的功能和复杂性,可以添加SQLAlchemy(或SQLObject),您将不会遇到这些问题。

#2


It really depends on how you've implemented this "simple database based on dictionaries" as to why straight negation isn't working - you'll have to give us some more clues.

这实际上取决于你如何实现这个“基于字典的简单数据库”,为什么直接否定不起作用 - 你必须给我们一些更多的线索。

There's no reason why a straightforward negation match shouldn't work in Python, e.g.:

没有理由为什么直接的否定匹配不适用于Python,例如:

components = [
    { 'name': 'resistor', 'footprint': '0402', },
    { 'name': 'LED', 'footprint': '0100', },
    { 'name': 'speaker', 'footprint': '2000', },
]

[comp for comp in components if comp['footprint'] != '0402']
# [{'footprint': '0100', 'name': 'LED'}, {'footprint': '2000', 'name': 'speaker'}]

At a stretch, this is a simple database based on dictionaries - the specific capabilities are really going to depend on your actual implementation.

在一段时间内,这是一个基于字典的简单数据库 - 具体功能实际上取决于您的实际实现。

I'm resisting the temptation to suggest use of a real database and query language as I assume this is a learning exercise. It is a learning exercise, right? :)

我正在抵制建议使用真实数据库和查询语言的诱惑,因为我认为这是一个学习练习。这是一次学习练习,对吧? :)

#3


I agree with S.Lott that you would be happier with a real DB. SQLite is really light and fast, almost no drawbacks on using it.

我同意S.Lott的说法,你会对真正的数据库更满意。 SQLite非常简单快速,使用它几乎没有任何缺点。

if you really need to expose a simpler, yet complete, query language to the user, check JSONPath or JSONQuery (the second is a superset of the first). The exact syntax is (obviously) thought for JavaScript; but it should give you some ideas.

如果你真的需要向用户公开一种更简单但更完整的查询语言,请检查JSONPath或JSONQuery(第二个是第一个的超集)。精确的语法(显然)是为JavaScript考虑的;但它应该给你一些想法。

also, try to check similarities and differences with XPath and XQuery. it helps to see what is useful and what's culture-specific.

另外,尝试检查与XPath和XQuery的相似之处和不同之处。它有助于了解什么是有用的,什么是文化特定的。