Using a dictionary in class declaration

Hello everyone…
I’m not a beginner in Python but not an expert yet. I’m dealing with class topic and I need to solve a challenge about how to use a dictionary as an attribute in class declaration. I hand a record for patient that’s include {id:‘’, {‘name’:‘’, ‘last name’:‘’ , ‘phone’:‘’, ‘address’:‘’ and ‘illnesses’:[ ], and ‘drugs’:[ ]} prescribed (this two as a lists). I want to use this dictionary figure because if fix very well to my requirements of using the id as key and the rest of information as a dictionary linked to the id. Any help will be welcome.
This way is how I propose to use the dictionary object in the class. However, how to declare the class and its constructor is the point I’m stuck…

HeathRecord = {
‘id’: {
‘name’: ‘’,
‘lastname’: ‘’,
‘illnesses’: ,
‘drugs’:
},
}

Do you generally know how to declare a class in python and how to make a constructor for that class? I am not sure I properly understand what you are trying to do. Are you saying that you want the constructor of the class to take a dictionary as the input and populate the fields in the class from the dictionary?

Hello jmrukle.
The answer to your question is YES. I need to use that dictionary structure to crear a class that allow me to populate the field for every patient. Using dictionary architecture will facilitate to save the information as json file.
Currently I have this class architecture:

class HealthRecord:
def init(self):
self.Id = ’ ’
self.Name = ’ ’
self.Lastname = ’ ’
self.Phone = ’ ’
self.Address = ’ ’
self.Illnesses = [ ]
self.Drugs = [ ]
It’s works, so far, but I haven’t any idea about how to save it as txt file, either.

Ok, we are getting a clearer picture of the situation. You need a program that builds a collection of HealthRecord objects (from what? command line? a file?) and outputs that data into a JSON format (to the command line? to a file? something else?). Is that correct?

I need more information about the input so that I can help you to craft your program. How does your program receive the input? Is it reading an input file? If so, what is the format of the input file? Or maybe it is a purely command line tool and takes input from users at the command line. Either way, I need a better description of what it is getting as input and what is the expected output.

Ideally this can be stated somewhat like this:

Input: CSV file with columns for each of the data types. For example: …

Output: JSON output representing the health records by ID (which can be sent to a file using I/O redirection). For example: …

Answers:
From command line at the beginning because the JSON file will be generate from that starting information.

It will be purely command line tool and takes input from users at the command line.
Ideally this can be stated somewhat like this:

Output: JSON output representing the health records by ID (which can be sent to a file using I/O redirection).

Handling information from JSON file (open and save) will deal with comparing illnesses and drugs between patients as well as adding or deleting illnesses and drugs.

In that case it sounds like you want to store the in-memory data in a dictionary mapping the id to a HealthRecord. So as you prompt for data, you create new HealthRecord objects and then store them in your dictionary. When your program is done, you iterate through each record printing as JSON which should be a method in the HealthRecord class. You can probably encapsulate the dictionary in a HealthRecords class (naming is up to you).

You should probably have all of the parameters you need in the constructor so that you can preserve the encapsulation:

class HealthRecord:
  def init(self, id, name, last_name, phone, address, illnesses, drugs):
    self._id = id
    self._name = name
    self._last_name = last_name
    self._phone = phone
    self._address = address
    self._illnesses = illnesses
    self._drugs = drugs

  def toJson(self):
    return '...' # JSON representation

Is there some specific place in that logic which is still giving you trouble?

Hello Jason.
Thank you very much for your help.