Skip to content

Commit

Permalink
Replace removeprefix with 3.7 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
IanCa committed May 7, 2024
1 parent 714d6c4 commit f8ac3c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion hed/schema/schema_io/ontology_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def _get_hedid_range(schema_name, df_key):
return set(range(final_start, final_end))


# todo: Replace this once we no longer support < python 3.9
def remove_prefix(text, prefix):
if text and text.startswith(prefix):
return text[len(prefix):]
return text


def get_all_ids(df):
"""Returns a set of all unique hedIds in the dataframe
Expand All @@ -82,7 +89,7 @@ def get_all_ids(df):
numbers(Set or None): None if this has no hed column, otherwise all unique numbers as a set.
"""
if constants.hed_id in df.columns:
modified_df = df[constants.hed_id].str.removeprefix("HED_")
modified_df = df[constants.hed_id].apply(lambda x: remove_prefix(x, "HED_"))
modified_df = pd.to_numeric(modified_df, errors="coerce").dropna().astype(int)
return set(modified_df.unique())
return None
Expand Down
4 changes: 2 additions & 2 deletions hed/schema/schema_io/schema2df.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Allows output of HedSchema objects as .mediawiki format"""

from hed.schema.hed_schema_constants import HedSectionKey, HedKey
from hed.schema.schema_io.ontology_util import get_library_name_and_id
from hed.schema.schema_io.ontology_util import get_library_name_and_id, remove_prefix
from hed.schema.schema_io.schema2base import Schema2Base
import pandas as pd
import hed.schema.hed_schema_df_constants as constants
Expand Down Expand Up @@ -40,7 +40,7 @@ def _get_object_name_and_id(self, object_name, include_prefix=False):
hed_id(str): The full formatted hed_id
"""
prefix, obj_id = get_library_name_and_id(self._schema)
name = f"{prefix}{object_name.removeprefix('Hed')}"
name = f"{prefix}{remove_prefix(object_name, 'Hed')}"
full_hed_id = self._get_object_id(object_name, obj_id, include_prefix)
return name, full_hed_id

Expand Down

0 comments on commit f8ac3c9

Please sign in to comment.