In [3]:
import pandas as pd
import timeit
from django.utils.text import slugify
from mixer.backend.django import mixer, Mixer
from random import choices
from re import sub

In [2]:
tic = timeit.default_timer()

In [3]:
mixer.blend(Occupation)


Out[3]:
<Occupation: aTuWqxvjKIMllMuRBsCJ>

In [4]:
mixer.cycle(3).blend(Occupation)


Out[4]:
[<Occupation: YWGiUeirZffpHhwKESQd>,
 <Occupation: CqZwVjODZmRDgfRvLksR>,
 <Occupation: oeftTGDzZCtVDjkQHcKN>]

In [5]:
# Usando faker
from faker import Faker, Factory

In [6]:
mixer = Mixer(locale='pt_br')

In [7]:
# Criando alguns cargos a partir de faker
occupations = [mixer.faker.job() for _ in range(10)]
occupations


Out[7]:
['Engenheiro florestal',
 'Salgadeira',
 'Cabeleireiro',
 'Biotecnólogo',
 'Aviador',
 'Engenheiro de produção agroindustrial',
 'Taxista',
 'Webmaster',
 'Peão de rodeiro',
 'Office-boy']

In [8]:
Occupation.objects.all().delete()


Out[8]:
(31,
 {'crm.PhonePerson': 0,
  'crm.CompanyContact': 0,
  'proposal.Contract': 0,
  'crm.Occupation': 22,
  'crm.Person': 9})

In [9]:
mixer.cycle(len(occupations)).blend(Occupation, occupation=(job for job in occupations))


Out[9]:
[<Occupation: Engenheiro florestal>,
 <Occupation: Salgadeira>,
 <Occupation: Cabeleireiro>,
 <Occupation: Biotecnólogo>,
 <Occupation: Aviador>,
 <Occupation: Engenheiro de produção agroindustrial>,
 <Occupation: Taxista>,
 <Occupation: Webmaster>,
 <Occupation: Peão de rodeiro>,
 <Occupation: Office-boy>]

In [10]:
mixer.faker.locale = 'en'

In [11]:
Customer.objects.all().delete()


Out[11]:
(11,
 {'crm.PhonePerson': 0,
  'crm.CompanyContact': 0,
  'proposal.Contract': 0,
  'crm.Customer': 11})

In [12]:
mixer.cycle(10).blend(Customer, person_type='c')


Out[12]:
[<Customer: Sandra>,
 <Customer: Barry>,
 <Customer: Richard>,
 <Customer: Jason>,
 <Customer: Victoria>,
 <Customer: Katie>,
 <Customer: David>,
 <Customer: Kimberly>,
 <Customer: Daniel>,
 <Customer: Shane>]

In [13]:
# from faker.providers import address

In [14]:
mixer.faker.name()


Out[14]:
'Dean Lopez'

In [15]:
mixer.faker.postcode()


Out[15]:
'11626'

In [16]:
Person.objects.all().delete()


Out[16]:
(5,
 {'crm.PhonePerson': 0,
  'crm.CompanyContact': 0,
  'proposal.Contract': 0,
  'crm.Person': 5})

In [17]:
mixer.cycle(5).blend(Person)


Out[17]:
[<Person: Melissa>,
 <Person: Jacqueline>,
 <Person: Janet>,
 <Person: Amber>,
 <Person: Megan>]

In [ ]:


In [ ]:


In [18]:
Work.objects.all().delete()


Out[18]:
(0, {})

In [19]:
max_length = 10

In [20]:
# Criando algumas obras a partir de faker
prefix = ('Cond.', 'Ed.', 'Residência', 'Pousada')
works = [('{} {} {}'.format(choices(prefix)[0], mixer.faker.company(), mixer.faker.uuid4()), mixer.faker.postcode()) for _ in range(max_length)]
works


Out[20]:
[('Pousada Ray-Lucero cb5cc90d-c910-598e-5773-de8d6316414e', '01649'),
 ('Pousada Hickman and Sons 4fac8625-a284-29a3-8b7e-fbeff7fe9e1e', '42262'),
 ('Ed. Santiago, Austin and Avila cbff84c9-66b9-2c6c-e18f-d82d3add3fc2',
  '24175'),
 ('Residência Neal, Edwards and Woods 2dcfb147-e536-c878-dbea-2abcb20f0baa',
  '62302'),
 ('Cond. Schneider-Walsh e15d0cb3-cae6-5471-f399-ca36f9ba82fb', '38164'),
 ('Residência Morgan-Ellis 148025a0-1272-e02f-232e-516c4f7a8d0e', '49083'),
 ('Ed. Escobar, Castillo and Watson 701baaa9-ae53-81e9-d794-db6574a48fbf',
  '08564'),
 ('Ed. Peters-Bray 24fb27ac-ebe0-1ea3-8300-db0a9199c6f9', '12146'),
 ('Ed. Espinoza Group ecf4f99e-5a46-a3f0-0514-cbc4c5743b83', '69977'),
 ('Pousada Neal, Simmons and Williams 796ea0a5-a85f-a73c-5631-c0d9158f76a9',
  '70624')]

In [ ]:


In [21]:
# Criar Work com FK
mixer.cycle(len(works)).blend(Work, name_work=(work[0] for work in works), cep=(work[1] for work in works), customer=mixer.SELECT, person=mixer.SELECT)


