Wednesday, September 21, 2022

Python Context Manager


By Llewellyn Falco & Matt Plavcan

I've been doing a lot python lately and context Manager has thrown me for a bit of a loop. Here's what happened

Context

First, A ContextManager is what python uses for the with statement. It is the python equivalent of:

  • java's try with resources
  • c#'s using

It allows you to do the following:

@contextmanager
def printer():
    print("enter")
    yield
    print("exit")

if __name__ == '__main__':
    with printer():
        print("middle")

Which prints

enter
middle
exit

Problem 1) Yield fails with exceptions

Let's say we have

@contextmanager
def printer():
    print("enter")
    yield
    print("exit")

if __name__ == '__main__':
    with printer():
        raise Exception("middle")

I would expect it to close the context and then pass on the Exception. Like such:

enter
Exception: middle
exit

But it doesn't. Instead, I get:

enter
Exception: middle

There is no exit printed.

Solution 1) Use ContextManager class

I need to expand the Printer to a full class. ContextManager is just an interface that assumes you have the __enter__ and __exit__ methods defined on a class.

For example:

class Printer():
    def __enter__(self):
        print("enter")

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("exit")

if __name__ == '__main__':
    with Printer():
        print("Middle")
        raise Exception("from middle")

Now I get the expected behavoir:

enter
Middle
exit

Problem 2) ContextManager is None?

But what if I want to reference the ContextManager? Python allows you to assign the object to a variable with the as variable_name syntax.

Let's try to print the ContextManager reference:

    class Printer():
        def __enter__(self):
            print("enter")

        def __exit__(self, exc_type, exc_val, exc_tb):
            print("exit")

        def __str__(self):
            return "<Printer>"

    if __name__ == '__main__':
        with Printer() as p:
            print(f"p = {p}")

Surprisingly this produces:

enter
p = None
exit

What is going on? The __enter__ and __exit__ work, but the value is somehow None? How can both be true?

Solution 2) How assignment from ContextManager works.

You would think that:

with Printer() as p

is the same as

p = Printer()
with p:

but it's not. It's actually

_p = Printer()
p = p.__enter__()

Here's the solution

class Printer():
    def __enter__(self):
        print("enter")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("exit")

    def __str__(self):
        return "<Printer>"

if __name__ == '__main__':
    with Printer() as p:
        print(f"p = {p}")

This produces the expected:

enter
p = <Printer>
exit

What's different? The __enter__ now returns self. Before it had no return, so there was an implicit return None that all python methods have as a minimum.

Wednesday, August 17, 2022

Practice vs. Competition

By Llewellyn Falco & Jacqueline Bilston

In any sport, you have practice and you have competitions. Practice helps you to improve, but what about competition? Is it just about ranking yourself against others? Or is it also a mechanism to improve?

We've done a lot of coderetreats, which are mainly about practice, but this February (2022), we did something different. We participated in a Lean Poker event, which is much more of a competition.

Everybody has a plan until they get punched in the mouth
--Mike Tyson

It turns out you can learn something from competition, but most of that learning doesn't come when you win, it comes after you lose and you reflect on what went wrong.

So what was the result? We got punched in the face (metaphorically)...

Competition Format

Lean Poker is a battle royale of poker bots. A new battle to the death commences every 30 seconds. The bots play poker until only 1 remains. The winning team gets 5 points added to their score on a global score board that determines the overall winner for the week. (The 2nd place player also gets 3 points).

Every team starts with a fully functioning bot in the language of their choice, but the only thing this bot does is fold.

Note: Lean Poker normally runs in a single day, but we decided to do it in one hour a day sessions over a 5 day week, to accommodate a remote global audience.

The Players

We had 6 teams comprised of a total of 22 players. All of them were specially invited, and the teams were balanced according to competencies, but the only two teams that matter are ours, so that's what we are going to tell you about :-)

Both of our teams chose a language where at least one team member was an expert (Ruby for one and Java for the other). Both of our teams chose to mob. Both of our teams followed most XP practices, including TDD.

... things started out the same ...

Session 1

Moments after the competition started, we felt the pressure. All of our bots were folding and losing. Most teams released their first change within 5 minutes.

After we had a bot that didn't just fold, we started to make it smarter. This involved crafting code and objects to represent our hands and help us decide on how much to bet. The bots were very basic, either betting on a good hand or folding on a bad hand.

