Python Pattern Matching, Sorta

Date Tags python

This trick is neat and potentially useful although maybe not very Pythonic:

txt = "two"
val = (
        1 if txt == "one" else
        2 if txt == "two" else
        3 if txt == "three" else
        0)
print val

The following form can be used instead:

txt = "three"
val = \
        1 if txt == "one" else \
        2 if txt == "two" else \
        3 if txt == "three" else \
        0
print val
more ...

Introducing Qprompt

Date Tags python

Python is an excellent multipurpose language. However, when writing scripts (in the shell script sense), a decent command-line user interface is necessary. Qprompt is a library of simple command-line interface functions that should simplify common tasks. For example, to ask the user for an integer between 1 and 100, simply use the following:

qprompt.ask_int(vld=range(1,101))

Menus are straightforward as well:

menu = qprompt.Menu()
menu.add("1", "Item 1")
menu.add("2 …
more ...

Properly Plural Python Printing

Date Tags python

This one is kind of silly but sometimes you just want messages to use the proper plural case. Here is an example in Python 2 (with a little help from Qprompt):

import qprompt
num = qprompt.ask_int("How many beers?", vld=[1,2,3])
print "Okay, %u %s." % (
        num,
        "beer" if num == 1 else "beers")
more ...

Testing Embedded Systems With Python

Date Tags python

Testing embedded systems with Python (or at all, really) is an extensive topic. This post is not going to dive too deep into the subject but instead suggest one potential approach from a high level as well as cover a few third party libraries that might make life a bit easier.

Before going any further, let's take a look at why you might want to incorporate Python into your test solution. Including a high-level scripting …

more ...

Python Data Serialization Comparison

Date Tags python

Did a quick comparison of some data serialization options for Python. My requirements for the serialization format were the following:

  • Input data is typically either a list or a dictionary.
  • Interoperability is important and must be compatible with at least C.
  • A human readable format is desirable but not necessary.

Based on the requirements, I took a look at the following Python packages:

more ...

Python Gotcha - Globals And Namespaces

Date Tags python

After several years of regular Python use, I really admire that things work how I expect about 90% of the time. To be fair, the other 10% is typically user error. It is easy to overlook nuances in a language, even if you should know better.

I ran into this gotcha in Python recently: when importing a global variable from a module explicitly by name, that variable will be bound to the local namespace. If …

more ...

Generating Documents With Jinja2 and YAML

Date Tags python

Quick example of generating documents by rendering data from a YAML file through a Jinja2 template. The target output is Latex vhistory data (revs.tex) which will be included in main document (main.tex). The output is generated by a Python script (create_revs_tex.py) from YAML data (revs.yaml).

import yaml
from jinja2 import Template

# For this example, assume that stdout will be redirected to `revs.tex`.
# In the template, the `{#--#}` entries are used to …
more ...

Python Geometric Mean

Date Tags python

Need to calculate a geometric mean in Python? You could use Numpy or this one-liner:

# NOTE: This import is only needed for Python3.
from functools import reduce

# Geometric mean of list `n`:
geomean = lambda n: reduce(lambda x,y: x*y, n) ** (1.0 / len(n))

print(geomean([1,2,3,4]))
# Result: 2.2133638394
more ...

Hi, I am Jeff Rimko!
A computer engineer and software developer in the greater Pittsburgh, Pennsylvania area.