提供分层工厂

Baluster的Python项目详细描述


https://travis-ci.org/palankai/baluster.svg?branch=master

这个包裹是干什么的

这个包提供了一种简单的方法来构建应用程序的后台结构。 可以将构建复合根用作资源工厂, 建造一个测试用的夹具工厂。

功能

  • Lazy initialisation
  • Simple composition and dependency handling

示例-复合根

frombalusterimportBaluster,placeholdersimportpsycopg2classApplicationRoot(Baluster):@placeholders.factorydefdb(self,root):# Will be called at the first use# Will be cached so won't be called againreturnpsycopg2.connect("dbname=test user=postgres")@db.closedef_close_db(self,root,db):db.close()@placeholders.factorydefcr(self,root):returnself.db.cursor()@cr.closedef_close_cr(self,root,cr):cr.close()defmain():approot=ApplicationRoot()withapproot:approot.cr.execute('SELECT * FROM user')# at this point the connection and the cursor has already been closed

示例-异步复合根

frombalusterimportBaluster,placeholdersclassAsyncApplicationRoot(Baluster):@placeholders.factoryasyncdefresource(self,root):# Will be called at the first use# Will be cached so won't be called againreturnawaitsome_aync_resource()@db.closeasyncdef_close_resource(self,root,resource):awaitresource.close()defmain():approot=AsyncApplicationRoot()asyncwithapproot:conn=awaitapproot.resourceawaitconn.operation(...)# at this point the resource has already been closed

示例-用于测试的夹具工厂

frombalusterimportBaluster,placeholdersimportpsycopg2classFixtures(Baluster):@placeholders.factorydefcr(self,root):conn=psycopg2.connect("dbname=test user=postgres")returnconn.cursor()classusers(Baluster):@placeholders.factorydefuser(self,root):root.cr.execute('SELECT * FROM user WHERE id=1')returnUser(root.cr.fetchone())@placeholders.factorydefcustomer(self,root):root.cr.execute('SELECT * FROM customer WHERE id=1')returnCustomer(root.cr.fetchone())classorders(Baluster):@placeholders.factorydefamount(self,root):return100@placeholders.factorydefquantity(self,root):return1@placeholders.factorydeforder(self,root):customer=root.users.customercreated_by=root.users.useramount=self.amount# Fictive order object...returnOrder(customer=customer,created_by=created_by,amount=amount,quantity=quantity)@placeholders.factorydefshipped_order(self,root):order=self.orderorder.mark_shipped()returnorderdeftest_order():# Demonstrate a few use fictive usecase# Creating order with defaultsf=Fixtures()assertf.order.calculated_total_value==100assertf.order.shipping_address==f.users.customer.address# Create new fixtures, but keep some cached dataf2=f.copy('cr','users')# Set some valuef2.order.amount=50f2.order.quantity=3assertf2.order.calculated_total_value==150# Manage different stage of object lifef3=f.copy('cr','users')order=f3.shipped_orderwithpytest.raises(OrderException):order.cancel()# as it is shipped

安装

python目标:>;=3.6

$ pip install baluster

依赖关系

这个包是独立的,只使用python标准库。

开发

pip install -r requirements-dev.txt

这将以开发模式安装包(setup.py development) 以及测试包。 我想达到近100%的测试覆盖率。

测试

pytest

贡献

我真的很欢迎任何评论! 如果你分叉我的代码并创建pull请求,我会很高兴的。 对于批准的拉取请求,flake8必须与所有测试一样通过。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
AmazonS3查找从S3forJava下载的压缩文件的MIME类型   java如何使用Selenium在<span>中查找具有特定文本的元素   python如何使用OpenIEDemo生成自定义三元组。由stanfordnlp提供的java   java遇到“方法不适用”编译错误   java如何使用Mockito在Looper中运行的验证代码。getMainLooper?   类Java目录错误   java在已知其他泛型信息时使用原始类型   java connect()和disconnect()在哪里实现?   java使用PDF Box库拆分PDF,生成的PDF几乎与源PDF文件大小相同   java PowerMockito返回错误的对象   java如何找到TIBCO集合消息的字节编码?   java Basic音乐播放器下一步和上一步按钮   添加模块描述符时,java没有名为“entityManagerFactory”的bean可用   java为什么我的代码不是线程安全的?   eclipse java。引用项目中的类的lang.NoClassDefFoundError