获取所有的python-rom对象到列表中 [英] Getting all python-rom objects into a list

查看:257
本文介绍了获取所有的python-rom对象到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用烧瓶和redis。我决定尝试使用redis orm( http://pythonhosted.org/rom/ )来管理一些稍微复杂的数据结构。我有一个对象列表,可以这样说:

  urls = ['www.google.com','www.example。 com','www.python.org'] 

我也有rom模型:

  class Stored_url(rom.Model):
url = rom.String(required = True,unique = True,suffix = True )
salt = rom.String()
hash = rom.String()
created_at = rom.Float(default = time.time)

这似乎是在我的开发设置上工作。我已经将大约25个Stored_url对象加载到了REDIS中(在cmd行确认)。我试图想出一种将所有Stored_url类型的对象变成python列表的方法。

 >>> test = Mymodels.Stored_url 
>>> type(test)
Out [35]:rom._ModelMetaclass
>>> h = test.query.filter(url ='。')。all()
>>> h.count()
Traceback(最近一次调用最后一个):
文件C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\第一行中的第一行interactiveshell.py,第3035行,run_code
exec(code_obj,self.user_global_ns,self.user_ns)
文件< ipython-input-37-43f0dc233d70>模块>
h.count()
TypeError:count()只需要一个参数(0给定)

我以为h会有一个对象列表。我究竟做错了什么? (我是过滤。,因为我想到的网址会有)

解决方案

已经提供了解释为什么你得到你得到的结果。

第一个问题是您的查询 test.query.filter(url =' 。')。all()将返回一个空的列表。这将仅返回一个空列表,因为您没有一个有效的索引与您指定的过滤器一起使用。这个列有2个索引 - 一个唯一的索引(用于查找精确字符串的url)和一个后缀索引(用于查找以某个字符串结尾的url) - 但都不能提供按照在关系世界中,一个喜欢的查询。前缀索引(用 prefix = True 创建)可以让你使用 test.query.like(url ='*。')

为了防止索引/查询相关的问题这个,当用户试图通过不存在的索引过滤他们的数据时,我添加了QueryError异常。我将在今晚晚些时候发布0.31.4这些更改。



第二个错误是异常的原因是您调用 .count()不带参数。在你的 h.count()调用中, type(h)== list 和Python列表对象需要一个参数来计算与列表中提供的参数相等的值。如果您跳过原始查询的 .all()部分,则会返回查询对象。那个查询对象有一个 .count()方法,并且会返回一个匹配结果的计数。

[1 ]在ROM中,并不是所有的like查询都很慢,但是那些速度很快的请求需要使用非通配符前缀才能将数据范围限制在扫描/过滤。


I am working with flask and redis. I've decided to try the rom redis orm (http://pythonhosted.org/rom/) to manage some mildly complex data structures. I have a list of objects, lets say:

urls = ['www.google.com', 'www.example.com', 'www.python.org']

I also have the rom model:

class Stored_url(rom.Model):
    url = rom.String(required=True, unique=True, suffix=True)
    salt = rom.String()
    hash = rom.String()
    created_at = rom.Float(default=time.time)

This appears to be working on my dev setup. I have loaded about 25 'Stored_url' objects into REDIS (confirmed at cmd line). I am trying to come up with a way of getting all the objects of type Stored_url into a python list.

>>> test = Mymodels.Stored_url
>>> type(test)
Out[35]: rom._ModelMetaclass
>>> h =test.query.filter(url ='.').all()
>>> h.count()
Traceback (most recent call last):
  File "C:\envs\virtalenvs\flaskenv\lib\site-packages\IPython\core\interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-37-43f0dc233d70>", line 1, in <module>
    h.count()
TypeError: count() takes exactly one argument (0 given)

I thought h would have a list of objects. What am I doing wrong? (I was filtering by "." because i figured on urls would have it)

解决方案

There are two problems with the code you have provided which explain why you get the results that you get.

The first problem is that your query test.query.filter(url ='.').all() will return an empty list. This will return an empty list simply because you don't have a valid index to be used with the filter you have specified. You do have 2 indexes for that column - a unique index (useful for looking up urls by exact string) and a suffix index (useful for finding urls that end with a certain string) - but neither offer the ability to filter by what would be in the relational world a 'like' query. A prefix index (created with prefix=True) would let you use test.query.like(url='*.'), but that would be very slow (it does an index scan instead of direct lookup[1]).

To help prevent index/query-related issues like this, I have added QueryError exceptions when users attempt to filter their data by indexes that don't exist. I'll be releasing 0.31.4 a bit later tonight with those changes.

The second error you have, which is the cause of the exception, is that you call .count() without an argument. At the point of your h.count() call, type(h) == list, and Python list objects require an argument to count values equal to the provided argument in the list. If you skipped the .all() portion of your original query, you would get a query object back. That query object has a .count() method, and would return a count of matched results.

[1] Not all 'like' queries in rom are slow, but those that are fast require non-wildcard prefixes to limit data ranges to scan/filter.

这篇关于获取所有的python-rom对象到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