Enable Table.convert() on falsey values

This commit is contained in:
Martin Carpenter 2023-02-09 23:51:40 +01:00
commit a09df3844a
3 changed files with 9 additions and 4 deletions

View file

@ -2660,8 +2660,6 @@ class Table(Queryable):
def convert_value(v): def convert_value(v):
bar.update(1) bar.update(1)
if not v:
return v
return jsonify_if_needed(fn(v)) return jsonify_if_needed(fn(v))
self.db.register_function(convert_value) self.db.register_function(convert_value)

View file

@ -14,6 +14,8 @@ def parsedate(value, dayfirst=False, yearfirst=False, errors=None):
- errors=r.IGNORE to ignore values that cannot be parsed - errors=r.IGNORE to ignore values that cannot be parsed
- errors=r.SET_NULL to set values that cannot be parsed to null - errors=r.SET_NULL to set values that cannot be parsed to null
""" """
if not value:
return value
try: try:
return ( return (
parser.parse(value, dayfirst=dayfirst, yearfirst=yearfirst) parser.parse(value, dayfirst=dayfirst, yearfirst=yearfirst)
@ -38,6 +40,8 @@ def parsedatetime(value, dayfirst=False, yearfirst=False, errors=None):
- errors=r.IGNORE to ignore values that cannot be parsed - errors=r.IGNORE to ignore values that cannot be parsed
- errors=r.SET_NULL to set values that cannot be parsed to null - errors=r.SET_NULL to set values that cannot be parsed to null
""" """
if not value:
return value
try: try:
return parser.parse(value, dayfirst=dayfirst, yearfirst=yearfirst).isoformat() return parser.parse(value, dayfirst=dayfirst, yearfirst=yearfirst).isoformat()
except parser.ParserError: except parser.ParserError:
@ -53,4 +57,6 @@ def jsonsplit(value, delimiter=",", type=str):
""" """
Convert a string like a,b,c into a JSON array ["a", "b", "c"] Convert a string like a,b,c into a JSON array ["a", "b", "c"]
""" """
if not value:
return value
return json.dumps([type(s.strip()) for s in value.split(delimiter)]) return json.dumps([type(s.strip()) for s in value.split(delimiter)])

View file

@ -80,7 +80,8 @@ def test_convert_import(test_db_and_path):
db_path, db_path,
"example", "example",
"dt", "dt",
"return re.sub('O..', 'OXX', value)", #"return re.sub('O..', 'OXX', value)",
"return None if value is None else re.sub('O..', 'OXX', value)",
"--import", "--import",
"re", "re",
], ],
@ -223,7 +224,7 @@ def test_convert_output_column(test_db_and_path, drop):
db_path, db_path,
"example", "example",
"dt", "dt",
"value.replace('October', 'Spooktober')", "None if value is None else value.replace('October', 'Spooktober')",
"--output", "--output",
"newcol", "newcol",
] ]