草庐IT

method-i-strftime

全部标签

python - "This inspection detects instance attribute definition outside __init__ method"派查姆

我正在使用以下类在firebase数据库中连接和创建游标:classFirebird:username="..."password="..."def__init__(self,archive):self.archive=archivedefconnect(self):try:self.connection=connect(dsn=self.archive,user=self.username,password=self.password)exceptError,e:print"Failedtoconnecttodatabase",eexit(0)PyCharm警告我:“此检查检测到in

python 3 : check if method is static

类似问题(与Python2相关:Python:checkifmethodisstatic)让我们考虑以下类定义:classA:deff(self):return'thisisf'@staticmethoddefg():return'thisisg'在Python3中没有instancemethod不再,一切都是函数,所以与Python2相关的答案将不再有效。正如我所说,一切都是函数,所以我们可以调用A.f(0),但我们当然不能调用A.f()(参数不匹配)。但是如果我们创建一个实例a=A()我们调用a.f()Python传递给函数A.fself作为第一个参数。打电话a.g()阻止发送或捕

python : why a method from super class not seen?

我正在尝试实现我自己的DailyLogFile版本fromtwisted.python.logfileimportDailyLogFileclassNDailyLogFile(DailyLogFile):def__init__(self,name,directory,rotateAfterN=1,defaultMode=None):DailyLogFile.__init__(self,name,directory,defaultMode)#whydonotusesuper.here?lisibilitymaybe?#self.rotateAfterN=rotateAfterNdefsh

python3 : bind method to class instance with . __get__(),它有效,但为什么呢?

我知道如果你想给一个类实例添加一个方法你不能像这样做一个简单的赋值:>>>defprint_var(self):#methodtobeaddedprint(self.var)>>>classMyClass:var=5>>>c=MyClass()>>>c.print_var=print_var这确实会导致print_var表现得像一个普通函数,所以self参数不会有他的典型含义:>>>c.print_var>>>c.print_var()Traceback(mostrecentcalllast):File"",line1,inc.print_var()TypeError:print_va

python - Django 1.2 : How to connect pre_save signal to class method

我试图在我的django1.2项目的某些类中定义一个“before_save”方法。我在将信号连接到models.py中的类方法时遇到问题。classMyClass(models.Model):....defbefore_save(self,sender,instance,*args,**kwargs):self.test_field="Itworked"我尝试将pre_save.connect(before_save,sender='self')放入“MyClass”本身,但没有任何反应。我也试过把它放在models.py文件的底部:pre_save.connect(MyClass.

python - 索引错误 : tuple index out of range when parsing method arguments

我已经检查过this问题,但在那里找不到答案。这是一个演示我的用例的简单示例:deflog(*args):message=str(args[0])arguments=tuple(args[1:])#messageitselfprint(message)#argumentsforstr.format()0print(arguments)#showsthatargumentshavecorrectindexesforindex,valueinenumerate(arguments):print("{}:{}".format(index,value))#andamountofplacehol

jquery - 在 flask 中检查 request.method 时出错

我目前正在学习Flask。使用jQuery通过$.ajax()发送数据后,type='post'当我检查request.method。type='get'也是如此。错误builtins.ValueErrorValueError:ViewfunctiondidnotreturnaresponseTraceback(mostrecentcalllast)File"C:\Python33\lib\site-packages\flask\app.py",line1836,in__call__returnself.wsgi_app(environ,start_response)File"C:\P

python Pandas : Add column to grouped DataFrame with method chaining

首先让我说我是pandas的新手。我正在尝试在DataFrame中创建一个新列。我能够按照我的示例中所示执行此操作。但我想通过链接方法来做到这一点,所以我不必分配新变量。首先让我展示一下我想要实现的目标,以及到目前为止我做了什么:In[1]:importnumpyasnpfrompandasimportSeries,DataFrameimportpandasaspdIn[2]:np.random.seed(10)df=pd.DataFrame(np.random.randint(1,5,size=(10,3)),columns=list('ABC'))dfOut[2]:ABC22141

python - 在哪里保存常见的 strftime 字符串,如 ("%d/%m/%Y")

在我的应用程序中,我发现自己经常使用stftime,而且主要使用2种字符串格式-("%d/%m/%Y")和("%H:%M")我不想每次都写字符串,而是想将这些字符串存储在某个全局变量或其他东西中,这样我就可以在我的应用程序中的一个地方定义格式字符串。这样做的pythonic方式是什么?我应该使用全局字典、类、函数还是其他东西?大概是这样的?classTimeFormats():def__init__(self):self.date="%d/%m/%Y"self.time="%H:%M"还是这样?defhourFormat(item):returnitem.strftime("%H:%M

python - 抽象类的错误 "__init__ method from base class is not called"

我有classA(object):def__init__(self):raiseNotImplementedError("A")classB(A):def__init__(self):....和pylint说__init__methodfrombaseclass'A'isnotcalled很明显,我不想做super(B,self).__init__()那我该怎么办?(我尝试了abc并得到了Undefinedvariable'abstractmethod'来自pylint,因此这也不是一个选项)。 最佳答案 忽略pylint。它只是一