Skip to content
This repository
Browse code

ohhhh welllll

  • Loading branch information...
commit b8dcf827188a5719aa6c31786552311cf30b7b5f 1 parent d9c24e1
quadrismegistus authored

Showing 36 changed files with 974 additions and 0 deletions. Show diff stats Hide diff stats

  1. +45 0 build/lib.linux-x86_64-2.6/python-fcc/FRNConversionsAPI.py
  2. 0  build/lib.linux-x86_64-2.6/python-fcc/__init__.py
  3. +13 0 build/lib.linux-x86_64-2.6/python-fcc/block_conversion_api.py
  4. +15 0 build/lib.linux-x86_64-2.6/python-fcc/broadband_api.py
  5. +130 0 build/lib.linux-x86_64-2.6/python-fcc/generic_api.py
  6. +22 0 build/lib.linux-x86_64-2.6/python-fcc/license_view_api.py
  7. +33 0 build/lib.linux-x86_64-2.6/python-fcc/sba_businesses_api.py
  8. +35 0 build/lib.linux-x86_64-2.6/python-fcc/sba_loans_grants_api.py
  9. 0  build/lib.linux-x86_64-2.6/python-fcc/tests/__init__.py
  10. +5 0 build/lib.linux-x86_64-2.6/python-fcc/tests/__main__.py
  11. +105 0 build/lib.linux-x86_64-2.6/python-fcc/tests/testcases.py
  12. BIN  dist/python_fcc-0.1-py2.6.egg
  13. +45 0 python-fcc/FRNConversionsAPI.py
  14. 0  python-fcc/__init__.py
  15. +13 0 python-fcc/block_conversion_api.py
  16. +15 0 python-fcc/broadband_api.py
  17. 0  python-fcc/build/lib.linux-x86_64-2.6/tests/__init__.py
  18. +5 0 python-fcc/build/lib.linux-x86_64-2.6/tests/__main__.py
  19. +105 0 python-fcc/build/lib.linux-x86_64-2.6/tests/testcases.py
  20. BIN  python-fcc/dist/python_fcc-0.1-py2.6.egg
  21. +130 0 python-fcc/generic_api.py
  22. +22 0 python-fcc/license_view_api.py
  23. +10 0 python-fcc/python_fcc.egg-info/PKG-INFO
  24. +8 0 python-fcc/python_fcc.egg-info/SOURCES.txt
  25. +1 0  python-fcc/python_fcc.egg-info/dependency_links.txt
  26. +1 0  python-fcc/python_fcc.egg-info/top_level.txt
  27. +33 0 python-fcc/sba_businesses_api.py
  28. +35 0 python-fcc/sba_loans_grants_api.py
  29. 0  python-fcc/tests/__init__.py
  30. +5 0 python-fcc/tests/__main__.py
  31. +105 0 python-fcc/tests/testcases.py
  32. +10 0 python_fcc.egg-info/PKG-INFO
  33. +16 0 python_fcc.egg-info/SOURCES.txt
  34. +1 0  python_fcc.egg-info/dependency_links.txt
  35. +1 0  python_fcc.egg-info/top_level.txt
  36. +10 0 setup.py
45 build/lib.linux-x86_64-2.6/python-fcc/FRNConversionsAPI.py
... ... @@ -0,0 +1,45 @@
  1 +from generic_api import *
  2 +import urllib,json
  3 +
  4 +
  5 +# Simple Python wrapper around the Broadband API provided by the FCC.
  6 +
  7 +class FRNConversionsAPI(GenericAPI):
  8 + """
  9 + FRNConversionsAPI:
  10 + - an API to access information about broadband provides.
  11 + - original API: http://reboot.fcc.gov/developer/frn-conversions-api
  12 +
  13 + - methods:
  14 + -- getList(stateCode, multi)
  15 + takes stateCode=IL or stateCode=17 (FIPS code),
  16 + (optional) multi=Yes or multi=No to signify whether to include FRNs also operating outside this state.
  17 +
  18 + -- getInfo(frn)
  19 + takes frn='0017855545' (FRN id)
  20 + """
  21 +
  22 +
  23 + def __init__(self):
  24 + apis= [
  25 + ('getList', 'http://data.fcc.gov/api/frn/getList'),
  26 + ('getInfo', 'http://data.fcc.gov/api/frn/getInfo')]
  27 +
  28 + GenericAPI.__init__(self,apis)
  29 +
  30 +
  31 +if __name__ == "__main__":
  32 + bb = FRNConversionsAPI()
  33 +
  34 + x=bb.getList(stateCode='IL')
  35 + print type(x)
  36 + print len(x)
  37 + print x.keys()
  38 +
  39 + #print x
  40 +
  41 + print
  42 + x=bb.getInfo(frn='0017855545')
  43 + print type(x)
  44 + print len(x)
  45 + print x
0  build/lib.linux-x86_64-2.6/python-fcc/__init__.py
No changes.
13 build/lib.linux-x86_64-2.6/python-fcc/block_conversion_api.py
... ... @@ -0,0 +1,13 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the Block Conversion API provided by the FCC.
  4 +
  5 +class BlockConversionAPI(GenericAPI):
  6 + def __init__(self):
  7 + GenericAPI.__init__(self, [("get_block", "http://data.fcc.gov/api/block/find")])
  8 +
  9 +
  10 +# Sample use of BlockConversionAPI
  11 +if __name__ == "__main__":
  12 + bc = BlockConversionAPI()
  13 + print bc.get_block(lat=41, long=-87) # (Should be San Francisco)
