Skip to content

Instantly share code, notes, and snippets.

View sabinem's full-sized avatar

Sabine Maennel sabinem

View GitHub Profile
@sabinem
sabinem / check_packages_in_python
Last active March 4, 2018 08:56
check_packages_in_python
# check packages in python
# the python packaging is sometimes hard to understand, in that way it often helps to see what python
# thinks about you packages
# (this code is written and tested with python 3.6)
import pkgutil
def inspect_package(name):
@sabinem
sabinem / case_switch_python
Created March 2, 2018 17:19
case switch in python
# Once in a while you really would need case switching: this is how to best do it in python:
def positive_handler(x):
print("the number {} is positive".format(x))
def zero_handler(x):
print("the number {} is zero".format(x))
def negative_handler(x):
print("the number {} is negative".format(x))
@sabinem
sabinem / README.md
Created March 2, 2018 09:29 — forked from joyrexus/README.md
collapsible markdown

collapsible markdown?

CLICK ME

yes, even hidden code blocks!

print("hello world!")
@sabinem
sabinem / wrapping_functions_in_python
Last active February 28, 2018 16:55
Wrapping functions in python
"""
This is a simple example on how to wrap functions in python:
- it uses inspect and wraps from functools
(this is built for python 3.6, you can copy this into a Jupyter notebook and execute!)
"""
# --------------------------------------------------------------------------
# Define the wrapper for logging the function
# --------------------------------------------------------------------------
@sabinem
sabinem / vars_in_python
Created February 27, 2018 19:47
Using vars() in Python
"""
This is a simple example on how to use vars() in python:
vars() is for getting the variables from objects that have a __dict__
(this is built for python 3.6, you can copy this into a Jupyter notebook and execute!)
"""
# vars is for getting variables from an objects __dict__
@sabinem
sabinem / logging_in_python
Last active February 28, 2018 07:40
# Logging in Python
"""
This is a simple example on how to set up logging in python
(this is built for python 3.6, you can copy this into a Jupyter notebook and execute!)
A good source for logging is the Hitchhikers guide to Python: http://docs.python-guide.org/en/latest/writing/logging/
Another good article on logging: https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
"""
# first you have to import the library
import logging
@sabinem
sabinem / yaml_python_config_file
Last active February 27, 2018 19:28
yaml_configuration_file_in_python
'''
In this example I will show you how to read and write yaml configuration files in python
(this is built for python 3.6, you can copy this into a Jupyter notebook and execute!)
Background Information:
-----------------------
yaml is a configuration language:
- see this link for a good introduction on what is possible with yaml: https://learnxinyminutes.com/docs/yaml/
In python you need pyyaml to write and read yaml files: