<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Python on blog.oddbit.com</title><link>https://blog.oddbit.com/tags/python/</link><description>Recent content in Python on blog.oddbit.com</description><generator>Hugo</generator><language>en-us</language><copyright>Lars Kellogg-Stedman</copyright><lastBuildDate>Tue, 27 Jun 2023 15:44:17 -0400</lastBuildDate><atom:link href="https://blog.oddbit.com/tags/python/rss.xml" rel="self" type="application/rss+xml"/><item><title>Managing containers with Pytest fixtures</title><link>https://blog.oddbit.com/post/2023-07-15-pytest-and-containers/</link><pubDate>Sat, 15 Jul 2023 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2023-07-15-pytest-and-containers/</guid><description>&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Test_fixture#Software"&gt;software fixture&lt;/a&gt; &amp;ldquo;sets up a system for the software testing process by initializing it, thereby satisfying any preconditions the system may have&amp;rdquo;. They allow us to perform setup and teardown tasks, provide state or set up services required for our tests, and perform other initialization tasks. In this article, we&amp;rsquo;re going to explore how to use fixtures in &lt;a href="https://docs.pytest.org/en/7.4.x/"&gt;Pytest&lt;/a&gt; to create and tear down containers as part of a test run.&lt;/p&gt;</description></item><item><title>Remediating poor PyPi performance with DevPi</title><link>https://blog.oddbit.com/post/2021-02-08-remediating-poor-pypi-performa/</link><pubDate>Mon, 08 Feb 2021 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2021-02-08-remediating-poor-pypi-performa/</guid><description>&lt;p&gt;Performance of the primary PyPi service has been so bad lately that&#10;it&amp;rsquo;s become very disruptive. Tasks that used to take a few seconds&#10;will now churn along for 15-20 minutes or longer before completing,&#10;which is incredibly frustrating.&lt;/p&gt;&#10;&lt;p&gt;I first went looking to see if there was a PyPi mirror infrastructure,&#10;like we see with &lt;a href="https://www.cpan.org/"&gt;CPAN&lt;/a&gt; for Perl or &lt;a href="https://ctan.org/"&gt;CTAN&lt;/a&gt; for Tex (and similarly&#10;for most Linux distributions). There is apparently no such beast,&lt;/p&gt;</description></item><item><title>symtool: a tool for interacting with your SYM-1</title><link>https://blog.oddbit.com/post/2021-02-06-symtool-a-tool-for-interacting/</link><pubDate>Sat, 06 Feb 2021 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2021-02-06-symtool-a-tool-for-interacting/</guid><description>&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/SYM-1"&gt;SYM-1&lt;/a&gt; is a &lt;a href="https://en.wikipedia.org/wiki/MOS_Technology_6502"&gt;6502&lt;/a&gt;-based single-board computer produced by&#10;&lt;a href="https://en.wikipedia.org/wiki/Synertek"&gt;Synertek Systems Corp&lt;/a&gt; in the mid 1970&amp;rsquo;s. I&amp;rsquo;ve had one&#10;floating around in a box for many, many years, and after a recent&#10;foray into the world of 6502 assembly language programming I decided&#10;to pull it out, dust it off, and see if it still works.&lt;/p&gt;&#10;&lt;p&gt;The board I have has a whopping 8KB of memory, and in addition to the&#10;standard SUPERMON monitor it has the expansion ROMs for the Synertek&#10;BASIC interpreter (yet another Microsoft BASIC) and RAE (the &amp;ldquo;Resident&#10;Assembler Editor&amp;rdquo;). One interacts with the board either through the&#10;onboard hex keypad and six-digit display, or via a serial connection&#10;at 4800bps (or lower).&lt;/p&gt;</description></item><item><title>To sleep or not to sleep?</title><link>https://blog.oddbit.com/post/2020-12-18-to-sleep-or-not-to-sleep/</link><pubDate>Fri, 18 Dec 2020 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2020-12-18-to-sleep-or-not-to-sleep/</guid><description>&lt;p&gt;Let&amp;rsquo;s say you have a couple of sensors attached to an ESP8266 running&#10;&lt;a href="https://micropython.org/"&gt;MicroPython&lt;/a&gt;. You&amp;rsquo;d like to sample them at different frequencies&#10;(say, one every 60 seconds and one every five minutes), and you&amp;rsquo;d like&#10;to do it as efficiently as possible in terms of power consumption.&#10;What are your options?&lt;/p&gt;&#10;&lt;p&gt;If we don&amp;rsquo;t care about power efficiency, the simplest solution is&#10;probably a loop like this:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;import machine&#10;&#10;lastrun_1 = 0&#10;lastrun_2 = 0&#10;&#10;while True:&#10; now = time.time()&#10;&#10; if (lastrun_1 == 0) or (now - lastrun_1 &amp;gt;= 60):&#10; read_sensor_1()&#10; lastrun_1 = now&#10; if (lastrun_2 == 0) or (now - lastrun_2 &amp;gt;= 300):&#10; read_sensor_2()&#10; lastrun_2 = now&#10;&#10; machine.idle()&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we were only reading a single sensor (or multiple sensors at the&#10;same interval), we could drop the loop and juse use the ESP8266&amp;rsquo;s deep&#10;sleep mode (assuming we have &lt;a href="http://docs.micropython.org/en/latest/esp8266/tutorial/powerctrl.html#deep-sleep-mode"&gt;wired things properly&lt;/a&gt;):&lt;/p&gt;</description></item><item><title>I see you have the machine that goes ping...</title><link>https://blog.oddbit.com/post/2020-03-20-i-see-you-have-the-machine-tha/</link><pubDate>Fri, 20 Mar 2020 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2020-03-20-i-see-you-have-the-machine-tha/</guid><description>&lt;p&gt;We&amp;rsquo;re all looking for ways to keep ourselves occupied these days, and&#10;for me that means leaping at the chance to turn a small problem into a&#10;slightly ridiculous electronics project. For reasons that I won&amp;rsquo;t go&#10;into here I wanted to generate an alert when a certain WiFi BSSID&#10;becomes visible. A simple solution to this problem would have been a&#10;few lines of shell script to send me an email&amp;hellip;but this article isn&amp;rsquo;t&#10;about simple solutions!&lt;/p&gt;</description></item><item><title>How long is a cold spell in Boston?</title><link>https://blog.oddbit.com/post/2020-01-23-how-long-is-a-cold-spell/</link><pubDate>Thu, 23 Jan 2020 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2020-01-23-how-long-is-a-cold-spell/</guid><description>&lt;p&gt;We&amp;rsquo;ve had some wacky weather recently. In the space of a week, the temperature went from a high of about 75°F to a low around 15°F. This got me to thinking about what constitutes &amp;ldquo;normal&amp;rdquo; weather here in the Boston area, and in particular, how common it is to have a string of consecutive days in which the high temperature stays below freezing. While this was an interesting question in itself, it also seemed like a great opportunity to learn a little about &lt;a href="https://pandas.pydata.org"&gt;Pandas&lt;/a&gt;, the Python data analysis framework.&lt;/p&gt;</description></item><item><title>Unpacking a Python regular expression</title><link>https://blog.oddbit.com/post/2019-05-07-unpacking-a-python-regular-exp/</link><pubDate>Tue, 07 May 2019 10:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2019-05-07-unpacking-a-python-regular-exp/</guid><description>&lt;p&gt;I recently answered &lt;a href="https://stackoverflow.com/q/55965819/147356"&gt;a question&lt;/a&gt; from &lt;a href="https://stackoverflow.com/users/7738974/harsha-nalore"&gt;Harsha Nalore&lt;/a&gt; on &lt;a href="https://stackoverflow.com/"&gt;StackOverflow&lt;/a&gt; that involved using Ansible to extract the output of a command sent to a BigIP device of some sort. My solution &amp;ndash; which I claim to be functional, but probably not optimal &amp;ndash; involved writing an &lt;a href="https://ansible.com/"&gt;Ansible&lt;/a&gt; filter module to parse the output. That filter made use of a complex-looking regular expression. Harsha asked for some details on that regular expression works, and the existing StackOverflow answer didn&amp;rsquo;t really seem the write place for that: so, here we are.&lt;/p&gt;</description></item><item><title>Writing Ansible filter plugins</title><link>https://blog.oddbit.com/post/2019-04-25-writing-ansible-filter-plugins/</link><pubDate>Thu, 25 Apr 2019 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2019-04-25-writing-ansible-filter-plugins/</guid><description>&lt;p&gt;I often see questions from people who are attemping to perform complex text transformations in their &lt;a href="https://www.ansible.com/"&gt;Ansible&lt;/a&gt; playbooks. While I am a huge fan of Ansible, data transformation is not one of its strong points. For example, this past week someone &lt;a href="https://stackoverflow.com/questions/55853384/ansible-build-list-dictionary-with-from-list-of-strings/55854394"&gt;asked a question&lt;/a&gt; on Stack Overflow in which they were attempting to convert the output of the &lt;a href="https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html"&gt;keytool&lt;/a&gt; command into a list of dictionaries. The output of the &lt;code&gt;keytool -list -v&lt;/code&gt; command looks something like this:&lt;/p&gt;</description></item><item><title>Using a TM1637 LED module with CircuitPython</title><link>https://blog.oddbit.com/post/2018-05-03-using-a-tm-led-module-with-cir/</link><pubDate>Thu, 03 May 2018 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2018-05-03-using-a-tm-led-module-with-cir/</guid><description>&lt;p&gt;&lt;a href="https://learn.adafruit.com/welcome-to-circuitpython/overview"&gt;CircuitPython&lt;/a&gt; is &amp;ldquo;an education friendly open source derivative of&#10;&lt;a href="https://micropython.org/"&gt;MicroPython&lt;/a&gt;&amp;rdquo;. MicroPython is a port of Python to microcontroller&#10;environments; it can run on boards with very few resources such as the&#10;&lt;a href="https://en.wikipedia.org/wiki/ESP8266"&gt;ESP8266&lt;/a&gt;. I&amp;rsquo;ve recently started experimenting with CircuitPython&#10;on a &lt;a href="https://wiki.wemos.cc/products:d1:d1_mini"&gt;Wemos D1 mini&lt;/a&gt;, which is a small form-factor ESP8266 board.&lt;/p&gt;&#10;&lt;p&gt;I had previously been using Mike Causer&amp;rsquo;s &lt;a href="https://github.com/mcauser/micropython-tm1637/"&gt;micropython-tm1637&lt;/a&gt; for&#10;MicroPython to drive a &lt;a href="http://a.co/gQVPtPr"&gt;4 digit LED display&lt;/a&gt;. I was hoping to&#10;get the same code working under CircuitPython, but when I tried to&#10;build an image that included the &lt;code&gt;tm1637&lt;/code&gt; module I ran into:&lt;/p&gt;</description></item><item><title>A Python interface to signalfd() using FFI</title><link>https://blog.oddbit.com/post/2013-11-28-a-python-interface-to-signalfd/</link><pubDate>Thu, 28 Nov 2013 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2013-11-28-a-python-interface-to-signalfd/</guid><description>&lt;p&gt;I just recently learned about the &lt;code&gt;signalfd(2)&lt;/code&gt; system call, which was&#10;introduced to the Linux kernel &lt;a href="http://lwn.net/Articles/225714/"&gt;back in 2007&lt;/a&gt;:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;signalfd() creates a file descriptor that can be used to accept&#10;signals targeted at the caller. This provides an alternative to&#10;the use of a signal handler or sigwaitinfo(2), and has the&#10;advantage that the file descriptor may be monitored by select(2),&#10;poll(2), and epoll(7).&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;The traditional asynchronous delivery mechanism can be tricky to get&#10;right, whereas this provides a convenient fd interface that integrates&#10;nicely with your existing event-based code.&lt;/p&gt;</description></item><item><title>Long polling with Javascript and Python</title><link>https://blog.oddbit.com/post/2013-11-23-long-polling-with-ja/</link><pubDate>Sat, 23 Nov 2013 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2013-11-23-long-polling-with-ja/</guid><description>&lt;p&gt;In this post I&amp;rsquo;m going to step through an example web chat system&#10;implemented in Python (with &lt;a href="http://bottlepy.org/docs/"&gt;Bottle&lt;/a&gt; and &lt;a href="http://www.gevent.org/"&gt;gevent&lt;/a&gt;) that uses long&#10;polling to implement a simple publish/subscribe mechanism for&#10;efficiently updating connected clients.&lt;/p&gt;&#10;&lt;p&gt;My &lt;a href="http://github.com/larsks/pusub_example/"&gt;pubsub_example&lt;/a&gt; repository on &lt;a href="http://github.com/"&gt;GitHub&lt;/a&gt; has a complete&#10;project that implements the ideas discussed in this article. This&#10;project can be deployed directly on &lt;a href="http://openshift.com/"&gt;OpenShift&lt;/a&gt; if you want to try&#10;things out on your own. You can also try it out online at&#10;&lt;a href="http://pubsub.example.oddbit.com/"&gt;http://pubsub.example.oddbit.com/&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Interrupts on the PiFace</title><link>https://blog.oddbit.com/post/2013-08-05-interrupts-on-the-pi/</link><pubDate>Mon, 05 Aug 2013 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2013-08-05-interrupts-on-the-pi/</guid><description>&lt;p&gt;I recently acquired both a &lt;a href="http://www.raspberrypi.org/"&gt;Raspberry Pi&lt;/a&gt; and a &lt;a href="http://www.element14.com/community/docs/DOC-52857/l/piface-digital-for-raspberry-pi"&gt;PiFace&lt;/a&gt; IO board.&#10;I had a rough time finding examples of how to read the input ports via&#10;interrupts (rather than periodically polling for values), especially&#10;for the &lt;a href="https://github.com/piface"&gt;newer versions&lt;/a&gt; of the PiFace python libraries.&lt;/p&gt;&#10;&lt;p&gt;After a little research, &lt;a href="https://gist.github.com/larsks/6161684"&gt;here&amp;rsquo;s&lt;/a&gt; some simple code that&#10;will print out pin names as you press the input buttons. Button 3&#10;will cause the code to exit:&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;#!/usr/bin/python&#10;&#10;import pifacecommon.core&#10;import pifacecommon.interrupts&#10;import os&#10;import time&#10;&#10;quit = False&#10;&#10;def print_flag(event):&#10; print 'You pressed button', event.pin_num, '.'&#10;&#10;def stop_listening(event):&#10; global quit&#10; quit = True&#10;&#10;pifacecommon.core.init()&#10;&#10;# GPIOB is the input ports, including the four buttons.&#10;port = pifacecommon.core.GPIOB&#10;&#10;listener = pifacecommon.interrupts.PortEventListener(port)&#10;&#10;# set up listeners for all buttons&#10;listener.register(0, pifacecommon.interrupts.IODIR_ON, print_flag)&#10;listener.register(1, pifacecommon.interrupts.IODIR_ON, print_flag)&#10;listener.register(2, pifacecommon.interrupts.IODIR_ON, print_flag)&#10;listener.register(3, pifacecommon.interrupts.IODIR_ON, stop_listening)&#10;&#10;# Start listening for events. This spawns a new thread.&#10;listener.activate()&#10;&#10;# Hang around until someone presses button 3.&#10;while not quit:&#10; time.sleep(1)&#10;&#10;print 'you pressed button 3 (quitting)'&#10;listener.deactivate()&#10;&lt;/code&gt;&lt;/pre&gt;</description></item><item><title>Python ctypes module</title><link>https://blog.oddbit.com/post/2010-08-10-python-ctypes-module/</link><pubDate>Tue, 10 Aug 2010 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2010-08-10-python-ctypes-module/</guid><description>&lt;p&gt;I just learned about the Python &lt;code&gt;ctypes&lt;/code&gt; module, which is a Python module for interfacing with C code. Among other things, &lt;code&gt;ctypes&lt;/code&gt; lets you call arbitrary functions in shared libraries. This is, from my perspective, some very cool magic. I thought I would provide a short example here, since it took me a little time to get everything working smoothly.&lt;/p&gt;&#10;&lt;p&gt;For this example, we&amp;rsquo;ll write a wrapper for the standard &lt;code&gt;statvfs(2)&lt;/code&gt; function:&lt;/p&gt;</description></item><item><title>Retrieving Blogger posts by post id</title><link>https://blog.oddbit.com/post/2010-01-29-retrieving-blogger-posts-by-po/</link><pubDate>Fri, 29 Jan 2010 00:00:00 +0000</pubDate><guid>https://blog.oddbit.com/post/2010-01-29-retrieving-blogger-posts-by-po/</guid><description>&lt;p&gt;I spent some time recently trying to figure out, using Google&amp;rsquo;s &lt;a href="http://code.google.com/apis/gdata/docs/2.0/basics.html"&gt;gdata&lt;/a&gt; API, how to retrieve a post from a &lt;a href="http://www.blogger.com/"&gt;Blogger&lt;/a&gt; blog if I know corresponding post id. As far as I can tell there is no obvious way of doing this, at least not using the gdata.blogger.client api, but after much nashing of teeth I came up with the following solution.&lt;/p&gt;&#10;&lt;p&gt;Given client, a &lt;a href="http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.blogger.client.html"&gt;gdata.blogger.client&lt;/a&gt; instance, and blog, a &lt;a href="http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.blogger.data.html"&gt;gdata.blogger.data.Blog&lt;/a&gt; instance, the following code will return a &lt;a href="http://gdata-python-client.googlecode.com/svn/trunk/pydocs/gdata.blogger.data.html"&gt;gdata.blogger.data.BlogPost&lt;/a&gt; instance:&lt;/p&gt;</description></item></channel></rss>