Implementation Hints
The preferred object types for the date/time objects are those
defined in the mxDateTime package. It provides all
necessary constructors and methods both at Python and C level.
The preferred object type for binary objects are
the buffer types available in standard Python starting with Version
1.5.2. Please see the Python documentation for details. For
information about the C interface have a look at
Include/bufferobject.h and
Objects/bufferobject.c in the Python source
distribution.
Here is a sample implementation of the Unix ticks based constructors for date/time delegating work to the generic constructors:
import time
def DateFromTicks(ticks):
return apply(Date,time.localtime(ticks)[:3])
def TimeFromTicks(ticks):
return apply(Time,time.localtime(ticks)[3:6])
def TimestampFromTicks(ticks):
return apply(Timestamp,time.localtime(ticks)[:6])This Python class allows implementing the above
type objects even though the description type code
field yields multiple values for on type object:
class DBAPITypeObject:
def __init__(self,*values):
self.values = values
def __cmp__(self,other):
if other in self.values:
return 0
if other < self.values:
return 1
else:
return -1The resulting type object compares equal to all
values passed to the constructor.
Here is a snippet of Python code that implements the exception hierarchy defined previously:
import exceptions class Error(exceptions.StandardError): pass class Warning(exceptions.StandardError): pass class InterfaceError(Error): ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access