我正在构建一个 SMTP 服务器,并aiosmtpd使用这些示例作为构建的基础。下面是程序入口点的代码片段。
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.create_task(amain(loop=loop))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,我收到以下警告:
server.py:61: DeprecationWarning: There is no current event loop
loop = asyncio.get_event_loop()
Run Code Online (Sandbox Code Playgroud)
实现这个的正确方法是什么?
假设我有一个bash函数
Yadda() {
# time-consuming processes that must take place sequentially
# the result will be appended >> $OUTFILE
# $OUTFILE is set by the main body of the script
# No manipulation of variables in the main body
# Only local-ly defined variables are manipulated
}
Run Code Online (Sandbox Code Playgroud)
我是否可以在子shell中调用该函数作为后台作业?例如:
OUTFILE=~/result
for PARM in $PARAMLIST; do
( Yadda $PARM ) &
done
wait
cat $OUTFILE
Run Code Online (Sandbox Code Playgroud)
你怎么看?
所以我有一个函数(我们称之为fun1),它接受一个函数作为参数。但在内部fun1,我需要访问参数的__self__,该参数仅当函数是绑定方法时才存在。
函数本身必须接受两个str参数并返回一个bool。
换句话说,像这样:
MyFuncType = Callable[[str, str], bool]
# fun1 is an unbound function
def fun1(func: MyFuncType):
...
o = func.__self__
...
# Some logic on the "o" object, such as logging the object's class,
# doing some inspection, etc.
...
Run Code Online (Sandbox Code Playgroud)
如果我MyFuncType像上面那样使用,PyCharm 会抱怨 that __self__is not a attribute of func.
那么,我应该用什么类型提示func进行注释,以便 PyCharm(可能还有 mypy)不会对该行提出抗议?
(顺便说一句,我正在使用Python 3.6 )
我想让脚本成为自我守护,即不需要nohup $SCRIPT &>/dev/null &在shell提示符下手动调用.
我的计划是创建一段代码,如下所示:
#!/bin/bash
SCRIPTNAME="$0"
...
# Preps are done above
if [[ "$1" != "--daemonize" ]]; then
nohup "$SCRIPTNAME" --daemonize "${PARAMS[@]}" &>/dev/null &
exit $?
fi
# Rest of the code are the actual procedures of the daemon
Run Code Online (Sandbox Code Playgroud)
这是明智的吗?你有更好的选择吗?
我正在制作一个自定义的ComboBox,继承自Winforms的标准ComboBox.对于我的自定义组合框,我定DrawMode要OwnerDrawFixed和DropDownStyle到DropDownList.然后我写自己的OnDrawItem方法.但我这样结束了:

