tunisia 0 Newbie Poster

I'm trying to modify this code to take advantage of classes and
some other oop features. However, I'm struggling with getting beyond the basics.

This is the code that I started with which I am trying to change and make more class absed.

import glob
import os
import sys
print(os.getcwd())
dir= raw_input(["Please enter directory location of dcp_inspect output"])
print (dir)
os.chdir(dir)
print(os.getcwd())
cpl    = []
content = []
errors = []
contentkind = []
summary = []





for file in glob.glob("*"):
    try:

        print (file,'\t Was Successfully Opened')
        #file = DcpParse()
        newfile = file
        print('Scanning...', newfile)
        data = open(newfile)
        for each_line in data:
            if 'CPL Id:' in each_line:
                cpl=(each_line.split())
            elif 'ContentTitleText:' in each_line:
                content=(each_line.split())
            elif 'Errors' in each_line:
                errors=(each_line.split())
            elif 'ContentKind' in each_line:
                contentkind=(each_line.split())
            elif 'Composition summary:' in each_line:
                summmary=(each_line)


        print('CPL=',cpl[2]) 
        content.index('ContentTitleText:')
        content.pop(0)
        print('File Name on DCP',content)
        print('Content Kind =',contentkind[1])
        errors.pop(0)
        print('There are',errors[0],'Errors and',errors[2],'hints for', content)
        if errors[0] != '0':
           print('could be issues')
        else:
            print('This DCP appears to be OK')
            #print(summary)
        data.close()

    except:
        pass        

That code changes to a directory where the files are located as specified by the user
and then parses some text. I want to do more with dictionaries, K/V and so on in the other script.

In this code I'm struggling with how to arrange my classes and the methods. I've commented out a lot
in order to get it to run and debug. But I'm not making much headway.

import os
import glob
import sys
import subprocess
import dcp_parse_gmail

print(os.getcwd())
dir= input('Please enter directory \n')




os.chdir('/Users/zacharymanning/Desktop/dcp_output')

print(os.getcwd())

class Parser:   # or `class Parser(object):` in Python 2
    '''Reads DCP manifest files into Python dictionaries.'''

    def __init__(self,path):
        '''Constructs a `Parser` to read the file at the specified `path`.'''

        self.cpl         = []
        self.content     = []
        self.errors      = []
        self.contentkind = []
        self.summary     = []
        self.path        = path
        print(self.path)

    def read(self):
        '''Reads the file specified at construction, and returns a dictionary
        of data from the file.'''
        # ...

                #file = DcpParse()
        file = open(self.path)
        print (file,'\t Was Successfully Opened')

        for each_line in file:
            if 'CPL Id:' in each_line:
                self.cpl=(each_line.split())
            elif 'ContentTitleText:' in each_line:
                self.content=(each_line.split())
            elif 'Errors' in each_line:
                self.errors=(each_line.split())
            elif 'ContentKind' in each_line:
                self.contentkind=(each_line.split())
            elif 'Composition summary:' in each_line:
                self.summmary=(each_line)
    # ...

def printDCP(values):
    '''Prints DCP-related key/value pairs in the specified `values` dictionary,
    or default values for unset keys.'''
    # ...
    print(values)
    print('CPL='.cpl[2]) 
    #self.content.index('ContentTitleText:')
    #self.content.pop(0)
    #print('File Name on DCP',Parser.content)
    #print('Content Kind =',contentkind[1])
    #errors.pop(0)
    #print('There are',errors[0],'Errors and',errors[2],'hints for', content)
    #if errors[0] != '0':
    #   print('could be issues')
    #else:
    #    print('This DCP appears to be OK')
        #print(summary)
    #data.close()



if __name__ == '__main__':
    for file in glob.glob("*"):
        tests = Parser(file)
        tests.read()
        #dcp_parse_gmail.main()
        #subprocess.call("dcp_parse_gmail.py", shell=True)
        #TEXT = 'some pre-test non test-driven code'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.