<commit_before>#!/usr/bin/env python
import codecs
from fractions import Fraction
import logging
import os
import pprint
import sys
from decimal import *

from simfile import *
import yaml
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

import synctools

__all__ = ['gimmicks']




def gimmicks(simfile):
    log = logging.getLogger('synctools')
    cfg = synctools.get_config()
    synctools.backup(simfile)
    gpath = os.path.join(os.path.split(simfile.filename)[0], 'gimmicks.txt')
    
    # Parse gimmicks.txt.
    with open(gpath, 'r') as gfile:
        g = yaml.load(gfile, Loader=Loader)
    
    # Convert each gimmick line into a set of BPMs and stops.
    t = {
        'bpms': {0: float(g['bpm'])},
        'stops': {},
    }
    gimmicks = sorted(g['gimmicks'].iteritems(), key=(lambda t: t[0].split('-')[0]))
    for beats, gimmick in gimmicks:
        start, stop = (float(b) for b in beats.split('-'))
        full_length = stop - start
        length, mul, name = gimmick.split()
        length = float(Fraction(length)) * 4
        mul = float(Fraction(mul.rstrip('x')))
        
        if 'definitions' in g and name in g['definitions']:
            definition = g['definitions'][name]
        elif name in builtin_gimmicks:
            definition = builtin_gimmicks[name]
        
        for timing in ('bpms', 'stops'):
            if timing in definition:
                for loc, eq in definition[timing].iteritems():
                    loc = float(loc)
                    pos = start + length * loc
                    val = eval(eq, {
                        '__builtins__': None,
                        'mul': mul,
                        'len': length,
                        'bpm': g['bpm'],
                    })
                    while decimal_from_192nd(pos) < decimal_from_192nd(stop):
                        t[timing][pos] = val<commit_msg>Fixed gimmicks.py rounding bug<commit_after>#!/usr/bin/env python
import codecs
from fractions import Fraction
import logging
import os
import pprint
import sys
from decimal import *

from simfile import *
import yaml
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

import synctools

__all__ = ['gimmicks']




def gimmicks(simfile):
    log = logging.getLogger('synctools')
    cfg = synctools.get_config()
    synctools.backup(simfile)
    gpath = os.path.join(os.path.split(simfile.filename)[0], 'gimmicks.txt')
    
    # Parse gimmicks.txt.
    with open(gpath, 'r') as gfile:
        g = yaml.load(gfile, Loader=Loader)
    
    # Ensure the BPM is a float, not an integer
    g['bpm'] = float(g['bpm'])
    
    # Convert each gimmick line into a set of BPMs and stops.
    t = {
        'bpms': {0: g['bpm']},
        'stops': {},
    }
    gimmicks = sorted(g['gimmicks'].iteritems(), key=(lambda t: t[0].split('-')[0]))
    for beats, gimmick in gimmicks:
        start, stop = (float(b) for b in beats.split('-'))
        full_length = stop - start
        length, mul, name = gimmick.split()
        length = float(Fraction(length)) * 4
        mul = float(Fraction(mul.rstrip('x')))
        
        if 'definitions' in g and name in g['definitions']:
            definition = g['definitions'][name]
        elif name in builtin_gimmicks:
            definition = builtin_gimmicks[name]
        
        for timing in ('bpms', 'stops'):
            if timing in definition:
                for loc, eq in definition[timing].iteritems():
                    loc = float(loc)
                    pos = start + length * loc
                    val = eval(eq, {
                        '__builtins__': None,
                        'mul': mul,
                        'len': length,
                        'bpm': g['bpm'],
                    })
                    while decimal_from_192nd(pos) < decimal_from_192nd(stop):
                        t[timing][pos] = val
{'fraction_non_alphanumeric': 0.11782326510234647, 'fraction_numerical': 0.0054917623564653024, 'mean_word_length': 1.9484915378955114, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6735692', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1215, 'n_words': 335}