Out[21]:
[<Work: Pousada Ray-Lucero cb5cc90d-c910-598e-5773-de8d6316414e>,
 <Work: Pousada Hickman and Sons 4fac8625-a284-29a3-8b7e-fbeff7fe9e1e>,
 <Work: Ed. Santiago, Austin and Avila cbff84c9-66b9-2c6c-e18f-d82d3add3fc2>,
 <Work: Residência Neal, Edwards and Woods 2dcfb147-e536-c878-dbea-2abcb20f0baa>,
 <Work: Cond. Schneider-Walsh e15d0cb3-cae6-5471-f399-ca36f9ba82fb>,
 <Work: Residência Morgan-Ellis 148025a0-1272-e02f-232e-516c4f7a8d0e>,
 <Work: Ed. Escobar, Castillo and Watson 701baaa9-ae53-81e9-d794-db6574a48fbf>,
 <Work: Ed. Peters-Bray 24fb27ac-ebe0-1ea3-8300-db0a9199c6f9>,
 <Work: Ed. Espinoza Group ecf4f99e-5a46-a3f0-0514-cbc4c5743b83>,
 <Work: Pousada Neal, Simmons and Williams 796ea0a5-a85f-a73c-5631-c0d9158f76a9>]

In [22]:
toc = timeit.default_timer()
print('time', toc - tic)


time 0.7579055009991862

In [23]:
# Com bulk_create

In [24]:
# Criando algumas obras a partir de faker
prefix = ('Cond.', 'Ed.', 'Residência', 'Pousada')
works2 = [('{} {} {}'.format(choices(prefix)[0], mixer.faker.company(), mixer.faker.uuid4()), mixer.faker.postcode()) for _ in range(max_length)]
works2


Out[24]:
[('Cond. Huynh and Sons 54838076-6d0b-edba-241a-8ec254788faa', '66447'),
 ('Cond. Lowe LLC 7f4f686f-48b3-5dd7-323f-6106ac6689b1', '32088'),
 ('Cond. Bell-Hall 4da7d981-a833-6d76-b6c6-0362d9b0388e', '70155'),
 ('Pousada Greer, Walker and Carroll 088d5c31-a939-12ca-52b3-5aefda8e1492',
  '72853'),
 ('Ed. Hernandez, Burns and Garcia 88c35ceb-0e28-1e49-4efd-163ca0adf8cc',
  '80027'),
 ('Ed. Larson-Reed 06ea2d06-7d61-2090-6408-125ee68b59a0', '58770'),
 ('Ed. Juarez-Berry 9a54515c-7f6b-d65b-441a-efac95cf080b', '43449'),
 ('Pousada Harrington Group 598444f7-b37a-57d0-f1bc-3b4387f0915c', '09972'),
 ('Ed. Smith-Jones 8ee0bf7a-c515-0a7c-6852-e0e17bcf735b', '07034'),
 ('Pousada Myers, Adams and Arellano 459595e9-a23a-55d9-c31d-60bf6c4bda14',
  '45330')]

In [25]:
tic2 = timeit.default_timer()

In [26]:
customers = Customer.objects.all()
customer = choices(customers)[0]
customer


Out[26]:
<Customer: Victoria>

In [27]:
persons = Person.objects.all()
person = choices(persons)[0]
person


Out[27]:
<Person: Janet>

In [28]:
aux = []
for work in works2:
    obj = Work(name_work=work[0], cep=work[1], customer=choices(customers)[0], person=choices(persons)[0])
    aux.append(obj)

In [29]:
Work.objects.bulk_create(aux)


Out[29]:
[<Work: Cond. Huynh and Sons 54838076-6d0b-edba-241a-8ec254788faa>,
 <Work: Cond. Lowe LLC 7f4f686f-48b3-5dd7-323f-6106ac6689b1>,
 <Work: Cond. Bell-Hall 4da7d981-a833-6d76-b6c6-0362d9b0388e>,
 <Work: Pousada Greer, Walker and Carroll 088d5c31-a939-12ca-52b3-5aefda8e1492>,
 <Work: Ed. Hernandez, Burns and Garcia 88c35ceb-0e28-1e49-4efd-163ca0adf8cc>,
 <Work: Ed. Larson-Reed 06ea2d06-7d61-2090-6408-125ee68b59a0>,
 <Work: Ed. Juarez-Berry 9a54515c-7f6b-d65b-441a-efac95cf080b>,
 <Work: Pousada Harrington Group 598444f7-b37a-57d0-f1bc-3b4387f0915c>,
 <Work: Ed. Smith-Jones 8ee0bf7a-c515-0a7c-6852-e0e17bcf735b>,
 <Work: Pousada Myers, Adams and Arellano 459595e9-a23a-55d9-c31d-60bf6c4bda14>]

In [30]:
toc2 = timeit.default_timer()

In [31]:
print('time', toc - tic)


time 0.7579055009991862

In [32]:
print('time', toc2 - tic2)


time 0.08457663998706266

In [33]:
# bulk_create foi mais rapido X vezes
(toc - tic) / (toc2 - tic2)


Out[33]:
8.961168250655499

In [34]:
# Importar dados de CSV

In [35]:
ls fix


clientes.csv  enderecos_.csv  obras_.csv

