我真的很难为Flask,SQLAlchemy和Celery进行适当的设置.我已经广泛搜索并尝试了不同的方法,似乎没有什么工作.我错过了应用程序上下文或无法运行工作程序或存在其他一些问题.结构非常通用,因此我可以构建更大的应用程序.
我正在使用:Flask 0.10.1,SQLAlchemy 1.0,Celery 3.1.13,我目前的设置如下:
应用程序/ __ init__.py
#Empty
Run Code Online (Sandbox Code Playgroud)
应用程序/ config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
@staticmethod
def init_app(app):
pass
class LocalConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = r"sqlite:///" + os.path.join(basedir,
"data-dev.sqlite")
CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
config = {
"local": LocalConfig}
Run Code Online (Sandbox Code Playgroud)
应用程序/ exstensions.py
from flask.ext.sqlalchemy import SQLAlchemy
from celery import Celery
db = SQLAlchemy()
celery = Celery()
Run Code Online (Sandbox Code Playgroud)
应用程序/ factory.py
from extensions import db, celery
from flask import Flask
from flask import g
from config import config
def create_before_request(app):
def before_request(): …Run Code Online (Sandbox Code Playgroud) 我想根据总和排序一个numpy数组.就像是
import numpy as np
a = np.array([1,2,3,8], [3,0,2,1])
b = np.sum(a, axis = 0)
idx = b.argsort()
Run Code Online (Sandbox Code Playgroud)
现在np.take(a,idx)导致[2,1,3,8].
但我想要一个数组:result = np.array([2,1,3,8],[0,3,2,1]]
什么是最聪明,最快速的方法?
我在C#中嵌入了IronPython 2.0.在IronPython中,我用以下内容定义了自己的异常:
def foobarException(Exception):
pass
Run Code Online (Sandbox Code Playgroud)
并将其提升到某个地方:
raise foobarException( "This is the Exception Message" )
Run Code Online (Sandbox Code Playgroud)
现在在C#中,我有:
try
{
callIronPython();
}
catch (Exception e)
{
// How can I determine the name (foobarException) of the Exception
// that is thrown from IronPython?
// With e.Message, I get "This is the Exception Message"
}
Run Code Online (Sandbox Code Playgroud) 我正在使用带有bottle.py的gevent-websocket来提供日志文件.如何从客户端检测到websocket连接是否已关闭?
目前我正在写作,直到我收到管道损坏错误:
return sock.send(data, flags)
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
但我想在服务器上正确检测客户端是否关闭了websocket连接.
我的代码看起来像:
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
import gevent.monkey
gevent.monkey.patch_all()
from bottle import route, Bottle, view, request, static_file
import json
import os
import time
app = Bottle()
# Other code
@app.route('/websocket/<filename>')
def ws_logfile(filename):
if request.environ.get('wsgi.websocket'):
ws = request.environ['wsgi.websocket']
try:
filename = os.path.join(os.getcwd(), "logfiles", filename)
logfile = file(filename)
lines = logfile.readlines()
for line in lines:
ws.send(json.dumps({'output': line}))
while True:
line = logfile.readline()
if line:
# Here detect if …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加两个具有Multiindex列和不同索引大小的数据帧.什么是最优雅的解决方案.例如:
names = ['Level 0', 'Level 1']
cols1 = pd.MultiIndex.from_arrays([['A', 'A', 'B'],['A1', 'A2', 'B1']], names = names)
cols2 = pd.MultiIndex.from_arrays([['A', 'A', 'B'],['A1', 'A3', 'B1']], names = names)
df1 = pd.DataFrame(np.random.randn(1, 3), index=range(1), columns=cols1)
df2 = pd.DataFrame(np.random.randn(5, 3), index=range(5), columns=cols2)
print(df1)
print(df2)
Level 0 A B
Level 1 A1 A2 B1
0 -0.116975 -0.391591 0.446029
Level 0 A B
Level 1 A1 A3 B1
0 1.179689 0.693096 -0.102621
1 -0.913441 0.187332 1.465217
2 -0.089724 -1.907706 -0.963699
3 0.203217 -1.233399 0.006726
4 …Run Code Online (Sandbox Code Playgroud)