更新默认字典,添加新的键和值。

huangapple go评论73阅读模式
英文:

updating default dictionary with new key and value

问题

以下是您要翻译的内容:

我需要在使用它在不同脚本中显示在GUI中之前将一些键和值添加到默认字典中

class MyClass:
    def __init__(self):
        self.my_dict = { 
               'dict1' : {
                   'col1' : value1,
                   'col2' : value2,
                   ...
               },
               'dict2' : {
                   'col1' : value1,
                   'col2' : value2,
               }
               ...

    def set_attribute(self, dict, col, val)
        self.my_dict[dict][col] = val

    def set_attributes(self, dict, **kwargs):
        for col, val in kwargs.items():
            self.set_attribute(dict, col, val)

    def get_attribute(self, dict, col):
        if dict in self.my_dict:
            return self.my_dict[dict].get(col)
        return None

if __name__ == '__main__':
    my_obj = MyClass()
    value = my_obj.my_dict.get_attribute('dict', 'col1') * .011
    my_obj.my_dict.set_attribute('dict1', 'col3', value)
这段代码用于更新字典以包含新的列名和值但当我从另一个脚本运行它时我只获得默认字典而不是更新后的字典

main.py

from PyQt5 import QtWidgets
from dictclass import MyClass

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    print(myclass_obj.my_dict)

if __name__ == '__main__':
    import sys
    myclass_obj = MyClass()
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

请注意,我已经将代码中的HTML实体(例如')替换为普通的单引号(')以进行更清晰的翻译。

英文:

I need to add to a default dictionary some keys and values before using it in a different script to display it in a GUI.


class MyClass:
    def __init__(self):
        self.my_dict = { 
               'dict1' : {
                   'col1' : value1,
                   'col2' : value2,
                   ...
               },
               'dict2' : {
                   'col1' : value1,
                   'col2' : value2,
               }
               ...

    def set_attribute(self, dict, col, val)
        self.my_dict[dict][col] = val

    def set_attributes(self, dict, **kwargs):
        for col, val in kwargs.items():
            self.set_attribute(dict, col, val)

    def get_attribute(self, dict, col):
        if dict in self.my_dict:
            return self.my_dict[dict].get(col)
        return None

if __name__ == '__main__':
    my_obj = MyClass()
    value = my_obj.my_dict.get_attribute('dict', 'col1') * .011
    my_obj.my_dict.set_attribute('dict1', 'col3', value)

This works for updating the dictionary to contain the new col name and value but when I run it from my other script I only get the default dictionary and not the updated one.

main.py


from PyQt5 import QtWidgets
from dictclass import MyClass

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
    super(MainWindow, self).__init__(parent)
    print(myclass_obj.my_dict)

if __name__ == '__main__':
    import sys
    myclass_obj = MyClass()
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
    

答案1

得分: 0

我能根据Tim Roberts的评论解决了这个问题。

我在myclass.py中添加了这个方法:

def update_dictionary(self):
    value = self.get_attribute('dict', 'col1') * 0.011
    self.set_attribute('dict1', 'col3', value)

在main.py中我调用了它:

class MainWindow():
    ...
        self.myclass_obj = myclass_obj
        self.myclass_obj.update_dictionary()
        self.myclass_obj.my_dict
英文:

I was able to solve this based on Tim Roberts comment

I added to myclass.py this method


def update_dictionary(self):
    value = self.get_attribute('dict', 'col1') * .011
    self.set_attribute('dict1', 'col3', value)

within main.py I called it.


class MainWindow():
    ...
        self.myclass_obj = myclass_obj
        self.myclass_obj.update_dictionary()
        self.myclass_obj.my_dict

huangapple
  • 本文由 发表于 2023年7月18日 02:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定