Permalink
Browse files

Code cleanup

  • Loading branch information...
1 parent e33f38e commit 91592549aed93897b85b982577619e87968876fe @johnfn johnfn committed Apr 17, 2011
Showing with 20 additions and 27 deletions.
  1. +3 −4 block_conversion_api.py
  2. +17 −23 generic_api.py
View
@@ -2,13 +2,12 @@
# Simple Python wrapper around the Block Conversion API provided by the FCC.
-class BlockConversionAPI(GenericLatLongAPI):
+class BlockConversionAPI(GenericAPI):
def __init__(self):
- GenericLatLongAPI.__init__(self, "http://data.fcc.gov/api/block/find")
-
+ GenericAPI.__init__(self, [("get_block", "http://data.fcc.gov/api/block/find")])
# Sample use of BlockConversionAPI
if __name__ == "__main__":
bc = BlockConversionAPI()
- print bc.request(lat=41, long=-87) # (Should be San Francisco)
+ print bc.get_block(lat=41, long=-87) # (Should be San Francisco)
View
@@ -10,13 +10,13 @@ def __str__(self):
return "The returned JSON was invalid."
-# Inheritable class to perform requests to generic APIs.
+# Perform requests to generic APIs.
class BaseAPIRequest:
def __init__(self, url):
self.url = url
- #Inherit this method to describe how to format URLs.
+ # Formats a URL with the provided keyword arguments.
def format_url(self, **args):
if args is None: raise NoArgumentsException
@@ -36,51 +36,45 @@ def request(self, **args):
if t.startswith("callback("):
t=t[t.index("(")+1:-1]
- return json.loads(t)
-
- """
try:
-
+ return json.loads(t)
+
except:
raise BadJSONException
return None
- """
-
- #object = json.loads("".join([l for l in urllib.urlopen(self.formatted_url)]))
class GenericAPI:
+
+ # __init__
+ # Parameters: APIS, a list of tuples of form (FUNCTIONNAME, LINK).
+ #
+ # Creates functions of name FUNCTIONNAME that perform an API call to LINK
+ # when called, giving back the response as JSON.
+ #
+ # Returns: Nothing
+
def __init__(self, apis):
self.api_objects = []
self.api_functions = []
self.functions = []
- #Who writes normal functions when you can use CLOSURES?!?
+ # Who writes normal functions when you can use CLOSURES?!?
number = 0
+ # Bind each function to the class.
for api in apis:
self.api_objects.append(BaseAPIRequest(api[1]))
self.bind_closure(number)
setattr(self, api[0], self.api_functions[number])
number += 1
+ # Creates a function on the current class.
def bind_closure(self, number):
def generic_api_call(**kwargs):
self.api_objects[number].format_url(**kwargs)
return self.api_objects[number].request(**kwargs)
- self.api_functions.append(generic_api_call)
-
-
-
-# Wraps any API that just uses Latitude and Longitude.
-
-#TODO: This is no longer necessary...
-class GenericLatLongAPI(BaseAPIRequest):
- def __init__(self, url):
- self.url = url
-
- def request(self, **args):
- return BaseAPIRequest.request(self, **args)
+ self.api_functions.append(generic_api_call)

0 comments on commit 9159254

Please sign in to comment.