有没有办法在Python中序列化使用元组作为键的字典:
a={(1,2):'a'}
Run Code Online (Sandbox Code Playgroud)
简单地使用json.dumps(a),产生:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
for chunk in self._iterencode_dict(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 268, in _iterencode_dict
raise TypeError("key {0!r} is not a string".format(key))
TypeError: key (1, 2) is not a string
Run Code Online (Sandbox Code Playgroud) 我正在python中编写一个简单的Web服务器.这是我的代码的简化版本:
class StreamerHandler(SimpleHTTPRequestHandler):
def do_POST(self):
try:
length = int(self.headers.getheader('content-length'))
data = self.rfile.read(length)
self.send_response(200, "OK")
#process_data(data, self.client_address)
except Exception as inst:
logging.error(type(self).__name__ + "/" + type(inst).__name__ + " (" + inst.__str__() + ")")
class Streamer(TCPServer):
def __init__(self, overlay):
self.allow_reuse_address = True
TCPServer.__init__(self, ("", port), StreamerHandler)
Run Code Online (Sandbox Code Playgroud)
我想做的是发送响应关闭TCP连接,然后运行process_data方法,这可能需要很长时间才能完成.
有没有办法实现这个目标?我能想到的唯一解决方案是使用专用线程来处理.
我在python中使用SocketServer模块在我的应用程序中实现一个简单的嵌入式Web服务器.目前我正在专用线程中调用serve_forver方法,以便继续执行我的应用程序.
我想知道是否有更多的pythonic方式来启动我的SocketServer并将执行重新恢复到我的应用程序.
我有一些包含bin文件(图像,流量跟踪转储等)的subversion仓库。问题是对那些文件的几次提交导致了一个很大的存储库。由于我们仅出于历史目的保留回购协议,是否有可能重置回购协议的历史记录?我基本上是在寻找一种简单的替代方法来导出内容并重新创建存储库。
让我们以 Tornado 主页中的 hello world 应用程序为例:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)
有没有办法,在 IOloop 启动后而不停止它,基本上停止应用程序并启动另一个应用程序(在同一端口或另一个端口上)?
我看到我可以在运行时添加新应用程序(侦听不同端口),但我不知道如何停止现有应用程序。
请考虑以下代码:
class Foo():
pass
Foo.entries = dict()
a = Foo()
a.entries['1'] = 1
b = Foo()
b.entries['3'] = 3
print(a.entries)
Run Code Online (Sandbox Code Playgroud)
这将打印:
{'1': 1, '3': 3}
Run Code Online (Sandbox Code Playgroud)
因为条目是作为静态属性添加的.有没有办法猴子修补类定义以添加新属性(不使用继承).
我设法找到了以下方式,但它看起来很复杂:
def patch_me(target, field, value):
def func(self):
if not hasattr(self, '__' + field):
setattr(self, '__' + field, value())
return getattr(self, '__' + field)
setattr(target, field, property(func))
patch_me(Foo, 'entries', dict)
Run Code Online (Sandbox Code Playgroud) 我可以在扩展其专业化的类中重载模板类函数吗?
我有以下代码(我试图将其简化为最低限度):
#include <iostream>
using namespace std;
class X {
public:
unsigned test_x() {
return 1;
}
};
class Y {
public:
unsigned test_y() {
return 2;
}
};
template <typename T, typename U>
class A {
public:
unsigned foo(U i) {
cout << "A" << endl;
return i.test_x();
}
unsigned bar(T i) {
return foo(i);
}
};
class B : public A<Y, X> {
public:
unsigned foo(Y i) {
cout << "B" << endl;
return i.test_y();
}
};
int …Run Code Online (Sandbox Code Playgroud)