草庐IT

task_scheduler_init

全部标签

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 - 什么方法在 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 - 操作系统 python 升级后,virtualenv python 失败,出现“ undefined symbol : _PyLong_AsInt¨ error on simple tasks

我有一个基于python-2.7.3的长期运行的virtualenv。在接受了推荐的平台操作系统(Ubuntu)更新后(在许多其他更改中)将python提升到2.7.6,virtualenv中的python已经开始在基本上所有重要任务上出错,堆栈结尾如下:ImportError:/home/myusername/ENVS/myvenv/lib/python2.7/lib-dynload/_io.so:undefinedsymbol:_PyLong_AsInt即使pipfreeze也会因这样的错误而失败——这使得它甚至无法在损坏的virtualenv中获得已安装软件包的准确list(可能

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 - Celery autodiscover_tasks 不适用于所有 Django 1.7 应用程序

我有一个Django1.7项目和Celery3.1。我的Django项目中的所有应用程序都使用新的AppConfig。问题是并不是所有的任务都可以通过autodiscover_tasks找到:app.autodiscover_tasks(settings.INSTALLED_APPS)如果我像这样使用autodiscover_tasks它将起作用:app.autodiscover_tasks(settings.INSTALLED_APPS+('apps.core','apps.sales'))找到网站中定义的任务,但找不到核心和销售中的任务。所有的布局都与apps.py和tasks.p

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

Python 日志记录 : Why is __init__ called twice?

我正在尝试将python日志记录与配置文件和自己的处理程序结合使用。这在某种程度上起作用。真正让我困惑的是__init__被调用了两次而__del__被调用了一次。当我删除整个配置文件内容并直接在代码中创建处理程序时,__init__被调用一次,而__del__永远不会被调用。我的问题:为什么__init__被调用了两次?为什么__del__的调用频率低于__init__?代码:#!/bin/envpythonimportloggingimportlogging.handlersimportlogging.configclassTest1TimedRotatingFileHandler