I’ve had an idea rattling around to get more detail from coverage
measurement. Can we measure the coverage in a function separately for each
caller of the function?
Here’s why I want it: in Acidica, my toy
BASIC interpreter, I have code to implement the built-in functions that looks
something like this:
match func_name:
case "LEN":
if len(args) != 1:
raise TypeError(f"Wrong arguments for LEN, got {len(args)}")
return len(args[0])
case "LEFT$":
if len(args) != 2:
raise TypeError(f"Wrong arguments for LEFT$, got {len(args)}")
return args[0][:args[1]]
# ... 19 other built-ins ...
I didn’t like the repeated code here: each different func_name has to
check that it got its expected number of arguments and perhaps raise an error.
So I refactored:
def expects(nargs: int, func_name: str, args: tuple) -> None:
if len(args) != nargs:
raise TypeError(f"Wrong arguments for {func_name}, got {len(args)}")
match func_name:
case "LEN":
expects(1, func_name, args)
return len(args[0])
case "LEFT$":
expects(2, func_name, args)
return args[0][:args[1]]
Nice. The code is tighter, easier to read, and common behavior is implemented
in one place.
But the old code had an advantage: because each error condition had its own
raise line, coverage measurement could tell me whether I had tested every
func_name for the wrong number of arguments. With the error handling
happening in a helper function, that information is lost. I’ll know that
somefunc_name had a test for the wrong number of arguments, but
not that all of them did.
Here’s where the new idea comes in. What if I could indicate that for the
expects function, I want separate coverage data for each distinct calling
site? Then I could see that every func_name had a test for both the wrong
number of arguments and the right number of arguments. The simple branch inside
expects would be measured separately for each caller.
I have a quick proof-of-concept. A decorator on expects does the work.
Coverage.py already has dynamic contexts which are used for things like tracking
which tests called which code. The decorator starts a new context named for the
calling location, then restores the context when the function returns:
def coverage_per_caller(func):
@functools.wraps(func)
def _wrapper(*args, **kwargs):
cov = coverage.Coverage.current()
name = func.__name__
caller = inspect.currentframe().f_back
file = caller.f_code.co_filename
lineno = caller.f_lineno
prev_context = cov.switch_context(f"per_caller:{name}:{file}:{lineno}")
try:
ret = func(*args, **kwargs)
finally:
cov.switch_context(prev_context)
return ret
return _wrapper
I had to make one tiny (unreleased) change to coverage.py for this:
switch_context used to return None, but now it returns the previous
context so that we can nest them properly.
To my delight, this works! I can look at the HTML coverage report and see the
caller contexts for the lines in expects. I can see that 20 callers ran
the if line, but only 2 ran the raise, and the context names show
the file and line number of the callers for each:
This isn’t the whole solution yet. Things to improve:
- I’d like to post-process these contexts to show which callers were missing
lines inside
expects. What I’m looking for is the same kind of “this
line is missing” information that I got from the original inlined logic. - These per-caller contexts overwrite the contexts we were already collecting
(the test names). Ideally we’d have some kind of sub-context so that we could
track both (or many) at once.
- It’s not great that I had to add a decorator to the source code. Driving
this through the coverage configuration would keep these kinds of details out of
the source.
But it’s a start, and gives me other ideas. I could use some aspect of the
data passed into a function as the context name. In this example, we could have
used func_name as the context instead of the caller’s location. Maybe
you have ideas for other uses.
My latest fun project is a BASIC interpreter called
Acidica. Classic BASIC is an
old-school language first developed in 1964 that saw an explosion of
implementations on microcomputers in the ‘70s and ‘80s. It’s much more primitive
than the Visual Basic that you might be familiar with.
A simple BASIC program:
10 INPUT "What is your name"; U$
20 PRINT "Hello "; U$
30 INPUT "How many stars do you want"; N
40 S$ = ""
50 FOR I = 1 TO N
60 S$ = S$ + "*"
70 NEXT I
80 PRINT S$
90 INPUT "Do you want more stars"; A$
100 IF LEN(A$) = 0 THEN 90
110 A$ = LEFT$(A$, 1)
120 IF A$ = "Y" OR A$ = "y" THEN 30
130 PRINT "Goodbye ";U$
140 END
Run it, and you get this:
What is your name? Ned
Hello Ned
How many stars do you want? 10
**********
Do you want more stars? y
How many stars do you want? 20
********************
Do you want more stars? n
Goodbye Ned
The wide variety of BASIC flavors meant I first had to decide what to
implement. I found Vintage BASIC and used its spec,
both because it is concisely described, and because it has an implementation I
could run to double-check behavior when I had questions. The site also has a
collection of runnable games from
Creative Computing magazine, which I remember
fondly.
This was a perfect vacation-week project. It has no real-world consequences.
It had some interesting problems to puzzle through. It was testable. It
satisfied some nostalgia for my earlier computing days. It was bounded enough to
be “done”.
In those ways, it’s very similar to a vacation project of mine from four
years ago: Stilted, an implementation of
PostScript.
Acidica is not useful for writing new programs, only because BASIC itself is
so difficult. There is no scoping beyond single-line functions, variables names
can be as long as you want but only the first two letters and first digit are
significant. Keywords are recognized anywhere, so FACTOR can’t be variable name
because it has TO in the middle. The only control structures are FOR, IF, and
GOTO. It’s something of a testament to human persistence that programs like
three-dimensional tic-tac-toe can be written in it.
As a side project, I could choose my development style: no real type
checking (partly because BASIC’s values would be awkward to squeeze into static
typing), and very few docstrings. There are lots of tests, but only integration
tests: every test is a BASIC program to run, with a check for the correct output
and/or the expected error.
To be honest, the “only integration tests” approach was kind of a pain, but I
stuck with it and resisted the temptation to add unit tests along the way.
Another choice I made: no AI. I like writing programs. I get a deeper sense
of the thing I am building when I have my fingers in the clay. Since there was
no deadline, or even any reason to ever finish the project, I could take my time
and not be rushed.
But I like the result. I enjoyed the time I spent working on it. I liked
being able to stop and devote pure thinking time while doing other things when I
got to the next hurdle. The next steps here might be to use this project as a
test bed for some development ideas. Or maybe add a BASIC-to-Python transpiler.
Or maybe it’s done.
My mother is 86, and she is declining. Things that used to be easy for her
now seem completely foreign. She was a programmer, writing software before I
could read, so it is very strange to see her like this.
She no longer uses a computer. If I mention some photos I found online, she
asks if there’s any way she can see them, as if she has never used the internet.
This is a new reality for me, but is easier than a year or two ago when she
still tried to be constantly online. As things got more confusing for her, she
struggled and complained “the computer is haunted.” Now she doesn’t have the
computer as a source of friction, but also not as a center of activity.
In many ways, she is following a similar path to her own mother, my
Grandma O. Like her, my mom is accepting
the changes in her relationship to the world. She is able to laugh at it a
bit. But it will still be difficult, especially because we know it is a
progression that is not going to get better and will very likely get worse.
The new her is very different from the original her. She was not timid. She
came out as gay in the mid ‘70s and ran a feminist
bookstore. She worked as a programmer. She got a PhD in computational
linguistics just because she was interested in the topic. These were the things
I was used to hearing about from her. She never lacked for enthusiasms, projects
and accomplishments.
She was always energetic and feisty, ready to engage in debate. This picture
does a good job capturing the spirit of many of our interactions in the
past:
Now she is mild and somewhat resigned. She says things like, “I don’t think
much anymore.” I know there are other ways this could go. Some people get very
angry as their abilities fade. In that sense, this is a good trajectory, but I
am still sad to see her shrink.
Last week we had a family gathering at my sister’s house, the usual location
for these big events. My mom has been there many times. But now she didn’t
recognize it. I sat with my mom and sister over lunch. They were discussing the
dining room we were in. It wasn’t familiar to my mom. She wasn’t upset about it,
just looked around and said, “no, I don’t remember this.”
My mom was enjoying her salad, but eating it with her hands. I pointed to
the fork on her plate and asked, “You don’t like the fork?” She looked at it as
if it was some unimportant detail of the tablecloth, and kept eating with her
hands. She wasn’t bothered, just calmly proceeded in her way.
At the end of the party, my mom and her wife Fumiko were getting ready to go.
Fumiko had scheduled a ride-share car, so we went out to the street to wait for
it. We brought out a chair for my mom to sit. The time for the car came and
went, but no car arrived. There were five of us out there: me, my sister and
brother, my mother and Fumiko. My brother and Fumiko were trying to figure out
where the car was. They were looking through the app for information. They
re-read the email confirming the scheduled ride. Should we keep waiting? We
could request a new ride. Would we be charged for the missed scheduled ride? It
was a whole thing, lots of discussion and questions.
In the middle of this, without warning, my mom tried unsteadily to get up
from her chair. Two of us quickly intercepted her. The uneven pavement seemed
particularly treacherous for her. We supported her arms to keep her steady.
“Mom, where are you trying to go?”
“I want a place of certainty. This place seems very uncertain.”
She was right: out there on the sidewalk we were all uncertain. But I have to
wonder if she was also talking about her larger experience in a world that is
less and less understandable for her.
In the back of my mind, I wonder what my own future holds. But that is
decades away, and my mother’s situation is now. I don’t know what her next
steps down will be like. She has already changed a great deal in the last
year.
I think we would all like a place of certainty. I know I would, but I also
know I am not going to get it soon.
I saw this dodecahedron with an Islamic-inspired pattern
designed by Taj Ragoo. As soon as I saw it, I knew I had to make one. I
studied the pattern, wrote some Python, and made myself a PDF. I cut it out,
folded it, glued it together, and now I have one of my own:
I love that this elegantly combines two pure geometric forms: the Platonic
dodecahedron (12 uniform pentagons), and an Islamic pattern using five-pointed
stars.
Looking closely, details emerge:
Each face has ten small stars in a ring. I’ve lightened them a bit in the
front face here. At the center of each face is a ten-pointed star (highlighted
in red), made of two overlaid five-pointed stars.
The real genius of the pattern is at the corners. I’ve highlighted one in
blue. It’s a star made of the same parts as the central ten-pointed star, but
there are only nine points. It works because three pentagons lying flat touching
at a point occupy 324 degrees, leaving a 36-degree gap.
When the dodecahedron is folded together, the gap is closed. 36 degrees is
exactly one-tenth of a complete 360-degree circle, so exactly one point of the
ten-pointed star is missing, leaving a perfect nine-pointed star using the same
shapes, spread over the corners of three pentagons. Beautiful!
If this appeals to you, follow Taj on Instagram:
he’s got more Platonic/Islamic mashups to enjoy. The paper versions are just
prototypes of the final versions he makes in wood.
Of course, you can get my PDF and make one
for yourself:
The Python code to draw the net isn’t great: it
has no real parallels to the structure of each face. It’s a lot of math and
line drawing to get things in the right places. My ideal would be to have a
toolset that used a tile-placing abstraction, to be able to do more interesting
designs. Some day.
It was a joy to work on this though. It was a slow process of studying the
original, working out the math, then mulling over coding approaches. The code was developed in small
steps over weeks. Then printing initial versions, marking them up, working out the tab structure.
Some copies were colored to understand how the lines flowed across the whole dodecahedron.
It was good to be working in both the mental and physical worlds:
Update: it looks like the design was originally by Dana Awartani:
Dodecahedron Within an Icosahedron.
Perhaps because 64 is a power of two, and a square and a cube, but also for
other reasons, it pops up in lots of places. Here are some of the things I’ve
associated it with over the years:
¶ Crayola
64-crayon box: as a kid, this box seemed like the ultimate luxury, the
Rolls-Royce of crayons. So many colors, and the box had a built-in sharpener.
Advanced technology!
¶ A chess board has 64 squares, and is used as the setting for the age-old
question about doubling: would you rather have one billion dollars, or a penny
on the first square, then double the number on each next square? It’s an
eye-opening demonstration of exponential growth and how big numbers can get.
Take the chess board: you’ll have 184 million times more money!
¶ I grew up in New York City too late to visit the
1964
World’s Fair, but its aura hung over the city. I was always fascinated by
it, and still am. It epitomized the early 60’s optimism about the future. This
Love of Theme Parks
video does a good job capturing the Fair’s original spirit and the current
state of the location, and explains the important part Walt Disney played in the
whole thing.
¶ As a power of 2, it appears in many tech things: Nintendo 64, Commodore 64,
base-64 encoding, 64-bit integers, 64-bit computing in general, and so on and so
on.
¶ The number famously appears in the Beatles’ song
When I’m
Sixty-Four. A surprising fact about the song is that it’s one of the first
Paul McCartney ever wrote, when he was about 14 years old. It’s an old-fashioned
tune because he wasn’t aware of rock and roll yet, or maybe it hadn’t even
happened yet. It’s thought they put the song on Sgt Pepper because Paul’s
father was turning 64 that year.
There are many historical artifacts and monuments in Boston. This is one of
my favorites:
It’s in the center of the
Granary Burying
Ground, the third-oldest cemetery in Boston. Casual tourists will assume the
monument marks Ben Franklin’s grave, but they are wrong: it is for his
parents.
Ben Franklin wrote an inscription for his parents’ grave. The marker
deteriorated and in 1827 was replaced with this large obelisk and a new
plaque.
The plaque is far from the walkway and hard to read even up close:
It reads:
JOSIAH FRANKLIN AND ABIAH HIS WIFE
lie here interred.
They lived lovingly together in wedlock fifty five years. And without an estate,
or any gainful employment, by constant labor and honest industry, maintained a large
family comfortably, and brought up thirteen children and seven grandchildren respectably.
From this instance, reader, be encouraged to diligence in thy calling, and distrust
not providence. He was a pious and prudent man; she a discreet and virtuous woman.
THEIR YOUNGEST SON,
in filial regard to their memory places this stone.
J.F. Born 1655 __ Died 1744, Æ. 89.
A.F. ___ 1667 _______ 1752, __ 85.
The original inscription having been nearly obliterated
A number of citizens erected this monument, as a mark of respect for the
ILLUSTRIOUS AUTHOR,
MDCCCXXVII
I love that neither the original inscription nor the re-dedication mentions
Ben Franklin by name. He wanted the focus to be on his parents, and the citizens
of 1827 understood and kept their words in his style. He writes lovingly about
his parents and their lifestyle, and keeps us thinking about them, not him.
I also like the word “reader” in there. Even when writing tombstones, Ben
couldn’t resist his Poor Richard’s Almanac pedagogical style.
A few blocks from the cemetery is a plaque on Court St
marking the location of James Franklin’s printing shop where Ben was an apprentice:
You can see in the picture there’s a one-block-long narrow dingy alleyway
typical of downtown areas. It’s used for vans and dumpsters. I guess because
it’s the location of the Franklin printing shop, this unremarkable and
depressing passage is named “Franklin Avenue”. I would have expected something
grander based on the name.
Maybe Ben wouldn’t have wanted something grander?
Older: