pytoolbox.validation module¶
- pytoolbox.validation.valid_filename(path)[source]¶
Returns True if path is a valid file name.
Example usage
>>> valid_filename('my_file_without_extension') False >>> valid_filename('my_file_with_extension.mp4') True
- pytoolbox.validation.valid_ip(address)[source]¶
Returns True if ip is a valid IP address.
Example usage
>>> valid_ip('123.0.0.') False >>> valid_ip('239.232.0.222') True
- pytoolbox.validation.valid_port(port)[source]¶
Returns True if port is a valid port.
Example usage
>>> assert not valid_port(-1) >>> assert not valid_port('something not a port') >>> assert valid_port('80') >>> valid_port(65535) True
- pytoolbox.validation.valid_int(value)[source]¶
Returns True if value is a valid integer (can be converted to an int).
Example usage
>>> valid_int('dimitri is not a valid integer') False >>> valid_int('-10') True
- pytoolbox.validation.valid_uuid(value, objectid_allowed=False, none_allowed=False)[source]¶
Returns True if id is a valid UUID / ObjectId.
Example usage
>>> valid_uuid(None) False >>> valid_uuid(None, none_allowed=True) True >>> valid_uuid('gaga-gogo-gaga-gogo') False >>> valid_uuid('gaga-gogo-gaga-gogo', objectid_allowed=True) False >>> valid_uuid(uuid.uuid4(), none_allowed=False) True >>> valid_uuid(uuid.uuid4().hex, none_allowed=False) True >>> valid_uuid(str(uuid.uuid4().hex), none_allowed=False) True >>> valid_uuid(ObjectId()) False >>> valid_uuid(ObjectId(), objectid_allowed=True) True >>> valid_uuid(ObjectId().binary, objectid_allowed=True) True
- class pytoolbox.validation.CleanAttributesMixin[source]¶
Bases:
object
Put validation logic, cleanup code, … into a method clean_<attribute_name> and this method will be called every time the attribute is set.
Example usage
>>> class Settings(CleanAttributesMixin): ... def __init__(self, locale, broker): ... self.locale = locale ... self.broker = broker ... ... def clean_locale(self, value): ... value = str(value) ... assert len(value) == 2 ... return value
>>> settings = Settings('fr', {}) >>> settings = Settings(10, {}) >>> assert isinstance(settings.locale, str) >>> settings = Settings(100, 'a string') Traceback (most recent call last): ... AssertionError
- pytoolbox.validation.valid_secret(secret, none_allowed)[source]¶
Returns True if secret is a valid secret.
A valid secret contains at least 8 alpha-numeric characters.
Example usage
>>> valid_secret('1234', False) False >>> valid_secret(None, True) True >>> valid_secret(None, False) False >>> valid_secret('my_password', False) True
- pytoolbox.validation.validate_list(the_list, regexes)[source]¶
Validate every element of the_list with corresponding regular expression picked-in from regexes.
- pytoolbox.validation.valid_email(email)[source]¶
Returns True if email is a valid e-mail address.
Example usage
>>> valid_email('Tabby@croquetes') False >>> valid_email('Tabby@bernex.ch') True
- pytoolbox.validation.valid_uri(uri, check_404, scheme_mandatory=False, port_mandatory=False, default_port=80, excepted_errnos=(2, 111, 101), timeout=None)[source]¶
Example usage*
>>> valid_uri('http://docs.python.org/2/library/httplib.html', check_404=True) True >>> valid_uri('//docs.python.org/2/library/httplib.html', check_404=True) True >>> valid_uri('domain_not_exist_404_404/index.htmlw', check_404=True) False
Set default value for port [time’d out request]:
>>> valid_uri('http://google.com/pytoolbox', check_404=True, default_port=80, timeout=0.2) False
Following the syntax … in RFC 1808, … input is presumed … a path component:
>>> valid_uri('docs.python.org/2/library/httplib.html', check_404=True) False
This function does not use scheme of uri at all, so here is the proof:
>>> valid_uri('gluster://docs.python.org/2/library/httplib.html', check_404=True) True
Enforce the scheme or the port to being set:
>>> valid_uri( ... '//domain_not_exist_404_404/index.html:80', check_404=False, scheme_mandatory=True) False >>> valid_uri('//domain_not_exist_404_404/index.html:80', check_404=False, port_mandatory=True) False
- class pytoolbox.validation.StrongTypedMixin[source]¶
Bases:
object
Annotate arguments of the class __init__ with types and then you’ll get a class with type checking.
Example usage
>>> class Settings(StrongTypedMixin): ... def __init__(self, *, locale: (str, list), broker: dict=None, debug: bool=True, ... timezone=None): ... self.locale = locale ... self.broker = broker ... self.debug = debug ... self.timezone = timezone ... >>> settings = Settings(locale='fr', broker={}, debug=False) >>> settings = Settings( ... locale='fr', broker={}, timezone='this argument is not type checked') >>> settings = Settings(locale='fr') >>> print(settings.broker) None >>> settings = Settings(locale=['en', 'fr'], broker={}) >>> settings = Settings(locale=10, broker={}) Traceback (most recent call last): ... AssertionError: Attribute locale must be set to an instance of (<class 'str'>, <class 'list'>)