R
rantingrick
Let me tell you folks about a recent case of culo rojo i experianced
whilst creating a customized bin packer with Python. First i want to
say that i actually like the fact that i can do this..
py> a = []
py> if a:
.... do something
Instead of this
py> if len(a) > 0:
.... do something
Ok but the buck stops with integers. Why? you ask in amazing
befuddlement...Well I happened upon this atrocity when creating
variables that hold indexes into a python list. the variables where
named "choice1 and choice2 and both where initialized to None. Fine no
problem there. So the algorithm will search for the two best choices.
The first choice "choice1" will always be array[0]. The second choice
"choice2" will need to be found using a completely different
algorithm. ...Well i could tell you about it but i would rather just
show you with some simple code..
array = [c1,c2,c3,c4,c5,c6,...]
while looping:
choiceIdx1 = None
choiceIdx2 = None
if array[0] meets condition this condition:
choiceIdx1 = 0
for i in range(len(array)):
if array meets this condition:
choiceIdx2 = i
break
if choiceIdx1 and not choiceIdx2:
best = array.pop(choiceIdx1)
elif choiceIdx2 and not choiceIdx1:
best = array.pop(choiceIdx2)
elif choiceIdx1 and choiceIdx2:
# figure out which choice is better.
best = choiceIdx1 if choiceIdx1.better() else choiceIdx2
elif not choiceIdx1 and not choiceIdx2:
break
else:
# assume the worst
raise
do_somthing_with(best)
BUT THAT WONT WORK BECAUSE OF CRAPPY INTEGER BOOLEAN DEFAULTS! So i
had to do this crap...!
array = [c1,c2,c3,c4,c5,c6,...]
while looping:
choiceIdx1 = ()
choiceIdx2 = ()
if array[0] meets condition this condition:
choiceIdx1 = (0,)
for i in range(len(array)):
if array meets this condition:
choiceIdx2 = (i,)
break
if choiceIdx1 and not choiceIdx2:
best = array.pop(choiceIdx1[0])
elif choiceIdx2 and not choiceIdx1:
best = array.pop(choiceIdx2[0])
elif choiceIdx1 and choiceIdx2:
# figure out which choice is better.
best = choiceIdx1[0] if choiceIdx1.better() else choiceIdx2[0]
elif not choiceIdx1 and not choiceIdx2:
break
else:
# assume the worst
raise
do_somthing_with(best)
Seems kinda dumb to build a tuple just so a conditional wont blow
chunks! This integer bool-ing need to be fixed right away!
whilst creating a customized bin packer with Python. First i want to
say that i actually like the fact that i can do this..
py> a = []
py> if a:
.... do something
Instead of this
py> if len(a) > 0:
.... do something
Ok but the buck stops with integers. Why? you ask in amazing
befuddlement...Well I happened upon this atrocity when creating
variables that hold indexes into a python list. the variables where
named "choice1 and choice2 and both where initialized to None. Fine no
problem there. So the algorithm will search for the two best choices.
The first choice "choice1" will always be array[0]. The second choice
"choice2" will need to be found using a completely different
algorithm. ...Well i could tell you about it but i would rather just
show you with some simple code..
array = [c1,c2,c3,c4,c5,c6,...]
while looping:
choiceIdx1 = None
choiceIdx2 = None
if array[0] meets condition this condition:
choiceIdx1 = 0
for i in range(len(array)):
if array meets this condition:
choiceIdx2 = i
break
if choiceIdx1 and not choiceIdx2:
best = array.pop(choiceIdx1)
elif choiceIdx2 and not choiceIdx1:
best = array.pop(choiceIdx2)
elif choiceIdx1 and choiceIdx2:
# figure out which choice is better.
best = choiceIdx1 if choiceIdx1.better() else choiceIdx2
elif not choiceIdx1 and not choiceIdx2:
break
else:
# assume the worst
raise
do_somthing_with(best)
BUT THAT WONT WORK BECAUSE OF CRAPPY INTEGER BOOLEAN DEFAULTS! So i
had to do this crap...!
array = [c1,c2,c3,c4,c5,c6,...]
while looping:
choiceIdx1 = ()
choiceIdx2 = ()
if array[0] meets condition this condition:
choiceIdx1 = (0,)
for i in range(len(array)):
if array meets this condition:
choiceIdx2 = (i,)
break
if choiceIdx1 and not choiceIdx2:
best = array.pop(choiceIdx1[0])
elif choiceIdx2 and not choiceIdx1:
best = array.pop(choiceIdx2[0])
elif choiceIdx1 and choiceIdx2:
# figure out which choice is better.
best = choiceIdx1[0] if choiceIdx1.better() else choiceIdx2[0]
elif not choiceIdx1 and not choiceIdx2:
break
else:
# assume the worst
raise
do_somthing_with(best)
Seems kinda dumb to build a tuple just so a conditional wont blow
chunks! This integer bool-ing need to be fixed right away!