python - 包含类的动态填充字典的自动完成?
问题描述
我有一个 GUI,通过它我加载了一些数据。加载文件时,其文件名用作标识符,填充 GUI 和下面的字典,以跟踪每个文件的当前状态。
但是,使用这种方法,我无法从MetaData
课堂上获得任何自动完成功能,例如当我想访问data.container.[GUIcurrentFile].one_of_many_attributes
.
有没有解决的办法?也许以完全不同的方式将文件保存在内存中?人们在这种情况下通常会做什么?我对GUI开发不太熟悉。
class Data:
def __init__(self):
self.container = dict()
def load(self, name):
self.container[name] = MetaData()
class MetaData:
def __init__(self):
self.one_of_many_attributes = None
# This is instantiated in the main part of the GUI, e.g. self.data = Data()
data = Data()
## Series of events happening through the GUI
# Grab loaded file through a GUI
GUIcurrentFile = "file1"
data.load(GUIcurrentFile)
GUIcurrentFile = "file2"
data.load(GUIcurrentFile)
# Each file has separate attributes
data.container[GUIcurrentFile].one_of_many_attributes = "foo"
# File is removed from GUI, and can easily be removed from dictionary internally
data.container.pop(GUIcurrentFile)
解决方案
好的,所以类型提示终于为我点击了。我希望原始标题与此答案有关。否则,请随时编辑它。
首先定义 MetaData,如果实现方法以返回“MetaData”类型的对象,则为 PyCharm 添加类型提示非常简单。
class MetaData:
def __init__(self):
self.foo = None
self.really_long_name = None
class Data:
def __init__(self):
self.container = dict()
def load(self, name):
self.container[name] = MetaData()
def get(self, name) -> MetaData: # specify what dict lookup returns
return self.container[name]
data = Data()
data.load("file1")
data.get("file1").foo
推荐阅读
- vue.js - Vue.js v3 中的多个应用程序使用 vue-router
- java - C++ 到 Java。从字节数组中获取结果
- haskell - Haskell wai 中间件 - 如果从第一个应用程序返回 404,如何运行应用程序?
- angular - 角度:通过动态地图键绑定到地图的 2 种方式
- c# - 为 Service Fabric 应用程序类编写单元测试
- python-3.x - python3.x中的日期时间模块
- laravel - 如何在 Laravel 中实现授权(Guard),而不使用 make:auth 命令
- php - 为什么没有创建项目?
- rest - 通过 https 使用 kubeconfig 提供 Kubernetes 集群身份验证
- html - 如何防止标签重定向用户,同时保持其 href 属性?