python •  •

# Binary flags with Python

Sometimes you just want to run a few specific sections of your python code for testing. Then a few more. Next, all sections. Now run just one. Or possibly need to run different parts of your code based on the time of day or week.

There are many ways you can accomplish this directly in your python script(s) or by using the python window. But what about passing a runtime argument with a little binary?! This technique is a great way to run different/multiple sections (functions) of your code without the need to modify the python script - binary flags with python. I even use this technique to keep some test functions to call them when needed - and also to run minor variations based on time of day.

The method behind this approach is to use binary (base 2 numbers), but not using a long string of 0’s and 1’s. Rather, converting regular long integers (base 10) into binary in order to determine what sections of code to run.

## Setup

You have 7 sections of code broken down into functions. Each section is assigned a value - these values are set to the power of 2 (assign the number on the far right side), examples:

• Section 0 = 2^0 = 1
• Section 1 = 2^1 = 2
• Section 2 = 2^2 = 4
• Section 3 = 2^3 = 8
• Section 4 = 2^4 = 18
• Section 5 = 2^5 = 32
• Section 6 = 2^6 = 64
• and so on…

## Usage

Whenever you want to call multiple sections, just add up the values of each section and pass as a runtime argument.

• Run section 3 only: MyScript.py 8
• Run section 4 only: MyScript.py 18
• Run section 1, 3, and 5 (2+8+32): MyScript.py 42

Since you are passing the integer (base 10) number as the argument, we need add a small function to the python script to determine the sections to run. The functions/sections will always be run from lowest to highest (highest to lowest is possible with a small modification to the script).

## The code

Using this small function, we can determine which section(s) to run.

``````def fn_bits(n):
while n:
b = n & (~n+1)
yield b
n ^= b``````

## Putting it all together

Now that we can pass the commands we want to run as a single argument, we need to put it all together. Here is a working sample of binary flags with python.

 # Name: BitCode/BitFlag Python Example # Author: Bryan McIntosh ###################################################################### # Set bitCode/Flag to run various/multiple sections of code. # Add the numbers together for each section to run. # 1: Call section 0 - example: Clean temp directory # 2: Call section 1 - example: Run FTP Download # 4: Call section 2 - example: Analyze FTP files and clean # 8: Call section 3 - example: Say hi ###################################################################### ##fn_bits: Calculate binary (base2) numbers from integer (base10) def fn_bits(n): while n: b = n & (~n+1) yield b n ^= b #End function fn_bits bitRunCode = 9 #This is default, Runs 1 and 8 (1+8=9). Providing runtime argument will override if len(sys.argv) == 2: try: bitRunCode = int(sys.argv) except: print 'Invalid runtime argument, exiting program without running' sys.exit() for b in fn_bits(bitRunCode): if b == 1: print 'Call section 0: Clean directory' elif b == 2: print '\n Call section 1: FTP' elif b == 4: print '\n Call section 2: Analyze' elif b == 8: print '\n Call section 3: Hi! else: print 'bitRunCode not in range - but you can always add more.' #FIN

Based on the full code sample above, here is how we would call different code combinations:

• Section 0 only: `BitFlagFunctions.py 1`
• Section 2 and 3 (4+8): `BitFlagFunctions.py 12`
• Section 0, 2, and 3 (1+4+8): `BitFlagFunctions.py 13`
• Section 1 and 3 (2+8): `BitFlagFunctions.py 10`

## Summary

This is just one of many ways you can setup binary flags with python. I’ve seen other examples where people use “0100101” to run various sections which also works well. The method described in the above gist is just a way that works well for me, giving me the ability to number each section and run them in ascending order as needed.

If you found my writing entertaining or useful and want to say thanks, you can always buy me a coffee. 