TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Alphareader: a custom separator and endline file reader in Python

3 点作者 canimus大约 5 年前

1 comment

eesmith大约 5 年前
Some tweaks:<p>#1:<p><pre><code> elif not isinstance(fn_transform, FunctionType) or not isinstance(fn_transform, LambdaType): raise TypeError(&#x27;Transformation parameter should be a function or lambda i.e. fn = lambda x: x.replace(a,b)&#x27;) </code></pre> What about using callable()? There&#x27;s no reason you couldn&#x27;t use a functor, for example.<p>#2:<p><pre><code> curr = file_handle.read(chunk_size) if encoding: curr = curr.decode(encoding) </code></pre> That assumes a single-byte encoding. Consider a multi-byte encoding where the chunk_size reads only part of the character:<p><pre><code> &gt;&gt;&gt; s=&quot;ü&quot; &gt;&gt;&gt; s.encode(&quot;utf8&quot;)[:1].decode(&quot;utf8&quot;) Traceback (most recent call last): File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt; UnicodeDecodeError: &#x27;utf-8&#x27; codec can&#x27;t decode byte 0xc3 in position 0: unexpected end of data </code></pre> #3:<p><pre><code> if chr(terminator) in chunk: lines = chunk.split(chr(terminator)) </code></pre> Might want to compute chr(terminator) once, rather than re-evaluate it each time.<p>#4:<p><pre><code> try: transformations = iter(fn_transform) yield list(map(lambda x: reduce(lambda a,b: b(a), fn_transform, x), columns)) except TypeError: yield list(map(fn_transform, columns)) </code></pre> Since you&#x27;ve already checked for the two cases, set a flag to remember what fn_transform contains. Then branch on that, rather than use the try&#x2F;except.<p>Otherwise, consider what happens if one of the callables raises a TypeError because of an internal error, rather than because of an expected structural mismatch.
评论 #22697977 未加载