In [36]:
filename = 'fix/clientes.csv'

In [37]:
df = pd.read_csv(filename)

In [38]:
df = df.fillna('')
df


Out[38]:
first_name last_name email occupation customer_type company cpf cnpj address complement district city uf cep active
0 Huang-Curry hc@email.com c 17.305.842/0001-54 Vale Lucca Costa, 598 Vila Jardim Leblon Azevedo do Amparo SC 57052491 sim
1 Ana Oliveira ana_oliveira@email.com Atendente p Campos e Melo 782.695.140-20 Campo de Moreira, 32 13º andar Vila Boa Vista Santos AC 75310300 sim
2 Adams, Eaton and Webb aew@email.com c 39.108.472/0001-06 Jardim de Monteiro, 6 Engenho Nogueira Monteiro da Mata MT 22748262 sim
3 Benedito Melo benedito@email.com Digitador p Acme 927.435.601-34 Esplanada de Caldeira, 30 14º andar Distrito Industrial Do Jatoba da Cruz RS 64853205 não
4 Watkins Group wg@email.com c 70.389.421/0001-31 Lagoa Pedro Lucas Oliveira, 34 Santa Monica Carvalho das Flores MG 30801523 sim
5 Juan Silveira juan_silveira@email.com Datilógrafo p Cruz S.A. 832.751.964-64 Residencial Camila Teixeira, 27 Leonina Rodrigues do Amparo AP 27729-726 sim
6 Johnson and Sons johnson@email.com a 72.839.504/0001-00 Largo de Monteiro, 3 São Gonçalo Pinto Paulista RR 93239-646 sim
7 Soraya Rocha soraya@email.com a 406.589.123-05 Praça Maria Clara Novaes, 80 42º andar Vila Bandeirantes Correia PA 42358-336 não
8 Duran PLC duran@email.com a 78.609.524/0001-71 Lagoa Benjamin Monteiro, 40 Vila Nova da Luz de Silveira PI 99853965 sim
9 Emanuel das Neves emanuel@email.com Pizzaiolo p Lima Ltda. 051.873.269-02 Aeroporto Alexia Castro, 19 Santo André Silva das Flores RO 09931-280 sim
10 Arnold PLC arnold@email.com c 12.967.085/0001-60 Residencial de da Paz Marieta 3ª Seção das Neves CE 40759161 sim
11 Pedro Freitas pedro@email.com Motoboy p Silva Gomes Ltda. 314.592.806-60 Lagoa João Miguel Peixoto Sobreloja Vila Olhos D'água Porto da Mata SC 39211611 não
12 Bryant, Morse and Davis bmd@email.com c 32.674.018/0001-65 Setor Juan da Conceição, 400 Mirtes da Mota BA 79929-544 sim
13 Leonardo da Luz leonardo@email.com Motorista p Rodrigues 364.508.972-10 Colônia Ribeiro, 20 Sala 2320 Urca Silva das Pedras SE 57721-797 sim
14 Schultz-Gray sg@email.com a 48.165.723/0001-77 Vereda Ian Nunes Vila Olhos D'água da Conceição MA 55893-976 sim
15 Luiz Gustavo Cardoso luiz.gustavo.cardoso@email.com Programador p 378.629.054-74 Jardim Freitas, 29 Serra Barbosa do Amparo CE 00727-135 sim
16 Lucas-Gordon lucas@gordon.com c 52.364.789/0001-63 Lagoa Ana Luiza Caldeira, 84 Marçola Santos do Amparo MS 71448555 sim
17 Danilo Azevedo danilo@azevedo.com Telefonista p Cardoso Pereira S.A. 089.657.412-11 Rodovia Lara Araújo, 89 São Francisco Das Chagas Peixoto do Galho PI 57839-486 não
18 Emanuel Cavalcanti emanuel@cavalcanti.com Digitador p Cardoso Pereira S.A. 807.463.512-08 Lago Heloísa Lopes, 56 Vila Da Paz Castro da Praia GO 00546-221 sim
19 Stevens-Mcpherson sm@email.com c Colônia Araújo, 1 sim

In [39]:
df.info()


<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20 entries, 0 to 19
Data columns (total 15 columns):
first_name       20 non-null object
last_name        20 non-null object
email            20 non-null object
occupation       20 non-null object
customer_type    20 non-null object
company          20 non-null object
cpf              20 non-null object
cnpj             20 non-null object
address          20 non-null object
complement       20 non-null object
district         20 non-null object
city             20 non-null object
uf               20 non-null object
cep              20 non-null object
active           20 non-null object
dtypes: object(15)
memory usage: 2.4+ KB

In [40]:
def get_occupation(occupation):
    obj = Occupation.objects.filter(occupation=occupation)
    if obj.first():
        return obj.first()
    if occupation:
        new_obj = Occupation.objects.create(occupation=occupation)
        return new_obj

In [41]:
get_occupation('Digitador')


Out[41]:
<Occupation: Digitador>

In [42]:
def get_slug(text):
    return slugify(text)

In [43]:
get_slug('Regis Santos')


Out[43]:
'regis-santos'

In [44]:
for row in df.head().itertuples():
    print(row.first_name, row.last_name)
    print(get_slug('{} {}'.format(row.first_name, row.last_name)))
    print('\n')


Huang-Curry 
huang-curry


