如何使main.py中的Class实例在另一个文件中可用?

时间:2017-08-23 21:11:20

标签: python design-patterns dependency-injection software-design circular-dependency

我很难在需要在另一个文件中FrameGrabber grabber = new FrameGrabber(videoFile); grabber.start(); BufferedImage image= null; while((image=grabber.grab())!=null){ // TODO set the image on the canvas or panel where ever you want. } grabber.stop(); 中实例化的类中提供信息。描述我想要做的事情的最佳方式可以在下面的流程图中看到:

Program flow diagram

您可以想象的问题是循环依赖。有没有办法在main.pyschema.py之间创建一个接口,以便我可以传递类信息?

感谢您的时间和任何帮助!

编辑:添加了参考代码

main.py

ws_transport.py

from autobahn.twisted.websocket import ( WebSocketServerProtocol, WebSocketServerFactory, ) from schema import schema class WsProtocol(WebSocketServerProtocol): def __init__(self): # Code here def onConnect(self, request): # Code here def onMessage(self, payload, isBinary): # Code here class WsProtocolFactory(WebSocketServerFactory): def __init__(self): super(WsProtocolFactory, self).__init__() self.connection_subscriptions = defaultdict(set) # Code here def check_events_db(self): # Code here def check_audit_log_db(self): # Code here

web_transport.py

import sys, os import json from twisted.web.resource import Resource from twisted.web.server import Site, http from schema import schema class HttpResource(Resource): isLeaf = True def render_OPTIONS(self, request): # Code here def render_GET(self, request): # Code here def render_POST(self, request): # Code here class LoginResource(Resource): isLeaf = True def render_OPTIONS(self, request): # Code here def render_GET(self, request): # Code here def render_POST(self, request): # Code here class RefreshResource(Resource): isLeaf = True def render_OPTIONS(self, request): # Code here def render_GET(self, request): # Code here def render_POST(self, request): # Code here class HttpFactory(Site): def __init__(self, resource): # Code here

schema.py

#!/usr/bin/python import graphene import json import sys, os from main import factory class Query(graphene.ObjectType): # Code here class Mutation(graphene.ObjectType): # Code here class Subscription(graphene.ObjectType): # Code here schema = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription)

main.py

干杯,

布赖恩

2 个答案:

答案 0 :(得分:1)

我知道这不一定是你需要的答案。但我遇到了同样的问题,对我而言,这意味着我将项目结构错误。意思是main.py或schema.py做他们不打算做的事情。当然你做了这个项目,所以你可以决定做什么,但我的意思是,你应该抽象更多。因为我不太了解你想用代码做什么,因为我不知道库。

一个简单的hacky类型的事情就是创建另一个名为maybe run.py的文件,然后将这两个文件和依赖项注入main导入到模式中,或者相反。 另一个不太好的解决方案是创建一个init()函数,然后在其余文件初始化之后导入另一个文件,从而确保导入只执行一次。

所做的是概念性地检查为什么主要需要导入(在你的情况下)模式,然后问自己这真的是main.py应该做的。如果是您需要为所有其他模块提供模板,那么为什么不创建templates.py或modules.py?或者在我的情况下更好的是创建一个总线系统。这可以允许模块仅在需要时共享所需的数据并公开一般API。但是,如果你只分享信息,这当然是有意义的。

总之:通常在应用程序设计良好时,您永远不会遇到循环导入。当你这样做时,你应该重新考虑如何构建你的程序。

答案 1 :(得分:0)

对于Python函数,你可以这样做。

def functionInSchema(objectParameter):
    # Add in code

这将允许您从main.py文件访问该对象。 要访问属性/方法,请使用参数名称,点和属性/函数名称。

def functionInSchema(objectParameter):
     attribute = objectParameter.attribute
     objectParameter.method()

然后在main.py中传递Class实例(Object)作为参数。

objectInMain = Class(param1, param2)

functionInSchema(objectInMain)

如果您需要访问schema.py中的课程,可以使用以下导入。

from main.py import ClassName

我希望这对你有所帮助!祝你好运!

相关问题