Skip to content

Commit dffbcf4

Browse files
committed
From the Pycon Sprint!!
1 parent 96593dc commit dffbcf4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class SO(object):
2+
3+
def __init__(self,**kwargs):
4+
self.base_url = kwargs.pop('base_url',[]) or 'http://api.stackoverflow.com/1.1'
5+
self.uriparts = kwargs.pop('uriparts',[])
6+
for k,v in kwargs.items():
7+
setattr(self,k,v)
8+
9+
def __getattr__(self,key):
10+
self.uriparts.append(key)
11+
return self.__class__(**self.__dict__)
12+
13+
def __getitem__(self,key):
14+
return self.__getattr__(key)
15+
16+
def __call__(self,**kwargs):
17+
call_url = "%s/%s"%(self.base_url,"/".join(self.uriparts))
18+
self.uriparts = []
19+
return call_url
20+
21+
if __name__ == '__main__':
22+
print SO().abc.mno.ghi.jkl()
23+
print SO().abc.mno['ghi'].jkl()
24+
user1 = SO().users['55562']
25+
print user1.questions.unanswered()
26+
print user1.questions.answered()

0 commit comments

Comments
 (0)