<?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 Hillel Wayne</title>
    <link>https://www.hillelwayne.com/tags/python/</link>
    <description>Recent content in Python on Hillel Wayne</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 17 Apr 2023 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://www.hillelwayne.com/tags/python/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Breaking the limits of TLA&#43; model checking</title>
      <link>https://www.hillelwayne.com/post/graphing-tla/</link>
      <pubDate>Mon, 17 Apr 2023 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/graphing-tla/</guid>
      <description>I haven&amp;rsquo;t written much about TLA+ on the blog since the new learntla went up. Any examples or special topics I think of go there instead. But I recently thought of a cool demo that doesn&amp;rsquo;t quite fit the theme of that book: there are some things you can&amp;rsquo;t easily check with the model checker but can check if you work with the state space as a directed graph.</description>
    </item>
    
    <item>
      <title>Context Managers for Debugging</title>
      <link>https://www.hillelwayne.com/tipjar/py-02/</link>
      <pubDate>Thu, 01 Sep 2022 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/tipjar/py-02/</guid>
      <description>&lt;p&gt;A small helper I use to debug some python programs:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #272822&#34;&gt;&lt;pre style=&#34;line-height: 125%;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;contextlib&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;import&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;contextmanager&lt;/span&gt;

&lt;span style=&#34;color: #a6e22e&#34;&gt;@contextmanager&lt;/span&gt;
&lt;span style=&#34;color: #66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color: #a6e22e&#34;&gt;debug_error&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;():&lt;/span&gt;
    &lt;span style=&#34;color: #66d9ef&#34;&gt;try&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;:&lt;/span&gt;
        &lt;span style=&#34;color: #66d9ef&#34;&gt;yield&lt;/span&gt;
    &lt;span style=&#34;color: #66d9ef&#34;&gt;except&lt;/span&gt; &lt;span style=&#34;color: #a6e22e&#34;&gt;Exception&lt;/span&gt; &lt;span style=&#34;color: #66d9ef&#34;&gt;as&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;e:&lt;/span&gt;
        &lt;span style=&#34;color: #f8f8f2&#34;&gt;breakpoint()&lt;/span&gt;
        &lt;span style=&#34;color: #f8f8f2&#34;&gt;quit()&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;### usage&lt;/span&gt;

&lt;span style=&#34;color: #66d9ef&#34;&gt;with&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;debug_error():&lt;/span&gt;
  &lt;span style=&#34;color: #f8f8f2&#34;&gt;x&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #ae81ff&#34;&gt;1&lt;/span&gt;
  &lt;span style=&#34;color: #f8f8f2&#34;&gt;y&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #ae81ff&#34;&gt;0&lt;/span&gt;
  &lt;span style=&#34;color: #f8f8f2&#34;&gt;x&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;y&lt;/span&gt; &lt;span style=&#34;color: #75715e&#34;&gt;# x and y will be avail in pdb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also put things before the &lt;code&gt;yield&lt;/code&gt; (which will run before the &lt;code&gt;with&lt;/code&gt; block), and after it (to run at the end of the block.) Anything put after it will run at the end of the block. See &lt;a href=&#34;https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager&#34;&gt;@contextmanager&lt;/a&gt; for more info.&lt;/p&gt;

&lt;h3 id=&#34;notes&#34;&gt;Notes&lt;/h3&gt;

&lt;p&gt;&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Path Objects</title>
      <link>https://www.hillelwayne.com/tipjar/py-01/</link>
      <pubDate>Tue, 23 Aug 2022 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/tipjar/py-01/</guid>
      <description>&lt;p&gt;Python 3.4 added &lt;a href=&#34;https://docs.python.org/3.10/library/pathlib.html#&#34;&gt;path objects&lt;/a&gt;, but most people still don&amp;rsquo;t know about them. They simplify a lot of file operations:&lt;/p&gt;
&lt;div class=&#34;highlight&#34; style=&#34;background: #272822&#34;&gt;&lt;pre style=&#34;line-height: 125%;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&#34;color: #75715e&#34;&gt;### Without pathlib&lt;/span&gt;

&lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #e6db74&#34;&gt;&amp;quot;path/to/file&amp;quot;&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;# parent dir&lt;/span&gt;
&lt;span style=&#34;color: #f8f8f2&#34;&gt;os&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;dirname(path)&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;# file suffix&lt;/span&gt;
&lt;span style=&#34;color: #f8f8f2&#34;&gt;os&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;splitext(path)[&lt;/span&gt;&lt;span style=&#34;color: #ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;]&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;# read file&lt;/span&gt;
&lt;span style=&#34;color: #66d9ef&#34;&gt;with&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;open(path)&lt;/span&gt; &lt;span style=&#34;color: #66d9ef&#34;&gt;as&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;f:&lt;/span&gt;
  &lt;span style=&#34;color: #f8f8f2&#34;&gt;x&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;read()&lt;/span&gt;