如何使我的自定义组合框看起来像标准组合?
在四处搜索之后,我找到了这ButtonRenderer堂课.它提供了一个DrawButton静态/共享方法 - 顾名思义 - 绘制正确的3D按钮.我现在正在试验它.
我尝试使用我能想到的各种对象的图形属性,但我总是失败.最后,我尝试了表单的图形,显然有些东西覆盖了我的按钮.
这是代码:
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim TextToDraw As String = _DefaultText
__Brush_Window.Color = Color.FromKnownColor(KnownColor.Window)
__Brush_Disabled.Color = Color.FromKnownColor(KnownColor.GrayText)
__Brush_Enabled.Color = Color.FromKnownColor(KnownColor.WindowText)
If e.Index >= 0 Then
TextToDraw = _DataSource.ItemText(e.Index)
End If
If TextToDraw.StartsWith("---") Then TextToDraw = StrDup(3, ChrW(&H2500)) ' U+2500 is "Box Drawing Light Horizontal"
If (e.State And DrawItemState.ComboBoxEdit) > 0 Then
'ButtonRenderer.DrawButton(e.Graphics, …Run Code Online (Sandbox Code Playgroud) vb.net combobox custom-controls visual-studio-2010 ondrawitem
Is there a "lenient" JSON Parser for Python?
I keep getting (handwritten) JSON files such as this:
/* This JSON file is created by someone who does not know JSON
And not competent enough to search about "JSON Validators" */
{
/* Hey look!
A honkin' block comment here!
Yeehaw */
"key1": "value1", // Hey look there's a standard-breaking comment here!
"key3": .65, // I'm too lazy to type "0"
"key4": -.75, // That "other" .Net program works anyways...
"key5": …Run Code Online (Sandbox Code Playgroud) 我们pytest可以定义一个session作用域固定装置,因此以下
@pytest.fixture(scope="session", autouse=True)
def some_fixt():
... some setup procedure ...
yield
... some cleanup procedure ...
Run Code Online (Sandbox Code Playgroud)
将在测试会话开始之前自动进行设置,并在测试结束后进行清理。
中有类似的东西吗unittest?我能找到的最好的是setUpModule+ tearDownModule,但这只是每个模块,需要在不同的测试模块之间复制粘贴。
编辑:这是我当前的解决方法:
对于每个测试模块:
from contextlib import ExitStack
...
ModuleResources = ExitStack()
def setUpModule():
# For funcs/objects that can act as a context_manager
ModuleResources.enter_context(
... something that can act as a context_manager ...
)
# For funcs/objects that cannot act as a context manager,
# but has a "cleanup" method such as "stop" …Run Code Online (Sandbox Code Playgroud) MSDN文档表明,由TPL启动的线程将享有更好的调度.但是,由于线程基于ThreadPool,因此它们将实现为后台线程.
现在,我希望并行执行一些任务,但必须执行这些任务直到完成.
那么,我如何创建本质上是前台线程的任务,但仍然享受TPL提供的增强调度?
我正在尝试将MurmurHash3的C#实现移植到VB.Net.
它运行...但有人可以提供一些已知的测试向量来验证正确性吗?
提前致谢.
编辑:我将实现限制为只有32位MurmurHash3,但如果你也可以提供64位实现的向量,也会很好.
假设我有这样的代码:
Public Interface ISomething
....
End Interface
Public Class SomeClass
Implements ISomething
....
End Class
Run Code Online (Sandbox Code Playgroud)
现在,如果我从SomeClass继承如下:
Public Class InheritedClass
Inherits SomeClass
....
End Class
Run Code Online (Sandbox Code Playgroud)
将InheritedClass自动实现ISomething,还是必须Implements ISomething在InheritedClass的定义中使用?
我一直在为这个问题抓狂
有问题的代码是此处开源项目的一部分:aiosmtpd(我的实际 FOSS 项目的分支,此处)
有问题的文件是这个:main.py
出现问题的代码位于main.py的第139行
这是一个片段:
...
from aiosmtpd.smtp import DATA_SIZE_DEFAULT, SMTP, __version__
...
...
# args is the result of ArgumentParser.parse_args
factory = partial(
SMTP, args.handler,
data_size_limit=args.size, enable_SMTPUTF8=args.smtputf8)
...
server = loop.run_until_complete(
loop.create_server(factory, host=args.host, port=args.port))
...
Run Code Online (Sandbox Code Playgroud)
有时- 也就是说,并非总是- 代码在此时失败RuntimeError: Event loop stopped before Future completed.
我的问题:
这种间歇性故障的原因可能是什么?
在测试过程中,大约有 10% 的时间会失败(使用 tox + nostest2),但 90% 的时间都进展顺利。
我应该如何检测和/或检查和/或断言以防止这种情况发生?
“恢复”错误并重做操作的最佳策略是什么?
我将第n次强调错误是间歇性发生的。虽然不常见,但经常发生,所以我觉得有必要追查根本原因。发生错误后,如果我立即或延迟后再次重新运行代码,则几乎总是不会发生相同的错误。
python ×3
vb.net ×3
bash ×2
python-3.x ×2
.net ×1
aiosmtpd ×1
background ×1
combobox ×1
daemon ×1
function ×1
inheritance ×1
interface ×1
json ×1
murmurhash ×1
ondrawitem ×1
python-3.6 ×1
subshell ×1
type-hinting ×1