我正在使用此命令来禁用回显并使用以下命令获取用户输入sys.stdin.read(1)
tty.setcbreak(sys.stdin.fileno())
Run Code Online (Sandbox Code Playgroud)
然而,在我的程序过程中,我需要再次启用和禁用控制台回显。我试过
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
termios.tcsetattr(fd, termios.TCSADRAIN, old)
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。如何优雅地启用回显?
ps:我使用的是mizipzor 的Python 非阻塞控制台输入的代码
代码如下:
import sys
import select
import tty
import termios
import time
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def calc_time(traw):
tfactor = {
's': 1,
'm': 60,
'h': 3600,
}
if is_number(g[:-1]):
return float(g[:-1]) * tfactor.get(g[-1])
else:
return None
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setcbreak(sys.stdin.fileno())
i = 0
while …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
class a
{
int a1;
public:
a()
{
printf("foo1\n");
}
};
class b : public a
{
int a2;
public:
b()
{
printf("foo2\n");
}
};
int main (int argc, const char * argv[])
{
b *instance = new a();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了错误:无法初始化类型"b*"的变量,其值为"a*",当我写的时候工作正常
a *instance = new b();
Run Code Online (Sandbox Code Playgroud)
输出是:
foo1
foo2
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下原因吗?我将非常感激:)
另一件事,如果我写
instance->~a();
Run Code Online (Sandbox Code Playgroud)
以上return 0;
没有什么额外的事 这是因为构造函数只能被调用一次吗?
我有一个函数的版本1,如:
def f(a,b,c):
#Some processing
return some_result
Run Code Online (Sandbox Code Playgroud)
后来,我将其升级到版本2.
def f(a,b,c):
#Some processing
#Some more processing
return some_result, additional_result
Run Code Online (Sandbox Code Playgroud)
后一版本返回一个元组.因此,使用版本1的所有客户端代码都已过时.我可以additional_result
按需求吗?
这就是你additional_result
当你问它,而你继续只得到some_result
仿佛什么都没有改变.
我想到的一个技巧:
def f(a,b,c, need_additional = False):
#Some processing
#Some more processing
if not need_addional:
return some_result
else:
return some_result, additional_result
Run Code Online (Sandbox Code Playgroud)
还有什么更好的?还是更通用的?
鉴于:
[(1,2),(3,4),(5,6),(3,7),(5,7)]
Run Code Online (Sandbox Code Playgroud)
输出:
[set(1,2), set(3,4,5,6,7)]
Run Code Online (Sandbox Code Playgroud)
解释:
(1,2)
(1,2), (3,4)
(1,2), (3,4), (5,6)
(1,2), (3,4,7), (5,6)
(1,2), (3,4,7,5,6)
Run Code Online (Sandbox Code Playgroud)
我写了一个糟糕的算法:
Case 1: both numbers in pair are new (never seen before):
Make a new set with these two numbers
Case 2: one of the number in pair is new, other is already a part of some set:
Merge the new number in other's set
Case 3: both the numbers belong to some set:
Union the second set into first. Destroy the second …
Run Code Online (Sandbox Code Playgroud) 我有像这样的字符串
"&% , USEFUL DATA ^$^@&#!*, USEFUL DATA *%@^#,,, "
Run Code Online (Sandbox Code Playgroud)
需要像这样清洁:
"USEFUL DATA ^$^@&#!*, USEFUL DATA"
Run Code Online (Sandbox Code Playgroud)
我们在 Javascript 中是否有任何标准库函数可以做到这一点(我们在 python 中有它)?
前任:trim(str, "!@#$%^^&*(), ")
xcode 11.4 从模拟器中删除了标准的“硬件”和设备选项。如何打开现有设备或创建新设备?
这是新的模拟器菜单:
在 xcode 11.4 中,Windows > 设备和模拟器,没有启动模拟器的选项。
我必须使用urllib2实现一个函数来获取头文件(不进行GET或POST).这是我的功能:
def getheadersonly(url, redirections = True):
if not redirections:
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
http_error_301 = http_error_303 = http_error_307 = http_error_302
cookieprocessor = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor)
urllib2.install_opener(opener)
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
info = {}
info['headers'] = dict(urllib2.urlopen(HeadRequest(url)).info())
info['finalurl'] = urllib2.urlopen(HeadRequest(url)).geturl()
return info
Run Code Online (Sandbox Code Playgroud)
使用代码回答这个和这个.但是,即使标志是,也会进行重定向False
.我试过代码:
print getheadersonly("http://ms.com", redirections = False)['finalurl']
print getheadersonly("http://ms.com")['finalurl']
Run Code Online (Sandbox Code Playgroud)
它在两种情况下给予morganstanley.com.这有什么不对?
i'm trying to limit the number of requests from an IP in case i get too many requests from it.
例如:如果我每分钟收到超过 50 个请求,我想阻止该 IP 5 分钟。
当我使用时,request.META['REMOTE_ADDR']
我总是获得本地主机的 IP,而不是发送请求的 IP。
我必须从数字列表中创建唯一的,无序的2个元素集.然后将每个集合插入一个列表.
例如:
setslist
如果它们不存在则插入每个集合.(集合是无序的.所以(1,2)与(2,1)相同)setslist = [(2,1),(2,3),(1,3)]
python中最优化的解决方案是什么?