Package recsql :: Module convert :: Class Autoconverter
[hide private]
[frames] | no frames]

Class Autoconverter

source code

object --+
         |
        Autoconverter

Automatically convert an input value to a special python object.

The :meth:`Autoconverter.convert` method turns the value into a special
python value and casts strings to the "best" type (see :func:`besttype`). 

The defaults for the conversion of a input field value to a
special python value are:

  ===========  ===============
  value        python
  ===========  ===============
    '---'       ``None``
    'none'
    'None'
    ''

    'True'      ``True``
    'x'
    'X'
    'yes'

    'False'     ``False``
    '-'
    'no'
  ===========  ===============

If the *sep* keyword is set to a string instead of ``False`` then
values are split into tuples. Probably the most convenient way to
use this is to set *sep* = ``True`` (or ``None``) because this
splits on all white space whereas *sep* = ' ' would split multiple
spaces.

**Example**
   - With *sep* = ``True``: 'foo bar 22  boing ---' --> ('foo', 'boing', 22, None)
   - With *sep* = ',':       1,2,3,4 --> (1,2,3,4) 

Instance Methods [hide private]
 
__init__(self, mode="fancy", mapping=None, active=True, sep=False, **kwargs)
Initialize the converter.
source code
 
_convert_singlet(self, s) source code
 
_convert_fancy(self, field)
Convert to a list (sep != None) and convert list elements.
source code

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

Class Variables [hide private]
  active = property(** active())
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, mode="fancy", mapping=None, active=True, sep=False, **kwargs)
(Constructor)

source code 
Initialize the converter.

:Arguments:
  *mode*
     defines what the converter does

        "simple"
            convert entries with :func:`besttype`
        "singlet"
            convert entries with :func:`besttype` and apply
            mappings
        "fancy"
            first splits fields into lists, tries mappings,
            and does the stuff that "singlet" does
        "unicode"
            convert all entries with :func:`to_unicode`             

  *mapping*
      any dict-like mapping that supports lookup. If``None`` then the
      hard-coded defaults are used
  *active* or *autoconvert*
      initial state of the :attr:`Autoconverter.active` toggle.
      ``False`` deactivates any conversion. [``True``]
   *sep*
      character to split on (produces lists); use ``True`` or ``None``
      (!) to split on all white space.
   *encoding*
      encoding of the input data [utf-8]

Overrides: object.__init__