Instruction:
How would you explain what the following Python 3 function does
def graph_coloring_qubo(graph, k):

    K = nx.complete_graph(k)
    g1 = nx.cartesian_product(nx.create_empty_copy(graph), K)
    g2 = nx.cartesian_product(graph, nx.create_empty_copy(K))
    return nx.compose(g1, g2)

Response: Returns a QUBO for k - coloring a graph A.


Instruction:
Can you tell what is the following Python 3 function doing
def chimera_blocks(M=16, N=16, L=4):
    for x in xrange(M):
        for y in xrange(N):
            for u in (0, 1):
                yield tuple((x, y, u, k) for k in xrange(L))

Response: Generator for a chimera block quotient


Instruction:
Explain what the following Python 3 code does
def chimera_block_quotient(G, blocks):
    from networkx import Graph
    from itertools import product

    BG = Graph()
    blockid = {}
    for i, b in enumerate(blocks):
        BG.add_node(i)
        if not b or not all(G.has_node(x) for x in b):
            continue
        for q in b:
            if q in blockid:
                raise(RuntimeError, "two blocks overlap")
            blockid[q] = i

    for q, u in blockid.items():
        ublock = blocks[u]
        for p in G[q]:
            if p not in blockid:
                continue
            v = blockid[p]
            if BG.has_edge(u, v) or u == v:
                continue
            vblock = blocks[v]

            if ublock[0][2] == vblock[0][2]:
                block_edges = zip(ublock, vblock)
            else:
                block_edges = product(ublock, vblock)

            if all(G.has_edge(x, y) for x, y in block_edges):
                BG.add_edge(u, v)

    return BG

Response:
This function extracts the blocks from a networkx graph and returns a new block - quotient graph according to the acceptability
    functions block_good and eblock_good functions.


Instruction:
Write a Python 3 script to
produce an embedding in target_graph suitable to check if source_graph is 4-colorable. More generally, if target_graph is a (M,N,L) Chimera subgraph, the test is for L-colorability. This depends heavily upon the Chimera structure Inputs: source_graph, target_graph: networkx graphs M,N,L: integers defining the base chimera topology Outputs: emb: a dictionary mapping (v,i)

Response:
def embed_with_quotient(source_graph, target_graph, M=16, N=16, L=4, **args):
    """
    Produce an embedding in target_graph suitable to
    check if source_graph is 4-colorable.  More generally,
    if target_graph is a (M,N,L) Chimera subgraph, the
    test is for L-colorability.  This depends heavily upon
    the Chimera structure

    Inputs:
        source_graph, target_graph: networkx graphs
        M,N,L: integers defining the base chimera topology

    Outputs:
        emb: a dictionary mapping (v,i)

    """
    from random import sample
    blocks = list(chimera_blocks(M, N, L))

    BG = chimera_block_quotient(target_graph, blocks)

    ublocks = {block: (block[0][2], i)
               for (i, block) in enumerate(blocks) if BG.has_node(i)}
    source_e = list(source_graph.edges())
    source_n = {x for e in source_e for x in e}
    fabric_e = list(BG.edges())

    # Construct the hints:
    # Goal: each source node must be connected to one horizontal block and one
    #       vertical block (by Chimera structure, each source node will
    #       contain a full (horizontal and vertical) unit cell
    # Construction:
    #   0. for each source node `z`, construct two dummy nodes (z,0) and (z,1)
    #     in both the source and target graphs used by the embedder
    #   1. fix the embedding `(z,u) -> (z,u)` for all dummy nodes
    #   2. for each target block `i` with orientation `u`, and for each source
    #     node `z`, add the target edge `((z,u), i)`
    #   3. for each source node `z` and each orientation `u`, add the source
    #     edge `((z,u), z)`

    fix_chains = {}
    for z in source_n:
        for u in (0, 1):
            source_e.append(((z, u), z))
            fix_chains[z, u] = [(z, u)]
        for u, i in ublocks.values():
            fabric_e.append(((z, u), i))

    # first, grab a few embeddings in the quotient graph. this is super fast
    embs = filter(None, [find_embedding(source_e, fabric_e,
                                        fixed_chains=fix_chains,
                                        chainlength_patience=0,
                                        **args) for _ in range(10)])

    # select the best-looking candidate so far
    emb = min(embs, key=lambda e: sorted((len(c)
                                          for c in e.values()), reverse=True))

    # work down the chainlengths in our embeding
    for _ in range(10):
        emb = find_embedding(source_e, fabric_e,
                             fixed_chains=fix_chains,
                             initial_chains=emb,
                             chainlength_patience=3,
                             skip_initialization=True,
                             **args)

    # next, translate the block-embedding to a qubit-embedding
    newemb = {}
    for v in source_n:
        for k in range(L):
            newemb[v, k] = [blocks[i][k] for i in emb[v]]

    return newemb


Instruction:
How would you implement a function in Python 3 that
returns a set of resonance forms as SMILES strings given a SMILES string.

Response:
def enumerate_resonance_smiles(smiles):
    """Return a set of resonance forms as SMILES strings, given a SMILES string.

    :param smiles: A SMILES string.
    :returns: A set containing SMILES strings for every possible resonance form.
    :rtype: set of strings.
    """
    mol = Chem.MolFromSmiles(smiles)
    #Chem.SanitizeMol(mol)  # MolFromSmiles does Sanitize by default
    mesomers = ResonanceEnumerator().enumerate(mol)
    return {Chem.MolToSmiles(m, isomericSmiles=True) for m in mesomers}


Instruction:
Can you create a Python 3 function that
enumerates all possible resonance forms and return them as a list.

Response:
def enumerate(self, mol):
        """Enumerate all possible resonance forms and return them as a list.

        :param mol: The input molecule.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: A list of all possible resonance forms of the molecule.
        :rtype: list of rdkit.Chem.rdchem.Mol
        """
        flags = 0
        if self.kekule_all:
            flags = flags | Chem.KEKULE_ALL
        if self.allow_incomplete_octets:
            flags = flags | Chem.ALLOW_INCOMPLETE_OCTETS
        if self.allow_charge_separation:
            flags = flags | Chem.ALLOW_CHARGE_SEPARATION
        if self.unconstrained_anions:
            flags = flags | Chem.UNCONSTRAINED_ANIONS
        if self.unconstrained_cations:
            flags = flags | Chem.UNCONSTRAINED_CATIONS
        results = []
        for result in Chem.ResonanceMolSupplier(mol, flags=flags, maxStructs=self.max_structures):
            # This seems necessary? ResonanceMolSupplier only does a partial sanitization
            Chem.SanitizeMol(result)
            results.append(result)
        return results


Instruction:
Can you tell what is the following Python 3 function doing
def normalize(self, mol):
        log.debug('Running Normalizer')
        # Normalize each fragment separately to get around quirky RunReactants behaviour
        fragments = []
        for fragment in Chem.GetMolFrags(mol, asMols=True):
            fragments.append(self._normalize_fragment(fragment))
        # Join normalized fragments into a single molecule again
        outmol = fragments.pop()
        for fragment in fragments:
            outmol = Chem.CombineMols(outmol, fragment)
        Chem.SanitizeMol(outmol)
        return outmol

Response: Normalizes a molecule by applying a series of normalization transforms to correct functional groups and recombine charges.


Instruction:
Can you tell what is the following Python 3 function doing
def canonicalize(self, mol):
        # TODO: Overload the mol parameter to pass a list of pre-enumerated tautomers
        tautomers = self._enumerate_tautomers(mol)
        if len(tautomers) == 1:
            return tautomers[0]
        # Calculate score for each tautomer
        highest = None
        for t in tautomers:
            smiles = Chem.MolToSmiles(t, isomericSmiles=True)
            log.debug('Tautomer: %s', smiles)
            score = 0
            # Add aromatic ring scores
            ssr = Chem.GetSymmSSSR(t)
            for ring in ssr:
                btypes = {t.GetBondBetweenAtoms(*pair).GetBondType() for pair in pairwise(ring)}
                elements = {t.GetAtomWithIdx(idx).GetAtomicNum() for idx in ring}
                if btypes == {BondType.AROMATIC}:
                    log.debug('Score +100 (aromatic ring)')
                    score += 100
                    if elements == {6}:
                        log.debug('Score +150 (carbocyclic aromatic ring)')
                        score += 150
            # Add SMARTS scores
            for tscore in self.scores:
                for match in t.GetSubstructMatches(tscore.smarts):
                    log.debug('Score %+d (%s)', tscore.score, tscore.name)
                    score += tscore.score
            # Add (P,S,Se,Te)-H scores
            for atom in t.GetAtoms():
                if atom.GetAtomicNum() in {15, 16, 34, 52}:
                    hs = atom.GetTotalNumHs()
                    if hs:
                        log.debug('Score %+d (%s-H bonds)', -hs, atom.GetSymbol())
                        score -= hs
            # Set as highest if score higher or if score equal and smiles comes first alphabetically
            if not highest or highest['score'] < score or (highest['score'] == score and smiles < highest['smiles']):
                log.debug('New highest tautomer: %s (%s)', smiles, score)
                highest = {'smiles': smiles, 'tautomer': t, 'score': score}
        return highest['tautomer']

Response: Return a canonical tautomer by enumerating and scoring all possible tautomers.


