2016-12-01 44 views
0

是否可以重構此腳本以使其作爲完全獨立的方法存在?將重構python腳本轉換爲可隔離的方法

import json 
import requests 
from collections import defaultdict 
from pprint import pprint 

def hasNumbers(inputString): 
    return any(char.isdigit() for char in inputString) 

# open up the output of 'data-processing.py' 
with open('job-numbers-by-location.txt') as data_file: 

    # print the output to a file 
    with open('phase_ii_output.txt', 'w') as output_file_: 
     for line in data_file: 
      identifier, name, coords, number_of_jobs = line.split("|") 
      coords = coords[1:-1] 
      lat, lng = coords.split(",") 
      # print("lat: " + lat, "lng: " + lng) 
      response = requests.get("http://api.geonames.org/countrySubdivisionJSON?lat="+lat+"&lng="+lng+"&username=s.matthew.english").json() 


      codes = response.get('codes', []) 
      for code in codes: 
       if code.get('type') == 'ISO3166-2': 
        country_code = '{}-{}'.format(response.get('countryCode', 'UNKNOWN'), code.get('code', 'UNKNOWN')) 
        if not hasNumbers(country_code): 
         # print("code: " + country_code + ", jobs: " + number_of_jobs) 
         output_file_.write("code: " + country_code + ", jobs: " + number_of_jobs) 
    output_file_.close() 

我一直在努力使它成爲一個更大的過程的一個組成部分。

回答

2

有一對夫婦的事情可以做。您可能希望將腳本的每個步驟分解爲單獨的方法,每個方法都有自己的異常處理,並記錄以指示作業失敗的位置。另外,我沒有在這裏提到返回參數。您可以返回True/False來表明處理是否已通過/失敗。

然後,您可以在其他位置導入process_file方法,並將它傳遞給需要處理的2個文件。

import json 
import requests 
from collections import defaultdict 
from pprint import pprint 

def hasNumbers(inputString): 
    return any(char.isdigit() for char in inputString) 

def handle_get(url, params) 
    try: 
     response = requests.get(url, params=urlencode(params)) 
    except requests.exceptions.RequestException as e: # This is the correct syntax 
     print e 
     # sys.exit(1) 
     response = None 

    return response 

def process_file(data_file_path, output_file_path) 
    # open up the output of 'data-processing.py' 
    with open(data_file_path) as data_file: 

     # print the output to a file 
     with open(output_file_path, 'w') as output_file_: 
      for line in data_file: 
       identifier, name, coords, number_of_jobs = line.split("|") 
       coords = coords[1:-1] 
       lat, lng = coords.split(",") 
       params = OrderedDict([('lat', lat), ('lng', lng), ('username', 's.matthew.english')]) 
       url = "http://api.geonames.org/countrySubdivisionJSON" 
       response = handle_get(url, params) 
       if response: 
        json_response = response.json() 
       else: 
        print('Something bad happened') 
        sys.exit(1) 


       codes = response.get('codes', []) 
       for code in codes: 
        if code.get('type') == 'ISO3166-2': 
         country_code = '{}-{}'.format(response.get('countryCode', 'UNKNOWN'), code.get('code', 'UNKNOWN')) 
         if not hasNumbers(country_code): 
          # print("code: " + country_code + ", jobs: " + number_of_jobs) 
          output_file_.write("code: " + country_code + ", jobs: " + number_of_jobs) 
+0

我想將它合併最終進入[這件事(http://stackoverflow.com/questions/40914419/parse-json-from-an-api-with-python-exception-handling-手術提取) –