Dec. 4th, 2016

elfs: (Default)
Last night Raen and I sat down to watch The Peacemaker, a Clinton-era action thriller about a nuclear weapon heist. It stars Nicole Kidman as the civilian director of the White House Nuclear Response Team, and George Clooney as an action-oriented Army colonel in Defense Intelligence who specializes in tracking down dealers in stolen chemical weapons. When a warhead being transported by train through Russia for dismantling blows up unexpectedly, these two work together to realize that it was a heist, and they have to find the remaining warheads that were on the train.

Everything about that film is still relevant twenty years later. The film opens with a dialogue between a Russian sergeant and his lieutenant: "I didn't sign up for the Russian Army to help the Americans take it apart." The humiliation of the Russian state is a huge driver of one major plot, and The Peacemaker muses on how Russia is descending into a kleptocratic state and how Russian organized crime is the only group that really seems to have its act together.

There are spoilers ahead, but it's a fairly old film.

At one point, the villians are trying to escape by driving through Azerbijan, but the roads are flooded with refugees from the Armenia/Azerbijani war. Those two countries are still at war with each other today. The Russians like this state of affairs: they get to sell arms to both sides, and since Armenia borders both Georgia and Turkey, they get to distract the West a lot with this bullshit.

There are two villians in the film: The one who performs the nuke-stealing operation, and the one who finances it. The latter is a survivor of the Bosnia/Croatia/Serbia War from '92 through '94, and who intends to wreak vengeance on the United Nations for their part in the war. His "terrorist speech" could have been written by a Syrian, the entirety of it an angry rejoinder to great forces meddling in affairs that are not theirs to meddle.

And during one scene where the heist villains are driving through Russia, we get a tense but precise dialogue about civilian control. Clooney's character honors civilian control, in the guise of Kidman, throughout the film, but his pressure on her to "get authorization or those nukes will end up in Iran!" is insistent, and her reaction to it is strong. Today, we're debating whether or not a retired Marine general should head the Department of Defense, and the issue of whether or not someone whose whole life has been shooting people should be in charge of sending others out to shoot other people is super-relevant.

It's a very political film without ever once really mentioning politics. It's a shame that Mimi Leder has become better known for her work on TV than movies. Yes, her other major film, Deep Impact was a terrible movie about good people making one stupid decision after another, and it deserved to die. But The Peacemaker is a damn fine action film and deserves a second look.
elfs: (Default)

I’ve discovered MyPy at work, and it works with Python 2.7 so amazingly well that I’m here to say, if you haven’t added it to your workflow, you need to immediately.


The last project I worked on at my day job, an automated inventory and services manager, was 2200 lines of one class object. When I was done, it was 1800 lines and 145 functions split over ten files, with separate classes for services and inventory, intake and output, local and network transformation, authorization, and utility. It’s primary purpose was to take in CSV (comma separated values– the text format for spreadsheets) and output JSON. We wanted to add other input formats, we wanted the output to be able to handle different formats as well, and we wanted to move the network transformation phase to an asynchronous handler with recovery-on-failure.


Doing that took a lot of heavy lifting. Figuring out what functions were for, what the dataflow looked like, what each step of the transformation required, how to handle conflicts with objects already in the inventory. Like most shops, we have rules about how to use docstrings that look a lot like jDoc, but by complete luck I found PEP-484 and MyPy the program that actually does Python type-hint checking, and immediately started using it aggressively.


The internals of the original, a one-off written for a single customer, had very strong assumptions about CSV inbound and JSON outbound. Functions were literally named with csv_ and json_ prefixes, even though internally everything was just Python dictionaries and lists!


I named things, and by their purpose: Classes called InventoryItem and TrackingService. I named collections of things. I named containers. And I added MyPy type hints to everything, like this (Code is from a personal project, mp_suggest, an early MP3 inventory manager I wrote a while ago.):


def make_album_deriver(opts, found, likely):
    # type: (Dict[Text, Text], Text, Text) -> Text
    if 'album' in opts:
        return opts[u'album']

    if 'usedir' in opts:
        return found

        return (ascii_or_nothing(likely) or sfix(found))

That little one-line comment there guarantees that anyone calling this function must send it what’s listed in the type line, and nothing else. MyPy will track back and make sure that the variables passed by the caller are actually created as a dictionary, a string, and a string! It will track forward and make sure that the receiver always uses the results as a string! It will ensure that the dictionary is created with strings as keys and values— no lists, no sets, no nested dictionaries are legal values. It will make sure that ascii_or_nothing and sfix take strings and return strings.


Programmers in dynamic, duck-typed languages like Python (and Javascript, Perl, Ruby, even Lisp) tend to assume we know what we’re doing. Given a problem, we come up with a solution, and then we start coding toward the solution, as if building and assembling a jigsaw puzzle along the way. On a very big project, we can miss things. Often, what we miss is error handling and corner cases. We can write a function and twenty minutes later call it, all the while assuming we can remember correctly what the calling protocol was. Often, we are wrong, and learning we’re wrong is sometimes a painstaking effort in type management. I believe half my debugging time is being a human typechecker: “Wait, it’s supposed to be passing back class X, but I’m getting a list. Why?”


PEP-484 eliminates all of that, and that makes PEP-484 the biggest leap forward in high-quality Python development since PEP-8. By eliminating the single most common class of running errors in all of Python, you can double your productivity. If you are not using MyPy, you are at a tactical disadvantage, and you will remain so until you adopt it.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 4th, 2026 01:31 pm
Powered by Dreamwidth Studios