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 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.

Tweet about this on TwitterShare on Google+1

One thought on “Python – my personal FAQ with StackOverflow

  1. Pingback: Everyone can have a Heiligenschein (Halo) | Filter Failure

Leave a Reply

Your email address will not be published. Required fields are marked *