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

问题描述:

我有困难,我需要在main.py在另一个文件中实例化的类中提供信息。最好的办法来形容什么,我试图做可以在下面的流程图中可以看出:如何让main.py中的Class实例在另一个文件中可用?

Program flow diagram

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

谢谢你的时间和任何帮助,你可以提供!

EDIT:增加了对参考

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) 
代码3210

main.py

import sys 

from twisted.internet import reactor 
from twisted.web.resource import Resource 
from autobahn.twisted.resource import WebSocketResource 

from ws_transport import WsProtocol, WsProtocolFactory 
from web_transport import HttpResource, LoginResource, RefreshResource, HttpFactory 


if __name__ == '__main__': 
    factory = WsProtocolFactory() 
    factory.protocol = WsProtocol 
    ws_resource = WebSocketResource(factory) 

    root = Resource() 
    root.putChild("", HttpResource()) 
    root.putChild("login", LoginResource()) 
    root.putChild("refresh", RefreshResource()) 
    root.putChild(b"ws", ws_resource) 

    site = HttpFactory(root) 

    reactor.listenTCP(8000, site) 

    reactor.run() 

干杯,

布赖恩

+0

您可以将对象作为参数传递给函数。 –

+0

你能提供一个关于如何解决这个问题的伪代码的例子吗?谢谢彼得! – Brian

+0

我不熟悉你的“流程”图 - 我们可以假设箭头暗示某种*交流* ?.您可以在您的问题中添加相关的import语句在文件中的样子吗? – wwii

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

一个简单的黑客事情就是创建另一个名为maybe run.py的文件,然后将这两个文件和dependency-injects main导入到模式或其他方式。 另一个不太好的解决方案是创建一个init()函数,然后在其他文件初始化后导入另一个文件,从而确保导入只执行一次。

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

结论:通常,当应用程序设计合理时,您永远不会遇到循环导入。当你这样做时,表明你应该重新考虑你如何构建你的程序。

+1

谢谢@Pixdigit对您的非常好的回应。总之,我认为你很好地解释了我遇到的问题是由于设计问题。当我需要时,我需要一个API来共享我想要的信息,我将解决这个问题,然后报告回来!再次感谢 – Brian

对于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 

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