&lt;span style=&#34;color: #75715e&#34;&gt;### With pathlib&lt;/span&gt;

&lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;pathlib&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;Path(&lt;/span&gt;&lt;span style=&#34;color: #e6db74&#34;&gt;&amp;quot;path/to/file&amp;quot;&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;)&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;# parent dir&lt;/span&gt;
&lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;parent&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;# file suffix&lt;/span&gt;
&lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;suffix&lt;/span&gt;

&lt;span style=&#34;color: #75715e&#34;&gt;# read file&lt;/span&gt;
&lt;span style=&#34;color: #f8f8f2&#34;&gt;x&lt;/span&gt; &lt;span style=&#34;color: #f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color: #f8f8f2&#34;&gt;path&lt;/span&gt;&lt;span style=&#34;color: #f92672&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color: #f8f8f2&#34;&gt;read_text()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Paths are also OS-independent, so the same code will generally work on both Windows and Unix.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Crimes with Python&#39;s Pattern Matching</title>
      <link>https://www.hillelwayne.com/post/python-abc/</link>
      <pubDate>Sun, 31 Jul 2022 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/python-abc/</guid>
      <description>One of my favorite little bits of python is __subclasshook__. Abstract Base Classes with __subclasshook__ can define what counts as a subclass of the ABC, even if the target doesn&amp;rsquo;t know about the ABC. For example:
class PalindromicName(ABC): @classmethod def __subclasshook__(cls, C): name = C.__name__.lower() return name[::-1] == name class Abba: ... class Baba: ... &amp;gt;&amp;gt;&amp;gt; isinstance(Abba(), PalindromicName) True &amp;gt;&amp;gt;&amp;gt; isinstance(Baba(), PalindromicName) False  You can do some weird stuff with this.</description>
    </item>
    
    <item>
      <title>Two workers are quadratically better than one</title>
      <link>https://www.hillelwayne.com/post/queueing-prism/</link>
      <pubDate>Mon, 02 Nov 2020 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/queueing-prism/</guid>
      <description>For latency, anyway.
A common architecture pattern is the &amp;ldquo;task queue&amp;rdquo;: tasks enter an ordered queue, workers pop tasks and process them. This pattern is used everywhere from distributed systems to thread pools. It applies just as well to human systems, like waiting in line at a supermarket or a bank. Task queues are popular because they are simple, easy to understand, and scale well.
There are two primary performance metrics for a task queue.</description>
    </item>
    
    <item>
      <title>Property Testing with Complex Inputs</title>
      <link>https://www.hillelwayne.com/post/property-testing-complex-inputs/</link>
      <pubDate>Fri, 19 Jun 2020 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/property-testing-complex-inputs/</guid>
      <description>This was originally written as a tutorial for Hypothesis and will eventually be reproduced there, but it might still be useful for people using other property-testing libraries. This essay assumes some familiarity with Hypothesis. If you haven&amp;rsquo;t used it, a better introduction is here.
Once you learn the basics, there are two hard parts to writing property-based tests:
 What are good properties to test? How do I generate complex inputs?</description>
    </item>
    
    <item>
      <title>The Hard Part of Learning a Language</title>
      <link>https://www.hillelwayne.com/post/learning-a-language/</link>
      <pubDate>Mon, 27 Apr 2020 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/learning-a-language/</guid>
      <description>I want to do $THING. Normally I do my hacking in Python, which is okay but has lots of frustrations. My friends tell me $LANGUAGE is better for doing $THING. After going through the online tutorial, I can see why. Maybe I&amp;rsquo;ll try $LANGUAGE for this project! Just a few things I need to figure out first:
 How do I install it? The docs say brew install, but I&amp;rsquo;m on Windows.</description>
    </item>
    
    <item>
      <title>How fast do I talk?</title>
      <link>https://www.hillelwayne.com/post/talk-fast/</link>
      <pubDate>Mon, 28 Oct 2019 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/talk-fast/</guid>
      <description>I speak very fast. It&amp;rsquo;s like the words are piled up in my mouth and I can&amp;rsquo;t say one without the rest tumbling out. Through my whole life people have told me to slow down, speak more clearly, and enunciate. I can do it if I concentrate but I quickly relapse into gushing out words.
As I now give lots of conference talks, this has become a professional issue:</description>
    </item>
    
    <item>
      <title>Python Negatypes</title>
      <link>https://www.hillelwayne.com/negatypes/</link>
      <pubDate>Thu, 26 Sep 2019 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/negatypes/</guid>
      <description>Back in 2007 Python added Abstract Base Classes, which were intended to be used as interfaces:
from abc import ABC class AbstractIterable(ABC): @abstractmethod def __iter__(self): while False: yield None def get_iterator(self): return self.__iter__()  ABCs were added to strengthen the duck typing a little. If you inherited AbstractIterable, then everybody knew you had an implemented __iter__ method, and could handle that appropriately.
Unsurprisingly, this idea never caught on. People instead prefered &amp;ldquo;better ask forgiveness than permission&amp;rdquo; and wrapped calls to __iter__ in a try block.</description>
    </item>
    
    <item>
      <title>Finding Property Tests</title>
      <link>https://www.hillelwayne.com/post/contract-examples/</link>
      <pubDate>Mon, 08 Apr 2019 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/contract-examples/</guid>
      <description>A while back I was ranting about APLs and included this python code to get the mode of a list:
def mode(l): max = None count = {} for x in l: if x not in count: count[x] = 0 count[x] += 1 if not max or count[x] &amp;gt; count[max]: max = x return max  There&amp;rsquo;s a bug in it. Do you see it? If not, try running it on the list [0, 0, 1]:</description>
    </item>
    
    <item>
      <title>Property Tests &#43; Contracts = Integration Tests</title>
      <link>https://www.hillelwayne.com/post/pbt-contracts/</link>
      <pubDate>Sun, 17 Dec 2017 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/pbt-contracts/</guid>
      <description>I&amp;rsquo;m a pretty big fan of contracts and property-based testing. While they&amp;rsquo;re both powerful, they do have tradeoffs:
 Contracts are simple and allow for complex conditionals, but you need to test them over a wide space to confirm them. Property-Based Testing can test your code over a very wide search space, but you have to come up with good generators and invariants.  Maybe they complement each other: &amp;ldquo;your code doesn&amp;rsquo;t violate any contracts&amp;rdquo; counts as a PBT invariant.</description>
    </item>
    
    <item>
      <title>Introduction to Contract Programming</title>
      <link>https://www.hillelwayne.com/post/contracts/</link>
      <pubDate>Mon, 27 Nov 2017 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/contracts/</guid>
      <description>I&amp;rsquo;ve namedropped contracts enough here that I think it&amp;rsquo;s finally time to go talk about them. A lot of people conflate them with class interfaces / dynamic typing / &amp;ldquo;your unit tests are your contract&amp;rdquo;, which muddies the discussion and makes it hard to show their benefits. So I&amp;rsquo;d like to build up the idea of contracts from first principles. We&amp;rsquo;re going to work in Python, up until the point where things get crazy.</description>
    </item>
    
    <item>
      <title>Instructive and Persuasive Examples</title>
      <link>https://www.hillelwayne.com/post/persuasive-examples/</link>
      <pubDate>Tue, 29 Aug 2017 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/persuasive-examples/</guid>
      <description>I recently read No excuses, write unit tests, which argues Unit Tests are Good and You Should Write Them. For the most part I strongly agree with their message, which is why I feel kinda bad criticizing the essay. They provide this as &amp;ldquo;an example of simple testing&amp;rdquo;:
constadd=(...numbers) =&amp;gt; { returnnumbers.reduce((acc, val) =&amp;gt; { returnacc+val; }, 0); }; it(&amp;#39;should add numbers&amp;#39;, () =&amp;gt; { constexpected=15; constactual=add(1, 2, 3, 4, 5); expect(actual).</description>
    </item>
    
    <item>
      <title>Hypothesis Testing with Oracle Functions</title>
      <link>https://www.hillelwayne.com/post/hypothesis-oracles/</link>
      <pubDate>Fri, 21 Jul 2017 09:00:00 -0500</pubDate>
      
      <guid>https://www.hillelwayne.com/post/hypothesis-oracles/</guid>
      <description>This post is about the Hypothesis testing library. If you&amp;rsquo;re unfamiliar with it, check it out! Property tests are a fantastic addition to your testing suite. All examples use pytest.
Imagine we&amp;rsquo;ve written a bubblesort:
def bubblesort(l: List[int]) -&amp;gt; List[int]: # blah blah blah  What are some of the properties we could test? We could check that the input has the same length as the output:
from hypothesis import given from hypothesis.</description>
    </item>
    
    <item>
      <title>Doctests in Python</title>
      <link>https://www.hillelwayne.com/post/python-doctests/</link>
      <pubDate>Thu, 16 Feb 2017 00:00:00 +0000</pubDate>
      
      <guid>https://www.hillelwayne.com/post/python-doctests/</guid>
      <description>Doctests are one of the most fascinating things in Python. Not necessarily because it&amp;rsquo;s particularly elegant or useful, but because it&amp;rsquo;s unique: I haven&amp;rsquo;t found another language that has a similar kind of feature.
Here&amp;rsquo;s how it works. Imagine I was writing an adder:
def add(a,b): return a + b  There&amp;rsquo;s two kinds of ways we can test it. The first is with unit tests, which everybody&amp;rsquo;s already used to.</description>
    </item>
    
    <item>
      <title>Hate Your Tools</title>
      <link>https://www.hillelwayne.com/hate-your-tools/</link>
      <pubDate>Tue, 27 Dec 2016 19:48:18 -0600</pubDate>
      
      <guid>https://www.hillelwayne.com/hate-your-tools/</guid>
      <description></description>
    </item>
    
  </channel>
</rss>