Skip to content

Instantly share code, notes, and snippets.

@Ma233
Last active April 7, 2016 10:27
Show Gist options
  • Save Ma233/ad40b78ded79ae46a7c2271b04d711eb to your computer and use it in GitHub Desktop.
Save Ma233/ad40b78ded79ae46a7c2271b04d711eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
from functools import partial
import requests
class URLAgent(object):
def __init__(self, host, handler=None, actions=set()):
self._host = host
self._url = host
self._handler = handler
self._actions = actions
def __getattr__(self, attr):
if attr in self._actions:
action_func = getattr(self._handler, attr)
return partial(action_func, self())
seperator = '' if self._url.endswith('/') else '/'
furl = seperator.join(['{prefix}', '{section}'])
self._url = furl.format(prefix=self._url, section=attr)
return self
# for those who cannot use `.` operation
_ = __getattr__
def __call__(self):
rst, self._url = self._url, self._host
return rst
if __name__ == '__main__':
HOST = 'http://douban.com/'
douban = URLAgent(HOST, handler=requests, actions={'get'})
print douban.group()
print douban.group.get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment