我最近一直在使用nodejs并且仍然掌握模块系统,所以如果这是一个显而易见的问题,请道歉.我想要的代码大致如下所示:
a.js(主节点与节点一起运行)
var ClassB = require("./b");
var ClassA = function() {
this.thing = new ClassB();
this.property = 5;
}
var a = new ClassA();
module.exports = a;
Run Code Online (Sandbox Code Playgroud)
b.js
var a = require("./a");
var ClassB = function() {
}
ClassB.prototype.doSomethingLater() {
util.log(a.property);
}
module.exports = ClassB;
Run Code Online (Sandbox Code Playgroud)
我的问题似乎是我无法从ClassB的实例中访问ClassA的实例.
是否有正确/更好的方法来构建模块以实现我想要的?有没有更好的方法在模块之间共享变量?
本clojure.spec指南中的一个示例是一个简单的选项解析规范:
(require '[clojure.spec :as s])
(s/def ::config
(s/* (s/cat :prop string?
:val (s/alt :s string? :b boolean?))))
(s/conform ::config ["-server" "foo" "-verbose" true "-user" "joe"])
;;=> [{:prop "-server", :val [:s "foo"]}
;; {:prop "-verbose", :val [:b true]}
;; {:prop "-user", :val [:s "joe"]}]
Run Code Online (Sandbox Code Playgroud)
稍后,在验证部分中,定义了一个函数,该函数conform使用此规范在内部输入:
(defn- set-config [prop val]
(println "set" prop val))
(defn configure [input]
(let [parsed (s/conform ::config input)]
(if (= parsed ::s/invalid)
(throw (ex-info "Invalid input" (s/explain-data ::config input)))
(doseq [{prop …Run Code Online (Sandbox Code Playgroud) namespaces clojure destructuring cyclic-dependency clojure.spec
我有一个中央包,提供其他包依赖的几个接口(让我们调用一个Client).那些其他包提供了那些第一个接口(UDPClient,TCPClient)的几个实现.我Client通过调用NewClient中央包来实例化,并从一个依赖包中选择并调用相应的客户端实现.
当我想告诉中央包关于那些其他包时,它就会崩溃,所以它知道它可以创建什么客户端.这些依赖的客户端实现也导入中央包,创建Go不允许的循环依赖.
前进的最佳方式是什么?我不想将所有这些实现混合在一个包中,并且创建一个单独的注册表包似乎有点过分.目前,我将每个实现注册本身与中央包一起注册,但这要求用户知道在每个使用客户端的单独二进制文件中导入每个实现.
import (
_ udpclient
_ tcpclient
client
)
Run Code Online (Sandbox Code Playgroud) 试图找到一个好的和正确的模式来处理Python中的循环模块依赖.通常,解决方案是将其删除(通过重构); 但是,在这种特殊情况下,我们真的希望拥有需要循环导入的功能.
编辑:根据下面的答案,这种问题的通常攻角是重构.但是,为了这个问题,假设这不是一个选项(无论出于何种原因).
问题:
该logging模块要求configuration模块提供一些配置数据.但是,对于某些configuration功能,我真的想使用logging模块中定义的自定义日志记录功能.显然,导入logging模块configuration会引发错误.
我们可以想到的可能的解决方案:
不要这样做.正如我之前所说,这不是一个好的选择,除非所有其他可能性都是丑陋和糟糕的.
猴子补丁模块.这听起来不是太糟糕了:在加载logging模块动态进入configuration 后的初始导入,和之前的任何职能被实际使用.这意味着定义全局的每模块变量.
依赖注入.我已阅读并尝试依赖注入替代方案(特别是在Java Enterprise空间中),它们消除了一些令人头痛的问题; 然而,它们可能太复杂而无法使用和管理,这是我们想要避免的.但是,我不知道Python中的全景图是怎样的.
什么是启用此功能的好方法?
非常感谢!
python module cyclic-reference importerror cyclic-dependency
假设我写下面的代码:
一个游戏模块
module Game where
import Player
import Card
data Game = Game {p1 :: Player,
p2 :: Player,
isP1sTurn :: Bool
turnsLeft :: Int
}
Run Code Online (Sandbox Code Playgroud)
一个播放器模块
module Player where
import Card
data Player = Player {score :: Int,
hand :: [Card],
deck :: [Card]
}
Run Code Online (Sandbox Code Playgroud)
和卡模块
module Card where
data Card = Card {name :: String, scoreValue :: Int}
Run Code Online (Sandbox Code Playgroud)
然后我写了一些代码来实现逻辑,玩家轮流从他们的手中抽取牌和牌来为他们的分数增加奖金,直到游戏结束.
但是,我意识到完成这段代码后,我写的游戏模块很无聊!
我想重构纸牌游戏,所以当你玩牌时,而不是仅仅添加一个分数,而是卡片任意改变游戏.
所以,我将Card模块更改为以下内容
module Card where
import Game
data Card = Card {name :: String,
onPlayFunction :: …Run Code Online (Sandbox Code Playgroud) 我在将依赖项注入拦截器时遇到问题。我想将 TranslateService 注入 HttpErrorInterceptor,但出现循环依赖错误。当我删除 TranslateService 注入时,一切正常。
\n我已在 app.module.ts 中声明了拦截器。\n我的应用程序模块如下所示:
\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n BrowserAnimationsModule,\n CoreModule,\n HttpClientModule,\n TranslateModule.forRoot({\n loader: {\n provide: TranslateLoader,\n useFactory: HttpLoaderFactory,\n deps: [HttpClient],\n },\n defaultLanguage: \'pl-pl\'\n }),\n AppRoutingModule,\n RouterModule,\n FormsModule,\n ReactiveFormsModule,\n ToastrModule.forRoot()\n ],\n providers: [\n {\n provide: HTTP_INTERCEPTORS,\n useClass: JwtInterceptor,\n multi: true\n },\n {\n provide: HTTP_INTERCEPTORS,\n useClass: HttpErrorInterceptor,\n multi: true,\n deps: [TranslateService, ToastrService]\n }\n ],\n bootstrap: [AppComponent]\n})\nexport class AppModule { }\nRun Code Online (Sandbox Code Playgroud)\n在 AppModule 中,我导入了 CoreModule,其中有一个包含拦截器的文件夹,我的 CoreModule 如下所示:
\n@NgModule({\n declarations: [],\n …Run Code Online (Sandbox Code Playgroud) 我的项目结构简单如下:
|- core.clj
|- dialogs.clj
|- dialogs/
|- name_dialog.clj
Run Code Online (Sandbox Code Playgroud)
name_dialog有依赖性core,core应该要求name_dialog.
所以我有这样的依赖:
(ns ddsl.core
(:gen-class)
(:require [clojure.xml :refer :all]
[ddsl.dialogs :refer :all]))
Run Code Online (Sandbox Code Playgroud)
(ns ddsl.dialogs
(:require [ddsl.core :refer :all]))
(load "dialogs/name_dialog")
Run Code Online (Sandbox Code Playgroud)
(in-ns 'ddsl.dialogs)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行该程序时,我收到以下错误
Cyclic load dependency: [ /ddsl/core ]->/ddsl/dialogs->[ /ddsl/core ]
请让我知道,如何重组我的项目(我是Clojure的新手).
我想有以下SBT构建设置:
object MyBuild extends Build {
lazy val core = Project("core", file("core"))
.dependsOn(testkit % "test")
lazy val testkit = Project("testkit", file("testkit"))
.dependsOn(core % "compile")
}
Run Code Online (Sandbox Code Playgroud)
什么core是主模块,包括域对象,并且testkit是一个模块,用于测试依赖于域对象和其他类/工具的支持代码(构建器,匹配器,测试驱动程序等; 而不是测试本身)core.
对于此设置SBT给出了一个Cyclic reference错误,虽然不是一个真正的循环依赖,因为使用不同配置(core编译,然后testkit根据汇总core,然后core test根据这两个编译).
我找到了一种通过替换其中一个dependsOn用途来解决这个问题的肮脏方法unmanagedClasspath,例如:
.settings(unmanagedClasspath in Compile <+= (packageBin in (LocalProject("core"), Compile)))
Run Code Online (Sandbox Code Playgroud)
这感觉就像一个黑客,并且还会sbt-idea生成不正确的IntelliJ项目(除其他外).
想要更好的解决方案吗?SBT是否支持这样的结构?
Pylint抱怨使用R0401错误代码循环导入NLTK包的特定文件,例如
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk -> nltk.internals)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.corpus -> nltk.tokenize -> nltk.tokenize.punkt -> nltk.probability)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.corpus -> nltk.tokenize -> nltk.tokenize.texttiling)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.draw.tree -> nltk.tree)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.tree -> nltk.treeprettyprinter)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.grammar -> nltk.parse.pchart)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.stem -> nltk.stem.porter)
nltk/nltk/ccg/lexicon.py:1: [R0401(cyclic-import), ] Cyclic import (nltk.classify.maxent -> nltk.classify.tadm)
Run Code Online (Sandbox Code Playgroud)
完整列表位于https://github.com/nltk/nltk/issues/2113
但看看进口:
from __future__ import unicode_literals from
import …Run Code Online (Sandbox Code Playgroud) 我正在尝试将APP_INITIALIZERAngular集成到我的项目中,以便在启动应用程序之前执行一些功能。当我在我的服务中使用来自 Angular的ActivatedRoute时,问题就出现了。
错误是:
Error: Provider parse errors:
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
Run Code Online (Sandbox Code Playgroud)
我想我在内部使用了两次导入或类似的东西。基本上我尝试了一些其他配置,但最后总是抛出同样的错误。
STACKBLITZ 示例: https ://stackblitz.com/edit/angular-bhpe7m
预期行为:只是为了能够通过 ActivatedRoute 服务检索一些 QueryParams 并在运行 Angular 应用程序之前使用它们执行一些功能