The logic defining a good or bad hand was also rather basic. At this point our logic was pretty close, but that's where things started to diverge.

The Middle

Llewellyn's team

We decided that we needed better logic to figure out better hands. We could easily detect a pair, and 3 of a kind and 4 of a kind includes a pair so we got that for free. We thought we could differentiate ourselves by being able to detect a straight.

If you have only 5 cards ordered from low to high, a straight is easy to detect. [5, 6, 7, 8, 9] is a straight because each one is one more than the other. it's slightly harder with 7 cards [2, 5, 6, 6, 7, 8, 9], but we figured it out - eventually.

However, even though we were deploying consistently, we weren't doing better in the battles.

Then we realized that none of the games were even getting to the point of having 5 cards in play. Other bots were going all in on the initial two cards, so it was impossible to raise after the 1st round.

Everything we had written was waste.

Jacqueline's team

With a poker expert on our team, we realized the most important cards to assess were the initial 2 cards (the pocket cards). Our strategy focused on refining our bet based on these 2 cards, but we were still losing... Why?

Our algorithm seemed solid, but when we finally looked at the playbacks, we realized we were consistently being overbet. We would have a good hand and bet 80%, only to have another team bet 100%, and then we would fold.

This forced us to change our strategy to all-in or fold based on those first two cards.

When we did this, we started to improve.

The End

Llewellyn's team

We now had a bunch of code, but we weren't getting the results we wanted, and we realized we needed a better understanding of what was happening in the battles. It was late to start adding logging to our system, but it was better late than never. Sometimes our logs didn't seem to match what our code should have done, so we also started labeling each version so we could better match the production to the code.

This telemetry should have been one of the first things we did, but the other teams were already winning, and we didn't want to be left behind. Our early shortcuts ended up hurting us.

Jacqueline's team

Even in the short amount of time available we were able to write some fairly confusing code. We knew it did what we wanted, because we used TDD, but we had been skipping the refactoring step.

Everyone on the team wanted to refactor, and we didn't know why everyone else seemed to avoid it. Finally, we made time for a retrospective where we discovered that the pressure to release a better bot kept getting in the way. We decided to spend the last session cleaning the code.

Once we had finished refactoring, it became obvious and simple to improve the code. These improvements helped - we gained 30% on the competition, but by that point, it was too late.

After the Game

Neither of our teams won, but in our failures, there was a lot of learning. Some retrospectives lasted up to 6 hours after the final match. All of us agreed that we made stupid mistakes because of the pressure we were under to perform. All of us could relate to that pressure in our day-to-day jobs.

This pressure resulted in cutting corners and preferencing short term wins which created long term problems (even just 5 hours later). It also dramatically reduced the amount of experimentation and exploration.

Worst of all, we all knew better, but we still didn't act better.

Ironically, at work, it often seems that this pressure is constructed artificially in the form of deadlines to try to drive better performance.

An odd observation

One question that came up in the final retro was:

"What if 1 team was only allowed to release every 30 minutes?"

Everyone agreed this would put that team at a major disadvantage.

It's obvious in the game that this restriction would be crippling. Yet we normalize monthly deploys in the business world, and no one seems to complain.

Details

Just in case you'd like to see the details of our match

Tuesday, August 2, 2022

should_methods_be_named_like_this?

tl;dr: all method names that return a True/False setting should begin with the word is

The Problem

The readability of code is very important to me. Unfortunately English is a remarkably complex language. This leads to a lot of variations on how you can ask a True/False question.

Here are a few:

  • am I allowed to write to files
  • are files writable
  • can I write to this file
  • is this file writable
  • should file writes be allowed
  • was the file writable
  • were the files writable
  • will file writes be allowed
  • would the file allow modification

Consistency

The issue with even considering which of these is the better way of expressing this question in English is Consistency.

“A consistent experience is a better experience.” — Mark Eberman

Your API is the UX that programmers experience and a consistent UX is better, even when it's worse. Why? Because it lowers the cognitive load on a user. New things fit into your existing understanding, Things are where you expect them to be so you don't miss them and they are easy to discover. If I want to know what an object can do it is helpful to be able to ask it 3 general questions:

  1. is - What properties are True/False?
  2. get - What other properties can you give me?
  3. set - What properties can I change?

