Tag: 模块

Python:logging模块 – 全局

嘿,我想知道如何实现一个全球logging器,可以在任何地方使用自己的设置: 我有 class customLogger(logging.Logger): … 在与格式化程序和其他东西的文件。 logging器完全依靠自己的工作。 我在我的main.py文件中导入这个模块并创build一个像这样的对象: self.log = log.customLogger(arguments) 但显然我不能从我的代码的其他部分访问这个对象。 我用错了吗? 有一个更好的方法吗?

'__import __('pkg_resources')。declare_namespace(__ name __)`是做什么的?

在一些模块的__init__.py文件中,我看到了这样一行: __import__('pkg_resources').declare_namespace(__name__) 它做了什么,为什么人们使用它? 假设它在运行时与dynamic导入和创build名称空间有关。

在ES6模块中导出多个类

我试图创build一个导出多个ES6类的模块。 假设我有以下目录结构: my/ └── module/ ├── Foo.js ├── Bar.js └── index.js Foo.js和Bar.js分别导出一个默认的ES6类: // Foo.js export default class Foo { // class definition } // Bar.js export default class Bar { // class definition } 我目前有我的index.js像这样设置: import Foo from './Foo'; import Bar from './Bar'; export default { Foo, Bar, } 但是,我无法导入。 我希望能够做到这一点,但没有find类: import {Foo, Bar} from […]

ansible:如何传递多个命令

我试过这个: – command: ./configure chdir=/src/package/ – command: /usr/bin/make chdir=/src/package/ – command: /usr/bin/make install chdir=/src/package/ 这工作,但我想有更多的东西..整洁。 所以我试过这个: 从: https : //stackoverflow.com/questions/24043561/multiple-commands-in-the-same-line-for-bruker-topspin哪给我回“没有这样的文件或目录” – command: ./configure;/usr/bin/make;/usr/bin/make install chdir=/src/package/ 我也试过: https : //u.osu.edu/hasnan.1/2013/12/16/ansible-run-multiple-commands-using-command-module-and-with-items/ 但我找不到正确的语法: – command: "{{ item }}" chdir=/src/package/ with_items: ./configure /usr/bin/make /usr/bin/make install 这不行,说有报价问题。 任何人?

Python的方式来克隆一个Git仓库

有没有使用一个subprocess克隆git仓库的Python方式? 我可以使用任何你推荐的模块。

rubymodule_function与包括模块

在ruby中,我明白模块的function可以通过使用module_function在模块中混合使用,如下所示。 我可以看到这是如何有用,所以你可以使用该function,而不需要在模块中混合。 module MyModule def do_something puts "hello world" end module_function :do_something end 我的问题是,为什么你可能想要定义这两种方式的function。 为什么不只是有 def MyModule.do_something 要么 def do_something 在什么样的情况下将这个function混合在一起,或者作为一个静态的方法来使用呢?

Python:如何从一个函数进行全局导入

我担心这是一个混乱的方式来解决这个问题,但… 假设我想根据一些条件在Python中进行一些导入。 为此我想写一个函数: def conditional_import_modules(test): if test == 'foo': import onemodule, anothermodule elif test == 'bar': import thirdmodule, and_another_module else: import all_the_other_modules 现在,我怎样才能有import模块全球可用? 例如: conditional_import_modules(test='bar') thirdmodule.myfunction()

Python:在其中的模块和类之间共享全局variables

我知道可以在Python中跨模块共享一个全局variables。 但是,我想知道这是可能的程度,为什么。 例如, global_mod.py x = None mid_access_mod.py from global_mod import * class delta: def __init__(self): print x bot_modif_mod.py import mid_access_mod import global_mod class mew: def __init__(self): global_mod.x = 5 def main(): m = mew() d = mid_access_mod.delta() 这将打印None,即使所有模块共享全局variablesx。 这是为什么? 在me_m()被分配到bot_modif_mod.py之前,似乎x在mid_access_mod.py处被评估。

业力开始找不到模块'jasmine-core'

当我运行“业力开始”时,我得到以下错误: module.js:340抛出err; ^错误:在init.asmine(/ usr / lib / node_modules)的Function.require.resolve(module.js:384:19)处的Function.Module._resolveFilename(module.js:338:15)处找不到模块'jasmine-core' /karma-jasmine/lib/index.js:8:42)Array.invoke [as 0](/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)at get( /usr/lib/node_modules/karma/node_modules/di/lib/injector.js:48:43)在/usr/lib/node_modules/karma/lib/server.js:137:20 Array.forEach(native)at Server._start(/usr/lib/node_modules/karma/lib/server.js:136:21)在调用(/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)at Server.start(/usr/lib/node_modules/karma/lib/server.js:101:18)在Object.exports.run(/usr/lib/node_modules/karma/lib/cli.js:231:26)at目的。 (module.js:474:10)在Module._compile(module.js:456:26)处的/ usr / lib / node_modules / karma / bin / Module.load(module.js:356:32)在Function.Module._load(module.js:312:12) 有谁知道这是为什么发生? 我在虚拟机中运行业力。 我尝试了几个npm安装命令,没有任何工作。 在我关机后的第二天,问题就消失了,一切都恢复正常了。 所以,我不再被阻止..任何人都有可能发生什么想法?

Pythonic组织模块和软件包的方式

我来自一个背景,我通常每个class级创build一个文件。 我也在目录下组织普通的类。 这种做法对我来说很直观,已被certificate在C ++,PHP,JavaSript等方面是有效的。 我很难把这个隐喻带入Python:文件不再是文件,而是正式的模块。 在一个模块中只有一个类是不正确的 – 大多数类都是自己无用的。 如果我有一辆automobile.py ,一辆Automobile ,总是把它称为automobile.Automobile , automobile.Automobile也是很愚蠢的。 但是,与此同时,将一吨代码放入一个文件并称之为一天似乎并不合适。 显然,一个非常复杂的应用程序应该有超过5个文件。 什么是正确的或pythonic的方式? (或者如果没有正确的方法,你最喜欢的方式是什么?为什么?)我应该在Python模块中投入多less代码?