Skip to content

Commit

Permalink
Move csv.join / split definitions to src/dbi.py
Browse files Browse the repository at this point in the history
Monkey patching a built-in library is just plain confusing.
  • Loading branch information
jlu5 committed Jun 5, 2023
1 parent a399a54 commit 5eb8e31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
17 changes: 15 additions & 2 deletions src/dbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,19 @@ def close(self):
'flat': FlatfileMapping,
}

###
# csv.{join,split} -- useful functions that should exist.
###
def csv_join(L):
fd = minisix.io.StringIO()
writer = csv.writer(fd)
writer.writerow(L)
return fd.getvalue().rstrip('\r\n')

def csv_split(s):
fd = minisix.io.StringIO(s)
reader = csv.reader(fd)
return next(reader)

class Record(object):
def __init__(self, id=None, **kwargs):
Expand Down Expand Up @@ -444,11 +457,11 @@ def __init__(self, id=None, **kwargs):
setattr(self, name, default)

def serialize(self):
return csv.join([repr(getattr(self, name)) for name in self.fields])
return csv_join([repr(getattr(self, name)) for name in self.fields])

def deserialize(self, s):
unseenRecords = set(self.fields)
for (name, strValue) in zip(self.fields, csv.split(s)):
for (name, strValue) in zip(self.fields, csv_split(s)):
setattr(self, name, self.converters[name](strValue))
unseenRecords.remove(name)
for name in unseenRecords:
Expand Down
17 changes: 0 additions & 17 deletions src/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@

from . import minisix

###
# csv.{join,split} -- useful functions that should exist.
###
import csv
def join(L):
fd = minisix.io.StringIO()
writer = csv.writer(fd)
writer.writerow(L)
return fd.getvalue().rstrip('\r\n')

def split(s):
fd = minisix.io.StringIO(s)
reader = csv.reader(fd)
return next(reader)
csv.join = join
csv.split = split

def force(x):
if callable(x):
return x()
Expand Down

0 comments on commit 5eb8e31

Please sign in to comment.