Create an interactive command loop using inheritance python34

我们两清 提交于 2019-12-11 12:03:29

问题


I'm trying to see how I can structure a script in a way that I can use the inheritance method. I'm fairly new to python. And my problem is using variables in one class from another class-def. I just recently learned about the super function and I don't think I'm using it right because it keeps printing and recalculating everything that it's pulling from.

Let's say I have a bunch of messages coming in a text file delimited by commas that give me different information. I want to be able to take that text file and...

  1. be able to read the content delimited by commas (done)
  2. tell me how many of each type of message there are (done)
  3. then create a class called messages that has defs for each type of message with its respective calculations and variables it creates in those instances (done)
  4. create class to print and write those calculations and variables in the client and xls (partially done due to my issue)
  5. create class to convert xls to csv and kml (somewhat done)

Here is a toy structure of what I'm working with


import bunch of stuff


data = []  #empty because we will store data into it


#Reads a CSV file and return it as a list of rows
def read_csv_file(filename):
    """Reads a CSV file and return it as a list of rows."""

    for row in csv.reader(open(filename)):
        data.append(row)
    return data

with open(path_in + data_file) as csvfile:
    read_it = list(csv.reader(csvfile, delimiter=','))  


#Counts the number of times a GPS command is observed
def list_msg_type_countdata):
    """Counts the number of times a GPS command is observed.

Returns a dictionary object."""

    msg_count = dict()
    for row in data:
        try:
            msg_count[row[0]] += 1 
        except KeyError:
            msg_count[row[0]] = 1

    return msg_count

print(list_msg_type_count(read_it))
print ("- - - - - - - - - - - - -")


class CreateWorkbook:
    def openworkbook(self, data):
        global output_filename
        output_filename = input('output filename:')
        global workbook
        workbook = xlsxwriter.Workbook(path_out + output_filename + '_' + command_type +'.xlsx')
        self.worksheet = workbook.add_worksheet()
        #formatting definitions
        global bold
        bold = workbook.add_format({'bold': True})
        global date_format
        date_format = workbook.add_format({'num_format': "m/d/yyyy hh:mm:ss"})
        global time_format
        time_format = workbook.add_format({'num_format': "hh:mm:ss"})   

    def closeworkbook_gprmc(self, data):
        print('closeworkbook')
        #pull data from process_msg1
        (i1, i2, i3) = messagetype.process_msg1(data)
        #sets up the header row
        self.worksheet.write('A1','item1',bold)
        self.worksheet.write('B1', 'item2',bold)
        self.worksheet.write('C1', 'item3',bold)
        self.worksheet.autofilter('A1:C1')   #dropdown menu created for filtering

        # Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
        for r, row in enumerate(data, start=1):  #where you want to start printing results inside workbook
            for c, col in enumerate(data):
                self.worksheet.write_column(r,0, i1)
                self.worksheet.write_column(r,1, i2)
                self.worksheet.write_column(r,2, i3)
        workbook.close()
        f.close()
        print('XLSX file named ' + output_filename + '_' + command_type +' was created')

    def closeworkbook_msg2(self, data):
        #pull data from process_msg2
        (i1, i2, i3, i4) = messagetype.process_msg2(data)
        #sets up the header row
        self.worksheet.write('A1','item1',bold)
        self.worksheet.write('B1', 'item2',bold)
        self.worksheet.write('C1', 'item3',bold)
        self.worksheet.write('C1', 'item4',bold)
        self.worksheet.autofilter('A1:C1')   #dropdown menu created for filtering

        # Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
        for r, row in enumerate(data, start=1):  #where you want to start printing results inside workbook
            for c, col in enumerate(data):
                self.worksheet.write_column(r,0, i1)
                self.worksheet.write_column(r,1, i2)
                self.worksheet.write_column(r,2, i3)
                self.worksheet.write_column(r,3, i4)
        workbook.close()
        f.close()
        print('XLSX file named ' + output_filename + '_' + command_type + ' was created')      


class ConvertFile
    def convert2csv(self, data):
        # set path to folder containing xlsx files
        os.chdir(path_out)

        # find the file with extension .xlsx
        xlsx = glob.glob(output_filename + '_' + command_type + '.xlsx')

        # create output filenames with extension .csv
        csvs = [x.replace('.xlsx','.csv') for x in xlsx]

        # zip into a list of tuples
        in_out = zip(xlsx,csvs)

        # loop through each file, calling the in2csv utility from subprocess
        for xl,csv in in_out:
           out = open(csv,'w')
           command = 'c:/python34/scripts/in2csv %s\\%s' % (path_out,xl)
           proc = subprocess.Popen(command,stdout=out)
           proc.wait()
           out.close()
        print('CSV file named ' + output_filename + '_' + command_type + ' was created')

    def convert2kml(self, data):
        #Input the file name.
        h = open(path_out + output_filename + '_' + command_type + '.csv')
        with h as csvfile2:
            data2 = csv.reader(csvfile2,delimiter=',')
            next(data2)

            #Open the file to be written.
            g = open(output_filename + '_' + command_type +'.kml','w')
            g.write("<?xml version='1.0' encoding='UTF-8'?>\n")
            g.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")
            g.write("<Document>\n")
            g.write("   <name>" + output_filename + '_' + command_type + '.kml' +"</name>\n")
            for row in data2:
                g.write("   <Placemark>\n")
                g.write("<TimeStamp><when>" + str(row[0]) + "</when></TimeStamp>\n")
                g.write("       <Point>\n")
                g.write("           <coordinates>" + str(row[2]) + "," + str(row[1]) + "</coordinates>\n")
                g.write("       </Point>\n")
                g.write("   </Placemark>\n")


        g.write("</Document>\n")
        g.write("</kml>\n")
        g.close()
        h.close()
        print('and ' + output_filename + '_' + command_type +'.kml was created,too!')