This convention has been decided decades ago. Furthermore, IDEs will often autocomplete in alphabetical order making things discoverable.

The answer and the problem

This means if you have a method that answers a True/False question, the method should begin with the word is.

But English can make this soooo ugly!
Yes
But...
... it doesn't matter :-(

Yes the present singular form of the to-be verb can make for an awkard method name, but consistency is more important.

> isThisMethodNameInTheCorrectForm()   
True

Wednesday, June 16, 2021

Hexploration



Over the past little while we've been exploring an alternative to the Game of Life with a hexagonal board. You can see the rules here. This led us to some super interesting discoveries that we'd like to share.

1. Verifying Sequences

The interesting part of the Game of Life is how it evolves over time. We wanted to write tests that could help us verify how the board changes frame to frame. You can do this by mapping time to space and creating a text file that prints out each frame one after the other, much like a comic book. Instead we decided to take advantage of the animated gif format, and play out the evolution much like a short film.

Here's an example of one of those calls displaying the initial board plus 33 additional frames:

verifySequence(board, 33, f -> board.advance())

which produces:

2. Exploratory Testing with Property-Based tests

Once we could verify sequences, we could start hunting for initial layouts that produce interesting sequences. There are lots of documented scenarios in a traditional square board (blinkers, guns and flyers, etc.), but we couldn't find any for hexagonal space. We decided to search on our own.

2A. Generating Random Boards

We took a page from property-based testing, and created a generator to randomly generate a board with a given number of live cells. We also printed out the board in a way that allowed us to capture and reproduce the ones we liked.

Here's an example of a board we reproduced.

new HexGameOfLife(_(2, 4), _(2, 6), _(1, 9), _(1, 3), _(5, 5), _(5, 1), _(3, 1))

This generated lots of results, and most of them were booooring.

2B. Filtering for "Interesting"

Taking another page from property-based testing, we created a filter to remove boring results. To do this, we had to ask

Q. What does interesting mean?
A. We can see cells in every frame.

Fortunately, coding this was quite simple.

  1. Generate a board
  2. Advance a turn, and check that cells still exist
  3. Repeat until you get to the end of the frames
  4. If you make it to the end, return, otherwise GOTO 1

With these steps, we found this board:

2C. The Blinker Search

In this process, we saw a simple blinker, but lost the board before we could capture it. We tried to get lucky again, but couldn't, so we decided to define what the properties of a blinker were, and then find one automatically.

Fortunately, coding this was quite simple.

  1. Generate a board
  2. Advance 12 turns, and check that the board is the same as the initial board.
  3. If it is, you have found something that repeats every 1,2,3,4,6, or 12 frames. Otherwise GOTO 1

In just under 300,000 randomized searches, this returned: 

If you would like to check out the full code, start here

Monday, May 3, 2021

What is an Agile Technical Coach?

What is an Agile Technical Coach? By Llewellyn Falco & Jacqueline Bilston


Note: We originally wrote this as an internal memo. It's not meant to be a complete definition, but we thought it might be helpful to others so we are sharing it here.

TL;DR: An agile technical coach writes code with programmers for the purpose of improving how the team programs. Common areas include:

  • Refactoring
  • Test Driven Development
  • DevOps
Overview

The focus of an agile technical coach is specifically on technical practices like refactoring, test driven development, and DevOps. This means that technical coaches are sitting with the programmers, writing code together. Their value is in the sustained behaviours the team continues using after they leave.

Although a coach is not a teacher (who works in practise problems), and a coach is not a consultant (whose main value is delivered while they are working with you), the roles sometimes overlap. A coach often wears the hat of a teacher in short bursts in order to enable a more productive coaching session while working with a team, and only rarely, if ever, acts as a consultant.

Agile is about being able to respond to change. There are two parts that work hand-in-hand to allow this to happen in software. One side comes from the management side of what and when to change. The other side comes from the technical side of having code that is easy to change. Technical Agile Coaches empower Agile Coaches to be able to respond in the ways they want.

Thursday, April 8, 2021

The 4 Benefits of Tests

By Llewellyn Falco & Jacqueline Bilston

I’ve found that when I'm creating tests they provide 4 categories of benefits. Having these categories helps me to see how to write better tests and helps me see what to improve when I am feeling a particular pain. These benefits tend to occur in chronological order, which is how I’m going to lay out the explanations.


