From 04f40de0556d15693f626947d2151261c039f6c0 Mon Sep 17 00:00:00 2001 From: YHordijk <42876712+YHordijk@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:48:16 +0200 Subject: [PATCH 1/6] getattr and setattr now simply point to getitem and setitem --- TCutility/results/result.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/TCutility/results/result.py b/TCutility/results/result.py index 5fff9e87..ef34e28a 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -3,19 +3,22 @@ class Result(dict): '''Class used for storing results from AMS calculations. The class is functionally a dictionary, but allows dot notation to access variables in the dictionary.''' + def __getitem__(self, key): + self.__set_empty(key) + val = super().__getitem__(self.__get_case(key)) + return val + def __getattr__(self, key): - if key not in self: - val = Result() - self.__setitem__(key, val) - return val + return self.__getitem__(key) - val = self.__getitem__(key) + def __setitem__(self, key, val): + # we set the item, but if it is a dict we convert the dict to a Result first if isinstance(val, dict): val = Result(val) - self.__setattr__(key, val) - return val + super().__setitem__(self.__get_case(key), val) - def __getitem__(self, key): + def __setattr__(self, key, val): + self.__setitem__(key, val) if key not in self: val = Result() self.__setitem__(key, val) From 527217379512db031c45381fded496751c41be98 Mon Sep 17 00:00:00 2001 From: YHordijk <42876712+YHordijk@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:48:47 +0200 Subject: [PATCH 2/6] Added new contains and get_case method, used for adding case-insensitivity --- TCutility/results/result.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/TCutility/results/result.py b/TCutility/results/result.py index ef34e28a..ddd9f1d2 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -19,6 +19,10 @@ def __setitem__(self, key, val): def __setattr__(self, key, val): self.__setitem__(key, val) + + def __contains__(self, key): + # Custom method to check if the key is defined in this object, case-insensitive. + return key.lower() in [key_.lower() for key_ in self.keys()] if key not in self: val = Result() self.__setitem__(key, val) @@ -27,3 +31,10 @@ def __setattr__(self, key, val): def __setattr__(self, key, val): self.__setitem__(key, val) + def __get_case(self, key): + # Get the case of the key as it has been set in this object. + # The first time a key-value pair has been assigned the case of the key will be set. + for key_ in self: + if key_.lower() == key.lower(): + return key_ + return key From 04dd2f387981ecf99e2d46978c35d968d7d112dd Mon Sep 17 00:00:00 2001 From: YHordijk <42876712+YHordijk@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:49:12 +0200 Subject: [PATCH 3/6] Added set_empty, which ensures that the key is present in the object --- TCutility/results/result.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TCutility/results/result.py b/TCutility/results/result.py index ddd9f1d2..94d4993e 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -23,14 +23,14 @@ def __setattr__(self, key, val): def __contains__(self, key): # Custom method to check if the key is defined in this object, case-insensitive. return key.lower() in [key_.lower() for key_ in self.keys()] + + def __set_empty(self, key): + # This function checks if the key has been set. + # If it has not, we create a new Result object and set it at the desired key if key not in self: val = Result() self.__setitem__(key, val) - return val - return super().__getitem__(key) - def __setattr__(self, key, val): - self.__setitem__(key, val) def __get_case(self, key): # Get the case of the key as it has been set in this object. # The first time a key-value pair has been assigned the case of the key will be set. From feff4e320a57aa71fa9a81e043d079f7aa5ca022 Mon Sep 17 00:00:00 2001 From: YHordijk <42876712+YHordijk@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:49:25 +0200 Subject: [PATCH 4/6] Added a simple use-case example --- TCutility/results/result.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/TCutility/results/result.py b/TCutility/results/result.py index 94d4993e..e3382b76 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -38,3 +38,13 @@ def __get_case(self, key): if key_.lower() == key.lower(): return key_ return key + + +if __name__ == '__main__': + ret = Result() + ret.aDf.x = {'a': 1, 'b': 2} + print(ret.adf.x.a) + + ret.ADF.y = 2345 + ret.dftb.z = 'hello' + print(ret) From cb1ab18c86e010b1605425d134e5d72af8785204 Mon Sep 17 00:00:00 2001 From: YHordijk <42876712+YHordijk@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:49:40 +0200 Subject: [PATCH 5/6] Improved doc-string for the class with information about case-insensitivity --- TCutility/results/result.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TCutility/results/result.py b/TCutility/results/result.py index e3382b76..a45d86d0 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -2,7 +2,7 @@ class Result(dict): - '''Class used for storing results from AMS calculations. The class is functionally a dictionary, but allows dot notation to access variables in the dictionary.''' + '''Class used for storing results from AMS calculations. The class is functionally a dictionary, but allows dot notation to access variables in the dictionary. The class works case-insensitively, but will retain the case of the key when it was first set.''' def __getitem__(self, key): self.__set_empty(key) val = super().__getitem__(self.__get_case(key)) From 281995411a949471ecca2bc464323862fca738d5 Mon Sep 17 00:00:00 2001 From: YHordijk <42876712+YHordijk@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:50:52 +0200 Subject: [PATCH 6/6] Shortened class docstring --- TCutility/results/result.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TCutility/results/result.py b/TCutility/results/result.py index a45d86d0..14bcbf0f 100644 --- a/TCutility/results/result.py +++ b/TCutility/results/result.py @@ -2,7 +2,8 @@ class Result(dict): - '''Class used for storing results from AMS calculations. The class is functionally a dictionary, but allows dot notation to access variables in the dictionary. The class works case-insensitively, but will retain the case of the key when it was first set.''' + '''Class used for storing results from AMS calculations. The class is functionally a dictionary, but allows dot notation to access variables in the dictionary. + The class works case-insensitively, but will retain the case of the key when it was first set.''' def __getitem__(self, key): self.__set_empty(key) val = super().__getitem__(self.__get_case(key))