Development Exercises – L9.1

Created by Ramses Alexander Coraspe Valdez

Created on April 20, 2020

In [0]:
import csv
import math
import random
import timeit as tiempo
import pandas as pd
import logging
In [0]:
#Creating and configuring logger

log_format = "%(asctime)s::%(levelname)s::%(name)s::"\
              "%(filename)s::%(lineno)d::%(message)s"
logger = logging.getLogger(__name__)

# #will be accepted all the logs from the level debug to above, INFO and ERROR are included
logger.setLevel('DEBUG')

file_handler = logging.FileHandler("filelogs.log")
stream_handler = logging.StreamHandler()

formatter = logging.Formatter(log_format)
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)

# Don't forget to add the file handler
logger.addHandler(file_handler)
logger.addHandler(stream_handler)

Development Exercises – L2

In [0]:
class directory:
    def __init__(self, n, a, p, e):
        self.Name= n
        self.Address = a
        self.Phone= p
        self.Email = e  
In [0]:
class manage_directory:
    def __init__(self):
        self.myList = [];

    def add(self, e):
        try:
            self.myList.append(e)
            logger.info("Added to the csv: " + e.Name + " " + e.Address + " " + e.Phone + " " + e.Email )
        except Exception as ex:
            logger.error(str(ex))        

    def remove(self, i):
        try:
            self.myList.pop(i);
            logger.info("Removd Item : " + str(i))
        except Exception as ex:
            logger.error(str(ex))

    def partition(self,arr,low,high): 
        i = ( low-1 )    
        pivot = arr[high] 
    
        for j in range(low , high):                 
            if   arr[j] < pivot:                             
                i = i+1 
                arr[i],arr[j] = arr[j],arr[i] 
    
        arr[i+1],arr[high] = arr[high],arr[i+1] 
        return ( i+1 )                

    def quickSort(self, arr,low,high): 
        if low < high: 
            pi = self.partition(arr,low,high)     
            self.quickSort(arr, low, pi-1) 
            self.quickSort(arr, pi+1, high)        

    def get_sorted(self):
        self.quickSort(self.myList,0,len(self.myList)-1);

    def merge(self, l, d):
        if(d=='p'):
            self.myList= self.myList + l;
        else:
            self.myList=  l + self.myList;
    
    def getList(self):
        return self.myList;              

    def saveToTextFile(self, filename):
        with open(filename, "w", newline='') as outfile:
              for r in self.myList:
                  outfile.write(str(r) + "\n")                

    def saveToCSVFile(self, filename):
        try:
            with open(filename, 'w',newline='') as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(['Name', 'Address', 'Phone', 'Email'])
                for r in self.myList:
                    writer.writerow([r.Name, r.Address, r.Phone, r.Email])  
            logger.info("List saved to the csv")                                      
        except Exception as ex:
            logger.error(str(ex))
    
    def readFromCSVFile (self, filename):
        try:
            with open(filename) as f:
                reader = csv.reader(f)
                data = [r for r in reader]
                data.pop(0);
                self.myList = data;
            logger.info("Reading data from file correctly")   
        except Exception as ex:
            logger.error(str(ex))

        
    def readFromTextFile(self, filename):
        return open(filename).readlines()

    def searchInList(self, t):
        try:
            match=[];
            for r in self.myList:
                m = [s for s in r if (t in s)];
                if(len(m)>0):
                    match.append(r);
            logger.info("String " + t + " founded")
            return match; 
        except Exception as ex:
            logger.error(str(ex))         
In [0]:
li= manage_directory();

li.add(directory("Ramses", "Jalisco", "555555", "test@gmail.com"));
li.add(directory("Gerardo", "Jalisco", "555555", "gerardo@gmail.com"));

li.saveToCSVFile("file.csv")
li.readFromCSVFile("file.csv");