15 build/lib.linux-x86_64-2.6/python-fcc/broadband_api.py
... ... @@ -0,0 +1,15 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the Broadband API provided by the FCC.
  4 +
  5 +APIS = [("get_data", "http://data.fcc.gov/api/speedtest/find")]
  6 +
  7 +class BroadbandApi(GenericAPI):
  8 + def __init__(self):
  9 + GenericAPI.__init__(self, APIS)
  10 +
  11 +
  12 +# Sample use of BroadbandApi
  13 +if __name__ == "__main__":
  14 + bb = BroadbandApi()
  15 + print bb.get_data(latitude=37, longitude=-122) # (Should be San Francisco)
130 build/lib.linux-x86_64-2.6/python-fcc/generic_api.py
... ... @@ -0,0 +1,130 @@
  1 +import urllib
  2 +import json
  3 +
  4 +class NoArgumentsException(Exception):
  5 + def __str__(self):
  6 + return "No arguments were found."
  7 +
  8 +class BadJSONException(Exception):
  9 + def __str__(self):
  10 + return "The returned JSON was invalid."
  11 +
  12 +class NonexistentStyleException(Exception):
  13 + def __str__(self):
  14 + return "The requested API style was invalid."
  15 +
  16 +class MustBeOrderedException(Exception):
  17 + def __str__(self):
  18 + return "The arguments you pass to the SBA API must be ordered."
  19 +
  20 +
  21 +# Perform requests to generic APIs.
  22 +
  23 +PHP_STYLE = 0 # &x=1&y=2
  24 +SBA_WEIRD_STYLE = 1
  25 +
  26 +class BaseAPIRequest:
  27 + def __init__(self, url):
  28 + self.url = url
  29 +
  30 + if self.url.startswith("http://api.sba.gov") or self.url.startswith("api.sba.gov"):
  31 + self.api_style = SBA_WEIRD_STYLE
  32 + else:
  33 + self.api_style = PHP_STYLE
  34 +
  35 + def __format_url_php_style(self, *ordered_args, **args):
  36 + """
  37 + Format the url with the arguments in PHP style. That is,
  38 + ?x=5&y=6
  39 + """
  40 + if args is None: raise NoArgumentsException
  41 +
  42 + args["format"] = "json" #Ask for JSON formatted response
  43 + append = ""
  44 +
  45 + for arg in args: append += str(arg) + "=" + str(args[arg]) + "&"
  46 +
  47 + append = append[:-1] #Take off last '&'
  48 + self.formatted_url = self.url + "?" + append
  49 +
  50 + def __format_url_weird_style(self, *ordered_args, **args):
  51 + """
  52 + Format the url with the arguments in the weird style given by SBA.
  53 + arg1/arg2/arg3.json
  54 + """
  55 +
  56 + append = ""
  57 + for arg in ordered_args: append += str(arg) + "/"
  58 + append = append[:-1] #Take off last '/'
  59 +
  60 +
  61 + self.formatted_url = self.url + append + ".json"
  62 +
  63 + # Formats a URL with the provided keyword arguments.
  64 + def format_url(self, *ordered_args, **args):
  65 + if self.api_style == PHP_STYLE:
  66 + self.__format_url_php_style(*ordered_args, **args)
  67 + elif self.api_style == SBA_WEIRD_STYLE:
  68 + print ordered_args
  69 + print args
  70 + if len(args) != 0:
  71 + raise MustBeOrderedException # MUST be ordered.
  72 +
  73 + self.__format_url_weird_style(*ordered_args)
  74 + else:
  75 + raise NonexistentStyleException
  76 +
  77 + # Requests the API and returns the JSON object.
  78 + def request(self, *ordered_args, **args):
  79 + self.format_url(*ordered_args, **args)
  80 + #print self.formatted_url
  81 + t = urllib.urlopen(self.formatted_url).read().strip()
  82 + if t.startswith("callback("):
  83 + t=t[t.index("(")+1:-1]
  84 +
  85 + print self.formatted_url
  86 +
  87 + try:
  88 + return json.loads(t)
  89 +
  90 + except:
  91 + raise BadJSONException
  92 + return None
  93 +
  94 +
  95 +class GenericAPI:
  96 +
  97 + """
  98 + __init__
  99 + Parameters: APIS, a list of tuples of form (FUNCTIONNAME, LINK).
  100 +
  101 + Creates functions of name FUNCTIONNAME that perform an API call to LINK
  102 + when called, giving back the response as JSON.
  103 +
  104 + Returns: Nothing
  105 + """
  106 +
  107 + def __init__(self, apis):
  108 +
  109 + self.api_objects = []
  110 + self.api_functions = []
  111 + self.functions = []
  112 +
  113 + # Who writes normal functions when you can use CLOSURES?!?
  114 +
  115 + number = 0
  116 +
  117 + # Bind each function to the class.
  118 + for api in apis:
  119 + self.api_objects.append(BaseAPIRequest(api[1]))
  120 + self.bind_closure(number)
  121 + setattr(self, api[0], self.api_functions[number])
  122 + number += 1
  123 +
  124 + # Creates a function on the current class.
  125 + def bind_closure(self, number):
  126 + def generic_api_call(*ordered_args, **kwargs):
  127 + self.api_objects[number].format_url(*ordered_args, **kwargs)
  128 + return self.api_objects[number].request(*ordered_args, **kwargs)
  129 +
  130 + self.api_functions.append(generic_api_call)
