clairenied15 / heart_rate_sentinel_server

0 stars 0 forks source link

TypeError: 'NoneType' object is not subscriptable #5

Closed clairenied15 closed 5 years ago

clairenied15 commented 5 years ago

I am trying to validate the /api/status/ and the /api/heart_rate/ parts of my server but when doing so, I keep getting the TypeError above. I'm not sure if it is a problem in my hr_server.py functions or with my call_status_patient.py and call_hr_patid.py files I'm trying to validate with. I am confused as to how to properly check that these parts of the server are working.

EDIT: the build_dict function that I am using here is something I found on StackExchange to make a new dictionary of the info for a specific patient id

clairenied15 commented 5 years ago

Please help!!! I don't know what's going on

suyashkumar commented 5 years ago

hi @clairenied15 is your code pushed to github? I see that your validation branch is 15 commits behind master so it's touch to compare. Can you link to the line numbers you want us to dive deeper into?

clairenied15 commented 5 years ago

Yes, I pushed them to the master branch on accident. Master should be all up to date.

suyashkumar commented 5 years ago

Can you post the full error that you get, so we can know what lines to look at?

clairenied15 commented 5 years ago

C:\Users\cen17\heart_rate_sentinel_server\venv\Scripts\python.exe C:/Users/cen17/heart_rate_sentinel_server/hr_server.py

clairenied15 commented 5 years ago

So its saying that hr_info is 'NoneType' but I don't know why

suyashkumar commented 5 years ago

https://github.com/clairenied15/heart_rate_sentinel_server/blob/4eac6a57182be6d1b52d651c35d6f3939c125f8b/hr_server.py#L119 That line can be None if patient_id does not exist in hrinfo_by_id. Can you print out hrinfo_by_id? I'm not sure what's going on with build_dict, will look at at next

suyashkumar commented 5 years ago

https://github.com/clairenied15/heart_rate_sentinel_server/blob/4eac6a57182be6d1b52d651c35d6f3939c125f8b/hr_server.py#L107-L123

So in this function, p and d are likely None. If you want to reference the global variables, you need to do the following (inside your prev_hr function) before referencing them

global p 
global d

which will link p and d inside your function scope to the global variables you are working with in the global scope

clairenied15 commented 5 years ago

I tried doing that and still get the same error

clairenied15 commented 5 years ago

I also printed hrinfo_by_id and it is an empty dictionary

clairenied15 commented 5 years ago

And hrlist is an empty list. Does this have to do with me defining b and p as empty lists outside of the functions at the top of hr_server.py?

suyashkumar commented 5 years ago

it could be that you didn't get a chance to add the patient? Are you using the index in the list to correspond with the patient's ID? Right now, it seems almost like that, except not sure how the insert into that list is working...

On another note, you seem to have a somewhat complicated data structure that ties into build_dict... here a proposed simple data structure for storing the data. Hope this is a useful hint!

datastore = {
    "1": {
        "age": 20,
        "heart_rates": [], # add patient ID 1's heart rates to this array
        "heart_rate_times": [] # add the timestamps of the heart rates in the array above here
        "attending_email": "",
        # other properties you may want to store about a user
    },
    "2": {
        "age": 20,
        "heart_rates": [], # add patient ID 1's heart rates to this array
        "heart_rate_times": [] # add the timestamps of the heart rates in the array above here
        "attending_email": "",
        # other properties you may want to store about a user
    }
}

You can share this data store globally among your flask handler functions. Anytime you need to add a new user, you add them to the outer dict using their patient_id as the key. Any time you need to add a heart rate to a particular user already in your system, you can look them up by the key and then append to their heart rate (for ex datastore["1"]["heart_rates"].append(NEW_HEARTRATE)).

You would start off by declaring an empty datastore dict at the top of your file. Then your handler functions in flask will modify that datastore when they are called.

Remember: when accessing datastore in functions you first need to call

global datastore
suyashkumar commented 5 years ago

If you want to stick with your current data structure, can you tell us a little more about how you intended for that to work?

clairenied15 commented 5 years ago

This data structure makes more sense. I'm just a little confused as to how to group everything for a specific user_id. That's why I was using build_dict, I didn't know how to group different dictionaries with the same user_id into one dictionary, and someone on StackExchange suggested the build_dict function but I didn't really understand what was going on. I think I'll move forward with your proposed method, but what would be an easy way to group everything for a given user_id?

clairenied15 commented 5 years ago

Actually, I think I understand it a lot better now. The thing that is still a little unclear is how we access the proper heart rates and time stamps when the user specifies the patient_id, like for the function when we have to determine if the specific patient is tachycardic or not. Can we do something like:

for item in datastore: if item["patient_id"] == patient_id: age = item["age"]

Will this take the corresponding age for the specified patient id or will it take any age out of the dictionary?

suyashkumar commented 5 years ago

Hiya! You would simply do the following

patient_id = ‘1’
patient_info = datastore[patient_id]
patient_info[‘age’]

Would get you the age for patient ID 1. Does that make sense? You can refresh your memory on dictionaries from the class notes and using the online python reference if that’s helpful!