Python - Преобразование CSV в объекты - Дизайн кода - PullRequest
5 голосов
/ 03 июня 2010

У меня есть небольшой скрипт, который мы используем для чтения в CSV-файле, содержащем сотрудников, и выполняем некоторые основные манипуляции с этими данными.

Мы читаем данные (import_gd_dump) и создаем Employees объект, содержащий список Employee объектов (может быть, мне следует подумать о лучшем соглашении об именовании ... смеется).Затем мы вызываем clean_all_phone_numbers() на Employees, что вызывает clean_phone_number() на каждом Employee, а также lookup_all_supervisors() на Employees.

import csv
import re
import sys

#class CSVLoader:
#    """Virtual class to assist with loading in CSV files."""
#    def import_gd_dump(self, input_file='Gp Directory 20100331 original.csv'):
#        gd_extract = csv.DictReader(open(input_file), dialect='excel')
#        employees = []
#        for row in gd_extract:
#            curr_employee = Employee(row)
#            employees.append(curr_employee)
#        return employees
#    #self.employees = {row['dbdirid']:row for row in gd_extract}

# Previously, this was inside a (virtual) class called "CSVLoader".
# However, according to here (http://tomayko.com/writings/the-static-method-thing) - the idiomatic way of doing this in Python is not with a class-function but with a module-level function
def import_gd_dump(input_file='Gp Directory 20100331 original.csv'):
    """Return a list ('employee') of dict objects, taken from a Group Directory CSV file."""
    gd_extract = csv.DictReader(open(input_file), dialect='excel')
    employees = []
    for row in gd_extract:
        employees.append(row)
    return employees

def write_gd_formatted(employees_dict, output_file="gd_formatted.csv"):
    """Read in an Employees() object, and write out each Employee() inside this to a CSV file"""
    gd_output_fieldnames = ('hrid', 'mail', 'givenName', 'sn', 'dbcostcenter', 'dbdirid', 'hrreportsto', 'PHFull', 'PHFull_message', 'SupervisorEmail', 'SupervisorFirstName', 'SupervisorSurname')
    try:
        gd_formatted = csv.DictWriter(open(output_file, 'w', newline=''), fieldnames=gd_output_fieldnames, extrasaction='ignore', dialect='excel')
    except IOError:
        print('Unable to open file, IO error (Is it locked?)')
        sys.exit(1)

    headers = {n:n for n in gd_output_fieldnames}
    gd_formatted.writerow(headers)
    for employee in employees_dict.employee_list:
        # We're using the employee object's inbuilt __dict__ attribute - hmm, is this good practice?
        gd_formatted.writerow(employee.__dict__)

class Employee:
    """An Employee in the system, with employee attributes (name, email, cost-centre etc.)"""
    def __init__(self, employee_attributes):
        """We use the Employee constructor to convert a dictionary into instance attributes."""
        for k, v in employee_attributes.items():
            setattr(self, k, v)

    def clean_phone_number(self):
        """Perform some rudimentary checks and corrections, to make sure numbers are in the right format.
        Numbers should be in the form 0XYYYYYYYY, where X is the area code, and Y is the local number."""
        if self.telephoneNumber is None or self.telephoneNumber == '':
            return '', 'Missing phone number.'
        else:
            standard_format = re.compile(r'^\+(?P<intl_prefix>\d{2})\((?P<area_code>\d)\)(?P<local_first_half>\d{4})-(?P<local_second_half>\d{4})')
            extra_zero = re.compile(r'^\+(?P<intl_prefix>\d{2})\(0(?P<area_code>\d)\)(?P<local_first_half>\d{4})-(?P<local_second_half>\d{4})')
            missing_hyphen = re.compile(r'^\+(?P<intl_prefix>\d{2})\(0(?P<area_code>\d)\)(?P<local_first_half>\d{4})(?P<local_second_half>\d{4})')
            if standard_format.search(self.telephoneNumber):
                result = standard_format.search(self.telephoneNumber)
                return '0' + result.group('area_code') + result.group('local_first_half') + result.group('local_second_half'), ''
            elif extra_zero.search(self.telephoneNumber):
                result = extra_zero.search(self.telephoneNumber)
                return '0' + result.group('area_code') + result.group('local_first_half') + result.group('local_second_half'), 'Extra zero in area code - ask user to remediate. '
            elif missing_hyphen.search(self.telephoneNumber):
                result = missing_hyphen.search(self.telephoneNumber)
                return '0' + result.group('area_code') + result.group('local_first_half') + result.group('local_second_half'), 'Missing hyphen in local component - ask user to remediate. '
            else:
                return '', "Number didn't match recognised format. Original text is: " + self.telephoneNumber