Ana Oliveira
ana-oliveira


Adams, Eaton and Webb 
adams-eaton-and-webb


Benedito Melo
benedito-melo


Watkins Group 
watkins-group



In [45]:
def remove_punctuation(x):
    ''' Remove caracteres especiais. '''
    return sub(r'[^0-9\s]', '', x)

In [46]:
remove_punctuation('909-9090,909,-,0-0.090.')


Out[46]:
'909909090900090'

In [47]:
df['occupation'] = df[['occupation']].apply(lambda x: get_occupation(*x), axis=1)
df


Out[47]:
first_name last_name email occupation customer_type company cpf cnpj address complement district city uf cep active
0 Huang-Curry hc@email.com None c 17.305.842/0001-54 Vale Lucca Costa, 598 Vila Jardim Leblon Azevedo do Amparo SC 57052491 sim
1 Ana Oliveira ana_oliveira@email.com Atendente p Campos e Melo 782.695.140-20 Campo de Moreira, 32 13º andar Vila Boa Vista Santos AC 75310300 sim
2 Adams, Eaton and Webb aew@email.com None c 39.108.472/0001-06 Jardim de Monteiro, 6 Engenho Nogueira Monteiro da Mata MT 22748262 sim
3 Benedito Melo benedito@email.com Digitador p Acme 927.435.601-34 Esplanada de Caldeira, 30 14º andar Distrito Industrial Do Jatoba da Cruz RS 64853205 não
4 Watkins Group wg@email.com None c 70.389.421/0001-31 Lagoa Pedro Lucas Oliveira, 34 Santa Monica Carvalho das Flores MG 30801523 sim
5 Juan Silveira juan_silveira@email.com Datilógrafo p Cruz S.A. 832.751.964-64 Residencial Camila Teixeira, 27 Leonina Rodrigues do Amparo AP 27729-726 sim
6 Johnson and Sons johnson@email.com None a 72.839.504/0001-00 Largo de Monteiro, 3 São Gonçalo Pinto Paulista RR 93239-646 sim
7 Soraya Rocha soraya@email.com None a 406.589.123-05 Praça Maria Clara Novaes, 80 42º andar Vila Bandeirantes Correia PA 42358-336 não
8 Duran PLC duran@email.com None a 78.609.524/0001-71 Lagoa Benjamin Monteiro, 40 Vila Nova da Luz de Silveira PI 99853965 sim
9 Emanuel das Neves emanuel@email.com Pizzaiolo p Lima Ltda. 051.873.269-02 Aeroporto Alexia Castro, 19 Santo André Silva das Flores RO 09931-280 sim
10 Arnold PLC arnold@email.com None c 12.967.085/0001-60 Residencial de da Paz Marieta 3ª Seção das Neves CE 40759161 sim
11 Pedro Freitas pedro@email.com Motoboy p Silva Gomes Ltda. 314.592.806-60 Lagoa João Miguel Peixoto Sobreloja Vila Olhos D'água Porto da Mata SC 39211611 não
12 Bryant, Morse and Davis bmd@email.com None c 32.674.018/0001-65 Setor Juan da Conceição, 400 Mirtes da Mota BA 79929-544 sim
13 Leonardo da Luz leonardo@email.com Motorista p Rodrigues 364.508.972-10 Colônia Ribeiro, 20 Sala 2320 Urca Silva das Pedras SE 57721-797 sim
14 Schultz-Gray sg@email.com None a 48.165.723/0001-77 Vereda Ian Nunes Vila Olhos D'água da Conceição MA 55893-976 sim
15 Luiz Gustavo Cardoso luiz.gustavo.cardoso@email.com Programador p 378.629.054-74 Jardim Freitas, 29 Serra Barbosa do Amparo CE 00727-135 sim
16 Lucas-Gordon lucas@gordon.com None c 52.364.789/0001-63 Lagoa Ana Luiza Caldeira, 84 Marçola Santos do Amparo MS 71448555 sim
17 Danilo Azevedo danilo@azevedo.com Telefonista p Cardoso Pereira S.A. 089.657.412-11 Rodovia Lara Araújo, 89 São Francisco Das Chagas Peixoto do Galho PI 57839-486 não
18 Emanuel Cavalcanti emanuel@cavalcanti.com Digitador p Cardoso Pereira S.A. 807.463.512-08 Lago Heloísa Lopes, 56 Vila Da Paz Castro da Praia GO 00546-221 sim
19 Stevens-Mcpherson sm@email.com None c Colônia Araújo, 1 sim

In [48]:
df['slug'] = df[['first_name','last_name']].apply(lambda x: get_slug('{} {}'.format(*x)), axis=1)
df.head()


Out[48]:
first_name last_name email occupation customer_type company cpf cnpj address complement district city uf cep active slug
0 Huang-Curry hc@email.com None c 17.305.842/0001-54 Vale Lucca Costa, 598 Vila Jardim Leblon Azevedo do Amparo SC 57052491 sim huang-curry
1 Ana Oliveira ana_oliveira@email.com Atendente p Campos e Melo 782.695.140-20 Campo de Moreira, 32 13º andar Vila Boa Vista Santos AC 75310300 sim ana-oliveira
2 Adams, Eaton and Webb aew@email.com None c 39.108.472/0001-06 Jardim de Monteiro, 6 Engenho Nogueira Monteiro da Mata MT 22748262 sim adams-eaton-and-webb
3 Benedito Melo benedito@email.com Digitador p Acme 927.435.601-34 Esplanada de Caldeira, 30 14º andar Distrito Industrial Do Jatoba da Cruz RS 64853205 não benedito-melo
4 Watkins Group wg@email.com None c 70.389.421/0001-31 Lagoa Pedro Lucas Oliveira, 34 Santa Monica Carvalho das Flores MG 30801523 sim watkins-group

