Skip to content

Commit

Permalink
Add bytearray as supported type (fixes #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
GjjvdBurg committed Apr 28, 2021
1 parent bc2ff81 commit e31b3ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clevercsv/detect_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def detect_type(self, cell, is_quoted=False):
("nan", self.is_nan),
("date", self.is_date),
("datetime", self.is_datetime),
('bytearray', self.is_bytearray),
]
for name, func in type_tests:
if func(cell, is_quoted=is_quoted):
Expand Down Expand Up @@ -283,6 +284,11 @@ def is_unix_path(self, cell, **kwargs):
cell = cell.strip(" ")
return self._run_regex(cell, "unix_path")

def is_bytearray(self, cell: str, **kwargs) -> bool:
if self.strip_whitespace:
cell = cell.strip(" ")
return cell.startswith("bytearray(b") and cell.endswith(")")


def gen_known_type(cells):
"""
Expand Down
21 changes: 21 additions & 0 deletions tests/test_unit/test_detect_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,27 @@ def test_unicode_alphanum(self):
)
)

def test_bytearray(self):
yes_bytearray = [
"bytearray(b'')",
"bytearray(b'abc,*&@\"')",
"bytearray(b'bytearray(b'')')",
]
no_bytearray = [
"bytearray(b'abc",
"bytearray(b'abc'",
"bytearray('abc')",
"abc,bytearray(b'def')",
]

for case in yes_bytearray:
with self.subTest(case=case):
self.assertTrue(self.td.is_bytearray(case))

for case in no_bytearray:
with self.subTest(case=case):
self.assertFalse(self.td.is_bytearray(case))

# Unix path

def test_unix_path(self):
Expand Down

0 comments on commit e31b3ff

Please sign in to comment.