Skip to content

Commit

Permalink
Added support for dict-style assignments for some edge cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshultz committed Jan 5, 2018
1 parent 7b31ab1 commit 818ebea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
17 changes: 17 additions & 0 deletions rawl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ def __getitem__(self, k):
except IndexError:
raise IndexError("Unknown index value %s" % k)

def __setitem__(self, k, v):
# If it's an int, use the int to lookup a column in the position of the
# sequence provided.
if type(k) == int:
return dict.__setitem__(self._data, self.columns[k], v)
# If it's a string, it's a dict lookup
elif type(k) == str:
return dict.__setitem__(self._data, k, v)
# Anything else and we have no idea how to handle it.
else:
int_k = None
try:
int_k = int(k)
return dict.__setitem__(self._data, self.columns[int_k], v)
except IndexError:
raise IndexError("Unknown index value %s" % k)

def __len__(self):
return len(self._data)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name = 'rawl',
version = '0.2.5b1',
version = '0.2.6b1',
description = 'An ugly raw SQL postgresql db layer',
url = 'https://github.com/mikeshultz/rawl',
author = 'Mike Shultz',
Expand Down
17 changes: 16 additions & 1 deletion tests/test_rawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,19 @@ def test_single_line_call(self, pgdb):

result = TheModel(RAWL_DSN).get(str(RAWL_ID))[0]

assert type(result) == RawlResult
assert type(result) == RawlResult

@pytest.mark.dependency(depends=['test_all', 'test_get_single_rawl'])
def test_dict_assignment(self, pgdb):
"""
This test tries to assign something to RawlResult as if it were a dict
"""

RAWL_ID = 5
NAME = 'Shoopadoop'

result = TheModel(RAWL_DSN).get(RAWL_ID)

result[0]['name'] = NAME

assert result[0].name == NAME

0 comments on commit 818ebea

Please sign in to comment.