Binary namedtuples in Python

Many systems have a need to efficiently manage a lot of boolean values associated with users or other objects. Just think about all of those different notification settings many applications offer. Services that use OAuth2 to control access to their APIs may have a lot of different scopes that limit access and refresh tokens. Some systems may also have fine grained permissions associated with different user accounts. All of these scenarios can require these values to be readily available for active users or API clients. You'd like to be able to efficiently cache/store these things but also easily work with them at runtime. Many approaches often use bitwise operators or bit masks to track these settings but that can often lead to code nobody wants to touch as the number of values grows large. A container datatype often overlooked for things like this in Python is the namedtuple:

"Named tuple instances do not have per-instance dictionaries, so they are lightweight and require no more memory than regular tuples."

— from https://docs.python.org/3.4/library/collections.html#collections.namedtuple

A named tuple is a regular Python class, so you can easily subclass it and attach additional functionality. So we can have a named tuple that allows us to easily access boolean settings/values via attributes that also knows how to represent itself in a binary form. This makes it easy to work with in code and also allows for efficient caching along with a user session or API access token. Consider this simple example:

A new BinaryNamedTuple can be initialized with a list of attributes that should be set to True. All other defined attributes will be initialized as False. If using BinaryNamedTuple within an OAuth2 implementation, this might be the list of granted scope names. The named tuple also provides a nice repr when working with them interactively or during debugging sessions.

Anytime settings need to be updated and cached, just make a new BinaryNamedTuple and use the to_binary method to store a binary representation of it.

When you need to switch back to named tuple form, just use from_binary to quickly get back to a more manageable form:

Attribute values are easily accessed and can be used during a web/API request cycle as needed to enforce rules, data access requirements or pretty much anything else. They're right at home inside a decorator protecting your Flask views.

Another nice benefit of using named tuples is that, like tuples, they're immutable. So you can avoid accidents where values might be changed outside of the normal process and bugs are introduced.

...and just in case you were wondering about that part where “named tuples don't take up any more space than a regular tuple”:

The next time you're faced with efficiently managing a lot of boolean values for user settings or an API token, don't forget about your friend the (binary) namedtuple.

About Brian Corbin

Brian Corbin is Engineer #1 at PokitDok. He's currently focused on making complex transactions as simple as possible via modern APIs.

Visit My Website
View All Posts

Leave a Reply

Your email address will not be published.