使用rethinkb和python处理空结果的最佳技巧是什么.我试试这个,但捕捉异常并不令人满意.
@staticmethod
def get_by_mail(mail):
try:
return User(
r.table('users').filter({"mail": mail}).limit(1).nth(0).run()
)
except RqlRuntimeError:
return None
Run Code Online (Sandbox Code Playgroud)
如果有人尝试过其他技术,我很感兴趣.谢谢你的帮助.
寻找一种解决方案来检测值变化:class_instance.list.append(value)。我写了一个小例子来说明我的问题。
class Foo(object):
def __setattr__(self, key, value):
print('set -> ', key, value)
self.__dict__[key] = value
if __name__ == '__main__':
f = Foo()
#set/change detected
f.bar = ['foo']
# change not detected
f.bar.append('bar')
#change detected
f.bar = ['foo', 'bar']
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助。
RethinkDB是一个非常方便的NoSQL数据库引擎.我在寻找插入Python日期时间对象的最佳方法.RethinkDB支持UTC时间戳,因此我找到了一种以正确的格式转换我的datetime对象的解决方案.
我使用这个litle函数转换我的datetime对象,在某些思路中RethinkDB理解:
import calendar
from datetime import datetime
import rethinkdb as r
def datetime_to_epoch_time(dt):
timestamp = calendar.timegm(dt.utctimetuple())
return r.epoch_time(timestamp)
title = u'foobar'
published_at = '2014-03-17 14:00'
# firts I convert 2014-03-17 14:00 to datetime
dt = datetime.strptime(published_at, '%Y-%m-%d %H:%M')
# then I store the result
r.table('stories').insert({
'title': title,
'published_at': datetime_to_epoch_time(dt),
}).run()
Run Code Online (Sandbox Code Playgroud)
我目前的时区是CET(GMT + 2小时)这是一个很好的解决方案,用于在rethinkdb中存储我的日期或存在更好的解决方案吗?
谢谢你的帮助
在Spacy 2.x中,我使用匹配器在文本语料库中查找特定的标记。每个规则都有一个ID('class-1_0'例如)。在解析期间,我使用回调函数on_match来处理每个匹配项。是否有解决方案来检索用于直接在回调中查找匹配项的规则。
这是我的示例代码。
txt = ("Aujourd'hui, je vais me faire une tartine au beurre "
"de cacahuète, c'est un pilier de ma nourriture "
"quotidienne.")
nlp = spacy.load('fr')
def on_match(matcher, doc, id, matches):
span = doc[matches[id][1]:matches[id][2]]
print(span)
# find a way to get the corresponding rule without fuzz
matcher = Matcher(nlp.vocab)
matcher.add('class-1_0', on_match, [{'LEMMA': 'pilier'}])
matcher.add('class-1_1', on_match, [{'LEMMA': 'beurre'}, {'LEMMA': 'de'}, {'LEMMA': 'cacahuète'}])
doc = nlp(txt)
matches = matcher(doc)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,matches返回:
[(12071893341338447867, 9, 12), (4566231695725171773, …Run Code Online (Sandbox Code Playgroud)