Skip to content

Instantly share code, notes, and snippets.

View tomschr's full-sized avatar
😀

Tom Schraitle tomschr

😀
View GitHub Profile
@tomschr
tomschr / nested_dict.py
Last active March 5, 2021 15:19
Nested dictionaries
"""
The "ddict" is able to create nested structures. With the help of "defaultdict",
it creates arbitrary levels of "dict"-like objects.
Run the doctests with:
$ python3 -m doctest nested_dict.py
>>> d = ddict(a=ddict(b=ddict(c=10)) )
@tomschr
tomschr / task.md
Last active December 1, 2021 14:08
Write a task.py script

Write a task.py script

The following document gives an overview about the exercise. It shows some examples how such script can work. You don't have to slavishly adhere to it. ;-) For example, if you prefer a certain file format or you don't like to output, then be creative and do how you like it!

Exercise

Write a Python script task.py which is able to manage your tasks. It has the following properties:

  1. Like "git", the task.py script contains several subcommands: create, list, edit, remove, show, help.
@tomschr
tomschr / f-strings.py
Last active October 20, 2020 12:22
F-String resolution with a f() function for Python <=3.5
#
# Source:
# https://github.com/python/core-workflow/blob/master/blurb/blurb.py
def f(s):
"""
Basic support for 3.6's f-strings, in 3.5!
Formats "s" using appropriate globals and locals
dictionaries. This f-string:
f"hello a is {a}"
@tomschr
tomschr / nestedentities.py
Last active June 27, 2020 13:28
Creates nested entities
#!/usr/bin/env python3
#
"""
<!ENTITY a "aaaaaa">
<!ENTITY b "&a;">
<!ENTITY c "&c;">
...
<!ENTITY z "&y;">
"""
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="sample">
<fo:region-body margin="15mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="sample">
<fo:flow flow-name="xsl-region-body" font-size="20pt">
<fo:block font-family="'Work Sans ExtraLight'">Work Sans Extra Light (200)</fo:block>
@tomschr
tomschr / test_cli.py
Created April 24, 2020 11:03
Snippet to test KeyoardInterrupt
"""
Snippet to test KeyboardInterrupts
We assume a module "mymodule" which contains (at least) two
functions:
* mymodule.parsecli: Parse CLI with argparse
import argparse
@tomschr
tomschr / parsecli.sh
Last active July 24, 2024 08:09
Bash Example for Parsing Command Line Arguments
#!/bin/bash
#
# A simple example of how to parse CLI options and arguments
## === variable Declarations
# Script name without any directories ("basename"):
PROG=${0##*/}
# Version information:
VERSION="1.0.0"
@tomschr
tomschr / combine_two_lists.py
Last active March 27, 2020 06:47
Python Simplifcation Exercises
# Task
# Combine the two lists so you can iterate in the for loop at once
# Tip: you need to find two builtin functions which helps with this task
names = ('Tux', 'Wilber', 'Geeko')
ages = (2020-1996, 2020-1995, 2020-1992)
for i in range(len(names)):
name = names[i]
age = ages[i]
@tomschr
tomschr / python-links.rst
Last active March 20, 2020 10:29
Python Links
@tomschr
tomschr / python-checklist.md
Last active May 13, 2024 02:13
Python Checklist for Clean Code

Programming Checklist for Clean Code in Python

This checklist contains some tips and recommendations sorted into a syntax and a design category.

It should help to overcome common pitfalls.

Syntax

  1. Check your header of your file. It should contain: