Skip to content

Commit

Permalink
Merge pull request #68 from DHI/bug/allow_all_python_identifiers_as_v…
Browse files Browse the repository at this point in the history
…ariable_name

Bug/allow all python identifiers as variable name
  • Loading branch information
ryan-kipawa authored Nov 29, 2023
2 parents 7213cdc + f12715f commit 68837dc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
42 changes: 35 additions & 7 deletions mikeio1d/result_network/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,51 @@
from html import escape


def make_proper_variable_name(string, extra_string_before_digit="_"):
class ValidPythonIdentifierTranslatorTable:
"""
Translator table for replacing invalid python characters with a valid identifier.
Used as argument to Python's built-in str.translate() method.
Parameters
----------
replacement : str, default = "_"
The character to replace invalid characters with.
"""

def __init__(self, replacement="_"):
self.replacement = replacement

def __getitem__(self, key):
key = chr(key)
if key.isdigit() or key.isidentifier():
return key
else:
return self.replacement


_translator_table = ValidPythonIdentifierTranslatorTable()


def make_proper_variable_name(string: str, extra_string_before_digit="_"):
"""
Makes a more proper variable name.
It is assumed that the input string never is or after manipulations
becomes an '_' or an empty string.
"""
# Replace all non alpha numeric characters by an underscore.
string = re.sub(r"[^a-zA-Z0-9]", "_", string)
# Replace all characters that are not valid python identifier by an underscore.
string = string.translate(_translator_table)
# Replace more than two underscores with a single underscore.
string = re.sub(r"_{2,}", "_", string)
# Add an extra string is the string starts with a number.
string = extra_string_before_digit + string if string and string[0].isdigit() else string
# Remove a starting underscore
string = string[1:] if string[0] == "_" else string
if len(string) > 1:
string = string[1:] if string[0] == "_" else string
# Remove a trailing underscore
string = string[:-1] if string[-1] == "_" else string
if len(string) > 1:
string = string[:-1] if string[-1] == "_" else string
# Add an extra string if the string starts with a number.
string = extra_string_before_digit + string if string and string[0].isdigit() else string
return string


Expand Down
40 changes: 40 additions & 0 deletions tests/test_various.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import pytest

from mikeio1d import Res1D
from mikeio1d.result_network.various import make_proper_variable_name


@pytest.fixture
def test_file_path():
test_folder_path = os.path.dirname(os.path.abspath(__file__))
# Original file name was Exam6Base.res1d
return os.path.join(test_folder_path, "testdata", "Network_chinese.res1d")


def test_make_proper_variable_name():
mpvn = make_proper_variable_name # alias
assert mpvn("a") == "a"
assert mpvn("1") == "_1"
assert mpvn("1a") == "_1a"
assert mpvn("a1") == "a1"
assert mpvn("a1a") == "a1a"
assert mpvn("myname??++") == "myname"
assert mpvn("myname??++something") == "myname_something"
assert mpvn("你好") == "你好"
assert mpvn("123你好") == "_123你好"


def test_network_attributes_allow_chinese_characters(test_file_path):
"""Test that network attributes allow chinese characters."""
res = Res1D(test_file_path)
assert res.nodes.风景
assert res.nodes.电脑
assert res.nodes.餐厅
assert res.nodes.爱情
assert res.nodes.音乐
assert res.nodes.星期
assert res.nodes.医生
assert res.nodes.美丽
assert res.nodes.学生
assert res.nodes.蛋糕
Binary file added tests/testdata/Network_chinese.res1d
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/testdata/Network_chinese_source.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Base model: MIKE+ Examples - Sirius - DWF_Network Scenario
Updates:
1. Change the simulation duration to 1 hour.
2. Change the saving frequency to 10 minutes.
3. Update the IDs of 10 links to use chinese words.
2 changes: 2 additions & 0 deletions tests/testdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class testdata:

Network_res1d: str = format_path("Network.res1d")
"""A basic urban network file."""
Network_res1d_chinese: str = format_path("Network_chinese.res1d")
"""A basic urban network file with chinese characters for some links."""
NetworkRiver_res1d: str = format_path("NetworkRiver.res1d")
"""A basic river network file."""
Catchments_res1d: str = format_path("Catchments.res1d")
Expand Down

0 comments on commit 68837dc

Please sign in to comment.