Advertisement
Guest User

Farms with comments

a guest
May 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import sys
  2.  
  3. # classes definitions
  4. class CollectiveFarm:
  5.     def __init__(self, name):
  6.         self.name = name
  7.         self.species = []
  8.  
  9. class Specie:
  10.     def __init__(self, name):
  11.         self.name = name
  12.  
  13. # creating species
  14. Species = {
  15.     'Rye' : Specie('Rye'),
  16.     'Teff' : Specie('Teff'),
  17.     'Oat' : Specie('Oat'),
  18.     'Rice' : Specie('Rice')
  19.     }
  20.  
  21. # creating farms
  22. Minsk = CollectiveFarm('Minsk')
  23. Yobastan = CollectiveFarm('Yobastan')
  24. Yoptastan = CollectiveFarm('Yoptastan')
  25.  
  26. # adding species to farms
  27. Minsk.species = [ Species['Rye'], Species['Teff'], Species['Oat'] ]
  28. Yobastan.species = [ Species['Rye'], Species['Teff'], Species['Rice'] ]
  29. Yoptastan.species = [ Species['Rye'], Species['Teff'] ]
  30.  
  31. # adding farms
  32. Farms = [Minsk, Yobastan, Yoptastan]
  33.  
  34. # main program
  35. def main():
  36.     common_species = Farms[0].species # using first farm as init value
  37.     print('Farms: ')
  38.     for farm in Farms:
  39.         # printing all farms for user
  40.         print(farm.name)
  41.         # replacing common_species with a intersetion of farm.species
  42.         # this is logically equal to comparing set(common_species) & set(farm.species), which gives uniqes
  43.         # each loop we'll throw out species that not being common to either previous filtering or current one
  44.         common_species = set(common_species).intersection(farm.species)
  45.    
  46.     # printig all species for user
  47.     print('\n')
  48.     print('Species:')
  49.     for name, specie in Species.items():
  50.         print(specie.name)
  51.    
  52.     # printing FILTERED common species
  53.     # should only contain species that's been found in all farms
  54.     print('\n')
  55.     print('Common species:')
  56.     for specie in common_species:
  57.         print(specie.name)
  58.  
  59.     # part2, display species for specific farm
  60.     print('\n')
  61.     # getting user input
  62.     farmid = input('Enter farm name or farm index to display species: ')
  63.  
  64.     try:
  65.         # we expect user typed integer, try to convert it to int index
  66.         farmid = int(farmid)
  67.         farm = Farms[farmid]
  68.     except ValueError:
  69.         # otherwise try to directly select farm from list
  70.         try:
  71.             farm = [_ for _ in Farms if _.name == farmid][0]
  72.         except IndexError:
  73.             print('%s not found in farms' % farmid)
  74.             sys.exit()
  75.  
  76.     # displaying selcted farm species
  77.     print('%s farm species' % farm.name)
  78.     for specie in farm.species:
  79.         print(specie.name)
  80.  
  81. if __name__ == '__main__':
  82.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement