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

import psycopg2
import config
import dbutils
import codecs
import sys
from consolemsg import step, error, fail, warn
from namespace import namespace as ns

def esPersonaFisica(soci) :
    return 0 if soci.nif[2] in "ABCDEFGHJNPQRSUVW" else 1

def ambPuntDeMilers(numero) :
    return '{:,}'.format(numero).replace(',','.')


db = psycopg2.connect(**config.psycopg)
with db.cursor() as cursor :
    cursor.execute("""\
        SELECT DISTINCT gi.id, rpa.email, rp.lang
        FROM res_partner AS rp, res_partner_address AS rpa, somenergia_soci AS ss, generationkwh_investment as gi
        WHERE rp.id = rpa.partner_id AND
            rp.id = ss.partner_id AND
            ss.id = gi.member_id AND
            gi.purchase_date <= '2015-09-15' AND
            gi.active = True
        ;
    """)


    print u'\t'.join(unicode(x) for x in [
        'id',
        'email',
        'lang',
        ])


    for line in dbutils.fetchNs(cursor) :
        try:


            print '\t'.join(
                    str(x)
                        .replace('\t',' ')
                        .replace('\n',' ')
                        .replace('\r',' ')
                    for x in [
                line.id,
                line.email,
                line.lang,
            ])
        except Exception as e:
            import traceback
            error("Error processant soci {}\n{}\n{}".format(
                line.nsoci,
                e,
                "\n".join(traceback.format_stack()),
                )) 
            error(ns(cas=line).dump())







<commit_msg>Script mchimp_amorendarrerides: Add purchase_date column and discart repeated emails<commit_after>#!/usr/bin/env python
#-*- coding: utf8 -*-

import psycopg2
import config
import dbutils
import codecs
import sys
from consolemsg import step, error, fail, warn
from namespace import namespace as ns

def esPersonaFisica(soci) :
    return 0 if soci.nif[2] in "ABCDEFGHJNPQRSUVW" else 1

def ambPuntDeMilers(numero) :
    return '{:,}'.format(numero).replace(',','.')


db = psycopg2.connect(**config.psycopg)
with db.cursor() as cursor :
    cursor.execute("""\
        SELECT DISTINCT MIN(gi.id) as id, rpa.email, MIN(rp.lang) as lang, MIN(gi.purchase_date) as data_compra
        FROM res_partner AS rp, res_partner_address AS rpa, somenergia_soci AS ss, generationkwh_investment as gi
        WHERE rp.id = rpa.partner_id AND
            rp.id = ss.partner_id AND
            ss.id = gi.member_id AND
            gi.purchase_date <= '2015-09-30' AND
            gi.active = True
        GROUP BY rpa.email
        ORDER BY data_compra
        ;
    """)


    print u'\t'.join(unicode(x) for x in [
        'id',
        'email',
        'lang',
        'purchase_date',
        ])


    for line in dbutils.fetchNs(cursor) :
        try:


            print '\t'.join(
                    str(x)
                        .replace('\t',' ')
                        .replace('\n',' ')
                        .replace('\r',' ')
                    for x in [
                line.id,
                line.email,
                line.lang,
                line.data_compra
            ])
        except Exception as e:
            import traceback
            error("Error processant soci {}\n{}\n{}".format(
                line.id,
                e,
                "\n".join(traceback.format_stack()),
                )) 
            error(ns(cas=line).dump())

{'fraction_non_alphanumeric': 0.11916356344886853, 'fraction_numerical': 0.008020624462904613, 'mean_word_length': 1.575221238938053, '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': '2102892', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1065, 'n_words': 263}