小编ivi*_*ica的帖子

登录对话框PyQt

当客户询问我是否可以在应用程序启动时实现某种登录表单时,我几乎完成了我的应用程序.

到目前为止,我已经设计了UI,并对实际执行进行了修改.用户名和密码暂时无关紧要.

class Login(QtGui.QDialog):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_dlgLogovanje()
        self.ui.setupUi(self)

        QtCore.QObject.connect(self.ui.buttonLogin, QtCore.SIGNAL("clicked()"), self.doLogin)

    def doLogin(self):
        name = str(self.ui.lineKorisnik.text())
        passwd = str(self.ui.lineSifra.text())
        if name == "john" and passwd =="doe":
            self.runIt()
        else:
            QtGui.QMessageBox.warning(self, 'Greška',
        "Bad user or password", QtGui.QMessageBox.Ok)           

    def runIt(self):
        myprogram = Window()        
        myprogram.showMaximized() #myprogram is

class Window(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)


if __name__=="__main__":
    program = QtGui.QApplication(sys.argv)
    myprogram = Window()
    if Login().exec_() == QtGui.QDialog.Accepted:       
        sys.exit(program.exec_())
Run Code Online (Sandbox Code Playgroud)

登录表单显示.如果输入了正确的用户名和密码,则会显示主窗口并正常工作.但是,登录表单保持活动状态,如果我关闭它,主窗口也将关闭.

python qt dialog login pyqt

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

Django RabbitMQ 消费者

我正在构建一个 Django 应用程序,多个外部应用程序将与该应用程序联系。Django 应用程序应该提供 UI 并使用从外部应用程序接收的数据填充数据库。

第一个想法是使用django_rest_framework,但这似乎创建一个紧密耦合的系统,因为每个外部应用程序都必须通过 REST 调用联系 Django 应用程序。

我的另一个想法最好用图片来描述:https://i.stack.imgur.com/dGgSg.jpg几个发布者会在 RabbitMQ 上创建消息,我的 Django 将使用这些消息并在数据库中创建适当的模型。

这样的事情可能吗?我已经pika为发布者和消费者使用了库中的异步示例,并且消息按预期流动。将 Django 混入其中会产生如下错误:

RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label  

django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Run Code Online (Sandbox Code Playgroud)

代码摘录:

# pika consumer
def on_message(self, unused_channel, basic_deliver, properties, body):
        # invoking view function
        from myapp.views import create_one_foo
        create_one_foo()
        self.acknowledge_message(basic_deliver.delivery_tag)

# views.py
from …
Run Code Online (Sandbox Code Playgroud)

python django rabbitmq

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

运行多个龙卷风进程

我已经阅读了有关如何运行N个Tornado进程的各种文章和教程,其中N =核心数.我的代码正在运行,在所有16个核心上运行,但我设法搞砸了它,我需要新的眼睛.

import tornado.ioloop
import tornado.web
import tornado.httpserver

from core import settings
from core import coreService
import services

from tornado.options import define, options, parse_command_line

define("port", default=settings.SERVER_PORT, help="run on the given port", type=int)



app = tornado.web.Application([
    (r'/upload', coreService.Upload)
])

if __name__ == "__main__":
    tornado.options.parse_command_line()
    server = tornado.httpserver.HTTPServer(app, max_buffer_size=1024*1024*201)
    server.bind(options.port)
    # autodetect cpu cores and fork one process per core
    server.start(0)
    try:        
        print 'running on port %s' % options.port
        tornado.ioloop.IOLoop.instance().start()

    except KeyboardInterrupt:
        tornado.ioloop.IOLoop.instance().stop()
Run Code Online (Sandbox Code Playgroud)

此代码抛出此错误:

