其中负责的代码-我的问题很简单,提高 ConnectionResetError对cpython3以下调用self._sslobj.read(len, buffer)上ssl.py?
我有时候ConnectionResetError尝试用ssl连接S3.这个错误很少发生,所以重现它很棘手.
# trimmed stacktrace
File "/MYPROJECT/MY_FUNC.py", line 123, in <genexpr>
rows = (row for row in reader)
File "/XXX/lib/python3.6/csv.py", line 112, in _next_
row = next(self.reader)
File "/XXX/lib/python3.6/tarfile.py", line 706, in readinto
buf = self.read(len(b))
File "/XXX/lib/python3.6/tarfile.py", line 695, in read
b = self.fileobj.read(length)
File "/XXX/lib/python3.6/gzip.py", line 276, in read
return self._buffer.read(size)
File "/XXX/lib/python3.6/_compression.py", line 68, in readinto
data = self.read(len(byte_view))
File "/XXX/lib/python3.6/gzip.py", line 469, in …Run Code Online (Sandbox Code Playgroud) original question: i got a StringIO object, how can i convert it into BytesIO?
update: The more general question is, how to convert a binary (encoded) file-like object into decoded file-like object in python3?
the naive approach i got is:
import io
sio = io.StringIO('wello horld')
bio = io.BytesIO(sio.read().encode('utf8'))
print(bio.read()) # prints b'wello horld'
Run Code Online (Sandbox Code Playgroud)
is there more elegant way of doing this?
for example, for the reverse question (BytesIO -> StringIO) there exist a class - io.TextIOWrapper …
我想将请求 Response对象序列化为json,最好是HAR格式。
import requests
resp = requests.get('http://httpbin.org/get')
har = to_har(resp) # <--- magic
Run Code Online (Sandbox Code Playgroud)
但是用我的Google Fu功能在网上找不到任何东西。
似乎所有数据都存在于该Response对象上,我希望我不需要实现整个HAR规范,并且存在一些可以重用的代码/实用程序。
一个有效的答案可能是:现有的库,或者如果python和和/或到目前为止都不存在,请参考起点requests。
当前,我对Response对象的3min解决方案(不是HAR格式)更简单的序列化看起来像这样(如果不存在,可能是一个很好的起点):
def resp2dict(resp, _root=True):
d = {
'text': resp.text,
'headers': dict(resp.headers),
'status_code': resp.status_code,
'request': {
'url': resp.request.url,
'method': resp.request.method,
'headers': dict(resp.request.headers),
},
}
if _root:
d['history'] = [resp2dict(h, False) for h in resp.history]
return d
Run Code Online (Sandbox Code Playgroud)
我之所以发表此文章,是因为我认为Response,无论HAR格式如何,我不仅会努力将对象序列化为json。
我使用的logging.config.dictConfig()设置登录到文件中使用FileHandler和RotatingFileHandler。dictConfig()多次打电话是不好的做法吗?如果是为什么?
我正在寻找一种方法来向其他开发人员和可选的客户公开我工作场所的过滤功能。
我想基于向我的其他开发人员以及后来向我们的客户公开的用户定义过滤器,对我的数据(python dicts)实现一种简单的查询语言。
在我的 dict / json 数据上公开 SQL 接口会很棒(我不想设置服务器)
db = [
{'first': 'john', 'last': 'doe', 'likes': ['cookies', 'http']},
{'first': 'jane', 'last': 'doe', 'likes': ['cookies', 'donuts']},
{'first': 'danny', 'last': 'foo', 'likes': ['http', 'donuts']},
]
query = '(first == "john" or last == "doe") and likes contains "cookies"'
results = run_query(db, query)
Run Code Online (Sandbox Code Playgroud)
这应该返回(在结果中):
[
{'first': 'john', 'last': 'doe', 'likes': ['cookies', 'http']},
{'first': 'jane', 'last': 'doe', 'likes': ['cookies', 'donuts']},
]
Run Code Online (Sandbox Code Playgroud)
注意:我不介意更改运算符名称,例如or -> OR contains …
有人可以向我解释 Rust 每晚的“生产”情况吗?
我想使用PyO3板条箱,它使用需要夜间 Rust的专业化功能。
使用 Rust 的每晚版本是否已做好生产准备?我知道事情可能会在未来的版本中出现问题,并且可能会引入 API 更改,但就质量/测试/生产准备而言,每晚是否安全?
从这个关于 Rust 用户的线程看来,只要我限制我的非稳定功能的使用(例如,仅限于专业化),我应该没问题?
我试图删除在我的词汇中出现一次的单词以减少我的词汇量.我正在使用sklearn TfidfVectorizer(),然后在我的数据框上使用fit_transform函数.
tfidf = TfidfVectorizer()
tfs = tfidf.fit_transform(df['original_post'].values.astype('U'))
Run Code Online (Sandbox Code Playgroud)
我首先想到的是tfidf矢量化器中的预处理器字段,或者在机器学习之前使用预处理包.
任何进一步实施的提示或链接?
我试图找到一种可靠的方法来衡量2个术语的语义相似性.第一个度量可以是下义/上位图上的路径距离(最终,2-3个度量的线性组合可能更好......).
from nltk.corpus import wordnet as wn
dog = wn.synset('dog.n.01')
cat = wn.synset('cat.n.01')
print(dog.path_similarity(cat))
Run Code Online (Sandbox Code Playgroud)
n.01方法,为什么它是必要的.python ×7
python-3.x ×5
encoding ×1
har ×1
io ×1
json ×1
logging ×1
nlp ×1
nltk ×1
production ×1
python-2.7 ×1
rust ×1
scikit-learn ×1
semantics ×1
stream ×1
tf-idf ×1