22 build/lib.linux-x86_64-2.6/python-fcc/license_view_api.py
... ... @@ -0,0 +1,22 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the License View API provided by the FCC.
  4 +
  5 +APIS = [ ("get_licenses", "http://data.fcc.gov/api/license-view/basicSearch/getLicenses" )
  6 + , ("get_common_names", "http://data.fcc.gov/api/license-view/licenses/getCommonNames")
  7 + , ("get_statuses", "http://data.fcc.gov/api/license-view/licenses/getStatuses")
  8 + , ("get_categories", "http://data.fcc.gov/api/license-view/licenses/getCategories")
  9 + , ("get_entities", "http://data.fcc.gov/api/license-view/licenses/getEntities")
  10 + , ("get_renewals", "http://data.fcc.gov/api/license-view/licenses/getRenewals")
  11 + , ("get_issued", "http://data.fcc.gov/api/license-view/licenses/getIssued")
  12 + ]
  13 +
  14 +
  15 +class LicenseViewAPI(GenericAPI):
  16 + def __init__(self):
  17 + GenericAPI.__init__(self, APIS)
  18 +
  19 +# Sample use of BlockConversionAPI
  20 +if __name__ == "__main__":
  21 + bc = LicenseViewAPI()
  22 + print bc.get_licenses(searchValue = "Verizon Wireless")
33 build/lib.linux-x86_64-2.6/python-fcc/sba_businesses_api.py
... ... @@ -0,0 +1,33 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the SBA Business API.
  4 +
  5 +def build_api_list(list, base):
  6 + result = []
  7 + for item in list:
  8 + result.append((item, base + "/" + item + "/" ))
  9 +
  10 + return result
  11 +
  12 +
  13 +#These are the function calls that the SBABusinessesAPI object exposes.
  14 +list = [ "by_category"
  15 + , "all_by_state"
  16 + , "by_business_type"
  17 + , "state_only"
  18 + , "state_and_county"
  19 + , "state_and_city"
  20 + , "by_zip"
  21 + ]
  22 +
  23 +APIS = build_api_list(list, "http://api.sba.gov/license_permit")
  24 +
  25 +class SBABusinessesAPI(GenericAPI):
  26 + def __init__(self):
  27 + GenericAPI.__init__(self, APIS)
  28 +
  29 +
  30 +# Sample use of BroadbandApi
  31 +if __name__ == "__main__":
  32 + bb = SBABusinessesAPI()
  33 + print bb.by_category("doing business as") # (Should be San Francisco)
35 build/lib.linux-x86_64-2.6/python-fcc/sba_loans_grants_api.py
... ... @@ -0,0 +1,35 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the SBA Business API.
  4 +
  5 +def build_api_list(list, base):
  6 + result = []
  7 + for item in list:
  8 + result.append((item, base + "/" + item + "/" ))
  9 +
  10 + return result
  11 +
  12 +
  13 +#These are the function calls that the SBABusinessesAPI object exposes.
  14 +list = [ "federal"
  15 + , "state_financing_for"
  16 + , "federal_and_state_financing_for"
  17 + # , "nil/for_profit"
  18 + # , "nil/for_profit/nil" #What were they thinking?!
  19 + # , "nil/for_profit"
  20 + # STATE ABBREVIATION/for_profit/nil/SPECIALTY
  21 + # STATE ABBREVIATION/for_profit/nil/SPECIALTY1-SPECIALTY2- ... SPECIALTYN
  22 + # STATE ABBREVIATION/for_profit/INDUSTRY/SPECIALTY
  23 + ]
  24 +
  25 +APIS = build_api_list(list, "http://api.sba.gov/loans_grants")
  26 +
  27 +class SBALoansGrantsAPI(GenericAPI):
  28 + def __init__(self):
  29 + GenericAPI.__init__(self, APIS)
  30 +
  31 +
  32 +# Sample use of BroadbandApi
  33 +if __name__ == "__main__":
  34 + bb = SBALoansGrantsAPI()
  35 + print bb.by_category("doing business as") # (Should be San Francisco)
0  build/lib.linux-x86_64-2.6/python-fcc/tests/__init__.py
No changes.
5 build/lib.linux-x86_64-2.6/python-fcc/tests/__main__.py
... ... @@ -0,0 +1,5 @@
  1 +from tests.testcases import *
  2 +from tests.testutils import *
  3 +
  4 +if __name__ == '__main__':
  5 + unittest.main()
