<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import collections
import shelve
import random
import restrictions
import sys

import pandas

import pig_schema

SHELVE_FILE = 'restrictions_shelve'

OUT_TUPLE = collections.namedtuple('Tuple', 'site company tstamp')

@pig_schema.pig_output(
    '(site:chararray, company: chararray, tstamp: chararray)'
    )
@pig_schema.pig_input(
    '(site:chararray, ranking: bag{(tstamp:chararray, rank:int)})'
    )
def main(field):
    series = pandas.Series(dict((pandas.Timestamp(d), r)
                                for d, r in field.ranking))
    for i, (company, restriction) in enumerate(RESTRICTIONS):
        if restriction.fulfills(series):
            RESTRICTIONS.pop(i)
            out = OUT_TUPLE(field.site, 
                            company, 
                            restriction.tstamp.date().isoformat())
            yield out
            break

if __name__ == '__main__':
    SHELF = shelve.open(SHELVE_FILE)
    RESTRICTIONS = SHELF.items()
    random.shuffle(RESTRICTIONS)
    SHELF.close()
    for i in main(sys.stdin):
        pass<commit_msg>improve performance<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import collections
import shelve
import random
import restrictions
import sys

import pandas

import pig_schema

SHELVE_FILE = 'restrictions_shelve'

OUT_TUPLE = collections.namedtuple('Tuple', 'site company tstamp')

@pig_schema.pig_output(
    '(site:chararray, company: chararray, tstamp: chararray)'
    )
@pig_schema.pig_input(
    '(site:chararray, ranking: bag{(tstamp:chararray, rank:int)})'
    )
def main(field):
    if len(RESTRICTIONS) > 0:
        series = pandas.Series(dict((pandas.Timestamp(d), r)
                                    for d, r in field.ranking))
        for i, (company, restriction) in enumerate(RESTRICTIONS):
            if restriction.fulfills(series):
                RESTRICTIONS.pop(i)
                out = OUT_TUPLE(field.site, 
                                company, 
                                restriction.tstamp.date().isoformat())
                yield out
                break

if __name__ == '__main__':
    SHELF = shelve.open(SHELVE_FILE)
    RESTRICTIONS = SHELF.items()
    random.shuffle(RESTRICTIONS)
    SHELF.close()
    for i in main(sys.stdin):
        pass
{'fraction_non_alphanumeric': 0.11082474226804123, 'fraction_numerical': 0.001288659793814433, 'mean_word_length': 2.404970760233918, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5693808', 'n_tokens_mistral': 787, 'n_tokens_neox': 725, 'n_words': 155}