Package konval :: Module typeval :: Class ToInt
[hide private]
[frames] | no frames]

Class ToInt

source code

                 object --+        
                          |        
basevalidator.BaseValidator --+    
                              |    
                         ToType --+
                                  |
                                 ToInt

Convert a value to an integer.

While you could just use int, this throws a much nicer error message.

For example:

>>> v = ToInt()
>>> v(1)
1
>>> v(2.3)
2
>>> v('foo')
Traceback (most recent call last):
...
ValueError: can't convert 'foo' to integer
Instance Methods [hide private]
 
__init__(self)
Class c'tor, accepting a type.
source code

Inherited from ToType: convert_value, make_conversion_error_msg

Inherited from basevalidator.BaseValidator: __call__, convert, make_validation_error_msg, raise_conversion_error, raise_validation_error, validate, validate_value

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self)
(Constructor)

source code 

Class c'tor, accepting a type.

For example:

>>> v = ToType(int, type_name='an integer')
>>> v(1)
1
>>> v(2.3)
2
>>> v('foo')
Traceback (most recent call last):
...
ValueError: can't convert 'foo' to an integer
>>> v = ToType(float)
>>> v('foo')
Traceback (most recent call last):
...
ValueError: can't convert 'foo' to float
Parameters:
  • to_type - A class constructor (old or new style), built-in type, or function that can be called to convert a type and will throw if it fails.
  • type_name - A name for the type produced. If not supplied, it will be extracted from to_type if possible.
Overrides: object.__init__
(inherited documentation)