-
Notifications
You must be signed in to change notification settings - Fork 0
/
headerRelated.py
77 lines (57 loc) · 2.39 KB
/
headerRelated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#-*- coding: utf-8 -*-
# 2015-2016 Fundamentos de Programação (FP)
# Grupo 12
# 43071 Ana Patrícia Dos Santos Abrantes
# 43134 Luís Filipe Leal Campos
# module with functions related to the header of files
from constants import *
from timeTT import changeFormatTime
def removeHeader(file):
"""Removes the header of any file (drivers, vehicles, reservations or services)
Requires:
file is of type file, containing a list of drivers, vehicles, reservations
or services organized as in the examples provided in the general
specification (omitted here for the sake of readability).
Ensures:
list where each element corresponds to each line after the header,
that is, each element corresponds to information about a driver, vehicle,
reservation or service
"""
return file.readlines()[NUMBEROfLinesInHeader:]
def getHeader(fileName):
"""Gets the header of a file.
Requires:
fileName is string with name of the file from which we want to get the header.
This file is organized as in the examples provided in the general
specification (omitted here for the sake of readability).
Ensures:
a list where each element corresponds to a header's line of the file
with the name fileName.
"""
file = open(fileName, 'r')
header = file.readlines()[:NUMBEROfLinesInHeader]
file.close()
return header
def createNewHeader(fileName, new_period):
"""Creates a new header, with the new period.
Requires:
fileName is string with name of the file from which we want to get the header.
This file is organized as in the examples provided in the general
specification (omitted here for the sake of readability)
new_period is a string corresponding to the next period,
in the format HHHH (see general specifications).
Ensures:
a string with the header of the file named fileName,
but in which the line corresponding to the period is
substitued for the period in new_period.
"""
# Changes the new_period format to the one used in the files
new_period = changeFormatTime(new_period)
header = getHeader(fileName)
header[INDEXPeriod] = new_period
# Turns header into string, each line separated by commas. To understand the
# use of commas, see outputStatus.writeServicesFile
header = ','.join(header)
# Deletes newlines
header = header.replace('\n', '')
return header