本文整理汇总了Python中sqlalchemy.ext.orderinglist.ordering_list方法的典型用法代码示例。如果您正苦于以下问题:Python orderinglist.ordering_list方法的具体用法?Python orderinglist.ordering_list怎么用?Python orderinglist.ordering_list使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.orderinglist
的用法示例。
在下文中一共展示了orderinglist.ordering_list方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: annotation_association
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def annotation_association(cls):
name = cls.__name__
discriminator = name.lower()
assoc_cls = type(
"%sAnnotationAssociation" % name, (AnnotationAssociation,),
dict(
__tablename__ = None,
__mapper_args__ = {"polymorphic_identity": discriminator},
),
)
cls.annotation = association_proxy(
"annotation_association",
"annotation",
creator = lambda annotation: assoc_cls(annotation = annotation),
)
return relationship(
assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position'))
)
示例2: if_data_association
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def if_data_association(cls):
name = cls.__name__
discriminator = name.lower()
assoc_cls = type(
"%sIfDataAssociation" % name, (IfDataAssociation,),
dict(
__tablename__ = None,
__mapper_args__ = {"polymorphic_identity": discriminator},
),
)
cls.if_data = association_proxy(
"if_data_association",
"if_data",
creator = lambda if_data: assoc_cls(if_data = if_data),
)
return relationship(
assoc_cls, backref = backref("parent", uselist = False, collection_class = ordering_list('position'))
)
示例3: inbound_relationships
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def inbound_relationships(cls):
"""
Relationships from other nodes.
:type: [:class:`Relationship`]
"""
return relationship.one_to_many(
cls, 'relationship', other_fk='target_node_fk', back_populates='target_node',
rel_kwargs=dict(
order_by='Relationship.target_position',
collection_class=ordering_list('target_position', count_from=0)
)
)
# endregion
# region many_to_one relationships
示例4: ordering_list
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def ordering_list(attr, count_from=None, **kw):
"""Prepares an :class:`OrderingList` factory for use in mapper definitions.
Returns an object suitable for use as an argument to a Mapper
relationship's ``collection_class`` option. e.g.::
from sqlalchemy.ext.orderinglist import ordering_list
class Slide(Base):
__tablename__ = 'slide'
id = Column(Integer, primary_key=True)
name = Column(String)
bullets = relationship("Bullet", order_by="Bullet.position",
collection_class=ordering_list('position'))
:param attr:
Name of the mapped attribute to use for storage and retrieval of
ordering information
:param count_from:
Set up an integer-based ordering, starting at ``count_from``. For
example, ``ordering_list('pos', count_from=1)`` would create a 1-based
list in SQL, storing the value in the 'pos' column. Ignored if
``ordering_func`` is supplied.
Additional arguments are passed to the :class:`.OrderingList` constructor.
"""
kw = _unsugar_count_from(count_from=count_from, **kw)
return lambda: OrderingList(attr, **kw)
# Ordering utility functions
示例5: outbound_relationships
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def outbound_relationships(cls):
"""
Relationships to other nodes.
:type: [:class:`Relationship`]
"""
return relationship.one_to_many(
cls, 'relationship', other_fk='source_node_fk', back_populates='source_node',
rel_kwargs=dict(
order_by='Relationship.source_position',
collection_class=ordering_list('source_position', count_from=0)
)
)
示例6: test_replace
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def test_replace(self):
self._setup(ordering_list("position"))
s1 = Slide("Slide #1")
s1.bullets = [Bullet("1"), Bullet("2"), Bullet("3")]
self.assert_(len(s1.bullets) == 3)
self.assert_(s1.bullets[2].position == 2)
session = create_session()
session.add(s1)
session.flush()
new_bullet = Bullet("new 2")
self.assert_(new_bullet.position is None)
# mark existing bullet as db-deleted before replacement.
# session.delete(s1.bullets[1])
s1.bullets[1] = new_bullet
self.assert_(new_bullet.position == 1)
self.assert_(len(s1.bullets) == 3)
id_ = s1.id
session.flush()
session.expunge_all()
srt = session.query(Slide).get(id_)
self.assert_(srt.bullets)
self.assert_(len(srt.bullets) == 3)
self.assert_(srt.bullets[1].text == "new 2")
self.assert_(srt.bullets[2].text == "3")
示例7: test_append_no_reorder
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def test_append_no_reorder(self):
self._setup(
ordering_list("position", count_from=1, reorder_on_append=False)
)
s1 = Slide("Slide #1")
self.assert_(not s1.bullets)
self.assert_(len(s1.bullets) == 0)
s1.bullets.append(Bullet("s1/b1"))
self.assert_(s1.bullets)
self.assert_(len(s1.bullets) == 1)
self.assert_(s1.bullets[0].position == 1)
s1.bullets.append(Bullet("s1/b2"))
self.assert_(len(s1.bullets) == 2)
self.assert_(s1.bullets[0].position == 1)
self.assert_(s1.bullets[1].position == 2)
bul = Bullet("s1/b100")
bul.position = 100
s1.bullets.append(bul)
self.assert_(s1.bullets[0].position == 1)
self.assert_(s1.bullets[1].position == 2)
self.assert_(s1.bullets[2].position == 100)
s1.bullets.append(Bullet("s1/b4"))
self.assert_(s1.bullets[0].position == 1)
self.assert_(s1.bullets[1].position == 2)
self.assert_(s1.bullets[2].position == 100)
self.assert_(s1.bullets[3].position == 4)
s1.bullets._reorder()
self.assert_(s1.bullets[0].position == 1)
self.assert_(s1.bullets[1].position == 2)
self.assert_(s1.bullets[2].position == 3)
self.assert_(s1.bullets[3].position == 4)
session = create_session()
session.add(s1)
session.flush()
id_ = s1.id
session.expunge_all()
del s1
srt = session.query(Slide).get(id_)
self.assert_(srt.bullets)
self.assert_(len(srt.bullets) == 4)
titles = ["s1/b1", "s1/b2", "s1/b100", "s1/b4"]
found = [b.text for b in srt.bullets]
self.assert_(titles == found)
示例8: test_insert
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def test_insert(self):
self._setup(ordering_list("position"))
s1 = Slide("Slide #1")
s1.bullets.append(Bullet("1"))
s1.bullets.append(Bullet("2"))
s1.bullets.append(Bullet("3"))
s1.bullets.append(Bullet("4"))
self.assert_(s1.bullets[0].position == 0)
self.assert_(s1.bullets[1].position == 1)
self.assert_(s1.bullets[2].position == 2)
self.assert_(s1.bullets[3].position == 3)
s1.bullets.insert(2, Bullet("insert_at_2"))
self.assert_(s1.bullets[0].position == 0)
self.assert_(s1.bullets[1].position == 1)
self.assert_(s1.bullets[2].position == 2)
self.assert_(s1.bullets[3].position == 3)
self.assert_(s1.bullets[4].position == 4)
self.assert_(s1.bullets[1].text == "2")
self.assert_(s1.bullets[2].text == "insert_at_2")
self.assert_(s1.bullets[3].text == "3")
s1.bullets.insert(999, Bullet("999"))
self.assert_(len(s1.bullets) == 6)
self.assert_(s1.bullets[5].position == 5)
session = create_session()
session.add(s1)
session.flush()
id_ = s1.id
session.expunge_all()
del s1
srt = session.query(Slide).get(id_)
self.assert_(srt.bullets)
self.assert_(len(srt.bullets) == 6)
texts = ["1", "2", "insert_at_2", "3", "4", "999"]
found = [b.text for b in srt.bullets]
self.assert_(texts == found)
示例9: test_slice
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def test_slice(self):
self._setup(ordering_list("position"))
b = [
Bullet("1"),
Bullet("2"),
Bullet("3"),
Bullet("4"),
Bullet("5"),
Bullet("6"),
]
s1 = Slide("Slide #1")
# 1, 2, 3
s1.bullets[0:3] = b[0:3]
for i in 0, 1, 2:
self.assert_(s1.bullets[i].position == i)
self.assert_(s1.bullets[i] == b[i])
# 1, 4, 5, 6, 3
s1.bullets[1:2] = b[3:6]
for li, bi in (0, 0), (1, 3), (2, 4), (3, 5), (4, 2):
self.assert_(s1.bullets[li].position == li)
self.assert_(s1.bullets[li] == b[bi])
# 1, 6, 3
del s1.bullets[1:3]
for li, bi in (0, 0), (1, 5), (2, 2):
self.assert_(s1.bullets[li].position == li)
self.assert_(s1.bullets[li] == b[bi])
session = create_session()
session.add(s1)
session.flush()
id_ = s1.id
session.expunge_all()
del s1
srt = session.query(Slide).get(id_)
self.assert_(srt.bullets)
self.assert_(len(srt.bullets) == 3)
texts = ["1", "6", "3"]
for i, text in enumerate(texts):
self.assert_(srt.bullets[i].position == i)
self.assert_(srt.bullets[i].text == text)
示例10: test_funky_ordering
# 需要导入模块: from sqlalchemy.ext import orderinglist [as 别名]
# 或者: from sqlalchemy.ext.orderinglist import ordering_list [as 别名]
def test_funky_ordering(self):
class Pos(object):
def __init__(self):
self.position = None
step_factory = ordering_list(
"position", ordering_func=step_numbering(2)
)
stepped = step_factory()
stepped.append(Pos())
stepped.append(Pos())
stepped.append(Pos())
stepped.append(Pos())
for li, pos in (0, 0), (1, 2), (2, 4), (3, 6):
self.assert_(stepped[li].position == pos)
fib_factory = ordering_list(
"position", ordering_func=fibonacci_numbering("position")
)
fibbed = fib_factory()
fibbed.append(Pos())
fibbed.append(Pos())
fibbed.append(Pos())
fibbed.append(Pos())
fibbed.append(Pos())
for li, pos in (0, 1), (1, 2), (2, 3), (3, 5), (4, 8):
self.assert_(fibbed[li].position == pos)
fibbed.insert(2, Pos())
fibbed.insert(4, Pos())
fibbed.insert(6, Pos())
for li, pos in (
(0, 1),
(1, 2),
(2, 3),
(3, 5),
(4, 8),
(5, 13),
(6, 21),
(7, 34),
):
self.assert_(fibbed[li].position == pos)
alpha_factory = ordering_list("position", ordering_func=alpha_ordering)
alpha = alpha_factory()
alpha.append(Pos())
alpha.append(Pos())
alpha.append(Pos())
alpha.insert(1, Pos())
for li, pos in (0, "A"), (1, "B"), (2, "C"), (3, "D"):
self.assert_(alpha[li].position == pos)