我想要下面的代码,但"pythonic"风格或使用标准库:
def combinations(a,b):
for i in a:
for j in b:
yield(i,j)
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种seaboarn
通过指定fmt
关键字在的热图上将“0.0045”显示为“0.45%”的方法:
sns.heatmap(data, annot=True, fmt='??')
Run Code Online (Sandbox Code Playgroud)
但是,我没有找到要使用的格式列表。在不同的示例之间搜索,我看到了“d”、“.2g”、“.1f”、“.1f%”。但不清楚我们在这里假设的约定是什么。
这是假设人们对格式格式有共同的理解吗?或者这出现在我错过的文档页面上?
我主要在C++中做事,其中析构函数方法实际上是用于破坏获得的资源.最近我开始使用python(这真的很有趣也很棒),我开始学习它像GC一样的GC.因此,没有强调对象所有权(构造和破坏).
据我所知,这个 __init__()
方法在python中对我来说比对ruby更有意义,但是__del__()
方法,我们真的需要在我们的类中实现这个内置函数吗?如果我想念,我的班级会缺少什么__del__()
?我可以看到__del__()
有用的一个场景是,如果我想在销毁对象时记录某些内容.除此之外还有什么吗?
最近,我决定从 MATLAB 移植到 Matplotlib 来绘制图形。在 MATLAB 中,我要做的就是转到“文件”>“导出”>“渲染”,然后选择 600 dpi,然后选择“应用到图形”,然后导出。在 Matplotlib 中,我使用 matplotlib 库中的命令 savefig 作为
matplotlib.pyplot.savefig(fname, dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', papertype=None, format=None,
transparent=False, bbox_inches=None, pad_inches=0.1,
frameon=None).
Run Code Online (Sandbox Code Playgroud)
然后我将 dpi 设置为 600,并将 .tiff 设置为输出格式。除了文件非常大 ~ 32 mb 之外,效果很好。显然,我不能在期刊手稿中使用这么大的文件。我想知道是否有一种方法可以将压缩合并到文件中,以便在不损失分辨率的情况下获得较小尺寸的图像文件。
我想在相同的参数化测试的不同实例之间共享夹具,其中夹具本身也是参数化的:
#!/usr/bin/py.test -sv
import pytest
numbers_for_fixture = [0]
def pytest_generate_tests(metafunc):
if "config_field" in metafunc.fixturenames:
metafunc.parametrize("config_field", [1], scope='session')
@pytest.fixture(scope = 'session')
def fixture_1(config_field):
numbers_for_fixture[0] += 1
return '\tfixture_1(%s)' % numbers_for_fixture[0]
@pytest.fixture(scope = 'session')
def fixture_2():
numbers_for_fixture[0] += 1
return '\tfixture_2(%s)' % numbers_for_fixture[0]
def test_a(fixture_1):
print('\ttest_a:', fixture_1)
def test_b(fixture_1):
print('\ttest_b:', fixture_1)
@pytest.mark.parametrize('i', range(3))
def test_c(fixture_1, i):
print('\ttest_c[%s]:' % i, fixture_1)
@pytest.mark.parametrize('i', range(3))
def test_d(fixture_2, i):
print('\ttest_d[%s]:' % i, fixture_2)
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
platform linux -- Python 3.4.1 -- py-1.4.26 -- pytest-2.6.4 -- /usr/bin/python
collecting …
Run Code Online (Sandbox Code Playgroud) 我有一个DataFrame,其中包含每列的列表,如下例所示,只有两列.
Gamma Beta
0 [1.4652917656926299, 0.9326935235505321, float] [91, 48.611034768515864, int]
1 [2.6008354611105995, 0.7608529935313189, float] [59, 42.38646954167245, int]
2 [2.6386970166722348, 0.9785848171888037, float] [89, 37.9011122659478, int]
3 [3.49336632573625, 1.0411524946972244, float] [115, 36.211134224288344, int]
4 [2.193991200007534, 0.7955134305428825, float] [128, 50.03563864975485, int]
5 [3.4574527664490997, 0.9399880977511021, float] [120, 41.841146628802875, int]
6 [3.1190582380554863, 1.0839109431114795, float] [148, 55.990072419824514, int]
7 [2.7757359940789916, 0.8889801332053203, float] [142, 51.08885697101243, int]
8 [3.23820908493237, 1.0587479742892683, float] [183, 43.831293356668425, int]
9 [2.2509032790941985, 0.8896196407231622, float] [66, 35.9377662201882, int]
Run Code Online (Sandbox Code Playgroud)
我想为每一列提取每行列表的第一个位置,以获得如下的DataFrame.
Gamma Beta
0 1.4652917656926299 91
1 …
Run Code Online (Sandbox Code Playgroud) 当我使用Selenium进行自动化测试时,遇到一个问题,这是所有场景:
页面顶部有几个选项卡,现在,我想单击这些选项卡并填写这些选项卡下的所有表单,但是如果我在formA
下方提交这些选项卡tabA
,则无法自动导航至其他选项卡。如果我没有提交表单数据,则不会发生此问题。这是日志:
1513753361368 Marionette DEBUG Received DOM event "beforeunload" for "https://192.168.1.20/link.cgi?1513753343333"
1513753361388 Marionette DEBUG Received DOM event "beforeunload" for "https://192.168.1.20/link.cgi?1513753343333"
1513753361391 Marionette DEBUG Received DOM event "pagehide" for "https://192.168.1.20/link.cgi?1513753343333"
1513753361391 Marionette DEBUG Received DOM event "unload" for "https://192.168.1.20/link.cgi?1513753343333"
1513753361427 Marionette DEBUG Received DOM event "DOMContentLoaded" for "about:neterror?e=connectionFailure&u=https%3A//192.168.1.20/network.cgi&c=UTF-8&f=regular&d=Firefox%20%E6%97%A0%E6%B3%95%E5%BB%BA%E7%AB%8B%E5%88%B0%20192.168.1.20%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E8%BF%9E%E6%8E%A5%E3%80%82"
org.openqa.selenium.WebDriverException: Reached error page: about:neterror?e=connectionFailure&u=https%3A//192.168.1.20/network.cgi&c=UTF-8&f=regular&d=Firefox%20%E6%97%A0%E6%B3%95%E5%BB%BA%E7%AB%8B%E5%88%B0%20192.168.1.20%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E8%BF%9E%E6%8E%A5%E3%80%82
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'PC-20161127KZEG', ip: '192.168.131.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver …
Run Code Online (Sandbox Code Playgroud) 我有大量的时间序列(> 500),我只想选择周期性的时间序列。我做了一些文献研究,发现应该寻找自相关。使用numpy
我计算自相关为:
def autocorr(x):
norm = x - np.mean(x)
result = np.correlate(norm, norm, mode='full')
acorr = result[result.size/2:]
acorr /= ( x.var() * np.arange(x.size, 0, -1) )
return acorr
Run Code Online (Sandbox Code Playgroud)
这将返回一组系数(r?),在绘制时应告诉我时间序列是否为周期性。
我生成了两个玩具示例:
#random signal
s1 = np.random.randint(5, size=80)
#periodic signal
s2 = np.array([5,2,3,1] * 20)
Run Code Online (Sandbox Code Playgroud)
生成自相关图时,我得到:
第二个自相关向量清楚地表明了一些周期性:
Autocorr1 = [1, 0.28, -0.06, 0.19, -0.22, -0.13, 0.07 ..]
Autocorr2 = [1, -0.50, -0.49, 1, -0.50, -0.49, 1 ..]
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何从自相关矢量自动确定时间序列是否为周期性?有没有一种方法可以将这些值汇总为单个系数,例如,如果= 1完美周期性,如果= 0则完全没有周期性。我试图计算平均值,但没有意义。我应该看看数字1吗?
我正在开发一个 API,它从本地文件夹返回一些文件,以模拟我们为开发人员环境提供的系统。
大多数系统的工作原理是输入识别人员的代码并返回他的文件。但是,该系统的一条路径具有独特的行为:它使用 POST 方法(请求正文包含 Id 代码),我正在努力使其工作。
这是我当前的代码:
import json
from pathlib import Path
import yaml
from fastapi import FastAPI
from pydantic.main import BaseModel
app = FastAPI()
class RequestModel(BaseModel):
assetId: str
@app.get("/{group}/{service}/{assetId}")
async def return_json(group: str, service: str, assetId: str):
with open("application-dev.yml", "r") as config_file:
output_dir = yaml.load(config_file)['path']
path = Path(output_dir + f"{group}/{service}/")
file = [f for f in path.iterdir() if f.stem == assetId][0]
if file.exists():
with file.open() as target_file:
return json.load(target_file)
@app.post("/DataService/ServiceProtocol")
async def return_post_path(request: RequestModel):
return return_json("DataService", "ServiceProtocol", …
Run Code Online (Sandbox Code Playgroud) python ×9
matplotlib ×2
pandas ×2
python-3.x ×2
cassandra ×1
correlation ×1
fastapi ×1
firefox ×1
geckodriver ×1
generator ×1
numpy ×1
pytest ×1
request ×1
seaborn ×1
selenium ×1
time-series ×1
webdriver ×1