Created
June 16, 2023 22:13
-
-
Save jarobins/7578ee8485007b45e3698eca1ba6b355 to your computer and use it in GitHub Desktop.
Custom Dictionary for Parameter Organization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Custom Dictonary | |
import struct | |
class CustomDict(dict): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
# Additional initialization code if needed | |
self.ss = None | |
self.bdata = None | |
self.data = None | |
def __str__(self): | |
return "\n".join(["{}, {}".format(x,y) for x, y in self.items() if type(x) is not int]) | |
def overwrite(self, key1, key2): | |
# Key2s value overwrites Key1s value | |
super().__setitem__(key1, self[key2]) | |
def assign(self, key, pos): | |
# Assign the key to the pos key | |
super().__setitem__(key, self[pos]) | |
del self[pos] | |
def load_keys(self, ks): | |
# Load a set of keys | |
if type(ks) not in (tuple, list): | |
print(type(ks)) | |
return False | |
self.update([(x,None) for x in ks]) | |
return True | |
def load_string(self, string): | |
# Load a stuct string | |
self.ss = string | |
def load_bdata(self, data): | |
self.bdata = data | |
def dupdate(self): | |
# Update the dictionary with the loaded data and string | |
# Going to check the needed parameters | |
for key in self.keys(): | |
if type(key) is int: | |
del self[key] | |
if self.ss == None: | |
return False | |
if self.bdata == None: | |
return False | |
ss_size = struct.calcsize(self.ss) | |
bd_size = len(self.bdata) | |
if ss_size != bd_size: | |
return False | |
self.data = struct.unpack(self.ss, self.bdata) | |
self.update([(x,y) for x,y in zip(range(len(self.data)), self.data)]) | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment