Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am andy47 on github.
  • I am andy47 (https://keybase.io/andy47) on keybase.
  • I have a public key ASAr8XqLaXRk2O6C-l37nEJckWiUyI3rI9yMsmjPCxkxDgo

To claim this, I am signing this object:

@andy47
andy47 / refactoring_candidate.py
Last active December 19, 2015 03:58
Example code to show refactoring opportunity
class Portfolio(object):
...
def get_portfolio(self, portfolio_code):
...
stmt = """SELECT id FROM portfolios WHERE code=?"""
cursor.execute(stmt, (portfolio_code,))
portfolio_id = cursor.fetchone()[0]
...
def add_value(self, portfolio_code, ...):
...
@andy47
andy47 / parse_conn_pytest.py
Created June 29, 2013 23:52
py.test test case for our example function
from parse_conn import parse_connection
import py.test
def test_not_at():
py.test.raises(ValueError, parse_connection, 'invalid uri')
@andy47
andy47 / parse_conn_unittest.py
Last active December 19, 2015 03:49
Unittest tests for our example function.
from parse_conn import parse_connection
import unittest
class InvalidInputs(unittest.TestCase):
def testNoAt(self):
"""parse_connection should raise an exception
for an invalid connection string"""
self.assertRaises(ValueError, parse_connection, 'invalid uri')
if __name__ == "__main__":
@andy47
andy47 / parse_conn.py
Created June 29, 2013 23:41
An example function for us to write tests for
def parse_connection(connection_string):
"""Work out the user name and password in connection_string
Connection string should be of the form 'database://username@password'
"""
if '@' in connection_string:
# Username is characters between '://' and '@'
slash_position = connection_string.find('://')
at_position = connection_string.find('@')
user_name = connection_string[slash_position+3:at_position]