Changeset: 0b7f07e12542 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=0b7f07e12542 Modified Files: clients/python/monetdb/exceptions.py clients/python/monetdb/mapi.py clients/python/monetdb/sql/converters.py clients/python/monetdb/sql/monetize.py clients/python/monetdb/sql/pythonize.py clients/python/monetdb/sql/types.py clients/python/test/capabilities.py clients/python/test/runtests.py Branch: default Log Message:
improved pylint score, improved test coverage diffs (truncated from 437 to 300 lines): diff --git a/clients/python/monetdb/exceptions.py b/clients/python/monetdb/exceptions.py --- a/clients/python/monetdb/exceptions.py +++ b/clients/python/monetdb/exceptions.py @@ -14,6 +14,9 @@ # Portions created by CWI are Copyright (C) 1997-July 2008 CWI. # Copyright August 2008-2012 MonetDB B.V. # All Rights Reserved. +""" +MonetDB Python API specific exceptions +""" class Warning(StandardError): """Exception raised for important warnings like data diff --git a/clients/python/monetdb/mapi.py b/clients/python/monetdb/mapi.py --- a/clients/python/monetdb/mapi.py +++ b/clients/python/monetdb/mapi.py @@ -23,11 +23,9 @@ import socket import logging import struct import hashlib -import platform - from cStringIO import StringIO -from monetdb.exceptions import * +from monetdb.exceptions import OperationalError, DatabaseError, ProgrammingError, NotSupportedError logger = logging.getLogger("monetdb") @@ -53,13 +51,23 @@ STATE_READY = 1 class Server(object): + """ + MAPI (low level MonetDB API) connection + """ + def __init__(self): self.state = STATE_INIT self._result = None - self.socket = None + self.socket = "" + self.hostname = "" + self.port = 0 + self.username = "" + self.password = "" + self.database = "" + self.language = "" def connect(self, hostname, port, username, password, database, language): - """ connect to a MonetDB database using the mapi protocol""" + """ setup connection to MAPI server""" self.hostname = hostname self.port = port @@ -78,7 +86,7 @@ class Server(object): self.socket.connect((hostname, port)) except socket.error, error: (error_code, error_str) = error - raise OperationalError(error_str) + raise OperationalError(error_str + " (%s)" % error_code) self.__login() @@ -169,7 +177,6 @@ class Server(object): """ generate a response to a mapi login challenge """ challenges = challenge.split(':') salt, identity, protocol, hashes, endian = challenges[:5] - password = self.password if protocol == '9': diff --git a/clients/python/monetdb/sql/converters.py b/clients/python/monetdb/sql/converters.py --- a/clients/python/monetdb/sql/converters.py +++ b/clients/python/monetdb/sql/converters.py @@ -23,15 +23,31 @@ from monetdb.sql import monetize from monetdb.sql import pythonize class Pythonizer: + """ + backwards compatible class, was used for convertion from + MonetDB column types to python types. You should use + monetdb.sql.pythonize now. + """ def __init__(self, use_unicode): pass def convert(self, data, type_code): + """ + use a type_code defined in monetdb.sql.types + """ return pythonize.convert(data, type_code) + class Monetizer: + """ + backwards compatible class, was used for convertion from + python types to MonetDB column types. You should use + monetdb.sql.monetize now. + """ def __init__(self): pass def convert(self, data): + """ + """ return monetize.convert(data) \ No newline at end of file diff --git a/clients/python/monetdb/sql/monetize.py b/clients/python/monetdb/sql/monetize.py --- a/clients/python/monetdb/sql/monetize.py +++ b/clients/python/monetdb/sql/monetize.py @@ -20,29 +20,33 @@ functions for converting python objects """ import datetime -import logging import decimal -logger = logging.getLogger("monetdb") def monet_none(data): + """ + returns a NULL string + """ return "NULL" def monet_bool(data): - if data: - return "true" - else: - return "false" + """ + returns "true" or "false" + """ + return ["false", "true"][bool(data)] def monet_escape(data): + """ + returns an escaped string + """ data = str(data).replace( "\\", "\\\\") data = data.replace( "\'", "\\\'") return "'%s'" % str(data) -def monet_string(data): - return str(data) - def monet_bytes(data): + """ + converts bytes to string + """ return monet_escape(data) def monet_unicode(data): @@ -51,14 +55,14 @@ def monet_unicode(data): mapping = { type(None): monet_none, bool: monet_bool, - int: monet_string, - float: monet_string, - complex: monet_string, - int: monet_string, + int: str, + float: str, + complex: str, + int: str, str: monet_escape, datetime.datetime: monet_escape, datetime.time: monet_escape, - decimal.Decimal: monet_string, + decimal.Decimal: str, datetime.timedelta: monet_escape, datetime.date: monet_escape, bytes: monet_bytes, @@ -66,6 +70,9 @@ mapping = { } def convert(data): + """ + Calls the appropriate convertion function based upon the python type + """ try: return mapping[type(data)](data) except KeyError: diff --git a/clients/python/monetdb/sql/pythonize.py b/clients/python/monetdb/sql/pythonize.py --- a/clients/python/monetdb/sql/pythonize.py +++ b/clients/python/monetdb/sql/pythonize.py @@ -19,19 +19,15 @@ functions for converting monetdb SQL fields to Python objects """ -import logging import time import datetime from decimal import Decimal from monetdb.sql import types -import monetdb.exceptions -import re - -logger = logging.getLogger("monetdb") +from monetdb.exceptions import ProgrammingError def strip(data): - """ returns a python string, chops off quotes, - replaces escape characters""" + """ returns a python string, with chopped off quotes, + and replaced escape characters""" return data[1:-1].decode('string_escape') def py_bool(data): @@ -39,18 +35,26 @@ def py_bool(data): return (data == "true") def py_time(data): + """ returns a python Time + """ return Time(*[int(float(x)) for x in data.split(':')]) def py_date(data): + """ Returns a python Date + """ return Date(*[int(float(x)) for x in data.split('-')]) def py_timestamp(data): + """ Returns a python Timestamp + """ splitted = data.split(" ") date = [int(float(x)) for x in splitted[0].split('-')] time = [int(float(x)) for x in splitted[1].split(':')] return Timestamp(*date+time) def py_timestamptz(data): + """ Returns a python Timestamp where data contains a tz code + """ if data.find('+')!= -1: (dt, tz) = data.split("+") (tzhour, tzmin) = [int(x) for x in tz.split(':')] @@ -69,15 +73,6 @@ def py_timestamptz(data): hour, minute, second = time return Timestamp(year, month, day, hour+tzhour, minute+tzmin, second) -def py_blob(x): - """ Converts a monetdb blob in string representation to a python string. - The input is a string in the format: '(length: char char char char ... )' - w/ char in hex representation. The output is the string of chars. """ - #TODO: need to check if blob datatype should use this? otherwise - # this can be removed - x = x[x.find(":")+2:-1] - return ''.join(map(lambda x: chr(int(x, 16)), x.split(" "))) - mapping = { types.CHAR: strip, types.VARCHAR: strip, @@ -107,6 +102,10 @@ mapping = { } def convert(data, type_code): + """ + Calls the appropriate convertion function based upon the python type + """ + # null values should always be replaced by None type if data == "NULL": return None @@ -118,46 +117,29 @@ def convert(data, type_code): # below us stuff required by the DBAPI +def Binary(data): + """returns binary encoding of data""" + return ''.join([hex(ord(i))[2:] for i in data]).upper() + +def DateFromTicks(ticks): + """Convert ticks to python Date""" + return Date(*time.localtime(ticks)[:3]) + +def TimeFromTicks(ticks): + """Convert ticks to python Time""" + return Time(*time.localtime(ticks)[3:6]) + +def TimestampFromTicks(ticks): + """Convert ticks to python Timestamp""" + return Timestamp(*time.localtime(ticks)[:6]) + Date = datetime.date Time = datetime.time Timestamp = datetime.datetime - -def Binary(x): - #return x.encode("hex").upper() - return ''.join([hex(ord(i))[2:] for i in x]).upper() - -def DateFromTicks(ticks): - return Date(*time.localtime(ticks)[:3]) - -def TimeFromTicks(ticks): - return Time(*time.localtime(ticks)[3:6]) - -def TimestampFromTicks(ticks): - return Timestamp(*time.localtime(ticks)[:6]) _______________________________________________ Checkin-list mailing list Checkin-list@monetdb.org http://mail.monetdb.org/mailman/listinfo/checkin-list