105 build/lib.linux-x86_64-2.6/python-fcc/tests/testcases.py
... ... @@ -0,0 +1,105 @@
  1 +import sys
  2 +import unittest
  3 +
  4 +sys.path.append('../')
  5 +
  6 +from broadband_api import *
  7 +from FRNConversionsAPI import *
  8 +from block_conversion_api import *
  9 +
  10 +class TestBroadbandAPI (unittest.TestCase):
  11 + def setUp(self):
  12 + self.bb = BroadbandApi()
  13 + self.frnapi = FRNConversionsAPI()
  14 +
  15 +
  16 + #Sweep across the US and compare to precomputed values.
  17 + def test_Sweep(self):
  18 +
  19 + # The following is how I computed the values beforehand:
  20 +
  21 + # print "results = "
  22 + # print "[",
  23 + # for x in range(10):
  24 + # print self.bb.request(latitude=41, longitude=-86 + x * 10)['status'] == 'OK', ",",
  25 + # print "]"
  26 + results = [ True , True , False , False , False , False , False , False , False , False , ]
  27 +
  28 + for x in range(10):
  29 + self.assertTrue((self.bb.get_data(latitude=41, longitude=-86 + x * 10)['status'] == 'OK') == results[x])
  30 +
  31 +
  32 + # Does SF exist?
  33 + def test_SF(self):
  34 + result = self.bb.get_data(latitude=37, longitude=-122)
  35 +
  36 + self.assertTrue(result['status'] == 'OK')
  37 + self.assertTrue('SpeedTestCounty' in result)
  38 +
  39 + # Does Chicago exist?
  40 + def test_Chicago(self):
  41 + result = self.bb.get_data(latitude=41, longitude=-87)
  42 +
  43 + self.assertTrue(result['status'] == 'OK')
  44 +
  45 +
  46 + # Test the middle of nowhere (Disclaimer: I have no idea where
  47 + # this is, so it may not be in the middle of nowhere)
  48 + def test_Nowhere(self):
  49 + result = self.bb.get_data(latitude=35, longitude=35)
  50 +
  51 + self.assertTrue(result['status'] == 'Fail')
  52 +
  53 + def test_FRN(self):
  54 + result = self.frnapi.getInfo(frn='0017855545')
  55 + self.assertTrue(result['Info']['frn'] == '0017855545')
  56 +
  57 + def test_companyName(self):
  58 + result = self.frnapi.getInfo(frn='0017855545')
  59 + self.assertTrue(result['Info']['companyName']=='Cygnus Telecommunications Corporation')
  60 +
  61 + def test_FRNapiIsDict(self):
  62 + result1 = self.frnapi.getList(stateCode='IL')
  63 + result2 = self.frnapi.getInfo(frn='0017855545')
  64 + self.assertTrue(type(result1)==type({}) and type(result2)==type({}))
  65 + def test_CygnusInIL(self):
  66 + result = self.frnapi.getList(stateCode='IL')
  67 + #print result['Frns']
  68 + #Cygnus Telecommunications Corporation
  69 + self.assertTrue('Cygnus Telecommunications Corporation' in [x['companyName'] for x in result['Frns']['Frn']])
  70 +
  71 +
  72 +
  73 +
  74 +
  75 +
  76 +
  77 +class TestBlockConversionAPI (unittest.TestCase):
  78 + def setUp(self):
  79 + self.bb = BlockConversionAPI()
  80 +
  81 + # Does SF exist?
  82 + def test_SF(self):
  83 + result = self.bb.get_block(latitude=37, longitude=-122)
  84 +
  85 + self.assertTrue(result['status'] == 'OK')
  86 + self.assertTrue(result['State']['code'] == 'CA')
  87 + self.assertTrue(result['Block']['FIPS'] == '060871001001002C')
  88 +
  89 +
  90 + # Does (somewhere near) Chicago exist?
  91 + def test_Chicago(self):
  92 + result = self.bb.get_block(latitude=41, longitude=-87)
  93 +
  94 + self.assertTrue(result['Block']['FIPS'] == '180739908004112')
  95 +
  96 +
  97 + # Test the middle of nowhere (Disclaimer: I have no idea where
  98 + # this is, so it may not be in the middle of nowhere)
  99 + def test_Nowhere(self):
  100 + result = self.bb.get_block(latitude=35, longitude=35)
  101 +
  102 + self.assertTrue(result['status'] == 'Fail')
  103 +
  104 +if __name__ == '__main__':
  105 + unittest.main()
BIN  dist/python_fcc-0.1-py2.6.egg
Binary file not shown
45 python-fcc/FRNConversionsAPI.py
... ... @@ -0,0 +1,45 @@
  1 +from generic_api import *
  2 +import urllib,json
  3 +
  4 +
  5 +# Simple Python wrapper around the Broadband API provided by the FCC.
  6 +
  7 +class FRNConversionsAPI(GenericAPI):
  8 + """
  9 + FRNConversionsAPI:
  10 + - an API to access information about broadband provides.
  11 + - original API: http://reboot.fcc.gov/developer/frn-conversions-api
  12 +
  13 + - methods:
  14 + -- getList(stateCode, multi)
  15 + takes stateCode=IL or stateCode=17 (FIPS code),
  16 + (optional) multi=Yes or multi=No to signify whether to include FRNs also operating outside this state.
  17 +
  18 + -- getInfo(frn)
  19 + takes frn='0017855545' (FRN id)
  20 + """
  21 +
  22 +
  23 + def __init__(self):
  24 + apis= [
  25 + ('getList', 'http://data.fcc.gov/api/frn/getList'),
  26 + ('getInfo', 'http://data.fcc.gov/api/frn/getInfo')]
  27 +
  28 + GenericAPI.__init__(self,apis)
  29 +
  30 +
  31 +if __name__ == "__main__":
  32 + bb = FRNConversionsAPI()
  33 +
  34 + x=bb.getList(stateCode='IL')
  35 + print type(x)
  36 + print len(x)
  37 + print x.keys()
  38 +
  39 + #print x
  40 +
  41 + print
  42 + x=bb.getInfo(frn='0017855545')
  43 + print type(x)
  44 + print len(x)
  45 + print x
0  python-fcc/__init__.py
No changes.
13 python-fcc/block_conversion_api.py
... ... @@ -0,0 +1,13 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the Block Conversion API provided by the FCC.
  4 +
  5 +class BlockConversionAPI(GenericAPI):
  6 + def __init__(self):
  7 + GenericAPI.__init__(self, [("get_block", "http://data.fcc.gov/api/block/find")])
  8 +
  9 +
  10 +# Sample use of BlockConversionAPI
  11 +if __name__ == "__main__":
  12 + bc = BlockConversionAPI()
  13 + print bc.get_block(lat=41, long=-87) # (Should be San Francisco)
15 python-fcc/broadband_api.py
... ... @@ -0,0 +1,15 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the Broadband API provided by the FCC.
  4 +
  5 +APIS = [("get_data", "http://data.fcc.gov/api/speedtest/find")]
  6 +
  7 +class BroadbandApi(GenericAPI):
  8 + def __init__(self):
  9 + GenericAPI.__init__(self, APIS)
  10 +
  11 +
  12 +# Sample use of BroadbandApi
  13 +if __name__ == "__main__":
  14 + bb = BroadbandApi()
  15 + print bb.get_data(latitude=37, longitude=-122) # (Should be San Francisco)
