小编gle*_*xey的帖子

C++ 11 regex :: icase不一致行为

来自类似perl的正则表达式,我期望下面的代码在所有8种情况下匹配正则表达式.但事实并非如此.我错过了什么?

#include <iostream>
#include <regex>
#include <string>

using namespace std;

void check(const string& s, regex re) {
    cout << s << " : " << (regex_match(s, re) ? "Match" : "Nope") << endl;
}

int main() {
    regex re1 = regex("[A-F]+", regex::icase);
    check("aaa", re1);
    check("AAA", re1);
    check("fff", re1);
    check("FFF", re1);
    regex re2 = regex("[a-f]+", regex::icase);
    check("aaa", re2);
    check("AAA", re2);
    check("fff", re2);
    check("FFF", re2);
}
Run Code Online (Sandbox Code Playgroud)

使用gcc 5.2运行:

$ g++ -std=c++11 test.cc -o test && ./test
aaa : Match
AAA : Match …
Run Code Online (Sandbox Code Playgroud)

c++ regex gcc c++11 gcc5.2

6
推荐指数
1
解决办法
825
查看次数

如何覆盖python函数上的默认帮助消息

我有一个带有许多参数和详细帮助信息的函数,例如:

def worker_function(arg1, arg2, arg3):
    """ desired help message:
    arg1 - blah
    arg2 - foo
    arg3 - bar
    """
    print arg1, arg2, arg3
Run Code Online (Sandbox Code Playgroud)

我也有一个包装函数,做一些会计,然后叫我worker_function,所有参数传递给它的是.

def wrapper_function(**args):
    """ this function calls worker_function """
    ### do something here ...
    worker_function(**args)
Run Code Online (Sandbox Code Playgroud)

我希望包装函数的帮助消息(由python内置的help()函数显示)具有来自worker函数的参数列表帮助消息.

我能得到的最接近的解决方案是:

wrapper_function.__doc__ += "\n\n" + worker_function.__doc__
Run Code Online (Sandbox Code Playgroud)

这导致:

>>? help(wrapper_function)
Help on function wrapper_function in module __main__:

wrapper_function(**args)
    this function calls worker function

    desired help message:
       arg1 - blah
       arg2 - foo
       arg3 - bar
Run Code Online (Sandbox Code Playgroud)

但是这个描述缺少必要的部分 - 参数列表,即: …

python function built-in

4
推荐指数
1
解决办法
1098
查看次数

c# vs python float舍入差异

我正在用 python 开发一个应用程序,其中一些部分是用 c# 编写的(为了加速),我很困惑为什么在使用“round”函数和字符串浮点格式化函数时,c# 浮点舍入的行为与 python 的行为“相反”,这是一个例子:

Python (2.7)

>>> round(6.25, 1)
6.3
>>> "%.1f"%6.25
6.2
Run Code Online (Sandbox Code Playgroud)

C#

>>> Math.Round(6.25,1)
6.2
>>> (6.25).ToString("F1")
6.3
Run Code Online (Sandbox Code Playgroud)

有谁理解为什么 Python 与 C# 之间的行为似乎“相反”?有没有办法将“双精度”浮点值舍入为 N 位十进制数字,以在 Python 和 C# 之间产生保证相同的字符串输出?

c# python floating-point rounding

0
推荐指数
1
解决办法
1383
查看次数

标签 统计

python ×2

built-in ×1

c# ×1

c++ ×1

c++11 ×1

floating-point ×1

function ×1

gcc ×1

gcc5.2 ×1

regex ×1

rounding ×1