class Employees:
    def __init__(self, import_list):
        self.employee_list = []    
        for employee in import_list:
            self.employee_list.append(Employee(employee))

    def clean_all_phone_numbers(self):
        for employee in self.employee_list:
            #Should we just set this directly in Employee.clean_phone_number() instead?
            employee.PHFull, employee.PHFull_message = employee.clean_phone_number()

    # Hmm, the search is O(n^2) - there's probably a better way of doing this search?
    def lookup_all_supervisors(self):
        for employee in self.employee_list:
            if employee.hrreportsto is not None and employee.hrreportsto != '':
                for supervisor in self.employee_list:
                    if supervisor.hrid == employee.hrreportsto:
                        (employee.SupervisorEmail, employee.SupervisorFirstName, employee.SupervisorSurname) = supervisor.mail, supervisor.givenName, supervisor.sn
                        break
                else:
                    (employee.SupervisorEmail, employee.SupervisorFirstName, employee.SupervisorSurname) = ('Supervisor not found.', 'Supervisor not found.', 'Supervisor not found.')
            else:
                (employee.SupervisorEmail, employee.SupervisorFirstName, employee.SupervisorSurname) = ('Supervisor not set.', 'Supervisor not set.', 'Supervisor not set.')

    #Is thre a more pythonic way of doing this?
    def print_employees(self):
        for employee in self.employee_list:
            print(employee.__dict__)

if __name__ == '__main__':
    db_employees = Employees(import_gd_dump())
    db_employees.clean_all_phone_numbers()
    db_employees.lookup_all_supervisors()
    #db_employees.print_employees()
    write_gd_formatted(db_employees)

Во-первых, мой вопрос в преамбуле звучит так:Можете ли вы увидеть что-то не так по сути с вышесказанным, с точки зрения дизайна класса или точки зрения Python?Является ли логика / дизайн звуком?

В любом случае, к конкретике:

  1. У объекта Employees есть метод clean_all_phone_numbers(), который вызывает clean_phone_number() для каждого Employee объект внутри него.Это плохой дизайн?Если так, то почему?Кроме того, способ, которым я вызываю lookup_all_supervisors(), плохой?
  2. Изначально я обернул методы clean_phone_number() и lookup_supervisor() в одну функцию с одним циклом for внутри.clean_phone_number равно O (n), я полагаю, lookup_supervisor равно O (n ^ 2) - нормально ли разбивать его на два цикла, как это?
  3. В clean_all_phone_numbers(), я зацикливаюсь на Employeeобъекты, и устанавливают их значения с помощью возврата / назначения - я должен установить это внутри самого clean_phone_number()?

Есть также несколько вещей, которые я вроде как взломал, не уверен, что они 'Это плохая практика - например, print_employee() и gd_formatted() оба используют __dict__, а конструктор для Employee использует setattr() для преобразования словаря в атрибуты экземпляра.

Я бы оценил любые мысли ввсе.Если вы считаете, что вопросы слишком широкие, дайте мне знать, и я могу опубликовать несколько разделенных сообщений (я просто не хотел загрязнять доски несколькими похожими вопросами, и эти три вопроса более или менее достаточно тесно связаны).

Ура, Виктор

Ответы [ 2 ]

3 голосов
/ 03 июня 2010

выглядит хорошо для меня. Хорошая работа. Как часто вы собираетесь запускать этот скрипт? Большинство ваших вопросов спорные, если это разовая вещь.

  1. Мне нравится, как Employees.cleen_all_phone_numbers() делегаты на Employee.clean_phone_number()
  2. Вы действительно должны использовать индекс (словарь) здесь. Вы можете индексировать каждого сотрудника по hrid, когда создаете их в O(n), а затем искать в O(1).
    • Но делайте это, только если вам когда-нибудь придется снова запускать скрипт ...
    • Просто привыкните пользоваться словарями. Они безболезненны и облегчают чтение кода. Всякий раз, когда вы пишете метод lookup_*, вы, вероятно, просто хотите проиндексировать словарь.
  3. не уверен. Мне нравится явно устанавливать состояние, но на самом деле это плохой дизайн - clean_phone_number() должен это делать, сотрудники должны нести ответственность за свое состояние.
2 голосов
/ 03 июня 2010

вы должны закрыть ваши файлы после прочтения их Я предлагаю перенести все скомпилированные данные на верхний уровень (иначе вы компилируете их при каждом вызове) если self.telephoneNumber равен None или self.telephoneNumber == '': может быть легко переписан, как если бы не self.telephoneNumber

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...