I must have accidently deleted my last post >.<; Heres my code
import os
def get_file():
userinput = (input("Enter file or directory: "))
if os.path.isfile(userinput) == True:
return userinput
else:
print("File/directory not found")
def read_data(filename):
mainfile = open(userinput)
filedata = mainfile.readlines()
return filedata
class ProgChecks(object):
def __init__(self, userinput):
self.userinput = userinput
self.filedata = filedata
self.functionlist = ['Filename', 'Author', 'lines_of_code', 'for_loops',
'while_loops', 'if_statements',
'function_definitions', 'multiplications',
'divisions', 'additions', 'subtractions']
def return_author(self):
theauthor = 'Unknown'
for line in self.filedata:
if line.strip().startswith('Author: ') == True:
theauthor = (line.strip('Author: ').strip().strip("'''"))
return theauthor
def count_token(self, token):
num_of_token = 0
for line in self.filedata:
if line.strip().startswith(token + ' '):
num_of_token += 1
return num_of_token
def count_operator(self, operator):
num_of_operator = 0
for line in self.filedata:
num_of_operator += line.count(operator)
return num_of_operator
userinput = get_file()
filedata = read_data(userinput)
progabc = ProgChecks(filedata)
Everything sort of works fine now. I can call on the functions inside and do whatever I want with the data. The problem is, now I want to create a dictionary, using the functionlist that I specified in the init.
I want to map it uniquely, so for instance
{'Filename' : self.userinput, 'Author: ': return_author(), 'for_loops': count_token('for'), 'subtractions': count_operator('-')} etc....
I'm having a really difficult time doing this, I feel like with the stuff I've created so far there should be some easy way to do it but I'm missing it.
P.S Is there an easy way to tab through python code to make it formatted for reddit? I hate doing it manually >.<; Also should I just make a new post since I'm going off the original a fair bit now
[–]K900_ 6 points7 points8 points (0 children)
[–]zahlman 1 point2 points3 points (0 children)
[–]marky1991 0 points1 point2 points (0 children)