Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions simple/stats/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
_DCS_PREFIX = "dcs:"

_NAMESPACE_DELIMITER = ':'
_GLOBAL_PREFIXES = ("dcid:", "dcs:", "schema:")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid adding defensive code or input-cleaning logic (such as stripping prefixes) for states or patterns that are guaranteed by the system's architecture or design to never occur in the input data. We should remove this prefix stripping logic entirely.

References
  1. Avoid adding defensive code or input-cleaning logic (such as stripping prefixes) for states or patterns that are guaranteed by the system's architecture or design to never occur in the input data.



@dataclass
Expand Down Expand Up @@ -591,7 +592,10 @@ def strip_namespace(v: str) -> str:
Strips namespaces from dcids.
e.g. 'dcid:country/USA' -> 'country/USA'
"""
return v[v.find(_NAMESPACE_DELIMITER) + 1:]
for prefix in _GLOBAL_PREFIXES:
if v.startswith(prefix):
return v[len(prefix):]
return v


def strip_namespace_series(series: pd.Series) -> pd.Series:
Expand All @@ -603,7 +607,7 @@ def strip_namespace_series(series: pd.Series) -> pd.Series:
Returns:
Series with namespaces stripped
"""
return series.str.split(_NAMESPACE_DELIMITER, n=1).str[-1]
return series.str.replace(r'^(dcid:|dcs:|schema:)', '', regex=True)


def filter_invalid_observation_values(df: pd.DataFrame) -> pd.DataFrame:
Expand Down
24 changes: 24 additions & 0 deletions simple/tests/stats/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import unittest

import pandas as pd

from stats.data import _get_flattened_dataclass_field_names
from stats.data import strip_namespace
from stats.data import strip_namespace_series
from stats.data import Event
from stats.data import McfNode
from stats.data import Observation
Expand Down Expand Up @@ -204,3 +208,23 @@ def test_get_flattened_dataclass_field_names(self):
]
self.assertListEqual(_get_flattened_dataclass_field_names(Observation),
expected)

def test_strip_namespace(self):
self.assertEqual(strip_namespace("dcid:country/USA"), "country/USA")
self.assertEqual(strip_namespace("dcs:measuredValue"), "measuredValue")
self.assertEqual(strip_namespace("schema:Thing"), "Thing")
# Custom namespace should NOT be stripped
self.assertEqual(strip_namespace("oecd:Annual_Average_Wage"), "oecd:Annual_Average_Wage")
# No namespace
self.assertEqual(strip_namespace("country/USA"), "country/USA")

def test_strip_namespace_series(self):
series = pd.Series([
"dcid:country/USA", "dcs:measuredValue", "schema:Thing",
"oecd:Annual_Average_Wage", "country/USA"
])
expected = pd.Series([
"country/USA", "measuredValue", "Thing",
"oecd:Annual_Average_Wage", "country/USA"
])
pd.testing.assert_series_equal(strip_namespace_series(series), expected)