小编jer*_*use的帖子

如何在 tty.setcbreak() 之后重新打开控制台回显

我正在使用此命令来禁用回显并使用以下命令获取用户输入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)

python

3
推荐指数
2
解决办法
6568
查看次数

为什么C++不能用"超类"类型的右值初始化"派生类"类型的变量?

请考虑以下代码:

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;没有什么额外的事 这是因为构造函数只能被调用一次吗?

c++ oop

2
推荐指数
2
解决办法
1万
查看次数

在python中返回多个值而不破坏以前的代码

我有一个函数的版本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)

还有什么更好的?还是更通用的?

python paradigms

2
推荐指数
2
解决办法
191
查看次数

给定相关数字列表,合并相关列表以创建不相交的集合

鉴于:

[(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)

python list-comprehension

2
推荐指数
1
解决办法
383
查看次数

Javascript删除字符串中所有前导和尾随的垃圾字符

我有像这样的字符串

"&% , USEFUL DATA ^$^@&#!*, USEFUL DATA *%@^#,,,   "
Run Code Online (Sandbox Code Playgroud)

需要像这样清洁:

"USEFUL DATA   ^$^@&#!*,  USEFUL DATA"
Run Code Online (Sandbox Code Playgroud)

我们在 Javascript 中是否有任何标准库函数可以做到这一点(我们在 python 中有它)?

前任:trim(str, "!@#$%^^&*(), ")

javascript regex

2
推荐指数
1
解决办法
2439
查看次数

如何在 xcode 11.4 中更改模拟器设备?

xcode 11.4 从模拟器中删除了标准的“硬件”和设备选项。如何打开现有设备或创建新设备?

这是新的模拟器菜单:

在此处输入图片说明


在 xcode 11.4 中,Windows > 设备和模拟器,没有启动模拟器的选项。

在此处输入图片说明

xcode ios ios-simulator xcode11.4

2
推荐指数
1
解决办法
1731
查看次数

python只使用urllib2获取头文件

我必须使用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.这有什么不对?

python urllib2

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

django limit the number of requests per minute

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。

  1. 如何获取发送请求的计算机的 IP?
  2. 我如何限制该 IP 在 X 时间内不发送更多请求?

python django request

1
推荐指数
2
解决办法
5683
查看次数

添加唯一的无序集列表

我必须从数字列表中创建唯一的,无序的2个元素集.然后将每个集合插入一个列表.

例如:

  1. setslist = [(2,1)]
  2. uniquenumbers = [1,2,3]
  3. 独特集 - (1,2),(2,3),(1,3)
  4. setslist如果它们不存在则插入每个集合.(集合是无序的.所以(1,2)与(2,1)相同)
  5. 最后 setslist = [(2,1),(2,3),(1,3)]

python中最优化的解决方案是什么?

python algorithm

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