<commit_before># Project Final for PNE 2018


import http.server
import json
import socketserver

socketserver.TCPServer.allow_reuse_address = True

PORT = 8000

OPENFDA_BASIC = False


class OpenFDAClient():

    def send_query(self, query):
        """
        Send a query to the OpenFDA API

        :param query: query to be sent
        :return: the result of the query in JSON format
        """

        headers = {'User-Agent': 'http-client'}

        conn = http.client.HTTPSConnection("api.fda.gov")

        # Get a  https://api.fda.gov/drug/label.json drug label from this URL and
        # extract what is the id,
        # the purpose of the drug and the manufacturer_name

        query_url = "/drug/label.json"

        if query:
            query_url += "?" + query

        print("Sending to OpenFDA the query", query_url)

        conn.request("GET", query_url, None, headers)
        r1 = conn.getresponse()
        print(r1.status, r1.reason)
        res_raw = r1.read().decode("utf-8")
        conn.close()

        res = json.loads(res_raw)

        if 'results' in res:
            items = res['results']
        else:
            items = []

        return items

    def search_drugs(self, active_ingredient, limit=10):
        """
        Search for drugs given an active ingredient drug_name

        :param drug_name: name of the drug to search
        :param limit: Number of items to be included in the list
        :return: a JSON list with the results
        """

        # drug_name for example: acetylsalicylic

        query = 'search=active_ingredient:"%s"' % active_ingredient

        if limit:
            query += "&limit=" + str(limit)

        drugs = self.send_query(query)

        if 'results' in drugs:
            drugs = drugs['results']
        else:
            drugs = []

        return drugs

    def list_drugs(self, limit=10):
        """
        List default drugs

        :param limit: Number of items to be included in the list
        :return: a JSON list with the results
        """

        query = "limit=" + str(limit)

        drugs = self.send_query(query)

        return drugs

    def search_companies(self, company_name, limit=10):
        """
        Search for companies given a company_name

        :param company_name: name of the company to search
        :return: a JSON list with the results
        """

        query = 'search=openfda.manufacturer_name:"%s"' % company_name

        if limit:
            query += "&limit=" + str(limit)

        drugs = self.send_query(query)

        return drugs


class OpenFDAHTML():

    def build_html_list(self, items):
        """
        Creates a HTML list string with the items

        :param items: list with the items to be included in the HTML list
        :return: string with the HTML list
        """

        html_list = "<ul>"
        for item in items:
            html_list += "<li>" + item + "</li>"
        html_list += "</ul>"

        return html_list


    def get_not_found_page(self):
        with open("not_found.html") as html_file:
            html = html_file.read()

        return html

class OpenFDAParser():

    def parse_companies(self, drugs):
        """
        Given a OpenFDA result, extract the drugs data

        :param drugs: result form a call to OpenFDA drugs API
        :return: list with companies info
        """

        companies = []
        for drug in drugs:
            if 'openfda' in drug and 'manufacturer_name' in drug['openfda']:
                companies.append(drug['openfda']['manufacturer_name'][0])
            else:
                companies.append("Unknown")

            companies.append(drug['id'])

        return companies

    def parse_drugs(self, drugs):
        """

        :param drugs: result form a call to OpenFDA drugs API
        :return: list with drugs info
        """

        drugs_labels = []

        for drug in drugs:
            drug_label = drug['id']
            if 'active_ingredient' in drug:
                drug_label += " " + drug['active_ingredient'][0]
            if 'openfda' in drug and 'manufacturer_name' in drug['openfda']:
                drug_label += " " + drug['openfda']['manufacturer_name'][0]

            drugs_labels.append(drug_label)

        return drugs_labels

    def parse_warnings(self, drugs):<commit_msg>[server] Fix errors detected with the tests<commit_after># Project Final for PNE 2018


import http.server
import json
import socketserver

socketserver.TCPServer.allow_reuse_address = True

PORT = 8000

OPENFDA_BASIC = False


class OpenFDAClient():

    def send_query(self, query):
        """
        Send a query to the OpenFDA API

        :param query: query to be sent
        :return: the result of the query in JSON format
        """

        headers = {'User-Agent': 'http-client'}

        conn = http.client.HTTPSConnection("api.fda.gov")

        # Get a  https://api.fda.gov/drug/label.json drug label from this URL and
        # extract what is the id,
        # the purpose of the drug and the manufacturer_name

        query_url = "/drug/label.json"

        if query:
            query_url += "?" + query

        print("Sending to OpenFDA the query", query_url)

        conn.request("GET", query_url, None, headers)
        r1 = conn.getresponse()
        print(r1.status, r1.reason)
        res_raw = r1.read().decode("utf-8")
        conn.close()

        res = json.loads(res_raw)

        if 'results' in res:
            items = res['results']
        else:
            items = []

        return items

    def search_drugs(self, active_ingredient, limit=10):
        """
        Search for drugs given an active ingredient drug_name

        :param drug_name: name of the drug to search
        :param limit: Number of items to be included in the list
        :return: a JSON list with the results
        """

        # drug_name for example: acetylsalicylic

        query = 'search=active_ingredient:"%s"' % active_ingredient

        if limit:
            query += "&limit=" + str(limit)

        drugs = self.send_query(query)

        return drugs

    def list_drugs(self, limit=10):
        """
        List default drugs

        :param limit: Number of items to be included in the list
        :return: a JSON list with the results
        """

        query = "limit=" + str(limit)

        drugs = self.send_query(query)

        return drugs

    def search_companies(self, company_name, limit=10):
        """
        Search for companies given a company_name

        :param company_name: name of the company to search
        :return: a JSON list with the results
        """

        query = 'search=openfda.manufacturer_name:"%s"' % company_name

        if limit:
            query += "&limit=" + str(limit)

        drugs = self.send_query(query)

        return drugs


class OpenFDAHTML():

    def build_html_list(self, items):
        """
        Creates a HTML list string with the items

        :param items: list with the items to be included in the HTML list
        :return: string with the HTML list
        """

        html_list = "<ul>"
        for item in items:
            html_list += "<li>" + item + "</li>"
        html_list += "</ul>"

        return html_list


    def get_not_found_page(self):
        with open("not_found.html") as html_file:
            html = html_file.read()

        return html

class OpenFDAParser():

    def parse_companies(self, drugs):
        """
        Given a OpenFDA result, extract the drugs data

        :param drugs: result form a call to OpenFDA drugs API
        :return: list with companies info
        """

        companies = []
        for drug in drugs:
            if 'openfda' in drug and 'manufacturer_name' in drug['openfda']:
                companies.append(drug['id'] + " "+ drug['openfda']['manufacturer_name'][0])
            else:
                companies.append("Unknown")

        return companies

    def parse_drugs(self, drugs):
        """

        :param drugs: result form a call to OpenFDA drugs API
        :return: list with drugs info
        """

        drugs_labels = []

        for drug in drugs:
            drug_label = drug['id']
            if 'active_ingredient' in drug:
                drug_label += " " + drug['active_ingredient'][0]
            if 'openfda' in drug and 'manufacturer_name' in drug['openfda']:
                drug_label += " " + drug['openfda']['manufacturer_name'][0]

            drugs_labels.append(drug_label)

        return drugs_labels

    def parse_warnings(self, drugs):
{'fraction_non_alphanumeric': 0.0974674721189591, 'fraction_numerical': 0.005111524163568773, 'mean_word_length': 1.983021483021483, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4707090', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2397, 'n_words': 771}