Note: This article is about tests as artifacts, not testing as performance. There is another area of testing known as exploratory testing. It is a different practice and provides different benefits.

#1 Specification

What am I building?”


At the very beginning, before any code is even written, writing a test scenario can help you understand what it is you’re trying to build. These scenarios often work much better than requirements, as the specifics of a scenario will often surface hidden issues. Programming is often done between two or more parties. Sometimes it can feel that way even if you are doing it by yourself. Scenarios will surface misunderstandings where you thought you were in agreement; edge cases you weren’t considering; and logical errors before they are written.


Even if this is as far as you proceed with your tests, they will still bring value.


#2 Feedback


“Does it work?”


After you have a test scenario, you can immediately start getting feedback on whether or not it is completed. This feedback can come in many forms:


Programming by red: Compiler errors can guide you on what needs to be built next. Failing tests can guide you on what needs to be implemented differently.


Debugging / Logging / Inspecting: Running code and seeing the results is critical to understanding that you’ve built what you meant to. Just imagine if you shipped code that you never ran :-/ 


People don’t know what they want until they see what they don’t: Showing the results to a customer will bring insight into if you are building the right thing.


#3 Regression


“Does it still work?”


99 bugs in the code, 99 bugs in the code, take one down pass it around, 127 bugs in the code

Just because code worked yesterday does NOT mean it still works today. Rerunning your tests allows you to know that things still work. A suite of passing tests gives confidence that things still work. Better still, a single failing test proves that something doesn’t.


Note: In my experience, you won’t get good regression without automated tests.


#4 Granularity


Why did it break?”


Granularity can show up in many forms, but it’s always about figuring out how to fix what went wrong. I tend to see 2 main forms of it.


#1 Temporal Granularity: 

You just changed a line of code and it broke the build. That line is the problem. Maybe you just roll it back, maybe you fix it and roll it forward. Either way it’s fixed and it was easier than if you had changed 1,000 lines of code.


or...


5 people commit and the nightly build breaks, who caused it?

vs.

CI runs after each commit and Jonny's commit broke it.


or ...


The tests passed 2 minutes ago and now they are broken

vs.

The tests passed this morning and now they don't.


#2 Differential Granularity

Exception thrown’ doesn’t help anybody

Array index out of bounds’  is a little better.

Array[10] is out of bounds for length 10’ means you have an off-by-one error.

Array[-2] is out of bounds for length 10’ means you have calculation bug.

Array[two] is out of bounds for length 10’ means you have to verify user input .

Array[5] is out of bounds for length 0’ means you have to check if result came back empty.


Logs in JSON are queryable which helps you find issues faster.

Objects with useful toStrings help you understand the state of your system.

Diff tools help to find what changed between two outputs.

Giving meaningful output helps us understand the causes for failures and the potential fixes.


Saturday, November 21, 2020

The Point of 'No Return'

The Point of 'No Return'
Welcome to \new world

I believe that life is important...
...and way too much of it has been wasted on dealing with issues arising from inconsistencies between line endings.

A modest proposal:
In today's world most programs can handle the line ending \n correctly.
The 'No Return' movement seeks to make inconsistent lines ends a thing of the past.
There are 3 practices on the path to achieve this:
  1. By default never use \r\n line ending in anything that it outputs.
  2. Have a toggle to turn this off for people stuck in the past
  3. (optional) On reading, replace all '\r\n' with a '\n' on any file used by the program
But:

Don't most programs already have the ability to toggle?

Yes, but they tend to default to the version of the system. Meaning Windows perpetuates the line ending inconsistency problem.  If you have dealt with this issue (which you probably have) this is why.
Most people don't change the defaults. We need to start making inconsistent line endings a very rare exception

It's just two options, is it really such a big deal?

It shouldn't be. But I personally have spent weeks of my life dealing with this issue. If you were to total up the hours of all programmers it's lifetimes of people, and it isn't going away. Our current path is pass this problem on to the next generation. We need a path to end it.

But isn't there a reason for \r\n?

Yes, it was based on the way typewriters worked. We are still dealing with legacy from type writers.

Ok, you've convinced me, how do I signup?

Simple, copy the above logo and add it to your site, link to this article and add the 3 rules to your code base.



note: I originally wrote this in august of 2014. For some reason I never published this. I'm finally doing it in November 2020 because I'm still dealing with this issue....