Tag: python

如何从子类调用基类的__init__方法?

如果我有一个Python类: class BaseClass(object): #code and the init function of the base class 然后我定义一个孩子类,如: class ChildClass(BaseClass): #here I want to call the init function of the base class 如果基类的初始化函数需要一些参数,我将它们作为子类的初始化函数的参数,那么如何将这些parameter passing给基类呢? 我写的代码是: class Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg class ElectricCar(Car): def __init__(self, battery_type, model, […]

python NameError:未定义全局名称__file__

当我在Python 2.7中运行这个代码时,我得到这个错误: Traceback (most recent call last): File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 30, in <module> long_description = read('README.txt'), File "C:\Python26\Lib\site-packages\pyutilib.subprocess-3.5.4\setup.py", line 19, in read return open(os.path.join(os.path.dirname(__file__), *rnames)).read() NameError: global name '__file__' is not defined 代码是: import os from setuptools import setup def read(*rnames): return open(os.path.join(os.path.dirname(__file__), *rnames)).read() setup(name="pyutilib.subprocess", version='3.5.4', maintainer='William E. Hart', maintainer_email='wehart@sandia.gov', url = 'https://software.sandia.gov/svn/public/pyutilib/pyutilib.subprocess', license = […]

SQL炼金术 – 获取表的列表

在文档中我找不到任何有关这方面的信息,但是如何获得在SQLAlchemy中创build的表的列表? 我使用类方法来创build表。

Pythonparsing时意外的EOF

这是我的Python代码。 有人可以告诉我它有什么问题吗? while 1: date=input("Example: March 21 | What is the date? ") if date=="June 21": sd="23.5° North Latitude" if date=="March 21" | date=="September 21": sd="0° Latitude" if date=="December 21": sd="23.5° South Latitude" if sd: print sd 以下是发生的事情: >>> Example: March 21 | What is the date? Traceback (most recent call last): File "C:\Users\Daniel\Desktop\Solar Declination […]

如何在Python中每60秒asynchronous执行一次函数?

我想在Python上每60秒执行一个函数,但是我不想在此期间被阻塞。 我怎样才能asynchronous做到这一点? import threading import time def f(): print("hello world") threading.Timer(3, f).start() if __name__ == '__main__': f() time.sleep(20) 使用此代码,函数f在20秒内每3秒执行一次。 最后它给出了一个错误,我认为这是因为threading.timer还没有被取消。 我怎样才能取消它? 提前致谢!

用给定的(数字)分布生成随机数字

我有一个文件,有不同的值的概率,例如: 1 0.1 2 0.05 3 0.05 4 0.2 5 0.4 6 0.2 我想用这个分布生成随机数。 处理这个的现有模块是否存在? 编写自己的代码非常简单(构build累积密度函数,生成一个随机值[0,1]并select相应的值),但似乎这应该是一个常见问题,可能有人创build了一个函数/模块它。 我需要这个,因为我想生成一个生日的列表(不遵循标准random模块中的任何分布)。

使用SciPy的分位数分位数图

你将如何使用Python创build一个qq图? 假设您有一大组测量值并正在使用一些以XY值作为input的绘图函数。 这个函数应该把测量的分位数与相应的分布(正常的,统一的)的分位数进行比较。 由此产生的情节让我们接着评估我们的测量是否遵循假定的分布。 http://en.wikipedia.org/wiki/Quantile-quantile_plot R和Matlab都为此提供了现成的函数,但是我想知道在Python中实现的最干净的方法是什么。

在python中从内部类访问外部类

我有这样的情况 class Outer(object): def some_method(self): # do something class Inner(object): def __init__(self): self.Outer.some_method() # <– this is the line in question 我如何从Inner类访问Outer类的方法? 编辑 – 感谢您的答复。 我得出结论,我需要重新评估我是如何devise这个实现的,并提出一个更强大的方法。

UnicodeDecodeError:'utf8'编解码器无法解码位置3-6中的字节:无效的数据

unicode的东西如何在python2上工作? 我只是没有得到它。 在这里我从服务器下载数据并parsing为JSON。 Traceback (most recent call last): File "/usr/local/lib/python2.6/dist-packages/eventlet-0.9.12-py2.6.egg/eventlet/hubs/poll.py", line 92, in wait readers.get(fileno, noop).cb(fileno) File "/usr/local/lib/python2.6/dist-packages/eventlet-0.9.12-py2.6.egg/eventlet/greenthread.py", line 202, in main result = function(*args, **kwargs) File "android_suggest.py", line 60, in fetch suggestions = suggest(chars) File "android_suggest.py", line 28, in suggest return [i['s'] for i in json.loads(opener.open('https://market.android.com/suggest/SuggRequest?json=1&query='+s+'&hl=de&gl=DE').read())] File "/usr/lib/python2.6/json/__init__.py", line 307, in loads return _default_decoder.decode(s) […]

TypeError:缺less1位置参数:“self”

我是python的新手,并且碰到了一堵墙。 我跟着几个教程,但不能通过错误: Traceback (most recent call last): File "C:\Users\Dom\Desktop\test\test.py", line 7, in <module> p = Pump.getPumps() TypeError: getPumps() missing 1 required positional argument: 'self' 我审查了几个教程,但似乎没有什么不同于我的代码。 我能想到的唯一的事情是Python 3.3需要不同的语法。 主要scipt: # test script from lib.pump import Pump print ("THIS IS A TEST OF PYTHON") # this prints p = Pump.getPumps() print (p) 泵类: import pymysql class Pump: […]