Smash data into two separate modules: sets and times. I never liked

that name anyway.

Set has been extended somewhat. The comparision operators really only
work properly with Python 2.1, due to the limitations of __cmp__.
Set also uses the binary operators (&, |, ^), since these make somewhat
more sense than the arithmetic ones, though there is no good analog for
- (if only there were a nand operator...)

Bump the version to 0.9.0b3. This is not the actual 0.9.0b3 release yet,
however. I want to do some more insanity checking. But almost ready for
some candidate releases.
This commit is contained in:
adustman
2001-05-12 04:30:34 +00:00
parent 9c6aaae1eb
commit ce00d83836
6 changed files with 164 additions and 97 deletions

View File

@ -20,7 +20,7 @@ version_info = (
9,
0,
"beta",
2)
3)
if version_info[3] == "final": __version__ = "%d.%d.%d" % version_info[:3]
else: __version__ = "%d.%d.%d%1.1s%d" % version_info[:5]
@ -40,17 +40,17 @@ from connections import Connection
from converters import *
from constants import FIELD_TYPE
STRING = Set(FIELD_TYPE.CHAR, FIELD_TYPE.ENUM, FIELD_TYPE.STRING,
STRING = DBAPISet(FIELD_TYPE.CHAR, FIELD_TYPE.ENUM, FIELD_TYPE.STRING,
FIELD_TYPE.VAR_STRING)
BINARY = Set(FIELD_TYPE.BLOB, FIELD_TYPE.LONG_BLOB, FIELD_TYPE.MEDIUM_BLOB,
FIELD_TYPE.TINY_BLOB)
NUMBER = Set(FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT,
BINARY = DBAPISet(FIELD_TYPE.BLOB, FIELD_TYPE.LONG_BLOB,
FIELD_TYPE.MEDIUM_BLOB, FIELD_TYPE.TINY_BLOB)
NUMBER = DBAPISet(FIELD_TYPE.DECIMAL, FIELD_TYPE.DOUBLE, FIELD_TYPE.FLOAT,
FIELD_TYPE.INT24, FIELD_TYPE.LONG, FIELD_TYPE.LONGLONG,
FIELD_TYPE.TINY, FIELD_TYPE.YEAR)
DATE = Set(FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE)
TIME = Set(FIELD_TYPE.TIME)
TIMESTAMP = Set(FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME)
ROWID = Set()
DATE = DBAPISet(FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE)
TIME = DBAPISet(FIELD_TYPE.TIME)
TIMESTAMP = DBAPISet(FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME)
ROWID = DBAPISet()
def Binary(x): return str(x)
@ -64,7 +64,7 @@ __all__ = [ 'BINARY', 'Binary', 'Connect', 'Connection', 'DATE',
'Date', 'Time', 'Timestamp', 'DateFromTicks', 'TimeFromTicks',
'TimestampFromTicks', 'DataError', 'DatabaseError', 'Error',
'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError',
'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError',
'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'DBAPISet',
'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME',
'TIMESTAMP', 'Set', 'Warning', 'apilevel', 'connect', 'connections',
'constants', 'cursors', 'debug', 'escape', 'escape_dict',

View File

@ -27,7 +27,8 @@ MySQL.connect().
from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
from constants import FIELD_TYPE
from data import *
from sets import *
from times import *
from string import split
import types

143
MySQLdb/MySQLdb/sets.py Normal file
View File

@ -0,0 +1,143 @@
"""sets module
This module provides some Set classes for dealing with MySQL data.
"""
class Set:
"""A simple class for handling sets. Sets are immutable in the same
way numbers are."""
def __init__(self, *values):
"""Use values to initialize the Set."""
self._values = values
def __contains__(self, value):
return value in self._values and 1 or 0
def __str__(self):
"""Returns the values as a comma-separated string."""
from string import join
return join(map(str, self._values),',')
def __repr__(self):
return "%s%s" % (self.__class__.__name__, `self._values`)
def __or__(self, other):
"""Union."""
values = list(self._values)
if isinstance(other, Set):
for v in other._values:
if v not in values:
values.append(v)
elif other not in self._values:
values.append(other)
return apply(self.__class__, values)
__add__ = __or__
def __sub__(self, other):
values = list(self._values)
if isinstance(other, Set):
for v in other._values:
if v in values:
values.remove(v)
elif other in self:
values.remove(other)
return apply(self.__class__, tuple(values))
def __and__(self, other):
"Intersection."
values = []
if isinstance(other, Set):
for v in self._values:
if v in other:
values.append(v)
elif other in self:
values.append(other)
return apply(self.__class__, tuple(values))
__mul__ = __and__
def __xor__(self, other):
"Intersection's complement."
return (self|other)-(self&other)
def __getitem__(self, n):
return self._values[n]
def __getslice__(self, n1, n2):
return self._values[n1:n2]
def __len__(self):
return len(self._values)
def __hash__(self):
return hash(self._values)
def __cmp__(self, other):
if isinstance(other, Set):
if not self ^ other:
return 0
elif self & other == self:
return 1
else:
return -1
elif other in self._values:
return 0
elif other > self._values:
return 1
else:
return -1
# rich comparison operators for Python 2.1 and up
def __ne__(self, other):
return self ^ other
def __eq__(self, other):
if not self != other:
return self
else:
return self.__class__()
def __le__(self, other):
return self & other == self
def __lt__(self, other):
if self <= other and self ^ other:
return self
else:
return self.__class__()
def __ge__(self, other):
return self & other == other
def __gt__(self, other):
if self >= other and self ^ other:
return self
else:
return self.__class__()
class DBAPISet(Set):
"""A special type of set for which A == x is true if A is a
DBAPISet and x is a member of that set."""
# Note that Set.__cmp__ works perfectly well in this case, if
# we are using < Python 2.1. It's just broken for <, >, etc.
def __ne__(self, other):
if isinstance(other, Set): # yes, Set
return self % other
elif other in self._values:
return 0
else:
return 1
def __eq__(self, other):
if self != other:
return self.__class__()
else:
return self

View File

@ -1,89 +1,11 @@
"""data module
"""times module
This module provides some useful classes for dealing with MySQL data.
This module provides some Date and Time classes for dealing with MySQL data.
"""
from time import strftime, localtime
from _mysql import string_literal
class Set:
"""A simple class for handling sets. Sets are immutable in the same
way numbers are."""
def __init__(self, *values):
"""Use values to initialize the Set."""
self._values = values
def contains(self, value):
"""Returns true if the value is contained within the Set."""
return value in self._values
def __str__(self):
"""Returns the values as a comma-separated string."""
from string import join
return join(map(str, self._values),',')
def __repr__(self):
return "Set%s" % `self._values`
def __add__(self, other):
"""Union."""
if isinstance(other, Set):
for v in other._values:
self = self + v
elif other not in self._values:
self = apply(Set, self._values+(other,))
return self
def __sub__(self, other):
if isinstance(other, Set):
for v in other._values:
if v in self:
self = self - v
elif other in self:
values = list(self._values)
values.remove(other)
return apply(Set, tuple(values))
return self
def __mul__(self, other):
"Intersection."
intersection = Set()
if isinstance(other, Set):
union = self + other
intersection = union
for v in union._values:
if v not in self or v not in other:
intersection = intersection - v
elif other in self:
intersection = apply(Set, (other,))
return intersection
def __mod__(self, other):
"Disjoint."
return (self+other)-(self*other)
def __getitem__(self, n):
return self._values[n]
def __len__(self):
return len(self._values)
def __hash__(self):
return hash(self._values)
def __cmp__(self, other):
if isinstance(other, Set):
d = self % other
if d._values:
return 1
else:
return 0
if other in self._values:
return 0
return -1
try:
try:
# new packaging

View File

@ -1 +1 @@
static char _mysql__version__[] = "0.9.0b2";
static char _mysql__version__[] = "0.9.0b3";

View File

@ -75,7 +75,7 @@ MySQLdb. MySQLdb is free software.
setup (# Distribution meta-data
name = "MySQL-python",
version = "0.9.0b2",
version = "0.9.0b3",
description = "An interface to MySQL",
long_description=long_description,
author = "Andy Dustman",
@ -89,7 +89,8 @@ setup (# Distribution meta-data
"MySQLdb.converters",
"MySQLdb.connections",
"MySQLdb.cursors",
"MySQLdb.data",
"MySQLdb.sets",
"MySQLdb.times",
"MySQLdb.constants.CR",
"MySQLdb.constants.FIELD_TYPE",
"MySQLdb.constants.ER",