Reputation: 1698
I have the following functions in python:
def extractParam(word, varName, stringToReplace):
if word.startswith(stringToReplace):
varName=int (word.replace(stringToReplace, ''))
return varName
def getMParams(line):
l = r = b = t = 0
sline = line.strip().split()
for i in range(len(sline)):
l = extractParam(sline[i], l, "l=")
r = extractParam(sline[i], r, "r=")
b = extractParam(sline[i], b, "b=")
t = extractParam(sline[i], t, "t=")
return l, r, b, t
def getIterParams (line):
width = height = stride = x = y = 0
sline = line.strip().split()
for i in range(len(sline)):
width = extractParam(sline[i], width, "width=")
height = extractParam(sline[i], height,"height=")
stride = extractParam(sline[i], stride,"stride=")
x = extractParam(sline[i], x, "x=")
y = extractParam(sline[i], y, "y=")
return width, height , stride, x, y
the functions getMparams and getIterParams are quite the same, my question is if there's a way to create a function that will replace both of them, I was thinking about something like that:
def func (line, params)
//params is an array of parameters (i.e [l,r,b,t] or [width,height,stride,x,y])
//init all params
sline = line.strip().split()
for i in range(len(sline)):
//for everyParam:
param = extractParam(sline[i],param,"param=")
is it possible? or there's another way to do it?
Upvotes: 2
Views: 613
Reputation: 61526
First off, some style points:
The way you handle varName
in extractParam
is ugly and confusing. It took me a while to figure out what you are trying to do (i.e., allow for the fact that extractParam
might not find any data). For now, this is better handled directly instead of trying to call out to a function.
That range(len(
thing you're doing has to stop.
You do not need to strip
the line before split
ting it - any leading and trailing whitespace will disappear during the splitting operation. You will not end up with any extra empty strings in the result.
The name sline
is just plain ugly. You've split the line
up into words; why not refer to the words as, well, words
? (And in any case, don't use abbreviations and jumble things up. Otherwise you get things like sline
that are not actually words.)
We don't use namesLikeThis
for functions (or anything else; although we do use NamesLikeThis
, with a starting capital letter, for classes) in Python. We use names_like_this
.
Also, it looks as though you are repeatedly trying to replace
That said, your proposed approach is fine. Note that since we don't know ahead of time how many items will be extracted, we can't just toss each one into a separate variable. But we can solve this easily by returning a dict
.
My approach is as follows: I iterate over the names
, and for each attempt to find the corresponding word in the line
. Upon finding one, I replace the default value
of 0, and after this check is done, I insert the corresponding key-value pair into the returned result
. I also take a simpler approach to cutting up the word around the equals sign.
def extract_params(line, names):
words = line.split()
result = {}
for name in names:
value = 0
for word in words:
maybe_name, equals, maybe_value = word.partition('=')
if maybe_name == name and equals == '=':
value = maybe_value
result[name] = value
return result
This could potentially be improved quite a bit, but much depends on your exact specifications. I tried to create something that follows your basic logic as closely as possible.
Upvotes: 1