gamete.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Implements gametes, which are the result of
  3. splitting an organism's genome in two, and are
  4. used in the organism's sexual reproduction
  5. In our model, I don't use any concept of a chromosome.
  6. In biology, during a cell's interphase, there are
  7. no chromosomes as such - the genetic material
  8. is scattered chaotically throughout the cell nucleus.
  9. Chromosomes (from my limited knowledge of biologi)
  10. are mostly just a device used in cell division.
  11. Since division of cells in this model isn't
  12. constrained by the physical structure of the cell,
  13. we shouldn't need a construct of chromosomes.
  14. Gametes support the python '+' operator for sexual
  15. reproduction. Adding two gametes together produces
  16. a whole new Organism.
  17. """
  18. from xmlio import PGXmlMixin
  19. class Gamete(PGXmlMixin):
  20. """
  21. Contains a set of genes.
  22. Two gametes can be added together to form a
  23. new organism
  24. """
  25. def __init__(self, orgclass, **genes):
  26. """
  27. Creates a new gamete from a set of genes
  28. """
  29. self.orgclass = orgclass
  30. self.genes = dict(genes)
  31. def __getitem__(self, name):
  32. """
  33. Fetch a single gene by name
  34. """
  35. return self.genes[name]
  36. def __add__(self, other):
  37. """
  38. Combines this gamete with another
  39. gamete to form an organism
  40. """
  41. return self.conceive(other)
  42. def conceive(self, other):
  43. """
  44. Returns a whole new Organism class
  45. from the combination of this gamete with another
  46. """
  47. if not isinstance(other, Gamete):
  48. raise Exception("Trying to mate a gamete with a non-gamete")
  49. return self.orgclass(self, other)