0  python-fcc/build/lib.linux-x86_64-2.6/tests/__init__.py
No changes.
5 python-fcc/build/lib.linux-x86_64-2.6/tests/__main__.py
... ... @@ -0,0 +1,5 @@
  1 +from tests.testcases import *
  2 +from tests.testutils import *
  3 +
  4 +if __name__ == '__main__':
  5 + unittest.main()
105 python-fcc/build/lib.linux-x86_64-2.6/tests/testcases.py
... ... @@ -0,0 +1,105 @@
  1 +import sys
  2 +import unittest
  3 +
  4 +sys.path.append('../')
  5 +
  6 +from broadband_api import *
  7 +from FRNConversionsAPI import *
  8 +from block_conversion_api import *
  9 +
  10 +class TestBroadbandAPI (unittest.TestCase):
  11 + def setUp(self):
  12 + self.bb = BroadbandApi()
  13 + self.frnapi = FRNConversionsAPI()
  14 +
  15 +
  16 + #Sweep across the US and compare to precomputed values.
  17 + def test_Sweep(self):
  18 +
  19 + # The following is how I computed the values beforehand:
  20 +
  21 + # print "results = "
  22 + # print "[",
  23 + # for x in range(10):
  24 + # print self.bb.request(latitude=41, longitude=-86 + x * 10)['status'] == 'OK', ",",
  25 + # print "]"
  26 + results = [ True , True , False , False , False , False , False , False , False , False , ]
  27 +
  28 + for x in range(10):
  29 + self.assertTrue((self.bb.get_data(latitude=41, longitude=-86 + x * 10)['status'] == 'OK') == results[x])
  30 +
  31 +
  32 + # Does SF exist?
  33 + def test_SF(self):
  34 + result = self.bb.get_data(latitude=37, longitude=-122)
  35 +
  36 + self.assertTrue(result['status'] == 'OK')
  37 + self.assertTrue('SpeedTestCounty' in result)
  38 +
  39 + # Does Chicago exist?
  40 + def test_Chicago(self):
  41 + result = self.bb.get_data(latitude=41, longitude=-87)
  42 +
  43 + self.assertTrue(result['status'] == 'OK')
  44 +
  45 +
  46 + # Test the middle of nowhere (Disclaimer: I have no idea where
  47 + # this is, so it may not be in the middle of nowhere)
  48 + def test_Nowhere(self):
  49 + result = self.bb.get_data(latitude=35, longitude=35)
  50 +
  51 + self.assertTrue(result['status'] == 'Fail')
  52 +
  53 + def test_FRN(self):
  54 + result = self.frnapi.getInfo(frn='0017855545')
  55 + self.assertTrue(result['Info']['frn'] == '0017855545')
  56 +
  57 + def test_companyName(self):
  58 + result = self.frnapi.getInfo(frn='0017855545')
  59 + self.assertTrue(result['Info']['companyName']=='Cygnus Telecommunications Corporation')
  60 +
  61 + def test_FRNapiIsDict(self):
  62 + result1 = self.frnapi.getList(stateCode='IL')
  63 + result2 = self.frnapi.getInfo(frn='0017855545')
  64 + self.assertTrue(type(result1)==type({}) and type(result2)==type({}))
  65 + def test_CygnusInIL(self):
  66 + result = self.frnapi.getList(stateCode='IL')
  67 + #print result['Frns']
  68 + #Cygnus Telecommunications Corporation
  69 + self.assertTrue('Cygnus Telecommunications Corporation' in [x['companyName'] for x in result['Frns']['Frn']])
  70 +
  71 +
  72 +
  73 +
  74 +
  75 +
  76 +
  77 +class TestBlockConversionAPI (unittest.TestCase):
  78 + def setUp(self):
  79 + self.bb = BlockConversionAPI()
  80 +
  81 + # Does SF exist?
  82 + def test_SF(self):
  83 + result = self.bb.get_block(latitude=37, longitude=-122)
  84 +
  85 + self.assertTrue(result['status'] == 'OK')
  86 + self.assertTrue(result['State']['code'] == 'CA')
  87 + self.assertTrue(result['Block']['FIPS'] == '060871001001002C')
  88 +
  89 +
  90 + # Does (somewhere near) Chicago exist?
  91 + def test_Chicago(self):
  92 + result = self.bb.get_block(latitude=41, longitude=-87)
  93 +
  94 + self.assertTrue(result['Block']['FIPS'] == '180739908004112')
  95 +
  96 +
  97 + # Test the middle of nowhere (Disclaimer: I have no idea where
  98 + # this is, so it may not be in the middle of nowhere)
  99 + def test_Nowhere(self):
  100 + result = self.bb.get_block(latitude=35, longitude=35)
  101 +
  102 + self.assertTrue(result['status'] == 'Fail')
  103 +
  104 +if __name__ == '__main__':
  105 + unittest.main()
