0% found this document useful (0 votes)
929 views2 pages

Start Date

This Python script allows a user to input a start date and then prints out all employees from a CSV file that started on that date or the closest following date. It retrieves employee records from a remote CSV file, parses the start dates, sorts the records, and iterates through them to find the earliest matching or later date and associated employees. It then continues iterating to print out each subsequent matching date and employees.

Uploaded by

Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
929 views2 pages

Start Date

This Python script allows a user to input a start date and then prints out all employees from a CSV file that started on that date or the closest following date. It retrieves employee records from a remote CSV file, parses the start dates, sorts the records, and iterates through them to find the earliest matching or later date and associated employees. It then continues iterating to print out each subsequent matching date and employees.

Uploaded by

Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Here is the same:

start_date_report:

#!/usr/bin/env python3
import csv
import datetime
import requests

FILE_URL="https://raw.githubusercontent.com/google/it-cert-automation-
practice/master/Course4/Lab4/employees-with-date.csv"

def get_start_date():
"""Interactively get the start date to query for."""

print()
print('Getting the first start date to query for.')
print()
print('The date must be greater than Jan 1st, 2018')
year = input('Enter a value for the year: ')
month = input('Enter a value for the month: ')
day = input('Enter a value for the day: ')
print()

return datetime.datetime(int(year), int(month), int(day))

def get_file_lines(url):
"""Returns the lines contained in the file at the given URL"""

# Download the file over the internet


response = requests.get(url, stream=True)

# Decode all lines into strings


lines = []
for line in response.iter_lines():
lines.append(line.decode("UTF-8"))
return lines

my_data = get_file_lines(FILE_URL)

def get_same_or_newer(start_date):
"""Returns the employees that started on the given date, or the closest one."""
data = get_file_lines(FILE_URL)
reader = csv.reader(data[1:])
lista = list(reader)
lista.sort(key=lambda x: x[3])

# We want all employees that started at the same date or the closest newer
# date. To calculate that, we go through all the data and find the
# employees that started on the smallest date that's equal or bigger than
# the given start date.
min_date = ()
min_date_employees = []
for row in reader:
row_date = datetime.datetime.strptime(row[3], '%Y-%m-%d')

# If this date is smaller than the one we're looking for,


# we skip this row
if row_date < start_date:
continue

# If this date is smaller than the current minimum,


# we pick it as the new minimum, resetting the list of
# employees at the minimal date.
if row_date < min_date:
min_date = row_date
min_date_employees = []

# If this date is the same as the current minimum,


# we add the employee in this row to the list of
# employees at the minimal date.
if row_date == min_date:
min_date_employees.append("{} {}".format(row[0], row[1]))

return min_date, min_date_employees

def list_newer(start_date):
while start_date < ():
start_date, employees = get_same_or_newer(start_date)
print("Started on {}: {}".format(start_date.strftime("%b %d, %Y"),
employees))

# Now move the date to the next one


start_date = start_date + datetime.timedelta(days=1)
def main():
start_date = get_start_date()
list_newer(start_date)

if __name__ == "__main__":
main()
Type a message here...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy