Changeset 901 for trunk/pg.py


Ignore:
Timestamp:
Jan 6, 2017, 7:25:02 AM (3 years ago)
Author:
cito
Message:

Improve creation of named tuples in Python 2.6 and 3.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r900 r901  
    4242from math import isnan, isinf
    4343from collections import namedtuple
     44from keyword import iskeyword
    4445from operator import itemgetter
    4546from functools import partial
     
    12641265
    12651266
     1267_re_fieldname = regex('^[A-Za-z][_a-zA-Z0-9]*$')
     1268
    12661269# The result rows for database operations are returned as named tuples
    12671270# by default. Since creating namedtuple classes is a somewhat expensive
     
    12751278            return namedtuple('Row', names, rename=True)._make
    12761279        except TypeError:  # Python 2.6 and 3.0 do not support rename
    1277             names = [v if v.isalnum() else 'column_%d' % (n,)
     1280            names = [v if _re_fieldname.match(v) and not iskeyword(v)
     1281                        else 'column_%d' % (n,)
    12781282                     for n, v in enumerate(names)]
    12791283            return namedtuple('Row', names)._make
Note: See TracChangeset for help on using the changeset viewer.