<commit_before>import subprocess
import psutil
from py4j.java_gateway import JavaGateway, GatewayParameters
from py4j.protocol import Py4JJavaError
import sys
import time

__author__ = '<NAME>'
__license__ = 'AGPLv3'
__copyright__ = '<NAME>'

if not any([True if 'CDKBridge' in p.cmdline() else False for p in psutil.process_iter()]):
    # compile and start py4j server
    subprocess.call(["javac -cp '/usr/share/py4j/py4j0.10.2.1.jar:./cdk/cdk-1.5.13.jar' ./cdk/cdk_bridge.java"], shell=True)
    p = subprocess.Popen(["java -cp './cdk:/usr/share/py4j/py4j0.10.2.1.jar:./cdk/cdk-1.5.13.jar' CDKBridge"], shell=True)

    # wait 5 sec to start up JVM and server
    time.sleep(5)

# connect to the JVM
gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_convert=True))

# shorten paths
cdk = gateway.jvm.org.openscience.cdk
java = gateway.jvm.java
javax = gateway.jvm.javax

# map exceptions
InvalidSmilesException = cdk.exception.InvalidSmilesException
CDKException = cdk.exception.CDKException
NullPointerException = java.lang.NullPointerException


class Compound(object):
    def __init__(self, compound_string, identifier_type):
        assert(identifier_type in ['smiles', 'inchi'])

        self.compound_string = compound_string
        self.identifier_type = identifier_type
        self.mol_container = None
        self.inchi_factory = cdk.inchi.InChIGeneratorFactory.getInstance()

        allowed_types = ['smiles', 'inchi']

        if self.identifier_type not in allowed_types:
            raise ValueError('Not a valid identifier type')
        try:
            if self.identifier_type == 'inchi':
                s = self.inchi_factory.getInChIToStructure(self.compound_string,
                                                           cdk.DefaultChemObjectBuilder.getInstance())
                self.mol_container = s.getAtomContainer()
            elif self.identifier_type == 'smiles':
                smiles_parser = cdk.smiles.SmilesParser(cdk.DefaultChemObjectBuilder.getInstance())
                self.mol_container = smiles_parser.parseSmiles(self.compound_string)
        except Py4JJavaError as e:
            print(e)
            raise ValueError('Invalid {} provided!'.format(self.identifier_type))

    def get_smiles(self, smiles_type='isomeric'):
        if smiles_type == 'isomeric':
            smiles_generator = cdk.smiles.SmilesGenerator.isomeric()
        elif smiles_type == 'unique':
            smiles_generator = cdk.smiles.SmilesGenerator.unique()
        elif smiles_type == 'generic':
            smiles_generator = cdk.smiles.SmilesGenerator.generic()
        else:
            smiles_generator = cdk.smiles.SmilesGenerator.absolute()

        return smiles_generator.create(self.mol_container)

    def get_inchi_key(self):<commit_msg>ensured thread safety for multithreaded calls<commit_after>import subprocess
import psutil
from py4j.java_gateway import JavaGateway, GatewayParameters
# from py4j.clientserver import ClientServer, JavaParameters, PythonParameters

from py4j.protocol import Py4JJavaError
import sys
import time

__author__ = '<NAME>'
__license__ = 'AGPLv3'
__copyright__ = '<NAME>'

if not any([True if 'CDKBridge' in p.cmdline() else False for p in psutil.process_iter()]):
    # compile and start py4j server
    subprocess.call(["javac -cp '/usr/share/py4j/py4j0.10.2.1.jar:./cdk/cdk-1.5.13.jar' ./cdk/cdk_bridge.java"], shell=True)
    p = subprocess.Popen(["java -cp './cdk:/usr/share/py4j/py4j0.10.2.1.jar:./cdk/cdk-1.5.13.jar' CDKBridge"], shell=True)

    # wait 5 sec to start up JVM and server
    time.sleep(5)

# connect to the JVM
# gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_convert=True))
# gateway = ClientServer(
#     java_parameters=JavaParameters(),
#     python_parameters=PythonParameters())


# shorten paths
# cdk = gateway.jvm.org.openscience.cdk
# java = gateway.jvm.java
# javax = gateway.jvm.javax

# map exceptions
# InvalidSmilesException = cdk.exception.InvalidSmilesException
# CDKException = cdk.exception.CDKException
# NullPointerException = java.lang.NullPointerException


class Compound(object):
    def __init__(self, compound_string, identifier_type):
        assert(identifier_type in ['smiles', 'inchi'])

        gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_convert=True))

        self.cdk = gateway.jvm.org.openscience.cdk
        java = gateway.jvm.java
        javax = gateway.jvm.javax

        self.compound_string = compound_string
        self.identifier_type = identifier_type
        self.mol_container = None
        self.inchi_factory = self.cdk.inchi.InChIGeneratorFactory.getInstance()

        allowed_types = ['smiles', 'inchi']

        if self.identifier_type not in allowed_types:
            raise ValueError('Not a valid identifier type')
        try:
            if self.identifier_type == 'inchi':
                s = self.inchi_factory.getInChIToStructure(self.compound_string,
                                                           self.cdk.DefaultChemObjectBuilder.getInstance())
                self.mol_container = s.getAtomContainer()
            elif self.identifier_type == 'smiles':
                smiles_parser = self.cdk.smiles.SmilesParser(self.cdk.DefaultChemObjectBuilder.getInstance())
                self.mol_container = smiles_parser.parseSmiles(self.compound_string)
        except Py4JJavaError as e:
            print(e)
            raise ValueError('Invalid {} provided!'.format(self.identifier_type))

    def get_smiles(self, smiles_type='isomeric'):
        if smiles_type == 'isomeric':
            smiles_generator = self.cdk.smiles.SmilesGenerator.isomeric()
        elif smiles_type == 'unique':
            smiles_generator = self.cdk.smiles.SmilesGenerator.unique()
        elif smiles_type == 'generic':
            smiles_generator = self.cdk.smiles.SmilesGenerator.generic()
        else:
            smiles_generator = self.cdk.smiles.SmilesGenerator.absolute()

        return smiles_generator.create(self.mol_container)

    def get_inchi_key(self):
{'fraction_non_alphanumeric': 0.10281296265833197, 'fraction_numerical': 0.0100345451554532, 'mean_word_length': 3.1192411924119243, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4577531', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1828, 'n_words': 352}