使用该unittest
模块,我喜欢跳过测试的功能,但它仅适用于Python 2.7+.
例如,考虑test.py
:
import unittest
try:
import proprietary_module
except ImportError:
proprietary_module = None
class TestProprietary(unittest.TestCase):
@unittest.skipIf(proprietary_module is None, "requries proprietary module")
def test_something_proprietary(self):
self.assertTrue(proprietary_module is not None)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用早期版本的Python运行测试,则会收到错误消息:
Traceback (most recent call last):
File "test.py", line 7, in <module>
class TestProprietary(unittest.TestCase):
File "test.py", line 8, in TestProprietary
@unittest.skipIf(proprietary_module is None, "requries proprietary module")
AttributeError: 'module' object has no attribute 'skipIf'
Run Code Online (Sandbox Code Playgroud)
有没有办法"欺骗"旧版本的Python来忽略unittest装饰器,并跳过测试?
我正在尝试使用Numpy的f2py.py
脚本从Fortran源构建Python扩展.我正在按照http://www.scipy.org/F2PY_Windows(网络存档)中的步骤操作.我的系统是Windows 7 64位,我主要使用Python 2.7.3 [MSC v.1500 64位(AMD64)].我有Numpy-MKL 1.7.1,来自http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
这是我尝试过的:
cmd.exe
shell,并编辑我解压缩GCC编译器的路径,即:
set PATH=%PATH%;c:\gnu\mingw64\bin
set C_INCLUDE_PATH=C:\gnu\mingw64\include
f2py.py
使用此命令尝试构建:
C:\Python27\python.exe C:\Python27\Scripts\f2py.py -c --fcompiler=gnu95 --compiler=mingw32 -lmsvcr90 -m foo foo.f90
输出是(去除噪声线<--snip-->
):
running build
<--snip-->
Reading fortran codes...
Reading file 'foo.f90' (format:free)
Post-processing...
Block: foo
Block: hello
Post-processing (stage 2)...
Building modules...
Building module "foo"...
Constructing wrapper function "hello"...
hello()
Wrote C/API module "foo" to file "c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7\foomodule.c"
adding 'c:\users\mtoews\appdata\local\temp\tmpjr6qop\src.win-amd64-2.7\fortranobject.c' to sources.
adding …
Run Code Online (Sandbox Code Playgroud) 我需要测试PostgreSQL中的数值/浮点值是否不是数字(NaN).请注意"PostgreSQL将NaN
值视为相等",因此这个C++技巧不起作用.因为我isnan
在PostgreSQL 9.3中没有看到任何功能,所以这是我最好的尝试:
create or replace function isnan(double precision) returns boolean as
$$select $1::text = 'NaN'::text$$ language sql;
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来测试NaN
?
我正在使用HDF5 C++ API编写2D数组数据集文件.HDF小组有一个例子来创建一个静态定义的数组大小的HDF5文件,我已经修改过以满足下面的需求.然而,我需要动态阵列,其中两个NX
和NY
在运行时确定.我找到了另一种使用"new"关键字创建2D数组的解决方案,以帮助创建动态数组.这是我有的:
#include "StdAfx.h"
#include "H5Cpp.h"
using namespace H5;
const H5std_string FILE_NAME("C:\\SDS.h5");
const H5std_string DATASET_NAME("FloatArray");
const int NX = 5; // dataset dimensions
const int NY = 6;
int main (void)
{
// Create a 2D array using "new" method
double **data = new double*[NX];
for (int j = 0; j < NX; j++) // 0 1 2 3 4 5
{ // 1 2 3 4 5 6 …
Run Code Online (Sandbox Code Playgroud) 与此问题类似,如何查找数组中是否存在NULL值?
这是一些尝试.
SELECT num, ar, expected,
ar @> ARRAY[NULL]::int[] AS test1,
NULL = ANY (ar) AS test2,
array_to_string(ar, ', ') <> array_to_string(ar, ', ', '(null)') AS test3
FROM (
SELECT 1 AS num, '{1,2,NULL}'::int[] AS ar, true AS expected
UNION SELECT 2, '{1,2,3}'::int[], false
) td ORDER BY num;
num | ar | expected | test1 | test2 | test3
-----+------------+----------+-------+-------+-------
1 | {1,2,NULL} | t | f | | t
2 | {1,2,3} | f | f …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用来自argmin(或相关的argmax等函数)的2D数组索引来索引大型3D数组.这是我的示例数据:
import numpy as np
shape3d = (16, 500, 335)
shapelen = reduce(lambda x, y: x*y, shape3d)
# 3D array of [random] source integers
intcube = np.random.uniform(2, 50, shapelen).astype('i').reshape(shape3d)
# 2D array of indices of minimum value along first axis
minax0 = intcube.argmin(axis=0)
# Another 3D array where I'd like to use the indices from minax0
othercube = np.zeros(shape3d)
# A 2D array of [random] values I'd like to assign in othercube
some2d = np.empty(shape3d[1:])
Run Code Online (Sandbox Code Playgroud)
此时,两个3D阵列具有相同的形状,而minax0
阵列具有形状(500,335).现在,我想使用第一维的索引位置将2D数组some2d
中的值分配给3D数组.这是我正在尝试,但不起作用: …
我有一个格式化时间戳的文件 25/03/2011 9:15:00 p.m.
如何使用strptime
或as.POSIXct
?将此文本解析为Date-Time类?
这几乎是有效的:
> as.POSIXct("25/03/2011 9:15:00", format="%d/%m/%Y %I:%M:%S", tz="UTC")
[1] "2011-03-25 09:15:00 UTC"
Run Code Online (Sandbox Code Playgroud)
这是什么不起作用,但我想工作:
> as.POSIXct("25/03/2011 9:15:00 p.m.", format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
[1] NA
Run Code Online (Sandbox Code Playgroud)
我在MS Windows上使用R版本2.13.2(2011-09-30).我的工作区域是"C":
Sys.setlocale("LC_TIME", "C")
Run Code Online (Sandbox Code Playgroud) 我有从数据记录器获得的时间序列数据,该数据记录器设置为一个时区而没有夏令时(NZST或UTC + 12:00),数据跨越几年.数据记录器不考虑DST更改,并且在有/无DST的情况下同步到本地时间(取决于部署它的人员).
但是,当我将数据输入R时,我无法正确使用as.POSIXct
忽略DST.我在具有以下设置的Windows计算机上使用R 2.14.0:
> Sys.timezone()
[1] "NZDT"
> Sys.getlocale("LC_TIME")
[1] "English_New Zealand.1252"
Run Code Online (Sandbox Code Playgroud)
以下是春季DST变化的三个时间戳,每个时间间隔为1小时:
> ts_str <- c("28/09/2008 01:00", "28/09/2008 02:00", "28/09/2008 03:00")
> as.POSIXct(ts_str, format="%d/%m/%Y %H:%M", tz="")
[1] "2008-09-28 01:00:00 NZST" NA
[3] "2008-09-28 03:00:00 NZDT"
> as.POSIXct(ts_str, format="%d/%m/%Y %H:%M", tz="UTC")
[1] "2008-09-28 01:00:00 UTC" "2008-09-28 02:00:00 UTC"
[3] "2008-09-28 03:00:00 UTC"
Run Code Online (Sandbox Code Playgroud)
如您所见,时钟在1:59到3:00向前跳跃,因此2:00无效,因此NA.此外,我可以使用tz="UTC"
它来忽略DST更改.但是,我宁愿保留正确的时区,因为我有其他数据系列记录了 DST(NZDT或UTC + 13:00),我想混合(通过merge
)我的分析.
如何tz
在MS Windows计算机上配置参数?我尝试过很多东西,比如"NZST","新西兰标准时间","UTC + 12:00","+ 1200"等,但没有运气.或者我修改其他一些设置?
在Python 2.5中,我正在使用文件指针读取结构化文本数据文件(大小约为30 MB):
fp = open('myfile.txt', 'r')
line = fp.readline()
# ... many other fp.readline() processing steps, which
# are used in different contexts to read the structures
Run Code Online (Sandbox Code Playgroud)
但是,在解析文件时,我点击了一些有趣的东西,我想报告行号,所以我可以在文本编辑器中调查该文件.我可以fp.tell()
用来告诉我字节偏移量在哪里(例如16548974L
),但没有"fp.tell_line_number()"来帮助我将其转换为行号.
是否有Python内置或扩展可以轻松跟踪并"告诉"文本文件指针所在的行号?
注意:我不是要求使用line_number += 1
样式计数器,因为我fp.readline()
在不同的上下文中调用,并且该方法需要更多的调试,而不是将计数器插入代码的右角.
我正在尝试将几何对象存储到我的postgist数据库中,该数据库有一个带有几何列的表.我从另一个带有几何列的表中获取了几何值,然后打印出我之前得到的值,这没关系.要存储几何值,我使用下一个函数:
static void insertaGeometria( Geometry geom, int idInstalacion) throws ClassNotFoundException, SQLException{
Connection congeom = conectarPGA();
String geomsql ="INSERT INTO georrepositorio.geometria(id, point) VALUES (?,?)";
PreparedStatement psSE= congeom.prepareStatement(geomsql);
psSE.setInt(1, idInstalacion);
psSE.setObject(2, geom);
psSE.execute();
psSE.close();
congeom.close();
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到这个错误:
org.postgresql.util.PSQLException:无法推断用于org.postgis.Point实例的SQL类型.使用带有显式Types值的setObject()来指定要使用的类型.
有谁知道如何存储它?='(
提前致谢!
postgresql ×3
python ×3
numpy ×2
r ×2
arrays ×1
backport ×1
c++ ×1
datetime ×1
decorator ×1
dst ×1
dynamic-data ×1
f2py ×1
hdf5 ×1
indexing ×1
java ×1
line-numbers ×1
mingw ×1
mingw-w64 ×1
nan ×1
null ×1
postgis ×1
sql ×1
strptime ×1
text-files ×1
time-series ×1
timestamp ×1
timezone ×1
unit-testing ×1