草庐IT

utility-method

全部标签

python - Kaggle 类型错误 : slice indices must be integers or None or have an __index__ method

我正在尝试在Kaggle上绘制seaborn直方图笔记本这样:sns.distplot(myseries,bins=50,kde=True)但是我得到这个错误:TypeError:sliceindicesmustbeintegersorNoneorhavean__index__method这是Kaggle笔记本:https://www.kaggle.com/asindico/slice-indices-must-be-integers-or-none/这是系列头:058500001600000025700000313100000416331452Name:price_doc,dtype

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

Pythonic 方式 : Utility functions in class or module

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭8年前。Improvethisquestion我是一名Python初学者,想知道编写实用函数的更多Pythonic方式是什么?与在Java/C++中一样,创建一个实用程序类并在其中包含方法或在模块内编写函数?该函数将在同一模块中的各个类中使用。模块中不同类和函数使用的变量的相同问题。我可以将它们放在实用程序类中或将它们定义在模块中。什么更像python?有人请指导我。我支持在类中编写它们的唯一论点是它使它更面向对象。

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 - smtplib 导入 email.utils 错误

当我尝试在我的Python代码中使用smtplib时出现以下错误。Traceback(mostrecentcalllast):File"myemail.py",line1,inimportsmtplibFile"/usr/lib64/python2.7/smtplib.py",line46,inimportemail.utilsImportError:Nomodulenamedutils令人惊讶的是,当我直接从Python解释器工作时,我可以包含该库。之前该文件被命名为'email.py',但根据stackoverflow上关于类似问题的回答,我将名称更改为'myemail.py'。它

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 - 什么相当于 scala.util.Try 在 pyspark 中?

我有一个糟糕的HTTPDaccess_log,只想跳过“糟糕”的行。在scala中这很简单:importscala.util.Tryvallog=sc.textFile("access_log")log.map(_.split('')).map(a=>Try(a(8))).filter(_.isSuccess).map(_.get).map(code=>(code,1)).reduceByKey(_+_).collect()对于python,我通过使用“lambda”表示法显式定义一个函数来获得以下解决方案:log=sc.textFile("access_log")defwrapExc

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