In [49]:
def get_active(value):
    return True if value == 'sim' else False

In [50]:
get_active('sim')


Out[50]:
True

In [51]:
df['cpf'] = df[['cpf']].apply(lambda x: remove_punctuation(*x), axis=1)
df['cnpj'] = df[['cnpj']].apply(lambda x: remove_punctuation(*x), axis=1)
df['cep'] = df[['cep']].apply(lambda x: remove_punctuation(*x), axis=1)
df['active'] = df[['active']].apply(lambda x: get_active(*x), axis=1)
df


Out[51]:
first_name last_name email occupation customer_type company cpf cnpj address complement district city uf cep active slug
0 Huang-Curry hc@email.com None c 17305842000154 Vale Lucca Costa, 598 Vila Jardim Leblon Azevedo do Amparo SC 57052491 True huang-curry
1 Ana Oliveira ana_oliveira@email.com Atendente p Campos e Melo 78269514020 Campo de Moreira, 32 13º andar Vila Boa Vista Santos AC 75310300 True ana-oliveira
2 Adams, Eaton and Webb aew@email.com None c 39108472000106 Jardim de Monteiro, 6 Engenho Nogueira Monteiro da Mata MT 22748262 True adams-eaton-and-webb
3 Benedito Melo benedito@email.com Digitador p Acme 92743560134 Esplanada de Caldeira, 30 14º andar Distrito Industrial Do Jatoba da Cruz RS 64853205 False benedito-melo
4 Watkins Group wg@email.com None c 70389421000131 Lagoa Pedro Lucas Oliveira, 34 Santa Monica Carvalho das Flores MG 30801523 True watkins-group
5 Juan Silveira juan_silveira@email.com Datilógrafo p Cruz S.A. 83275196464 Residencial Camila Teixeira, 27 Leonina Rodrigues do Amparo AP 27729726 True juan-silveira
6 Johnson and Sons johnson@email.com None a 72839504000100 Largo de Monteiro, 3 São Gonçalo Pinto Paulista RR 93239646 True johnson-and-sons
7 Soraya Rocha soraya@email.com None a 40658912305 Praça Maria Clara Novaes, 80 42º andar Vila Bandeirantes Correia PA 42358336 False soraya-rocha
8 Duran PLC duran@email.com None a 78609524000171 Lagoa Benjamin Monteiro, 40 Vila Nova da Luz de Silveira PI 99853965 True duran-plc
9 Emanuel das Neves emanuel@email.com Pizzaiolo p Lima Ltda. 05187326902 Aeroporto Alexia Castro, 19 Santo André Silva das Flores RO 09931280 True emanuel-das-neves
10 Arnold PLC arnold@email.com None c 12967085000160 Residencial de da Paz Marieta 3ª Seção das Neves CE 40759161 True arnold-plc
11 Pedro Freitas pedro@email.com Motoboy p Silva Gomes Ltda. 31459280660 Lagoa João Miguel Peixoto Sobreloja Vila Olhos D'água Porto da Mata SC 39211611 False pedro-freitas
12 Bryant, Morse and Davis bmd@email.com None c 32674018000165 Setor Juan da Conceição, 400 Mirtes da Mota BA 79929544 True bryant-morse-and-davis
13 Leonardo da Luz leonardo@email.com Motorista p Rodrigues 36450897210 Colônia Ribeiro, 20 Sala 2320 Urca Silva das Pedras SE 57721797 True leonardo-da-luz
14 Schultz-Gray sg@email.com None a 48165723000177 Vereda Ian Nunes Vila Olhos D'água da Conceição MA 55893976 True schultz-gray
15 Luiz Gustavo Cardoso luiz.gustavo.cardoso@email.com Programador p 37862905474 Jardim Freitas, 29 Serra Barbosa do Amparo CE 00727135 True luiz-gustavo-cardoso
16 Lucas-Gordon lucas@gordon.com None c 52364789000163 Lagoa Ana Luiza Caldeira, 84 Marçola Santos do Amparo MS 71448555 True lucas-gordon
17 Danilo Azevedo danilo@azevedo.com Telefonista p Cardoso Pereira S.A. 08965741211 Rodovia Lara Araújo, 89 São Francisco Das Chagas Peixoto do Galho PI 57839486 False danilo-azevedo
18 Emanuel Cavalcanti emanuel@cavalcanti.com Digitador p Cardoso Pereira S.A. 80746351208 Lago Heloísa Lopes, 56 Vila Da Paz Castro da Praia GO 00546221 True emanuel-cavalcanti
19 Stevens-Mcpherson sm@email.com None c Colônia Araújo, 1 True stevens-mcpherson

In [52]:
my_json = df.T.apply(dict).tolist()
my_json


