I dare say it's silly enough. I have a piece of code in Gimp that reads:
Code:
postfix='_xcf.webp'
if GS: postfix='-GS' + postfix
if crop: postfix='-crop' + postfix
theName=name + postfix
to variously modify the name of my issue file.
It works to do what I wish, but it looks as ungainly as a five-legged mammoth.
Please, is there some more elegant way to achieve the same?
I have been reading about 'lambdas', but nothing seemed applicable...
And, if my quesion is as foolish as I feel at posting it, please disregard it...
Can you have
GS and
crop at the same time and if so are you expecting a
-crop-GS_xcf result?
Are
GS and
crop booleans?
Lambdas not a solution here. To avoid
ifs your best weapon is an array or dictionary of postfixes, keyed by the option.
Something I sometimes do with booleans is take advantage of the fact that they are automatically converted to 0 or 1, so you can write things like:
Code:
value=[valueIfFalse,ValueIfTrue][boolean]
Otherwise your code is also a backwards walking mammoth, I would have used:
Code:
theName=name
if GS: theName+='-GS'
if crop: theName+='-crop'
theName+=postFix
Or using %-formatting and my boolean-indexed array trick
Code:
theName='%s%s%s_xcf.webp' % (name, ['','-crop'][crop],['','-GS'][GS])
Other people would build a list of name parts and use
Code:
theName=''.join(nameParts)
Thanks!
Yes,both are booleans and not exclusive: I can conceivably wish to output a webp (or indexed png) both grayscale (I always work in RGB) and without margins, as those are better added through css in an epub... more control.
I dare say I shall try all variants... although it will take me some to get used to 'x+=y' ... as did accepting 'x=x+y' without concluding 'y=0', and remembering moreover that one had lost the initial value of 'x'--live and learn!
Cute the variant with list and formatting!
(10-27-2019, 10:29 PM)carmen Wrote: [ -> ]Thanks!
Yes,both are booleans and not exclusive: I can conceivably wish to output a webp (or indexed png) both grayscale (I always work in RGB) and without margins, as those are better added through css in an epub... more control.
I dare say I shall try all variants... although it will take me some to get used to 'x+=y' ... as did accepting 'x=x+y' without concluding 'y=0', and remembering moreover that one had lost the initial value of 'x'--live and learn!
Cute the variant with list and formatting!
x+=y is fairly frequent. It is actually more readable that
x=x+y because you don't have to check that it's the same variable on both sides.
Compare:
Code:
offsetX+=1
offsetY+=1
offsetZ+=1
with:
Code:
offsetX=offsetX+1
offsetY=offsetZ+1
offsetZ=offsetY+1
Did you catch the Y<->Z swap in the last one?
Formatting is IMHO the preferred method.