草庐IT

page-init

全部标签

python - 包 __init__.py 的相对导入

假设我有一个包含两个子模块的包以及__init__.py中的大量代码本身:pkg/__init__.pypkg/foo.pypkg/bar.py并且,为了使计划中的future重构更容易,我希望包的组件专门使用相对导入来相互引用。特别是,importpkg不应该出现。来自foo.py我能行from__future__importabsolute_importfrom.importbar访问bar.py模块,反之亦然。问题是,我要写什么来导入__init__.py以这种方式?我想要与importpkgaslocal_name完全相同的效果,只是不必指定绝对名称pkg.#importpkg

Python命令行程序: generate man page from existing documentation and include in the distribution

按照(希望如此)常见的做法,我有一个Python包,其中包括几个模块和一个可执行脚本,位于单独的scripts目录中,如here所示。.除了optparse自动生成的帮助外,脚本的文档与包文档一起位于Sphinx子目录中。我正在尝试:根据现有文档为脚本生成手册页在发行版中包含手册页我可以使用Sphinx、man_pages设置和sphinx-build-bman轻松完成#1。所以我可以调用pythonsetup.pybuild_sphinx-bman并在build/sphinx/man目录中生成手册页。现在我希望能够将生成的手册页包含在分发压缩包中,这样GNU/Linux打包程序就可以

python - 在导入的 __init__.py 中导入行为不同

__init__.py中的导入在文件运行时和导入时的行为似乎有所不同。如果我们有以下文件:run.py:importtesttest/b.py:classB(object):pass测试/__init__.py:frombimportBprintBprintb如果我们运行__init__.py,我们会得到一个错误,如我所料:%pythontest/__init__.pyTraceback(mostrecentcalllast):File"test/__init__.py",line6,inprintbNameError:name'b'isnotdefined但是如果我们run.py那么

python - 将 InitSpider 与启动画面 : only parsing the login page? 一起使用

这是oneIaskedearlier的后续问题.我正在尝试抓取一个必须先登录才能访问的网页。但是经过身份验证后,我需要的网页需要运行一点Javascript才能查看内容。我所做的是遵循说明here安装splash以尝试呈现Javascript。然而……在我切换到splash之前,使用Scrapy的InitSpider进行身份验证没问题。我正在通过登录页面并抓取目标页面OK(显然,没有Javascript工作除外)。但是一旦我添加代码以通过splash传递请求,看起来我没有解析目标页面。下面的蜘蛛。splash版本(此处)和非splash版本之间的唯一区别是函数defstart_requ

python - 什么方法在 Python 类中调用 `__init__()`

我想知道如何调用__init__()方法。__new__()调用它,还是__call__()在使用__new__()或其他方式创建实例后调用它? 最佳答案 Python判断是否__new__()应该调用__init__():If__new__()returnsaninstanceofcls,thenthenewinstance’s__init__()methodwillbeinvokedlike__init__(self[,...]),whereselfisthenewinstanceandtheremainingarguments

python - 模拟补丁不适用于 __init__.py 中的类

我正在尝试使用patch从一个方法中返回一个Mock。基本结构如下:MyCode.pyclassMyClass:def__init__(self,first_name,last_name):self.first=first_nameself.last=last_namedefget_greeting(self):return'Hello{f}{l}'.format(f=self.first,l=self.last)defget_new_greeting(first_name,last_name):obj=MyClass(first_name,last_name)returnobj.ge

python - 断言 __init__ 是用正确的参数调用的

我正在使用python模拟来断言特定对象是使用正确的参数创建的。这是我的代码的样子:classInstaller:def__init__(foo,bar,version):#Initstuffpassdef__enter__(self):returnselfdef__exit__(self,type,value,tb):#cleanuppassdefinstall(self):#InstallstuffpassclassDeployer:defdeploy(self):withInstaller('foo','bar',1)asinstaller:installer.install()

python - 如何延迟 __init__ 调用直到访问属性?

我有一个测试框架,需要使用以下类模式定义测试用例:classTestBase:def__init__(self,params):self.name=str(self.__class__)print('initializingtest:{}withparams:{}'.format(self.name,params))classTestCase1(TestBase):defrun(self):print('runningtest:'+self.name)当我创建并运行测试时,我得到以下信息:>>>test1=TestCase1('test1params')initializingtest

python - 使用 super 为多个父类调用 init?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:CanSuperdealwithmultipleinheritance?Python继承?我有一个类结构(如下),并希望子类调用parent双方的__init__。这有可能以“super”方式进行,还是只是一个糟糕的想法?classParent1(object):def__init__(self):self.var1=1classParent2(object):def_init__(self):self.var2=2classChild(Parent1,Parent2):def__init__(self):#

Python 抽象类应强制派生类在 __init__ 中初始化变量

我想要一个抽象类,它强制每个派生类在其__init__方法中设置某些属性。我查看了几个没有完全解决我的问题的问题,特别是here或here。This看起来很有前途,但我没能成功。我假设我想要的结果可能类似于以下伪代码:fromabcimportABCMeta,abstractmethodclassQuadrature(object,metaclass=ABCMeta):@someMagicKeyword#以下是我尝试过的一些方法:fromabcimportABCMeta,abstractmethodclassQuadrature(object,metaclass=ABCMeta):@p