Remove sets module in favor of standard sets module (Python-2.3 and

newer)
This commit is contained in:
adustman
2006-02-27 17:19:40 +00:00
parent b13701f252
commit b14a0fa91d
2 changed files with 31 additions and 153 deletions

View File

@ -37,22 +37,42 @@ apilevel = "2.0"
paramstyle = "format"
from _mysql import *
from MySQLdb.sets import DBAPISet, Set
#from MySQLdb.sets import DBAPISet, Set
from MySQLdb.constants import FIELD_TYPE
from MySQLdb.times import Date, Time, Timestamp, \
DateFromTicks, TimeFromTicks, TimestampFromTicks
from sets import ImmutableSet
class DBAPISet(ImmutableSet):
STRING = DBAPISet(FIELD_TYPE.ENUM, FIELD_TYPE.STRING,
FIELD_TYPE.VAR_STRING)
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,
"""A special type of set for which A == x is true if A is a
DBAPISet and x is a member of that set."""
def __ne__(self, other):
from sets import BaseSet
if isinstance(other, BaseSet):
return super(self).__ne__(self, other)
else:
return other not in self
def __eq__(self, other):
from sets import BaseSet
if isinstance(other, BaseSet):
return super(self).__eq__(self, other)
else:
return other in self
STRING = DBAPISet([FIELD_TYPE.ENUM, FIELD_TYPE.STRING,
FIELD_TYPE.VAR_STRING])
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 = DBAPISet(FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE)
TIME = DBAPISet(FIELD_TYPE.TIME)
TIMESTAMP = DBAPISet(FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME)
FIELD_TYPE.TINY, FIELD_TYPE.YEAR])
DATE = DBAPISet([FIELD_TYPE.DATE, FIELD_TYPE.NEWDATE])
TIME = DBAPISet([FIELD_TYPE.TIME])
TIMESTAMP = DBAPISet([FIELD_TYPE.TIMESTAMP, FIELD_TYPE.DATETIME])
DATETIME = TIMESTAMP
ROWID = DBAPISet()
@ -73,7 +93,7 @@ __all__ = [ 'BINARY', 'Binary', 'Connect', 'Connection', 'DATE',
'FIELD_TYPE', 'IntegrityError', 'InterfaceError', 'InternalError',
'MySQLError', 'NULL', 'NUMBER', 'NotSupportedError', 'DBAPISet',
'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'TIME',
'TIMESTAMP', 'Set', 'Warning', 'apilevel', 'connect', 'connections',
'TIMESTAMP', 'Warning', 'apilevel', 'connect', 'connections',
'constants', 'cursors', 'debug', 'escape', 'escape_dict',
'escape_sequence', 'escape_string', 'get_client_info',
'paramstyle', 'string_literal', 'threadsafety', 'version_info']

View File

@ -1,142 +0,0 @@
"""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."""
return ','.join([ str(x) for x in 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 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 self.__class__(*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 self.__class__(*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