File "/opt/tornado/tornado/process.py", line 112, in fork_processes
    raise RuntimeError("Cannot …
Run Code Online (Sandbox Code Playgroud)

python tornado

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

用Python编写的Windows服务仅在调试模式下有效

我编写了一个简单的Windows服务,该服务应使用websocket报告VirtualBox计算机的状态。

安装并启动该服务后,我的Websocket服务器收到一个连接请求,但该连接几乎立即被关闭。

启动服务时的服务器输出

running on port 8888
new connection
connection closed
Run Code Online (Sandbox Code Playgroud)

运行该服务可pythonservice.exe -debug myservice打开一个websocket连接并发送我期望的数据。

使用调试标志启动服务时的服务器输出

running on port 8888
new connection
message received VM NAME: win1, Memory: 111, CPUS: 1
message received VM NAME: win2, Memory: 266, CPUS: 1
message received VM NAME: win3, Memory: 256, CPUS: 1
message received VM NAME: lin1, Memory: 256, CPUS: 1
message received VM NAME: lin2, Memory: 200, CPUS: 1
message received VM NAME: lin3, Memory: 222, CPUS: 1
connection closed …
Run Code Online (Sandbox Code Playgroud)

python windows-services websocket

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

BeautifulSoup HTML提取表格数据

我有这个HTML表,我需要一些数据:

<table id="curFullTable" class="tablesorter" style="width:728px;margin-left:auto;margin-right:auto;">
    <tr>
        <td>Euro</td>
        <td align="center">EUR</td>
        <td align="center">€</td>
        <td align="center">1</td>
        <td align="center">110.9416</td>
        <td align="center">111.2754</td>
        <td align="center">111.6092</td>
        </tr>
    <tr>
        <td>Australian dollar</td>
        <td align="center">AUD</td>
        <td align="center">$</td>
        <td align="center">1</td>
        <td align="center">84.9671</td>
        <td align="center">85.2228</td>
        <td align="center">85.4785</td>
    </tr>
    <tr>
        <td>Canadian dollar</td>
        <td align="center">CAD</td>
        <td align="center">$</td>
        <td align="center">1</td>
        <td align="center">81.6167</td>
        <td align="center">81.8623</td>
        <td align="center">82.1079</td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

使用此代码:

tableData = htmlText.find("table", attrs={"class":"tablesorter"})
rows = tableData.findAll('tr')
Run Code Online (Sandbox Code Playgroud)

我将所有表行和表格单元格放在一个列表中.到目前为止,我已经设法逐个提取货币名称,但我真的需要一个货币名称列表,就像这样

currencies = ['Euro','Australian dollar','Canadian dollar']
Run Code Online (Sandbox Code Playgroud)

实现这个目标的方法是什么?稍后,我还需要从每个TR元素的最后一个TD元素中提取数据.

python beautifulsoup extraction

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

线程BaseHTTPServer,每个请求一个线程

我正在尝试使用BaseHttpServer和创建一个多线程Web服务器ThreadingMixIn(如各种示例所示).伪代码类似于:

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
             pass
    def do_POST(self):
             pass

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""

if __name__ == '__main__':
    server = ThreadedHTTPServer(('localhost', 9999), Handler)
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()
Run Code Online (Sandbox Code Playgroud)

这可以按预期工作,但我的问题是并非每个请求都获得一个线程,但是每个URL都进行了线程化.我测试过这样:我有一个URL绑定执行以下方法:

import time
import datetime

def request_with_pause(self):
    print datetime.datetime.now().strftime("%H:%M:%S.%f"), 'REQUEST RECEIVED'
    time.sleep(10)
    print datetime.datetime.now().strftime("%H:%M:%S.%f"), 'SENT RESPONSE'
Run Code Online (Sandbox Code Playgroud)

它运行正常,除非我在5秒暂停时调用url 两次(单击URL,等待5秒并再次单击它) - 两个"响应"在10秒后到达(第一次单击响应).

python multithreading

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

mongoengine来自另一个字段的默认值

我正在尝试使用MongoEngine进行一个项目而且相当不错.我想知道是否可以为另一个字段中的字段设置默认值?像这样的东西

import mongoengine as me

class Company(me.Document):
    short_name = me.StringField(required=True)
    full_name = me.StringField(required=True, default=short_name)
Run Code Online (Sandbox Code Playgroud)

这失败了,错误 ValidationError (Company:None) (StringField only accepts string values: ['full_name'])

:编辑:

我没有提到我的应用程序有一个服务层,使我能够像这样简单地执行:

if company_data['short_name'] is None:
            myCompany.full_name = company_data['short_name']


        obj = myCompany.save()
Run Code Online (Sandbox Code Playgroud)

而且效果很好.

python mongoengine

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

LAN 上的 SQLite

我的应用程序使用 SQLite 数据库来存储数据。理想情况下,该数据库应该驻留在某个网络驱动器上,我们将其命名为 Z:(Windows XP 的“映射网络驱动器”功能)。

应用程序在Linux下开发,数据库本地存储。这是一个模块的一部分:

import sqlite3 as lite

con = lite.connect("base.db")
Run Code Online (Sandbox Code Playgroud)

访问 Z: 驱动器上的数据库的正确方法是什么?大致如下:

import sqlite3 as lite
import os

path = 'Z:\'
con = lite.connect(path+"base.db")
Run Code Online (Sandbox Code Playgroud)

python sql sqlite

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