Out[52]:
[{'first_name': 'Huang-Curry',
  'last_name': '',
  'email': 'hc@email.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '17305842000154',
  'address': 'Vale Lucca Costa, 598',
  'complement': '',
  'district': 'Vila Jardim Leblon',
  'city': 'Azevedo do Amparo',
  'uf': 'SC',
  'cep': '57052491',
  'active': True,
  'slug': 'huang-curry'},
 {'first_name': 'Ana',
  'last_name': 'Oliveira',
  'email': 'ana_oliveira@email.com',
  'occupation': <Occupation: Atendente>,
  'customer_type': 'p',
  'company': 'Campos e Melo',
  'cpf': '78269514020',
  'cnpj': '',
  'address': 'Campo de Moreira, 32',
  'complement': '13º andar',
  'district': 'Vila Boa Vista',
  'city': 'Santos',
  'uf': 'AC',
  'cep': '75310300',
  'active': True,
  'slug': 'ana-oliveira'},
 {'first_name': 'Adams, Eaton and Webb',
  'last_name': '',
  'email': 'aew@email.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '39108472000106',
  'address': 'Jardim de Monteiro, 6',
  'complement': '',
  'district': 'Engenho Nogueira',
  'city': 'Monteiro da Mata',
  'uf': 'MT',
  'cep': '22748262',
  'active': True,
  'slug': 'adams-eaton-and-webb'},
 {'first_name': 'Benedito',
  'last_name': 'Melo',
  'email': 'benedito@email.com',
  'occupation': <Occupation: Digitador>,
  'customer_type': 'p',
  'company': 'Acme',
  'cpf': '92743560134',
  'cnpj': '',
  'address': 'Esplanada de Caldeira, 30',
  'complement': '14º andar',
  'district': 'Distrito Industrial Do Jatoba',
  'city': 'da Cruz',
  'uf': 'RS',
  'cep': '64853205',
  'active': False,
  'slug': 'benedito-melo'},
 {'first_name': 'Watkins Group',
  'last_name': '',
  'email': 'wg@email.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '70389421000131',
  'address': 'Lagoa Pedro Lucas Oliveira, 34',
  'complement': '',
  'district': 'Santa Monica',
  'city': 'Carvalho das Flores',
  'uf': 'MG',
  'cep': '30801523',
  'active': True,
  'slug': 'watkins-group'},
 {'first_name': 'Juan',
  'last_name': 'Silveira',
  'email': 'juan_silveira@email.com',
  'occupation': <Occupation: Datilógrafo>,
  'customer_type': 'p',
  'company': 'Cruz S.A.',
  'cpf': '83275196464',
  'cnpj': '',
  'address': 'Residencial Camila Teixeira, 27',
  'complement': '',
  'district': 'Leonina',
  'city': 'Rodrigues do Amparo',
  'uf': 'AP',
  'cep': '27729726',
  'active': True,
  'slug': 'juan-silveira'},
 {'first_name': 'Johnson and Sons',
  'last_name': '',
  'email': 'johnson@email.com',
  'occupation': None,
  'customer_type': 'a',
  'company': '',
  'cpf': '',
  'cnpj': '72839504000100',
  'address': 'Largo de Monteiro, 3',
  'complement': '',
  'district': 'São Gonçalo',
  'city': 'Pinto Paulista',
  'uf': 'RR',
  'cep': '93239646',
  'active': True,
  'slug': 'johnson-and-sons'},
 {'first_name': 'Soraya',
  'last_name': 'Rocha',
  'email': 'soraya@email.com',
  'occupation': None,
  'customer_type': 'a',
  'company': '',
  'cpf': '40658912305',
  'cnpj': '',
  'address': 'Praça Maria Clara Novaes, 80',
  'complement': '42º andar',
  'district': 'Vila Bandeirantes',
  'city': 'Correia',
  'uf': 'PA',
  'cep': '42358336',
  'active': False,
  'slug': 'soraya-rocha'},
 {'first_name': 'Duran PLC',
  'last_name': '',
  'email': 'duran@email.com',
  'occupation': None,
  'customer_type': 'a',
  'company': '',
  'cpf': '',
  'cnpj': '78609524000171',
  'address': 'Lagoa Benjamin Monteiro, 40',
  'complement': '',
  'district': 'Vila Nova',
  'city': 'da Luz de Silveira',
  'uf': 'PI',
  'cep': '99853965',
  'active': True,
  'slug': 'duran-plc'},
 {'first_name': 'Emanuel',
  'last_name': 'das Neves',
  'email': 'emanuel@email.com',
  'occupation': <Occupation: Pizzaiolo>,
  'customer_type': 'p',
  'company': 'Lima Ltda.',
  'cpf': '05187326902',
  'cnpj': '',
  'address': 'Aeroporto Alexia Castro, 19',
  'complement': '',
  'district': 'Santo André',
  'city': 'Silva das Flores',
  'uf': 'RO',
  'cep': '09931280',
  'active': True,
  'slug': 'emanuel-das-neves'},
 {'first_name': 'Arnold PLC',
  'last_name': '',
  'email': 'arnold@email.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '12967085000160',
  'address': 'Residencial de da Paz',
  'complement': '',
  'district': 'Marieta 3ª Seção',
  'city': 'das Neves',
  'uf': 'CE',
  'cep': '40759161',
  'active': True,
  'slug': 'arnold-plc'},
 {'first_name': 'Pedro',
  'last_name': 'Freitas',
  'email': 'pedro@email.com',
  'occupation': <Occupation: Motoboy>,
  'customer_type': 'p',
  'company': 'Silva Gomes Ltda.',
  'cpf': '31459280660',
  'cnpj': '',
  'address': 'Lagoa João Miguel Peixoto',
  'complement': 'Sobreloja',
  'district': "Vila Olhos D'água",
  'city': 'Porto da Mata',
  'uf': 'SC',
  'cep': '39211611',
  'active': False,
  'slug': 'pedro-freitas'},
 {'first_name': 'Bryant, Morse and Davis',
  'last_name': '',
  'email': 'bmd@email.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '32674018000165',
  'address': 'Setor Juan da Conceição, 400',
  'complement': '',
  'district': 'Mirtes',
  'city': 'da Mota',
  'uf': 'BA',
  'cep': '79929544',
  'active': True,
  'slug': 'bryant-morse-and-davis'},
 {'first_name': 'Leonardo',
  'last_name': 'da Luz',
  'email': 'leonardo@email.com',
  'occupation': <Occupation: Motorista>,
  'customer_type': 'p',
  'company': 'Rodrigues',
  'cpf': '36450897210',
  'cnpj': '',
  'address': 'Colônia Ribeiro, 20',
  'complement': 'Sala 2320',
  'district': 'Urca',
  'city': 'Silva das Pedras',
  'uf': 'SE',
  'cep': '57721797',
  'active': True,
  'slug': 'leonardo-da-luz'},
 {'first_name': 'Schultz-Gray',
  'last_name': '',
  'email': 'sg@email.com',
  'occupation': None,
  'customer_type': 'a',
  'company': '',
  'cpf': '',
  'cnpj': '48165723000177',
  'address': 'Vereda Ian Nunes',
  'complement': '',
  'district': "Vila Olhos D'água",
  'city': 'da Conceição',
  'uf': 'MA',
  'cep': '55893976',
  'active': True,
  'slug': 'schultz-gray'},
 {'first_name': 'Luiz',
  'last_name': 'Gustavo Cardoso',
  'email': 'luiz.gustavo.cardoso@email.com',
  'occupation': <Occupation: Programador>,
  'customer_type': 'p',
  'company': '',
  'cpf': '37862905474',
  'cnpj': '',
  'address': 'Jardim Freitas, 29',
  'complement': '',
  'district': 'Serra',
  'city': 'Barbosa do Amparo',
  'uf': 'CE',
  'cep': '00727135',
  'active': True,
  'slug': 'luiz-gustavo-cardoso'},
 {'first_name': 'Lucas-Gordon',
  'last_name': '',
  'email': 'lucas@gordon.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '52364789000163',
  'address': 'Lagoa Ana Luiza Caldeira, 84',
  'complement': '',
  'district': 'Marçola',
  'city': 'Santos do Amparo',
  'uf': 'MS',
  'cep': '71448555',
  'active': True,
  'slug': 'lucas-gordon'},
 {'first_name': 'Danilo',
  'last_name': 'Azevedo',
  'email': 'danilo@azevedo.com',
  'occupation': <Occupation: Telefonista>,
  'customer_type': 'p',
  'company': 'Cardoso Pereira S.A.',
  'cpf': '08965741211',
  'cnpj': '',
  'address': 'Rodovia Lara Araújo, 89',
  'complement': '',
  'district': 'São Francisco Das Chagas',
  'city': 'Peixoto do Galho',
  'uf': 'PI',
  'cep': '57839486',
  'active': False,
  'slug': 'danilo-azevedo'},
 {'first_name': 'Emanuel',
  'last_name': 'Cavalcanti',
  'email': 'emanuel@cavalcanti.com',
  'occupation': <Occupation: Digitador>,
  'customer_type': 'p',
  'company': 'Cardoso Pereira S.A.',
  'cpf': '80746351208',
  'cnpj': '',
  'address': 'Lago Heloísa Lopes, 56',
  'complement': '',
  'district': 'Vila Da Paz',
  'city': 'Castro da Praia',
  'uf': 'GO',
  'cep': '00546221',
  'active': True,
  'slug': 'emanuel-cavalcanti'},
 {'first_name': 'Stevens-Mcpherson',
  'last_name': '',
  'email': 'sm@email.com',
  'occupation': None,
  'customer_type': 'c',
  'company': '',
  'cpf': '',
  'cnpj': '',
  'address': 'Colônia Araújo, 1',
  'complement': '',
  'district': '',
  'city': '',
  'uf': '',
  'cep': '',
  'active': True,
  'slug': 'stevens-mcpherson'}]

