Skip to content

Commit 31caee8

Browse files
author
Joel Becker
committed
Added and tested log.add_attribute method for adding new properties to log records.
1 parent c6e4434 commit 31caee8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

gitnet/gitnet_tests/test_log.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ def test_commit_log_values(self):
102102
# Delete the temporary .git folder
103103
sub.call(["rm", "-rf", ".git"])
104104

105+
class AddAttributeTest(unittest.TestCase):
106+
"""
107+
Tests for the add_attribute method.
108+
"""
109+
def setUp(self):
110+
data = {"Bob": {"author": 'Bob',
111+
"email": 'bob@gmail.com',
112+
"type": 'author',
113+
"loc": 'Waterloo',
114+
"books": ['BookA', 'BookB']},
115+
"Bobby": {"author": 'Bobby',
116+
"email": 'bobby@gmail.com',
117+
"type": 'author',
118+
"loc": 'Kitchener',
119+
"books": ['BookC', 'BookD']},
120+
"Robert": {"author": 'Robert',
121+
"email": 'robby@gmail.com',
122+
"type": 'author',
123+
"loc": 'Kitchener',
124+
"books": ['BookC', 'BookD']}}
125+
self.log = gitnet.Log(data)
126+
127+
def test_new_attr_1(self):
128+
new_log = self.log.add_attribute("letter", lambda d: d["author"][0])
129+
self.assertEqual(new_log["Bob"]["letter"], "B")
130+
self.assertEqual(new_log["Bobby"]["letter"], "B")
131+
self.assertEqual(new_log["Robert"]["letter"], "R")
132+
133+
def test_new_attr_1(self):
134+
new_log = self.log.add_attribute("edge",lambda d: gitnet.net_edges_simple("v1","v2",d,["loc"]))
135+
self.assertEqual(new_log["Bob"]["edge"], ("v1","v2",{"loc":"Waterloo"}))
136+
self.assertEqual(new_log["Bobby"]["edge"], ("v1","v2",{"loc":"Kitchener"}))
137+
self.assertEqual(new_log["Robert"]["edge"], ("v1","v2",{"loc":"Kitchener"}))
105138

106139
class DescribeTests(unittest.TestCase):
107140
def setUp(self):

gitnet/log.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ def attributes(self):
136136
attr_list = attr_list + sorted(list(attr))
137137
return attr_list
138138

139+
def add_attribute(self,name,helper):
140+
"""
141+
Creates a new record attribute.
142+
143+
**Parameters** :
144+
145+
> *name* : `string`
146+
147+
>> The name of the new attribute.
148+
149+
> *helper* : `None`
150+
151+
>> A helper function, which takes an attribute dict and produces the new attribute.
152+
153+
**Return** :
154+
155+
> A new Log object, identical to self but with the desired attribute.
156+
"""
157+
self_copy = copy.deepcopy(self)
158+
for n in self_copy:
159+
self_copy[n][name] = helper(self_copy[n])
160+
return self_copy
161+
139162
def author_email_list(self):
140163
"""
141164
Gathers each unique author email combination from the log, and then prints them in a list.

0 commit comments

Comments
 (0)