Friday, June 28, 2013

Python assertRaises exception inside generator

So I have a generator and I want to make sure it throws an exception after a certain number of calls. So expected behaviour is this:
def generate():
    yield 1
    yield 2
    raise Exception()
How do you check that in a test? You can't do this:
class testExample(unittest.TestCase):
    def test_generatorExample(self):
        self.assertRaises(Exception, a)
since a generator isn't callable. Turns out that assertRaises is a context manager as of python 2.7, which is very cool:
class testExample(unittest.TestCase):
    def test_generatorExample(self):
        with self.assertRaises(Exception):
Or for earlier versions of python you could use a lambda:
self.assertRaises(Exception, lambda: list(a))

Thursday, June 27, 2013

Continuously loop (cycle) through a list in python

Thank you itertools:
In [7]: a=[1,2,3]

In [8]: licycle = itertools.cycle(a)

In [9]:
Out[9]: 1

In [10]:
Out[10]: 2

In [11]:
Out[11]: 3

In [12]:
Out[12]: 1

In [13]:
Out[13]: 2

In [14]:
Out[14]: 3

In [15]:
Out[15]: 1

In [16]:
Out[16]: 2

In [17]:
Out[17]: 3

Wednesday, June 12, 2013

Disable Java web applet in Internet Explorer

Microsoft has joined the good fight to make disabling the vulnerability-ridden Java web plugin easier. They have released a fixit that blocks the Java ActiveX DLLs from loading and disables the JNLP handler, which effectively disables Java in IE.

Sunday, June 2, 2013

SQL join syntax

Jeff Atwood posted a useful memory aid for SQL join syntax, although as many commenters point out there are a number of caveats to using set theory for this task.