Instruction:
How would you explain what the following Python 3 function does
def enumerate(self, mol):
        smiles = Chem.MolToSmiles(mol, isomericSmiles=True)
        tautomers = {smiles: copy.deepcopy(mol)}
        # Create a kekulized form of the molecule to match the SMARTS against
        kekulized = copy.deepcopy(mol)
        Chem.Kekulize(kekulized)
        kekulized = {smiles: kekulized}
        done = set()
        while len(tautomers) < self.max_tautomers:
            for tsmiles in sorted(tautomers):
                if tsmiles in done:
                    continue
                for transform in self.transforms:
                    for match in kekulized[tsmiles].GetSubstructMatches(transform.tautomer):
                        # log.debug('Matched rule: %s to %s for %s', transform.name, tsmiles, match)
                        # Create a copy of in the input molecule so we can modify it
                        # Use kekule form so bonds are explicitly single/double instead of aromatic
                        product = copy.deepcopy(kekulized[tsmiles])
                        # Remove a hydrogen from the first matched atom and add one to the last
                        first = product.GetAtomWithIdx(match[0])
                        last = product.GetAtomWithIdx(match[-1])
                        # log.debug('%s: H%s -> H%s' % (first.GetSymbol(), first.GetTotalNumHs(), first.GetTotalNumHs() - 1))
                        # log.debug('%s: H%s -> H%s' % (last.GetSymbol(), last.GetTotalNumHs(), last.GetTotalNumHs() + 1))
                        first.SetNumExplicitHs(max(0, first.GetTotalNumHs() - 1))
                        last.SetNumExplicitHs(last.GetTotalNumHs() + 1)
                        # Remove any implicit hydrogens from the first and last atoms now we have set the count explicitly
                        first.SetNoImplicit(True)
                        last.SetNoImplicit(True)
                        # Adjust bond orders
                        for bi, pair in enumerate(pairwise(match)):
                            if transform.bonds:
                                # Set the resulting bond types as manually specified in the transform
                                # log.debug('%s-%s: %s -> %s' % (product.GetAtomWithIdx(pair[0]).GetSymbol(), product.GetAtomWithIdx(pair[1]).GetSymbol(), product.GetBondBetweenAtoms(*pair).GetBondType(), transform.bonds[bi]))
                                product.GetBondBetweenAtoms(*pair).SetBondType(transform.bonds[bi])
                            else:
                                # If no manually specified bond types, just swap single and double bonds
                                current_bond_type = product.GetBondBetweenAtoms(*pair).GetBondType()
                                product.GetBondBetweenAtoms(*pair).SetBondType(BondType.DOUBLE if current_bond_type == BondType.SINGLE else BondType.SINGLE)
                                # log.debug('%s-%s: %s -> %s' % (product.GetAtomWithIdx(pair[0]).GetSymbol(), product.GetAtomWithIdx(pair[1]).GetSymbol(), current_bond_type, product.GetBondBetweenAtoms(*pair).GetBondType()))
                        # Adjust charges
                        if transform.charges:
                            for ci, idx in enumerate(match):
                                atom = product.GetAtomWithIdx(idx)
                                # log.debug('%s: C%s -> C%s' % (atom.GetSymbol(), atom.GetFormalCharge(), atom.GetFormalCharge() + transform.charges[ci]))
                                atom.SetFormalCharge(atom.GetFormalCharge() + transform.charges[ci])
                        try:
                            Chem.SanitizeMol(product)
                            smiles = Chem.MolToSmiles(product, isomericSmiles=True)
                            log.debug('Applied rule: %s to %s', transform.name, tsmiles)
                            if smiles not in tautomers:
                                log.debug('New tautomer produced: %s' % smiles)
                                kekulized_product = copy.deepcopy(product)
                                Chem.Kekulize(kekulized_product)
                                tautomers[smiles] = product
                                kekulized[smiles] = kekulized_product
                            else:
                                log.debug('Previous tautomer produced again: %s' % smiles)
                        except ValueError:
                            log.debug('ValueError Applying rule: %s', transform.name)
                done.add(tsmiles)
            if len(tautomers) == len(done):
                break
        else:
            log.warning('Tautomer enumeration stopped at maximum %s', self.max_tautomers)
        # Clean up stereochemistry
        for tautomer in tautomers.values():
            Chem.AssignStereochemistry(tautomer, force=True, cleanIt=True)
            for bond in tautomer.GetBonds():
                if bond.GetBondType() == BondType.DOUBLE and bond.GetStereo() > BondStereo.STEREOANY:
                    begin = bond.GetBeginAtomIdx()
                    end = bond.GetEndAtomIdx()
                    for othertautomer in tautomers.values():
                        if not othertautomer.GetBondBetweenAtoms(begin, end).GetBondType() == BondType.DOUBLE:
                            neighbours = tautomer.GetAtomWithIdx(begin).GetBonds() + tautomer.GetAtomWithIdx(end).GetBonds()
                            for otherbond in neighbours:
                                if otherbond.GetBondDir() in {BondDir.ENDUPRIGHT, BondDir.ENDDOWNRIGHT}:
                                    otherbond.SetBondDir(BondDir.NONE)
                            Chem.AssignStereochemistry(tautomer, force=True, cleanIt=True)
                            log.debug('Removed stereochemistry from unfixed double bond')
                            break
        return list(tautomers.values())

Response: Enumerate all possible tautomers and return them as a list.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate_smiles(smiles):
    # Skip sanitize as standardize does this anyway
    mol = Chem.MolFromSmiles(smiles)
    logs = Validator().validate(mol)
    return logs

Response: Validate a single SMILES string using default validations.


Instruction:
Create a Python 3 function to
disconnect all metal atoms from any metal and returns the original molecule with metals disconnected.

Response:
def disconnect(self, mol):
        """Break covalent bonds between metals and organic atoms under certain conditions.

        The algorithm works as follows:

        - Disconnect N, O, F from any metal.
        - Disconnect other non-metals from transition metals + Al (but not Hg, Ga, Ge, In, Sn, As, Tl, Pb, Bi, Po).
        - For every bond broken, adjust the charges of the begin and end atoms accordingly.

        :param mol: The input molecule.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: The molecule with metals disconnected.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        log.debug('Running MetalDisconnector')
        # Remove bonds that match SMARTS
        for smarts in [self._metal_nof, self._metal_non]:
            pairs = mol.GetSubstructMatches(smarts)
            rwmol = Chem.RWMol(mol)
            orders = []
            for i, j in pairs:
                # TODO: Could get the valence contributions of the bond instead of GetBondTypeAsDouble?
                orders.append(int(mol.GetBondBetweenAtoms(i, j).GetBondTypeAsDouble()))
                rwmol.RemoveBond(i, j)
            # Adjust neighbouring charges accordingly
            mol = rwmol.GetMol()
            for n, (i, j) in enumerate(pairs):
                chg = orders[n]
                atom1 = mol.GetAtomWithIdx(i)
                atom1.SetFormalCharge(atom1.GetFormalCharge() + chg)
                atom2 = mol.GetAtomWithIdx(j)
                atom2.SetFormalCharge(atom2.GetFormalCharge() - chg)
                log.info('Removed covalent bond between %s and %s', atom1.GetSymbol(), atom2.GetSymbol())
        Chem.SanitizeMol(mol)
        return mol


Instruction:
Can you generate a brief explanation for the following Python 3 code
def standardize_smiles(smiles):
    # Skip sanitize as standardize does this anyway
    mol = Chem.MolFromSmiles(smiles, sanitize=False)
    mol = Standardizer().standardize(mol)
    return Chem.MolToSmiles(mol, isomericSmiles=True)

Response: Return a standardized canonical SMILES string given a SMILES string.


Instruction:
Implement a Python 3 function for
returning a set of tautomers as SMILES strings given a SMILES string.

Response:
def enumerate_tautomers_smiles(smiles):
    """Return a set of tautomers as SMILES strings, given a SMILES string.

    :param smiles: A SMILES string.
    :returns: A set containing SMILES strings for every possible tautomer.
    :rtype: set of strings.
    """
    # Skip sanitize as standardize does this anyway
    mol = Chem.MolFromSmiles(smiles, sanitize=False)
    mol = Standardizer().standardize(mol)
    tautomers = TautomerEnumerator().enumerate(mol)
    return {Chem.MolToSmiles(m, isomericSmiles=True) for m in tautomers}


Instruction:
Here you have a function in Python 3, explain what it does
def canonicalize_tautomer_smiles(smiles):
    # Skip sanitize as standardize does this anyway
    mol = Chem.MolFromSmiles(smiles, sanitize=False)
    mol = Standardizer().standardize(mol)
    tautomer = TautomerCanonicalizer().canonicalize(mol)
    return Chem.MolToSmiles(tautomer, isomericSmiles=True)

Response: Return a standardized tautomer SMILES string given a SMILES string.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def standardize(self, mol):
        mol = copy.deepcopy(mol)
        Chem.SanitizeMol(mol)
        mol = Chem.RemoveHs(mol)
        mol = self.disconnect_metals(mol)
        mol = self.normalize(mol)
        mol = self.reionize(mol)
        Chem.AssignStereochemistry(mol, force=True, cleanIt=True)
        # TODO: Check this removes symmetric stereocenters
        return mol

Response: Return a standardized version of the given molecule.


Instruction:
Make a summary of the following Python 3 code
def tautomer_parent(self, mol, skip_standardize=False):
        if not skip_standardize:
            mol = self.standardize(mol)
        tautomer = self.canonicalize_tautomer(mol)
        tautomer = self.standardize(tautomer)
        return tautomer

Response: Return the tautomer parent of a given molecule.


Instruction:
Implement a Python 3 function for
returning the fragment parent of a given molecule.

Response:
def fragment_parent(self, mol, skip_standardize=False):
        """Return the fragment parent of a given molecule.

        The fragment parent is the largest organic covalent unit in the molecule.

        :param mol: The input molecule.
        :type mol: rdkit.Chem.rdchem.Mol
        :param bool skip_standardize: Set to True if mol has already been standardized.
        :returns: The fragment parent molecule.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        if not skip_standardize:
            mol = self.standardize(mol)
        # TODO: Consider applying FragmentRemover first to remove salts, solvents?
        fragment = self.largest_fragment(mol)
        return fragment


Instruction:
Here you have a function in Python 3, explain what it does
def stereo_parent(self, mol, skip_standardize=False):
        if not skip_standardize:
            mol = self.standardize(mol)
        else:
            mol = copy.deepcopy(mol)
        Chem.RemoveStereochemistry(mol)
        return mol

Response: Returns the stereo parent of a given molecule.


Instruction:
Write a Python 3 function for
returning the isotope parent of a given molecule.

Response:
def isotope_parent(self, mol, skip_standardize=False):
        """Return the isotope parent of a given molecule.

        The isotope parent has all atoms replaced with the most abundant isotope for that element.

        :param mol: The input molecule.
        :type mol: rdkit.Chem.rdchem.Mol
        :param bool skip_standardize: Set to True if mol has already been standardized.
        :returns: The isotope parent molecule.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        if not skip_standardize:
            mol = self.standardize(mol)
        else:
            mol = copy.deepcopy(mol)
        # Replace isotopes with common weight
        for atom in mol.GetAtoms():
            atom.SetIsotope(0)
        return mol


Instruction:
Can you implement a function in Python 3 that
returns the charge parent of a given molecule.

Response:
def charge_parent(self, mol, skip_standardize=False):
        """Return the charge parent of a given molecule.

        The charge parent is the uncharged version of the fragment parent.

        :param mol: The input molecule.
        :type mol: rdkit.Chem.rdchem.Mol
        :param bool skip_standardize: Set to True if mol has already been standardized.
        :returns: The charge parent molecule.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        # TODO: All ionized acids and bases should be neutralised.
        if not skip_standardize:
            mol = self.standardize(mol)
        fragment = self.fragment_parent(mol, skip_standardize=True)
        if fragment:
            uncharged = self.uncharge(fragment)
            # During final standardization, the Reionizer ensures any remaining charges are in the right places
            uncharged = self.standardize(uncharged)
            return uncharged


