编辑 接受的答案适用于满足严格部分有序集的要求的集合,以便可以构造有向非循环图:
not a < a
:列表不包含像['a','a']
if a < b and b < c then a < c
:列表不包含像['a','b'],['b','c'],['c','a']
if a < b then not b < a
:列表不包含像['a','b'],['b','a']
获取此列表列表:
[['b', 'c'], ['a', 'c'], ['b', 'a'], ['a', 'c', 'd'], ]
并将其展平为单个列表,根据值的邻居进行排序:
子列表之间的整体排序是一致的,这意味着不会有像这样的子列表:['b','c'],['c','b']
.所以结果应该是:['b', 'a', 'c', 'd']
经过一段很长的时间,我想出了这个丑陋的混乱:
def get_order(_list):
order = _list[0]
for sublist in _list[1:]:
if not sublist:
continue
if …
Run Code Online (Sandbox Code Playgroud) 关于pyreverse的文档很少,谷歌向我提供的一半页面,重定向到一些异国情调的页面,如果你的老板在你的浏览器历史记录中找到它们,则需要一些解释。
由于pyreverse的工作方式,您必须从根项目/django应用程序目录中使用它(例如,在其中运行它时,MyApp/docs/
可能找不到您感兴趣的类的每个祖先)。这意味着生成的图表将“堵塞”您的项目文件夹,除非您手动将它们移动到其他地方。
有没有办法为pyreverse指定输出文件夹?
有没有装饰类属性的首选方法?如我所见,您可以用另一个属性来装饰属性本身,也可以装饰基础方法,然后@property
在其上应用。
两种方法都需要考虑吗?
def decorate_property(prop):
@property
def inner(instance):
return prop.__get__(instance) + 1
return inner
def decorate_func(func):
def inner(instance):
return func(instance) +1
return inner
class A:
x = 1
@decorate_property
@property
def f(self):
return self.x
@property
@decorate_func
def g(self):
return self.x
a = A()
print(a.f) # 2
print(a.g) # 2
Run Code Online (Sandbox Code Playgroud) ATTR__SUBATTR
我认为您应该能够直接传递已经存在的实例,而不是以 的形式将数据传递到工厂的RelatedFactory 。除非我错过了一些非常明显的东西,否则这似乎不起作用。看一看:
class Owner(models.Model):
name = models.CharField()
class Item(models.Model):
name = models.CharField()
owner = models.ForeignKey(Owner, null = True, related_name = 'items')
class ItemFactory(factory.django.DjangoModelFactory):
class Meta:
model = Item
class OwnerFactory(factory.django.DjangoModelFactory):
class Meta:
model = Owner
items = factory.RelatedFactory(ItemFactory, 'owner')
item = Item.objects.create(name='Foo')
alice = OwnerFactory(name='Alice', items__name='Bar')
alice.items.all()
<QuerySet [<Item: Bar>]>
bob = OwnerFactory(name='Bob', items=item) # or items = [item] doesn't matter
bob.items.all()
<QuerySet []>
Run Code Online (Sandbox Code Playgroud)
一直致力于让我的工厂变得漂亮、干燥,但遇到了这个障碍。我自己编写了一个RelatedFactory
允许同时处理多个值的改编版本,如果您在过程中创建新对象,则效果很好 - 但如果您使用已经存在的对象,则效果不佳。
有效的示例:OwnerFactory(items__name=['Foo','Bar'])=> Foo and Bar in owner.items
.
不起作用的示例: …