【问题标题】:Refactor Python script into an isolatable method将 Python 脚本重构为可隔离的方法
【发布时间】:2016-12-01 17:47:07
【问题描述】:

是否可以重构这个脚本,使它作为一个完全独立的方法存在?

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()

我一直在努力使它成为一个更大的过程的一个组成部分。

【问题讨论】:

    标签: python refactoring


    【解决方案1】:

    您可以做几件事。您可能希望将脚本的每个步骤分解为单独的方法,每个方法都有自己的异常处理和日志记录以指示作业失败的位置。另外,这里我没有提到返回参数。您可以返回 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)
    

    【讨论】:

    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2021-07-16
    相关资源
    最近更新 更多