Skip to content

Commit

Permalink
importers.csvbase: Allow to specifying a default value
Browse files Browse the repository at this point in the history
Add to all implemented column types the possibility to specify a
default value to return when the parsed field is empty. When a default
is not specified, the empty field is passed to the parser to generate
a value.
  • Loading branch information
dnicolodi committed Jan 24, 2025
1 parent 04b641b commit 1dbcf2c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
16 changes: 9 additions & 7 deletions beangulp/importers/csvbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ class Columns(Column):
Args:
name: Column names or indexes.
sep: Separator to use to join columns.
default: Value to return all the fields are empty, if specified.
"""
def __init__(self, *names, sep=' '):
super().__init__(*names)
def __init__(self, *names, sep=' ', default=NA):
super().__init__(*names, default=default)
self.sep = sep

def parse(self, *values):
Expand All @@ -119,10 +120,11 @@ class Date(Column):
Args:
name: Column name or index.
frmt: Date format specification.
default: Value to return if the field is empty, if specified.
"""
def __init__(self, name, frmt='%Y-%m-%d'):
super().__init__(name)
def __init__(self, name, frmt='%Y-%m-%d', default=NA):
super().__init__(name, default=default)
self.frmt = frmt

def parse(self, value):
Expand All @@ -142,11 +144,11 @@ class Amount(Column):
subs: Dictionary mapping regular expression patterns to
replacement strings. Substitutions are performed with
re.sub() in the order they are specified.
default: Value to return if the field is empty, if specified.
"""

def __init__(self, name, subs=None):
super().__init__(name)
def __init__(self, name, subs=None, default=NA):
super().__init__(name, default=default)
self.subs = subs if subs is not None else {}

def parse(self, value):
Expand Down
42 changes: 42 additions & 0 deletions beangulp/importers/csvbase_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ def test_custom_format(self):
value = func(('16.05.2021', ))
self.assertEqual(value, datetime.date(2021, 5, 16))

def test_default_value(self):
column = Date(0, default=datetime.date.today())
func = column.getter(None)
value = func(('', ))
self.assertEqual(value, datetime.date.today())

def test_default_value_none(self):
column = Date(0, default=None)
func = column.getter(None)
value = func(('', ))
self.assertIsNone(value)


class TestColumnsColumn(unittest.TestCase):

Expand All @@ -78,6 +90,24 @@ def test_custom_sep(self):
value = func(('0', '1', '2', '3', ))
self.assertEqual(value, '0: 1')

def test_default_value(self):
column = Columns(0, 1, default='something')
func = column.getter(None)
value = func(('', '', ))
self.assertEqual(value, 'something')

def test_default_value_none(self):
column = Columns(0, 1, default=None)
func = column.getter(None)
value = func(('', '', ))
self.assertIsNone(value)

def test_some_empty(self):
column = Columns(0, 1, default=None)
func = column.getter(None)
value = func(('this', '', ))
self.assertEqual(value, 'this')


class TestAmountColumn(unittest.TestCase):

Expand Down Expand Up @@ -109,6 +139,18 @@ def test_parse_subs_currency(self):
self.assertIsInstance(value, decimal.Decimal)
self.assertEqual(value, decimal.Decimal('1000.00'))

def test_default_value(self):
column = Amount(0, default=decimal.Decimal(42))
func = column.getter(None)
value = func(('', ))
self.assertEqual(value, decimal.Decimal(42))

def test_default_value_none(self):
column = Amount(0, default=None)
func = column.getter(None)
value = func(('', ))
self.assertIsNone(value)


class TestCSVMeta(unittest.TestCase):

Expand Down

0 comments on commit 1dbcf2c

Please sign in to comment.