[more of a challenge to see how others may do it better than anything else] The 'Person' model in Django has an object 'bloodtype'. I'm using a spawning routine to create people in a universe with 65536 "planets" (for want of a better description), one of it's subroutines determines the blood type of the new creation. If there's no existing populous, or no possible fertile options, the id of the parents (obviously 'Person' as well) will be equal. In this case a random blood type is returned.
So far it appears to be working well, I've adopted the logic from a Red Cross site:
Coming up with this routine:
def getbloodtype(a,b):
#a = Male parent, b = Female parent. (doesn't really matter)
if a == b:
return choice(( 'O+', 'O-', 'A+', 'A-', 'B+', 'B-', 'AB+', 'AB-' ))
else:
bloodA = Person.object.get(id=a).bloodtype
bloodB = Person.object.get(id=b).bloodtype
if bloodA == bloodB:
if 'O' in bloodA:
blood = 'O'
elif 'AB' in bloodA:
blood = choice(( 'A', 'B', 'AB' ))
else:
blood = choice(( 'O', bloodA ))
elif ( 'AB' in bloodA ) or ( 'AB' in bloodB ):
if ( 'O' in bloodA ) or ( 'O' in bloodB ):
blood = choice(('A', 'B' ))
else:
blood = choice(('A', 'B', 'AB' ))
elif ( 'O' in bloodA ):
blood = choice(( 'O', bloodB ))
elif ( 'O' in bloodB ):
blood = choice(( 'O', bloodA ))
else:
blood = choice(( 'A', 'B', 'AB' ))
if bloodA.endswith('-') and bloodB.endswith('-'):
blood += '-'
else:
blood += choice(('+', '-'))
return blood
Not being a 'master' with Python, how may you do it better? Are all those elif going to cope? As I say, it seems to work, but will need some run time testing results. Excuse the excessive bracketing.
Many thanks,
Aucun commentaire:
Enregistrer un commentaire