-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingToFile.py
248 lines (202 loc) · 7.09 KB
/
LoggingToFile.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /usr/bin/env/python
import sys
import json
import datetime
import glob
import logging
import logging.handlers
from collections import defaultdict
# https://pymotw.com/2/logging/
LOG_FILENAME = 'logging_rotatingfile_example.out'
LEVELS = { 'debug':logging.DEBUG,
'info':logging.INFO,
'warning':logging.WARNING,
'error':logging.ERROR,
'critical':logging.CRITICAL,
}
# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
# Set log level from user
if len(sys.argv) > 1:
level_name = sys.argv[1]
level = LEVELS.get(level_name, logging.NOTSET)
my_logger.setLevel(level)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME,
maxBytes=50,
backupCount=5,
)
my_logger.addHandler(handler)
#-------------------------------------------------------------------------------------------
# getInputList(myList):
# Get Input Numbers to a list
#-------------------------------------------------------------------------------------------
# Main Function
def getInputList(myList):
my_logger.debug('getInputList')
# Get a list of numbers until the user enters a blank line
line = input("Enter list of numbers: \n")
my_logger.debug('getInputList')
#while (line not in ['\n', '\r\n']):
while (line):
my_logger.debug('getInputList')
myList.append(int(line))
line = input()
if (line in ('\n', '\r\n')):
break
#-------------------------------------------------------------------------------------------
# printList(myList)
# Function to print a list
# Print a list using three methods
#-------------------------------------------------------------------------------------------
def printList(myList):
# METHOD 1
print(myList)
# METHOD 2
for elmt in myList:
# Extra comma at end makes sure that it gets printed on the same line
print(elmt, ', ', end=''),
print()
my_logger.debug('printList')
# METHOD 3
for index in range(len(myList)):
# Extra comma at end makes sure that it gets printed on the same line
print(myList[index], ', ', end=''),
print()
#-------------------------------------------------------------------------------------------
# sumOfNum(myList)
# Function to calculate the sum of numbers
#-------------------------------------------------------------------------------------------
def sumOfNum(myList):
my_logger.debug('sumOfNum')
sumList = 0
for elmt in myList:
sumList += elmt
return sumList
#-------------------------------------------------------------------------------------------
# sort(myList)
# Function to sort a list
#-------------------------------------------------------------------------------------------
def sortList(myList):
my_logger.warn('sortList')
# Copy a list
tmpList = myList[:]
tmpList.sort()
print("Tmp List")
print(tmpList)
#-------------------------------------------------------------------------------------------
# findFirstRepeatedChar(myStr):
# Find First Repeated Char
#-------------------------------------------------------------------------------------------
def findFirstRepeatedChar(myStr):
strDict = {}
repChar = ''
my_logger.error('findFirstRepeatedChar')
for c in myStr:
if (c in strDict):
repChar = c
break;
else:
strDict[c] = 1
if (repChar):
print("First Repeated Char in \"%s\" : %c" % (myStr, repChar))
else:
print("No Repeated Char in \"%s\"" % myStr)
#-------------------------------------------------------------------------------------
# Utility function to print lines in Json format
#-------------------------------------------------------------------------------------
def get_pretty_print(json_object):
my_logger.critical('get_pretty_print')
return json.dumps(json_object, sort_keys=False, indent=4, separators=(',', ': '))
#-------------------------------------------------------------------------------------------
# Main Function
#-------------------------------------------------------------------------------------------
def main():
print("hello")
# Declare a list
myList = []
# Fill the list with Numbers
getInputList(myList)
# Print the List
printList(myList)
# Calculate the Sum of Numbers in the List
sumList = sumOfNum(myList)
# Problem 1
# Use the inbuilt sum function to get the sum
print(sum(myList))
print(sumList)
# Problem 2
# Find First Repeated Character in a String
myStr = "helola"
repChar = findFirstRepeatedChar(myStr)
# Problem 3
# Sort a list using Builtin method
numsList = [3, 5, 2, 1, 8, 4]
sortList(numsList)
print("Original List")
print(numsList)
# Problem 4
# Adding Variables to list and adding List to Dict
transType = "debit"
desc = "restaurant"
accNo = 123
accRo = 56
amt = 9877
myList = [desc, accNo, accRo, amt]
myDict1 = {}
myDict1["Desc"] = desc
myDict1["AccNo"] = accNo
myDict1["AccRo"] = accRo
myDict1["Amount"] = amt
transType2 = "debit"
desc2 = "games"
accNo2 = 100
accRo2 = 56
amt2 = 1000
myList2 = [desc2, accNo2, accRo2, amt2]
myDict2 = {}
myDict2["Desc"] = desc2
myDict2["AccNo"] = accNo2
myDict2["AccRo"] = accRo2
myDict2["Amount"] = amt2
#myDictFinal = {}
#myDictFinal.setdefault(transType, []).append(myList)
#myDictFinal.setdefault(transType2, []).append(myList2)
myDictFinal = {}
myDictFinal.setdefault(transType, []).append(myDict1)
myDictFinal.setdefault(transType2, []).append(myDict2)
#myDict2 = defaultdict(myList)
#myDict2[transType2].append(myList2)
#print(myList)
#print(myList)
print(myDict1)
print(myDict2)
print(myDictFinal)
print(get_pretty_print(myDictFinal))
# Problem 5
# Convert mm/dd/yy to YYYY-MM-DD
print(datetime.datetime.strptime("21/12/2008", "%d/%m/%Y").strftime("%Y-%m-%d"))
#transDate = "9/9/16"
transDate = "09/29/2016"
dateMonth = transDate.split('/')[0]
dateDay = transDate.split('/')[1]
dateYear = transDate.split('/')[2]
# Assumption that the year will be 2000 plus
if len(dateMonth) == 1:
dateMonth = '0' + dateMonth
if len(dateDay) == 1:
dateDay = '0' + dateDay
if len(dateYear) == 2:
# If the year is between 50 and 99, then it is likely to be 1950 - 1999
if (50 <= int(dateYear) >= 99):
dateYear = str(int(dateYear) + 1900)
else:
dateYear = str(int(dateYear) + 2000)
newDate = dateYear + '-' + dateMonth + '-' + dateDay
print(transDate)
print(newDate)
#-------------------------------------------------------------------------------------------
# Start Main
#-------------------------------------------------------------------------------------------
if __name__ == "__main__":
main()