<commit_before>"""Python interface to GenoLogics LIMS via its REST API.

LIMS interface.

<NAME>, Science for Life Laboratory, Stockholm, Sweden.
Copyright (C) 2012 <NAME>
"""

__all__ = ['Lab', 'Researcher', 'Project', 'Sample',
           'Containertype', 'Container', 'Processtype', 'Process',
           'Artifact', 'Lims']

#python 2.7 compatibility
try:
    from urllib.parse import urljoin
except:
    from urlparse import urljoin
from io import StringIO

# http://docs.python-requests.org/
import requests

from .entities import *

TIMEOUT=16

class Lims(object):
    "LIMS interface through which all entity instances are retrieved."

    VERSION = 'v2'

    def __init__(self, baseuri, username, password, version = VERSION):
        """baseuri: Base URI for the GenoLogics server, excluding
                    the 'api' or version parts!
                    For example: https://genologics.scilifelab.se:8443/
        username: The account name of the user to login as.
        password: <PASSWORD>.
        version: The optional LIMS API version, by default 'v2' 
        """
        self.baseuri = baseuri.rstrip('/') + '/'
        self.username = username
        self.password = password
        self.VERSION = version
        self.cache = dict()
        # For optimization purposes, enables requests to persist connections
        self.request_session = requests.Session()
        #The connection pool has a default size of 10
        self.adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
        self.request_session.mount('http://', self.adapter)

    def get_uri(self, *segments, **query):
        "Return the full URI given the path segments and optional query."
        segments = ['api', self.VERSION] + list(segments)
        url = urljoin(self.baseuri, '/'.join(segments))
        if query:
            url += '?' + urllib.parse.urlencode(query)
        return url

    def get(self, uri, params=dict()):
        "GET data from the URI. Return the response XML as an ElementTree."
        try:
            r = self.request_session.get(uri, params=params,
                         auth=(self.username, self.password),
                         headers=dict(accept='application/xml'),
                         timeout=TIMEOUT)
        except requests.exceptions.Timeout as e:
            raise type(e)("{0}, Error trying to reach {1}".format(e.message, uri))
<commit_msg>another python 2.7 compatibility<commit_after>"""Python interface to GenoLogics LIMS via its REST API.

LIMS interface.

<NAME>, Science for Life Laboratory, Stockholm, Sweden.
Copyright (C) 2012 <NAME>
"""

__all__ = ['Lab', 'Researcher', 'Project', 'Sample',
           'Containertype', 'Container', 'Processtype', 'Process',
           'Artifact', 'Lims']

#python 2.7 compatibility
try:
    from urllib.parse import urljoin
    from urllib.parse import urlencode
except:
    from urlparse import urljoin
    from urllib import urlencode

from io import StringIO

# http://docs.python-requests.org/
import requests

from .entities import *

TIMEOUT=16

class Lims(object):
    "LIMS interface through which all entity instances are retrieved."

    VERSION = 'v2'

    def __init__(self, baseuri, username, password, version = VERSION):
        """baseuri: Base URI for the GenoLogics server, excluding
                    the 'api' or version parts!
                    For example: https://genologics.scilifelab.se:8443/
        username: The account name of the user to login as.
        password: The password for the user account to login as.
        version: The optional LIMS API version, by default 'v2' 
        """
        self.baseuri = baseuri.rstrip('/') + '/'
        self.username = username
        self.password = password
        self.VERSION = version
        self.cache = dict()
        # For optimization purposes, enables requests to persist connections
        self.request_session = requests.Session()
        #The connection pool has a default size of 10
        self.adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
        self.request_session.mount('http://', self.adapter)

    def get_uri(self, *segments, **query):
        "Return the full URI given the path segments and optional query."
        segments = ['api', self.VERSION] + list(segments)
        url = urljoin(self.baseuri, '/'.join(segments))
        if query:
            url += '?' + urlencode(query)
        return url

    def get(self, uri, params=dict()):
        "GET data from the URI. Return the response XML as an ElementTree."
        try:
            r = self.request_session.get(uri, params=params,
                         auth=(self.username, self.password),
                         headers=dict(accept='application/xml'),
                         timeout=TIMEOUT)
        except requests.exceptions.Timeout as e:
            raise type(e)("{0}, Error trying to reach {1}".format(e.message, uri))

{'fraction_non_alphanumeric': 0.10600564288593309, 'fraction_numerical': 0.01007658202337767, 'mean_word_length': 2.5223562810503903, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1024091', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1344, 'n_words': 443}