pandas DataFrame 包含一列,其中包含花括号中的描述和占位符:
descr replacement
This: {should be replaced} with this
Run Code Online (Sandbox Code Playgroud)
任务是将大括号中的文本替换为同一行中另一列的文本。不幸的是,这并不那么容易:
df["descr"] = df["descr"].str.replace(r"{*?}", df["replacement"])
~/anaconda3/lib/python3.6/site-packages/pandas/core/strings.py in replace(self, pat, repl, n, case, flags, regex)
2532 def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
2533 result = str_replace(self._parent, pat, repl, n=n, case=case,
-> 2534 flags=flags, regex=regex)
2535 return self._wrap_result(result)
2536
~/anaconda3/lib/python3.6/site-packages/pandas/core/strings.py in str_replace(arr, pat, repl, n, case, flags, regex)
548 # Check whether repl is valid (GH 13438, GH 15055)
549 if not (is_string_like(repl) or callable(repl)):
--> 550 raise TypeError("repl …Run Code Online (Sandbox Code Playgroud) 如何为我的Jython程序设置JVM内存限制(Java的-Xmx选项)?
我知道Jython 2.5引入了-J选项以便向JVM发送选项:
jython -J-Xmx8000m
Run Code Online (Sandbox Code Playgroud)
但是,我必须在java1.6.0_23上使用Jython 2.2a0,它没有该选项.
假设我想使用进度条打印机跟踪循环的进度ProgressMeter(如本配方中所述).
def bigIteration(collection):
for element in collection:
doWork(element)
Run Code Online (Sandbox Code Playgroud)
我希望能够打开和关闭进度条.出于性能原因,我还想每x步更新一次.我天真的做法是
def bigIteration(collection, progressbar=True):
if progressBar:
pm = progress.ProgressMeter(total=len(collection))
pc = 0
for element in collection:
if progressBar:
pc += 1
if pc % 100 = 0:
pm.update(pc)
doWork(element)
Run Code Online (Sandbox Code Playgroud)
但是,我不满意.从"审美"的角度来看,循环的功能代码现在被"污染"了一般的进度跟踪代码.
您能想到一种清晰地分离进度跟踪代码和功能代码的方法吗?(可以有进度跟踪装饰器吗?)
这是MATLAB内置函数的docstring spones(S):
spones
用1替换非零稀疏矩阵元素.R = spones(S)生成具有与S相同的稀疏结构的矩阵,但是具有非零位置的矩阵.
我想使用numpy/scipy数据结构(例如来自稀疏矩阵scipy.sparse)与此函数等效.我怎样才能有效地做到这一点?
我正在尝试使用googletest在C++中进行单元测试.我在以下位置定义了一个文本夹具ClusteringTest.h:
#include <gtest/gtest.h>
namespace EnsembleClustering {
class ClusteringTest: public ::testing::Test {
ClusteringTest() {};
virtual ~ClusteringTest() {};
virtual void SetUp() {
};
virtual void TearDown() {
};
};
TEST_F(ClusteringTest, doesGTestWork) {
EXPECT_EQ(42, 42);
}
} /* namespace EnsembleClustering */
Run Code Online (Sandbox Code Playgroud)
在我的主要功能中,我打电话给:
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
Run Code Online (Sandbox Code Playgroud)
结果是:
running EnsembleClustering
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (1 ms total)
[ PASSED ] 0 tests.
Run Code Online (Sandbox Code Playgroud)
为什么我的测试没有运行?
C++引用仍然让我感到困惑.假设我有一个函数/方法,它创建一个类型的对象Foo并通过引用返回它.(我假设如果我想返回对象,它不能是在堆栈上分配的局部变量,所以我必须在堆上分配它new):
Foo& makeFoo() {
...
Foo* f = new Foo;
...
return *f;
}
Run Code Online (Sandbox Code Playgroud)
当我想存储在另一个函数的局部变量中创建的对象时,该类型应该是 Foo
void useFoo() {
Foo f = makeFoo();
f.doSomething();
}
Run Code Online (Sandbox Code Playgroud)
还是Foo&?
void useFoo() {
Foo& f = makeFoo();
f.doSomething();
}
Run Code Online (Sandbox Code Playgroud)
由于两者都是正确的语法:两个变体之间是否存在显着差异?
我有一个C++ 11项目,其中包含许多googletest单元测试
TEST_F(GTest, testSomething) {
int64_t n = 42;
// following code depends on input size n
...
}
Run Code Online (Sandbox Code Playgroud)
n我希望能够从一个位置设置输入大小,而不是在每个测试中都有一个局部常量,最好是命令行:
./RunMyProgram --gtest_filter=* --n=1000
Run Code Online (Sandbox Code Playgroud)
本main应该是这样的:
int main(int argc, char **argv) {
// TODO: parse command line argument n here
INFO("=== starting unit tests ===");
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)
我应该?在测试功能中替换什么?
TEST_F(GTest, testSomething) {
int64_t n = ?;
// following code depends on input size n
...
}
Run Code Online (Sandbox Code Playgroud) 我使用log4cxx进行日志记录。但是,日志记录可能会带来一些性能开销,我需要将其最小化。
如何关闭日志记录(在运行时或编译时)以使开销最小化 - 无需从代码中删除所有日志语句?
该文档指出
用户应该注意以下性能问题。
关闭日志记录时的日志记录性能。
当完全关闭日志记录或仅关闭一组级别的日志记录时,日志请求的成本包括方法调用加上整数比较。如果未启用请求,则 LOG4CXX_DEBUG 和类似的宏会抑制不必要的表达式计算。
但如何完全关闭它呢?这是可以实现的最小开销吗?
考虑以下情节:

由该函数产生:
def timeDiffPlot(dataA, dataB, saveto=None, leg=None):
labels = list(dataA["graph"])
figure(figsize=screenMedium)
ax = gca()
ax.grid(True)
xi = range(len(labels))
rtsA = dataA["running"] / 1000.0 # running time in seconds
rtsB = dataB["running"] / 1000.0 # running time in seconds
rtsDiff = rtsB - rtsA
ax.scatter(rtsDiff, xi, color='r', marker='^')
ax.scatter
ax.set_yticks(range(len(labels)))
ax.set_yticklabels(labels)
ax.set_xscale('log')
plt.xlim(timeLimits)
if leg:
legend(leg)
plt.draw()
if saveto:
plt.savefig(saveto, transparent=True, bbox_inches="tight")
Run Code Online (Sandbox Code Playgroud)
这里重要的是 的值的正差或负差x = 0。如果能更清楚地想象这一点就好了,例如
这可以用 matplotlib 完成吗?需要添加什么代码?

该图应该显示时间差异,可以是负值和正值.有些差异非常小,而其他差异非常大.
我可以缩放x轴,使分辨率在x = 0附近非常精细,并且远离x = 0粗糙吗?是否有可能从x = 0向外输出对数标度?
编辑:
正如@Evert所建议的,这解决了我的问题:
ax = gca()
...
ax.set_xscale("symlog")
Run Code Online (Sandbox Code Playgroud)
并产生这个情节:

python ×5
c++ ×4
googletest ×2
matplotlib ×2
plot ×2
unit-testing ×2
jvm ×1
jython ×1
log4cxx ×1
logging ×1
numpy ×1
oop ×1
pandas ×1
performance ×1
python-3.x ×1
reference ×1
regex ×1
return ×1
return-type ×1
scipy ×1
string ×1
testing ×1