print(li.getList());
print(li.searchInList("Ram"))
print(li.searchInList("Ger"))
2020-04-22 23:47:54,047::INFO::__main__::<ipython-input-4-5ada137640a5>::8::Added to the csv: Ramses Jalisco 555555 test@gmail.com
2020-04-22 23:47:54,048::INFO::__main__::<ipython-input-4-5ada137640a5>::8::Added to the csv: Gerardo Jalisco 555555 gerardo@gmail.com
2020-04-22 23:47:54,049::INFO::__main__::<ipython-input-4-5ada137640a5>::61::List saved to the csv
2020-04-22 23:47:54,050::INFO::__main__::<ipython-input-4-5ada137640a5>::72::Reading data from file correctly
2020-04-22 23:47:54,051::INFO::__main__::<ipython-input-4-5ada137640a5>::87::String Ram founded
2020-04-22 23:47:54,053::INFO::__main__::<ipython-input-4-5ada137640a5>::87::String Ger founded
[['Ramses', 'Jalisco', '555555', 'test@gmail.com'], ['Gerardo', 'Jalisco', '555555', 'gerardo@gmail.com']]
[['Ramses', 'Jalisco', '555555', 'test@gmail.com']]
[['Gerardo', 'Jalisco', '555555', 'gerardo@gmail.com']]

Development Exercises – L5.1

In [0]:
class SortingAlgorithms:

        def __init__(self):
            self.fileinputpath = None
            self.fileoutputpath = None
            self.number_records = 0
            self.time_consumed = 0
            self.start_time = 0
            self.end_time = 0
            self.values = []
            self.outvalues = []

        def set_input_data(self, file_path_name):
            self.fileinputpath = file_path_name
            self.__set_values()

        def __set_values(self):
            try:
                self.values = []
                with open(self.fileinputpath) as f:
                    data = [list(map(int, rec))
                            for rec in csv.reader(f, delimiter=',')]
                    self.values = data[0]
                    self.number_records = len(self.values)
                logger.info("fileinputpath setted") 
            except Exception as ex:
                logger.error(str(ex))
                raise ex

        def get_values(self):
            return self.values

        def set_output_data(self, file_path_name):
            self.fileoutputpath = file_path_name
            self.__set_out_values()

        def __set_out_values(self):
            try:
                with open(self.fileoutputpath, 'w', newline='') as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(self.outvalues)
                logger.info("fileinputpath out setted")
            except Exception as ex:
                logger.error(str(ex))
                raise ex

        def __execute_merge_sort(self, a):
            if len(a) > 1:
                middle = len(a)//2
                l = a[:middle]
                r = a[middle:]
                self.__execute_merge_sort(l)
                self.__execute_merge_sort(r)
                x = y = z = 0
                while x < len(l) and y < len(r):
                    if l[x] < r[y]:
                        a[z] = l[x]
                        x += 1
                    else:
                        a[z] = r[y]
                        y += 1
                    z += 1
                while x < len(l):
                    a[z] = l[x]
                    x += 1
                    z += 1
                while y < len(r):
                    a[z] = r[y]
                    y += 1
                    z += 1

        def execute_merge_sort(self):
            self.start_time = tiempo.default_timer()
            self.outvalues = self.values
            self.__execute_merge_sort(self.outvalues)
            self.end_time = tiempo.default_timer()
            self.time_consumed = format(self.end_time-self.start_time, '.8f')

        def __heapify(self, a, n, i):
            largest = i
            l = 2 * i + 1
            r = 2 * i + 2
            if l < n and a[i] < a[l]:
                largest = l
            if r < n and a[largest] < a[r]:
                largest = r
            if largest != i:
                a[i], a[largest] = a[largest], a[i]
                self.__heapify(a, n, largest)

        def __execute_heap_sort(self, a):
            n = len(a)
            for i in range(n, -1, -1):
                self.__heapify(a, n, i)
            for i in range(n-1, 0, -1):
                a[i], a[0] = a[0], a[i]
                self.__heapify(a, i, 0)

        def execute_heap_sort(self):
            self.start_time = tiempo.default_timer()
            self.outvalues = self.values
            self.__execute_heap_sort(self.outvalues)
            self.end_time = tiempo.default_timer()
            self.time_consumed = format(self.end_time-self.start_time, '.8f')

        def __partition(self, a, low, high):
            i = (low-1)
            pivot = a[high]
            for j in range(low, high):
                if a[j] < pivot:
                    i = i+1
                    a[i], a[j] = a[j], a[i]
            a[i+1], a[high] = a[high], a[i+1]
            return (i+1)

        def __execute_quick_sort(self, a, low, high):
            if low < high:
                pi = self.__partition(a, low, high)
                self.__execute_quick_sort(a, low, pi-1)
                self.__execute_quick_sort(a, pi+1, high)

        def execute_quick_sort(self):
            self.start_time = tiempo.default_timer()
            self.outvalues = self.values
            self.__execute_quick_sort(self.outvalues, 0, len(self.outvalues)-1)
            self.end_time = tiempo.default_timer()
            self.time_consumed = format(self.end_time-self.start_time, '.8f')

        def get_performance_data(self):
            return self.number_records, self.time_consumed, self.start_time, self.end_time

        def get_overall_stats(self):
            files = ["random_numbers_1", "random_numbers_2",
                     "random_numbers_3", "random_numbers_4",
                     "random_numbers_5"]
            Algorithms = ["Merge", "Heap", "Quick"]
            fl = []
            nr = []
            alg = []
            tc = []

            i = 0
            for a in Algorithms:
                for f in files:
                    sa = SortingAlgorithms()
                    sa.set_input_data(f + '.csv')
                    alg.append(a)
                    fl.append(f)
                    if a in 'Merge':
                        sa.execute_merge_sort()
                    elif a in 'Heap':
                        sa.execute_heap_sort()
                    else:
                        sa.execute_quick_sort()

                    sa.set_output_data(f + "_" + a)
                    r, t, st, et = sa.get_performance_data()
                    tc.append(t)
                    nr.append(r)
            results = pd.DataFrame(list(zip(alg, fl, nr, tc)),
                                   columns=['Algorithm',
                                            'File name',
                                            'Number records',
                                            'Time consumed'])
            return results
