让我们说,我们想要用正则表达式来捕获一些东西,使用rawstring来定义模式,哪个模式具有重复元素,以及内部的变量.我们还想使用format()字符串格式化表单.这该怎么做?
import re
text = '"""!some text'
re.findall(r'"{3}{symbol}some\stext'.format(symbol='!'), text)
Run Code Online (Sandbox Code Playgroud)
但这条线路引导我们IndexError:
# IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:如果原始字符串具有格式化花括号表达式,并且在里面重复花括号表达式,如何格式化原始字符串?
提前致谢!
在Python中,如何拆分空格或连字符?
输入:
You think we did this un-thinkingly?
Run Code Online (Sandbox Code Playgroud)
期望的输出:
["You", "think", "we", "did", "this", "un", "thinkingly"]
Run Code Online (Sandbox Code Playgroud)
我可以得到
mystr.split(' ')
Run Code Online (Sandbox Code Playgroud)
但我不知道如何拆分连字符和空格,并且拆分的Python定义似乎只指定了一个字符串.我需要使用正则表达式吗?
我该怎么用wait?它让我感到困惑不已.我fork是一个带递归的触发树,现在孩子们必须暂停(等待/睡眠),而我运行pstree,这样我就可以打印proc树了.
我应该用吗?
int status;
wait(&status);
Run Code Online (Sandbox Code Playgroud)
更确切地说
wait(NULL)
Run Code Online (Sandbox Code Playgroud)
我应该把它放在哪里?在父母if(pid > 0)或孩子if(pid==0)?也许在ifs的末尾,所以我将所有pids 存储在数组中然后运行for它们并使用wait?
我的代码模板:
void ProcRec(int index)
{
pid_t pid;
int noChild = getNChild(index);
int i= 0;
for(i = 0; i < noChild; i++)
{
pid = fork();
if (pid > 0)
{
/* parent process */
}
else if (pid == 0)
{
/* child process. */
createProc(index+1);
}
else
{
/* error */
exit(EXIT_FAILURE);
}
}
if(getpid() == …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C++包装一个C库,使其成为一个现代的,高水平的,惯用的C++库.我想要做的是使C对象完全不透明和/或直接从C++代码中不可用,并用更高级别的替代品包装/替换它们.
我面临的问题很简单:我想只将C头包含在C++源代码中,这样C++头文件在包含时也不会包含C头文件的声明,也就是说,它不会污染C++头部.全局命名空间
但看起来头文件和源文件的正确分离似乎不允许我这样做.这是我的问题的一个非常模糊的版本,评论会告诉你其余的:
my_header.h:
typedef enum
{
my_Consts_ALPHA = /* some special value */,
my_Consts_BETA = /* other special value */,
} my_Consts;
typedef struct
{
// members...
} my_Type;
void
my_Type_method(my_Type *const,
my_Enum);
Run Code Online (Sandbox Code Playgroud)
my_header.hpp:
namespace my
{
enum class Consts; // <-- This header is missing the constant values of
// this enum, because its values are defined by
// the C header :(
class Type : public my_Type // <-- The super struct is coming from the
// …Run Code Online (Sandbox Code Playgroud) 我想做的就是通过pyglet在OpenGL中创建一个非常简单的平移和缩放功能.正如你所看到的,缩放在第一次跳转后完美运行:(再次,拖动(平移)也在工作,但它也会跳跃(它会跳得很大).
这是我的简化代码和视频(pyglet_test.mp4),它显示了它的行为:
import pyglet
from pyglet.gl import *
# Zooming constants
ZOOM_IN_FACTOR = 1.2
ZOOM_OUT_FACTOR = 1/ZOOM_IN_FACTOR
class App(pyglet.window.Window):
def __init__(self, width, height, *args, **kwargs):
# Create GL configuration
conf = Config( sample_buffers=1,
samples=4,
depth_size=16,
double_buffer=True )
# Initialize parent
super().__init__( width, height, config=conf, *args, **kwargs )
# Create Group
self.group = group = pyglet.graphics.Group()
# Create Batch
self.batch = batch = pyglet.graphics.Batch()
# Create QUAD for testing and add it to batch
batch.add(
4, GL_QUADS, group,
('v2i', …Run Code Online (Sandbox Code Playgroud) 我正在使用Tkinter和线程编写应用程序.
我得到的问题是,关闭主应用程序后,一些线程仍在运行,我需要一种方法来检查根窗口是否已被销毁以避免TclError: can't invoke "wm" command.
我知道的所有方法:一旦root被销毁wminfo_exists(),state()所有返回错误.
我有一个进程,它是这样创建的:
p = subprocess.Popen(args = './myapp',
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
universal_newlines=True)
Run Code Online (Sandbox Code Playgroud)
后来,我试着写p的stdin:
p.stdin.write('my message\n')
Run Code Online (Sandbox Code Playgroud)
该myapp过程具有以下设置:
q = queue.Queue()
def get_input():
for line in iter(sys.stdin.readline, ''):
q.put(line)
sys.stdin.close()
threading.Thread(name = 'input-getter',
target = get_input).start()
Run Code Online (Sandbox Code Playgroud)
它正在尝试不断读取新行,如下所示:
try:
print('input:', q.get_nowait())
except Empty:
print('no input')
Run Code Online (Sandbox Code Playgroud)
不幸的是,子进程从不接收任何消息.当然,当我使用时:
p.communicate('my message\n')
Run Code Online (Sandbox Code Playgroud)
子进程临危消息,但正如所料,communicate方法关闭p的stdin,所以没有更多的沟通正在进行.
所以我需要一个函数来生成一个从a增加的字母列表,并以zzz结尾.
应该是这样的:
a
b
c
...
aa
ab
ac
...
zzx
zzy
zzz
Run Code Online (Sandbox Code Playgroud)
我目前的代码是这样的:
for combo in product(ascii_lowercase, repeat=3):
print(''.join(combo))
Run Code Online (Sandbox Code Playgroud)
但是,这只会增加3个字母,输出更像
a
ab
abc
abcd
...
Run Code Online (Sandbox Code Playgroud)
因此,回顾:字母增加的函数,当它超过z时,它返回到aa.谢谢!
更新:
我有与以前相同的输出.这是我想要插入的内容:
a = hashlib.md5()
for chars in chain(ALC, product(ALC, repeat=1), product(ALC, repeat=1)):
a.update(chars.encode('utf-8'))
print(''.join(chars))
print(a.hexdigest())
Run Code Online (Sandbox Code Playgroud)
我的哈希最终结果如下:
f1784031a03a8f5b11ead16ab90cc18e
Run Code Online (Sandbox Code Playgroud)
但我希望:
415290769594460e2e485922904f345d
Run Code Online (Sandbox Code Playgroud)
谢谢!
处理变量参数时,是否va_start()需要匹配va_end()调用,或者也va_copy()需要匹配va_end(),即
void foo(char *x, ...)
{
va_list l,c;
va_start(l,x);
va_copy(c,l);
---
va_end(c); //is this correct ?
va_end(l);
}
Run Code Online (Sandbox Code Playgroud) python ×7
c ×3
formatting ×2
python-3.x ×2
string ×2
alphabet ×1
c++ ×1
c++11 ×1
c99 ×1
coroutine ×1
graphics ×1
header ×1
increment ×1
namespaces ×1
nonblocking ×1
opengl ×1
pyglet ×1
python-2.7 ×1
python-c-api ×1
rawstring ×1
regex ×1
stdin ×1
subprocess ×1
tkinter ×1
wait ×1
wrapper ×1