当前位置: 首页>>代码示例>>Python>>正文


Python orm.validates方法代码示例

本文整理汇总了Python中sqlalchemy.orm.validates方法的典型用法代码示例。如果您正苦于以下问题:Python orm.validates方法的具体用法?Python orm.validates怎么用?Python orm.validates使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy.orm的用法示例。


在下文中一共展示了orm.validates方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _validate_date

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def _validate_date(*field_names):
    @validates(*field_names)
    def make_date(self, key, value):
        return datetime.datetime.strptime(value, '%Y%m%d').date()
    return make_date 
开发者ID:jarondl,项目名称:pygtfs,代码行数:7,代码来源:gtfs_entities.py

示例2: _validate_time_delta

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def _validate_time_delta(*field_names):
    @validates(*field_names)
    def time_delta(self, key, value):
        if value is None or value == "":
            return None
        (hours, minutes, seconds) = map(int, value.split(":"))
        return datetime.timedelta(hours=hours, minutes=minutes,
                                  seconds=seconds)
    return time_delta 
开发者ID:jarondl,项目名称:pygtfs,代码行数:11,代码来源:gtfs_entities.py

示例3: _validate_int_bool

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def _validate_int_bool(*field_names):
    @validates(*field_names)
    def int_bool(self, key, value):
        if value not in ("0", "1"):
            raise PygtfsValidationError("{0} must be 0 or 1, "
                                        "was {1}".format(key, value))
        return value == "1"
    return int_bool 
开发者ID:jarondl,项目名称:pygtfs,代码行数:10,代码来源:gtfs_entities.py

示例4: _validate_int_choice

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def _validate_int_choice(int_choice, *field_names):
    @validates(*field_names)
    def in_range(self, key, value):
        if value is None or value == "":
            if (None in int_choice):
                return None
            else:
                raise PygtfsValidationError("Empty value not allowed in {0}".format(key))
        else:
            int_value = int(value)
        if int_value not in int_choice:
            raise PygtfsValidationError(
                "{0} must be in range {1}, was {2}".format(key, int_choice, value))
        return int_value
    return in_range 
开发者ID:jarondl,项目名称:pygtfs,代码行数:17,代码来源:gtfs_entities.py

示例5: _validate_float_range

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def _validate_float_range(float_min, float_max, *field_names):
    @validates(*field_names)
    def in_range(self, key, value):
        float_value = float(value)
        if not (float_min <= float_value <= float_max):
            raise PygtfsValidationError(
                "{0} must be in range [{1}, {2}],"
                " was {2}".format(key, float_min, float_max, value))
        return float_value
    return in_range 
开发者ID:jarondl,项目名称:pygtfs,代码行数:12,代码来源:gtfs_entities.py

