Why does str(float) return more digits in Python 3 than Python 2?

No, there’s no PEP. There’s an issue in the bug tracker, and an associated discussion on the Python developers mailing list. While I was responsible for proposing and implementing the change, I can’t claim it was my idea: it had arisen during conversations with Guido at EuroPython 2010. Some more details: as already mentioned in … Read more

‘is’ operator behaves differently when comparing strings with spaces

Warning: this answer is about the implementation details of a specific python interpreter. comparing strings with is==bad idea. Well, at least for cpython3.4/2.7.3, the answer is “no, it is not the whitespace”. Not only the whitespace: Two string literals will share memory if they are either alphanumeric or reside on the same block (file, function, … Read more

aiohttp web.response body as json

You can use web.json_response: async def api_server(request): res = {“q”: “qqq”, “a”: “aaa”} return web.json_response(res) Furthermore the json_response has additional parameters, like: json_response(data, text=None, body=None, status=200, reason=None, headers=None, content_type=”application/json”, dumps=json.dumps) Most of the parameters are the same as the generic web.Response(..), but the dumps is more interesting: it is a reference to a method that … Read more

PerformanceWarning: DataFrame is highly fragmented. This is usually the result of calling `frame.insert` many times, which has poor performance

Aware that this might be a reply that some will find highly controversial, I’m still posting my opinion here… Proposed answer: Ignore the warning. If the user thinks/observes that the code suffers from poor performance, it’s the user’s responsibility to fix it, not the module’s responsibility to propose code refactoring steps. Rationale for this harsh … Read more

Replace values in a pandas series via dictionary efficiently

One trivial solution is to choose a method dependent on an estimate of how completely values are covered by dictionary keys. General case Use df[‘A’].map(d) if all values mapped; or Use df[‘A’].map(d).fillna(df[‘A’]).astype(int) if >5% values mapped. Few, e.g. < 5%, values in d Use df[‘A’].replace(d) The “crossover point” of ~5% is specific to Benchmarking below. … Read more

Modifying logging message format based on message logging level in Python3

With a bit of digging, I was able to modify the Python 2 solution to work with Python 3. In Python2, it was necessary to temporarily overwrite Formatter._fmt. In Python3, support for multiple format string types requires us to temporarily overwrite Formatter._style._fmt instead. # Custom formatter class MyFormatter(logging.Formatter): err_fmt = “ERROR: %(msg)s” dbg_fmt = “DBG: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)