我正在尝试编写一个使用Python 3.6类型提示的函数装饰器,以检查参数字典是否尊重类型提示,并且如果未出现带有问题清晰说明的错误,则将其用于HTTP API。
问题是,当函数具有使用Union类型的参数时,我无法在运行时根据其检查变量。
例如我有这个功能
from typing import Union
def bark(myname: str, descr: Union[int, str], mynum: int = 3) -> str:
return descr + myname * mynum
Run Code Online (Sandbox Code Playgroud)
我可以:
isinstance('Arnold', bark.__annotations__['myname'])
Run Code Online (Sandbox Code Playgroud)
但不是:
isinstance(3, bark.__annotations__['descr'])
Run Code Online (Sandbox Code Playgroud)
因为Union不能与isinstance或一起使用issubclass。
我找不到使用类型对象检查它的方法。我尝试自己执行检查,但是在REPL中bark.__annotations__['descr']显示typing.Union[int, str]时,如果不使用检查的丑陋技巧,我将无法在运行时访问类型列表bark.__annotations__['descr'].__repr__()。
是否有访问此信息的正确方法?还是故意在运行时不易于访问它?
我需要用一个小集群(~10 个服务器)处理一些大文件(~2 TB),以便生成一个相对较小的报告(一些 GB)。
我只关心最终报告,不关心中间结果,而且机器有大量 RAM,因此最好使用它来减少尽可能多的磁盘访问(从而提高速度),最好是通过存储数据仅在使用磁盘时才在易失性内存中阻塞。
查看配置文件和上一个问题,似乎 Hadoop 不提供此功能。Spark 网站讨论了 memory_and_disk选项,但我更愿意要求公司部署基于新语言的新软件。
唯一的“解决方案”,我发现是设置 dfs.datanode.data.dir为/dev/shm/在HDFS-default.xml中,欺骗它在这种情况下,它会表现得不好使用非易失性存储器,而不是文件系统来存储数据,但是,我认为,当RAM得到充分它使用交换。
有没有什么技巧可以让 Hadoop 尽可能多地将数据块存储在 RAM 上并仅在必要时才写入磁盘?
我有一个带有 pytest 套件的 Python 3 项目,想在 Travis 上运行它。
我正在尝试编写一个test.sh既可以在开发人员机器上也可以在 Travis CI 上运行的脚本,它必须设置 virtualenv,安装包,运行 pytest,然后销毁 virtualenv。我不能使用 Tox,因为它不遵循我们正在使用的依赖链接。
脚本(简化)是:
python3 -m venv .venv
pip install -e . --process-dependency-links --allow-all-external
.venv/bin/pip install pytest
.venv/bin/pytest
rm -rf .venv
Run Code Online (Sandbox Code Playgroud)
在我的机器上它可以工作,但在 Travis 上这失败了,因为里面.venv/bin没有pip可执行文件,pip2或者pip3。
我试图强制安装它,.venv/bin/python3 -m pip install --upgrade pip但仍然没有二进制文件。
运行python3 -m venv --help我看到有一个可选标志--without-pip可用,并且相同的帮助文本说pip is bootstrapped by default. 如何确保 pip 存在于 virtualenv 中?
编辑:一个可能的解决方案是使用python3 -m pip install pytest …
我正在尝试使用 Python 中 Virtualbox 6.1 的 SOAP 接口来获取机器的屏幕截图。我可以启动机器,但每当我尝试检索屏幕布局时都会出现锁定错误。
这是代码:
import zeep
# helper to show the session lock status
def show_lock_state(session_id):
session_state = service.ISession_getState(session_id)
print('current session state:', session_state)
# connect
client = zeep.Client('http://127.0.0.1:18083?wsdl')
service = client.create_service("{http://www.virtualbox.org/}vboxBinding", 'http://127.0.0.1:18083?wsdl')
manager_id = service.IWebsessionManager_logon('fakeuser', 'fakepassword')
session_id = service.IWebsessionManager_getSessionObject(manager_id)
# get the machine id and start it
machine_id = service.IVirtualBox_findMachine(manager_id, 'Debian')
progress_id = service.IMachine_launchVMProcess(machine_id, session_id, 'gui')
service.IProgress_waitForCompletion(progress_id, -1)
print('Machine has been started!')
show_lock_state(session_id)
# unlock and then lock to be sure, doesn't have any effect …Run Code Online (Sandbox Code Playgroud) 我正在将 aiohttp 2 与 Python 3.6 一起使用,并希望记录进入应用程序的请求。
我做了:
# use ISO timestamps
from time import gmtime
logging.Formatter.converter = gmtime
# create a formatter
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s - %(message)s', '%Y-%m-%dT%H:%M:%S')
ch.setFormatter(formatter)
# show all emssages (default is WARNING)
logging.getLogger('aiohttp.access').setLevel(logging.DEBUG)
# attach the handler
logging.getLogger('aiohttp.access').addHandler(ch)
Run Code Online (Sandbox Code Playgroud)
现在,当应用程序运行时,我会得到以下格式的日志:
2017-04-19T16:02:17 INFO aiohttp.access - 127.0.0.1 - - [19/Apr/2017:16:02:17 +0000] "GET /test HTTP/1.1" 404 547 "-" "curl/7.51.0"
Run Code Online (Sandbox Code Playgroud)
该message组件有一个冗余时间戳,我想自定义其格式。该文件说,它应该是可能的,但我不明白如何使它实际工作,也没有代码示例。
我只发现了这种用法,但有:
mylogger = logging.Logger('aiohttp.access')
mylogger.setLevel(logging.DEBUG)
mylogger.addHandler(ch)
handler = …Run Code Online (Sandbox Code Playgroud) python-3.x ×3
python ×2
aiohttp ×1
annotations ×1
caching ×1
hadoop ×1
hdfs ×1
logging ×1
pip ×1
python-3.6 ×1
soap ×1
travis-ci ×1
typechecking ×1
virtualbox ×1