<commit_before>import re
import sys
import numpy as np
import scipy.sparse as sparse


class ProgressBar(object):
    def __init__(self, N, length=40):
        self.N      = N
        self.nf     = float(N)
        self.length = length

    def bar(self, i):
        """Assumes i ranges through [0, N-1]"""
        b = int(np.ceil(((i+1) / self.nf) * self.length))
        sys.stdout.write("\r[%s%s] %d%%" % ("="*b, " "*(self.length-b), int(100*((i+1) / self.nf))))
        sys.stdout.flush()

    def close(self):
        sys.stdout.write("\n\n")
        sys.stdout.flush()


def get_ORM_instance(ORM_class, session, instance):
    """
    Given an ORM class and *either an instance of this class, or the name attribute of an instance
    of this class*, return the instance
    """
    if isinstance(instance, str):
        return session.query(ORM_class).filter(ORM_class.name == instance).one()
    else:
        return instance


def camel_to_under(name):
    """
    Converts camel-case string to lowercase string separated by underscores.
<commit_msg>Speed up ProgressBar 500x by precalculating move points<commit_after>import re
import sys
import numpy as np
import scipy.sparse as sparse


class ProgressBar(object):
    def __init__(self, N, length=40):
        # Protect against division by zero (N = 0 results in full bar being printed)
        self.N      = max(1, N)
        self.nf     = float(self.N)
        self.length = length
        # Precalculate the i values that should trigger a write operation
        self.ticks = set([round(i/100.0 * N) for i in range(101)])
        self.ticks.add(N-1)
        self.bar(0)

    def bar(self, i):
        """Assumes i ranges through [0, N-1]"""
        if i in self.ticks:
            b = int(np.ceil(((i+1) / self.nf) * self.length))
            sys.stdout.write("\r[%s%s] %d%%" % ("="*b, " "*(self.length-b), int(100*((i+1) / self.nf))))
            sys.stdout.flush()

    def close(self):
        # Move the bar to 100% before closing
        self.bar(self.N-1)
        sys.stdout.write("\n\n")
        sys.stdout.flush()


def get_ORM_instance(ORM_class, session, instance):
    """
    Given an ORM class and *either an instance of this class, or the name attribute of an instance
    of this class*, return the instance
    """
    if isinstance(instance, str):
        return session.query(ORM_class).filter(ORM_class.name == instance).one()
    else:
        return instance


def camel_to_under(name):
    """
    Converts camel-case string to lowercase string separated by underscores.

{'fraction_non_alphanumeric': 0.13174354964816262, 'fraction_numerical': 0.014073494917904612, 'mean_word_length': 2.5394190871369293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832543', 'n_tokens_mistral': 843, 'n_tokens_neox': 801, 'n_words': 244}