Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
3.0 kB
1
Indexable
Never
import csv


def insert_person(person_file, first_name, last_name):
    if person_file == "" or first_name == "" or last_name == "":
        return None
    with open(person_file, 'r+', newline='\n') as f:
        reader = csv.DictReader(f)
        writer = csv.DictWriter(f, reader.fieldnames)
        for line in reader:
            last_index = line["Index"]
        last_index = int(last_index) + 1
        line_out1 = {"Index": last_index, "FirstName": first_name, "LastName": last_name}
        writer.writerow(line_out1)
        return last_index


def insert_colleague(person_file,
                     colleagues_file,
                     first_name,
                     last_name,
                     dept,
                     extension):
    if person_file == "" or first_name == "" or last_name == "" \
            or colleagues_file == "" or dept == "" or extension == "":
        return None
    index = insert_person(person_file, first_name, last_name)
    with open(colleagues_file, 'a', newline='') as f:
        writer = csv.writer(f)
        writer.writerow([index, dept, extension])
        return index


def insert_friend(person_file,
                  friends_file,
                  first_name,
                  last_name,
                  street,
                  city,
                  country,
                  phone):
    if person_file == "" or first_name == "" or last_name == "" \
            or friends_file == "" or street == "" or city == "" or country == "" or phone == "":
        return None
    index = insert_person(person_file, first_name, last_name)
    with open(friends_file, 'a', newline='') as f:
        writer = csv.writer(f)
        writer.writerow([index, street, city, country, phone])
        return index


def update_field(data_file, target, field_name, field_value):
    with open(data_file, 'r') as f_in:
        reader = csv.DictReader(f_in)
        fieldnames = reader.fieldnames
        if field_name not in fieldnames:
            return None
        text = list(reader)
        for i in text:
            if int(i['Index']) == target:
                i[field_name] = field_value
                with open(data_file, 'w', newline='') as f_out:
                    writer = csv.DictWriter(f_out, fieldnames)
                    writer.writeheader()
                    writer.writerows(text)
                return True
    return None


def delete_person(data_file, target):
    with open(data_file, 'r') as f_in:
        reader = csv.DictReader(f_in)
        fieldnames = reader.fieldnames
        text = list(reader)
        for k in text:
            if int(k['Index']) == target:
                text.remove(k)
                with open(data_file, 'w', newline='') as f_out:
                    writer = csv.DictWriter(f_out, fieldnames)
                    writer.writeheader()
                    writer.writerows(text)
                    return True
        return None