小编col*_*yre的帖子

如何使用 PyQt5 运行“While Loop”?

前段时间我制作了一个脚本来记录用户的监视器帧,现在我正在尝试为它创建一个 GUI。目前它只有一个 START 和一个 STOP 按钮,但 STOP 按钮不会停止录制。

我怎样才能改变我的stop_thread功能让它工作?我应该先终止工人然后终止线程吗?我怎样才能终止工人?

import sys
from PyQt5.QtWidgets import (QWidget,
                             QPushButton, QApplication, QGridLayout)
from PyQt5.QtCore import QThread, QObject


class Worker(QObject):

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)

    def do_work(self):
        i = 1
        while True:
            print(i)
            QThread.sleep(1)
            i = i + 1

    def stop(self):
        print("stopped")
        self.deleteLater() # How do I stop it?


class Gui(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        # Buttons:
        self.btn_start = QPushButton('Start')
        self.btn_start.resize(self.btn_start.sizeHint())
        self.btn_start.move(50, 50)
        self.btn_stop = QPushButton('Stop')
        self.btn_stop.resize(self.btn_stop.sizeHint())
        self.btn_stop.move(150, 50)

        # GUI …
Run Code Online (Sandbox Code Playgroud)

python pyqt5

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

Can I set active master in an Elasticsearch cluster manually?

I know that it is possible to define more than one master for the ElasticSearch cluster, where only one acts as master and the others can step in if necessary. See also /sf/answers/1051597431/ .

What I don't understand is how I can determine which master is active and which could step in if necessary.

The following setting I currently have:

node-01: master (x) data(-)
node-02: master (-) data(x)
node-03: master (-) data(x)
node-04: master (-) data(x)
node-05: master (-) data(x) …
Run Code Online (Sandbox Code Playgroud)

elasticsearch

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

Python:如何用ast重载运算符

我的问题是重新+定义 - 运算符用ast评估表达式.我有一个表达式列表,使用eval()很容易解决:

>>> expr = '1+2*3**4/5'
>>> print(eval(expr))
33.4
Run Code Online (Sandbox Code Playgroud)

但我喜欢重新定义+-operator(添加)列表和dict,如下所示:

expr = '[1,2,3]+[4,5,6]'
Run Code Online (Sandbox Code Playgroud)

eval的常规结果是

[1,2,3,4,5,6]

但我想拥有

[5,7,9]

喜欢用R语言.

对于这样的字典,这同样适用:

expr = "{'a':1, 'b':2} + {'a':3, 'b':4}"
Run Code Online (Sandbox Code Playgroud)

我想要

{'a':4,'b':6}

简而言之,我认为要替换普通的add函数,即当操作数是list或dict正确的动作时.

我试图用astNodeTransformer,但没有成功.有人可以帮帮我吗?

python overloading abstract-syntax-tree

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

如何打开命令提示符以及使用 python 脚本运行的命令?

我尝试了以下方法来打开命令提示符并运行示例命令。但它立即关闭:

import os
# as of now i am just passing cd /../, later will be changing to a different command
os.system("start /wait cmd /c {cd /../}")
Run Code Online (Sandbox Code Playgroud)

我也尝试过这种方式,但这会打开两个命令外壳:

import os
os.system("start /B start cmd.exe @cmd /k cd /d D:")
Run Code Online (Sandbox Code Playgroud)

正在打开两个命令外壳

是否可以只打开一个命令提示符并运行该命令?

python windows

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

返回最小值的键

我想dictionary在 Python 中返回 a中最小值的键。key, value 对的值会有几个数字,即dict[current] = (total, gone, heuristic). 如何返回最小消失值的键?

python dictionary

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

max in a list of tuples (Python)

I have this list of list of tuples

var1 = [
    [(10, '?'), (7, '?')],
    [(14, '?'), (2, '?')],
    [(2, '?'), (9, '?')],
    [(11, '?'), (10, '?')],
    [(11, '?'), (5, '?')]
]
Run Code Online (Sandbox Code Playgroud)

and I wanna extract the tuple with the maximun value which is the second one or var1[1]. I've used a lot of different codes during my programming but the one I'm using now and until now and didn't have any major issues is this one:

 maximo = max(var1, …
Run Code Online (Sandbox Code Playgroud)

python tuples list max

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

通过将列表转换为集合再转换回列表对列表进行排序的时间复杂度

我最近观看了Raymond Hettingers 谈论 Python 字典(以及扩展集......),他提到整数散列到它们自己,并且将整数添加到字典(或集合......)将按顺序插入它们,只要你不要删除项目,订单将保留在 python 3.6(可能是更高版本?)中。在对这个问题的回答中指出,字典保留插入顺序,但对于集合而言,它的接缝就像整数一样根据它们的值进行排序。

现在,根据所述python.org的时间复杂度的部分,并且更详细这里它被指出,添加元素的一组的平均时间复杂度是O(1)。这意味着如果您有一个未排序的整数列表,应该可以通过简单地对它们进行排序:

sorted_list = list(set(unsorted_list))
Run Code Online (Sandbox Code Playgroud)

就我测试过的情况而言,情况确实如此(用随机序列做了几 1000 次)。

我现在的问题是:这是否意味着可以在 O(n) 时间内对 Python 中的整数进行排序?

对我来说它会接缝,因为它需要 O(n) 来构建集合和 O(n) 将集合转换回列表还是我在这里遗漏了什么?

python sorting list set time-complexity

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

用Popen打开一个进程并在一段时间后杀死

我想在5分钟之后杀死一个进程(ex),我打开了

subprocess.Popen()
p = subprocess.Popen('calc.exe', shell=True)  # example
Run Code Online (Sandbox Code Playgroud)

我打印了点子print(p.pid),睡了10秒钟time.sleep(10),然后杀了这个过程p.kill().

问题是calc.exe仍然在运行.我已经用它Process Explorer来看看这里发生了什么,似乎子进程将创建一个带有pid =的cmd,p.pid它会创建calc.exe,但是另一个pid我不知道.

这里的所有代码:

import subprocess, os, time

p = subprocess.Popen('calc.exe', shell=True)

time.sleep(1)
print(p.pid)
p.kill()
Run Code Online (Sandbox Code Playgroud)

那么我做错了什么?

谢谢!

python windows subprocess

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

Python-Flask 中的 render_template 不起作用

我实际上是在用 Flask 创建一个应用程序,但我遇到了有关我的路由的问题。

我的情况很简单:用户输入一个令牌来验证自己。一旦他点击了authentication,一个有角度的 HTTP 请求就会使用 POST 将他的令牌发送到 Python 服务器。在那里,如果他被授予访问权限,则使用render_template;显示主页。否则登录保持静止。

但是,当用户对自己进行身份验证时,我在命令行上看到 POST 成功,身份验证成功,但页面只是停留在登录状态并且不会重定向到主页,就好像第二个render_template不起作用一样。请帮忙!

@app.route('/')
def index():
    if not session.get('logged_in'):
        return render_template('auth.html')  # this is ok.
    else:
        return render_template('index.html')  # this does not work


@app.route('/login', methods=['POST','GET'])
def login():
    tok = request.form['token']

    if (check_token(tok) == "pass"):  # check_token is a function I've implemented
                                      # to check if token is ok=pass, ko=fail
        session['logged_in'] = True
    else:
        flash("wrong token")

    return index()  
Run Code Online (Sandbox Code Playgroud)

python routing flask

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

为什么在这个简单的python程序中发生异常?

我写了这个非常简单的Python程序来了解Exception的工作原理:

def Divide(x,y):
    try:
        print (int(a)/int(b))
    except:
        print "Exception Occured!"
Run Code Online (Sandbox Code Playgroud)

但奇怪的是,每次都会发生异常:

>>> Divide(int(1),int(2))
Exception Occured!
>>> Divide(1,2)
Exception Occured!
Run Code Online (Sandbox Code Playgroud)

虽然它不应该发生:

>>> print 1/2
0
>>> print (1/2)
0
>>> print (int(1)/int(2))
0
Run Code Online (Sandbox Code Playgroud)

怎么了?

python exception python-2.7

-4
推荐指数
1
解决办法
75
查看次数