In [0]:
sa = SortingAlgorithms();
values= sa.get_overall_stats()
print(values)
2020-04-22 23:47:54,341::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:47:54,343::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:47:54,345::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:47:54,353::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:47:54,357::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:47:54,385::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:47:54,421::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:47:54,978::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:47:55,332::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:02,176::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:02,214::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:02,216::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:02,219::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:02,227::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:02,231::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:02,266::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:02,296::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:03,127::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:03,457::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:14,846::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:14,883::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:14,888::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:14,890::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:14,897::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:14,903::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:14,924::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:14,970::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:15,441::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
2020-04-22 23:48:15,764::INFO::__main__::<ipython-input-6-9ad2621dabdb>::25::fileinputpath setted
2020-04-22 23:48:20,273::INFO::__main__::<ipython-input-6-9ad2621dabdb>::42::fileinputpath out setted
   Algorithm         File name  Number records Time consumed
0      Merge  random_numbers_1             336    0.00142284
1      Merge  random_numbers_2            1126    0.00636062
2      Merge  random_numbers_3            5726    0.02557245
3      Merge  random_numbers_4          102726    0.52897800
4      Merge  random_numbers_5         1038013    6.49653431
5       Heap  random_numbers_1             336    0.00108574
6       Heap  random_numbers_2            1126    0.00685161
7       Heap  random_numbers_3            5726    0.03298156
8       Heap  random_numbers_4          102726    0.80487000
9       Heap  random_numbers_5         1038013   11.06002015
10     Quick  random_numbers_1             336    0.00145675
11     Quick  random_numbers_2            1126    0.00570632
12     Quick  random_numbers_3            5726    0.01897505
13     Quick  random_numbers_4          102726    0.44300564
14     Quick  random_numbers_5         1038013    4.18654301

Log file content

texto alternativo