BIN  python-fcc/dist/python_fcc-0.1-py2.6.egg
Binary file not shown
130 python-fcc/generic_api.py
... ... @@ -0,0 +1,130 @@
  1 +import urllib
  2 +import json
  3 +
  4 +class NoArgumentsException(Exception):
  5 + def __str__(self):
  6 + return "No arguments were found."
  7 +
  8 +class BadJSONException(Exception):
  9 + def __str__(self):
  10 + return "The returned JSON was invalid."
  11 +
  12 +class NonexistentStyleException(Exception):
  13 + def __str__(self):
  14 + return "The requested API style was invalid."
  15 +
  16 +class MustBeOrderedException(Exception):
  17 + def __str__(self):
  18 + return "The arguments you pass to the SBA API must be ordered."
  19 +
  20 +
  21 +# Perform requests to generic APIs.
  22 +
  23 +PHP_STYLE = 0 # &x=1&y=2
  24 +SBA_WEIRD_STYLE = 1
  25 +
  26 +class BaseAPIRequest:
  27 + def __init__(self, url):
  28 + self.url = url
  29 +
  30 + if self.url.startswith("http://api.sba.gov") or self.url.startswith("api.sba.gov"):
  31 + self.api_style = SBA_WEIRD_STYLE
  32 + else:
  33 + self.api_style = PHP_STYLE
  34 +
  35 + def __format_url_php_style(self, *ordered_args, **args):
  36 + """
  37 + Format the url with the arguments in PHP style. That is,
  38 + ?x=5&y=6
  39 + """
  40 + if args is None: raise NoArgumentsException
  41 +
  42 + args["format"] = "json" #Ask for JSON formatted response
  43 + append = ""
  44 +
  45 + for arg in args: append += str(arg) + "=" + str(args[arg]) + "&"
  46 +
  47 + append = append[:-1] #Take off last '&'
  48 + self.formatted_url = self.url + "?" + append
  49 +
  50 + def __format_url_weird_style(self, *ordered_args, **args):
  51 + """
  52 + Format the url with the arguments in the weird style given by SBA.
  53 + arg1/arg2/arg3.json
  54 + """
  55 +
  56 + append = ""
  57 + for arg in ordered_args: append += str(arg) + "/"
  58 + append = append[:-1] #Take off last '/'
  59 +
  60 +
  61 + self.formatted_url = self.url + append + ".json"
  62 +
  63 + # Formats a URL with the provided keyword arguments.
  64 + def format_url(self, *ordered_args, **args):
  65 + if self.api_style == PHP_STYLE:
  66 + self.__format_url_php_style(*ordered_args, **args)
  67 + elif self.api_style == SBA_WEIRD_STYLE:
  68 + print ordered_args
  69 + print args
  70 + if len(args) != 0:
  71 + raise MustBeOrderedException # MUST be ordered.
  72 +
  73 + self.__format_url_weird_style(*ordered_args)
  74 + else:
  75 + raise NonexistentStyleException
  76 +
  77 + # Requests the API and returns the JSON object.
  78 + def request(self, *ordered_args, **args):
  79 + self.format_url(*ordered_args, **args)
  80 + #print self.formatted_url
  81 + t = urllib.urlopen(self.formatted_url).read().strip()
  82 + if t.startswith("callback("):
  83 + t=t[t.index("(")+1:-1]
  84 +
  85 + print self.formatted_url
  86 +
  87 + try:
  88 + return json.loads(t)
  89 +
  90 + except:
  91 + raise BadJSONException
  92 + return None
  93 +
  94 +
  95 +class GenericAPI:
  96 +
  97 + """
  98 + __init__
  99 + Parameters: APIS, a list of tuples of form (FUNCTIONNAME, LINK).
  100 +
  101 + Creates functions of name FUNCTIONNAME that perform an API call to LINK
  102 + when called, giving back the response as JSON.
  103 +
  104 + Returns: Nothing
  105 + """
  106 +
  107 + def __init__(self, apis):
  108 +
  109 + self.api_objects = []
  110 + self.api_functions = []
  111 + self.functions = []
  112 +
  113 + # Who writes normal functions when you can use CLOSURES?!?
  114 +
  115 + number = 0
  116 +
  117 + # Bind each function to the class.
  118 + for api in apis:
  119 + self.api_objects.append(BaseAPIRequest(api[1]))
  120 + self.bind_closure(number)
  121 + setattr(self, api[0], self.api_functions[number])
  122 + number += 1
  123 +
  124 + # Creates a function on the current class.
  125 + def bind_closure(self, number):
  126 + def generic_api_call(*ordered_args, **kwargs):
  127 + self.api_objects[number].format_url(*ordered_args, **kwargs)
  128 + return self.api_objects[number].request(*ordered_args, **kwargs)
  129 +
  130 + self.api_functions.append(generic_api_call)
22 python-fcc/license_view_api.py
... ... @@ -0,0 +1,22 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the License View API provided by the FCC.
  4 +
  5 +APIS = [ ("get_licenses", "http://data.fcc.gov/api/license-view/basicSearch/getLicenses" )
  6 + , ("get_common_names", "http://data.fcc.gov/api/license-view/licenses/getCommonNames")
  7 + , ("get_statuses", "http://data.fcc.gov/api/license-view/licenses/getStatuses")
  8 + , ("get_categories", "http://data.fcc.gov/api/license-view/licenses/getCategories")
  9 + , ("get_entities", "http://data.fcc.gov/api/license-view/licenses/getEntities")
  10 + , ("get_renewals", "http://data.fcc.gov/api/license-view/licenses/getRenewals")
  11 + , ("get_issued", "http://data.fcc.gov/api/license-view/licenses/getIssued")
  12 + ]
  13 +
  14 +
  15 +class LicenseViewAPI(GenericAPI):
  16 + def __init__(self):
  17 + GenericAPI.__init__(self, APIS)
  18 +
  19 +# Sample use of BlockConversionAPI
  20 +if __name__ == "__main__":
  21 + bc = LicenseViewAPI()
  22 + print bc.get_licenses(searchValue = "Verizon Wireless")
