From 7b8b08229637171d79619e5d5caecf81dabe31c7 Mon Sep 17 00:00:00 2001 From: Geson-anko <59220704+Geson-anko@users.noreply.github.com> Date: Sat, 7 May 2022 17:29:34 +0900 Subject: [PATCH 1/2] ADD join_relatively --- JarvisEngine/core/name.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/JarvisEngine/core/name.py b/JarvisEngine/core/name.py index 161a2bf..7ebcfe3 100644 --- a/JarvisEngine/core/name.py +++ b/JarvisEngine/core/name.py @@ -33,6 +33,23 @@ def count_head_sep(name:str) -> int: num += 1 return num +def join_relatively(name:str, another:str) -> str: + """Join another to name. + Ex: + join_relatively("a.b.c","..d.e") -> "a.b.d.e" + join_relatively("a.", "...d.e.f") -> "d.e.f" + """ + name = clean(name) + n_sep = count_head_sep(another) - 1 + if n_sep > 0: + splited = name.split(SEP)[:-n_sep] + name = SEP.join(splited) + another = clean(another) + if name == "": + return another + else: + return f"{name}{SEP}{another}" + def join(name:str, *others:str) -> str: """join names Ex: join("a.b.","c.d") -> "a.b.c.d" From 5723efa82dac5f234780cd6063632311b01ae422 Mon Sep 17 00:00:00 2001 From: Geson-anko <59220704+Geson-anko@users.noreply.github.com> Date: Sat, 7 May 2022 17:29:47 +0900 Subject: [PATCH 2/2] ADD test_join_relatively --- tests/core/test_name.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/core/test_name.py b/tests/core/test_name.py index 7424cae..1332239 100644 --- a/tests/core/test_name.py +++ b/tests/core/test_name.py @@ -26,4 +26,10 @@ def test_count_head_sep(): assert name.count_head_sep(".....") == 5 assert name.count_head_sep("asf....") == 0 assert name.count_head_sep("..v..s..a.sd") == 2 - \ No newline at end of file + + +def test_join_relatively(): + assert name.join_relatively("a.b.c","..d.e") == "a.b.d.e" + assert name.join_relatively("a.", "...d.e.f") == "d.e.f" + assert name.join_relatively("a.b.c",".d.e") == "a.b.c.d.e" + assert name.join_relatively("a","b") == "a.b" \ No newline at end of file