我是python和线程的新手.我编写了python代码,它充当网络爬虫,并在网站上搜索特定的关键字.我的问题是,如何使用线程同时运行我的类的三个不同实例.当其中一个实例找到关键字时,所有三个实例都必须关闭并停止对Web进行爬网.这是一些代码.
class Crawler:
def __init__(self):
# the actual code for finding the keyword
def main():
Crawl = Crawler()
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
我如何使用线程让Crawler同时进行三次不同的抓取?
尽管至少有两个 关于如何在Python 库中索引DataFrame的好教程pandas,但我仍然无法找到一个优雅的方法来SELECT处理多个列.
>>> d = pd.DataFrame({'x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8]})
>>> d
x y
0 1 4
1 2 5
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2] # This works fine
x y
2 3 6
3 4 7
4 5 8
>>> d[d['x']>2 & d['y']>7] # I had expected this to work, but it doesn't
Traceback (most recent call last):
File "<stdin>", …Run Code Online (Sandbox Code Playgroud) 想象一下您想要继承的基类:
class Shape:
def __init__(self, x: float, y: float):
self.x = x
self.y = y
Run Code Online (Sandbox Code Playgroud)
在子类的方法中处理父类的 kwargs 似乎有两种常见的模式__init__。
您可以完全重述父级的界面:
class Circle(Shape):
def __init__(self, x: float, y: float, radius: float):
super().__init__(x=x, y=y)
self.radius = radius
Run Code Online (Sandbox Code Playgroud)
或者,您可以仅指定特定于子级的接口部分,并将剩余的 kwargs 交给父级__init__:
class Circle(Shape):
def __init__(self, radius: float, **kwargs):
super().__init__(**kwargs)
self.radius = radius
Run Code Online (Sandbox Code Playgroud)
这两者似乎都有很大的缺点,所以我很想听听什么是标准或最佳实践。
“重述接口”方法在玩具示例中很有吸引力,就像您在Python 继承的讨论中常见的那样,但是如果我们使用非常复杂的接口(例如pandas.DataFrameor )对某些东西进行子类化怎么办logging.Logger?
另外,如果父接口发生变化,我必须记住更改所有子类的接口以匹配、类型提示等。不是很干。
在这些情况下,您几乎肯定会选择该**kwargs选项。
但该**kwargs选项使用户不确定实际需要哪些参数。
在上面的玩具示例中,用户可能天真地写道:
circle = Circle() # Argument missing for parameter "radius"
Run Code Online (Sandbox Code Playgroud)
他们的 …
我尝试了以下但是我遇到了语法错误
ALTER TABLE Grades (
DROP COLUMN (Student_FamilyName, Student_Name),
ADD Student_id INT );
Run Code Online (Sandbox Code Playgroud)
是否有可能在同一声明中执行a DROP和a ?ADDALTER TABLE
我有一个看起来像这样的numpy数组:
[[41.743617 -87.626839]
[41.936943 -87.669838]
[41.962665 -87.65571899999999]]
Run Code Online (Sandbox Code Playgroud)
我想将数组中的数字四舍五入到两位小数,或三位.我尝试使用numpy.around和numpy.round,但它们都给我以下错误:
File "/Library/Python/2.7/site-packages/numpy-1.8.0.dev_3084618_20130514-py2.7-macosx-10.8-intel.egg/numpy/core/fromnumeric.py", line 2452, in round_
return round(decimals, out)
AttributeError: rint
Run Code Online (Sandbox Code Playgroud)
我用过numpy.around(x, decimals = 2)
和numpy.round(x,decimals=2)
难道我做错了什么?有没有其他方法可以有效地为大型阵列做到这一点?
pandas 支持多级列名:
>>> x = pd.DataFrame({'instance':['first','first','first'],'foo':['a','b','c'],'bar':rand(3)})
>>> x = x.set_index(['instance','foo']).transpose()
>>> x.columns
MultiIndex
[(u'first', u'a'), (u'first', u'b'), (u'first', u'c')]
>>> x
instance first
foo a b c
bar 0.102885 0.937838 0.907467
Run Code Online (Sandbox Code Playgroud)
此功能非常有用,因为它允许同一数据帧的多个版本"水平"附加,第一级列名称(在我的示例中instance)区分实例.
想象一下,我已经拥有了这样的数据帧:
a b c
bar 0.102885 0.937838 0.907467
Run Code Online (Sandbox Code Playgroud)
有没有一种很好的方法可以为列名添加另一个级别,类似于行索引:
x['instance'] = 'first'
x.set_level('instance',append=True)
Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何最好地将对父节点(例如SVG g元素)发生的数据的更改传递给它的子节点(例如SVG circle元素).
这是一个最低限度的工作示例.该示例假设您有一个被调用的对象svg,该对象引用包含SVG元素的d3选择.
data = [{"id":"A","name":"jim"},{"id":"B","name":"dave"},{"id":"C","name":"pete"}];
g = svg.selectAll("g").data(data, function(d) { return d.id; }).enter().append("g");
g.append("circle")
.attr("r", 3)
.attr("cx", 100)
.attr("cy", function(d,i) {return 100 + (i * 30);})
// The data gets passed down to the circles (I think):
console.log("circle data:");
d3.selectAll("g circle").each(function(d) { console.log(d.name); });
// Now change the data, and update the groups' data accordingly
data = [{"id":"A","name":"carol"},{"id":"B","name":"diane"},{"id":"C","name":"susan"}];
svg.selectAll("g").data(data, function(d) { return d.id;});
// These are the results of …Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关切片器一百万次的文档,但是我从来没有理解它,所以我仍然试图弄清楚如何使用loc切片DataFramea MultiIndex.
我DataFrame将从这个SO答案开始:
value
first second third fourth
A0 B0 C1 D0 2
D1 3
C2 D0 6
D1 7
B1 C1 D0 10
D1 11
C2 D0 14
D1 15
A1 B0 C1 D0 18
D1 19
C2 D0 22
D1 23
B1 C1 D0 26
D1 27
C2 D0 30
D1 31
A2 B0 C1 D0 34
D1 35
C2 D0 38
D1 39
B1 …Run Code Online (Sandbox Code Playgroud) 我正在使用一个异步库 ( asyncpg ),我想调试一些异步调用来查询数据库。
我放置了一个 pdb 断点并想尝试一些查询:
(pdb) await asyncpg.fetch("select * from foo;")
*** SyntaxError: 'await' outside function
Run Code Online (Sandbox Code Playgroud)
能够这样做会很棒,因为它允许我尝试一些 SQL 查询并查看结果,所有这些都来自我的调试器。
是否可以?
尽管阅读了本教程,这个问题和numpy docstring标准,但我无法让sphinx autodoc与numpy docstrings很好地配合.
在我的conf.py身上:
extensions = ['sphinx.ext.autodoc', 'numpydoc']
Run Code Online (Sandbox Code Playgroud)
在我的doc文件中,我有:
.. automodule:: python_file
.. autoclass:: PythonClass
:members:
Run Code Online (Sandbox Code Playgroud)
哪里python_file.py有:
class PythonClass(object):
def do_stuff(x):
"""
This does good stuff.
Here are the details about the good stuff it does.
Parameters
----------
x : int
An integer which has amazing things done to it
Returns
-------
y : int
Some other thing
"""
return x + 1
Run Code Online (Sandbox Code Playgroud)
当我跑步时,make html我得到了ERROR: Unknown …