簡體   English   中英

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

[英]How to make Class instance in main.py available in another file?

我在需要在另一個文件的main.py中實例化的類中提供信息時遇到了困難。 可以在下面的流程圖中找到描述我要執行的操作的最佳方法:

程序流程圖

您可以想象的問題是循環依賴。 有沒有辦法在schema.pymain.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

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()

干杯,

布賴恩

我知道這不一定是您需要的答案。 但是我遇到了同樣的問題,對我來說,這意味着我把項目的結構安排錯了。 意味着main.py或schema.py會執行它們不打算執行的操作。 當然,您進行了該項目,因此您可以決定做什么,但是我的意思是也許您應該進一步抽象。 由於我不太了解這些庫,因此我不太了解您要如何處理這些代碼。

一個簡單的事情就是創建另一個可能是run.py的文件,然后將文件和依賴項-main導入到架構中,或者反過來。 另一個不太好的解決方案是創建一個init()函數,然后在初始化其余文件后再導入另一個文件,從而確保導入僅執行一次。

但是要做的是從概念上檢查為什么main需要導入(在您的情況下)架構,然后問自己這真的是main.py應該做什么。 例如,如果您的主要用戶需要為所有其他模塊提供模板,那么為什么不創建template.py或modules.py? 或者在我看來更好的是創建一個總線系統。 這可以允許模塊僅在需要時共享所需的數據,並公開通用的api。 但是,當然,只有在您共享信息的情況下,這才有意義。

結論:通常,當應用程序設計良好時,您永遠不會遇到循環導入的情況。 當您這樣做時,這表明您應該重新考慮如何構造程序。

對於Python函數,您可以執行以下操作。

def functionInSchema(objectParameter):
    # Add in code

這將使您可以從main.py文件訪問該對象。 要訪問屬性/方法,請使用參數名稱,點和屬性/函數名稱。

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

然后在main.py ,將Class實例(對象)作為參數傳遞。

objectInMain = Class(param1, param2)

functionInSchema(objectInMain)

如果需要訪問schema.py的類, schema.py可以使用以下導入。

from main.py import ClassName

希望對您有所幫助! 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM