我正在尝试在 Alpine 3.10 docker 容器内运行 GeckoDriver v0.26.0,特别是python:3.6.6-alpine3.10
.
在弄清楚一些事情之后,我碰壁了:
/ # geckodriver --version
Error relocating /usr/bin/geckodriver: __register_atfork: symbol not found
Error relocating /usr/bin/geckodriver: __res_init: symbol not found
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
首先启动 docker 容器:
docker run -it python:3.6.9-alpine3.10 /bin/sh
Run Code Online (Sandbox Code Playgroud)
然后尝试安装 GeckoDriver
/ # wget https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz
/ # tar -zxf geckodriver-v0.26.0-linux64.tar.gz -C /usr/bin
/ # geckodriver --version
/bin/sh: geckodriver: not found.
Run Code Online (Sandbox Code Playgroud)
真的吗?但我刚刚提取了它......嗯......好吧。它提取正确吗?是$PATH
正确的吗?
/ # ls -lah /usr/bin/geckodriver
-rwxr-xr-x 1 1000 1000 6.7M Oct 12 10:19 /usr/bin/geckodriver
/ # echo $PATH …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python 的 contextlib.ContextDecorator 类编写上下文管理器装饰器。
有没有办法在上下文管理器中访问装饰函数的参数?
这是我正在做的一个例子:
from contextlib import ContextDecorator
class savePen(ContextDecorator):
def __enter__(self):
self.prevPen = self.dc.GetPen() # AttributeError
return self
def __exit__(self, *exc):
self.dc.SetPen(self.prevPen)
return False
Run Code Online (Sandbox Code Playgroud)
鉴于上述,这:
@savePen()
def func(dc, param1, param2):
# do stuff, possibly changing the pen style
Run Code Online (Sandbox Code Playgroud)
应该相当于:
def func(dc, param1, param2):
prevPen = dc.GetPen()
# do stuff, possibly changing the pen style
dc.SetPen(prevPen)
Run Code Online (Sandbox Code Playgroud)
我已经冲刷的文档contextlib并没有发现任何有用的东西。
有谁知道如何从 ContextDecorator 类中访问装饰函数的属性?
正如@chepner 在此回复中所说,ContextDecorator 是糖
def func(dc, param1, param2):
with savePen():
...
Run Code Online (Sandbox Code Playgroud)
并且它不能访问函数的参数。
然而,在这种情况下,任何的内部运行 …
在 Python3.4 中,是否可以从 io.BytesIO 流打开 SQLite3 数据库?
类似于:
with open("temp.db", "rb") as openf:
byte_stream = io.BytesIO(openf.read())
sqlite3.connect(byte_stream)
Run Code Online (Sandbox Code Playgroud)
简短的故事是:我有一个流 ( byte_stream
),它是sqlite 数据库文件。出于安全原因,我无法执行以下操作(无法创建未加密的文件):
with open("temp.db", "wb") as openf:
openf.write(byte_stream)
sqlite3.connect("temp.db")
Run Code Online (Sandbox Code Playgroud)
是否有一些我找不到的用于 sqlite3 的低级 API?我假设sqlite3.connect
只是open()
在某个时候调用并无论如何都将文件作为字节流打开。我只是想跳过这open()
一步。
我正在玩Python的datetime.datetime.utcnow()方法,我注意到微秒值始终是相同的.
>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2015, 11, 16, 23, 20, 46, 42286)
>>> datetime.datetime.utcnow()
datetime.datetime(2015, 11, 16, 23, 20, 55, 505286)
>>> datetime.datetime.utcnow()
datetime.datetime(2015, 11, 16, 23, 21, 1, 552286)
Run Code Online (Sandbox Code Playgroud)
注意微秒值是如何总是286.为什么会这样?我该怎么做才能解决这个问题?
更多信息:time.time()
也总是有286us.毫秒值很好.我认为这实际上是根本原因,因为我相信datetime.datetime.utcnow()会调用time.time().
这是一个简短的脚本,表明它不仅仅是运气:
import datetime, random, time
for wait_time in [random.random() for _ in range(10)]:
time.sleep(wait_time)
print("Waited {}, result {}".format(wait_time, datetime.datetime.utcnow()))
Run Code Online (Sandbox Code Playgroud)
结果:
Waited 0.6074311218736113, result 2015-11-16 23:35:24.603286
Waited 0.960317012489652, result 2015-11-16 23:35:25.563286
Waited 0.13555474339177553, result 2015-11-16 23:35:25.698286
Waited 0.6179213307667111, result 2015-11-16 23:35:26.315286
Waited 0.2872301475401443, result 2015-11-16 23:35:26.602286 …
Run Code Online (Sandbox Code Playgroud) 假设我在脚本中定义了以下正则表达式。我想保留这些评论以备将来使用,因为我很健忘。
RE_TEST = re.compile(r"""[0-9] # 1 Number
[A-Z] # 1 Uppercase Letter
[a-y] # 1 lowercase, but not z
z # gotta have z...
""",
re.VERBOSE)
print(magic_function(RE_TEST)) # returns: "[0-9][A-Z][a-y]z"
Run Code Online (Sandbox Code Playgroud)
Python(3.4+)是否有办法将其转换为简单的字符串"[0-9][A-Z][a-y]z"
?
这个问题(“strip a verbose python regex”)似乎与我的要求非常接近,并且得到了回答。但那是几年前的事了,所以我想知道是否找到了新的(最好是内置的)解决方案。
除了上述之外,还有一些变通方法,例如使用隐式字符串连接,然后使用.pattern
属性:
RE_TEST = re.compile(r"[0-9]" # 1 Number
r"[A-Z]" # 1 Uppercase Letter
r"[a-y]" # 1 lowercase, but not z
r"z", # gotta have z...
re.VERBOSE)
print(RE_TEST.pattern) # returns: "[0-9][A-Z][a-y]z"
Run Code Online (Sandbox Code Playgroud)
或者只是单独评论模式而不编译它:
# matches pattern "nXxz" …
Run Code Online (Sandbox Code Playgroud) 我在Python中创建一个容器类,它将继承list
或仅实现所有标准列表方法(我真的不关心哪个).
如何创建仅对切片返回的项目起作用的方法?我已经能够制作一个能够作用于整个容器的方法(见下文),但我似乎无法弄清楚如何仅对切片采取行动.
我正在使用python 2.7.6并from __future__ import print_function, division
在我的所有代码中使用.
示例代码:
from __future__ import print_function, division
import itertools
class MyContainerClass(list):
""" For now, I'm inheriting from list. Is this my problem? """
def __init__(self, data_array):
list.__init__(self, data_array)
def __getitem__(self, *args):
arg = args[0]
# specific indices MyContainerClass[0, 3, 5] => indexes 0, 3, and 5
if isinstance(arg, (list, tuple)) and not isinstance(arg[0], bool):
return [list.__getitem__(self, _i) for _i in arg]
# standard slice notation
elif isinstance(arg, slice):
return …
Run Code Online (Sandbox Code Playgroud) 有一些项目使用名字Python ___ Authority
和py_a
缩写.
例如:
这些组提供高质量的代码,通常与开发人员和python核心团队共享.
我只是想知道:这些团体是以某种方式获得许可还是官方授权?例如,PSF是否管理/批准它们?
是否有逆误差函数的纯Python实现?
我知道 SciPy 有scipy.special.erfinv(),但这依赖于一些 C 扩展。我想要一个纯 python 实现。
我尝试使用维基百科和Wolfram参考文献编写自己的代码,但当 arg > 0.9 时,它似乎总是偏离真实值。
我还尝试移植 Scipy 使用的底层 C 代码(ndtri.c
以及cephespolevl.c
函数),但这也没有通过我的单元测试。
编辑:根据要求,我添加了移植的代码。
文档字符串(和文档测试)已被删除,因为它们比函数长。我还没有付出太多的努力来使端口更加Pythonic - 一旦我得到通过单元测试的东西,我就会担心这一点。
polevl.c
def polevl(x, coefs, N):
ans = 0
power = len(coefs) - 1
for coef in coefs[:N]:
ans += coef * x**power
power -= 1
return ans
def p1evl(x, coefs, N):
return polevl(x, [1] + coefs, N)
Run Code Online (Sandbox Code Playgroud)
def inv_erf(z):
if z < -1 …
Run Code Online (Sandbox Code Playgroud) 是否有更清洁或更pythonic的方式来做以下事情?
try:
error_prone_function(arg1)
except MyError:
try:
error_prone_function(arg2)
except MyError:
try:
another_error_prone_function(arg3)
except MyError:
try:
last_error_prone_function(arg4)
except MyError:
raise MyError("All backup parameters failed.")
Run Code Online (Sandbox Code Playgroud)
基本上是:如果尝试#1失败,请尝试#2.如果#2失败,请尝试#3.如果#3失败,请尝试#4.如果#4失败,......如果#n失败,那么最后会引发一些异常.
请注意,我不一定每次调用相同的函数,也不是每次都使用相同的函数参数.我是,但是,期待相同的异常MyError
各项功能.
我有一个wxPython/bs4应用程序,我正在使用cx_freeze构建一个exe.
构建成功没有错误,但尝试运行EXE会导致FeatureNotFound
BeautifulSoup4出错.它抱怨我没有安装我的lxml库.
我已经将程序剥离到它的最小状态并仍然得到错误.
有没有其他人使用cx_freeze成功构建bs4应用程序?
请查看下面的详细信息,并告诉我您可能有的任何想法.
谢谢,
我已经将应用程序简化为最基本的状态,但仍然会出错.我在Python 3.4上也得到了同样的错误.
Traceback (most recent call last):
File "C:\WinPython27\python-2.6.7\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "test.py", line 6, in <module>
File "C:\WinPython27\python-2.6.7\lib\site-packages\bs4\__init__.py", line 152, in __init__
% ",".join(feautres))
FeatureNotFound: Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?
Run Code Online (Sandbox Code Playgroud)
我发现有些人说我需要在构建脚本中包含lxml及其依赖项:http://sourceforge.net/p/cx-freeze/mailman/message/27973651/(对不起SF链接).我试过这个,但仍然没有骰子.
注释掉该行soup = BeautifulSoup("<tag>value</tag>", 'xml')
不会导致错误.
我想完成以下任务.想象一下类的某个方法中的以下代码(我懒得写出整个类).
foo = {"A": self.method1,
"B": self.method2,
"C": self.method3}
if var in foo.keys():
foo[var] # Executes one of the class methods
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用,否则我不会在这里问.
我已经尝试()
在每个字典值的末尾添加:self.method1()
,但这只是在创建foo时执行所有三种方法(正如我所料).
我也试过exec(foo[var])
但是引发了TypeError.
基本上我正在尝试完成与UI事件相同的事情.该事件以下列方式绑定:
Bind(event_signal, self.method1)
Run Code Online (Sandbox Code Playgroud)
当看到event_signal时,程序就会执行self.method1(event)
.self.method1()
如果var
是密钥,我怎样才能调用我的代码foo
?
python ×10
python-3.x ×4
alpine-linux ×1
cx-freeze ×1
datetime ×1
docker ×1
geckodriver ×1
list ×1
lxml ×1
math ×1
python-3.4 ×1
regex ×1
scipy ×1
slice ×1
sqlite ×1
wxpython ×1