Instruction:
Can you generate the documentation for the following Python 3 function
def super_parent(self, mol, skip_standardize=False):
        if not skip_standardize:
            mol = self.standardize(mol)
        # We don't need to get fragment parent, because the charge parent is the largest fragment
        mol = self.charge_parent(mol, skip_standardize=True)
        mol = self.isotope_parent(mol, skip_standardize=True)
        mol = self.stereo_parent(mol, skip_standardize=True)
        mol = self.tautomer_parent(mol, skip_standardize=True)
        mol = self.standardize(mol)
        return mol

Response: Return the super parent of a given molecule.


Instruction:
Write a Python 3 function for
returning a canonicalized version of the TautomerCanonicalizer.

Response:
def canonicalize_tautomer(self):
        """
        :returns: A callable :class:`~molvs.tautomer.TautomerCanonicalizer` instance.
        """
        return TautomerCanonicalizer(transforms=self.tautomer_transforms, scores=self.tautomer_scores,
                                     max_tautomers=self.max_tautomers)


Instruction:
Can you generate the documentation for the following Python 3 function
def reionize(self, mol):
        log.debug('Running Reionizer')

        start_charge = Chem.GetFormalCharge(mol)

        # Apply forced charge corrections
        for cc in self.charge_corrections:
            for match in mol.GetSubstructMatches(cc.smarts):
                atom = mol.GetAtomWithIdx(match[0])
                log.info('Applying charge correction %s (%s %+d)', cc.name, atom.GetSymbol(), cc.charge)
                atom.SetFormalCharge(cc.charge)

        current_charge = Chem.GetFormalCharge(mol)
        charge_diff = Chem.GetFormalCharge(mol) - start_charge
        # If molecule is now neutral, assume everything is now fixed
        # But otherwise, if charge has become more positive, look for additional protonated acid groups to ionize
        if not current_charge == 0:
            while charge_diff > 0:
                ppos, poccur = self._strongest_protonated(mol)
                if ppos is None:
                    break
                log.info('Ionizing %s to balance previous charge corrections', self.acid_base_pairs[ppos].name)
                patom = mol.GetAtomWithIdx(poccur[-1])
                patom.SetFormalCharge(patom.GetFormalCharge() - 1)
                if patom.GetNumExplicitHs() > 0:
                    patom.SetNumExplicitHs(patom.GetNumExplicitHs() - 1)
                # else:
                patom.UpdatePropertyCache()
                charge_diff -= 1

        already_moved = set()
        while True:
            ppos, poccur = self._strongest_protonated(mol)
            ipos, ioccur = self._weakest_ionized(mol)
            if ioccur and poccur and ppos < ipos:
                if poccur[-1] == ioccur[-1]:
                    # Bad! H wouldn't be moved, resulting in infinite loop.
                    log.warning('Aborted reionization due to unexpected situation')
                    break

                key = tuple(sorted([poccur[-1], ioccur[-1]]))
                if key in already_moved:
                    log.warning('Aborting reionization to avoid infinite loop due to it being ambiguous where to put a Hydrogen')
                    break
                already_moved.add(key)

                log.info('Moved proton from %s to %s', self.acid_base_pairs[ppos].name, self.acid_base_pairs[ipos].name)

                # Remove hydrogen from strongest protonated
                patom = mol.GetAtomWithIdx(poccur[-1])
                patom.SetFormalCharge(patom.GetFormalCharge() - 1)
                # If no implicit Hs to autoremove, and at least 1 explicit H to remove, reduce explicit count by 1
                if patom.GetNumImplicitHs() == 0 and patom.GetNumExplicitHs() > 0:
                    patom.SetNumExplicitHs(patom.GetNumExplicitHs() - 1)
                    # TODO: Remove any chiral label on patom?
                patom.UpdatePropertyCache()

                # Add hydrogen to weakest ionized
                iatom = mol.GetAtomWithIdx(ioccur[-1])
                iatom.SetFormalCharge(iatom.GetFormalCharge() + 1)
                # Increase explicit H count if no implicit, or aromatic N or P, or non default valence state
                if (iatom.GetNoImplicit() or
                        ((patom.GetAtomicNum() == 7 or patom.GetAtomicNum() == 15) and patom.GetIsAromatic()) or
                        iatom.GetTotalValence() not in list(Chem.GetPeriodicTable().GetValenceList(iatom.GetAtomicNum()))):
                    iatom.SetNumExplicitHs(iatom.GetNumExplicitHs() + 1)
                iatom.UpdatePropertyCache()
            else:
                break

        # TODO: Canonical ionization position if multiple equivalent positions?

        Chem.SanitizeMol(mol)
        return mol

Response: Enforce charges on certain atoms then perform competitive reionization.


Instruction:
Can you write a function in Python 3 where it
neutralizes the molecule by adding hydrogen to positive charges and removing hydrogen from negative charges.

Response:
def uncharge(self, mol):
        """Neutralize molecule by adding/removing hydrogens.

        :param mol: The molecule to uncharge.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: The uncharged molecule.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        log.debug('Running Uncharger')
        mol = copy.deepcopy(mol)

        # Neutralize positive charges
        pos_remainder = 0
        neg_count = 0
        for atom in mol.GetAtoms():
            # Remove hydrogen from positive atoms and reduce formal change until neutral or no more hydrogens
            while atom.GetFormalCharge() > 0 and atom.GetNumExplicitHs() > 0:
                atom.SetNumExplicitHs(atom.GetNumExplicitHs() - 1)
                atom.SetFormalCharge(atom.GetFormalCharge() - 1)
                log.info('Removed positive charge')
            chg = atom.GetFormalCharge()
            if chg > 0:
                # Record number of non-neutralizable positive charges
                pos_remainder += chg
            elif chg < 0:
                # Record total number of negative charges
                neg_count += -chg

        # Choose negative charges to leave in order to balance non-neutralizable positive charges
        neg_skip = self._get_neg_skip(mol, pos_remainder)

        # Neutralize remaining negative charges
        for atom in mol.GetAtoms():
            log.info(atom.GetIdx())
            if atom.GetIdx() in neg_skip:
                continue
            # Make sure to stop when neg_count <= pos_remainder, as it is possible that neg_skip is not large enough
            while atom.GetFormalCharge() < 0 and neg_count > pos_remainder:
                atom.SetNumExplicitHs(atom.GetNumExplicitHs() + 1)
                atom.SetFormalCharge(atom.GetFormalCharge() + 1)
                neg_count -= 1
                log.info('Removed negative charge')
        return mol


Instruction:
Can you write a function in Python 3 where it
gets negatively charged atoms to skip up to pos_count.

Response:
def _get_neg_skip(self, mol, pos_count):
        """Get negatively charged atoms to skip (up to pos_count)."""
        neg_skip = set()
        if pos_count:
            # Get negative oxygens in charge-separated nitro groups TODO: Any other special cases to skip?
            for occurrence in mol.GetSubstructMatches(self.nitro):
                neg_skip.add(occurrence[-1])
                if len(neg_skip) >= pos_count:
                    return neg_skip
            # Get strongest ionized acids
            for position, pair in enumerate(self.acid_base_pairs):
                for occurrence in mol.GetSubstructMatches(pair.base):
                    neg_skip.add(occurrence[-1])
                    if len(neg_skip) >= pos_count:
                        return neg_skip
        return neg_skip


Instruction:
Write a Python 3 script to
remove the fragments from the molecule.

Response:
def remove(self, mol):
        """Return the molecule with specified fragments removed.

        :param mol: The molecule to remove fragments from.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: The molecule with fragments removed.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        log.debug('Running FragmentRemover')
        # Iterate FragmentPatterns and remove matching fragments
        for frag in self.fragments:
            # If nothing is left or leave_last and only one fragment, end here
            if mol.GetNumAtoms() == 0 or (self.leave_last and len(Chem.GetMolFrags(mol)) <= 1):
                break
            # Apply removal for this FragmentPattern
            removed = Chem.DeleteSubstructs(mol, frag.smarts, onlyFrags=True)
            if not mol.GetNumAtoms() == removed.GetNumAtoms():
                log.info('Removed fragment: %s', frag.name)
            if self.leave_last and removed.GetNumAtoms() == 0:
                # All the remaining fragments match this pattern - leave them all
                break
            mol = removed
        return mol


Instruction:
Write a Python 3 function that can
return the largest covalent unit from the given molecule.

Response:
def choose(self, mol):
        """Return the largest covalent unit.

        The largest fragment is determined by number of atoms (including hydrogens). Ties are broken by taking the
        fragment with the higher molecular weight, and then by taking the first alphabetically by SMILES if needed.

        :param mol: The molecule to choose the largest fragment from.
        :type mol: rdkit.Chem.rdchem.Mol
        :return: The largest fragment.
        :rtype: rdkit.Chem.rdchem.Mol
        """
        log.debug('Running LargestFragmentChooser')
        # TODO: Alternatively allow a list of fragments to be passed as the mol parameter
        fragments = Chem.GetMolFrags(mol, asMols=True)
        largest = None
        for f in fragments:
            smiles = Chem.MolToSmiles(f, isomericSmiles=True)
            log.debug('Fragment: %s', smiles)
            organic = is_organic(f)
            if self.prefer_organic:
                # Skip this fragment if not organic and we already have an organic fragment as the largest so far
                if largest and largest['organic'] and not organic:
                    continue
                # Reset largest if it wasn't organic and this fragment is organic
                if largest and organic and not largest['organic']:
                    largest = None
            # Count atoms
            atoms = 0
            for a in f.GetAtoms():
                atoms += 1 + a.GetTotalNumHs()
            # Skip this fragment if fewer atoms than the largest
            if largest and atoms < largest['atoms']:
                continue
            # Skip this fragment if equal number of atoms but weight is lower
            weight = rdMolDescriptors.CalcExactMolWt(f)
            if largest and atoms == largest['atoms'] and weight < largest['weight']:
                continue
            # Skip this fragment if equal atoms and equal weight but smiles comes last alphabetically
            if largest and atoms == largest['atoms'] and weight == largest['weight'] and smiles > largest['smiles']:
                continue
            # Otherwise this is the largest so far
            log.debug('New largest fragment: %s (%s)', smiles, atoms)
            largest = {'smiles': smiles, 'fragment': f, 'atoms': atoms, 'weight': weight, 'organic': organic}
        return largest['fragment']


Instruction:
How would you implement a function in Python 3 that
integrates an IVP problem of van der Polymer.

