Python sqlalchemy.ext.orderinglist.ordering_list() Examples

The following are 18 code examples of sqlalchemy.ext.orderinglist.ordering_list(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.ext.orderinglist , or try the search function .
Example #1
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 6 votes vote down vote up
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'))
        ) 
Example #2
Source File: __init__.py    From pyA2L with GNU General Public License v2.0 6 votes vote down vote up
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'))
        ) 
Example #3
Source File: service_instance.py    From incubator-ariatosca with Apache License 2.0 6 votes vote down vote up
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 
Example #4
Source File: orderinglist.py    From sqlalchemy with MIT License 5 votes vote down vote up
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 
Example #5
Source File: orderinglist.py    From android_universal with MIT License 5 votes vote down vote up
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 
Example #6
Source File: orderinglist.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #7
Source File: orderinglist.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #8
Source File: test_orderinglist.py    From sqlalchemy with MIT License 5 votes vote down vote up
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") 
Example #9
Source File: orderinglist.py    From jbox with MIT License 5 votes vote down vote up
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 
Example #10
Source File: orderinglist.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #11
Source File: service_instance.py    From incubator-ariatosca with Apache License 2.0 5 votes vote down vote up
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)
            )
        ) 
Example #12
Source File: orderinglist.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #13
Source File: orderinglist.py    From planespotter with MIT License 5 votes vote down vote up
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 
Example #14
Source File: orderinglist.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #15
Source File: test_orderinglist.py    From sqlalchemy with MIT License 4 votes vote down vote up
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) 
Example #16
Source File: test_orderinglist.py    From sqlalchemy with MIT License 4 votes vote down vote up
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) 
Example #17
Source File: test_orderinglist.py    From sqlalchemy with MIT License 4 votes vote down vote up
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) 
Example #18
Source File: test_orderinglist.py    From sqlalchemy with MIT License 4 votes vote down vote up
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)