Skip to content

Commit

Permalink
Merge pull request #35 from gdcc/non-alphabetical-attrs
Browse files Browse the repository at this point in the history
Fix attributes starting with numeric names raise `ValueError`
  • Loading branch information
JR-1991 authored Nov 11, 2024
2 parents be66167 + 36f144d commit c86eb82
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
24 changes: 24 additions & 0 deletions easyDataverse/classgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,30 @@ def process_name(attr_name, common_part):
Returns:
str: The processed attribute name.
"""

if len(attr_name) == 0:
raise ValueError("Attribute name cannot be empty.")

# If the first letter is not aplhabet, append an underscore
if attr_name[0].isnumeric():
# Convert number to word
mapping = {
"0": "zero",
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine",
}
attr_name = mapping[attr_name[0]] + "_" + attr_name[1:]
elif not attr_name[0].isalpha():
# If the first character is not a letter, remove it
attr_name = attr_name[1:]

return camel_to_snake(attr_name).replace(common_part, "", 1).replace(" ", "")


Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ def test_dataset_creation(self, credentials):
assert hasattr(citation, "author")
assert hasattr(citation, "dataset_contact")
assert hasattr(citation, "ds_description")

def test_numeric_namespace(self):
# Harvard Dataverse hosts a metadata block with first letter numeric
# attribute names. Hence, this tests checks if the numeric parts are
# parsed correctly.

try:
Dataverse(server_url="https://dataverse.harvard.edu")
except ValueError as e:
AssertionError("Failed to parse numeric namespace: " + str(e))
6 changes: 2 additions & 4 deletions tests/unit/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ def test_empty_attribute_name_returns_empty_string(self):
common_part = ""

# Act
processed_name = process_name(attr_name, common_part)

# Assert
assert processed_name == ""
with pytest.raises(ValueError):
process_name(attr_name, common_part)

# Empty common part returns processed attribute name without common part.
@pytest.mark.unit
Expand Down

0 comments on commit c86eb82

Please sign in to comment.