omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Print object not memory location

    Pythonista
    memory location object
    4
    9
    2568
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • resserone13
      resserone13 last edited by

      How can I print the object and not the memory location to my list. I also added a patient Id generator to generator a patient records number for each patient that is created. It’s also shows as a memory location. Any advice on this or any of my code is welcome. Like do I need to have a default value of none for all my class variables in init?

      
      master_patient_list = []
      current_patient_list = []
      iso_patient_list = []
      
      iso_type_dict = {'covid': 'droplets', 'cdiff': 'contact', 'tuberculosis': 'airborne', 'influenza': 'droplets', 'reverse isolation': 'patient saftey'}
      
      def count_patients(list):
      		return len(list)
      
      class Patient (object):
      		
      		def __init__(self, name='', age=None, room_number=None, assigned_doctor=None, assigned_nurse=None, illness=None, iso_status=None, patient_record_nunber=None,patient_medications=[]):
      				self.name = name
      				self.age = age 
      				self.room_number = room_number
      				self.assigned_doctor = assigned_doctor
      				self.assigned_nurse = assigned_nurse
      				self.illness = illness
      				self.iso_status = iso_type_dict
      				self.patient_record_number = self.patient_id_gen()
      				self.patient_medications = list(patient_medications)
      				self.add_patient()
      											
      		def add_patient(self):
      				master_patient_list.append(self)
      				current_patient_list.append(self)
      				
      		def remove_patient():
      				pass
      									
      		def move_patient(self, new_room):
      				self.room_number = new_room			
      				
      		def add_med(self, med):
      				if med not in self.patient_medications:
      						self.patient_medications.append(med)
      						
      		def remove_med(self, med):
      				if med in self.patient_medications:
      						self.patient_medications.remove(med)	
      						
      #generates patine records numbers
      		def patient_id_gen(self):
      				''' generates patient id one at a time as patient object is created '''
      			
      				first_digit = 0
      				second_digit = 0
      				third_digit = 0
      				
      				for i in range(201):
      						final_num = f'AD{third_digit}{second_digit}{first_digit}'
      						#print(final_num)
      		
      						if first_digit < 10:
      								first_digit += 1
      								if first_digit == 10:
      										first_digit = 0
      										second_digit += 1
      										if second_digit == 10:
      												second_digit = 0
      												third_digit += 1
      		
      								
      								yield final_num
      		
      	
      													
      		def __str__(self):
      				return f'Room: {self.room_number} \nName: {self.name} \nPhysician: {self.assigned_doctor} \nNurse: {self.assigned_nurse} \nIllness: {self.illness} \nIso status: {self.iso_status} \nPRN: {self.patient_record_number} \nMedication: {self.patient_medications}'	
      				
      
      #patient info				
      patient1 = Patient()
      patient1.name = 'Sally'
      patient1.age = 35
      patient1.room_number = 430
      patient1.assigned_doctor = 'Dr. Seuss'
      patient1.assigned_nurse = 'Jessica'
      patient1.illness = 'Broken Leg'
      patient1.iso_status = 'NONE'
      #patient1.patient_reord_number = 'AD123456789'
      patient1.add_med('norco')
      
      patient2 = Patient()
      patient2.name = 'Labrone'
      patient2.age = 40
      patient2.room_number = 46208
      patient2.assigned_doctor = 'Dr. Patel'
      patient2.assigned_nurse = 'Melissa'
      patient2.illness = 'Broken Heart'
      patient2.iso_status = '5150'
      patient2.patient_record_number
      patient2.add_med('norco')
      
      
      #test code
      print(patient1)
      print()
      print(patient2)
      print()
      print(f'Master list: \n{master_patient_list}')
      print()
      print(f'Current list: \n{current_patient_list}')
      
      
      
      
      

      Here’s the output...

      Room: 430
      Name: Sally
      Physician: Dr. Seuss
      Nurse: Jessica
      Illness: Broken Leg
      Iso status: NONE
      PRN: <generator object Patient.patient_id_gen at 0x11528b468>
      Medication: ['norco']

      Room: 46208
      Name: Labrone
      Physician: Dr. Patel
      Nurse: Melissa
      Illness: Broken Heart
      Iso status: 5150
      PRN: <generator object Patient.patient_id_gen at 0x115237bf8>
      Medication: ['norco']

      Master list:
      [<main.Patient object at 0x1154d8438>, <main.Patient object at 0x1154d86a0>]

      Current list:
      [<main.Patient object at 0x1154d8438>, <main.Patient object at 0x1154d86a0>]

      GxAl2aa 1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        self.patient_record_number = next(self.patient_id_gen())

        resserone13 1 Reply Last reply Reply Quote 0
        • resserone13
          resserone13 @ccc last edited by resserone13

          @ccc thanks that worked for the Id. It now shows the object. Any ideas on how to show the objects in my list. Also the generator is generating the same number for both patients?

          1 Reply Last reply Reply Quote 0
          • GxAl2aa
            GxAl2aa @resserone13 last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              #!/usr/bin/env python3
              
              # unique_identifier.py
              # To run these doctests, please do: `python3 -m doctest -v unique_identifier.py`
              
              from typing import Iterator
              
              
              def unique_id_generator(prefix: str = "AD") -> Iterator[str]:
                  """
                  Generate unique identifiers one at a time as each new object is created.
              
                  >>> unique_identifier_generator = unique_id_generator()
                  >>> next(unique_identifier_generator)
                  'AD001'
                  >>> next(unique_identifier_generator)
                  'AD002'
                  >>> xy_identifier_generator = unique_id_generator("XY")
                  >>> next(xy_identifier_generator)
                  'XY001'
                  >>> next(unique_identifier_generator)
                  'AD003'
                  """
              
                  first_digit = 0
                  second_digit = 0
                  third_digit = 0
              
                  for i in range(201):
                      if first_digit < 10:
                          first_digit += 1
                          if first_digit == 10:
                              first_digit = 0
                              second_digit += 1
                              if second_digit == 10:
                                  second_digit = 0
                                  third_digit += 1
              
                      yield f"{prefix}{third_digit}{second_digit}{first_digit}"
              
              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by

                The body of the function could be reduced to:

                    current_identifier = 0
                    while current_identifier < 999:
                        current_identifier += 1
                        yield f"{prefix}{current_identifier:03}"
                    raise IndexError("{current_identifier} is our upper limit!")
                
                resserone13 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB last edited by

                  Classes should have a __repr__(self) method, which returns a str, which is what gets used when calling print, or typing the name at the console, or when printing lists, etc.

                  You can thus decide how much detail you want to include -- do you want all info? Or just patient name, etc. There is also a__str__ method which you can implement that returns a different representation, when you use str(myobject) ... So you can have a short version used as repr, and a long version in str, or vice versa.

                  resserone13 1 Reply Last reply Reply Quote 1
                  • resserone13
                    resserone13 @JonB last edited by resserone13

                    @JonB nice. My patient class Has a str method Which prints out most of the patient details.. I’m gonna add a rep and it will be simple print out. Thanks. Adding a repr was the next path I was going to go down. I just thought I would ask her cause I didn’t want to have to research everything just to get it to print out. Thanks again

                    1 Reply Last reply Reply Quote 0
                    • resserone13
                      resserone13 @ccc last edited by resserone13

                      @ccc your right. When I made the body of the function I was imagining 3 dials with the numbers 0 through 9 and as they rotate when first dial hit 9 the next dial would click over to 1 and the first dial change to 0. Like an odometer. I figured it out but I see now all I need is <999 += 1.

                      I was watching a video and thinking of what you recommended and came up with this

                      
                      def patient_id_gen():
                      		''' generates patient id one at a time as patient object is created '''
                      		
                      		prefix = 'AD'
                      		for i in range(1000,9999):
                      				yield f"{prefix}{i}"
                      
                      
                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      Powered by NodeBB Forums | Contributors