有没有办法在C函数中使用命名参数?
像原型的功能 void foo(int a, int b, int c);
我想用foo(a=2, c=3, b=1);[替换顺序b & c并用它们的名字来区分] 来调用它
动机:我想要一个更多的Pythonic C,我可以轻松地操纵我的函数参数而不会误将它们混合在一起
在阅读本文时,我看到了一个我不了解的UB,希望您能澄清一下
size_t f(int x)
{
size_t a;
if(x) // either x nonzero or UB
a = 42;
return a;
}
Run Code Online (Sandbox Code Playgroud)
我猜UB是由于a没有初始化的值,但这不是它定义的行为吗?意思是,无论f(0)变量a是什么, 都将返回其所保存的值(我认为这类似于rand())。我们是否必须知道代码片段返回的值使代码具有明确定义的行为?
在尝试调试我的代码时,我用以下标记了一些printf:
#if debug
printf(...);
#endif
Run Code Online (Sandbox Code Playgroud)
在文件的开头,我错误地写了#define debug而不是#define debug 1
gcc 抛出以下错误:
错误:#if 没有表达式
现在我有两个选择:
#define debug 1#ifdef debug我知道在看到#define debug 1PP 会替换debug我代码中的每一个1,这就是为什么#if debug在我的代码中不起作用 - 编译器看不到任何表达......
我的问题是,当我使用时#ifdef debug- 会发生什么?我想debug保存在某处并检查,但在哪里以及如何?
我看到了这个用于合并列表的解决方案,例如a = [1,2,3], b = [4,5,6]使用res = [*a, *b].
假设我有一个子列表列表,例如ls = [a,b]是否可以执行类似的操作res = [*i for i in ls]?
该特定行自 以来无效SyntaxError: iterable unpacking cannot be used in comprehension。可以做类似的事情吗?
如果没有,我如何轻松创建一个包含子列表中所有元素的列表?
使用Python 3.5.3
有没有一种方法可以在不预先分配缓冲区的情况下连接字符串?
考虑以下:
int main()
{
char buf1[] = "world!";
char buf2[100] = "hello ";
char * p = "hello ";
// printf("%s", strcat(p, buf1)); // UB
printf("%s", strcat(buf2, buf1)); // correct way to use strcat
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个我想要作为字符串前缀的指针,我必须先将它复制到缓冲区然后strcat()吗?是否有任何功能隐含地这样做?
我认为唯一的选择是strcat(),和sprintf(),都需要一个缓冲.
str1 + str2语法的东西我知道strncpy是一个更安全的版本,strcpy如说在这里.
但是,当我想要复制src到dst并且dst不是一个干净的缓冲区时,我会收到不需要的结果,这可以通过以下方式避免strcpy:
char *p = "123";
char a[10] = "aaaaaa";
strncpy(a,p,strlen(p));
printf("%s\n", a); // 123aaa
strcpy(a,p);
printf("%s\n", a); // 123 <- desired output, as the trailing a's are garbage
Run Code Online (Sandbox Code Playgroud)
在我的实际情况中,我知道strlen(src) < sizeof(dst)(至少,如果不是这样,程序会很快崩溃),所以我可以安全地strcpy.
但是,如果strncpy是我应该使用的,那么我必须在之后添加dst[strlen(src)] = '\0'以避免垃圾(或者更好的是,事先启动缓冲区吗?)?
尝试使用以下命令从空项目创建轮子setup.py:
from setuptools import setup
setup(name='bla', version='1')
Run Code Online (Sandbox Code Playgroud)
我调用python setup.py bdist_wheel --python-tag py35 --plat-name linux_x86_64并得到
bla-1-py35-none-linux_x86_64.whl
python -V: Python 3.6.9
uname -p: x86_64
Run Code Online (Sandbox Code Playgroud)
我有一个脚本,我必须为其设置一个特定的 virtualenv。
#!/usr/bin/env python / python3 / python3.6如果脚本运行时 virtualenv 未打开,则运行脚本不会切断它,所以我做了这样的事情:
#!/home/bla/bla2/bla3/venv/bin/python
import virtualenv stuff...
Run Code Online (Sandbox Code Playgroud)
我假设
鉴于这两个假设,这是一个好主意吗?如果不是,什么会更好?我不愿意有一个包装 bash 脚本来打开 venv 然后调用我的脚本
是否pytest有像一个计时器测试?
IE
@pytest.mark.timer(60)
def test_this_code_runs_in_less_than_x():
bla()
Run Code Online (Sandbox Code Playgroud)
如果bla执行超过 60 秒,哪个会失败?
我可以使用自己的装饰器轻松实现这一点,例如:
import time, threading
def timeout(func):
def wrapper():
start_time = time.time()
timeout = start_time + 1
th = threading.Thread(target=func)
th.start()
while time.time() < timeout and th.isAlive():
pass
if th.isAlive():
raise TimeoutError()
th.join()
return wrapper
@timeout
def _test():
time.sleep(2)
def test_should_fail_after_one_second():
_test()
Run Code Online (Sandbox Code Playgroud)
但我不想发明轮子...
我知道 pytest 可以有一个 --timeout 参数,但我正在寻找一些东西作为测试定义的一部分,而不是影响所有测试的可配置参数
我有一个 json 键,其中有空格:
My_dict = {"my key": 1}
Run Code Online (Sandbox Code Playgroud)
我想创建一个Model来建模它:
from pydantic import BaseModel
class MyModel(BaseModel):
mykey: int
# my key isn't a legit variable name
# my_key is, but like mykey - it doesn't catch the correct key from the json
MyModel(**my_dict)
Run Code Online (Sandbox Code Playgroud)
这是行不通的。
我尝试玩BaseModel.Config,但没有取得任何进展。也没有在文档中看到任何内容。
这可能吗?
我可以使用解决方法:遍历json,将所有键的空格替换为下划线,然后使用,pydantic但我不想使用这个...
python ×6
c ×5
abi ×1
cpython ×1
json ×1
list ×1
nested-lists ×1
pydantic ×1
pytest ×1
python-3.x ×1
python-wheel ×1
setuptools ×1
strcat ×1
strcpy ×1
string ×1
strncpy ×1
virtualenv ×1