Skip to content

Instantly share code, notes, and snippets.

View tomschr's full-sized avatar

Tom Schraitle tomschr

View GitHub Profile
@tomschr
tomschr / parse_param.sh
Created March 22, 2015 08:30
Parse Parameter-Value Pair into Global Shell Array
# Declare our array
declare -A PARAMS
function parse_param() {
# Syntax: parse_param "PARAMETER=VALUE"
# Extract parameter and value and store it in Shell array"
local _PARAM=$1
[[ "$_PARAM" == *"="* ]] || exit_on_error "Option -p must have the syntax PARAMETER=VALUE"
p=${_PARAM%=*}
v=${_PARAM#*=}
@tomschr
tomschr / root_sourceline.py
Last active August 29, 2015 14:21
Gets the starting line number of the start-tag of root
def root_sourceline(source, resolver=None):
"""Gets the starting line number of the start-tag of root
:param source:
:type source: file name/path, file object, file-like object, URL
:param etree.Resolver resolver: custom resolver
:return: (line number, column)
:rtype: tuple
"""
if hasattr(source, 'read'):
@tomschr
tomschr / glossary.xml
Last active September 22, 2015 06:26
Very rough XSLT to backlink from DocBook's glossary to the first glossterm|firstterm entry
<article xml:lang="en" version="5.0"
xmlns="http://docbook.org/ns/docbook"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Test</title>
<para xml:id="first"><glossterm>XML</glossterm></para>
<para xml:id="second">bla bla bla <glossterm>XML</glossterm></para>
<glossary>
<title>Glossary</title>
@tomschr
tomschr / build_manpage.py
Created November 1, 2015 14:50
setup.py: build_manpage command -- Generate manpage from setup()
# https://crunchyfrog.googlecode.com/hg/utils/command/build_manpage.py
"""build_manpage command -- Generate man page from setup()"""
import datetime
from distutils.command.build import build
from distutils.core import Command
from distutils.errors import DistutilsOptionError
import optparse
@tomschr
tomschr / pytest_raises_nose.py
Last active March 14, 2016 10:38
pytest: raises Decorator from nose (implemented as function and class)
from functools import wraps
# Taken from nosetest: see file nose/tools/nontrivial.py
class raises(object):
"""Test must raise one of expected exceptions to pass.
Example use::
@raises(TypeError, ValueError)
def test_raises_type_error():
@tomschr
tomschr / test_mock.py
Last active January 12, 2024 21:45
Example of mocking os.path.isfile
#!/usr/bin/env python3
#
# Use py.test:
# $ pytest test_mock.py
from unittest.mock import patch
import os.path
def my_isfile(filename):
@tomschr
tomschr / sdsc_docopt.py
Last active January 24, 2017 20:53
Proof of concept of sdsc with docopt
#!/usr/bin/env python3
"""Checks a given DocBook XML file for stylistic errors
Usage:
sdsc [-h | --help]
sdsc [options] INPUTFILE [OUTPUTFILE]
Required arguments:
INPUTFILE the DocBook XML file to check for
@tomschr
tomschr / modules.py
Last active May 20, 2017 20:36
Lists all functions and classes from a loaded module
import pkgutil
import inspect
def members(module, predicate=None):
"""Yields all functions and classes of a module if predicate=None"""
def func_or_class(obj):
"""Inspects an object if it is a function or a class (=True),
otherwise False
"""
@tomschr
tomschr / asyncio-producer-consumer-task_done.py
Last active January 31, 2020 21:38
Producer/Consumer pattern for asyncio (Python >=3.4)
# Original source from http://asyncio.readthedocs.io/en/latest/producer_consumer.html
# Rewritten for Python >=3.4
import asyncio
import random
@asyncio.coroutine
def produce(queue, n):
for x in range(n):
@tomschr
tomschr / flatten.py
Last active July 10, 2017 15:19
Flatten nested list with unlimited depth
from collections.abc import Iterable
def flatten(items, ltypes=Iterable):
"""flatten(sequence) -> list
Returns a single, flat list which contains all elements retrieved from the sequence
"""
if not isinstance(items, list):
yield items
stack = [iter(items)]