10 python-fcc/python_fcc.egg-info/PKG-INFO
... ... @@ -0,0 +1,10 @@
  1 +Metadata-Version: 1.0
  2 +Name: python-fcc
  3 +Version: 0.1
  4 +Summary: An API abstraction of the FCC API found at http://reboot.fcc.gov/developer
  5 +Home-page: http://github.com/codeforamerica/FCC-Python-Egg
  6 +Author: johnfn, quadrismegistus
  7 +Author-email: grantm@stanford.edu, heuser@stanford.edu
  8 +License: UNKNOWN
  9 +Description: UNKNOWN
  10 +Platform: UNKNOWN
8 python-fcc/python_fcc.egg-info/SOURCES.txt
... ... @@ -0,0 +1,8 @@
  1 +setup.py
  2 +python_fcc.egg-info/PKG-INFO
  3 +python_fcc.egg-info/SOURCES.txt
  4 +python_fcc.egg-info/dependency_links.txt
  5 +python_fcc.egg-info/top_level.txt
  6 +tests/__init__.py
  7 +tests/__main__.py
  8 +tests/testcases.py
1  python-fcc/python_fcc.egg-info/dependency_links.txt
... ... @@ -0,0 +1 @@
1  python-fcc/python_fcc.egg-info/top_level.txt
... ... @@ -0,0 +1 @@
  1 +tests
33 python-fcc/sba_businesses_api.py
... ... @@ -0,0 +1,33 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the SBA Business API.
  4 +
  5 +def build_api_list(list, base):
  6 + result = []
  7 + for item in list:
  8 + result.append((item, base + "/" + item + "/" ))
  9 +
  10 + return result
  11 +
  12 +
  13 +#These are the function calls that the SBABusinessesAPI object exposes.
  14 +list = [ "by_category"
  15 + , "all_by_state"
  16 + , "by_business_type"
  17 + , "state_only"
  18 + , "state_and_county"
  19 + , "state_and_city"
  20 + , "by_zip"
  21 + ]
  22 +
  23 +APIS = build_api_list(list, "http://api.sba.gov/license_permit")
  24 +
  25 +class SBABusinessesAPI(GenericAPI):
  26 + def __init__(self):
  27 + GenericAPI.__init__(self, APIS)
  28 +
  29 +
  30 +# Sample use of BroadbandApi
  31 +if __name__ == "__main__":
  32 + bb = SBABusinessesAPI()
  33 + print bb.by_category("doing business as") # (Should be San Francisco)
35 python-fcc/sba_loans_grants_api.py
... ... @@ -0,0 +1,35 @@
  1 +from generic_api import *
  2 +
  3 +# Simple Python wrapper around the SBA Business API.
  4 +
  5 +def build_api_list(list, base):
  6 + result = []
  7 + for item in list:
  8 + result.append((item, base + "/" + item + "/" ))
  9 +
  10 + return result
  11 +
  12 +
  13 +#These are the function calls that the SBABusinessesAPI object exposes.
  14 +list = [ "federal"
  15 + , "state_financing_for"
  16 + , "federal_and_state_financing_for"
  17 + # , "nil/for_profit"
  18 + # , "nil/for_profit/nil" #What were they thinking?!
  19 + # , "nil/for_profit"
  20 + # STATE ABBREVIATION/for_profit/nil/SPECIALTY
  21 + # STATE ABBREVIATION/for_profit/nil/SPECIALTY1-SPECIALTY2- ... SPECIALTYN
  22 + # STATE ABBREVIATION/for_profit/INDUSTRY/SPECIALTY
  23 + ]
  24 +
  25 +APIS = build_api_list(list, "http://api.sba.gov/loans_grants")
  26 +
  27 +class SBALoansGrantsAPI(GenericAPI):
  28 + def __init__(self):
  29 + GenericAPI.__init__(self, APIS)
  30 +
  31 +
  32 +# Sample use of BroadbandApi
  33 +if __name__ == "__main__":
  34 + bb = SBALoansGrantsAPI()
  35 + print bb.by_category("doing business as") # (Should be San Francisco)
0  python-fcc/tests/__init__.py
No changes.
5 python-fcc/tests/__main__.py
... ... @@ -0,0 +1,5 @@
  1 +from tests.testcases import *
  2 +from tests.testutils import *
  3 +
  4 +if __name__ == '__main__':
  5 + unittest.main()
