Attempt to deal with some obvious errors in the code. These were all

due to using a single module-level namespace where multiple namespaces
were used before.

There *really* need to be tests for the sniffer stuff.  This could
have been avoided.

Skip, please review, and add sniffer tests!
This commit is contained in:
Fred Drake 2003-04-25 14:27:00 +00:00
parent 376e636f18
commit 7c852f33a2
1 changed files with 14 additions and 15 deletions

View File

@ -161,7 +161,6 @@ class Sniffer:
""" """
Takes a file-like object and returns a dialect (or None) Takes a file-like object and returns a dialect (or None)
""" """
self.fileobj = fileobj self.fileobj = fileobj
data = fileobj.read(self.sample) data = fileobj.read(self.sample)
@ -171,17 +170,17 @@ class Sniffer:
if delimiter is None: if delimiter is None:
delimiter, skipinitialspace = self._guessDelimiter(data) delimiter, skipinitialspace = self._guessDelimiter(data)
class Dialect(csv.Dialect): class SniffedDialect(Dialect):
_name = "sniffed" _name = "sniffed"
lineterminator = '\r\n' lineterminator = '\r\n'
quoting = csv.QUOTE_MINIMAL quoting = QUOTE_MINIMAL
# escapechar = '' # escapechar = ''
doublequote = False doublequote = False
Dialect.delimiter = delimiter SniffedDialect.delimiter = delimiter
Dialect.quotechar = quotechar SniffedDialect.quotechar = quotechar
Dialect.skipinitialspace = skipinitialspace SniffedDialect.skipinitialspace = skipinitialspace
self.dialect = Dialect self.dialect = SniffedDialect
return self.dialect return self.dialect
@ -189,8 +188,8 @@ class Sniffer:
return self._hasHeaders(self.fileobj, self.dialect) return self._hasHeaders(self.fileobj, self.dialect)
def register_dialect(self, name = 'sniffed'): def register_dialect(self, name='sniffed'):
csv.register_dialect(name, self.dialect) register_dialect(name, self.dialect)
def _guessQuoteAndDelimiter(self, data): def _guessQuoteAndDelimiter(self, data):
@ -378,19 +377,19 @@ class Sniffer:
# objects... # objects...
fileobj.seek(0) fileobj.seek(0)
reader = csv.reader(fileobj, r = csv.reader(fileobj,
delimiter = dialect.delimiter, delimiter=dialect.delimiter,
quotechar = dialect.quotechar, quotechar=dialect.quotechar,
skipinitialspace = dialect.skipinitialspace) skipinitialspace=dialect.skipinitialspace)
header = reader.next() # assume first row is header header = r.next() # assume first row is header
columns = len(header) columns = len(header)
columnTypes = {} columnTypes = {}
for i in range(columns): columnTypes[i] = None for i in range(columns): columnTypes[i] = None
checked = 0 checked = 0
for row in reader: for row in r:
# arbitrary number of rows to check, to keep it sane # arbitrary number of rows to check, to keep it sane
if checked > 20: if checked > 20:
break break