Response:
def integrate_ivp(u0=1.0, v0=0.0, mu=1.0, tend=10.0, dt0=1e-8, nt=0,
                  nsteps=600, t0=0.0, atol=1e-8, rtol=1e-8, plot=False,
                  savefig='None', method='bdf', dpi=100, verbose=False):
    """
    Example program integrating an IVP problem of van der Pol oscillator
    """
    f, j = get_f_and_j(mu)
    if nt > 1:
        tout = np.linspace(t0, tend, nt)
        yout, nfo = integrate_predefined(
            f, j, [u0, v0], tout, dt0, atol, rtol, nsteps=nsteps,
            check_indexing=False, method=method)
    else:
        tout, yout, nfo = integrate_adaptive(
            f, j, [u0, v0], t0, tend, dt0, atol, rtol, nsteps=nsteps,
            check_indexing=False, method=method)  # dfdt[:] also for len == 1
    if verbose:
        print(nfo)
    if plot:
        import matplotlib.pyplot as plt
        plt.plot(tout, yout[:, 1], 'g--')
        plt.plot(tout, yout[:, 0], 'k-', linewidth=2)
        if savefig == 'None':
            plt.show()
        else:
            plt.savefig(savefig, dpi=dpi)


Instruction:
Can you implement a function in Python 3 that
integrates an adaptive system of ordinary differential equations.

Response:
def integrate_adaptive(rhs, jac, y0, x0, xend, atol, rtol, dx0=.0,
                       dx_min=.0, dx_max=.0, nsteps=500, method=None, nderiv=0,
                       roots=None, nroots=0, return_on_root=False,
                       check_callable=False, check_indexing=False,
                       **kwargs):
    """ Integrates a system of ordinary differential equations.

    Solves the initial value problem (IVP) defined by the user supplied
    arguments. The solver chooses at what values of the independent variable
    results should be reported.

    Parameters
    ----------
    rhs : callable
        Function with signature f(t, y, fout) which modifies fout *inplace*.
    jac : callable
        Function with signature either jac(t, y, jmat_out, dfdx_out) for
        dense/banded jacobians, or jac(t, y, data, colptrs, rowvals) for
        sparse (CSC) jacobians. ``jac`` should modify ``jmat_out``, ``dfdx_out``
        (dense, banded) or (``data``, ``colptrs``, ``rowvals``) *inplace*.
        (see also ``lband``, ``uband``, ``nnz``)
    y0 : array_like
        Initial values of the dependent variables.
    x0 : float
        Initial value of the independent variable.
    xend : float
        Stopping value for the independent variable.
    dx0 : float
        Initial step-size.
    atol : float
        Absolute tolerance.
    rtol : float
        Relative tolerance.
    dx_min : float
        Minimum step (default: 0.0).
    dx_max : float
        Maximum step (default: 0.0).
    nsteps : int
        Maximum number of steps (default: 500).
    method : str
        One of: 'adams' or 'bdf' (default: 'bdf')
    nderiv : int
        Number of derivatives (default: 0).
    roots : callback
        With signature ``roots(x, yarr[:ny], out[:nroots]) -> None``.
    nroots : int
        Number of root functions in roots.
    return_on_root : bool
        Exit early (on first found root).
    check_callable : bool
        perform signature sanity checks on ``rhs`` and ``jac``
    check_indexing : bool
        Perform item setting sanity checks on ``rhs`` and ``jac``.
    \*\*kwargs:
        'lband' : int
            Number of lower bands.
            Indexing: ``banded[row_i - col_i + uband, col_i]``.
        'uband' : int
            Number of upper bands.
            Indexing: ``banded[row_i - col_i + uband, col_i]``.
        'iter_type' : str (default: 'default')
            One of: 'default', 'functional', 'newton'
        'linear_solver': str (default: 'default')
            One of: 'default', 'dense', 'banded', 'gmres',
            'gmres_classic', 'bicgstab', 'tfqmr'
        'return_on_error' : bool
            Returns on error without raising an excpetion (with ``'success'==False``).
        'autorestart' : int
            Useful for autonomous systems where conditions change during integration.
            Will restart the integration with ``x==0``. Maximum number of steps is then
            given by ``autorestart * nsteps``.
        'record_rhs_xvals' : bool
            When True: will return x values for rhs calls in ``info['rhs_xvals']``.
        'record_jac_xvals' : bool
            When True will return x values for jac calls in ``info['jac_xvals']``.
        'record_order' : bool
            When True will return used time stepper order in ``info['orders']``.
        'record_fpe' : bool
            When True will return observed floating point errors in ``info['fpes']``. (see ``fpes``)
        'record_steps' : bool
            When True will return stepsizes taken in ``info['steps']``.
        'dx0cb' : callable
            Callback for calculating dx0 (make sure to pass ``dx0==0.0``) to enable.
            Signature: ``f(x, y[:]) -> float``.
        'dx_max_cb' : callable
            Callback for calculating dx_max.
            Signature: ``f(x, y[:]) -> float``.
        'autonomous_exprs' bool
            Whether expressions contain the independent variable. If not, autorestart
            is allowed to shift the independent variable to zero at restart).
        'nnz' : int
            Maximum number of nonzero entries in the sparse (CSC) jacobian (default: -1).
            Must set ``nnz >= 0`` and ``linear_solver`` to 'klu' to enable use of sparse
            ``jac`` signature.
        'jtimes' : callable
            Function with signature f(v, Jv, t, y, fy) to calculate the product of the
            Jacobian evaluated at t, y with a vector v. Should modify Jv *inplace*.
            For use with linear solvers 'gmres', 'gmres_classic', 'bicgstab', 'tfqmr'.
        'ew_ele' : bool
            Whether to return error_weights, estimated_local_errors in info dict.
        'constraints': array
            Per component constraints 0.0: no constraint, 1.0: >=0, -1.0: <=0, 2.0: >0.0, -2.0: <0.0.

    Returns
    -------
    (xout, yout, info):
        xout: 1-dimensional array of values for the independent variable
        yout: 2-dimensional array of the dependent variables (axis 1) for
            values corresponding to xout (axis 0).
        info: Dictionary with information about the integration.

    """
    # Sanity checks to reduce risk of having a segfault:
    lband, uband = kwargs.get('lband', None), kwargs.get('uband', None)
    nnz = kwargs.get('nnz', None)
    _check_jac_type(lband=lband, uband=uband, nnz=nnz)

    if check_callable:
        _check_callable(rhs, jac, x0, y0, lband, uband, nnz)

    if check_indexing:
        _check_indexing(rhs, jac, x0, y0, lband, uband, nnz)

    return adaptive(rhs, jac, np.ascontiguousarray(y0, dtype=np.float64), x0, xend,
                    atol, rtol, method or ('adams' if jac is None else 'bdf'),
                    nsteps, dx0, dx_min, dx_max, nderiv=nderiv, roots=roots, nroots=nroots,
                    return_on_root=return_on_root, **kwargs)


Instruction:
How would you explain what the following Python 3 function does
def integrate_predefined(rhs, jac, y0, xout, atol, rtol, jac_type="dense",
                         dx0=.0, dx_min=.0, dx_max=.0, nsteps=500, method=None,
                         nderiv=0, roots=None, nroots=0, check_callable=False,
                         check_indexing=False, **kwargs):
    """ Integrates a system of ordinary differential equations.

    Solves the initial value problem (IVP) defined by the user supplied
    arguments. The user chooses at what values of the independent variable
    results should be reported.

    Parameters
    ----------
    rhs : callable
        Function with signature f(t, y, fout) which modifies fout *inplace*.
    jac : callable
        Function with signature either jac(t, y, jmat_out, dfdx_out) for
        dense/banded jacobians, or jac(t, y, data, colptrs, rowvals) for
        sparse (CSC) jacobians. ``jac`` should modify ``jmat_out``, ``dfdx_out``
        (dense, banded) or (``data``, ``colptrs``, ``rowvals``) *inplace*.
        (see also ``lband``, ``uband``, ``nnz``)
    y0 : array_like
        Initial values of the dependent variables.
    xout : array_like
        Values of the independent variable.
    dx0 : float
        Initial step-size.
    atol : float
        Absolute tolerance.
    rtol : float
        Relative tolerance.
    dx_min : float
        Minimum step (default: 0.0).
    dx_max : float
        Maximum step (default: 0.0).
    nsteps : int
        Maximum number of steps (default: 500).
    method : str
        One of: 'adams' or 'bdf' (default: 'bdf').
    nderiv : int
        Number of derivatives (default: 0).
    roots : callback (default: None)
        With signature ``roots(x, yarr[:ny], out[:nroots]) -> None``,
        see info['root_indices'], note that xout is unaffected.
    nroots : int (default: 0)
        Number of root functions in roots.
    check_callable : bool (default: False)
        Perform signature sanity checks on ``rhs`` and ``jac``.
    check_indexing : bool (default: False)
        Perform item setting sanity checks on ``rhs`` and ``jac``.
    \*\*kwargs:
        'lband' : int
            Number of lower bands.
            Indexing: ``banded[row_i - col_i + uband, col_i]``.
        'uband' : int
            Number of upper bands.
            Indexing: ``banded[row_i - col_i + uband, col_i]``.
        'iter_type' : str (default: 'default')
            One of: 'default', 'functional', 'newton'.
        'linear_solver' : str (default: 'default')
            One of: 'default', 'dense', 'banded', 'gmres',
            'gmres_classic', 'bicgstab', 'tfqmr', 'klu'.
        'return_on_error' : bool
            Returns on error without raising an excpetion (with ``'success'==False``).
        'autorestart' : int
            Useful for autonomous systems where conditions change during integration.
            Will restart the integration with ``x==0``. Maximum number of steps is then
            given by ``2**autorestart * nsteps``.
        'record_rhs_xvals' : bool
            When True: will return x values for rhs calls in ``info['rhs_xvals']``.
        'record_jac_xvals' : bool
            When True will return x values for jac calls in ``info['jac_xvals']``.
        'record_order' : bool
            When True will return used time stepper order in ``info['orders']``.
        'record_fpe' : bool
            When True will return observed floating point errors in ``info['fpes']``. (see ``fpes``)
        'record_steps' : bool
            When True will return stepsizes taken in ``info['steps']``.
        'dx0cb': callable
            Callback for calculating dx0 (make sure to pass ``dx0==0.0``) to enable.
            Signature: ``f(x, y[:]) -> float``.
        'dx_max_cb' : callable
            Callback for calculating dx_max.
            Signature: ``f(x, y[:]) -> float``.
        'autonomous_exprs' : bool
            Whether expressions contain the independent variable. If not, autorestart
            is allowed to shift the independent variable to zero at restart).
        'nnz' : int
            Maximum number of nonzero entries in the sparse (CSC) jacobian (default: -1).
            Must set ``nnz >= 0`` and ``linear_solver`` to 'klu' to enable use of sparse
            ``jac`` signature.
        'jtimes' : callable
            Function with signature f(v, Jv, t, y, fy) to calculate the product of the
            Jacobian evaluated at t, y with a vector v. Should modify Jv *inplace*.
            For use with linear solvers 'gmres', 'gmres_classic', 'bicgstab', 'tfqmr'.
        'ew_ele' : bool
            Whether to return error_weights, estimated_local_errors in info dict.
        'constraints': array
            Per component constraints 0.0: no constraint, 1.0: >=0, -1.0: <=0, 2.0: >0.0, -2.0: <0.0.

    Returns
    -------
    (yout, info):
        yout: 2-dimensional array of the dependent variables (axis 1) for
            values corresponding to xout (axis 0)
        info: Dictionary with information about the integration.

    """
    # Sanity checks to reduce risk of having a segfault:
    x0 = xout[0]
    lband, uband = kwargs.get('lband', None), kwargs.get('uband', None)
    nnz = kwargs.get('nnz', None)
    _check_jac_type(lband=lband, uband=uband, nnz=nnz)

    if check_callable:
        _check_callable(rhs, jac, x0, y0, lband, uband, nnz)

    if check_indexing:
        _check_indexing(rhs, jac, x0, y0, lband, uband, nnz)

    return predefined(
        rhs, jac,
        np.ascontiguousarray(y0, dtype=np.float64),
        np.ascontiguousarray(xout, dtype=np.float64),
        atol, rtol, method or ('adams' if jac is None else 'bdf'),
        nsteps, dx0, dx_min, dx_max, nderiv=nderiv, roots=roots,
        nroots=nroots, **kwargs)

