Ejemplo n.º 1
0
def tests_for_hw5_solution1():
    u"""Тесты задачи 1"""
    petroff = Person("Petrov", "Petro", "1952-01-02")
    ivanoff = Person("Ivanov", "Ivan", "2000-10-20")
    sydoroff = Person("Sidorov", "S***n", "1980-12-31", "Senya")
    kozloff = Person("Kozlov", "Viktor", "1980-11-22", "Vitek")

    assert "first_name" in dir(petroff)
    assert "get_fullname" in dir(ivanoff)
    assert "nickname" not in dir(petroff)
    assert "nickname" in dir(sydoroff)

    assert petroff.surname == "Petrov"
    assert petroff.first_name == "Petro"
    assert petroff.get_fullname() == "Petrov Petro"
    assert sydoroff.nickname == "Senya"

    assert petroff.birth_date == datetime.date(1952, 01, 02)
    assert isinstance(petroff.birth_date, datetime.date)
    assert petroff.get_age() == "62"

    assert sydoroff.birth_date == datetime.date(1980, 12, 31)
    assert sydoroff.get_age() == "33"

    assert kozloff.get_age() == "33"


    print 'All is Ok!'
Ejemplo n.º 2
0
def runner():
    u"""Запускает выполнение всех задач"""
    sydoroff = Person("Sidorov", "S***n", "1980-12-31", "Senya")
    print sydoroff.get_fullname()
    print sydoroff.nickname
    print sydoroff.get_age()

    petroff = Person("Petrov", "Petro", "1952-01-02")
    print petroff.get_fullname()
    print petroff.get_age()
Ejemplo n.º 3
0
def modifier(filename):

    f = open(filename, 'r+')
    reader = csv.DictReader(f)

    # read
    rows = []
    for row in reader:
        rows.append(row)

    f.seek(0)

    # add age and full name
    for row in rows:
        p = Person(row['surname'], row['name'], row['birthdate'])
        row['age'] = p.get_age()
        row['full_name'] = p.get_fullname()

    # write
    writer = csv.DictWriter(
        f,
        ['id', 'surname', 'name', 'full_name', 'birthdate', 'nickname', 'age'])
    writer.writeheader()
    for row in rows:
        writer.writerow(row)

    f.close()
Ejemplo n.º 4
0
def tests_for_hw5_solution1():
    u"""Тесты задачи 1"""
    petroff = Person("Petrov", "Petro", "1952-11-12")
    ivanoff = Person("Ivanov", "Ivan", "2000-10-20")
    sydoroff = Person("Sidorov", "S***n", "1980-12-31", "Senya")

    assert "first_name" in dir(petroff)
    assert "get_fullname" in dir(ivanoff)
    assert "nickname" not in dir(petroff)
    assert "nickname" in dir(sydoroff)

    assert petroff.surname == "Petrov"
    assert petroff.first_name == "Petro"
    assert petroff.get_fullname() == "Petrov Petro"
    assert sydoroff.nickname == "Senya"

    assert petroff.birth_date == datetime.date(1952, 11, 12)
    assert isinstance(petroff.birth_date, datetime.date)
    assert petroff.get_age() == "65"
Ejemplo n.º 5
0
def runner():
    u"""Запускает выполнение всех задач"""
    input_data =("Sidorov", "S***n", "1980-12-31", "Senya")
    print input_data

    if len(input_data) == 3:
    	nicname = ''
    else:
    	nicname = input_data[3]

    result = Person(input_data[0], input_data[1], input_data[2], nicname)

    print result.birth_date

    print result.get_age()
    print result.get_fullname()
Ejemplo n.º 6
0
def modifier(filename):
    u"""Получает строку c адресом файла и изменяет согласно условиям задачи."""
    with open(filename, 'r') as csvfile:
        data = []
        index_fullname = 0

        for i in csv.reader(csvfile):
            if not i[0].isdigit():
                index_fullname = i.index('name') + 1
                i.insert(index_fullname, 'fullname')
                i.append('age')
            else:
                p = Person(*i[1:])
                i.insert(index_fullname, p.get_fullname())
                i.append(p.get_age())
            data.append(i)

    with open(filename, 'w') as csvfile:
        wdata = csv.writer(csvfile)
        wdata.writerows(data)
Ejemplo n.º 7
0
def modifier(filename):
    '''
    Modifies original csv file with person data
    :param filename: str
    '''
    with open(filename, "r") as data_file:
        data = csv.reader(data_file)
        header = data.next()
        persons = [Person(*row[1:]) for row in data]
    header.insert(header.index('name') + 1, 'fullname')
    header.append('age')

    with open(filename, 'w') as data_file:
        data = csv.writer(data_file)
        data.writerow(header)
        for id, person in enumerate(persons, start=1):
            data.writerow([
                id, person.surname, person.first_name,
                person.get_fullname(), person.birth_date,
                getattr(person, 'nickname', ''),
                person.get_age()
            ])
Ejemplo n.º 8
0
def modifier(file):
    """
     This function accepts a csv file and modifies it according to the task:
    creates Person classes, adds fullname and age columns
    """
    fp = open(file, "r")
    csv_reader = csv.reader(fp)
    new = []
    for i in csv_reader:
        if i[0] == "id":
            index = i.index('name') + 1
            i.insert(index, "fullname")
            i.append("age")
        else:
            person = Person(*i[1:])
            i.insert(index, person.get_fullname())
            i.append(person.get_age())
        new.append(i)

    fw = open(file, "w")
    csv_writer = csv.writer(fw)
    for i in new:
        csv_writer.writerow(i)
Ejemplo n.º 9
0
def modifier(filename):
    u"""Функция получает название файла и изменяет его согласно условию."""
    with open(filename) as csvfile:
        data = csv.reader(csvfile)
        header = data.next()
        persons = []
        for row in data:
            person = Person(*row[1:])
            persons.append(person)
        header.insert(3, "fullname")
        header.append("age")

    with open(filename, "w") as csvfile:
        data = csv.writer(csvfile)
        data.writerow(header)
        count = 1
        for item in persons:
            data.writerow([
                count, item.surname, item.first_name,
                item.get_fullname(), item.birth_date, item.nickname,
                item.get_age()
            ])
            count = count + 1
Ejemplo n.º 10
0
def runner():
    u"""Запускает выполнение всех задач"""
    sydoroff = Person("Sidorov", "S***n", "1980-12-31", "Senya")
    print sydoroff