在向服务提交查询后,我得到一个字典/列表,我想确保它不是空的.我在Python 2.7上.
我很惊讶我没有看到类实例的任何assertEmpty方法unittest.TestCase.
现有的替代品如:
self.assertTrue(bool(d))
Run Code Online (Sandbox Code Playgroud)
和
self.assertNotEqual(d,{})
Run Code Online (Sandbox Code Playgroud)
和
self.assertGreater(len(d),0)
Run Code Online (Sandbox Code Playgroud)
只是看起来不对劲.
Python unittest框架中是否缺少这种方法?如果是,那么断言迭代不为空的最pythonic方法是什么?
我尝试使用line_profiler模块获取Python文件的逐行配置文件.这是我到目前为止所做的:
1)使用.exe文件(我在WinXP和Win7上)从pypi安装line_profiler .只需单击安装向导即可.
2)写了一小段代码(类似于此处另一个回答问题中提到的代码).
from line_profiler import LineProfiler
def do_stuff(numbers):
print numbers
numbers = 2
profile = LineProfiler(do_stuff(numbers))
profile.print_stats()
Run Code Online (Sandbox Code Playgroud)
3)运行IDLE/PyScripter中的代码.我只得到了时间.
Timer unit: 4.17188e-10 s
Run Code Online (Sandbox Code Playgroud)
如何在我执行的代码上获得完整的逐行配置文件?我从未使用任何高级Python功能,如装饰器,所以我很难理解如何使用这里和这里的几个帖子提供的指南.
我知道用于构建 Google 风格的文档字符串的语法,例如:
def function_with_types_in_docstring(param1, param2):
"""Example function with types documented in the docstring.
`PEP 484`_ type annotations are supported. If attribute, parameter, and
return types are annotated according to `PEP 484`_, they do not need to be
included in the docstring:
Args:
param1 (int): The first parameter.
param2 (str): The second parameter.
Returns:
bool: The return value. True for success, False otherwise.
"""
Run Code Online (Sandbox Code Playgroud)
但是,如果我有一个函数可以根据执行的代码分支返回多种类型怎么办?记录这一点的正确方法是什么?
下面是一个例子。该部分应该放入什么Returns?
def foo(x, y):
"""Dummy function.
Args:
x (int): integer
y (int): integer …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,它读取了几个.csv文件(它们不大,每个都有几千行),我做了一些数据清理和争吵,这是每个.csv文件看起来的最终结构(假数据为仅用于说明目的).
import pandas as pd
data = [[112233, 'Rob', 99], [445566, 'John', 88]]
managers = pd.DataFrame(data)
managers.columns = ['ManagerId', 'ManagerName', 'ShopId']
print managers
ManagerId ManagerName ShopId
0 112233 Rob 99
1 445566 John 88
data = [[99, 'Shop1'], [88, 'Shop2']]
shops = pd.DataFrame(data)
shops.columns = ['ShopId', 'ShopName']
print shops
ShopId ShopName
0 99 Shop1
1 88 Shop2
data = [[99, 2000, 3000, 4000], [88, 2500, 3500, 4500]]
sales = pd.DataFrame(data)
sales.columns = ['ShopId', 'Year2010', 'Year2011', 'Year2012']
print sales
ShopId Year2010 …Run Code Online (Sandbox Code Playgroud) 我在PyCharm Community Edition 2017.2中打开的Python模块中有这段代码。
class Sample(object):
def __init__(self):
self.prop1 = 5
self.prop2 = 10
def method1(self):
return foo
def do_work(self):
not_defined_func()
s = Sample()
s.method1()
bar = call_func
Run Code Online (Sandbox Code Playgroud)
对于某些相当严重的问题,IDE不会发出任何警告:
foo尚未定义。call_func变量bar。not_defined_func在方法内部调用了未定义的函数do_work。如何启用PyCharm突出显示这些内容?我重新安装了PyCharm,并在中Inspections启用了所有功能File > Settings > Editor > Inspections > Python。
给定一个tests包含几个子目录的目录,每个子目录都包含测试模块,如何创建一个pytest在仅在特定子目录中找到的每个测试之前运行的固定装置?
tests\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdirXX\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test_module1.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test_module2.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 subdirYY\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test_module3.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test_module4.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\nRun Code Online (Sandbox Code Playgroud)\n\n我想要一个固定装置,该装置将在唯一的模块中找到的每个测试之前运行subdirYY(在本例中,在模块test_module3.py和中test_module4.py)。
我目前有一个固定装置定义了两次,一次在每个模块内,该模块subdirYY可以工作,但是多余的:
@pytest.fixture(autouse=True)\ndef clean_directory():\n ...\nRun Code Online (Sandbox Code Playgroud)\n\n如果无法实现这一点,则 中的每个测试都subdirYY用自定义标记 ( @pytest.mark.mycustommark) 进行装饰,因此确保在用特定自定义标记标记的每个测试之前运行某个固定装置也是一个可行的选择。
我正在研究空间分析问题,该工作流程的一部分是计算连接线段之间的角度.
每个线段仅由两个点组成,每个点都有一对XY坐标(笛卡尔坐标).这是GeoGebra的图像.我总是对在0到180范围内获得正角度感兴趣.但是,根据输入线段中顶点的顺序,我得到所有类型的角度.

我使用的输入数据以坐标元组的形式提供.根据顶点创建顺序,每个线段的最后/结束点可以不同.以下是Python代码中的一些案例.我得到它们的线段的顺序是随机的,但在元组的元组中,第一个元素是起点,第二个元素是终点.DE线段,例如,本来((1,1.5),(2,2))和(1,1.5)为起点,因为它在坐标元组第一的位置.
但是,我需要确保我会得到的相同的角度DE,DF,并ED,DF依此类推.
vertexType = "same start point; order 1"
#X, Y X Y coords
lineA = ((1,1.5),(2,2)) #DE
lineB = ((1,1.5),(2.5,0.5)) #DF
calcAngle(lineA, lineB,vertexType)
#flip lines order
vertexType = "same start point; order 2"
lineB = ((1,1.5),(2,2)) #DE
lineA = ((1,1.5),(2.5,0.5)) #DF
calcAngle(lineA, lineB,vertexType)
vertexType = "same end point; order 1"
lineA = ((2,2),(1,1.5)) #ED
lineB = ((2.5,0.5),(1,1.5)) #FE
calcAngle(lineA, lineB,vertexType)
#flip lines order
vertexType = …Run Code Online (Sandbox Code Playgroud) 我继承了一个包含多个 JavaScript 文件的项目。他们每个人都有一堆功能;这些文件以 AMD 风格定义。
例如:
math.js
define([], function () {
return {
func1: function (a, b) {
return a + b;
},
func2: function (c, d) {
return c + d;
},
};
});
Run Code Online (Sandbox Code Playgroud)
我想在tests文件夹中生成一个名为 ( math.js)的新文件,该文件将包含框架tdd样式的单元测试的样板代码intern。
我使用了intern-generator一个 Yeoman 生成器,它可以搭建脚手架并生成一个具有指定名称和路径的测试文件,但是,这个生成器不会让我创建引用 JS 文件中的函数的单元测试。
所以,对于math.js源文件,我想创建一个测试文件(自动):
define(function (require) {
var tdd = require('intern!tdd');
var assert = require('intern/chai!assert');
var math = require('src/app/math');
tdd.suite('Suite name', function () {
tdd.test1('Test foo', function () { …Run Code Online (Sandbox Code Playgroud) 我使用.pydC++ 和pybind11. 我想.pyi为我的项目生成一个 Python 接口文件.pyd。
有几个类似的问题涉及该mypy stubgen模块,但是,这个问题会产生一个UnicodeError尝试运行文件stubgen Dummy在哪里Dummy的Dummy.pyd问题:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte
另一个项目make-stub-files根本不处理.pyd文件,给出('not a python file'文件,从而出现错误。
是否有任何工具可以让我从源.cpp文件或编译后生成 .pyi 文件.pyd文件生成 .pyi 文件?
该问题已在mypyGitHub 存储库中注册。
我有一个pandas数据框(v 0.20.3):
df = pd.DataFrame({'coname1': ['Apple','Yahoo'], 'coname2':['Apple', 'Google']})
df['eq'] = df.apply(lambda row: row['coname1'] == row['coname2'], axis=1).astype(bool)
coname1 coname2 eq
0 Apple Apple True
1 Yahoo Google False
Run Code Online (Sandbox Code Playgroud)
如果我想更换True/False到'Yes'/'No',我可以运行如下命令:
df.replace({
True: 'Yes',
False: 'No'
})
coname1 coname2 eq
0 Apple Apple Yes
1 Yahoo Google No
Run Code Online (Sandbox Code Playgroud)
这似乎完成了工作.但是,如果数据框只是一行,其值为0/1列,则它也将被替换,因为它被视为布尔值.
df1 = pd.DataFrame({'coname1': [1], 'coname2':['Google'], 'coname3':[777]})
df1['eq'] = True
coname1 coname2 coname3 eq
0 1 Google 777 True
df1.replace({
True: 'Yes',
False: 'No'
})
coname1 coname2 …Run Code Online (Sandbox Code Playgroud) python ×9
pandas ×2
python-2.7 ×2
testing ×2
unit-testing ×2
angle ×1
assertions ×1
atan2 ×1
boolean ×1
class ×1
dataframe ×1
dictionary ×1
docstring ×1
fixtures ×1
ide ×1
intern ×1
javascript ×1
mypy ×1
oop ×1
profiling ×1
pycharm ×1
pytest ×1
replace ×1
return-type ×1
syntax ×1
trigonometry ×1
typeshed ×1
yeoman ×1