Response: Integrates a system of ordinary differential equations with the given initial values.


Instruction:
Write a Python 3 script for
getting the statistics from the given organization with the given credentials. Will not retreive data if file exists and force is set to True.

Response:
def get_stats(self, username='', password='', organization='llnl',
        force=True, repo_type='public'):
        """
        Retrieves the statistics from the given organization with the given
        credentials. Will not retreive data if file exists and force hasn't been
        set to True. This is to save GH API requests.
        """
        date = str(datetime.date.today())
        file_path =  ('../github_stats_output/' + date[:4] + '/' + date[:7] + '/'
            + date + '.csv')
        if force or not os.path.isfile(file_path):
            my_github.login(username, password)
            calls_beginning = self.logged_in_gh.ratelimit_remaining + 1
            print 'Rate Limit: ' + str(calls_beginning)
            my_github.get_org(organization)
            count_members = my_github.get_mems_of_org()
            count_teams = my_github.get_teams_of_org()
            my_github.repos(repo_type=repo_type, organization=organization)
            #Write JSON
            my_github.write_org_json(dict_to_write=self.members_json,
                path_ending_type='members', is_list=True)
            my_github.write_org_json(dict_to_write=
                {'singleton': self.org_retrieved.to_json()},
                path_ending_type='organization')
            my_github.write_org_json(dict_to_write=self.teams_json,
                path_ending_type='teams', is_list=True)

            my_github.write_repo_json(dict_to_write=self.repos_json,
                path_ending_type='repo')
            my_github.write_repo_json(dict_to_write=self.contributors_json,
                path_ending_type='contributors', is_list=True)
            my_github.write_repo_json(dict_to_write=self.pull_requests_json,
                path_ending_type='pull-requests', is_list=True)
            my_github.write_repo_json(dict_to_write=self.issues_json,
                path_ending_type='issues', is_list=True)
            my_github.write_repo_json(dict_to_write=self.languages_json,
                path_ending_type='languages', is_dict=True)
            my_github.write_repo_json(dict_to_write=self.commits_json,
                path_ending_type='commits', is_list=True)
            #Write CSV
            my_github.write_to_file(file_path,
                                    date,
                                    organization,
                                    count_members,
                                    count_teams)
            calls_remaining = self.logged_in_gh.ratelimit_remaining
            calls_used = calls_beginning - calls_remaining
            print ('Rate Limit Remaining: ' + str(calls_remaining) + '\nUsed '
                + str(calls_used) + ' API calls.')


Instruction:
Can you generate the documentation for the following Python 3 function
def get_mems_of_org(self):
        print 'Getting members.'
        counter = 0
        for member in self.org_retrieved.iter_members():
            self.members_json[member.id] = member.to_json()
            counter += 1
        return counter

Response: Returns the number of members of the organization.


Instruction:
Implement a function in Python 3 to
get the number of teams of the organization.

Response:
def get_teams_of_org(self):
        """
        Retrieves the number of teams of the organization.
        """
        print 'Getting teams.'
        counter = 0
        for team in self.org_retrieved.iter_teams():
            self.teams_json[team.id] = team.to_json()
            counter += 1
        return counter


Instruction:
Here you have a function in Python 3, explain what it does
def repos(self, repo_type='public', organization='llnl'):
        print 'Getting repos.'
        for repo in self.org_retrieved.iter_repos(type=repo_type):
            #JSON
            json = repo.to_json()
            self.repos_json[repo.name] = json
            #CSV
            temp_repo = my_repo.My_Repo()
            temp_repo.name = repo.full_name
            self.total_repos += 1
            temp_repo.contributors = my_github.get_total_contributors(repo)
            self.total_contributors += temp_repo.contributors
            temp_repo.forks = repo.forks_count
            self.total_forks += temp_repo.forks
            temp_repo.stargazers = repo.stargazers
            self.total_stars += temp_repo.stargazers
            temp_repo.pull_requests_open, temp_repo.pull_requests_closed = \
                my_github.get_pull_reqs(repo)
            temp_repo.pull_requests = (temp_repo.pull_requests_open
                + temp_repo.pull_requests_closed)
            self.total_pull_reqs += temp_repo.pull_requests_open
            self.total_pull_reqs += temp_repo.pull_requests_closed
            self.total_pull_reqs_open += temp_repo.pull_requests_open
            self.total_pull_reqs_closed += temp_repo.pull_requests_closed
            temp_repo.open_issues = repo.open_issues_count
            self.total_open_issues += temp_repo.open_issues
            temp_repo.closed_issues = my_github.get_issues(repo, organization=organization)
            temp_repo.issues = temp_repo.closed_issues + temp_repo.open_issues
            self.total_closed_issues += temp_repo.closed_issues
            self.total_issues += temp_repo.issues
            my_github.get_languages(repo, temp_repo)
            temp_repo.readme = my_github.get_readme(repo)
            #temp_repo.license = my_github.get_license(repo)
            temp_repo.commits = self.get_commits(repo=repo, organization=organization)
            self.total_commits += temp_repo.commits
            self.all_repos.append(temp_repo)

Response: This method extracts the info about the repos of the current organization.


Instruction:
Can you write a function in Python 3 where it
returns the number of contributors to a repo in the organization.

Response:
def get_total_contributors(self, repo):
        """
        Retrieves the number of contributors to a repo in the organization.
        Also adds to unique contributor list.
        """
        repo_contributors = 0
        for contributor in repo.iter_contributors():
            repo_contributors += 1
            self.unique_contributors[contributor.id].append(repo.name)
            self.contributors_json[repo.name].append(contributor.to_json())
        return repo_contributors


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_pull_reqs(self, repo):
        pull_reqs_open = 0
        pull_reqs_closed = 0
        for pull_request in repo.iter_pulls(state='all'):
            self.pull_requests_json[repo.name].append(pull_request.to_json())
            if pull_request.closed_at is not None:
                pull_reqs_closed += 1
            else:
                pull_reqs_open += 1
        return pull_reqs_open, pull_reqs_closed

Response: Retrieves the number of pull requests on a repo in the organization.


Instruction:
Here you have a function in Python 3, explain what it does
def get_issues(self, repo, organization='llnl'):
        #JSON
        path = ('../github-data/' + organization + '/' + repo.name + '/issues')
        is_only_today = False
        if not os.path.exists(path): #no previous path, get all issues
            all_issues = repo.iter_issues(state='all')
            is_only_today = True
        else:
            files = os.listdir(path)
            date = str(files[-1][:-5])
            if date == str(datetime.date.today()):
                #most recent date is actually today, get previous most recent date
                if len(files) > 2:
                    date = str(files[-2][:-5])
                else:
                    #This means there is only one file, today. Retrieve every issue
                    all_issues = repo.iter_issues(state='all')
                    is_only_today = True
            if not is_only_today:#there's a previous saved JSON that's not today
                all_issues = repo.iter_issues(since=date, state='all')
        for issue in all_issues:
            self.issues_json[repo.name].append(issue.to_json())
        #CSV
        closed_issues = 0
        for issue in repo.iter_issues(state='closed'):
            if issue is not None:
                closed_issues += 1
        return closed_issues

Response: Retrieves the number of closed issues in a repository.


Instruction:
Can you tell what is the following Python 3 function doing
def get_languages(self, repo, temp_repo):
        try:
            self.languages[repo.language] += 1
        except KeyError:
            count = self.languages[repo.language] = 1
        for repo_languages in repo.iter_languages():
            self.languages_json[repo.name][repo_languages[0]] = repo_languages[1]
            for language in repo_languages:
                if isinstance(language, basestring):#is language
                    temp_repo.languages.append(language)
                    self.previous_language = language
                else:#record size bytes of language
                    try:
                        self.languages_size[self.previous_language] += \
                            language
                    except KeyError:
                        size = self.languages_size[self.previous_language] \
                            = language

Response:
Retrieves the languages used in the repository and increments the respective
        counts of those languages.


Instruction:
Write a Python 3 function for
checking to see if the given repo has a readme. MD means it has a correct readme. MIS means it has a correct readme. MIS means it has a correct readme. MIS means it has a correct readme. MIS means it has a correct readme. MIS means it has a correct readme.

Response:
def get_readme(self, repo):
        """
        Checks to see if the given repo has a ReadMe. MD means it has a correct
        Readme recognized by GitHub.
        """
        readme_contents = repo.readme()
        if readme_contents is not None:
            self.total_readmes += 1
            return 'MD'
        if self.search_limit >= 28:
            print 'Hit search limit. Sleeping for 60 sec.'
            time.sleep(60)
            self.search_limit = 0
        self.search_limit += 1
        search_results = self.logged_in_gh.search_code('readme'
            + 'in:path repo:' + repo.full_name)
        try:
            for result in search_results:
                path = result.path[1:]
                if '/' not in path and 'readme' in path.lower():
                    self.total_readmes += 1
                    return path
            return 'MISS'
        except (github3.models.GitHubError, StopIteration) as e:
            return 'MISS'


