Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from urllib.parse import urlencode
- from urllib.request import urlopen
- class WebovaMetoda:
- def __init__(self,url,params):
- self._url = url
- self._params = params
- #na to aby bol object callable musi mat metodu call
- def __call__(self, *args,**kwargs):
- if len(args)+len(kwargs) != len(self._params):
- raise TypeError("nespravny pocet parametrov")
- parametre = dict(zip(self._params, args))
- if set(parametre) & set(kwargs):
- raise TypeError("Dva krat specifikujes ten isty parameter")
- if set(kwargs) - set(self._params):
- raise TypeError("Neznamy parameter")
- parametre.update(kwargs)
- url =self._url + '?' + urlencode(parametre)
- with urlopen(url) as data:
- return json.load(data)
- class WebovaSluzba:
- def __init__(self,url):
- self.url = url
- with urlopen(url) as dokument:
- popis = (json.load(dokument)) #zoznam v ktorm je dictionary
- for metoda in popis:
- setattr(self,metoda['name'], WebovaMetoda(metoda['url'],metoda['params'])) #vyhladavanie v dictionary cez []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement