Python – my personal FAQ with StackOverflow

I’m not ashamed, no, not all … really! ;-): I wrote my first, worth-mentioning Python-script only in November 2014. I’m not new to script-languages, but so far I haven’t had the need nor really the opportunity to write a Python-based one myself from scratch.

The task was to write a small text-processor (others might call it a parser) for a proprietary and simple, linear (no branching), variable-argument-count, command-based language – to debug scripts written in this language.

When advancing, and thus learning how to write Python, I extensively used StackOverflow.com for finding answers to my questions. In retrospective, I figured out that in fact all the answers I needed were from SO. Reading my profile became a quite an interesting collection of Qs and As – so I thought, why not making a post about it, not the least for my own reference. My script is written in Python 3 (so print is now a function).

Python – language in general

Sometimes, while coding, I sometimes don’t want to fill an ifelse-block. How about ’empty if statement’ in pythonpass is an empy block, like {} is in C

When writing the grammer for my parser I needed to Determine the type of an object?, or in reality the type of a variable. type() does that.

Referencing to function/method-arguments in the callee as a list or dict is answered here: What does ** (double star) and * (star) do for Python parameters? This allows variable number of arguments for callers.

What does `if __name__ == “__main__”:` do? – good question, seems to be the main-function in Python. The answer is good.

Ever wondered why regex-strings are prefixed with rWhat exactly do “u” and “r” string flags do in Python, and what are raw string literals?

That all values are passed by reference is explained here: How do I pass a variable by reference?

I haven’t understood all quirks for Unicode yet, but having set LANG to a UTF-8 based language is important. I had to passthru UTF-8-files to my output and my system was LANG=C I had UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position 1.

Why do people write #!/usr/bin/env python on the first line of a Python script? The shebang-line in a Python-script should use env.

Argparse optional positional arguments? Positional arguments are args without a switch.

How to calculate a mod b in python? Like in Perl and C: with %.

When sourcing files which are located next to your script, it is nice to know how to get the path of script.

Tokens, Strings

When parsing you need to tokenize. Search engines gave me this answer to ‘tokenizer python’: Pythonic way to implement a tokenizer: re.Scanner – an undocumented class, utterly useful for this task.

How to check whether a string is emtpy in Python: Most elegant way to check if the string is empty in Python?: An empty string is False.

Display number with leading zeroszfill will fill up (from the left) with zeros.

How to print in Python without newline or space? Printing without a line-break in Python3 is done with arguments to print.

Dicts and lists

What is the preferred syntax for initializing a dict: curly brace literals {} or the dict() function?

How to Add to a dictionary in Python? Dead simple. Delete an element from a dictionary intuitive.

Use len() to find out How to get the size of a list. And how to iterate over dictionary sorted by key?

Almost everything is copied or passed by reference in Python when you assign or return a variable. Modifying a list while iterating over it, requires to know How to clone or copy a list in Python? Copying lists while keeping the references to its elements is done with slicing.

Map/combine two lists into a dictionary in Python where one list is the keys and one the values is done with zipHow can I merge two Python dictionaries in a single expression? for merging two dicts. Updating a dict with a dict Python append dictionary to dictionary replaces elements in target dict if elements with the same key exists.

ASICS Gel Kayano running shoe series – not what they used to be?

Early 2007 I bought my first real running shoes. My choice fell on ASICS Gel Kayano 12 (Code: TN600) which was a current model of the Gel Kayano series at that time. Since then, I ran roughly between 3000 – 4000 kilometers with these shoes.

When they started falling apart in 2013 I decided to renew them, though still keeping the old ones, for garden jobs. For several reasons I chose to stay with the same series from the same maker. Of course the model has changed: they were now at 19 (Code: T300N) and counting (I think 20 was already existing). I ran about 1000 kilometers since then.

Some time ago I noticed that the tissue of the back-inner-part, where the Achilles tendon is sitting, started ripping apart, on both shoes. It wasn’t until today that I realized the importance of this part of the shoe: it scratched open the skin of the my Achilles during my run. I think the shoes have become unusable.

Even though in 2014 I ran more than in the years before, I knew that with the Kayano 12 I had run a lot more and never remember this issue. See for yourself the difference of the tissue in question:

Yes, the left one is the Kayano 12, 7 years old, and the right one is his younger brother. I’m not an tissue expert, but I clearly see that the tissue used on the Kayano 12 is much more robust than the one of the 19.

I don’t want to speculate about the reasons for this degradation regarding this product – although I could rant for hours. Instead I will look for solutions asking a question on fitness.stackexchange.com.