Instruction:
Create a Python 3 function for
checking to see if the given repo has a top level LICENSE file. Returns the path of the LICENSE file.

Response:
def get_license(self, repo):
        """
        Checks to see if the given repo has a top level LICENSE file.
        """
        if self.search_limit >= 28:
            print 'Hit search limit. Sleeping for 60 sec.'
            time.sleep(60)
            self.search_limit = 0
        self.search_limit += 1
        search_results = self.logged_in_gh.search_code('license'
            + 'in:path repo:' + repo.full_name)
        try:
            for result in search_results:
                path = result.path[1:]
                if '/' not in path and 'license' in path.lower():
                    self.total_licenses += 1
                    return path
            return 'MISS'
        except (StopIteration) as e:
            return 'MISS'


Instruction:
Can you write a function in Python 3 where it
gets the number of commits to a repo in the organization.

Response:
def get_commits(self, repo, organization='llnl'):
        """
        Retrieves the number of commits to a repo in the organization. If it is
        the first time getting commits for a repo, it will get all commits and
        save them to JSON. If there are previous commits saved, it will only get
        commits that have not been saved to disk since the last date of commits.
        """
        #JSON
        path = ('../github-data/' + organization + '/' + repo.name + '/commits')
        is_only_today = False
        if not os.path.exists(path): #no previous path, get all commits
            all_commits = repo.iter_commits()
            is_only_today = True
        else:
            files = os.listdir(path)
            date = str(files[-1][:-5])
            if date == str(datetime.date.today()):
                #most recent date is actually today, get previous most recent date
                if len(files) > 2:
                    date = str(files[-2][:-5])
                else:
                    #This means there is only one file, today. Retrieve every commit
                    all_commits = repo.iter_commits()
                    is_only_today = True
            if not is_only_today:#there's a previous saved JSON that's not today
                all_commits = repo.iter_commits(since=date)
        for commit in all_commits:
            self.commits_json[repo.name].append(commit.to_json())
        #for csv
        count = 0
        for commit in repo.iter_commits():
            count += 1
        return count


Instruction:
How would you explain what the following Python 3 function does
def write_org_json(self, date=(datetime.date.today()),
        organization='llnl',dict_to_write={}, path_ending_type='',
        is_list=False):
        """
        Writes stats from the organization to JSON.
        """
        path = ('../github-data/' + organization + '-org/'
            + path_ending_type + '/' + str(date) + '.json')
        self.checkDir(path)
        with open(path, 'w') as out_clear:#clear old data
            out_clear.close()
        with open(path, 'a') as out:
            if is_list:#used for list of items
                out.write('[')
            for item in dict_to_write:
                out.write(json.dumps(dict_to_write[item], sort_keys=True,
                    indent=4, separators=(',', ': ')) + ',')
            out.seek(-1, os.SEEK_END)#kill last comma
            out.truncate()
            if is_list:
                out.write(']')
        out.close()

Response: Writes stats from the organization to JSON.


Instruction:
Can you write a function in Python 3 where it
writes repo specific data to JSON.

Response:
def write_repo_json(self, date=(datetime.date.today()),
        organization='llnl', dict_to_write={}, path_ending_type='',
        is_list=False, is_dict=False):
        """
        #Writes repo specific data to JSON.
        """
        for repo in dict_to_write:
            path = ('../github-data/' + organization + '/' + repo + '/' +
                 path_ending_type + '/' + str(date) + '.json')
            self.checkDir(path)
            with open(path, 'w') as out:
                if is_list:
                    out.write('[')
                    for value in dict_to_write[repo]:
                        if is_dict:
                            for inner_dict in value:
                                out.write(json.dumps(inner_dict, sort_keys=True,
                                    indent=4, separators=(',', ': ')) + ',')
                        else:
                            out.write(json.dumps(value, sort_keys=True,
                                indent=4, separators=(',', ': ')) + ',')
                    out.seek(-1, os.SEEK_END)#kill last comma
                    out.truncate()
                    out.write(']')
                else:
                    out.write(json.dumps(dict_to_write[repo], sort_keys=True,
                        indent=4, separators=(',', ': ')))
            out.close()


Instruction:
Implement a function in Python 3 to
write the current information to a file.

Response:
def write_to_file(self, file_path='', date=str(datetime.date.today()),
        organization='N/A', members=0, teams=0):
        """
        Writes the current organization information to file (csv).
        """
        self.checkDir(file_path)
        with open(file_path, 'w+') as output:
            output.write('date,organization,members,teams,unique_contributors,'
            + 'repository,contributors,forks,stargazers,pull_requests,'
            + 'open_issues,has_readme,has_license,languages,pull_requests_open,'
            + 'pull_requests_closed,commits,closed_issues,issues\n' + date + ','
            + organization + ',' + str(members) + ',' + str(teams) + ','
            + str(len(self.unique_contributors)) + '\n')
            for repo in self.all_repos:
                output.write(',,,,,' + repo.name + ',' + str(repo.contributors)
                    + ',' + str(repo.forks) + ','
                    + str(repo.stargazers) + ',' + str(repo.pull_requests) + ','
                    + str(repo.open_issues) + ',' + str(repo.readme) + ','
                    + str(repo.license) + ',' + ' '.join(sorted(repo.languages))
                    + ',' + str(repo.pull_requests_open) + ','
                    + str(repo.pull_requests_closed) + ',' + str(repo.commits)
                    + ',' + str(repo.closed_issues) + ',' + str(repo.issues)
                    + '\n')
            output.write(',,,,total,' + str(self.total_repos) + ','
                + str(self.total_contributors) + ','
                + str(self.total_forks) + ',' + str(self.total_stars) + ','
                + str(self.total_pull_reqs) + ',' + str(self.total_open_issues)
                + ',' + str(self.total_readmes) + ',' + str(self.total_licenses)
                + ',,' + str(self.total_pull_reqs_open) + ','
                + str(self.total_pull_reqs_closed) + ','
                + str(self.total_commits) + ',' + str(self.total_closed_issues)
                + ',' + str(self.total_issues))
        output.close()
        #Update total
        self.write_totals(file_path="../github_stats_output/total.csv", date=date,
            organization=organization, members=members, teams=teams)
        #Update language sizes
        self.write_languages(file_path='../github_stats_output/languages.csv',
            date=date)


Instruction:
Make a summary of the following Python 3 code
def write_totals(self, file_path='', date=str(datetime.date.today()),
        organization='N/A', members=0, teams=0):
        """
        Updates the total.csv file with current data.
        """

        total_exists = os.path.isfile(file_path)
        with open(file_path, 'a') as out_total:
            if not total_exists:
                out_total.write('date,organization,repos,members,teams,'
                    + 'unique_contributors,total_contributors,forks,'
                    + 'stargazers,pull_requests,open_issues,has_readme,'
                    + 'has_license,pull_requests_open,pull_requests_closed,'
                    + 'commits,id,closed_issues,issues\n')
            self.delete_last_line(date=date, file_path=file_path)
        out_total.close()
        with open(file_path, 'r') as file_read:
            row_count = sum(1 for row in file_read) - 1
        file_read.close()
        with open(file_path, 'a') as out_total:
            out_total.write(date + ',' + organization + ','
                + str(self.total_repos) + ',' + str(members) + ',' + str(teams)
                + ',' + str(len(self.unique_contributors)) + ','
                + str(self.total_contributors) + ',' + str(self.total_forks)
                + ',' + str(self.total_stars) + ',' + str(self.total_pull_reqs)
                + ',' + str(self.total_open_issues) + ','
                + str(self.total_readmes) + ',' + str(self.total_licenses) + ','
                + str(self.total_pull_reqs_open) + ','
                + str(self.total_pull_reqs_closed) + ','
                + str(self.total_commits) + ',' + str(row_count) + ','
                + str(self.total_closed_issues) + ',' + str(self.total_issues)
                + '\n')
        out_total.close()

Response: Updates the total. csv file with current data.


Instruction:
How would you code a function in Python 3 to
update languages. csv file with current data.

Response:
def write_languages(self, file_path='',date=str(datetime.date.today())):
        """
        Updates languages.csv file with current data.
        """
        self.remove_date(file_path=file_path, date=date)
        languages_exists = os.path.isfile(file_path)
        with open(file_path, 'a') as out_languages:
            if not languages_exists:
                out_languages.write('date,language,count,size,size_log\n')
            languages_sorted = sorted(self.languages_size)
            #self.delete_last_line(date=date, file_path=file_path)
            for language in languages_sorted:
                try:
                    out_languages.write(date + ',' + language + ','
                        + str(self.languages[language]) + ','
                        + str(self.languages_size[language]) + ','
                        + str(math.log10(int(self.languages_size[language])))
                        + '\n')
                except (TypeError, KeyError) as e:
                    out_languages.write(date + ',' + language + ','
                        + str(0) + ','
                        + str(self.languages_size[language]) + ','
                        + str(math.log10(int(self.languages_size[language])))
                        + '\n')


Instruction:
Here you have a function in Python 3, explain what it does
def checkDir(self, file_path=''):
        if not os.path.exists(os.path.dirname(file_path)):
            try:
                os.makedirs(os.path.dirname(file_path))
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

Response: Checks if a directory exists. If not creates one with the specified file_path.


Instruction:
Create a Python 3 function to
remove all rows of the associated date from the given csv file.

Response:
def remove_date(self, file_path='', date=str(datetime.date.today())):
        """
        Removes all rows of the associated date from the given csv file.
        Defaults to today.
        """
        languages_exists = os.path.isfile(file_path)
        if languages_exists:
            with open(file_path, 'rb') as inp, open('temp.csv', 'wb') as out:
                writer = csv.writer(out)
                for row in csv.reader(inp):
                    if row[0] != date:
                        writer.writerow(row)
            inp.close()
            out.close()
            os.remove(file_path)
            os.rename("temp.csv",file_path)


Instruction:
Make a summary of the following Python 3 code
def delete_last_line(self, file_path='', date=str(datetime.date.today())):
        deleted_line = False
        if os.path.isfile(file_path):
            with open(file_path, 'r+') as file:
                reader = csv.reader(file, delimiter=',')
                for row in reader:
                    if date == row[0]:
                        file.seek(0, os.SEEK_END)
                        pos = file.tell() - 1
                        while pos > 0 and file.read(1) != "\n":
                            pos -= 1
                            file.seek(pos, os.SEEK_SET)
                        if pos > 0:
                            file.seek(pos, os.SEEK_SET)
                            file.truncate()
                            deleted_line = True
                            break
                if deleted_line: file.write('\n')
            file.close()