In [53]:
Customer.objects.all().delete()


Out[53]:
(30,
 {'crm.PhonePerson': 0,
  'crm.CompanyContact': 0,
  'proposal.Contract': 0,
  'proposal.Work': 20,
  'crm.Customer': 10})

In [54]:
tic3 = timeit.default_timer()

In [55]:
aux = []
for item in my_json:
    if not item.get('occupation'):
        del item['occupation']
    if item.get('cpf') == '':
        del item['cpf']
    if item.get('cnpj') == '':
        del item['cnpj']
    item['person_type'] = 'c'
    obj = Customer(**item)
    aux.append(obj)

In [56]:
Customer.objects.bulk_create(aux)


Out[56]:
[<Customer: Huang-Curry>,
 <Customer: Ana Oliveira>,
 <Customer: Adams, Eaton and Webb>,
 <Customer: Benedito Melo>,
 <Customer: Watkins Group>,
 <Customer: Juan Silveira>,
 <Customer: Johnson and Sons>,
 <Customer: Soraya Rocha>,
 <Customer: Duran PLC>,
 <Customer: Emanuel das Neves>,
 <Customer: Arnold PLC>,
 <Customer: Pedro Freitas>,
 <Customer: Bryant, Morse and Davis>,
 <Customer: Leonardo da Luz>,
 <Customer: Schultz-Gray>,
 <Customer: Luiz Gustavo Cardoso>,
 <Customer: Lucas-Gordon>,
 <Customer: Danilo Azevedo>,
 <Customer: Emanuel Cavalcanti>,
 <Customer: Stevens-Mcpherson>]

