|
1 | | -# Licensed to the Apache Software Foundation (ASF) under one |
2 | | -# or more contributor license agreements. See the NOTICE file |
3 | | -# distributed with this work for additional information |
4 | | -# regarding copyright ownership. The ASF licenses this file |
5 | | -# to you under the Apache License, Version 2.0 (the |
6 | | -# "License"); you may not use this file except in compliance |
7 | | -# with the License. You may obtain a copy of the License at |
8 | | -# |
9 | | -# http://www.apache.org/licenses/LICENSE-2.0 |
10 | | -# |
11 | | -# Unless required by applicable law or agreed to in writing, software |
12 | | -# distributed under the License is distributed on an "AS IS" BASIS, |
13 | | -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | -# See the License for the specific language governing permissions and |
15 | | -# limitations under the License. |
16 | | - |
17 | 1 | from binascii import hexlify |
18 | 2 | from collections import namedtuple |
19 | | -try: |
20 | | - from collections import OrderedDict |
21 | | -except ImportError: # Python <2.7 |
22 | | - from cassandra.util import OrderedDict # NOQA |
23 | | - |
24 | 3 | import datetime |
25 | 4 | import logging |
| 5 | +import re |
26 | 6 | import socket |
27 | 7 | import types |
28 | 8 | from uuid import UUID |
| 9 | + |
| 10 | +try: |
| 11 | + from collections import OrderedDict |
| 12 | +except ImportError: # Python <2.7 |
| 13 | + from cassandra.util import OrderedDict # NOQA |
| 14 | + |
29 | 15 | try: |
30 | 16 | from cStringIO import StringIO |
31 | 17 | except ImportError: |
@@ -60,12 +46,20 @@ class InternalError(Exception): |
60 | 46 | HEADER_DIRECTION_MASK = 0x80 |
61 | 47 |
|
62 | 48 |
|
| 49 | +NON_ALPHA_REGEX = re.compile('\W') |
| 50 | +END_UNDERSCORE_REGEX = re.compile('^_*(\w*[a-zA-Z0-9])_*$') |
| 51 | + |
| 52 | + |
| 53 | +def _clean_column_name(name): |
| 54 | + return END_UNDERSCORE_REGEX.sub("\g<1>", NON_ALPHA_REGEX.sub("_", name)) |
| 55 | + |
| 56 | + |
63 | 57 | def tuple_factory(colnames, rows): |
64 | 58 | return rows |
65 | 59 |
|
66 | 60 |
|
67 | 61 | def named_tuple_factory(colnames, rows): |
68 | | - Row = namedtuple('Row', colnames) |
| 62 | + Row = namedtuple('Row', map(_clean_column_name, colnames)) |
69 | 63 | return [Row(*row) for row in rows] |
70 | 64 |
|
71 | 65 |
|
|
0 commit comments