~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/processors.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# sqlalchemy/processors.py
2
 
# Copyright (C) 2010-2011 the SQLAlchemy authors and contributors <see AUTHORS file>
 
2
# Copyright (C) 2010-2013 the SQLAlchemy authors and contributors <see AUTHORS file>
3
3
# Copyright (C) 2010 Gaetan de Menten gdementen@gmail.com
4
4
#
5
5
# This module is part of SQLAlchemy and is released under
16
16
import re
17
17
import datetime
18
18
 
 
19
 
19
20
def str_to_datetime_processor_factory(regexp, type_):
20
21
    rmatch = regexp.match
21
22
    # Even on python2.6 datetime.strptime is both slower than this code
22
23
    # and it does not support microseconds.
 
24
    has_named_groups = bool(regexp.groupindex)
 
25
 
23
26
    def process(value):
24
27
        if value is None:
25
28
            return None
28
31
                m = rmatch(value)
29
32
            except TypeError:
30
33
                raise ValueError("Couldn't parse %s string '%r' "
31
 
                                "- value is not a string." % (type_.__name__ , value))
 
34
                                "- value is not a string." %
 
35
                                (type_.__name__, value))
32
36
            if m is None:
33
37
                raise ValueError("Couldn't parse %s string: "
34
 
                                "'%s'" % (type_.__name__ , value))
35
 
            return type_(*map(int, m.groups(0)))
 
38
                                "'%s'" % (type_.__name__, value))
 
39
            if has_named_groups:
 
40
                groups = m.groupdict(0)
 
41
                return type_(**dict(zip(groups.iterkeys(),
 
42
                                        map(int, groups.itervalues()))))
 
43
            else:
 
44
                return type_(*map(int, m.groups(0)))
36
45
    return process
37
46
 
 
47
 
38
48
def boolean_to_int(value):
39
49
    if value is None:
40
50
        return None
41
51
    else:
42
52
        return int(value)
43
53
 
 
54
 
44
55
def py_fallback():
45
56
    def to_unicode_processor_factory(encoding, errors=None):
46
57
        decoder = codecs.getdecoder(encoding)
118
129
 
119
130
except ImportError:
120
131
    globals().update(py_fallback())
121