我试图理解这两种模拟方法的方法之间的区别.有人可以帮助区分它们吗?对于此示例,我使用passlib库.
from passlib.context import CryptContext
from unittest import mock
with mock.patch.object(CryptContext, 'verify', return_value=True) as foo1:
mycc = CryptContext(schemes='bcrypt_sha256')
mypass = mycc.encrypt('test')
assert mycc.verify('tesssst', mypass)
with mock.patch('passlib.context.CryptContext.verify', return_value=True) as foo2:
mycc = CryptContext(schemes='bcrypt_sha256')
mypass = mycc.encrypt('test')
assert mycc.verify('tesssst', mypass)
Run Code Online (Sandbox Code Playgroud) 注意:在RhodiumToad的#postgresql帮助下,我已经找到了一个解决方案,我将其作为答案发布.如果有人可以改进这一点,请加入!
我无法使先前的递归查询解决方案适应包含多个"根"(无祖先)节点的以下有向非循环图.我正在尝试编写一个查询,其输出通常称为闭包表:一个多对多表,它存储从每个节点到每个后代及其自身的每条路径:
1 2 11 8 4 5 7
\/ | | \ | /
3 | \ 6
\ | \ /
9 | 10
\/ /
12 13
\ /
14
CREATE TABLE node (
id SERIAL PRIMARY KEY,
node_name VARCHAR(50) NOT NULL
);
CREATE TABLE node_relations (
id SERIAL PRIMARY KEY,
ancestor_node_id INT REFERENCES node(id),
descendant_node_id INT REFERENCES node(id)
);
INSERT into node (node_name)
SELECT 'node ' || g FROM generate_series(1,14) g;
INSERT INTO node_relations(ancestor_node_id, …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的例子来说明PostgreSQL中使用递归查询的传递闭包.
但是,我的递归查询有些问题.我不熟悉语法,所以这个请求可能完全是我的noobish,为此我提前道歉.如果运行查询,您将看到节点1在路径结果中重复自身.有人可以帮我弄清楚如何调整SQL吗?
/* 1
/ \
2 3
/ \ /
4 5 6
/
7
/ \
8 9
*/
create table account(
acct_id INT,
parent_id INT REFERENCES account(acct_id),
acct_name VARCHAR(100),
PRIMARY KEY(acct_id)
);
insert into account (acct_id, parent_id, acct_name) values (1,1,'account 1');
insert into account (acct_id, parent_id, acct_name) values (2,1,'account 2');
insert into account (acct_id, parent_id, acct_name) values (3,1,'account 3');
insert into account (acct_id, parent_id, acct_name) values (4,2,'account 4');
insert into account (acct_id, parent_id, acct_name) values (5,2,'account 5'); …Run Code Online (Sandbox Code Playgroud) sql postgresql recursive-query common-table-expression transitive-closure-table
我希望熟悉Python的编译/运行时程序的人可以解释一下我关于Python如何编译装饰器函数的问题.
在我的示例代码中,我在定义logtofile闭包之前在"writeit"装饰器中包含了一个测试print语句.如果你运行我提供的整个代码,那么在使用writeit之前,为Customer类中定义的每个@writeit装饰器调用writeit中的"testing"print语句.
为什么在编译时调用logtofile?有人可以解释一下这种行为吗?
def writeit(func):
print('testing')
def logtofile(customer, *arg, **kwargs):
print('logtofile')
result = func(customer, *arg, **kwargs)
with open('dictlog.txt','w') as myfile:
myfile.write(func.__name__)
return result
return logtofile
class Customer(object):
def __init__(self,firstname,lastname,address,city,state,zipcode):
self._custinfo = dict(firstname=firstname,lastname=lastname,address=address,city=city,state=state,zipcode=zipcode)
@writeit
def setFirstName(self,firstname):
print('setFirstName')
self._custinfo['firstname']=firstname
@writeit
def setLastName(self,lastname):
print('setLastName')
self._custinfo['lastname']=lastname
@writeit
def setAddress(self,address):
print('setAddress')
self._custinfo['address']=address
def main():
cust1 = Customer('Joe','Shmoe','123 Washington','Washington DC','DC','12345')
cust1.setFirstName('Joseph')
cust1.setLastName('Shmoestein')
if(__name__ == '__main__'): main()
Run Code Online (Sandbox Code Playgroud) 我正在尝试理解Postgresql 9.3中的array_agg函数.我为每个可能有兴趣参与的人提供了一个有趣的例子.
20世纪80年代的任何一部美国电影迷都可能熟悉一起出现在很多热门影片中的"小伙子".在wikipedia上使用关于brat pack影片的信息,我创建了表格,当它们连接在一起时,可以告诉我们谁在彼此工作 - 如果我们有正确的查询!
/*
See: http://en.wikipedia.org/wiki/Brat_Pack_(actors)
*/
CREATE TABLE actor(
id SERIAL PRIMARY KEY,
name VARCHAR(50)
);
insert into actor(name) values ('Emilio Estevez'),('Anthony Michael Hall'),('Rob Lowe'),('Andrew McCarthy'),('Demi Moore'),('Judd Nelson'),('Molly Ringwald'),('Ally Sheedy')
CREATE TABLE movie(
id SERIAL PRIMARY KEY,
title VARCHAR(200)
);
insert into movie(title) values ('The Outsiders'),('Class'),('Sixteen Candles'),('Oxford Blues'),('The Breakfast Club'),('St. Elmos Fire'),
('Pretty in Pink'),('Blue City'),('About Last Night'),('Wisdom'), ('Fresh Horses'),('Betsys Wedding'),('Hail Caesar');
CREATE TABLE movie_brats(
id SERIAL PRIMARY KEY,
movie_id INT REFERENCES movie(id),
actor_id INT REFERENCES …Run Code Online (Sandbox Code Playgroud) 我正在使用py.test和模拟。我无法模拟常量。我的测试修改了分配给常量的dict值。这应该在我的测试中引发一个Exception,但到目前为止还没有。我不确定问题出在什么地方,不胜感激可以帮助您查明问题所在。谢谢。
the_module.py
MY_DICT = {'one': 1, 'two': 2, 'three': 3}
class OneMissingException(Exception):
pass
class Test1(object):
def __init__(self):
self.mydict = MY_DICT
@property
def mydict(self):
return self._mydict
@mydict.setter
def mydict(self, mydict):
if 'one' not in mydict:
raise OneMissingException
self._mydict = mydict
Run Code Online (Sandbox Code Playgroud)
test_themodule.py
import pytest
from unittest import mock
from the_module import Test1, OneMissingException
@pytest.fixture(scope='function')
def my_dict():
return {'one': 1, 'two': 2, 'three': 3}
def test_verify_test1_exception(my_dict):
my_dict.pop('one') # comment this out and test still passes
with mock.patch("the_module.MY_DICT") as mydict:
mydict.return_value.return_value = my_dict
with …Run Code Online (Sandbox Code Playgroud) postgresql ×3
python ×3
mocking ×2
sql ×2
class ×1
decorator ×1
many-to-many ×1
pytest ×1
testing ×1
unit-testing ×1