server module

server.correct_input(in_data: Dict[str, Union[str, list]], expected: Dict[str, type]) Union[server.db_entry, str]

Convert the default string inputs of the GUI into the indicated types

Takes the dictionary in_data from the post request and converts the values to the data types indicated in the expected type dictionary. If the conversion fails, the function returns a string indicating where the error occurred. The internal conversions are handled by try_intify and try_floatify for int and float types respectively.

Parameters
  • in_data (Dict[str, Union[str, list]]) – Data received to the server by the POST request

  • expected (Dict[str, type]) – Typed dictionary with keys matching the in_data keys

Returns

A dictionary with correspondingly corrected types such that the types match the expected types

Return type

Union[db_entry, str]

class server.db_entry

Bases: dict

hr: float
image: list
patient_id: int
patient_name: str
server.get_all()

Applies route for showing all data present on the server

This function is a GET request that when the address http://vcm-23126.vm.duke.edu/get is inputted online, returns a jsonified string that states all the data contained in the database defined in database.py. The jsonified string, when parsed, is a dictionary with index values as keys and all data (except the image data) associated with those MRNs as values. If the database is empty, returns an empty dict.

Returns

Dictionary with mrns as keys and data as values

Return type

Tuple[dict, int]

server.get_data(name_or_mrn: str) Tuple[Union[dict, str], int]

Applies route for showing all data associated with name or mrn

This function is a GET request that when the address http://vcm-23126.vm.duke.edu/get/<name_or_mrn> is inputted online, returns a jsonified string that states all the data contained in the database associated with the name or MRN inputted. If there is more than one MRN associated with the name given, then the most recent mrn is returned, and other data can only be retrieved by inputting the mrn of the older data.

Parameters

name_or_mrn (str) – name or mrn of the relevant data to be retrieved

Returns

data associated with that name or mrn

Return type

Tuple[dict, int]

server.get_image(name_or_mrn: str) Tuple[str, int]

Applies route for showing image associated with the given name or mrn

This function is a GET request that when the address http://vcm-23126.vm.duke.edu/get/<name_or_mrn>/image is inputted online, returns a html rendered string (using render_image) that shows the image and associated name on a webpage associated with the name or MRN inputted. If there is more than one MRN associated with the name given, then the most recent mrn is returned, and other images can only be retrieved by inputting the mrn of the older data.

Parameters

name_or_mrn (str) – name or mrn of the relevant data to be retrieved

Returns

html string of rendered image and name

Return type

Tuple[str, int]

server.get_status()

Applies route for showing that the server is on.

This route function is a get request that when the address http://vcm-23126.vm.duke.edu/ is inputted online, returns a jsonified string that says “Server is on”.

Returns

json string that the server is on

Return type

Tuple[str, int]

server.new_patient()

This applies the new_patient route to post new patient information to a dictionary.

This function is a POST request that when the address http://vcm-23126.vm.duke.edu/new_patient is inputted online, returns a jsonified string that states a dictionary of the patient_id, the patient_name, the heart rate as ‘hr’, and the time that the data was stored in the database. This function uses the posted dictionary of new patient data and checks the id and hr calling the correct input function, which uses try intify and try floatify as backend. Then, the function calls the validate input function that checks if the input was a dictionary(if not, return string and 400 error), if the key specified is missing(if missing return string and 400 error), and if the data type of each key is correct (if not return str and 400). If all of this is correct, returns True and 200 code. Once the data is determined to be True, then the time is recorded and it is added to a database and the added data as a ditionary is returned stating that a new patient was added with the code 200.

Returns

dictionary that includes patient_id, patient_name, time,

and hr; string + error code or string + completion code :rtype: Tuple[dict, int]

server.render_image(b64_img: str, name: str) str

Converts b64 image and patient name to a rendered html page

This function takes a b64 png image and patient name and converts it to a string which can be rendered in a standard html page on a browser. It does this by inserting the data into a locally stored html template and returning a correctly rendered image and name on a webpage.

Parameters
  • b64_img (str) – A string that is a base64 encoded image that decodes into image bytes

  • name (str) – The name of the patient

Returns

A rendered html string that encodes for a webpage

Return type

str

server.try_floatify(num: Union[int, float, bool, str, complex]) Union[float, bool]

Function that tests if object can be converted to number

A function that takes any input and detects if it is a number by attempting to convert the input to a float. This function catches convertable digit string cases.

Source: https://stackoverflow.com/questions/354038

Parameters

num (Union[int, float, bool, str, complex]) – object data to determine if it is conceivably a number

Returns

a boolean determination if the input is a number, or the floating point number itself

Return type

Union[float, bool]

server.try_intify(num: Union[int, float, bool, str, complex]) Union[int, bool]

Tries to convert input to integer and if this is not possible, then the func returns false.

The func runs the value through a series of statements that determine if the value has an imaginary value of 0 (return only the real integer value or otherwise to return False), if the value is a float and if it is equal to the integer of that value (returns the integer), if the value is a boolean (returns False), or if there is any other error to return False.

Parameters

num (Union[int, float, bool, str, complex]) – single value can be int, float, bool, str, complex

Returns

integer if it was convertable and a False if not

Return type

Union[int, bool]

server.validate_input(in_data: dict, expected: Dict[str, type]) Tuple[Union[str, bool], int]

Determines if the data given is a dictionary, if the key exsists in the input, and if the value in the key matches the type that was expected.

An expectation of the type of data in each key is established and fed into this function along with a dictionary data set. The Validate input funx then checks if the input was a dictionary(if not, return string and 400 error), if the key specified is missing(if missing return string and 400 error), and if the data type of each key is correct (if not return str and 400).

Parameters
  • in_data – dictionary of data

  • expected – dictionary data key types expectations (tuple)

Returns

String and 400 error (not dictionary, or wrong dictionary value type) or True + 200 code.

Return type

Tuple[Union[str, bool], int]