Response: Delete the last line of the file that is not in the file.


Instruction:
Here you have a function in Python 3, explain what it does
def gov_orgs():
    us_gov_github_orgs = set()

    gov_orgs = requests.get('https://government.github.com/organizations.json').json()

    us_gov_github_orgs.update(gov_orgs['governments']['U.S. Federal'])
    us_gov_github_orgs.update(gov_orgs['governments']['U.S. Military and Intelligence'])
    us_gov_github_orgs.update(gov_orgs['research']['U.S. Research Labs'])

    return list(us_gov_github_orgs)

Response: Returns a list of the names of US Government GitHub organizations


Instruction:
Implement a Python 3 function for
creating a github3. py session connected to GitHub. com

Response:
def create_session(token=None):
    """
    Create a github3.py session connected to GitHub.com

    If token is not provided, will attempt to use the GITHUB_API_TOKEN
    environment variable if present.
    """
    if token is None:
        token = os.environ.get('GITHUB_API_TOKEN', None)

    gh_session = github3.login(token=token)

    if gh_session is None:
        raise RuntimeError('Invalid or missing GITHUB_API_TOKEN')

    return gh_session


Instruction:
Can you write a function in Python 3 where it
creates a github3. py session for a GitHub Enterprise instance.

Response:
def create_enterprise_session(url, token=None):
    """
    Create a github3.py session for a GitHub Enterprise instance

    If token is not provided, will attempt to use the GITHUB_API_TOKEN
    environment variable if present.
    """

    gh_session = github3.enterprise_login(url=url, token=token)

    if gh_session is None:
        msg = 'Unable to connect to GitHub Enterprise (%s) with provided token.'
        raise RuntimeError(msg, url)

    return gh_session


Instruction:
Can you generate the documentation for the following Python 3 function
def _check_api_limits(gh_session, api_required=250, sleep_time=15):
    api_rates = gh_session.rate_limit()

    api_remaining = api_rates['rate']['remaining']
    api_reset = api_rates['rate']['reset']
    logger.debug('Rate Limit - %d requests remaining', api_remaining)

    if api_remaining > api_required:
        return

    now_time = time.time()
    time_to_reset = int(api_reset - now_time)
    logger.warn('Rate Limit Depleted - Sleeping for %d seconds', time_to_reset)

    while now_time < api_reset:
        time.sleep(10)
        now_time = time.time()

    return

Response:
Simplified check for API limits
    If necessary sleep in place waiting for API to reset before returning.


Instruction:
Write a Python 3 script for
connecting to a GitHub session

Response:
def connect(url='https://github.com', token=None):
    """
    Create a GitHub session for making requests
    """

    gh_session = None
    if url == 'https://github.com':
        gh_session = create_session(token)
    else:
        gh_session = create_enterprise_session(url, token)

    if gh_session is None:
        msg = 'Unable to connect to (%s) with provided token.'
        raise RuntimeError(msg, url)

    logger.info('Connected to: %s', url)

    return gh_session


Instruction:
Can you implement a function in Python 3 that
yields GitHub3. py repo objects for the provided organizations and repositories.

Response:
def query_repos(gh_session, orgs=None, repos=None, public_only=True):
    """
    Yields GitHub3.py repo objects for provided orgs and repo names

    If orgs and repos are BOTH empty, execute special mode of getting ALL
    repositories from the GitHub Server.

    If public_only is True, will return only those repos that are marked as
    public. Set this to false to return all organizations that the session has
    permissions to access.
    """

    if orgs is None:
        orgs = []
    if repos is None:
        repos = []
    if public_only:
        privacy = 'public'
    else:
        privacy = 'all'

    _check_api_limits(gh_session, 10)

    for org_name in orgs:
        org = gh_session.organization(org_name)
        num_repos = org.public_repos_count

        _check_api_limits(gh_session, _num_requests_needed(num_repos))

        for repo in org.repositories(type=privacy):
            _check_api_limits(gh_session, 10)
            yield repo

    for repo_name in repos:
        _check_api_limits(gh_session, 10)
        org, name = repo_name.split('/')
        yield gh_session.repository(org, name)

    if not (orgs or repos):
        for repo in gh_session.all_repositories():
            yield repo


Instruction:
Create a Python 3 function to
get the stats for the users of the given organization.

Response:
def get_stats(self, username='', password='', organization='llnl', force=True):
        """
        Retrieves the traffic for the users of the given organization.
        Requires organization admin credentials token to access the data.
        """
        date = str(datetime.date.today())
        stargazers_file_path =  ('../github_stats_output/stargazers.csv')
        if force or not os.path.isfile(file_path):
            my_github.login(username, password)
            calls_beginning = self.logged_in_gh.ratelimit_remaining + 1
            print 'Rate Limit: ' + str(calls_beginning)
            my_github.get_org(organization)
            my_github.get_repos()
            my_github.write_to_file(file_path=stargazers_file_path)
            #my_github.write_to_file(file_path=stargazers_file_path)
            calls_remaining = self.logged_in_gh.ratelimit_remaining
            calls_used = calls_beginning - calls_remaining
            print ('Rate Limit Remaining: ' + str(calls_remaining) + '\nUsed '
                + str(calls_used) + ' API calls.')


Instruction:
Create a Python 3 function to
get an organization via given org name.

Response:
def get_org(self, organization_name=''):
        """
        Retrieves an organization via given org name. If given
        empty string, prompts user for an org name.
        """
        self.organization_name = organization_name
        if(organization_name == ''):
            self.organization_name = raw_input('Organization: ')
        print 'Getting organization.'
        self.org_retrieved = self.logged_in_gh.organization(organization_name)


Instruction:
Can you write a function in Python 3 where it
gets the repos for the organization and builds the URL and headers for the organization and calculates the timestamps of stargazers.

Response:
def get_repos(self):
        """
        Gets the repos for the organization and builds the URL/headers for
        getting timestamps of stargazers.
        """
        print 'Getting repos.'
        #Uses the developer API. Note this could change.

        headers = {'Accept': 'application/vnd.github.v3.star+json', 'Authorization': 'token ' + self.token}
        temp_count = 0
        for repo in self.org_retrieved.iter_repos():
            temp_count += 1
            url = ('https://api.github.com/repos/' + self.organization_name + '/' + repo.name)
            self.repos[repo.name] = self.get_stargazers(url=url, headers=headers)
        self.calc_stargazers(start_count=650)
        print 'total count: \t' + str(self.total_count)
        print str(temp_count) + ' repos'


Instruction:
Here you have a function in Python 3, explain what it does
def get_stargazers(self, url, headers={}):
        url = url + '/stargazers?per_page=100&page=%s'
        page = 1
        gazers = []

        json_data = requests.get(url % page, headers=headers).json()
        while json_data:
            gazers.extend(json_data)
            page += 1
            json_data = requests.get(url % page, headers=headers).json()
        return gazers

Response: Return a list of the stargazers of a GitHub repo


Instruction:
How would you implement a function in Python 3 that
writes the data to a file.

Response:
def write_to_file(self, file_path='', date=(datetime.date.today()),
        organization='llnl'):
        """
        Writes stargazers data to file.
        """
        with open(file_path, 'w+') as out:
            out.write('date,organization,stargazers\n')
            sorted_stargazers = sorted(self.stargazers)#sort based on lowercase
            for star in sorted_stargazers:
                out.write(star + ',' + str(self.stargazers[star]) + '\n')
        out.close()


Instruction:
Can you generate the documentation for the following Python 3 function
def from_github3(klass, repository, labor_hours=True):
        if not isinstance(repository, github3.repos.repo._Repository):
            raise TypeError('Repository must be a github3 Repository object')

        logger.info('Processing: %s', repository.full_name)

        project = klass()

        logger.debug('GitHub3: repository=%s', repository)

        # -- REQUIRED FIELDS --

        project['name'] = repository.name
        project['repositoryURL'] = repository.git_url
        project['description'] = repository.description

        try:
            repo_license = repository.license()
        except github3.exceptions.NotFoundError:
            logger.debug('no license found for repo=%s', repository)
            repo_license = None

        if repo_license:
            license = repo_license.license
            if license:
                logger.debug('license spdx=%s; url=%s', license.spdx_id, license.url)
                if license.url is None:
                    project['permissions']['licenses'] = [{"name": license.spdx_id}]
                else:
                    project['permissions']['licenses'] = [{"URL": license.url, "name": license.spdx_id}]
            else:
                project['permissions']['licenses'] = None

        public_server = repository.html_url.startswith('https://github.com')
        if not repository.private and public_server:
            project['permissions']['usageType'] = 'openSource'
        elif date_parse(repository.created_at) < POLICY_START_DATE:
            project['permissions']['usageType'] = 'exemptByPolicyDate'

        if labor_hours:
            project['laborHours'] = labor_hours_from_url(project['repositoryURL'])
        else:
            project['laborHours'] = 0

        project['tags'] = ['github']
        old_accept = repository.session.headers['Accept']
        repository.session.headers['Accept'] = 'application/vnd.github.mercy-preview+json'
        topics = repository._get(repository.url + '/topics').json()
        project['tags'].extend(topics.get('names', []))
        repository.session.headers['Accept'] = old_accept

        # Hacky way to get an Organization object back with GitHub3.py >= 1.2.0
        owner_url = repository.owner.url
        owner_api_response = repository._get(owner_url)
        organization = repository._json(owner_api_response, 200)
        project['contact']['email'] = organization['email']
        project['contact']['URL'] = organization['html_url']

        # -- OPTIONAL FIELDS --

        # project['version'] = ''

        project['organization'] = organization['name']

        # TODO: Currently, can't be an empty string, see: https://github.com/GSA/code-gov-web/issues/370
        project['status'] = 'Development'

        project['vcs'] = 'git'

        project['homepageURL'] = repository.html_url

        project['downloadURL'] = repository.downloads_url

        project['languages'] = [l for l, _ in repository.languages()]

        # project['partners'] = []

        # project['relatedCode'] = []

        # project['reusedCode'] = []

        # date: [object] A date object describing the release.
        #   created: [string] The date the release was originally created, in YYYY-MM-DD or ISO 8601 format.
        #   lastModified: [string] The date the release was modified, in YYYY-MM-DD or ISO 8601 format.
        #   metadataLastUpdated: [string] The date the metadata of the release was last updated, in YYYY-MM-DD or ISO 8601 format.
        try:
            created_at = repository.created_at.date()
        except AttributeError:
            created_at = date_parse(repository.created_at).date()
        try:
            updated_at = repository.updated_at.date()
        except AttributeError:
            updated_at = date_parse(repository.updated_at).date()

        project['date'] = {
            'created': created_at.isoformat(),
            'lastModified': updated_at.isoformat(),
            'metadataLastUpdated': '',
        }

        _prune_dict_null_str(project)

        return project

