<commit_before>#!/usr/bin/env python
# -*- coding: utf8 -*-
from django.conf import settings

import open_municipio.testdatabuilder.random_items_factory as random_factory
from open_municipio.people.models import *
from open_municipio.votations.models import *

from datetime import *
import random
import itertools

# cleanup
Sitting.objects.all().delete()
Votation.objects.all().delete()

council_institution = Institution.objects.get(institution_type=Institution.COUNCIL)
# create council sitting
s1 = Sitting(
    idnum="1",
    number=1,
    date="2012-01-12",
    institution=council_institution
)
s1.save()
print "Seduta 1 creata"


# TODO:
# .. yes and no percentages vary according to group and majority


# generates 10 votations related to sitting s1
# 
for i in range(1, 10):
    
    # add a votation
    v = Votation(
        sitting=s1,
        idnum="%s" % i,
        n_legal=20,
    )
    v.save();
    print "Votazione %s creata"  % i
    
    # define vote weights
    # itertools.chain.from_iterable flattens a nested list of lists
    # [i]*N creates a list of N integers i
    # choosing randomly from the flattened lists is almost equivalent
    # to choosing with weighted probabilities
    vw = list(itertools.chain.from_iterable([
        [0]*30,  # 30% NO
        [1]*60,  # 60% YES
        [2]*5,   #  5% ABSTAINED
        [4]*5    #  5% ABSENT
    ])) 
    
    for charge in council_institution.institutioncharge_set.all():
        if (charge.charge_type == InstitutionCharge.COUNCIL_PRES_CHARGE or
            charge.charge_type == InstitutionCharge.COUNCIL_VICE_CHARGE):
            # president and vicepresident do not vote twice
            continue
        else:
            # votings are drawn randomly with weighted probabilities
            vote = random.choice(vw)
        
        charge_vote = ChargeVote.objects.create(votation=v, charge=charge, vote=vote)
        charge_vote.save()
        # print "%s voted %s" % (charge, charge_vote.vote)
    print "Voti creati"
    
    
    # compute voting totals
    v.n_yes = ChargeVote.objects.filter(votation=v, vote=ChargeVote.YES).count()
    v.n_no = ChargeVote.objects.filter(votation=v, vote=ChargeVote.NO).count()
    v.n_abst = ChargeVote.objects.filter(votation=v, vote=ChargeVote.ABSTAINED).count()
    n_president = ChargeVote.objects.filter(votation=v, vote=ChargeVote.PRES).count()
    v.n_present = v.n_yes + v.n_no + v.n_abst + n_president
    v.n_maj = int(v.n_present / 2) + 1
    if v.n_yes >= v.n_maj:
        v.outcome = Votation.PASSED
    else:
        v.outcome = Votation.REJECTED
    v.save()
    
    print "Totali"
    print "  Presenti: %s, Maggioranza: %s" % (v.n_present, v.n_maj)
    print "  %s YES, %s NO, %s ABSTAINED" % (v.n_yes, v.n_no, v.n_abst)
    print "  Voto %s" % (v.get_outcome_display())<commit_msg>random votations generator script now generates a number of sittings in the past (it is now possible to test votations coming from more than one sittings)<commit_after># -*- coding: utf8 -*-
from open_municipio.people.models import *
from open_municipio.votations.models import *

import datetime
import random
import itertools

# cleanup
Sitting.objects.all().delete()
Votation.objects.all().delete()

council_institution = Institution.objects.get(institution_type=Institution.COUNCIL)
# create council sitting


# TODO:
# .. yes and no percentages vary according to group and majority


# generates N sittings and M votes
n_sittings = random.randint(3, 7)
n_votation = random.randint(5, 10)
date_sitting = first_sitting_date = datetime.date.today() - datetime.timedelta(days=random.randint(n_sittings, 10))

for s_cnt in range(1, n_sittings):

    date_sitting += datetime.timedelta(days=1)

    s = Sitting(
        idnum="C/000%s" % s_cnt,
        number=s_cnt,
        institution=council_institution,
        date=date_sitting
    )
    s.save()
    print "Seduta % screata" % s_cnt

    # generates N votations related to the current sitting
    for i in range(1, 10):

        # add a votation
        v = Votation(
            sitting=s,
            idnum="%s" % i,
            n_legal=20,
        )
        v.save()
        print "Votazione %s creata"  % i

        # define vote weights
        # itertools.chain.from_iterable flattens a nested list of lists
        # [i]*N creates a list of N integers i
        # choosing randomly from the flattened lists is almost equivalent
        # to choosing with weighted probabilities
        vw = list(itertools.chain.from_iterable([
            [0]*30,  # 30% NO
            [1]*60,  # 60% YES
            [2]*5,   #  5% ABSTAINED
            [4]*5    #  5% ABSENT
        ]))

        for charge in council_institution.institutioncharge_set.all():
            if (charge.charge_type == InstitutionCharge.COUNCIL_PRES_CHARGE or
                charge.charge_type == InstitutionCharge.COUNCIL_VICE_CHARGE):
                # president and vicepresident do not vote twice
                continue
            else:
                # votings are drawn randomly with weighted probabilities
                vote = random.choice(vw)

            charge_vote = ChargeVote.objects.create(votation=v, charge=charge, vote=vote)
            charge_vote.save()
            # print "%s voted %s" % (charge, charge_vote.vote)
        print "Voti creati"


        # compute voting totals
        v.n_yes = ChargeVote.objects.filter(votation=v, vote=ChargeVote.YES).count()
        v.n_no = ChargeVote.objects.filter(votation=v, vote=ChargeVote.NO).count()
        v.n_abst = ChargeVote.objects.filter(votation=v, vote=ChargeVote.ABSTAINED).count()
        n_president = ChargeVote.objects.filter(votation=v, vote=ChargeVote.PRES).count()
        v.n_present = v.n_yes + v.n_no + v.n_abst + n_president
        v.n_maj = int(v.n_present / 2) + 1
        if v.n_yes >= v.n_maj:
            v.outcome = Votation.PASSED
        else:
            v.outcome = Votation.REJECTED
        v.save()

        print "Totali"
        print "  Presenti: %s, Maggioranza: %s" % (v.n_present, v.n_maj)
        print "  %s YES, %s NO, %s ABSTAINED" % (v.n_yes, v.n_no, v.n_abst)
        print "  Voto %s" % (v.get_outcome_display())
{'fraction_non_alphanumeric': 0.10601163542340013, 'fraction_numerical': 0.012443438914027148, 'mean_word_length': 2.6129597197898424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2137829', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2075, 'n_words': 492}