<commit_before>import abc
import re
import sys
from urllib.parse import urlparse
from urllib.request import urlopen

from Bio import Entrez


class PubMedLookup(metaclass=abc.ABCMeta):
    """
    This is an abstract base class for PubMed lookups.
    Don't use directly!
    """

    @abc.abstractmethod
    def __init__(self, query, user_email):
        Entrez.email = user_email

    @staticmethod
    def parse_pubmed_url(pubmed_url):
        """Get PubMed ID (pmid) from PubMed URL."""
        parse_result = urlparse(pubmed_url)
        pattern = re.compile('^/pubmed/(\d+)$')
        pmid = pattern.match(parse_result.path).group(1)
        return pmid

    @staticmethod
    def get_pubmed_record(pmid):
        """Get PubMed record from PubMed ID."""
        handle = Entrez.esummary(db="pubmed", id=pmid)
        record = Entrez.read(handle)
        return record


class PubMedLookupPMID(PubMedLookup):
    """
    Retrieve a PubMed record using its PubMed ID.
    (e.g., '22331878' of 'http://www.ncbi.nlm.nih.gov/pubmed/22331878')
    """
    def __init__(self, pmid, user_email):
        super().__init__(pmid, user_email)
        self.record = self.get_pubmed_record(pmid)[0]


class PubMedLookupURL(PubMedLookup):
    """
    Retrieve a PubMed record using its PubMed URL.
    (e.g., 'http://www.ncbi.nlm.nih.gov/pubmed/22331878')
    """
    def __init__(self, pubmed_url, user_email):
        super().__init__(pubmed_url, user_email)
        pmid = self.parse_pubmed_url(pubmed_url)
        self.record = self.get_pubmed_record(pmid)[0]<commit_msg>Demo PubMed lookups if executed as standalone script<commit_after>import abc
import re
import sys
from urllib.parse import urlparse
from urllib.request import urlopen

from Bio import Entrez


class PubMedLookup(metaclass=abc.ABCMeta):
    """
    This is an abstract base class for PubMed lookups.
    Don't use directly!
    """

    @abc.abstractmethod
    def __init__(self, query, user_email):
        Entrez.email = user_email

    @staticmethod
    def parse_pubmed_url(pubmed_url):
        """Get PubMed ID (pmid) from PubMed URL."""
        parse_result = urlparse(pubmed_url)
        pattern = re.compile('^/pubmed/(\d+)$')
        pmid = pattern.match(parse_result.path).group(1)
        return pmid

    @staticmethod
    def get_pubmed_record(pmid):
        """Get PubMed record from PubMed ID."""
        handle = Entrez.esummary(db="pubmed", id=pmid)
        record = Entrez.read(handle)
        return record


class PubMedLookupPMID(PubMedLookup):
    """
    Retrieve a PubMed record using its PubMed ID.
    (e.g., '22331878' of 'http://www.ncbi.nlm.nih.gov/pubmed/22331878')
    """
    def __init__(self, pmid, user_email):
        super().__init__(pmid, user_email)
        self.record = self.get_pubmed_record(pmid)[0]


class PubMedLookupURL(PubMedLookup):
    """
    Retrieve a PubMed record using its PubMed URL.
    (e.g., 'http://www.ncbi.nlm.nih.gov/pubmed/22331878')
    """
    def __init__(self, pubmed_url, user_email):
        super().__init__(pubmed_url, user_email)
        pmid = self.parse_pubmed_url(pubmed_url)
        self.record = self.get_pubmed_record(pmid)[0]

if __name__ == '__main__':
    # NCBI will contact user by email if excessive queries are detected
    email = ''

    # Example of PubMed URL query
    url = 'http://www.ncbi.nlm.nih.gov/pubmed/22331878'
    pub1 = PubMedLookupURL(url, email)

    # Example of PubMed ID query
    pmid = '11402162'
    pub2 = PubMedLookupPMID(pmid, email)

    # Demo contents of entire record
    import pprint
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(pub1.record)

    # Demo extraction of record's contents
    title = pub2.record['Title']
    authors = ", ".join(pub2.record['AuthorList'])
    pub_date = pub2.record['PubDate']
    journal = pub2.record['Source']
    print("TITLE: {}\nAUTHORS: {}\nJOURNAL: {}\nPUBDATE: {}"
        .format(title, authors, journal, pub_date))
{'fraction_non_alphanumeric': 0.11870412553783852, 'fraction_numerical': 0.019741837509491267, 'mean_word_length': 3.065843621399177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 5, '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': '1019334', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1293, 'n_words': 306}