Response: Create CodeGovProject object from github3 Repository object.


Instruction:
Write a Python 3 function that can
create CodeGovProject object from GitLab Repository object.

Response:
def from_gitlab(klass, repository, labor_hours=True):
        """
        Create CodeGovProject object from GitLab Repository
        """
        if not isinstance(repository, gitlab.v4.objects.Project):
            raise TypeError('Repository must be a gitlab Repository object')

        project = klass()

        logger.debug(
            'GitLab: repository_id=%d path_with_namespace=%s',
            repository.id,
            repository.path_with_namespace,
        )

        # -- REQUIRED FIELDS --

        project['name'] = repository.name
        project['repositoryURL'] = repository.http_url_to_repo
        project['description'] = repository.description

        # TODO: Update licenses from GitLab API
        project['permissions']['licenses'] = None

        web_url = repository.web_url
        public_server = web_url.startswith('https://gitlab.com')

        if repository.visibility in ('public') and public_server:
            project['permissions']['usageType'] = 'openSource'
        elif date_parse(repository.created_at) < POLICY_START_DATE:
            project['permissions']['usageType'] = 'exemptByPolicyDate'

        if labor_hours:
            project['laborHours'] = labor_hours_from_url(project['repositoryURL'])
        else:
            project['laborHours'] = 0

        project['tags'] = ['gitlab'] + repository.tag_list

        project['contact'] = {
            'email': '',
            'URL': web_url,
        }

        # -- OPTIONAL FIELDS --

        # project['version'] = ''

        project['organization'] = repository.namespace['name']

        # TODO: Currently, can't be an empty string, see: https://github.com/GSA/code-gov-web/issues/370
        project['status'] = 'Development'

        project['vcs'] = 'git'

        project['homepageURL'] = repository.web_url

        api_url = repository.manager.gitlab._url
        archive_suffix = '/projects/%s/repository/archive' % repository.get_id()
        project['downloadURL'] = api_url + archive_suffix

        # project['languages'] = [l for l, _ in repository.languages()]
        # project['partners'] = []
        # project['relatedCode'] = []
        # project['reusedCode'] = []

        project['date'] = {
            'created': date_parse(repository.created_at).date().isoformat(),
            'lastModified': date_parse(repository.last_activity_at).date().isoformat(),
            'metadataLastUpdated': '',
        }

        _prune_dict_null_str(project)

        return project


Instruction:
Write a Python 3 script to
create a new instance of the given class from a stashy repository.

Response:
def from_stashy(klass, repository, labor_hours=True):
        """
        Handles crafting Code.gov Project for Bitbucket Server repositories
        """
        # if not isinstance(repository, stashy.repos.Repository):
        #     raise TypeError('Repository must be a stashy Repository object')
        if not isinstance(repository, dict):
            raise TypeError('Repository must be a dict')

        project = klass()

        logger.debug(
            'Stashy: project_key=%s repository_slug=%s',
            repository['name'],
            repository['project']['key'],
        )

        # -- REQUIRED FIELDS --

        project['name'] = repository['name']

        clone_urls = [clone['href'] for clone in repository['links']['clone']]
        for url in clone_urls:
            # Only rely on SSH Urls for repository urls
            if url.startswith('ssh://'):
                project['repositoryURL'] = url
                break

        description = repository['project'].get('description', '')
        if description:
            project['description'] = 'Project description: %s' % description

        project['permissions']['licenses'] = None

        web_url = repository['links']['self'][0]['href']
        public_server = web_url.startswith('https://bitbucket.org')
        if repository['public'] and public_server:
            project['permissions']['usageType'] = 'openSource'

        if labor_hours:
            project['laborHours'] = labor_hours_from_url(project['repositoryURL'])
        else:
            project['laborHours'] = 0

        project['tags'] = ['bitbucket']

        project['contact']['email'] = ''
        project['contact']['URL'] = repository['links']['self'][0]['href']

        # -- OPTIONAL FIELDS --

        # project['version'] = ''

        # project['organization'] = organization.name

        # TODO: Currently, can't be an empty string, see: https://github.com/GSA/code-gov-web/issues/370
        project['status'] = 'Development'

        project['vcs'] = repository['scmId']

        project['homepageURL'] = repository['links']['self'][0]['href']

        # project['downloadURL'] =

        # project['languages'] =

        # project['partners'] = []

        # project['relatedCode'] = []

        # project['reusedCode'] = []

        # date: [object] A date object describing the release.
        #   created: [string] The date the release was originally created, in YYYY-MM-DD or ISO 8601 format.
        #   lastModified: [string] The date the release was modified, in YYYY-MM-DD or ISO 8601 format.
        #   metadataLastUpdated: [string] The date the metadata of the release was last updated, in YYYY-MM-DD or ISO 8601 format.
        # project['date'] = {
        #     'created': repository.pushed_at.isoformat(),
        #     'lastModified': repository.updated_at.isoformat(),
        #     'metadataLastUpdated': '',
        # }

        _prune_dict_null_str(project)

        return project


Instruction:
Can you create a Python 3 function that
creates CodeGovProject object from DOE CODE record.

Response:
def from_doecode(klass, record):
        """
        Create CodeGovProject object from DOE CODE record

        Handles crafting Code.gov Project
        """
        if not isinstance(record, dict):
            raise TypeError('`record` must be a dict')

        project = klass()

        # -- REQUIRED FIELDS --

        project['name'] = record['software_title']
        logger.debug('DOE CODE: software_title="%s"', record['software_title'])

        link = record.get('repository_link', '')
        if not link:
            link = record.get('landing_page')
            logger.warning('DOE CODE: No repositoryURL, using landing_page: %s', link)

        project['repositoryURL'] = link

        project['description'] = record['description']

        licenses = set(record['licenses'])
        licenses.discard(None)
        logger.debug('DOE CODE: licenses=%s', licenses)

        license_objects = []
        if 'Other' in licenses:
            licenses.remove('Other')
            license_objects = [{
                'name': 'Other',
                'URL': record['proprietary_url']
            }]

        if licenses:
            license_objects.extend([_license_obj(license) for license in licenses])

        project['permissions']['licenses'] = license_objects

        if record['open_source']:
            usage_type = 'openSource'
        else:
            usage_type = 'exemptByLaw'
            project['permissions']['exemptionText'] = 'This source code is restricted by patent and / or intellectual property law.'

        project['permissions']['usageType'] = usage_type

        # TODO: Compute from git repo
        project['laborHours'] = 0

        project['tags'] = ['DOE CODE']
        lab_name = record.get('lab_display_name')
        if lab_name is not None:
            project['tags'].append(lab_name)

        project['contact']['email'] = record['owner']
        # project['contact']['URL'] = ''
        # project['contact']['name'] = ''
        # project['contact']['phone'] = ''

        # -- OPTIONAL FIELDS --

        if 'version_number' in record and record['version_number']:
            project['version'] = record['version_number']

        if lab_name is not None:
            project['organization'] = lab_name

        # Currently, can't be an empty string, see: https://github.com/GSA/code-gov-web/issues/370
        status = record.get('ever_announced')
        if status is None:
            raise ValueError('DOE CODE: Unable to determine "ever_announced" value!')
        elif status:
            status = 'Production'
        else:
            status = 'Development'

        project['status'] = status

        vcs = None
        link = project['repositoryURL']
        if 'github.com' in link:
            vcs = 'git'
        if vcs is None:
            logger.debug('DOE CODE: Unable to determine vcs for: name="%s", repositoryURL=%s', project['name'], link)
            vcs = ''
        if vcs:
            project['vcs'] = vcs

        url = record.get('landing_page', '')
        if url:
            project['homepageURL'] = url

        # record['downloadURL'] = ''

        # self['disclaimerText'] = ''

        # self['disclaimerURL'] = ''

        if 'programming_languages' in record:
            project['languages'] = record['programming_languages']

        # self['partners'] = []
        # TODO: Look into using record['contributing_organizations']

        # self['relatedCode'] = []

        # self['reusedCode'] = []

        # date: [object] A date object describing the release.
        #   created: [string] The date the release was originally created, in YYYY-MM-DD or ISO 8601 format.
        #   lastModified: [string] The date the release was modified, in YYYY-MM-DD or ISO 8601 format.
        #   metadataLastUpdated: [string] The date the metadata of the release was last updated, in YYYY-MM-DD or ISO 8601 format.
        if 'date_record_added' in record and 'date_record_updated' in record:
            project['date'] = {
                'created': record['date_record_added'],
                # 'lastModified': '',
                'metadataLastUpdated': record['date_record_updated']
            }

        return project


Instruction:
How would you explain what the following Python 3 function does
def from_tfs(klass, tfs_project, labor_hours=True):
        project = klass()
        project_web_url = ''

        # -- REQUIRED FIELDS --
        project['name'] = tfs_project.projectInfo.name

        if 'web' in tfs_project.projectInfo._links.additional_properties:
            if 'href' in tfs_project.projectInfo._links.additional_properties['web']:
                # URL Encodes spaces that are in the Project Name for the Project Web URL
                project_web_url = requote_uri(tfs_project.projectInfo._links.additional_properties['web']['href'])

        project['repositoryURL'] = project_web_url

        project['homepageURL'] = project_web_url

        project['description'] = tfs_project.projectInfo.description

        project['vcs'] = 'TFS/AzureDevOps'

        project['permissions']['license'] = None

        project['tags'] = []

        if labor_hours:
            logger.debug('Sorry labor hour calculation not currently supported.')
            # project['laborHours'] = labor_hours_from_url(project['repositoryURL'])
        else:
            project['laborHours'] = 0

        if tfs_project.projectCreateInfo.last_update_time < POLICY_START_DATE:
            project['permissions']['usageType'] = 'exemptByPolicyDate'
        else:
            project['permissions']['usageType'] = 'exemptByAgencyMission'
            project['permissions']['exemptionText'] = 'This source code resides on a private server and has not been properly evaluated for releaseability.'

        project['contact'] = {
            'email': '',
            'URL': project_web_url
        }

        project['date'] = {
            'lastModified': tfs_project.projectLastUpdateInfo.last_update_time.date().isoformat(),
            'created': tfs_project.projectCreateInfo.last_update_time.date().isoformat(),
            'metadataLastUpdated': ''
        }

        _prune_dict_null_str(project)

        return project

Response: Creates CodeGovProject object from a TFS instance.