PYTHON   29

person list py

Guest on 5th August 2022 03:01:10 PM

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Creates a list of person data records.
  6.  
  7. This module uses the person module to store information about a
  8. certain person. Changes in the person module may require that you
  9. rewrite parts of this module as well.
  10. """
  11.  
  12. import person
  13.  
  14. def create():
  15.     """
  16.    Creates an empty person object
  17.  
  18.    @type lexicon: a lexicon (created by this module)
  19.    @param lexicon: the lexicon where the concept is stored
  20.    @rtype: dictionary string to integer.
  21.    @return: a dictionary with concept as keys and an integer as value.
  22.    """
  23.     return []
  24.  
  25. def add_person(p, plist):
  26.     """
  27.    Add a new person to given person list.
  28.  
  29.    @type p: a person object
  30.    @param p: the person that is added to the list
  31.    @type plist: the person list (created by this module)
  32.    @param plist: the person list where the concept is stored
  33.    """
  34.     plist.append(p)
  35.  
  36. def no_persons(plist):
  37.     """
  38.    Returns number of persons on given person list.
  39.  
  40.    @type plist: the person list (created by this module)
  41.    @param plist: the person list where the concept is stored
  42.    @rtype: integer
  43.    @return: number of persons on given list
  44.    """
  45.     return len(plist)
  46.  
  47. def remove_person(p, plist):
  48.     """
  49.    Removes the given person from the given person list.
  50.    @type p: a person object
  51.    @param p: the person that is removed from the list
  52.    @type plist: the person list (created by this module)
  53.    @param plist: the person list where the concept is stored
  54.    """
  55.     plist.remove(p, plist)
  56.  
  57. def get_all_ages(plist):
  58.     """
  59.    Returns a list of all ages occurring on the given person list.
  60.  
  61.    Each age only occurs at most once on the returned list.
  62.    @type plist: the person list (created by this module)
  63.    @param plist: the person list where the concept is stored
  64.    @rtype: list of integer
  65.    @return: a list of all ages occuring on the person list.
  66.    """
  67.     ages = []
  68.     for p in plist:
  69.         age = person.get_age(p)
  70.         if not (age in ages):
  71.             ages.append(age)
  72.     ages.sort()
  73.     return ages
  74.  
  75. def get_all_names(plist):
  76.     """
  77.    Returns a list of all names occurring on the given person list.
  78.  
  79.    Each name only occurs at most once on the returned list.
  80.    @type plist: the person list (created by this module)
  81.    @param plist: the person list where the concept is stored
  82.    @rtype: list of string
  83.    @return: a list of all names occuring on the person list.
  84.    """
  85.     names = []
  86.     for p in plist:
  87.         name = person.get_name(p)
  88.         if not (name in names):
  89.             names.append(name)
  90.     names.sort()
  91.     return names
  92.  
  93. def get_all_with_same_name(p1, plist):
  94.     """
  95.    Returns all persons with the same name as given person.
  96.  
  97.    The given person itself is not part of the returned list.
  98.    @type p1: a person object
  99.    @param p1: the person that is removed from the list
  100.    @type plist: the person list (created by this module)
  101.    @param plist: the person list where the concept is stored
  102.    @rtype: list of person
  103.    @return: a list of persons with the same name as given person.
  104.    """
  105.     persons = []
  106.     for p2 in plist:
  107.         if (p2 != p1 and
  108.             person.has_same_name(p1,p2)):
  109.             persons.append(p2)
  110.     return persons
  111.    
  112. def remove_all_with_name(name, plist):
  113.     """
  114.    Remove all persons with given name.
  115.  
  116.    @type name: string
  117.    @param name: the name of the persons that will be removed.
  118.    @type plist: the person list (created by this module)
  119.    @param plist: the person list where the concept is stored
  120.    """
  121.     for p in plist:
  122.         if name == person.get_name(p):
  123.             plist.remove(p)
  124.  
  125. # Test code
  126. # Examples in Swedish
  127. if __name__ == '__main__':    
  128.     plist = create()
  129.     micke1 = person.create("Micke", 44)
  130.     add_person(micke1, plist)
  131.     add_person(person.create("Bosse", 44), plist)
  132.     add_person(person.create("Karin", 30), plist)
  133.     add_person(person.create("Micke", 30), plist)
  134.     print "Följande namn förkommer: "r: " + str(get_all_names(plist))
  135.     print "Följande Ã¥ldrar förekommer: " + str(get_all_ages(plist))
  136.     print "Max Ã¥lder är %d " % max(get_all_ages(plist))
  137.     print "Min Ã¥lder är %d " % min(get_all_ages(plist))
  138.     print "Alla andra Micke: " + str(get_all_with_same_name(micke1, plist))
  139.     remove_all_with_name("Micke", plis

Raw Paste


Login or Register to edit or fork this paste. It's free.