Skip to content

Commit c02f5f7

Browse files
committed
refactor finalized
1 parent 67f28a3 commit c02f5f7

2 files changed

Lines changed: 13 additions & 31 deletions

File tree

egsim/api/forms/flatfile.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
get_dtype_of,
1818
column_exists,
1919
column_type,
20-
column_categorical_dtype,
2120
column_aliases,
2221
column_dtype,
2322
column_help,
@@ -271,11 +270,9 @@ def get_hr_flatfile_column_meta(name: str, values: Optional[pd.Series] = None) -
271270

272271
if column_exists(name):
273272
c_dtype = column_dtype(name)
274-
cat_dtype = column_categorical_dtype(name)
275-
if cat_dtype is not None:
276-
# c_categories is a pandas CategoricalStype. So:
277-
c_dtype = get_dtype_of(cat_dtype.categories)
278-
c_categories = cat_dtype.categories.tolist()
273+
if isinstance(c_dtype, pd.CategoricalDtype):
274+
c_categories = c_dtype.categories.tolist()
275+
c_dtype = get_dtype_of(c_dtype.categories)
279276
c_type = getattr(column_type(name), 'value', "")
280277
c_help = column_help(name) or ""
281278
c_aliases = column_aliases(name)

egsim/smtk/flatfile.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ def validate_flatfile_dataframe(
221221
xp_dtype = extra_dtypes[col]
222222
else:
223223
xp_dtype = column_dtype(col)
224-
if xp_dtype == ColumnDtype.category:
225-
xp_dtype = column_categorical_dtype(col)
226224

227225
if xp_dtype is None:
228226
continue
@@ -510,30 +508,15 @@ def column_help(column: str) -> str:
510508
return _load_flatfile_columns_properties()[column].get('help', "")
511509

512510

513-
def column_dtype(column: str) -> Union[ColumnDtype, None]:
511+
def column_dtype(column: str) -> Union[ColumnDtype, pd.CategoricalDtype, None]:
514512
"""
515-
Return the data type of the given column name, as `ColumnDtype` Enum item,
516-
or None if the column has no known data type. If the return value is
517-
`ColumnDtype.category`, get more info via `get_categorical_dtype(column)`.
513+
Return the data type of the given column name, as None (no data type set),
514+
`ColumnDtype` Enum item, or pands `CategoricalDtype` object (in this case
515+
`column_dtype(...).categories` will list the possible values that the column can
516+
take). As such `ColumnDtype.category` is never returned.
518517
If `column` is 'SA(<period>)', it will default to 'SA'
519518
"""
520-
dtype = _column_dtype(column)
521-
if isinstance(dtype, pd.CategoricalDtype):
522-
return ColumnDtype.category
523-
return dtype
524-
525-
526-
def column_categorical_dtype(column: str) -> Union[pd.CategoricalDtype, None]:
527-
"""
528-
Return the pandas CategoricalDtype, a data type for categorical data, for
529-
the given column. To get the possible categories, use the `.categories` attribute
530-
of the returned object. Return None if the column data type is not categorical.
531-
If `column` is 'SA(<period>)', it will default to 'SA'
532-
"""
533-
dtype = _column_dtype(column)
534-
if isinstance(dtype, pd.CategoricalDtype):
535-
return dtype
536-
return None
519+
return _load_flatfile_columns_properties()[column].get('dtype', None)
537520

538521

539522
def _column_dtype(column: str) -> Union[pd.CategoricalDtype, ColumnDtype, None]:
@@ -759,8 +742,10 @@ def valid_expr_sequence(tok_num1: int, tok_val1: str, tok_num2: int, tok_val2: s
759742
elif tok_num2 == NEWLINE:
760743
return tok_num1 in {NUMBER, NAME, STRING} or tok_val1 == ')'
761744
elif (tok_num1, tok_num2) == (OP, OP):
762-
return (tok_val1 + tok_val2 in
763-
{'(~', '~(', '((', '~~', '))', '&(', '|(', ')|', ')&'})
745+
return (
746+
tok_val1 + tok_val2 in
747+
{'(~', '~(', '((', '~~', '))', '&(', '|(', ')|', ')&'}
748+
)
764749
elif (tok_num1, tok_num2) == (NAME, OP):
765750
return tok_val2 in {'==', '!=', '<', '<=', '>', '>=', ')', '+', '-', '*', '/'}
766751
elif (tok_num1, tok_num2) == (OP, NAME):

0 commit comments

Comments
 (0)