summaryrefslogtreecommitdiff
path: root/lib/extras.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/extras.py')
-rw-r--r--lib/extras.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/extras.py b/lib/extras.py
index 1b0b2b6..3ef223e 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -344,7 +344,20 @@ class NamedTupleCursor(_cursor):
return
def _make_nt(self):
- return namedtuple("Record", [d[0] for d in self.description or ()])
+ # ascii except alnum and underscore
+ nochars = ' !"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~'
+ re_clean = _re.compile('[' + _re.escape(nochars) + ']')
+
+ def f(s):
+ s = re_clean.sub('_', s)
+ # Python identifier cannot start with numbers, namedtuple fields
+ # cannot start with underscore. So...
+ if s[0] == '_' or '0' <= s[0] <= '9':
+ s = 'f' + s
+
+ return s
+
+ return namedtuple("Record", [f(d[0]) for d in self.description or ()])
class LoggingConnection(_connection):