From e7f32219f93c95a3e6ea21d53be8aad3a17abf08 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Fri, 14 Jun 2024 16:43:48 +0200 Subject: [PATCH 1/9] Lagt inn noen funksjoner for xml filer --- src/ssb_konjunk/xml_handling.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/ssb_konjunk/xml_handling.py diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py new file mode 100644 index 0000000..0344707 --- /dev/null +++ b/src/ssb_konjunk/xml_handling.py @@ -0,0 +1,46 @@ +"""A collection of functions to make xml files handling easier both in dapla and prodsone. +""" + +import pandas as pd +import xml.etree.ElementTree as ET + + +def read_xml(xml_file: str, fs=None) -> ET.Element: + """Funtion to get xml root from disk. + + Args: + xml_file: Strin value for xml filepath. + fs: filesystem + + Returns: + ET.Element: Root of xml file.""" + + if fs: + with fs.open(xml_file, mode="r") as file: + single_xml = file.read() + file.close() + else: + with open(xml_file, mode="r") as file: + single_xml = file.read() + file.close() + + return ET.fromstring(single_xml) + + +def return_txt_xml(root: ET.Element, child: str) -> str|None: + """ + Function to return text value from child element in xml file. + + Args: + root: Root with all data stored in a branch like structure. + child: String value to find child element which contains a value. + + Returns: + str: Returns string value from child element. + """ + for element in root.iter(child): + return element.text + + + + From c985099ee6f845deb86d4037b4835147c3a5cba8 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Fri, 14 Jun 2024 16:54:42 +0200 Subject: [PATCH 2/9] pre-commit fiks --- src/ssb_konjunk/xml_handling.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index 0344707..35ee989 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,35 +1,33 @@ """A collection of functions to make xml files handling easier both in dapla and prodsone. """ -import pandas as pd import xml.etree.ElementTree as ET def read_xml(xml_file: str, fs=None) -> ET.Element: """Funtion to get xml root from disk. - + Args: xml_file: Strin value for xml filepath. fs: filesystem - + Returns: - ET.Element: Root of xml file.""" - + ET.Element: Root of xml file. + """ if fs: with fs.open(xml_file, mode="r") as file: single_xml = file.read() file.close() else: - with open(xml_file, mode="r") as file: + with open(xml_file) as file: single_xml = file.read() file.close() - + return ET.fromstring(single_xml) -def return_txt_xml(root: ET.Element, child: str) -> str|None: - """ - Function to return text value from child element in xml file. +def return_txt_xml(root: ET.Element, child: str) -> str | None: + """Function to return text value from child element in xml file. Args: root: Root with all data stored in a branch like structure. @@ -40,7 +38,3 @@ def return_txt_xml(root: ET.Element, child: str) -> str|None: """ for element in root.iter(child): return element.text - - - - From 374d28e2df8d3718a5293149a07ab2c79667f51b Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Fri, 14 Jun 2024 17:04:15 +0200 Subject: [PATCH 3/9] pre-commit fiks --- src/ssb_konjunk/xml_handling.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index 35ee989..3daaf85 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,33 +1,34 @@ -"""A collection of functions to make xml files handling easier both in dapla and prodsone. -""" +"""A collection of functions to make xml files handling easier both in dapla and prodsone.""" +import pandas as pd import xml.etree.ElementTree as ET -def read_xml(xml_file: str, fs=None) -> ET.Element: +def read_xml(xml_file: str, fs=None) -> ET.Element: #Type ignore """Funtion to get xml root from disk. - + Args: xml_file: Strin value for xml filepath. fs: filesystem - + Returns: - ET.Element: Root of xml file. - """ + ET.Element: Root of xml file.""" + if fs: with fs.open(xml_file, mode="r") as file: single_xml = file.read() file.close() else: - with open(xml_file) as file: + with open(xml_file, mode="r") as file: single_xml = file.read() file.close() - + return ET.fromstring(single_xml) -def return_txt_xml(root: ET.Element, child: str) -> str | None: - """Function to return text value from child element in xml file. +def return_txt_xml(root: ET.Element, child: str) -> str|None: + """ + Function to return text value from child element in xml file. Args: root: Root with all data stored in a branch like structure. @@ -37,4 +38,4 @@ def return_txt_xml(root: ET.Element, child: str) -> str | None: str: Returns string value from child element. """ for element in root.iter(child): - return element.text + return element.text \ No newline at end of file From b54cc5039b4f8623dc7735177415246a6b576642 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Fri, 14 Jun 2024 17:05:10 +0200 Subject: [PATCH 4/9] pre-commit fiks --- src/ssb_konjunk/xml_handling.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index 3daaf85..f7cfd7f 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,34 +1,32 @@ """A collection of functions to make xml files handling easier both in dapla and prodsone.""" -import pandas as pd import xml.etree.ElementTree as ET -def read_xml(xml_file: str, fs=None) -> ET.Element: #Type ignore +def read_xml(xml_file: str, fs=None) -> ET.Element: # Type ignore """Funtion to get xml root from disk. - + Args: xml_file: Strin value for xml filepath. fs: filesystem - + Returns: - ET.Element: Root of xml file.""" - + ET.Element: Root of xml file. + """ if fs: with fs.open(xml_file, mode="r") as file: single_xml = file.read() file.close() else: - with open(xml_file, mode="r") as file: + with open(xml_file) as file: single_xml = file.read() file.close() - + return ET.fromstring(single_xml) -def return_txt_xml(root: ET.Element, child: str) -> str|None: - """ - Function to return text value from child element in xml file. +def return_txt_xml(root: ET.Element, child: str) -> str | None: + """Function to return text value from child element in xml file. Args: root: Root with all data stored in a branch like structure. @@ -38,4 +36,4 @@ def return_txt_xml(root: ET.Element, child: str) -> str|None: str: Returns string value from child element. """ for element in root.iter(child): - return element.text \ No newline at end of file + return element.text From cd46a59006b39517f8991ddc158d7b3c510ccc61 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 17 Jun 2024 14:44:49 +0200 Subject: [PATCH 5/9] Lagt til noen greie funksjoner for xml filer. --- src/ssb_konjunk/xml_handling.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index f7cfd7f..0037ad4 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,9 +1,10 @@ """A collection of functions to make xml files handling easier both in dapla and prodsone.""" import xml.etree.ElementTree as ET +import dapla +from typing import Optional - -def read_xml(xml_file: str, fs=None) -> ET.Element: # Type ignore +def read_xml(xml_file: str, fs: dapla.gcs.GCSFileSystem = None) -> ET.Element: """Funtion to get xml root from disk. Args: @@ -11,7 +12,7 @@ def read_xml(xml_file: str, fs=None) -> ET.Element: # Type ignore fs: filesystem Returns: - ET.Element: Root of xml file. + ET.Element: Root of xml file. """ if fs: with fs.open(xml_file, mode="r") as file: @@ -25,7 +26,7 @@ def read_xml(xml_file: str, fs=None) -> ET.Element: # Type ignore return ET.fromstring(single_xml) -def return_txt_xml(root: ET.Element, child: str) -> str | None: +def return_txt_xml(root: ET.Element, child: str) -> Optional[str]: """Function to return text value from child element in xml file. Args: @@ -36,4 +37,21 @@ def return_txt_xml(root: ET.Element, child: str) -> str | None: str: Returns string value from child element. """ for element in root.iter(child): - return element.text + string = element.text + return string + + +def dump_element(element:ET.Element, indent:int=0): + """Function to print xml in pretty format. + + Args: + element: ET.Element you want to print. + indent: Level of ident you want. + """ + print(" " * indent + f"Tag: {element.tag}") + if element.text and element.text.strip(): + print(" " * (indent + 1) + f"Text: {element.text.strip()}") + for attribute, value in element.attrib.items(): + print(" " * (indent + 1) + f"Attribute: {attribute}={value}") + for child in element: + dump_element(child, indent + 1) \ No newline at end of file From 29c6db2c720741a6dfdc43ff47928bec438830a0 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 17 Jun 2024 14:48:27 +0200 Subject: [PATCH 6/9] Nox fiks --- src/ssb_konjunk/xml_handling.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index 0037ad4..491987d 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,8 +1,9 @@ """A collection of functions to make xml files handling easier both in dapla and prodsone.""" import xml.etree.ElementTree as ET + import dapla -from typing import Optional + def read_xml(xml_file: str, fs: dapla.gcs.GCSFileSystem = None) -> ET.Element: """Funtion to get xml root from disk. @@ -26,7 +27,7 @@ def read_xml(xml_file: str, fs: dapla.gcs.GCSFileSystem = None) -> ET.Element: return ET.fromstring(single_xml) -def return_txt_xml(root: ET.Element, child: str) -> Optional[str]: +def return_txt_xml(root: ET.Element, child: str) -> str | None: """Function to return text value from child element in xml file. Args: @@ -41,7 +42,7 @@ def return_txt_xml(root: ET.Element, child: str) -> Optional[str]: return string -def dump_element(element:ET.Element, indent:int=0): +def dump_element(element: ET.Element, indent: int = 0): """Function to print xml in pretty format. Args: @@ -54,4 +55,4 @@ def dump_element(element:ET.Element, indent:int=0): for attribute, value in element.attrib.items(): print(" " * (indent + 1) + f"Attribute: {attribute}={value}") for child in element: - dump_element(child, indent + 1) \ No newline at end of file + dump_element(child, indent + 1) From b6721649cbba2e3712330c3ca47900ee1d0927bd Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 17 Jun 2024 14:49:54 +0200 Subject: [PATCH 7/9] lagt til Typehints --- src/ssb_konjunk/xml_handling.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index 491987d..67e6758 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,7 +1,6 @@ """A collection of functions to make xml files handling easier both in dapla and prodsone.""" import xml.etree.ElementTree as ET - import dapla @@ -42,7 +41,7 @@ def return_txt_xml(root: ET.Element, child: str) -> str | None: return string -def dump_element(element: ET.Element, indent: int = 0): +def dump_element(element: ET.Element, indent: int = 0) -> None: """Function to print xml in pretty format. Args: From b8a2c30f05324faa86f9322e29a4f69991dbd8f4 Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 17 Jun 2024 14:50:41 +0200 Subject: [PATCH 8/9] Siste fiks xml funk --- src/ssb_konjunk/xml_handling.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ssb_konjunk/xml_handling.py b/src/ssb_konjunk/xml_handling.py index 67e6758..a12999b 100644 --- a/src/ssb_konjunk/xml_handling.py +++ b/src/ssb_konjunk/xml_handling.py @@ -1,6 +1,7 @@ """A collection of functions to make xml files handling easier both in dapla and prodsone.""" import xml.etree.ElementTree as ET + import dapla From 9fdb3c5b7bc9069f1f0ddd103ae344dcb9b17dad Mon Sep 17 00:00:00 2001 From: EdvardGarmannslund Date: Mon, 17 Jun 2024 14:53:35 +0200 Subject: [PATCH 9/9] Konjunk ver 0.0.7 xml funksjoner --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 6a7e462..ecb5955 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ssb-konjunk" -version = "0.0.6" +version = "0.0.7" description = "SSB Konjunk" authors = ["Edvard Garmannslund "] license = "MIT"