假设我有一个函数:
def get_some_date(some_argument: int=None) -> %datetime_or_None%:
if some_argument is not None and some_argument == 1:
return datetime.utcnow()
else:
return None
我如何为可以为None的东西指定返回类型?
假设我有一个函数:
def get_some_date(some_argument: int=None) -> %datetime_or_None%:
if some_argument is not None and some_argument == 1:
return datetime.utcnow()
else:
return None
我如何为可以为None的东西指定返回类型?
我在Windows上使用PyCharm,想更改设置,将最大行长限制为79个字符,而不是默认的120个字符。
我可以在哪里更改PyCharm中的每行最大字符数?
假设我有一个大小为n的std::vector(让我们称之为myVec),构造一个由元素X到Y的副本组成的新向量,其中0 <= X <= Y <= N-1,最简单的方法是什么?例如,大小为150000的向量中的myVec[100000]到myVec[100999]。
如果这不能有效地用一个向量,是否有另一种STL数据类型,我应该使用代替?
我应该用
std::sort(numbers.begin(), numbers.end(), std::greater<int>());
or
std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators
按降序对向量排序?这两种方法有什么优点或缺点吗?
我正在使用Socket运行一个Express.js应用程序。IO的聊天网络应用程序 我在24小时内随机得到了5次以下错误。 节点进程永远被包装起来,并立即重新启动自己。
问题是重新启动Express会把我的用户赶出他们的房间 没有人希望这样。
web服务器通过HAProxy代理。插座不存在稳定性问题, 只是使用websockets和flashsockets传输。 我不能故意复制这个。
这是节点v0.10.11的错误:
events.js:72
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET //alternatively it s a 'write'
at errnoException (net.js:900:11)
at TCP.onread (net.js:555:19)
error: Forever detected script exited with code: 8
error: Forever restarting script for 2 time
编辑(2013-07-22)
增加了两个socket。IO客户端错误处理程序和未捕获的异常处理程序。 这个似乎捕获了错误:
process.on('uncaughtException', function (err) {
console.error(err.stack);
console.log("Node NOT Exiting...");
});
所以我怀疑这不是插座。发送HTTP请求到另一个服务器 或者MySQL/Redis连接。问题在于错误堆栈 不能帮我找出我的代码问题。以下是日志输出:
Error: read ECONNRESET
at errnoException (net.js:900:11)
at TCP.onread (net.js:555:19)
我怎么知道是什么引起的呢?我如何从错误中得到更多?
好吧,不是很啰嗦,但这里是与朗约翰的堆栈跟踪:
Exception caught: Error ECONNRESET
{ [Error: read ECONNRESET]
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read',
__cached_trace__:
[ { receiver: [Object],
fun: [Function: errnoException],
pos: 22930 },
{ receiver: [Object], fun: [Function: onread], pos: 14545 },
{},
{ receiver: [Object],
fun: [Function: fireErrorCallbacks],
pos: 11672 },
{ receiver: [Object], fun: [Function], pos: 12329 },
{ receiver: [Object], fun: [Function: onread], pos: 14536 } ],
__previous__:
{ [Error]
id: 1061835,
location: 'fireErrorCallbacks (net.js:439)',
__location__: 'process.nextTick',
__previous__: null,
__trace_count__: 1,
__cached_trace__: [ [Object], [Object], [Object] ] } }
这里我提供了flash套接字策略文件:
net = require("net")
net.createServer( (socket) =>
socket.write("<?xml version=\"1.0\"?>\n")
socket.write("<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\n")
socket.write("<cross-domain-policy>\n")
socket.write("<allow-access-from domain=\"*\" to-ports=\"*\"/>\n")
socket.write("</cross-domain-policy>\n")
socket.end()
).listen(843)
这是原因吗?
iPhone上的Safari会自动为出现在电话号码中的数字字符串创建链接。我正在编写一个包含IP地址的网页,Safari将其转换为电话号码链接。是否可以为整个页面或页面上的某个元素禁用此行为?
我的网站在iPhone/Safari浏览器上呈现得很好,只有一个例外:我的文本输入字段有一个奇怪的圆形样式,这与我网站的其他部分看起来一点都不好看。
是否有一种方法来指导Safari(通过CSS或元数据)不四舍五入的输入字段,并将它们呈现为预期的矩形?
我正在尝试使用Python的类型注释和抽象基类来编写一些接口。是否有一种方法来注释*args和**kwargs的可能类型?
例如,如何表达一个函数的合理参数是一个整型或两个整型?type(args)给出元组,所以我的猜测是将类型注释为Union[Tuple[int, int], Tuple[int]],但这行不通。
from typing import Union, Tuple
def foo(*args: Union[Tuple[int, int], Tuple[int]]):
try:
i, j = args
return i + j
except ValueError:
assert len(args) == 1
i = args[0]
return i
# ok
print(foo((1,)))
print(foo((1, 2)))
# mypy does not like this
print(foo(1))
print(foo(1, 2))
来自myypy的错误消息:
t.py: note: In function "foo":
t.py:6: error: Unsupported operand types for + ("tuple" and "Union[Tuple[int, int], Tuple[int]]")
t.py: note: At top level:
t.py:12: error: Argument 1 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
t.py:14: error: Argument 1 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
t.py:15: error: Argument 1 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
t.py:15: error: Argument 2 to "foo" has incompatible type "int"; expected "Union[Tuple[int, int], Tuple[int]]"
myypy不喜欢这个函数调用是有道理的,因为它期望在调用本身中有一个元组。unpacking后的添加也给出了一个我不理解的输入错误。
如何注释*args和**kwargs的敏感类型?
我试图用pyplot绘制一个简单的图形,例如:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show()
但是这个图没有出现,我得到了以下消息:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
我在几个地方看到必须使用以下命令更改matplotlib的配置:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
我这样做了,但得到了一个错误消息,因为它找不到一个模块:
ModuleNotFoundError: No module named 'tkinter'
然后,我尝试使用pip install tkinter(在虚拟环境中)安装“tkinter”,但它没有找到它:
Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
我还应该提到,我是在使用虚拟环境的Pycharm Community Edition IDE上运行所有这些,并且我的操作系统是Linux/Ubuntu 18.04。
我想知道我如何解决这个问题,以便能够显示图形。
为什么c++ STL不提供任何“树”容器,用什么代替最好?
我想将对象的层次结构存储为树,而不是使用树来增强性能……