Python Protobuf 'syntax' Error

08/20/2017

A common error somebody may encounter using Python protobufs on common Linux distros is:

TypeError: __init__() got an unexpected keyword argument 'syntax'

This is most likely due to the fact that a lot of OSs have an outdated version of the Python protobuf package. Typically a user may acquire protobuf tools from the OS package manager like

sudo apt-get install protobuf-compiler
sudo apt-get install python-protobuf

The protobuf compiler will compile a version of the protobuf which has a syntax the python-protobuf, the package that allows you to do import google.protobuf, doesn’t understand. Specifically, all objects will be passed a syntax field to their __init__ function. Update Python protobuf by visiting the repo and getting the latest version.

Ducktyping + Python Protobuf

08/18/2017

Given a compiled Python protobuf as follows:

message Person {
    optional string email = 1;
}

Don’t use duck typing to see if an optional field has been set!

Though the value is known to be unset, accessing the value always returns a default value! (Source. See optional field description.)

import Person_pb2

person = Person()

if person.email:
    print('Oh wow you totally have an email!')
    print('It is: '.format(person.email))
else:
    print('I got nothing')

> Oh wow you totally have an email!

> It is:

Use the compiled protobuf’s data member functions to check existance:

if person.HasField('email'):
    # do stuff

It’s not very clear you need to do it like this…

REST Example

08/17/2017

Not RESTful

REST interface: “Do something and let me know when you’re done.”

Resource: “K.”

Resource: “Hey, I’m done.”

REST interface: “K.”

RESTful

REST interface: “Do something.”

Resource: “K.”

REST interface: “Are you done?”

Resource: “No.”

REST interface: “Are you done?”

Resource: “No.”

REST interface: “Are you done?”

Resource: “No.”

REST interface: “Are you done?”

Resource: “No.”

Portrait Mode

07/10/2017

Portrait Mode

(#1) Programming is like...

06/30/2017

To be garbage collected player

Garbage collection is like if in basketball, coaches can substitute players by simply introducing the new player into play but the replaced player can remain on the court. You assume they won’t move or touch the ball or interfere with other players. And then every 5 minutes, the referees yell “FREEZE!” to check every player and sit out the ones that were substituted.

Active players