<s> from collections import namedtuple <EOL> import re <EOL> from builtins import str <EOL> from decimal import Decimal <EOL> from datetime import date <EOL> from datetime import datetime <EOL> from dateutil import parser <EOL> from vertica_python import errors <EOL> import pytz <EOL> years_re = re . compile ( r'<STR_LIT>' ) <EOL> def timestamp_parse ( s ) : <EOL> try : <EOL> dt = _timestamp_parse ( s ) <EOL> except ValueError : <EOL> year_match = years_re . match ( s ) <EOL> if year_match : <EOL> year = year_match . groups ( ) [ <NUM_LIT:0> ] <EOL> dt = _timestamp_parse_without_year ( s [ len ( year ) + <NUM_LIT:1> : ] ) <EOL> dt = dt . replace ( year = int ( year ) % <NUM_LIT> ) <EOL> else : <EOL> raise errors . DataError ( '<STR_LIT>' % s ) <EOL> return dt <EOL> def _timestamp_parse ( s ) : <EOL> if len ( s ) == <NUM_LIT> : <EOL> return datetime . strptime ( s , '<STR_LIT>' ) <EOL> return datetime . strptime ( s , '<STR_LIT>' ) <EOL> def _timestamp_parse_without_year ( s ) : <EOL> if len ( s ) == <NUM_LIT> : <EOL> return datetime . strptime ( s , '<STR_LIT>' ) <EOL> return datetime . strptime ( s , '<STR_LIT>' ) <EOL> def timestamp_tz_parse ( s ) : <EOL> if s . endswith ( '<STR_LIT>' ) : <EOL> ts = timestamp_parse ( s [ : - <NUM_LIT:3> ] ) <EOL> ts = ts . replace ( tzinfo = pytz . UTC ) <EOL> return ts <EOL> return parser . parse ( s ) <EOL> def date_parse ( s ) : <EOL> """<STR_LIT>""" <EOL> if s . endswith ( b'<STR_LIT>' ) : <EOL> raise errors . NotSupportedError ( '<STR_LIT>' + str ( s , '<STR_LIT:utf-8>' ) ) <EOL> return date ( * map ( lambda x : int ( x ) , s . split ( b'<STR_LIT:->' ) ) ) <EOL> ColumnTuple = namedtuple ( <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> class Column ( object ) : <EOL> @ classmethod <EOL> def data_type_conversions ( cls , unicode_error = None ) : <EOL> if unicode_error is None : <EOL> unicode_error = '<STR_LIT:strict>' <EOL> return [ <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT:bool>' , lambda s : s == '<STR_LIT:t>' ) , <EOL> ( '<STR_LIT>' , lambda s : int ( s ) ) , <EOL> ( '<STR_LIT:float>' , lambda s : float ( s ) ) , <EOL> ( '<STR_LIT>' , lambda s : str ( s , '<STR_LIT:utf-8>' , unicode_error ) ) , <EOL> ( '<STR_LIT>' , lambda s : str ( s , '<STR_LIT:utf-8>' , unicode_error ) ) , <EOL> ( '<STR_LIT:date>' , date_parse ) , <EOL> ( '<STR_LIT:time>' , None ) , <EOL> ( '<STR_LIT>' , timestamp_parse ) , <EOL> ( '<STR_LIT>' , timestamp_tz_parse ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , lambda s : Decimal ( s ) ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ] <EOL> @ property <EOL> def data_types ( ) : <EOL> return map ( lambda x : x [ <NUM_LIT:0> ] , Column . data_type_conversions ( ) ) <EOL> def __init__ ( self , col , unicode_error = None ) : <EOL> self . name = col [ '<STR_LIT:name>' ] <EOL> self . type_code = col [ '<STR_LIT>' ] <EOL> self . display_size = None <EOL> self . internal_size = col [ '<STR_LIT>' ] <EOL> self . precision = None <EOL> self . scale = None <EOL> self . null_ok = None <EOL> self . unicode_error = unicode_error <EOL> self . data_type_conversions = Column . data_type_conversions ( unicode_error = self . unicode_error ) <EOL> if self . type_code == <NUM_LIT> : <EOL> self . type_code = <NUM_LIT:9> <EOL> if self . type_code >= len ( self . data_type_conversions ) : <EOL> self . type_code = <NUM_LIT:0> <EOL> self . props = ColumnTuple ( col [ '<STR_LIT:name>' ] , self . type_code , None , col [ '<STR_LIT>' ] , None , None , None ) <EOL> self . converter = self . data_type_conversions [ self . type_code ] [ <NUM_LIT:1> ] <EOL> def convert ( self , s ) : <EOL> if s is None : <EOL> return <EOL> return self . converter ( s ) if self . converter is not None else s <EOL> def __str__ ( self ) : <EOL> return self . props . __str__ ( ) <EOL> def __unicode__ ( self ) : <EOL> return str ( self . props . __str__ ( ) ) <EOL> def __repr__ ( self ) : <EOL> return self . props . __str__ ( ) <EOL> def __iter__ ( self ) : <EOL> for prop in self . props : <EOL> yield prop <EOL> def __getitem__ ( self , key ) : <EOL> return self . props [ key ] </s>
