Tag: programming

Often we need to read from, or write to, files in directories that are local to our script. For instance, we could use the below code to save to file all possible values that can be stored in one byte: my_bytes = bytearray(list(range(0,256))) with open("output/local.bin", 'wb') as f: f.write(my_bytes) Our script, local_file.py, is located in the following directory: /home/python [wintermute@hive python]$ cd /home/python [wintermute@hive python]$ ls local_file.py output Now, let's say we launch this script from within its location: [wintermute@hive python]...

Continue reading »

Have you ever had a need to iterate over two lists at the same time? I did recently and it turns out that Python has a built-in function called zip() that can be used for this. This function takes iterables as arguments, such as lists or strings, and returns a list of tuples containing elements from each of the arguments. When using it in our code we can mix types of arguments, they don't have to be the same. In the examples below we will use lists of characters, digits, and tuples. For good measure, we will throw in a...

Continue reading »

I recently had a need to read values from a file and insert them into a dictionary of dictionaries. This could be useful for having, say, dictionary with hostnames of some devices as keys and values being dictionaries with data collected from each of the devices. To achieve that I created a helper dictionary which had data for each device that I read from a file. Then I would assign a copy of a helper dictionary to the key equal to the name of the device for which I collected data. My code was functionally identical to the below snippet:...

Continue reading »