a few things I learned in 3 hour python hacking

I don’t usually use python. My daily language is C#, so it was a nice, refreshing time to code in python.

A couple of weeks ago, I wrote a small plug in with Emily, that checks the disk free space in servers where rabbit-mq is installed. We had failing tests because rabbit-mq mal-functioned due to the storage shortage. Writing in python was a very nice experience and I learned several pythony things, the way python deals with problem. We finished coding and proudly deployed the plug-in, thinking it’s done now. You know the fulfilling emotion when you deploy your stuff.

After a day or two, Richard came back saying Watchdog, our service monitoring tool, crashed, because there was a moment those network drives were not available and it crashed the plugin, and the crashed plugin actually stopped the website. Again, developers were only good at happy paths in testing.

The fix was simple. I just added “try … except”. Also added a couple of unit tests to test the scenario when the network drive is not available. But again, I learned several things doing it. Nothing deep, but just a list of surprises a python newbie had during the hack.

Module and class are two different things.

A module is a “a file containing Python definitions and statements”. In my unittest, in order to use a class, I had to reference module and then the class name, like poll_services.ServiceChecker()

import sys
import poll_services
import unittest

class TestDiskFreeSpace(unittest.TestCase):
    ''' test disk free space features '''

    def test_when_the_network_drive_is_available(self):
        sc = poll_services.ServiceChecker()
        diskSpace = sc.get_free_disk_space_in_gb('localhost', 'C$')

        self.assertTrue(diskSpace > 0)

Importing files from different folder isn’t supported out of the box

I used sys.path.append(‘.\sentinels’). It’s a shame that I can’t reference it more gracefully.

Unit-test runs itself

.NET, you have a test-runner like NUnit. with python, unittest is a included package, so you have run tests in the test module.

if __name__ == '__main__':

Don’t pass self to a method

I have a method “def get_free_disk_space_in_gb(self, machine, drive):” but when you call the method you do like “self.get_free_disk_space_in_gb(machine, rabbit_drive)”. I keep forgetting that I don’t pass self. It’s like C#’s extension method.

Private methods has __ prefix in the method name. Initially, I thought it’s just a convention and presummed that I can access those methods in my unit tests. It turned out that methods with __ translate self to the calling class. So, it ended up doing something like “TestDiskFreeSpace.__get_free_disk_space_in_gb(…)” and it errored that the module doesn’t have that attribute. It was interesting. I made the method public for test’s sake for now.

Clearing all keys in redis

You can do “flushdb” or “flushall” for all databases. Handy commands.

It can go on, but should be enough for now.

a few things I learned in 3 hour python hacking

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s