普通旧python对象的工厂。

calve_machine的Python项目详细描述


普通旧python对象的工厂。

基本用法

一个简单的例子:

>>> from should_dsl import should, should_not
>>> from calve_machine import inseminate, pregnant

>>> @inseminate
... def programmer(p):
...     p.name = 'Sheldon Cooper, Ph.D.'
...     p.age = 29
...     p.languages = ['eiffel', 'io', 'erlang']

>>> prog = pregnant.calve('programmer')
>>> prog.name |should| equal_to('Sheldon Cooper, Ph.D.')
>>> prog.age |should| be(29)
>>> prog.languages |should| equal_to(['eiffel', 'io', 'erlang'])

继承

工厂可以从其他工厂继承:

>>> @inseminate
... def programmer(p):
...     p.name = 'Sheldon Cooper, Ph.D.'
...     p.age = 29
...     p.languages = ['eiffel', 'io', 'erlang']

>>> @inseminate(seed_from='programmer')
... def python_programmer(p):
...     p.languages = ['python']
...     p.foo = 'spam'

>>> prog = pregnant.calve('python_programmer')
>>> prog.name |should| equal_to('Sheldon Cooper, Ph.D.')
>>> prog.age |should| be(29)
>>> prog.languages |should| equal_to(['python'])
>>> prog.foo |should| equal_to('spam')

>>> @inseminate(seed_from='python_programmer')
... def zope_programmer(p):
...     p.languages = ['zcml']
...     p.age = 30

>>> prog = pregnant.calve('zope_programmer')
>>> prog.name |should| equal_to('Sheldon Cooper, Ph.D.')
>>> prog.age |should| be(30)
>>> prog.languages |should| equal_to(['zcml'])
>>> prog.foo |should| equal_to('spam')

序列

工厂可以生成序列:

>>> from calve_machine import sequence

>>> @inseminate
... def id_holder(i):
...     i.name = sequence(lambda n: "Crazy Guy %s" %n)
...     i.square_id = sequence(lambda n: n ** 2)

>>> obj = pregnant.calve('id_holder')
>>> obj.name |should| equal_to("Crazy Guy 1")
>>> obj.square_id |should| be(1)

>>> obj = pregnant.calve('id_holder')
>>> obj.name |should| equal_to("Crazy Guy 2")
>>> obj.square_id |should| be(4)

>>> obj = pregnant.calve('id_holder')
>>> obj.name |should| equal_to("Crazy Guy 3")
>>> obj.square_id |should| be(9)

模板化

对象可以使用现有对象作为模板生成。给定一个对象:

>>> class Spam(object):
...     def __init__(self, eggs):
...         self.eggs = eggs
...     def fried(self):
...         return self.eggs + ' w/ cholesterol'

>>> spam = Spam("Guava")
>>> guava_prog = pregnant.calve('programmer', template=spam)

“程序员”的一切都按预期进行:

>>> guava_prog.name |should| equal_to('Sheldon Cooper, Ph.D.')
>>> guava_prog.age |should| be(29)
>>> guava_prog.languages |should| equal_to(['eiffel', 'io', 'erlang'])

模板对象中的所有成员都存在:

>>> guava_prog.eggs |should| equal_to('Guava')
>>> guava_prog.fried() |should| equal_to('Guava w/ cholesterol')

新生对象的类与模板对象的类相同:

>>> guava_prog |should| be_instance_of(Spam)

模板对象保持不变:

>>> spam |should_not| respond_to('name')
>>> spam |should_not| respond_to('age')
>>> spam |should_not| respond_to('languages')

类作为模板

模板可以是类,而不仅仅是对象:

>>> class Person:
...     def __init__(self):
...         self.city = 'Campos dos Goytacazes/RJ/Brazil'

>>> prog = pregnant.calve('programmer', template=Person)

“程序员”的一切都按预期进行:

>>> prog.name |should| equal_to('Sheldon Cooper, Ph.D.')
>>> prog.age |should| be(29)
>>> prog.languages |should| equal_to(['eiffel', 'io', 'erlang'])

模板类中的所有成员都存在:

>>> prog.city |should| equal_to('Campos dos Goytacazes/RJ/Brazil')

新生对象的类与模板对象的类相同:

>>> prog |should| be_instance_of(Person)

如果类具有必需的init参数:

>>> class User:
...     def __init__(self, email, password):
...         self.email, self.password = email, password

可以使用init()方法定义构造函数参数:

>>> @inseminate
... def parameterized_programmer(u):
...     u.init('admin@admin.com', 's3cr3t')
...     u.name = 'Sheldon Cooper'

>>> prog = pregnant.calve('parameterized_programmer', template=User)
>>> prog.email |should| equal_to('admin@admin.com')
>>> prog.password |should| equal_to('s3cr3t')
>>> prog.name |should| equal_to('Sheldon Cooper')

对于没有构造函数的类,将忽略构造函数的参数 参数:

>>> prog = pregnant.calve('parameterized_programmer', template=Person)

关联

calve_machine支持对象关联:

>>> from calve_machine import association

>>> @inseminate
... def customer(c):
...     c.name = 'Someone'

>>> @inseminate
... def order(o):
...     o.owner = association('customer')

>>> an_order = pregnant.calve('order')
>>> an_order.owner.name |should| equal_to('Someone')

如果字段名等于种子名,则无需指定种子名:

>>> @inseminate
... def order(o):
...     o.customer = association()

>>> an_order = pregnant.calve('order')
>>> an_order.customer.name |should| equal_to('Someone')

生成之后

可以定义任意代码以在创建的对象上运行:

>>> class User(object):
...    def __init__(self):
...        self.authorized = False
...    def authorize(self):
...        self.authorized = True

>>> @inseminate
... def user(u):
...     u.email = 'admin@admin.com'
...     u.password = 'adm!npassword'
...     u.after_build(lambda user: user.authorize())

>>> user = pregnant.calve('user', template=User())
>>> user |should| be_authorized

如何安装

对于最新版本,请运行:

pip install calve_machine

您还可以直接从github安装当前的开发版本:

pip install -e git+https://github.com/rodrigomanhaes/calve_machine.git#egg=calve_machine

如何运行测试

只需运行:

make test

用于安装所有测试依赖项(Should-DSL 以及Specloud,目前)和 运行测试。小牛机器本身没有依赖关系。

想法

犊牛机器的灵感来自factory_girl

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

推荐PyPI第三方库


热门话题
java如何在sqlite数据库中保存特定列的历史记录   java如何更改/更新timeseriechart名称(JFreeChart)   java如何将整数转换为可绘制的   汇编什么解释Java的字节码   java查找已编译的类版本号   我应该什么时候在ColdFusion应用程序中使用Java?   java当一个实体的两个字段为(unique=true)时,如何处理JPA异常?   java为什么在所有其他实例都正确的情况下返回错误的布尔值?   java Hibernate每次都准备语句   java停留在平均字长上   对Java和日语字符进行编码   java如何将导致异常的方法的错误消息传递给侦听器中的onTestFailure方法   java代码没有打印结果   java为什么私有内部接口的方法必须是公共的?   休眠发生错误。有关详细信息,请参阅错误日志。JAVAlang.NullPointerException