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

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

updating default dictionary with new key and value

问题

以下是您要翻译的内容:

  1. 我需要在使用它在不同脚本中显示在GUI中之前将一些键和值添加到默认字典中
  2. class MyClass:
  3. def __init__(self):
  4. self.my_dict = {
  5. 'dict1' : {
  6. 'col1' : value1,
  7. 'col2' : value2,
  8. ...
  9. },
  10. 'dict2' : {
  11. 'col1' : value1,
  12. 'col2' : value2,
  13. }
  14. ...
  15. def set_attribute(self, dict, col, val)
  16. self.my_dict[dict][col] = val
  17. def set_attributes(self, dict, **kwargs):
  18. for col, val in kwargs.items():
  19. self.set_attribute(dict, col, val)
  20. def get_attribute(self, dict, col):
  21. if dict in self.my_dict:
  22. return self.my_dict[dict].get(col)
  23. return None
  24. if __name__ == '__main__':
  25. my_obj = MyClass()
  26. value = my_obj.my_dict.get_attribute('dict', 'col1') * .011
  27. my_obj.my_dict.set_attribute('dict1', 'col3', value)
  1. 这段代码用于更新字典以包含新的列名和值但当我从另一个脚本运行它时我只获得默认字典而不是更新后的字典
  2. main.py
  3. from PyQt5 import QtWidgets
  4. from dictclass import MyClass
  5. class MainWindow(QtWidgets.QMainWindow):
  6. def __init__(self, parent=None):
  7. super(MainWindow, self).__init__(parent)
  8. print(myclass_obj.my_dict)
  9. if __name__ == '__main__':
  10. import sys
  11. myclass_obj = MyClass()
  12. app = QtWidgets.QApplication(sys.argv)
  13. window = MainWindow()
  14. window.show()
  15. 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.

  1. class MyClass:
  2. def __init__(self):
  3. self.my_dict = {
  4. 'dict1' : {
  5. 'col1' : value1,
  6. 'col2' : value2,
  7. ...
  8. },
  9. 'dict2' : {
  10. 'col1' : value1,
  11. 'col2' : value2,
  12. }
  13. ...
  14. def set_attribute(self, dict, col, val)
  15. self.my_dict[dict][col] = val
  16. def set_attributes(self, dict, **kwargs):
  17. for col, val in kwargs.items():
  18. self.set_attribute(dict, col, val)
  19. def get_attribute(self, dict, col):
  20. if dict in self.my_dict:
  21. return self.my_dict[dict].get(col)
  22. return None
  23. if __name__ == '__main__':
  24. my_obj = MyClass()
  25. value = my_obj.my_dict.get_attribute('dict', 'col1') * .011
  26. 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

  1. from PyQt5 import QtWidgets
  2. from dictclass import MyClass
  3. class MainWindow(QtWidgets.QMainWindow):
  4. def __init__(self, parent=None):
  5. super(MainWindow, self).__init__(parent)
  6. print(myclass_obj.my_dict)
  7. if __name__ == '__main__':
  8. import sys
  9. myclass_obj = MyClass()
  10. app = QtWidgets.QApplication(sys.argv)
  11. window = MainWindow()
  12. window.show()
  13. sys.exit(app.exec_())

答案1

得分: 0

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

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

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

在main.py中我调用了它:

  1. class MainWindow():
  2. ...
  3. self.myclass_obj = myclass_obj
  4. self.myclass_obj.update_dictionary()
  5. self.myclass_obj.my_dict
英文:

I was able to solve this based on Tim Roberts comment

I added to myclass.py this method

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

within main.py I called it.

  1. class MainWindow():
  2. ...
  3. self.myclass_obj = myclass_obj
  4. self.myclass_obj.update_dictionary()
  5. 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:

确定