patternpythonModerate
Filtering log files from a server
Viewed 0 times
logfilesserverfromfiltering
Problem
I have made a program in Python to do a filter of log files from a server. What I would like is some guidance on how this code could be optimized, streamlined or 'short-handed' so my 'pythonic' programming becomes better. I don't program for work, I do it on a requirement basis so I'm not good by any means.
Requirement:
I need assistance with:
I am using Tkinter for a GUI which is where the folder path is retrieved.
```
#==================================================================
### OS and python based Imports
import os
### Office, Excel and other file imports
import glob, csv, xlwt, operator
### GUI based imports
import errno, Tkinter, tkFileDialog
from Tkinter import *
#==================================================================
'''Define Command Functions'''
# ======== Select a directory:
def folder_select():
fname = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
if not fname == "":
folderPath.delete(0, END) ## This is to erase anything in the "browse" bar
folderPath.insert(0, fname) ## and use what has been selected from the selection window
else:
folderPath.insert(0, fname) ## This just inserts what has been selected from the selection window
# ======== Make Directories
def makedirs(d):
print("Making Directory")
try:
os.makedirs(d)
except OSError as e:
# If the file alre
Requirement:
- The code I have has functions. I would like to know, in regards to OOP, how I should best use this in a class object.
- I would also like to know if you have any other alternatives to the coding I have used (like the libraries, any sorting or filtering algorithms etc.).
I need assistance with:
- Optimizing in both coding and performance efficiency.
- Advice on how this would turn into a class object.
- Any better solution to how I've done it.
I am using Tkinter for a GUI which is where the folder path is retrieved.
```
#==================================================================
### OS and python based Imports
import os
### Office, Excel and other file imports
import glob, csv, xlwt, operator
### GUI based imports
import errno, Tkinter, tkFileDialog
from Tkinter import *
#==================================================================
'''Define Command Functions'''
# ======== Select a directory:
def folder_select():
fname = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
if not fname == "":
folderPath.delete(0, END) ## This is to erase anything in the "browse" bar
folderPath.insert(0, fname) ## and use what has been selected from the selection window
else:
folderPath.insert(0, fname) ## This just inserts what has been selected from the selection window
# ======== Make Directories
def makedirs(d):
print("Making Directory")
try:
os.makedirs(d)
except OSError as e:
# If the file alre
Solution
Duplicated code:
You have
PEP8
Your code does not follow PEP8 standards. This is the recommended style guideline for Python, and most Python programmers follow it.
Naming:
Comparison to Boolean Literals:
You do not need to compare a value to
Addition:
if not fname == "":
folderPath.delete(0, END) ## This is to erase anything in the "browse" bar
folderPath.insert(0, fname) ## and use what has been selected from the selection window
else:
folderPath.insert(0, fname)You have
folderPath.insert(0, fname) in both the if and the else, at the end of each. Remove duplicated code like this and place it after the if/else:if not fname == "":
folderPath.delete(0, END) ## This is to erase anything in the "browse" bar
folderPath.insert(0, fname)PEP8
Your code does not follow PEP8 standards. This is the recommended style guideline for Python, and most Python programmers follow it.
Naming:
d is not a descriptive variable name (def makedirs(d):), not is f (with open(filename,'w') as f:). You use good names in most other places, so please be consistent.Comparison to Boolean Literals:
You do not need to compare a value to
False or True: if said_it == False:. Just use if not said_if:. Again, you do this in other places in your code.Addition:
count = count+1 can be count += 1.Code Snippets
if not fname == "":
folderPath.delete(0, END) ## This is to erase anything in the "browse" bar
folderPath.insert(0, fname) ## and use what has been selected from the selection window
else:
folderPath.insert(0, fname)if not fname == "":
folderPath.delete(0, END) ## This is to erase anything in the "browse" bar
folderPath.insert(0, fname)Context
StackExchange Code Review Q#104285, answer score: 10
Revisions (0)
No revisions yet.