示例6: test_scalar

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def test_scalar(self):
        users = self.tables.users
        canary = Mock()

        class User(fixtures.ComparableEntity):
            @validates("name")
            def validate_name(self, key, name):
                canary(key, name)
                ne_(name, "fred")
                return name + " modified"

        mapper(User, users)
        sess = Session()
        u1 = User(name="ed")
        eq_(u1.name, "ed modified")
        assert_raises(AssertionError, setattr, u1, "name", "fred")
        eq_(u1.name, "ed modified")
        eq_(canary.mock_calls, [call("name", "ed"), call("name", "fred")])

        sess.add(u1)
        sess.commit()

        eq_(
            sess.query(User).filter_by(name="ed modified").one(),
            User(name="ed"),
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:28,代码来源:test_validators.py

示例7: test_collection

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def test_collection(self):
        users, addresses, Address = (
            self.tables.users,
            self.tables.addresses,
            self.classes.Address,
        )

        canary = Mock()

        class User(fixtures.ComparableEntity):
            @validates("addresses")
            def validate_address(self, key, ad):
                canary(key, ad)
                assert "@" in ad.email_address
                return ad

        mapper(User, users, properties={"addresses": relationship(Address)})
        mapper(Address, addresses)
        sess = Session()
        u1 = User(name="edward")
        a0 = Address(email_address="noemail")
        assert_raises(AssertionError, u1.addresses.append, a0)
        a1 = Address(id=15, email_address="foo@bar.com")
        u1.addresses.append(a1)
        eq_(canary.mock_calls, [call("addresses", a0), call("addresses", a1)])
        sess.add(u1)
        sess.commit()

        eq_(
            sess.query(User).filter_by(name="edward").one(),
            User(
                name="edward", addresses=[Address(email_address="foo@bar.com")]
            ),
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:36,代码来源:test_validators.py

示例8: test_validators_dict

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def test_validators_dict(self):
        users, addresses, Address = (
            self.tables.users,
            self.tables.addresses,
            self.classes.Address,
        )

        class User(fixtures.ComparableEntity):
            @validates("name")
            def validate_name(self, key, name):
                ne_(name, "fred")
                return name + " modified"

            @validates("addresses")
            def validate_address(self, key, ad):
                assert "@" in ad.email_address
                return ad

            def simple_function(self, key, value):
                return key, value

        u_m = mapper(
            User, users, properties={"addresses": relationship(Address)}
        )
        mapper(Address, addresses)

        eq_(
            dict((k, v[0].__name__) for k, v in list(u_m.validators.items())),
            {"name": "validate_name", "addresses": "validate_address"},
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:32,代码来源:test_validators.py

示例9: test_validator_bulk_collection_set

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def test_validator_bulk_collection_set(self):
        users, addresses, Address = (
            self.tables.users,
            self.tables.addresses,
            self.classes.Address,
        )

        class User(fixtures.ComparableEntity):
            @validates("addresses", include_removes=True)
            def validate_address(self, key, item, remove):
                if not remove:
                    assert isinstance(item, str)
                else:
                    assert isinstance(item, Address)
                item = Address(email_address=item)
                return item

        mapper(User, users, properties={"addresses": relationship(Address)})
        mapper(Address, addresses)

        u1 = User()
        u1.addresses.append("e1")
        u1.addresses.append("e2")
        eq_(
            u1.addresses,
            [Address(email_address="e1"), Address(email_address="e2")],
        )
        u1.addresses = ["e3", "e4"]
        eq_(
            u1.addresses,
            [Address(email_address="e3"), Address(email_address="e4")],
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:34,代码来源:test_validators.py

示例10: test_validator_multi_warning

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def test_validator_multi_warning(self):
        users = self.tables.users

        class Foo(object):
            @validates("name")
            def validate_one(self, key, value):
                pass

            @validates("name")
            def validate_two(self, key, value):
                pass

        assert_raises_message(
            exc.InvalidRequestError,
            "A validation function for mapped attribute "
            "'name' on mapper Mapper|Foo|users already exists",
            mapper,
            Foo,
            users,
        )

        class Bar(object):
            @validates("id")
            def validate_three(self, key, value):
                return value + 10

            @validates("id", "name")
            def validate_four(self, key, value):
                return value + "foo"

        assert_raises_message(
            exc.InvalidRequestError,
            "A validation function for mapped attribute "
            "'name' on mapper Mapper|Bar|users already exists",
            mapper,
            Bar,
            users,
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:40,代码来源:test_validators.py

示例11: test_validator_w_removes

# 需要导入模块: from sqlalchemy import orm [as 别名]
# 或者: from sqlalchemy.orm import validates [as 别名]
def test_validator_w_removes(self):
        users, addresses, Address = (
            self.tables.users,
            self.tables.addresses,
            self.classes.Address,
        )
        canary = Mock()

        class User(fixtures.ComparableEntity):
            @validates("name", include_removes=True)
            def validate_name(self, key, item, remove):
                canary(key, item, remove)
                return item

            @validates("addresses", include_removes=True)
            def validate_address(self, key, item, remove):
                canary(key, item, remove)
                return item

        mapper(User, users, properties={"addresses": relationship(Address)})
        mapper(Address, addresses)

        u1 = User()
        u1.name = "ed"
        u1.name = "mary"
        del u1.name
        a1, a2, a3 = Address(), Address(), Address()
        u1.addresses.append(a1)
        u1.addresses.remove(a1)
        u1.addresses = [a1, a2]
        u1.addresses = [a2, a3]

        eq_(
            canary.mock_calls,
            [
                call("name", "ed", False),
                call("name", "mary", False),
                call("name", "mary", True),
                # append a1
                call("addresses", a1, False),
                # remove a1
                call("addresses", a1, True),
                # set to [a1, a2] - this is two appends
                call("addresses", a1, False),
                call("addresses", a2, False),
                # set to [a2, a3] - this is a remove of a1,
                # append of a3.  the appends are first.
                # in 1.2 due to #3896, we also get 'a2' in the
                # validates as it is part of the set
                call("addresses", a2, False),
                call("addresses", a3, False),
                call("addresses", a1, True),
            ],
        ) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:56,代码来源:test_validators.py


注:本文中的sqlalchemy.orm.validates方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。