Posts

Changing decimal comma to point in logging asctime

For some years, I had been eying at the logging output of the following code: FORMAT='%(asctime)s %(message)s' logging.basicConfig(format=FORMAT, level='INFO') logging.info('Comma, where is the point?') It would produce: 2017-07-18 13:44:41,837 Comma, where is the point? I am certain that the decimal mark was a full stop or period and it was to six decimals instead of just three, but somewhere in the line, it defaulted to comma.  It could conform to a standard, but I just find it too unusual to read, the comma catches my eyes every time I scan through. Finally, I decided to look into how to change it back, and the following code does the job: FORMAT='%(asctime)s.%(msecs)03d %(message)s' logging.basicConfig(format=FORMAT, datefmt='%Y-%m-%d %H:%M:%S', level='INFO') This only changes the decimal mark, the rest stays the same.  The datafmt argument is to change the asctime field format, but you need to use msecs field for the mi

Element-wise Operation on Iterables

A simple example is to add two lists item-or-element-wisely, there are a few methods to achieve it, using map with operator.add or lambda, list comprehension or libraries like numpy. There are pros and cons, some are easier to expand to operate on more iterables like summation on elements over three lists, others are easier to change the operations like add elements from first two and subtract element from the third. The following is the benchmark of each method, ran with 3.4.5: With 10,000 elements, average time of 1,000 runs map with operator.add : list(map(add, X, Y)) = 1,746 us map with lambda : list(map(add, X, Y)) = 3,209 us map with sum and zip : list(map(sum, zip(X, Y))) = 2,853 us list comprehension with sum and zip: [sum(z) for z in zip(X, Y)] = 3,901 us list comprehension with + and zip : [x + y for x, y in zip(X, Y)] = 1,665 us numpy : X + Y

Opening Note

This blog is the first blog that I created for a specific programming language, in this case, the Python.  I used to have a few computer-related blogs, some general topic, one for programming and one for web stuff.  But never created one for a particular subject, always broader.. This will be only about Python coding and nothing else.  Notes for things that I have learned and experiences about issues that I've encountered and resolved. I have been coding in Python for about a decade, but still not having much of results to show, perhaps this blog would change things for me if I work hard to write down stuff I find educational. My experience with Python starting from around Python 2.3 on Windows, then after I switched to Linux, it's the virtually the only language I coded, the only other languages I still code is C and Bash.  In the era of Python 3, this blog would only have codes that almost guarantee to run with Python 3.X. Since this is for notes, there probably is no