class MessageType:
    def process_msg1(self,data)
        item1 = []
        item2 = []
        item3 = []

        print('printing stuff')

        for r in data:
            if row[0] == 'msg type1'
                item1.append('calculations')
                item2.append('calculations')
                item3.append('calculations')
        print('calculations done')
        return(array(item1),array(item2),array(item3))

    def process_msg2(self,data)
        item1 = []
        item2 = []
        item3 = []
        item4 = []

        print('printing stuff')

        for r in data:
            if row[0] == 'msg type1'
                item1.append('calculations')
                item2.append('calculations')
                item3.append('calculations')
                item4.append('calculations')
        print('calculations done')
        return(array(item1),array(item2),array(item3),array(item4))    

class PrintMSG(MessageType):
    def process_msg1(self, data):
        (i1, i2, i3) = super(PrintMSG, self).process_msg1(data)
        print('printing plus plotting using variables from class Message')

    def process_msg2(self, data):
        (i1, i2, i3,i4) = super(PrintMSG, self).process_msg2(data)
        print('printing plus plotting using variables from class Message') 


#processing piece
keep_asking = True
while keep_asking:
    command_type = input("What message type do you want to look at?")
    if command_type == 'msg type1':
        createworkbook = CreateWorkbook()
        createworkbook.openworkbook(data)
        msg = MessageType()
        print_msg = PrintMSG()
        print_msg.process_msg1(data)
        createworkbook.closeworkbook_msg1(data) 
        convert2csv(data)
        convert2kml(data)
    elif command_type == 'msg type2':
        createworkbook = CreateWorkbook()
        createworkbook.openworkbook(data)
        msg = MessageType()
        print_msg = PrintMSG()
        print_msg.process_msg2(data)
        createworkbook.closeworkbook_msg2(data) 
        convert2csv(data)
        convert2kml(data)
    else:
        print("Invalid type:", command_type)

    wannalook = input('Want to look at another message or no?')
    if not wannalook.startswith('y'):
        keep_asking = False

回答1:


Class definition

The code is kind of big and there are many things that do not work or could be improved. As a starter, take the class CreateWorkbook. You need always use self, as the first argument for methods. (There are a few exceptions but they are not relevant here.) To be able to use variables defined in one method in another, you need to prefix them with self.:

class CreateWorkbook:
    def openworkbook(self, data):
        self.output_filename = input('output filename:')
        self.workbook = xlsxwriter.Workbook(path_out + output_filename + '_' + command_type +'.xlsx')
        self.worksheet = workbook.add_worksheet()

    def closeworkbook_msg1(self, data):
        #sets up the header row
        self.worksheet.write('A1','item1',bold)
        self.worksheet.write('B1', 'item2',bold)
        self.worksheet.write('C1', 'item3',bold)
        self.worksheet.autofilter('A1:C1')   #dropdown menu created for filtering

        # Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
        for r, row in enumerate(data, start=1):  #where you want to start printing results inside workbook
            for c, col in enumerate(data):
                self.worksheet.write_column(r,0, i1)
                self.worksheet.write_column(r,1, i2)
                self.worksheet.write_column(r,2, i3)
        self.workbook.close()
        print('XLSX file named ' + output_filename + '_' + command_type +' was created')

    def closeworkbook_msg2(self, data):
        #sets up the header row
        self.worksheet.write('A1','item1',bold)
        self.worksheet.write('B1', 'item2',bold)
        self.worksheet.write('C1', 'item3',bold)
        self.worksheet.write('C1', 'item4',bold)
        self.worksheet.autofilter('A1:C1')   #dropdown menu created for filtering

        # Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
        for r, row in enumerate(data, start=1):  #where you want to start printing results inside workbook
            for c, col in enumerate(data):
                self.worksheet.write_column(r,0, i1)
                self.worksheet.write_column(r,1, i2)
                self.worksheet.write_column(r,2, i3)
                self.worksheet.write_column(r,3, i4)
        self.workbook.close()
        print('XLSX file named ' + output_filename + '_' + command_type + ' was created')

Reading csv

This doesn't make much sense:

f = open(path_in + data_file)
read_it = read_csv_file(path_in + data_file)

with f as csvfile:
    readCSV = csv.reader(csvfile,delimiter=',')

I would interpret it as something like this:

with open(path_in + data_file) as csvfile:
    read_it = list(csv.reader(csvfile, delimiter=','))    


来源:https://stackoverflow.com/questions/34548763/create-an-interactive-command-loop-using-inheritance-python34

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!