The Employee Class Record Revisited

vegaseat 3 Tallied Votes 363 Views Share

Just a few more explorations using a Python class to mimic a C Structure or Pascal Record. Loading the record from a csv type data file, displaying the data and sorting and searching the data in various ways.

Ene Uran commented: helpful +12
'''Class_StructEmp2_file.py
mimic a C Structure or Pascal Record using a class
load the data from a csv type file like this:

John Q. Johnson,computer research,3500
Loyd Tetris,Human Resources,6000
Mark Marksman,external development,4800

tested with Python27 and Python32  by  vegaseat
'''

class Employee():
    """
    mimics a C Structure or Pascal Record
    """
    def __init__(self, name, dept, salary):
        self.name = name
        self.dept = dept
        self.salary = salary

def by_last_name(record):
    """
    helper function to sort by last name
    assume names are "first middle last"
    """
    return record.name.split()[-1]

def by_department(record):
    """
    helper function to sort by department case-insensitive
    """
    return record.dept.lower()

# load the data file
fname = "employee_data.txt"
with open(fname, "r") as fin:
    employee_data = fin.readlines()

record_list = []
for line in employee_data:
    line = line.rstrip()
    #print(line, type(line), line.split(','))  # test
    name, dept, salary = line.split(',')
    #print(name, dept, salary)  # test
    record_list.append(Employee(name, dept, salary))

print("Employee names:")

# explore the record_list by instance
for emp in record_list:
    print(emp.name)

print('-'*35)
print("Employee names sorted by last name:")

# sort the records by last name using a helper function
for emp in sorted(record_list, key=by_last_name):
    print(emp.name)

print('-'*35)
print("High paid employees:")

# list the folks that get more than $4000 salary per month
for emp in record_list:
    if float(emp.salary) > 4000:
        print("%s gets more then $4000 a month" % emp.name)

print('-'*35)
print("Sorted by department:")

# sort the records by case insensitive department name
for emp in sorted(record_list, key=by_department):
    print("%s works in %s" % (emp.name, emp.dept))

'''result -->
Employee names:
John Q. Johnson
Loyd Tetris
Mark Marksman
-----------------------------------
Employee names sorted by last name:
John Q. Johnson
Mark Marksman
Loyd Tetris
-----------------------------------
High paid employees:
Loyd Tetris gets more then $4000 a month
Mark Marksman gets more then $4000 a month
-----------------------------------
Sorted by department:
John Q. Johnson works in computer research
Mark Marksman works in external development
Loyd Tetris works in Human Resources
'''
Lardmeister 461 Posting Virtuoso

Easy to convert to a music album manager.

hughesadam_87 54 Junior Poster

I'm working on a package for the management and easy creation of mutable and immutable records. The idea is that we can either use object subclasses (mutable) or modified named tuples (immutable) as well as a bunch of very handy utilities/functions to create a record storage class in python. The page I made is really crappy and not put together yet but I will update it soon.

Source

Info

I know this API is horribly documented, and that's because sphinx generates the page automatically from the source files that have shabby doc strings. I'll update this soon to actually sound legitimate and also with more examples of input output.

I'd really like to add more functions to this package that are like the methods you added here. I made some general functions that do things like sort records by argument, filter etc... and I want to expand it. I'll try to update the pyrecords site soon to better reflect the current state of the programs, but since there's so many good code snippets here about records I REALLY hope you guys can help contribute (even if just knowledge). My email address is hughesadam87@gmail.com

Anyone interested in talking more, please let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.