105 python-fcc/tests/testcases.py
... ... @@ -0,0 +1,105 @@
  1 +import sys
  2 +import unittest
  3 +
  4 +sys.path.append('../')
  5 +
  6 +from broadband_api import *
  7 +from FRNConversionsAPI import *
  8 +from block_conversion_api import *
  9 +
  10 +class TestBroadbandAPI (unittest.TestCase):
  11 + def setUp(self):
  12 + self.bb = BroadbandApi()
  13 + self.frnapi = FRNConversionsAPI()
  14 +
  15 +
  16 + #Sweep across the US and compare to precomputed values.
  17 + def test_Sweep(self):
  18 +
  19 + # The following is how I computed the values beforehand:
  20 +
  21 + # print "results = "
  22 + # print "[",
  23 + # for x in range(10):
  24 + # print self.bb.request(latitude=41, longitude=-86 + x * 10)['status'] == 'OK', ",",
  25 + # print "]"
  26 + results = [ True , True , False , False , False , False , False , False , False , False , ]
  27 +
  28 + for x in range(10):
  29 + self.assertTrue((self.bb.get_data(latitude=41, longitude=-86 + x * 10)['status'] == 'OK') == results[x])
  30 +
  31 +
  32 + # Does SF exist?
  33 + def test_SF(self):
  34 + result = self.bb.get_data(latitude=37, longitude=-122)
  35 +
  36 + self.assertTrue(result['status'] == 'OK')
  37 + self.assertTrue('SpeedTestCounty' in result)
  38 +
  39 + # Does Chicago exist?
  40 + def test_Chicago(self):
  41 + result = self.bb.get_data(latitude=41, longitude=-87)
  42 +
  43 + self.assertTrue(result['status'] == 'OK')
  44 +
  45 +
  46 + # Test the middle of nowhere (Disclaimer: I have no idea where
  47 + # this is, so it may not be in the middle of nowhere)
  48 + def test_Nowhere(self):
  49 + result = self.bb.get_data(latitude=35, longitude=35)
  50 +
  51 + self.assertTrue(result['status'] == 'Fail')
  52 +
  53 + def test_FRN(self):
  54 + result = self.frnapi.getInfo(frn='0017855545')
  55 + self.assertTrue(result['Info']['frn'] == '0017855545')
  56 +
  57 + def test_companyName(self):
  58 + result = self.frnapi.getInfo(frn='0017855545')
  59 + self.assertTrue(result['Info']['companyName']=='Cygnus Telecommunications Corporation')
  60 +
  61 + def test_FRNapiIsDict(self):
  62 + result1 = self.frnapi.getList(stateCode='IL')
  63 + result2 = self.frnapi.getInfo(frn='0017855545')
  64 + self.assertTrue(type(result1)==type({}) and type(result2)==type({}))
  65 + def test_CygnusInIL(self):
  66 + result = self.frnapi.getList(stateCode='IL')
  67 + #print result['Frns']
  68 + #Cygnus Telecommunications Corporation
  69 + self.assertTrue('Cygnus Telecommunications Corporation' in [x['companyName'] for x in result['Frns']['Frn']])
  70 +
  71 +
  72 +
  73 +
  74 +
  75 +
  76 +
  77 +class TestBlockConversionAPI (unittest.TestCase):
  78 + def setUp(self):
  79 + self.bb = BlockConversionAPI()
  80 +
  81 + # Does SF exist?
  82 + def test_SF(self):
  83 + result = self.bb.get_block(latitude=37, longitude=-122)
  84 +
  85 + self.assertTrue(result['status'] == 'OK')
  86 + self.assertTrue(result['State']['code'] == 'CA')
  87 + self.assertTrue(result['Block']['FIPS'] == '060871001001002C')
  88 +
  89 +
  90 + # Does (somewhere near) Chicago exist?
  91 + def test_Chicago(self):
  92 + result = self.bb.get_block(latitude=41, longitude=-87)
  93 +
  94 + self.assertTrue(result['Block']['FIPS'] == '180739908004112')
  95 +
  96 +
  97 + # Test the middle of nowhere (Disclaimer: I have no idea where
  98 + # this is, so it may not be in the middle of nowhere)
  99 + def test_Nowhere(self):
  100 + result = self.bb.get_block(latitude=35, longitude=35)
  101 +
  102 + self.assertTrue(result['status'] == 'Fail')
  103 +
  104 +if __name__ == '__main__':
  105 + unittest.main()
10 python_fcc.egg-info/PKG-INFO
... ... @@ -0,0 +1,10 @@
  1 +Metadata-Version: 1.0
  2 +Name: python-fcc
  3 +Version: 0.1
  4 +Summary: An API abstraction of the FCC API found at http://reboot.fcc.gov/developer
  5 +Home-page: http://github.com/codeforamerica/FCC-Python-Egg
  6 +Author: johnfn, quadrismegistus
  7 +Author-email: grantm@stanford.edu, heuser@stanford.edu
  8 +License: UNKNOWN
  9 +Description: UNKNOWN
  10 +Platform: UNKNOWN
16 python_fcc.egg-info/SOURCES.txt
... ... @@ -0,0 +1,16 @@
  1 +setup.py
  2 +python-fcc/FRNConversionsAPI.py
  3 +python-fcc/__init__.py
  4 +python-fcc/block_conversion_api.py
  5 +python-fcc/broadband_api.py
  6 +python-fcc/generic_api.py
  7 +python-fcc/license_view_api.py
  8 +python-fcc/sba_businesses_api.py
  9 +python-fcc/sba_loans_grants_api.py
  10 +python-fcc/tests/__init__.py
  11 +python-fcc/tests/__main__.py
  12 +python-fcc/tests/testcases.py
  13 +python_fcc.egg-info/PKG-INFO
  14 +python_fcc.egg-info/SOURCES.txt
  15 +python_fcc.egg-info/dependency_links.txt
  16 +python_fcc.egg-info/top_level.txt
1  python_fcc.egg-info/dependency_links.txt
... ... @@ -0,0 +1 @@
1  python_fcc.egg-info/top_level.txt
... ... @@ -0,0 +1 @@
  1 +python-fcc
10 setup.py
... ... @@ -0,0 +1,10 @@
  1 +from setuptools import setup, find_packages
  2 +setup(
  3 + name = "python-fcc",
  4 + version = "0.1",
  5 + description = "An API abstraction of the FCC API found at http://reboot.fcc.gov/developer",
  6 + author='johnfn, quadrismegistus',
  7 + author_email='grantm@stanford.edu, heuser@stanford.edu',
  8 + url='http://github.com/codeforamerica/FCC-Python-Egg',
  9 + packages = find_packages(),
  10 +)

1 comment on commit b8dcf82

Dan Colish

Looks like a lot of the dist and build code got committed. Might want to also add package ignores for any tests. Maybe consider a namespace for the top level?

Please sign in to comment.
Something went wrong with that request. Please try again.