In [57]:
toc3 = timeit.default_timer()

In [58]:
print('time', toc - tic)


time 0.7579055009991862

In [59]:
print('time', toc2 - tic2)


time 0.08457663998706266

In [60]:
print('time', toc3 - tic3)


time 0.07647419901331887

In [61]:
customers = Customer.objects.all()
customer = choices(customers)[0]
customer


Out[61]:
<Customer: Lucas-Gordon>

In [62]:
persons = Person.objects.all()
person = choices(persons)[0]
person


Out[62]:
<Person: Jacqueline>

In [63]:
aux = []
for work in works2:
    obj = Work(name_work=work[0], cep=work[1], customer=choices(customers)[0], person=choices(persons)[0])
    aux.append(obj)

In [64]:
Work.objects.bulk_create(aux)


Out[64]:
[<Work: Cond. Huynh and Sons 54838076-6d0b-edba-241a-8ec254788faa>,
 <Work: Cond. Lowe LLC 7f4f686f-48b3-5dd7-323f-6106ac6689b1>,
 <Work: Cond. Bell-Hall 4da7d981-a833-6d76-b6c6-0362d9b0388e>,
 <Work: Pousada Greer, Walker and Carroll 088d5c31-a939-12ca-52b3-5aefda8e1492>,
 <Work: Ed. Hernandez, Burns and Garcia 88c35ceb-0e28-1e49-4efd-163ca0adf8cc>,
 <Work: Ed. Larson-Reed 06ea2d06-7d61-2090-6408-125ee68b59a0>,
 <Work: Ed. Juarez-Berry 9a54515c-7f6b-d65b-441a-efac95cf080b>,
 <Work: Pousada Harrington Group 598444f7-b37a-57d0-f1bc-3b4387f0915c>,
 <Work: Ed. Smith-Jones 8ee0bf7a-c515-0a7c-6852-e0e17bcf735b>,
 <Work: Pousada Myers, Adams and Arellano 459595e9-a23a-55d9-c31d-60bf6c4bda14>]

In [7]:
num_prop = 12
mixer.cycle(num_prop).blend(Proposal, num_prop=(n for n in range(11, num_prop + 1)))


Traceback (most recent call last):
  File "/home/rg3915/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py", line 116, in <genexpr>
    for name, value in defaults.items()
  File "/home/rg3915/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/backend/django.py", line 218, in get_value
    return self._get_value(name, value, field)
  File "/home/rg3915/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/backend/django.py", line 225, in _get_value
    return self._get_value(name, next(value), field)
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/rg3915/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py", line 568, in blend
    return type_mixer.blend(**values)
  File "/home/rg3915/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py", line 116, in blend
    for name, value in defaults.items()
RuntimeError: Mixer (<class 'orcamentos.proposal.models.Proposal'>): generator raised StopIteration

---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
~/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py in <genexpr>(.0)
    115             else self.get_value(name, value)
--> 116             for name, value in defaults.items()
    117         )

~/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/backend/django.py in get_value(self, name, value)
    217 
--> 218             return self._get_value(name, value, field)
    219 

~/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/backend/django.py in _get_value(self, name, value, field)
    224         if isinstance(value, GeneratorType):
--> 225             return self._get_value(name, next(value), field)
    226 

StopIteration: 

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-7-87bf10085d8f> in <module>
      1 num_prop = 1200
----> 2 mixer.cycle(num_prop).blend(Proposal, num_prop=(n for n in range(11, num_prop + 1)))

~/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py in blend(self, scheme, **values)
    418         for _ in range(self.count):
    419             result.append(
--> 420                 self.mixer.blend(scheme, **values)
    421             )
    422         return result

~/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py in blend(self, scheme, **values)
    566         type_mixer = self.get_typemixer(scheme)
    567         try:
--> 568             return type_mixer.blend(**values)
    569         except Exception as e:
    570             if self.params.get('silence'):

~/gh/my/orcamentos/.venv/lib/python3.7/site-packages/mixer/main.py in blend(self, **values)
    114             if isinstance(value, t.ServiceValue)
    115             else self.get_value(name, value)
--> 116             for name, value in defaults.items()
    117         )
    118 

RuntimeError: Mixer (<class 'orcamentos.proposal.models.Proposal'>): generator raised StopIteration

In [ ]: