Reputation: 35
I am new to programming. I have created following function in a class. I noticed that I am repeating my code. I am looking for best solution to eliminate this repetition.
I have following function in my class:
def init(self):
if not os.path.exists(self.src_flag):
if os.path.exists(self.src):
removeFolder(self.src)
print 'File {0} does not exist'.format(self.src_flag)
open(self.src_flag, 'a').close()
if not os.path.exists(self.dst_flag):
if os.path.exists(self.dst):
removeFolder(self.dst)
print 'File {0} does not exist'.format(self.dst_flag)
open(self.dst_flag, 'a').close()
and then I call this function by
Folder.init()
Not sure if this will be the best approach but I was thinking to change the function to following:
def init(self, flag, path):
if not os.path.exists(flag):
if os.path.exists(path):
removeFolder(path)
print 'File {0} does not exist'.format(flag)
open(flag, 'a').close()
but then I have to run function twice to execute it for src and dst e.g.
Folder.init('C:\src\flag.txt', 'C:\src')
Folder.init('C:\dst\flag.txt', 'C:\dst')
Can someone tell me if my solution is ok or maybe there is a better approach?
Upvotes: 3
Views: 115
Reputation: 19414
As you perform the exact same block of code you should obviously use a loop. The only question is how to get the arguments, you can do that in a few ways and choose what fits best for you:
Just pass 2 lists: one for the paths and one for the corresponding flags. Now you can iterate over them using zip
:
def init(self, flags, paths):
for flag, path in zip(flags, paths):
# your block with flag and path
You would call it like:
Folder.init(['C:\src\flag.txt', 'C:\dst\flag.txt'], ['C:\src', 'C:\dst'])
Alternatively, pass a list of already paired flag
s and path
s:
def init(self, pairs):
for flag, path in pairs:
# your block with flag and path
You would call it like:
Folder.init([('C:\src\flag.txt', 'C:\src'), ('C:\dst\flag.txt', 'C:\dst')])
Just pass all arguments together, assuming they are ordered in pairs. Then, iterate on pairs:
def init(self, *args):
for i in range(0, len(args)-1, 2):
flag = args[i]
path = args[i+1]
# your block with flag and path
Or, using zip
again:
for flag, path in zip(args[::2], args[1::2]):
# your block with flag and path
You would call it like: Folder.init('C:\src\flag.txt', 'C:\src', 'C:\dst\flag.txt', 'C:\dst')
Lastly, you didn't mention that in the question, but assuming path
is the directory of the file flag
, you can use the os.path
module and just pass the flags and get path
using the dirname
function:
def init(self, *flags):
for flag in flags:
path = os.path.dirname(flag)
# your block with flag and path
You would call it like: Folder.init('C:\src\flag.txt', 'C:\dst\flag.txt')
Upvotes: 1
Reputation: 5521
You could use an array as parameter and loop over this [src,dst] array in your function. So, you could build the paths in your method and just exchange the src / dst part in the path.
This only makes sense if both src and dest are initialized always together. You could get rid of 2 similar method calls or 4 parameters, in case you would use füll paths in one single method calls
If this is not the case, I like your solution and the two calls.
Upvotes: 0