This is a valid RSS feed.
This feed is valid, but interoperability with the widest range of feed readers could be improved by implementing the following recommendations.
line 31, column 0: (15 occurrences) [help]
from a nice guy named Mark.</p><h1 id="h_how_to_write_nice_code_in_research" ...
line 89, column 0: (16 occurrences) [help]
100 through 140:</p><div class="figurep"><figure><picture><img src="https:// ...
<a href="https://genart.social/@hamoid/115125620138280715" rel="external noo ...
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="https://nedbatchelder.com/rssfull2html.xslt" media="screen" ?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/"> <channel rdf:about="https://nedbatchelder.com/blog"> <title>Ned Batchelder's blog</title> <link>https://nedbatchelder.com/blog</link> <description>Ned Batchelder's personal blog.</description> <dc:language>en-US</dc:language> <image rdf:resource="https://nedbatchelder.com/pix/rss-banner.gif"/> <items> <rdf:Seq> <rdf:li resource="https://nedbatchelder.com/blog/202510/side_project_advice.html"/><rdf:li resource="https://nedbatchelder.com/blog/202510/natural_cubics_circular_simplex.html"/><rdf:li resource="https://nedbatchelder.com/blog/202509/hobby_hilbert_simplex.html"/><rdf:li resource="https://nedbatchelder.com/blog/202509/testing_is_better_than_dsa.html"/><rdf:li resource="https://nedbatchelder.com/blog/202508/finding_unneeded_pragmas.html"/><rdf:li resource="https://nedbatchelder.com/blog/202508/starting_with_pytests_parametrize.html"/><rdf:li resource="https://nedbatchelder.com/blog/202507/coveragepy_regex_pragmas.html"/><rdf:li resource="https://nedbatchelder.com/blog/202507/coverage_7100_patch.html"/><rdf:li resource="https://nedbatchelder.com/blog/202507/2048_iterators_and_iterables.html"/><rdf:li resource="https://nedbatchelder.com/blog/202506/math_factoid_of_the_day_63.html"/> </rdf:Seq> </items> </channel> <image rdf:about="https://nedbatchelder.com/pix/rss-banner.gif"> <title>Ned Batchelder's blog</title> <link>https://nedbatchelder.com/blog</link> <url>https://nedbatchelder.com/pix/rss-banner.gif</url> </image> <item rdf:about="https://nedbatchelder.com/blog/202510/side_project_advice.html"> <title>Side project advice</title> <link>https://nedbatchelder.com/blog/202510/side_project_advice.html</link> <dc:date>2025-10-30T06:23:13-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>Last night was a <a rel="external noopener" href="https://about.bostonpython.com/">Boston Python project night</a> where Ihad a good conversation with a few people that was mostly guided by questionsfrom a nice guy named Mark.</p><h1 id="h_how_to_write_nice_code_in_research">How to write nice code in research<a class="headerlink" aria-label="Link to this header" href="#h_how_to_write_nice_code_in_research"></a></h1><p>Mark works in research and made the classic observation that research code isoften messy, and asked about how to make it nicer.</p><p>I pointed out that for software engineers, the code is the product. Forresearch, the results are the product, so there’s a reason the code can be andoften is messier. It’s important to keep the goal in mind. I mentioned it mightnot be worth it to add type annotations, detailed docstrings, or whatever elsewould make the code “nice”.</p><p>But the more you can make “nice” a habit, the less work it will be to do itas a matter of course. Even in a result-driven research environment, you’ll beable to write code the way you want, or at least push back a little bit. Codeusually lives longer than people expect, so the nicer you can make it,the better it will be.</p><h1 id="h_side_projects">Side projects<a class="headerlink" aria-label="Link to this header" href="#h_side_projects"></a></h1><p>Side projects are a good opportunity to work differently. If work means messycode, your side project could be pristine. If work is very strict, your sideproject can be thrown together just for fun. You get to set the goals.</p><p>And different side projects can be different. I develop<a href="https://coverage.readthedocs.io" rel="external noopener">coverage.py</a> very differentlythan <a href="https://nedbatchelder.com/blog/202510/natural_cubics_circular_simplex.html">fun math artprojects</a>. Coverage.py has an extensive test suite run on many versions ofPython (including nightly builds of the tip of main). The math art projectsusually have no tests at all.</p><p>Side projects are a great place to decide how you want to code and topractice that style. Later you can bring those skills and learnings back to awork environment.</p><h1 id="h_forgive_yourself">Forgive yourself<a class="headerlink" aria-label="Link to this header" href="#h_forgive_yourself"></a></h1><p>Mark said one of his difficulties with side projects is perfectionism. He’llcome back to a project and find he wants to rewrite the whole thing.</p><p>My advice is: forgive yourself. It’s OK to rewrite the whole thing. It’s OKto not rewrite the whole thing. It’s OK to ignore it for months at a time. It’sOK to stop in the middle of a project and never come back to it. It’s OK toobsess about “irrelevant” details.</p><p>The great thing about a side project is that you are the only person whodecides what and how it should be.</p><h1 id="h_how_to_stay_motivated">How to stay motivated<a class="headerlink" aria-label="Link to this header" href="#h_how_to_stay_motivated"></a></h1><p>But how to stay motivated on side projects? For me, it’s very motivating thatmany people use and get value from coverage.py. It’s a service to the communitythat I find rewarding. Other side projects will have other motivations: achance to learn new things, flex different muscles, stretch myself in newways.</p><p>Find a reason that motivates you, and structure your side projects to leaninto that reason. Don’t forget to forgive yourself if it doesn’t work out theway you planned or if you change your mind.</p><h1 id="h_how_to_write_something_people_will_use">How to write something people will use<a class="headerlink" aria-label="Link to this header" href="#h_how_to_write_something_people_will_use"></a></h1><p>Sure, it’s great to have a project that many people use, but how do you finda project that will end up like that? The best way is to write something thatyou find useful. Then talk about it with people. You never know what will catchon.</p><p>I mentioned my <a href="https://pypi.org/project/cogapp/" rel="external noopener">cog</a> project,which I first wrote in 2004 for one reason, but which is now being used by otherpeople (including me) for different purposes. It<a href="https://nedbatchelder.com/blog/202201/cog_resurgence.html">took years to catch on</a>.</p><p>Of course there’s no guarantee something like that will happen: it mostlikely won’t. But I don’t know of a better way to make something people willuse than to start by making something that <em>you</em> will use.</p><h1 id="h_other_topics">Other topics<a class="headerlink" aria-label="Link to this header" href="#h_other_topics"></a></h1><p>The discussion wasn’t as linear as this. We touched on other things along theway: unit tests vs system tests, obligations to support old versions ofsoftware, how to navigate huge code bases. There were probably other tangentsthat I’ve forgotten.</p><p>Project nights are almost never just about projects: they are aboutconnecting with people in lots of different ways. This discussion felt like agood connection. I hope the ideas of choosing your own paths and forgivingyourself hit home.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202510/natural_cubics_circular_simplex.html"> <title>Natural cubics, circular Simplex</title> <link>https://nedbatchelder.com/blog/202510/natural_cubics_circular_simplex.html</link> <dc:date>2025-10-21T07:14:23-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>This post continues where <a href="https://nedbatchelder.com/blog/202509/hobby_hilbert_simplex.html">Hobby Hilbert Simplex</a> leftoff. If you haven’t read it yet, start there. It explains the basics of Hobbycurves, Hilbert sorting and Simplex noise that I’m using.</p><h1 id="h_animation">Animation<a class="headerlink" aria-label="Link to this header" href="#h_animation"></a></h1><p>To animate one of our drawings, instead of considering 40 lines, we’ll thinkabout 140 lines. The first frame of the animation will draw lines 1 through 40,the second draws lines 2 through 41, and so on until the 100th frame is lines100 through 140:</p><div class="figurep"><figure><picture><img src="https://nedbatchelder.com/pix/fluidity/linear_hobby.gif" alt="Swoopy lines flowing across the image, but with occasional jumps" width="600" height="600" class="hairline"></picture></figure></div><p>I’ve used a single Hilbert sorter for all of the frames to remove somejumping, but the Hobby curves still hop around. Also the animation doesn’t loopsmoothly, so there’s a giant jump from frame 100 back to frame 1.</p><h1 id="h_natural_cubics">Natural cubics<a class="headerlink" aria-label="Link to this header" href="#h_natural_cubics"></a></h1><p>Hobby curves look nice, but have this unfortunate discontinuity where a smallchange in a point can lead to a radical change in the curve. There’s another wayto compute curves through points automatically, called natural cubic curves.These curves don’t jump around the way Hobby curves can.</p><p>Jake Low’s <a rel="external noopener" href="https://www.jakelow.com/blog/hobby-curves">page about Hobby curves</a> has interactiveexamples of natural cubic curves which you should try. Natural cubics don’tlook as nice to our eyes as Hobby curves. Below is a comparison. Each row hasthe same points, with Hobby curves on the left and natural cubic curves on theright:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/hobby_vs_cubic.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/hobby_vs_cubic.png" alt="On the right are nice blobby shapes, on the left are the same points but connected with sometimes pointy awkward curves" width="1200" height="1800" class="hairline"></picture></figure></div><p>The “natural” cubics actually have a quite unnatural appearance. But in ananimation, those quirks could be a good trade-off for smooth transitions. Here’san animation with the same points as our first one, but with natural cubiccurves:</p><div class="figurep"><figure><picture><img src="https://nedbatchelder.com/pix/fluidity/linear_cubic.gif" alt="A flowing animation with pointier curves, only one jump at the end" width="600" height="600" class="hairline"></picture></figure></div><p>Now the motion is smooth except for the jump from frame 100 back to frame 1.Let’s do something about that.</p><h1 id="h_circular_simplex">Circular Simplex<a class="headerlink" aria-label="Link to this header" href="#h_circular_simplex"></a></h1><p>So far, we’ve been choosing points by sampling the simplex noise in small steps alonga horizontal line: use a fixed u value, then take tiny steps along the v axis.That gave us our x coordinates, and a similar line with a different u value gaveus the y coordinates. The ending point will be completely unrelated to thestarting point. To make a seamlessly looping animation, we need our x,y valuesto cycle seamlessly, returning to where they started.</p><p>We can make our x,y coordinates loop by choosing u,v values in a circle.Because the u,v values return to their starting point in the continuous simplexnoise, the x,y coordinates will return as well. We use two circles: one for thex coordinates and another for the y. The circles are far from each other tokeep x and y independent of each other. The size of the circle is determined bythe distance we want for each step and how many steps we want in the loop.</p><p>Here are three point paths created two ways, with linear sampling on theright and circular sampling on the left. Because simplex provides values between-1 and 1, the points wander within a square:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/point_trails.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/point_trails.png" alt="On the right, three trails of points that don't form a closed loop. On the left, three closed loops but still with interesting random shapes" width="1200" height="600" class="hairline"></picture></figure></div><p>It can get a bit confusing at this point: these traces are not the curves weare drawing. They are the paths of the control points for successive curves. Wedraw curves through corresponding sets of points to get our animation. The firstcurve connects the first red/green/blue points, the second curve connects thesecond set, and so on.</p><p>Using circular sampling of the simplex noise, we can make animations thatloop perfectly:</p><div class="figurep"><figure><picture><img src="https://nedbatchelder.com/pix/fluidity/circular_5.gif" alt="A smoothly looping animation" width="600" height="600" class="hairline"></picture></figure></div><div class="figurep"><figure><picture><img src="https://nedbatchelder.com/pix/fluidity/circular_9.gif" alt="A smoothly looping animation" width="600" height="600" class="hairline"></picture></figure></div><div class="figurep"><figure><picture><img src="https://nedbatchelder.com/pix/fluidity/circular_25.gif" alt="A smoothly looping animation" width="600" height="600" class="hairline"></picture></figure></div><h1 id="h_colophon">Colophon<a class="headerlink" aria-label="Link to this header" href="#h_colophon"></a></h1><p>If you are interested, the code is available on GitHub at<a href="https://github.com/nedbat/fluidity" rel="external noopener">nedbat/fluidity</a>.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202509/hobby_hilbert_simplex.html"> <title>Hobby Hilbert Simplex</title> <link>https://nedbatchelder.com/blog/202509/hobby_hilbert_simplex.html</link> <dc:date>2025-09-26T08:14:04-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>I saw a generative art piece I liked and wanted to learn how it was made.Starting with the artist’s Kotlin code, I dug into three new algorithms, hackedtogether some Python code, experimented with alternatives, and learned a lot.Now I can explain it to you.</p><p>It all started with this post by<a href="https://genart.social/@hamoid/115125620138280715" rel="external noopener">aBe on Mastodon</a>:</p><blockquote class="mastodon-post" lang="en" cite="https://genart.social/@hamoid/115125620138280715" data-source="fediverse"> <p>I love how these lines separate and reunite. And the fact that I can express this idea in 3 or 4 lines of code.</p><p>For me they’re lives represented by closed paths that end where they started, spending part of the journey together, separating while we go in different directions and maybe reconnecting again in the future.</p><p><a href="https://genart.social/tags/CreativeCoding" rel="nofollow noopener" class="mention hashtag" target="_blank">#<span>CreativeCoding</span></a> <a href="https://genart.social/tags/algorithmicart" rel="nofollow noopener" class="mention hashtag" target="_blank">#<span>algorithmicart</span></a> <a href="https://genart.social/tags/proceduralArt" rel="nofollow noopener" class="mention hashtag" target="_blank">#<span>proceduralArt</span></a> <a href="https://genart.social/tags/OPENRNDR" rel="nofollow noopener" class="mention hashtag" target="_blank">#<span>OPENRNDR</span></a> <a href="https://genart.social/tags/Kotlin" rel="nofollow noopener" class="mention hashtag" target="_blank">#<span>Kotlin</span></a></p> <figure><figure><img src="https://media.hachyderm.io/cache/media_attachments/files/115/125/620/285/265/947/small/5a73d40e6a4a81c1.png" alt="80 wobbly black hobby curves with low opacity. In some places the curves travel together, but sometimes they split in 2 or 3 groups and later reunite. Due to the low opacity, depending on how many curves overlap the result is brighter or darker." width="480" height="480"></figure></figure> <footer> — aBe (@hamoid@genart.social) <a href="https://genart.social/@hamoid/115125620138280715" rel="external noopener"><time datetime="2025-08-31T21:59:13.000Z">8/31/2025, 5:59:13 PM</time></a> </footer></blockquote><p>The drawing is made by choosing 10 random points, drawing a curve throughthose points, then slightly scooching the points and drawing another curve.There are 40 curves, each slightly different than the last. Occasionallythe next curve makes a jump, which is why they separate and reunite.</p><p>Eventually I made something similar:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/repro_139.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/repro_139.png" alt="An image similar to the one from Mastodon, with smoky sinuous curves" width="600" height="600" class="hairline"></picture></figure></div><p>Along the way I had to learn about three techniques I got from the Kotlincode: Hobby curves, Hilbert sorting, and simplex noise.</p><p>Each of these algorithms tries to do something “natural” automatically, sothat we can generate art that looks nice without any manual steps.</p><h1 id="h_hobby_curves">Hobby curves<a class="headerlink" aria-label="Link to this header" href="#h_hobby_curves"></a></h1><p>To draw swoopy curves through our random points, we use an algorithmdeveloped by John Hobby as part of Donald Knuth’s Metafont type design system.Jake Low has a <a rel="external noopener" href="https://www.jakelow.com/blog/hobby-curves">great interactive page for playing with Hobbycurves</a>, you should try it.</p><p>Here are three examples of Hobby curves through ten random points:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/hobby_unsorted.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/hobby_unsorted.png" alt="Red random points connected by green lines then with a curve through all ten." width="600" height="200" class="hairline"></picture></figure></div><p>The curves are nice, but kind of a scribble, because we’re joining pointstogether in the order we generated them (shown by the green lines). If youasked a person to connect random points, they wouldn’t jump back and forthacross the canvas like this. They would find a nearby point to use next,producing a more natural tour of the set.</p><p>We’re generating everything automatically, so we can’t manually interveneto choose a natural order for the points. Instead we use Hilbert sorting.</p><h1 id="h_hilbert_sorting">Hilbert sorting<a class="headerlink" aria-label="Link to this header" href="#h_hilbert_sorting"></a></h1><p>The Hilbert space-filling fractal visits every square in a 2D grid.<a rel="external noopener" href="https://doc.cgal.org/latest/Spatial_sorting/index.html">Hilbert sorting</a> uses a Hilbert fractal traversingthe canvas, and sorts the points by when their square is visited by the fractal.This gives a tour of the points that corresponds more closely to what peopleexpect. Points that are close together in space are likely (but not guaranteed)to be close in the ordering.</p><p>If we sort the points using Hilbert sorting, we get much nicer curves. Hereare the same points as last time:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/hobby_sorted.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/hobby_sorted.png" alt="The same three examples of ten points, but the curves make more sense now" width="600" height="200" class="hairline"></picture></figure></div><p>Here are pairs of the same points, unsorted and sorted side-by-side:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/hilbert_compared.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/hilbert_compared.png" alt="Comparing the scribbles and the nice curves" width="400" height="800" class="hairline"></picture></figure></div><p>If you compare closely, the points in each pair are the same, but the sortedpoints are connected in a better order, producing nicer curves.</p><h1 id="h_simplex_noise">Simplex noise<a class="headerlink" aria-label="Link to this header" href="#h_simplex_noise"></a></h1><p>Choosing random points would be easy to do with a random number generator,but we want the points to move in interesting graceful ways. To do that, we usesimplex noise. This is a 2D function (let’s call the inputs u and v) thatproduces a value from -1 to 1. The important thing is the function iscontinuous: if you sample it at two (u,v) coordinates that are close together,the results will be close together. But it’s also random: the continuous curvesyou get are wavy in unpredictable ways. Think of the simplex noise function asa smooth hilly landscape.</p><p>To get an (x,y) point for our drawing, we choose a (u,v) coordinate toproduce an x value and a completely different (u,v) coordinate for the y. Toget the next (x,y) point, we keep the u values the same and change the v values byjust a tiny bit. That makes the (x,y) points move smoothly but interestingly.</p><p>Here are the trails of four points taking 50 steps using this scheme:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/point_motion.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/point_motion.png" alt="Four trails of red dots showing how the randomness creates unpredictable but interesting paths" width="400" height="400" class="hairline"></picture></figure></div><p>If we use seven points taking five steps, and draw curves through the sevenpoints at each step, we get examples like this:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/small_runs.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/small_runs.png" alt="Drawing curves through the points, widely spaced to show the construction" width="600" height="300" class="hairline"></picture></figure></div><p>I’ve left the points visible, and given them large steps so the lines arevery widely spaced to show the motion. Taking out the points and drawing morelines with smaller steps gives us this:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/large_runs.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/large_runs.png" alt="More lines to move toward the look we want" width="600" height="300" class="hairline"></picture></figure></div><p>With 40 lines drawn wider with some transparency, we start to see the smokyfluidity:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/larger_runs.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/larger_runs.png" alt="Now we're getting the original effect" width="600" height="300" class="hairline"></picture></figure></div><h1 id="h_jumps">Jumps<a class="headerlink" aria-label="Link to this header" href="#h_jumps"></a></h1><p>In his Mastodon post, aBe commented on the separating of the lines as one ofthe things he liked about this. But why do they do that? If we are moving thepoints in small increments, why do the curves sometimes make large jumps?</p><p>The first reason is because of Hobby curves. They do a great job drawing acurve through a set of points as a person might. But a downside of thealgorithm is sometimes changing a point a small amount makes the entire curvetake a different route. If you play around with the interactive examples on<a rel="external noopener" href="https://www.jakelow.com/blog/hobby-curves">Jake Low’s page</a> you will see the curve can unexpectedlytake a different shape.</p><p>As we inch our points along, sometimes the Hobby curve jumps.</p><p>The second reason is due to Hilbert sorting. Each of our lines is sortedindependently of how the previous line was sorted. If a point’s small motionmoves it into a different grid square, it can change the sorting order, whichchanges the Hobby curve even more.</p><p>If we sort the first line, and then keep that order of points for all thelines, the result has fewer jumps, but the Hobby curves still actunpredictably:</p><div class="figurep"><figure><picture><source type="image/webp" srcset="https://nedbatchelder.com/iv/webp/pix/fluidity/first_line_runs.png.webp"><img src="https://nedbatchelder.com/pix/fluidity/first_line_runs.png" alt="The same two sets of points as the last figure. Fewer jumps, but still with some discontinuities" width="600" height="300" class="hairline"></picture></figure></div><h1 id="h_colophon">Colophon<a class="headerlink" aria-label="Link to this header" href="#h_colophon"></a></h1><p>This was all done with Python, using other people’s implementations of thehard parts:<a href="https://github.com/ltrujello/Hobby_Curve_Algorithm/blob/main/python/hobby.py" rel="external noopener">hobby.py</a>,<a href="https://pypi.org/project/hilbertcurve/" rel="external noopener">hilbertcurve</a>, and<a href="https://pypi.org/project/super-simplex/" rel="external noopener">super-simplex</a>. My codeis on GitHub(<a href="https://github.com/nedbat/fluidity" rel="external noopener">nedbat/fluidity</a>), but it’s amess. Think of it as a woodworking studio with half-finished pieces and woodchips strewn everywhere.</p><p>A lot of the learning and experimentation was in<a href="https://github.com/nedbat/fluidity/blob/main/play.ipynb" rel="external noopener">my Jupyternotebook</a>. Part of the process for work like this is playing around withdifferent values of tweakable parameters and seeds for the random numbers to getthe effect you want, either artistic or pedagogical. The notebook shows some ofthe thumbnail galleries I used to pick the examples to show.</p><p>I went on to play with animations, which led to other learnings, but thosewill have to wait for another blog post.<b>Update:</b> I animated these in <a href="https://nedbatchelder.com/blog/202510/natural_cubics_circular_simplex.html">Natural cubics, circular Simplex</a>.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202509/testing_is_better_than_dsa.html"> <title>Testing is better than DSA</title> <link>https://nedbatchelder.com/blog/202509/testing_is_better_than_dsa.html</link> <dc:date>2025-09-22T12:04:08-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>I see new learners asking about “DSA” a lot. Data Structures and Algorithmsare of course important: considered broadly, they are the two ingredients thatmake up all programs. But in my opinion, “DSA” as an abstract field of studyis over-emphasized.</p><p>I understand why people focus on DSA: it’s a concrete thing to learn about,there are web sites devoted to testing you on it, and most importantly, becausejob interviews often involve DSA coding questions.</p><p>Before I get to other opinions, let me make clear that anything you can do tohelp you get a job is a good thing to do. If grinding<a rel="external noopener" href="https://leetcode.com/">leetcode</a> will land you a position, then do it.</p><p>But I hope companies hiring entry-level engineers aren’t asking them toreverse linked lists or balance trees. Asking about techniques that can bememorized ahead of time won’t tell them anything about how well you can work.The stated purpose of those interviews is to see how well you can figure outsolutions, in which case memorization will defeat the point.</p><p>The thing new learners don’t understand about DSA is that actual softwareengineering almost never involves implementing the kinds of algorithms that“DSA” teaches you. Sure, it can be helpful to work through some of thesepuzzles and see how they are solved, but writing real code just doesn’t involvewriting that kind of code.</p><p>Here is what I think in-the-trenches software engineers should know aboutdata structures and algorithms:</p><ul> <li>Data structures are ways to organize data. Learn some of the basics: linkedlist, array, hash table, tree. By “learn” I mean understand what it doesand why you might want to use one.</li> <li>Different data structures can be used to organize the same data in differentways. Learn some of the trade-offs between structures that are similar.</li> <li>Algorithms are ways of manipulating data. I don’t mean named algorithmslike Quicksort, but algorithms as any chunk of code that works on data anddoes something with it.</li> <li>How you organize data affects what algorithms you can use to work with thedata. Some data structures will be slow for some operations where anotherstructure will be fast.</li> <li>Algorithms have a “time complexity” (Big O): <a rel="external noopener" href="/text/bigo.html">how the codeslows as the data grows</a>. Get a sense of what this means.</li> <li>Python has a number of built-in data structures. Learn how they work, andthe time complexity of their operations.</li> <li>Learn how to think about your code to understand its time complexity.</li> <li>Read a little about more esoteric things like <a rel="external noopener" href="https://systemdesign.one/bloom-filters-explained/">Bloomfilters</a>, so you can find them later in the unlikely case you need them.</li> </ul><p>Here are some things you don’t need to learn:</p><ul> <li>The details of a dozen different sorting algorithms. Look at two to seedifferent ways of approaching the same problem, then move on.</li> <li>The names of “important” algorithms. Those have all been implemented foryou.</li> <li>The answers to all N problems on some quiz web site. You won’t be askedthese exact questions, and they won’t come up in your real work. Again: try afew to get a feel for how some algorithms work. The exact answers are not whatyou need.</li> </ul><p>Of course some engineers need to implement hash tables, or sorting algorithmsor whatever. We love those engineers: they write libraries we can use off theshelf so we don’t have to implement them ourselves.</p><p>There have been times when I implemented something that felt like AnAlgorithm (for example, <a href="https://nedbatchelder.com/blog/201707/finding_fuzzy_floats.html">Finding fuzzy floats</a>), but it wasmore about considering another perspective on my data, looking at the timecomplexity, and moving operations around to avoid quadratic behavior. It wasn’topening a textbook to find the famous algorithm that would solve my problem.</p><p>Again: if it will help you get a job, deep-study DSA. But don’t bedisappointed when you don’t use it on the job.</p><p>If you want to prepare yourself for a career, and also stand out in jobinterviews, learn how to write tests:</p><ul> <li>This will be a skill you use constantly. Real-world software means writingtests much more than school teaches you to.</li> <li>In a job search, testing experience will stand out more than DSA depth. Itshows you’ve thought about what it takes to write high-quality software insteadof just academic exercises.</li> <li>It’s not obvious how to test code well. It’s a puzzle and a problem tosolve. If you like figuring out solutions to tricky questions, focus on how towrite code so that it can be tested, and how to test it.</li> <li>Testing not only gives you more confidence in your code, it helps you writebetter code in the first place.</li> <li>Testing applies everywhere, from tiny bits of code to entire architectures,assisting you in design and implementation at all scales.</li> <li>If pursued diligently, testing is an engineering discipline in its ownright, with a fascinating array of tools and techniques.</li> </ul><p>Less DSA, more testing.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202508/finding_unneeded_pragmas.html"> <title>Finding unneeded pragmas</title> <link>https://nedbatchelder.com/blog/202508/finding_unneeded_pragmas.html</link> <dc:date>2025-08-24T17:28:12-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>To answer a <a rel="external noopener" href="https://github.com/nedbat/coveragepy/issues/251">long-standing coverage.py feature request</a>, Ithrew together an experiment: a tool to identify lines that have been excludedfrom coverage, but which were actually executed.</p><p>The program is a standalone file in the coverage.py repo. It is unsupported.I’d like people to try it to see what they think of the idea. Later we candecide what to do with it.</p><p>To try it: copy <a rel="external noopener" href="https://github.com/nedbat/coveragepy/blob/master/lab/warn_executed.py">warn_executed.py</a> fromGitHub. Create a .toml file that looks something like this:</p><blockquote class="code"><pre class="toml"><div class="source"><span class="c1"># Regexes that identify excluded lines:</span><br><span class="n">warn-executed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><br><span class="w">    </span><span class="s2">"pragma: no cover"</span><span class="p">,</span><br><span class="w">    </span><span class="s2">"raise AssertionError"</span><span class="p">,</span><br><span class="w">    </span><span class="s2">"pragma: cant happen"</span><span class="p">,</span><br><span class="w">    </span><span class="s2">"pragma: never called"</span><span class="p">,</span><br><span class="w">    </span><span class="p">]</span><br><br><span class="c1"># Regexes that identify partial branch lines:</span><br><span class="n">warn-not-partial</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><br><span class="w">    </span><span class="s2">"pragma: no branch"</span><span class="p">,</span><br><span class="w">    </span><span class="p">]</span><br></div></pre></blockquote><p>These are exclusion regexes that you’ve used in your coverage runs. Theprogram will print out any line identified by a pattern and that ran during yourtests. It might be that you don’t need to exclude the line, because it ran.</p><p>In this file, none of your coverage settings or the default regexes areassumed: you need to explicitly specify all the patterns you want flagged.</p><p>Run the program with Python 3.11 or higher, giving the name of the coveragedata file and the name of your new TOML configuration file. It will print thelines that might not need excluding:</p><blockquote class="code"><pre class="shell"><div class="source">$<span class="w"> </span>python3.12<span class="w"> </span>warn_executed.py<span class="w"> </span>.coverage<span class="w"> </span><span>warn.toml</span><br></div></pre></blockquote><p>The reason for a new list of patterns instead of just reading the existingcoverage settings is that some exclusions are “don’t care” rather than “thiswill never happen.” For example, I exclude “def __repr__” because some__repr__’s are just to make my debugging easier. I don’t care if the test suiteruns them or not. It might run them, so I don’t want it to be a warning thatthey actually ran.</p><p>This tool is not perfect. For example, I exclude “if TYPE_CHECKING:” becauseI want that entire clause excluded. But the if-line itself is actually run. IfI include that pattern in the warn-executed list, it will flag all of thoselines. Maybe I’m forgetting a way to do this: it would be good to have a way toexclude the body of the if clause while understanding that the if-line itself isexecuted.</p><p>Give <a rel="external noopener" href="https://github.com/nedbat/coveragepy/blob/master/lab/warn_executed.py">warn_executed.py</a> a try and comment on<a rel="external noopener" href="https://github.com/nedbat/coveragepy/issues/251">the issue</a> about what you think of it.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202508/starting_with_pytests_parametrize.html"> <title>Starting with pytest’s parametrize</title> <link>https://nedbatchelder.com/blog/202508/starting_with_pytests_parametrize.html</link> <dc:date>2025-08-13T06:14:46-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>Writing tests can be difficult and repetitive. Pytest has a feature calledparametrize that can make it reduce duplication, but it can be hard tounderstand if you are new to the testing world. It’s not as complicated as itseems.</p><p>Let’s say you have a function called <code>add_nums()</code> that adds up a list ofnumbers, and you want to write tests for it. Your tests might look likethis:</p><blockquote class="code"><pre class="python"><div class="source"><span class="k">def</span><span class="w"> </span><span class="nf">test_123</span><span class="p">():</span><br>    <span class="k">assert</span> <span class="n">add_nums</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> <span class="o">==</span> <span class="mi">6</span><br><br><span class="k">def</span><span class="w"> </span><span class="nf">test_negatives</span><span class="p">():</span><br>    <span class="k">assert</span> <span class="n">add_nums</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">])</span> <span class="o">==</span> <span class="mi">0</span><br><br><span class="k">def</span><span class="w"> </span><span class="nf">test_empty</span><span class="p">():</span><br>    <span class="k">assert</span> <span class="n">add_nums</span><span class="p">([])</span> <span class="o">==</span> <span class="mi">0</span><br></div></pre></blockquote><p>This is great: you’ve tested some behaviors of your <code>add_nums()</code>function. But it’s getting tedious to write out more test cases. The names of thefunction have to be different from each other, and they don’t mean anything, soit’s extra work for no benefit. The test functions all have the same structure,so you’re repeating uninteresting details. You want to add more cases but itfeels like there’s friction that you want to avoid.</p><p>If we look at these functions, they are very similar. In any software, whenwe have functions that are similar in structure, but differ in some details, wecan refactor them to be one function with parameters for the differences. We cando the same for our test functions.</p><p>Here the functions all have the same structure: call <code>add_nums()</code> andassert what the return value should be. The differences are the list we pass to<code>add_nums()</code> and the value we expect it to return. So we can turn thoseinto two parameters in our refactored function:</p><blockquote class="code"><pre class="python"><div class="source"><span class="k">def</span><span class="w"> </span><span class="nf">test_add_nums</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span> <span class="n">expected_total</span><span class="p">):</span><br>    <span class="k">assert</span> <span class="n">add_nums</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span> <span class="o">==</span> <span class="n">expected_total</span><br></div></pre></blockquote><p>Unfortunately, tests aren’t run like regular functions. We write the testfunctions, but we don’t call them ourselves. That’s the reason the names of thetest functions don’t matter. The test runner (pytest) finds functions named<code>test_*</code> and calls them for us. When they have no parameters, pytest cancall them directly. But now that our test function has two parameters, we haveto give pytest instructions about how to call it.</p><p>To do that, we use the <code>@pytest.mark.parametrize</code> decorator. Using itlooks like this:</p><blockquote class="code"><pre class="python"><div class="source"><span class="kn">import</span><span class="w"> </span><span class="nn">pytest</span><br><br><span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">parametrize</span><span class="p">(</span><br>    <span class="s2">"nums, expected_total"</span><span class="p">,</span><br>    <span class="p">[</span><br>        <span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="mi">6</span><span class="p">),</span><br>        <span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">],</span> <span class="mi">0</span><span class="p">),</span><br>        <span class="p">([],</span> <span class="mi">0</span><span class="p">),</span><br>    <span class="p">]</span><br><span class="p">)</span><br><span class="k">def</span><span class="w"> </span><span class="nf">test_add_nums</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span> <span class="n">expected_total</span><span class="p">):</span><br>    <span class="k">assert</span> <span class="n">add_nums</span><span class="p">(</span><span class="n">nums</span><span class="p">)</span> <span class="o">==</span> <span class="n">expected_total</span><br></div></pre></blockquote><p>There’s a lot going on here, so let’s take it step by step.</p><p>If you haven’t seen a decorator before, it starts with <code>@</code> and is like aprologue to a function definition. It can affect how the function is defined orprovide information about the function.</p><p>The parametrize decorator is itself a function call that takes two arguments.The first is a string (“nums, expected_total”) that names the two arguments tothe test function. Here the decorator is instructing pytest, “when you call<code>test_add_nums</code>, you will need to provide values for its <code>nums and</code><code>expected_total parameters</code>.”</p><p>The second argument to <code>parametrize</code> is a list of the values to supplyas the arguments. Each element of the list will become one call to our testfunction. In this example, the list has three tuples, so pytest will call ourtest function three times. Since we have two parameters to provide, eachelement of the list is a tuple of two values.</p><p>The first tuple is <code>([1, 2, 3], 6)</code>, so the first time pytest callstest_add_nums, it will call it as test_add_nums([1, 2, 3], 6). All together,pytest will call us three times, like this:</p><blockquote class="code"><pre class="python"><div class="source"><span class="n">test_add_nums</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="mi">6</span><span class="p">)</span><br><span class="n">test_add_nums</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span><br><span class="n">test_add_nums</span><span class="p">([],</span> <span class="mi">0</span><span class="p">)</span><br></div></pre></blockquote><p>This will all happen automatically. With our original test functions, whenwe ran pytest, it showed the results as three passing tests because we had threeseparate test functions. Now even though we only have one function, it stillshows as three passing tests! Each set of values is considered a separate testthat can pass or fail independently. This is the main advantage of usingparametrize instead of writing three separate assert lines in the body of asimple test function.</p><p>What have we gained?</p><ul> <li>We don’t have to write three separate functions with different names.</li> <li>We don’t have to repeat the same details in each function (<code>assert</code>,<code>add_nums()</code>, <code>==</code>).</li> <li>The differences between the tests (the actual data) are written succinctlyall in one place.</li> <li>Adding another test case is as simple as adding another line of data to thedecorator.</li> </ul>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202507/coveragepy_regex_pragmas.html"> <title>Coverage.py regex pragmas</title> <link>https://nedbatchelder.com/blog/202507/coveragepy_regex_pragmas.html</link> <dc:date>2025-07-28T12:04:12-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p><a rel="external noopener" href="https://coverage.readthedocs.io/">Coverage.py</a> lets you indicate code to exclude frommeasurement by adding comments to your Python files. But coverage implementsthem differently than other similar tools. Rather than having fixed syntax forthese comments, they are defined using regexes that you can change or add to.This has been surprisingly powerful.</p><p>The basic behavior: coverage finds lines in your source files that match theregexes. These lines are excluded from measurement, that is, it’s OK if theyaren’t executed. If a matched line is part of a multi-line statement thewhole multi-line statement is excluded. If a matched line introduces a block ofcode the entire block is excluded.</p><p>At first, these regexes were just to make it easier to implement the basic“here’s the comment you use” behavior for pragma comments. But it also enabledpragma-less exclusions. You could decide (for example) that you didn’t care totest any <code>__repr__</code> methods. By adding <code>def __repr__</code> as an exclusionregex, all of those methods were automatically excluded from coveragemeasurement without having to add a comment to each one. Very nice.</p><p>Not only did this let people add custom exclusions in their projects, butit enabled third-party plugins that could configure regexes in other interestingways:</p><ul> <li><a href="https://pypi.org/project/covdefaults/" rel="external noopener">covdefaults</a> adds abunch of default exclusions, and also platform- and version-specific commentsyntaxes.</li> <li><a href="https://pypi.org/project/coverage-conditional-plugin/" rel="external noopener">coverage-conditional-plugin</a>gives you a way to create comment syntaxes for entire files, for whether otherpackages are installed, and so on.</li> </ul><p>Then about a year ago, <a rel="external noopener" href="https://github.com/nedbat/coveragepy/pull/1807">Daniel Diniz contributed achange</a> that amped up the power: regexes could match multi-line patterns.This sounds like not that large a change, but it enabled much more powerfulexclusions. As a sign, it made it possible to support <a rel="external noopener" href="https://coverage.readthedocs.io/en/latest/changes.html#version-7-6-0-2024-07-11">fourdifferent feature requests</a>.</p><p>To make it work, Daniel changed the matching code. Originally, it was a loopover the lines in the source file, checking each line for a match against theregexes. The new code uses the entire source file as the target string, andloops over the matches against that text. Each match is converted into a set ofline numbers and added to the results.</p><p>The power comes from being able to use one pattern to match many lines. Forexample, one of the four feature requests was <a rel="external noopener" href="https://github.com/nedbat/coveragepy/issues/118">how to exclude anentire file</a>. With configurable multi-line regex patterns, you can do thisyourself:</p><blockquote class="code"><pre>\A(?s:.*# pragma: exclude file.*)\Z<br></pre></blockquote><p>With this regex, if you put the comment “# pragma: exclude file” in yoursource file, the entire file will be excluded. The <code>\A</code> and <code>\Z</code>match the start and end of the target text, which remember is the entire file.The <code>(?s:...)</code> means the <a rel="external noopener" href="https://docs.python.org/3/library/re.html#re.S">s/DOTALL</a> flag is ineffect, so <code>.</code> can match newlines. This pattern matches the entire sourcefile if the desired pragma is somewhere in the file.</p><p>Another requested feature was <a rel="external noopener" href="https://github.com/nedbat/coveragepy/issues/1803">excluding code between twolines</a>. We can use “# no cover: start” and “# no cover: end” as delimiterswith this regex:</p><blockquote class="code"><pre># no cover: start(?s:.*?)# no cover: stop<br></pre></blockquote><p>Here <code>(?s:.*?)</code> means any number of any character at all, but as few aspossible. A star in regexes means as many as possible, but star-question-markmeans as few as possible. We need the minimal match so that we don’t match fromthe start of one pair of comments all the way through to the end of a differentpair of comments.</p><p>This regex approach is powerful, but is still fairly shallow. For example,either of these two examples would get the wrong lines if you had a stringliteral with the pragma text in it. There isn’t a regex that skips easily overstring literals.</p><p>This kind of difficulty hit home when I added a new default pattern toexclude empty placeholder methods like this:</p><blockquote class="code"><pre class="python"><div class="source"><span class="k">def</span><span class="w"> </span><span class="nf">not_yet</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span><br><br><span class="k">def</span><span class="w"> </span><span class="nf">also_not_this</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><br>    <span class="o">...</span><br><br><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">definitely_not_this</span><span class="p">(</span><br>    <span class="bp">self</span><span class="p">,</span><br>    <span class="n">arg1</span><span class="p">,</span><br><span class="p">):</span><br>    <span class="o">...</span><br></div></pre></blockquote><p>We can’t just match three dots, because ellipses can be used in other placesthan empty function bodies. We need to be more delicate. I ended up with:</p><blockquote class="code"><pre>^\s*(((async )?def .*?)?\)(\s*->.*?)?:\s*)?\.\.\.\s*(#|$)<br></pre></blockquote><p>This craziness ensures the ellipsis is part of an (async) def, that theellipsis appears first in the body (but no docstring allowed, doh!), allows fora comment on the line, and so on. And even with a pattern this complex, itwould incorrectly match this contrived line:</p><blockquote class="code"><pre class="python"><div class="source"><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">():</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"(well): ... #2 false positive!"</span><span class="p">)</span><br></div></pre></blockquote><p>So regexes aren’t perfect, but they’re a pretty good balance: flexible andpowerful, and will work great on real code even if we can invent weird edgecases where they fail.</p><p>What started as a simple implementation expediency has turned into a powerfulconfiguration option that has done more than I would have thought.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202507/coverage_7100_patch.html"> <title>Coverage 7.10.0: patch</title> <link>https://nedbatchelder.com/blog/202507/coverage_7100_patch.html</link> <dc:date>2025-07-24T19:03:27-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>Years ago I greeted a friend returning from vacation and asked how it hadbeen. She answered, “It was good, I got a lot done!” I understand that feeling.I just had a long vacation myself, and used the time to clean up some old issuesand add some new features in <a rel="external noopener" href="https://pypi.org/project/coverage/">coverage.py v7.10</a>.</p><p>The major new feature is a configuration option,<a rel="external noopener" href="https://coverage.readthedocs.io/en/latest/config.html#run-patch"><code>[run] patch</code></a>. With it, you specify namedpatches that coverage can use to monkey-patch some behavior that gets in the wayof coverage measurement.</p><p>The first is <code>subprocess</code>. Coverage works great when you start yourprogram with coverage measurement, but has long had the problem of how to alsomeasure the coverage of sub-processes that your program created. The existingsolution had been a complicated two-step process of creating obscure .pth filesand setting environment variables. Whole projects appeared on PyPI to handlethis for you.</p><p>Now, <code>patch = subprocess</code> will do this for you automatically, and cleanitself up when the program ends. It handles sub-processes created by the<a rel="external noopener" href="https://docs.python.org/3/library/subprocess.html#module-subprocess">subprocess</a> module, the<a rel="external noopener" href="https://docs.python.org/3/library/os.html#os.system">os.system()</a> function, and any of the<a rel="external noopener" href="https://docs.python.org/3/library/os.html#os.execl">execv</a> or <a rel="external noopener" href="https://docs.python.org/3/library/os.html#os.spawnl">spawnv</a> families offunctions.</p><p>This alone has spurred <a rel="external noopener" href="https://bsky.app/profile/did:plc:yj4vzsbzzkpswr7x5yagzhhx/post/3luqfffiiqk27">one user to exclaim</a>,</p><blockquote><div><p>The latest release of Coverage feels like a Christmas present!The native support for Python subprocesses is so good!</p></div></blockquote><p>Another patch is <code>_exit</code>. This patches<a rel="external noopener" href="https://docs.python.org/3/library/os.html#os._exit">os._exit()</a> so that coverage saves its data beforeexiting. The os._exit() function is an immediate and abrupt termination of theprogram, skipping all kinds of registered clean up code. This patch makes itpossible to collect coverage data from programs that end this way.</p><p>The third patch is <code>execv</code>. The <a rel="external noopener" href="https://docs.python.org/3/library/os.html#os.execl">execv</a> functionsend the current program and replace it with a new program in the same process.The <code>execv</code> patch arranges for coverage to save its data before thecurrent program is ended.</p><p>Now that these patches are available, it seems silly that it’s taken so long.They (mostly) weren’t difficult. I guess it took looking at the old issues,realizing the friction they caused, and thinking up a new way to let userscontrol the patching. Monkey-patching is a bit invasive, so I’ve never wantedto do it implicitly. The patch option gives the user an explicit way to requestwhat they need without having to get into the dirty details themselves.</p><p>Another process-oriented feature was contributed by Arkady Gilinsky: with<code>--save-signal=USR1</code> you can specify a user signal that coverage willattend to. When you send the signal to your running coverage process, it willsave the collected data to disk. This gives a way to measure coverage in along-running process without having to end the process.</p><p>There were some other fixes and features along the way, like better HTMLcoloring of multi-line statements, and more default exclusions(<code>if TYPE_CHECKING:</code> and <code>...</code>).</p><p>It feels good to finally address some of these pain points. I also closedsome stale issues and pull requests. There is more to do, always more to do,but this feels like a real step forward. Give <a rel="external noopener" href="https://coverage.readthedocs.io/en/7.10.0/changes.html#version-7-10-0-2025-07-24">coverage7.10.0</a> a try and let me know how it works for you.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202507/2048_iterators_and_iterables.html"> <title>2048: iterators and iterables</title> <link>https://nedbatchelder.com/blog/202507/2048_iterators_and_iterables.html</link> <dc:date>2025-07-15T06:52:29-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>I wrote a <a rel="external noopener" href="https://github.com/nedbat/odds/blob/master/2048/2048.py">low-tech terminal-based version</a> of theclassic <a rel="external noopener" href="https://play2048.co/">2048 game</a> and had some interesting difficultieswith iterators along the way.</p><p>2048 has a 4<span class="times">×</span>4 grid with sliding tiles. Because the tiles can slideleft or right and up or down, sometimes we want to loop over the rows andcolumns from 0 to 3, and sometimes from 3 to 0. My first attempt looked likethis:</p><blockquote class="code"><pre class="python"><div class="source"><span class="n">N</span> <span class="o">=</span> <span class="mi">4</span><br><span class="k">if</span> <span class="n">sliding_right</span><span class="p">:</span><br>    <span class="n">cols</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>   <span class="c1"># 3 2 1 0</span><br><span class="k">else</span><span class="p">:</span><br>    <span class="n">cols</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>             <span class="c1"># 0 1 2 3</span><br><br><span class="k">if</span> <span class="n">sliding_down</span><span class="p">:</span><br>    <span class="n">rows</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>   <span class="c1"># 3 2 1 0</span><br><span class="k">else</span><span class="p">:</span><br>    <span class="n">rows</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>             <span class="c1"># 0 1 2 3</span><br><br><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">rows</span><span class="p">:</span><br>    <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">cols</span><span class="p">:</span><br>        <span class="o">...</span><br></div></pre></blockquote><p>This worked, but those counting-down ranges are ugly. Let’s make itnicer:</p><blockquote class="code"><pre class="python"><div class="source"><span class="n">cols</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>                 <span class="c1"># 0 1 2 3</span><br><span class="k">if</span> <span class="n">sliding_right</span><span class="p">:</span><br>    <span class="n">cols</span> <span class="o">=</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">cols</span><span class="p">)</span>       <span class="c1"># 3 2 1 0</span><br><br><span class="n">rows</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>                 <span class="c1"># 0 1 2 3</span><br><span class="k">if</span> <span class="n">sliding_down</span><span class="p">:</span><br>    <span class="n">rows</span> <span class="o">=</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span>       <span class="c1"># 3 2 1 0</span><br><br><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">rows</span><span class="p">:</span><br>    <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">cols</span><span class="p">:</span><br>        <span class="o">...</span><br></div></pre></blockquote><p>Looks cleaner, but it doesn’t work! Can you see why? It took me a bit ofdebugging to see the light.</p><p><code>range()</code> produces an iterable: something that can be iterated over.Similar but different is that <code>reversed()</code> produces an iterator: somethingthat is already iterating. Some iterables (like ranges) can be used more thanonce, creating a new iterator each time. But once an iterator like<code>reversed()</code> has been consumed, it is done. Iterating it again willproduce no values.</p><p>If “iterable” vs “iterator” is already confusing here’s a quick definition:an iterable is something that can be iterated, that can produce values in aparticular order. An iterator tracks the state of an iteration in progress. Ananalogy: the pages of a book are iterable; a bookmark is an iterator. TheEnglish hints at it: an iter-able is able to be iterated at some point, aniterator is actively iterating.</p><p>The outer loop of my double loop was iterating only once over the rows, sothe row iteration was fine whether it was going forward or backward. But thecolumns were being iterated again for each row. If the columns were goingforward, they were a range, a reusable iterable, and everything worked fine.</p><p>But if the columns were meant to go backward, they were a one-use-onlyiterator made by <code>reversed()</code>. The first row would get all the columns,but the other rows would try to iterate using a fully consumed iterator and getnothing.</p><p>The simple fix was to use <code>list()</code> to turn my iterator into a reusableiterable:</p><blockquote class="code"><pre class="python"><div class="source"><span class="n">cols</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">cols</span><span class="p">))</span><br></div></pre></blockquote><p>The code was slightly less nice, but it worked. An even better fixwas to change my doubly nested loop into a single loop:</p><blockquote class="code"><pre class="python"><div class="source"><span class="k">for</span> <span class="n">row</span><span class="p">,</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">cols</span><span class="p">):</span><br></div></pre></blockquote><p>That also takes care of the original iterator/iterable problem, so I can getrid of that first fix:</p><blockquote class="code"><pre class="python"><div class="source"><span class="n">cols</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><br><span class="k">if</span> <span class="n">sliding_right</span><span class="p">:</span><br>    <span class="n">cols</span> <span class="o">=</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">cols</span><span class="p">)</span><br><br><span class="n">rows</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">)</span><br><span class="k">if</span> <span class="n">sliding_down</span><span class="p">:</span><br>    <span class="n">rows</span> <span class="o">=</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span><br><br><span class="k">for</span> <span class="n">row</span><span class="p">,</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">cols</span><span class="p">):</span><br>    <span class="o">...</span><br></div></pre></blockquote><p>Once I had this working, I wondered why <code>product()</code> solved theiterator/iterable problem. The <a rel="external noopener" href="https://docs.python.org/3/library/itertools.html#itertools.product">docs have a sample Pythonimplementation</a> that shows why: internally, <code>product()</code> is doing justwhat my <code>list()</code> call did: it makes an explicit iterable from each of theiterables it was passed, then picks values from them to make the pairs. Thislets <code>product()</code> accept iterators (like my reversed range) rather thanforcing the caller to always pass iterables.</p><p>If your head is spinning from all this iterable / iterator / iteration talk,I don’t blame you. Just now I said, “it makes an explicit iterable from each ofthe iterables it was passed.” How does that make sense? Well, an iterator is aniterable. So <code>product()</code> can take either a reusable iterable (like a rangeor a list) or it can take a use-once iterator (like a reversed range). Eitherway, it populates its own reusable iterables internally.</p><p>Python’s iteration features are powerful but sometimes require carefulthinking to get right. Don’t overlook the tools in itertools, and mind youriterators and iterables!</p><p class="bulletsep">• • •</p><p>Some more notes:</p><p>1: Another way to reverse a range: you can slice them!</p><blockquote class="code"><pre class="python"><div class="source"><span class="o">>>></span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><br><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span><br><span class="o">>>></span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><br><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span><br><span class="o">>>></span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span><br><span class="o"><</span><span class="n">range_iterator</span> <span class="nb">object</span> <span class="n">at</span> <span class="mh">0x10307cba0</span><span class="o">></span><br></div></pre></blockquote><p>It didn’t occur to me to reverse-slice the range, since <code>reversed</code> isright there, but the slice gives you a new reusable range object while reversingthe range gives you a use-once iterator.</p><p>2: Why did <code>product()</code> explicitly store the values it would need but<code>reversed</code> did not? Two reasons: first, <code>reversed()</code> depends on the<code>__reversed__</code> dunder method, so it’s up to the original object to decidehow to implement it. Ranges know how to produce their values in backward order,so they don’t need to store them all. Second, <code>product()</code> is going to needto use the values from each iterable many times and can’t depend on theiterables being reusable.</p>]]></description> </item> <item rdf:about="https://nedbatchelder.com/blog/202506/math_factoid_of_the_day_63.html"> <title>Math factoid of the day: 63</title> <link>https://nedbatchelder.com/blog/202506/math_factoid_of_the_day_63.html</link> <dc:date>2025-06-16T00:00:00-04:00</dc:date> <dc:creator>Ned Batchelder</dc:creator> <description><![CDATA[<p>63 is a <a rel="external noopener" href="https://en.wikipedia.org/wiki/Centered_octahedral_number">centered octahedral number</a>. That means if youbuild an approximation of an octahedron with cubes, one size of octahedron willhave 63 cubes.</p><p>In the late 1700’s <a rel="external noopener" href="https://en.wikipedia.org/wiki/Ren%C3%A9_Just_Ha%C3%BCy">René Just Haüy</a> developed a theoryabout how crystals formed: successive layers of fundamental primitives inorderly arrangements. One of those arrangements was stacking cubes together tomake an octahedron.</p><p>Start with one cube:</p><div class="figurep"><img src="https://nedbatchelder.com/code/diagrams/hauy/0.svg" alt="Just one lonely cube"></div><p>Add six more cubes around it, one on each face. Now we have seven:</p><div class="figurep"><img src="https://nedbatchelder.com/code/diagrams/hauy/1.svg" alt="Seven cubes as a crude octahedron"></div><p>Add another layer, adding a cube to touch each visible cube, making 25:</p><div class="figurep"><img src="https://nedbatchelder.com/code/diagrams/hauy/2.svg" alt="25 cubes arranged like an octahedron five cubes wide"></div><p>One more layer and we have a total of 63:</p><div class="figurep"><img src="https://nedbatchelder.com/code/diagrams/hauy/3.svg" alt="63 cubes arranged like an octahedron seven cubes wide"></div><p>The remaining numbers in <a href="https://oeis.org/A001845" rel="external noopener">the sequence</a>less than 10,000 are 129, 231, 377, 575, 833, 1159, 1561, 2047, 2625, 3303,4089, 4991, 6017, 7175, 8473, 9919.</p><p>63 also shows up in the <a rel="external noopener" href="https://en.wikipedia.org/wiki/Delannoy_number">Delannoy numbers</a>: thenumber of ways to traverse a grid from the lower left corner to upper rightusing only steps north, east, or northeast. Here are the 63 ways of moving on a3<span class="times">×</span>3 grid:</p><div class="figurep"><img src="https://nedbatchelder.com/code/diagrams/delannoy3.svg" alt="63 different ways to traverse a 3x3 grid"></div><p>(Diagram from <a href="https://en.wikipedia.org/wiki/File:Delannoy3x3.svg" rel="external noopener">Wikipedia</a>)</p><p>In fact, the number of cubes in a Haüy octahedron with N layers is the sameas the number of Delannoy steps on a 3<span class="times">×</span>N grid!</p><p>Since the two ideas are both geometric and fairly simple, I would love tofind a geometric explanation for the correspondence. The octahedron isthree-dimensional, and the Delannoy grids have that tantalizing 3 in them. Itseems like there should be a way to convert Haüy coordinates to Delannoycoordinates to show how they relate. But I haven’t found one...</p><p class="bulletsep">• • •</p><p>Colophon: I made the octahedron diagrams by asking Claude to write a<a href="https://nedbatchelder.com/code/diagrams/hauy/hauy_oct.py">Python program</a> to do it.It wasn’t a fast process because it took pushing and prodding to get thediagrams to come out the way I liked. But Claude was very competent, and Icould think about the results rather than about projections or color spaces. Icould dip into it for 10 minutes at a time over a number of days without havingto somehow reconstruct a mental context.</p><p>This kind of casual hobby programming is perfect for AI assistance. I don’tneed the code to be perfect or even good, I just want the diagrams to be nice.I don’t have the focus time to learn how to write the program, so I can leave itto an imperfect assistant.</p>]]></description> </item> </rdf:RDF> If you would like to create a banner that links to this page (i.e. this validation result), do the following:
Download the "valid RSS" banner.
Upload the image to your own server. (This step is important. Please do not link directly to the image on this server.)
Add this HTML to your page (change the image src attribute if necessary):
If you would like to create a text link instead, here is the URL you can use:
http://www.feedvalidator.org/check.cgi?url=https%3A//www.nedbatchelder.com/blog/rss.xml