我正在使用一个pygame.joystick.Joystick
对象并希望能够打印一条消息,要求用户在拔下 USB 游戏杆后重新连接它。
现在我有(大致):
js = pygame.joystick.Joystick(0)
#... some game code and stuff
pygame.joystick.quit()
pygame.joystick.init()
while pygame.joystick.get_count() == 0:
print 'please reconnect joystick'
pygame.joystick.quit()
pygame.joystick.init()
js = pygame.joystick.Joystick(0)
js.init()
Run Code Online (Sandbox Code Playgroud)
但它没有正确重新连接,不知道它到底在做什么,但这绝对是错误的。这方面的任何方向都会有所帮助
Ruby是否提供任何机制来允许迭代器yield
来自另一个迭代器的所有值?(或"subiterator",我不确定正确的名称是什么).类似于Python3.3 +的收益率
def f
yield 'a'
yield 'b'
end
def g
# yield everything from f
yield 'c'
yield 'd'
end
Run Code Online (Sandbox Code Playgroud) 有没有办法告诉我FILE *
打开了什么模式?
具体来说,我需要判断一个文件流是否可写.要么只是它的可写的真/假结果,要么const char *
模式本身是好的.
rows = driver.find_element_by_xpath("//div[@style='display: block;']//table[@style='display: table;']//tr")
Run Code Online (Sandbox Code Playgroud)
当我尝试迭代行时它会抛出一个错误 TypeError: 'WebElement' object is not iterable
def get_size(e):
for entry in e:
count = count + 1
return count
get_size(rows)
Run Code Online (Sandbox Code Playgroud) 我有两个小python文件,第一个使用读取一行input
,然后打印另一行
a = input()
print('complete')
Run Code Online (Sandbox Code Playgroud)
第二次尝试将其作为子进程运行
import subprocess
proc = subprocess.Popen('./simp.py',
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
bufsize=1)
print('writing')
proc.stdin.write(b'hey\n')
print('reading')
proc.stdout.readline()
Run Code Online (Sandbox Code Playgroud)
上面的脚本将先打印“正在书写”,然后显示“正在阅读”,然后挂起。起初我以为这是一个标准输出缓冲的问题,所以我换bufsize=1
到bufsize=0
了,这确实解决问题。但是,似乎是标准输入引起了问题。
使用bufsize=1
,如果我proc.stdin.flush()
在写入下方添加,则过程继续。这两种方法都显得笨拙,因为(1)无缓冲的流很慢(2)在所有地方添加刷新都很容易出错。为什么上面的代码write
不能在换行符上刷新?文档说这bufsize
是在为子进程创建stdin,stdout和stderr流时使用的,那么是什么导致写操作不在换行符上刷新呢?
假设我有一些Python代码:
class Mother:
def __init__(self):
print("Mother")
class Father:
def __init__(self):
print("Father")
class Daughter(Mother, Father):
def __init__(self):
print("Daughter")
super().__init__()
d = Daughter()
Run Code Online (Sandbox Code Playgroud)
此脚本打印"女儿".反正是否确保调用基类的所有__init__方法?我想出的一个方法是:
class Daughter(Mother, Father):
def __init__(self):
print("Daughter")
for base in type(self).__bases__:
base.__init__(self)
Run Code Online (Sandbox Code Playgroud)
这个脚本打印"女儿","母亲","父亲".使用super()或其他方法有一个很好的方法吗?
假设我有一个结构Foo
,我想确定它是否Foo
有int
内部.
struct Foo { int a; char c; };
has_int<Foo>::value; // should be true
Run Code Online (Sandbox Code Playgroud)
这是我真正想要的最基本形式,检查特定类型:
has_type<Foo, int>::value;
Run Code Online (Sandbox Code Playgroud)
如果我知道如何做到这一点,我可以将它转换为我的最终目标:
has_pointer<Foo>::value; // false
struct Bar { int a; void *b; };
has_pointer<Bar>::value; // true
Run Code Online (Sandbox Code Playgroud)
至于我尝试过的,很难开始,我能想到的最好的是,如果我能得到一个包含在结构中的类型的包,我可以写下其余的:
template <typename... Ts>
constexpr bool any_is_pointer() noexcept {
return (... || std::is_pointer_v<Ts>);
}
Run Code Online (Sandbox Code Playgroud)
我要求的似乎很可能是不可能的.我找不到重复,但我很惊讶,我不能这样,它可能会在那里.
或者另一种表达方式的方法是:编译器是否可以假设一个实例enum
只能保存声明为基于该假设而保持和优化的值?
enum MyType { A = 1, B = 2 };
const MyType C = static_cast<MyType>(3);
void fun(MyType m) {
switch (m) {
case A:
// ...
break;
case B:
// ...
break;
case C:
// can this be optimized away?
}
}
Run Code Online (Sandbox Code Playgroud) 如何在编译时确定我的平台是小端还是大端?我已经看到很多方法可以在运行时使用强制转换来确定,以及一些与平台相关的选项.有便携式或标准方式吗?
constexpr bool is_little_endian = ?;
Run Code Online (Sandbox Code Playgroud) 我试图学习有关C11原子的更多信息,但看不到为什么要使用__atomic_load_n
over __atomic_load
。该文档仅声明一个是通用的,但用法看起来是相同的:
内置函数:类型__atomic_load_n(类型* ptr,int内存)此内置函数实现原子加载操作。它返回* ptr的内容。
有效的内存顺序变量是__ATOMIC_RELAXED,__ATOMIC_SEQ_CST,__ATOMIC_ACQUIRE和__ATOMIC_CONSUME。
内置函数:void __atomic_load(类型* ptr,类型* ret,int内存)这是原子负载的通用版本。它在* ret中返回* ptr的内容。
https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html