-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.py
727 lines (396 loc) · 17.3 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import os
import json
from time import sleep
from typing import List
def clean() -> None:
os.system('cls' if os.name == 'nt' else 'clear')
def decorator_text(text: str, decorator: str, clean_screen: bool = False, color: str = '32') -> None:
if clean_screen:
clean()
if color == 'default':
text = text.title()
print(f'\033[1;33m{decorator}\033[m' * (len(text)+2))
print(f' {text}')
print(f'\033[1;33m{decorator}\033[m' * (len(text)+2))
print()
return
text = text.title()
print(f'\033[1;33m{decorator}\033[m' * (len(text)+2))
print(f' \033[1;{color}m{text}\033[m')
print(f'\033[1;33m{decorator}\033[m' * (len(text)+2))
print()
return
def text_formatter(text: str) -> None:
count = 0
for letter in text:
print(letter, end='')
if letter == ' ':
count += 1
if count == 13:
print('\n', end='')
count = 0
def remove_rs(num: str) -> list:
num = num.lower()
num = num.replace(',', '.')
num = num.replace('r', '')
num = num.replace('s', '')
num = num.replace('$', '')
num = num.replace(' ', '')
try:
return [float(num), True]
except ValueError:
return [num, False]
def number_formatter(num) -> str:
count = 0
sum = 0
num2 = str(num).replace('.', ',')
new_num = ''
for number in num2[::-1]:
new_num += number
count += 1
if number == ',':
count = 0
sum = 3
if count % 3 == 0 and count != 0 and count + sum < len(str(num2)):
new_num += '.'
return f'{new_num[::-1]}'
def receive_company_sector() -> list:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
print('''Qual o setor da sua empresa?
[0] Sair
[1] Varejo
[2] Software
[3] Serviços
[4] Indústria
[5] Outro''')
quit = False
company_sector = False
while not company_sector:
company_sector = input('\n\033[1;36mEscolha do Usuário:\033[m ').strip().lower().replace('á', 'a').replace('ç', 'c').replace('ú', 'u')
if company_sector == '1' or company_sector == 'varejo':
company_sector = 'Varejo'
elif company_sector == '2' or company_sector == 'software':
company_sector = 'Software'
elif company_sector == '3' or company_sector == 'serviços':
company_sector = 'Serviços'
elif company_sector == '4' or company_sector == 'indústria':
company_sector = 'Indústria'
elif company_sector == '5' or company_sector == 'outro':
company_sector = 'Outro'
elif company_sector == '0' or company_sector == 'sair':
quit = True
company_sector = True
else:
company_sector = False
return [company_sector, quit]
def check_fuel_expenditure() -> list:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
valid = False
avg_fuel = 0
while not valid:
text_formatter('No último ano, qual foi a média mensal de gasto da sua empresa com combustível (usado em sua própria frota ou equipamentos, em R$)?: ')
avg_fuel, valid = remove_rs(input(''))
if not valid: print('\nDigite um valor válido\n')
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
print('''Qual o principal combustível que sua empresa utiliza?
[0] Sair
[1] Diesel
[2] Gasolina
[3] Etanol
[4] Gás Natural''')
quit = False
while True:
main_fuel = input('\n\033[1;36mEscolha do Usuário:\033[m ').strip().lower().replace('á', 'a')
if main_fuel == '1' or main_fuel == 'diesel':
main_fuel = 1
multiplier = 0.0042
break
elif main_fuel == '2' or main_fuel == 'gasolina':
main_fuel = 2
multiplier = 0.003504
break
elif main_fuel == '3' or main_fuel == 'etanol':
main_fuel = 3
multiplier = 0.000024
break
elif main_fuel == '4' or main_fuel == 'gas natural':
main_fuel = 4
multiplier = 0.006708
break
elif main_fuel == '0' or main_fuel == 'sair':
multiplier = 0
quit = True
break
else: main_fuel = 0
annual_issue1 = avg_fuel * multiplier
return [annual_issue1, quit]
def check_energy_expenditure(workers: int, sector: int) -> list:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
quit = False
while True:
avg_energy_bill = input('Qual o valor médio mensal da conta de energia\nelétrica de sua empresa (R$)?: ')
avg_energy_bill, valid = remove_rs(avg_energy_bill)
if not valid:
print('\nDigite um valor válido\n')
else: break
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
print('''Sua empresa faz uso de energia solar?
[0] Sair
[1] Não
[2] Sim, compro a minha energia de uma fazenda solar
[3] Sim, além de consumir da rede, faço geração própria
''')
while True:
solar_energy = input('\033[1;36mEscolha do Usuário:\033[m ').strip().lower().replace('ã', 'a')
if solar_energy == '1' or solar_energy =='nao':
if sector == 'Indústria' or sector == 'Outro':
multiplier = 0.002424
else:
multiplier = 0.002136
break
if solar_energy == '2':
if sector == 'Indústria' or sector == 'Outro': multiplier = 0.000936
else: multiplier = 0.000816
break
if solar_energy == '3':
if sector == 'Indústria' or sector == 'Outro': multiplier = 0.004728
else: multiplier = 0.004164
break
if solar_energy == '0' or solar_energy == 'sair':
quit = True
multiplier = 0
break
if quit: return [0, quit]
workers_emission, quit = calculate_distribution_workers(workers)
energy_emission = avg_energy_bill * multiplier
annual_emission2 = workers_emission + energy_emission
return [annual_emission2, quit]
def calculate_distribution_workers(workers: int) -> list:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
print('''Como as pessoas que trabalham na sua empresa estão distribuídas?
[0] Sair
[1] Majoritariamente presencial
[2] Híbrido
[3] Majoritariamente remoto
''')
quit = False
while True:
distribution_workers = input('\033[1;36mEscolha do Usuário:\033[m ').strip().lower().replace('í', 'i')
if distribution_workers == '1' or distribution_workers == 'majoritariamente presencial':
multiplier_workers = 0
break
if distribution_workers == '2' or distribution_workers == 'hibrido':
multiplier_workers = 0.1
break
if distribution_workers == '3' or distribution_workers == 'majoritariamente remoto':
multiplier_workers = 0.2
break
if distribution_workers == '0' or distribution_workers == 'sair':
quit = True
multiplier_workers = 0
break
return [workers*multiplier_workers, quit]
def check_number_deliveries() -> int:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
print('''Quantas vendas com frete sua empresa faz por mês?
[1] 0
[2] 1 - 200
[3] 200 - 500
[4] 500 - 1000
[5] 1000 - 2000
[6] Outro
Observação: considere aqui somente o frete entregue por uma empresa terceira (e.g. correios, loggi, etc.)''')
while True:
deliveries = input('\n\033[1;36mEscolha do Usuário:\033[m ').strip().lower()
if deliveries == '1':
deliveries = 0
break
if deliveries == '2' or deliveries == '1 - 200':
deliveries = 100
break
if deliveries == '3' or deliveries == '200 - 500':
deliveries = 350
break
if deliveries == '4' or deliveries == '500 - 1000':
deliveries = 750
break
if deliveries == '5' or deliveries == '1000 - 2000':
deliveries = 1500
break
if deliveries == '6' or deliveries == 'outro':
while True:
deliveries = input('\nInforme a quantidade de entregas por mês: ').strip().replace(',', '.')
try:
deliveries = int(deliveries)
break
except ValueError:
print('\nDigite um valor válido\n')
break
return deliveries
def receive_deliveries_weight() -> float:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
while True:
weight = input('Qual o peso médio do pacote que você envia no frete em quilos(kg)?: ').lower().strip()
if weight.replace('k', '').replace('g', '').isnumeric():
weight = weight.replace('k', '').replace('g', '')
break
return float(weight)
def location_consumers_is_1(locations_consumers: str, is_industry: bool) -> List[float]:
if locations_consumers == '1':
if is_industry:
return [0.00432, 0.000171]
return [0.0044055, 0]
return [0, 0]
def location_consumers_is_2(locations_consumers: str, is_industry: bool) -> List[float]:
if locations_consumers == '2':
if is_industry:
return [0.00432, 0.00042]
return [0.00453, 0]
return [0, 0]
def location_consumers_is_3(locations_consumers: str, is_industry: bool) -> List[float]:
if locations_consumers == '3':
if is_industry:
return [0.00432, 0.00156]
return [0.0051, 0]
return [0, 0]
def location_consumers_is_4(locations_consumers: str, is_industry: bool) -> List[float]:
if locations_consumers == '4':
if is_industry:
return [0.00432, 0.0036]
return [0.00612, 0]
return [0, 0]
def check_location_consumers(deliveries: int, company_sector: str) -> float:
is_industry = False
weight = 0
if company_sector == 'Indústria' or company_sector == 'Outro':
weight = receive_deliveries_weight()
is_industry = True
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
print('''Onde se localizam a maioria dos seus consumidores?
[1] Perto do meu município (até 50 km distante)
[2] Dentro do meu estado (50km a 300 km)
[3] Em um estado vizinho (300km a 1000 km)
[4] Em outra região do país (1000km a 2000 km)
''')
while True:
locations_consumers = input('\033[1;36mEscolha do Usuário:\033[m ').strip()
multiplier, multiplier2 = location_consumers_is_1(locations_consumers, is_industry)
if multiplier == 0: multiplier, multiplier2 = location_consumers_is_2(locations_consumers, is_industry)
if multiplier == 0: multiplier, multiplier2 = location_consumers_is_3(locations_consumers, is_industry)
if multiplier == 0: multiplier, multiplier2 = location_consumers_is_4(locations_consumers, is_industry)
if multiplier != 0: break
if not is_industry:
annual_issue3 = deliveries * multiplier
else:
result = 0.00432 * deliveries
result2 = (multiplier2 * weight) * deliveries
annual_issue3 = result + result2
return annual_issue3
def show_details_enterprise() -> bool:
with open('enterprise.json', encoding='utf-8') as my_json:
data = json.load(my_json)
print('Escolha uma das opções abaixo\n')
for indice, enterprise in enumerate(data['empresas']):
if indice == 0:
print(f'[0] - Sair\n')
print(f"[{indice+1}] - {enterprise['nome']}, {enterprise['estado']}, {enterprise['cidade']}")
while True:
interprise_choiced = input('\n\033[1;36mEscolha do Usuário:\033[m ')
if interprise_choiced.isnumeric():
if 0 < int(interprise_choiced) <= len(data['empresas']):
break
if interprise_choiced == '0':
break
if interprise_choiced == '0':
return True
interprise_choiced = int(interprise_choiced)-1
info_interprise = data['empresas'][interprise_choiced]
values = list(info_interprise['dados'].values())
count = 0
decorator_text('INFORMAÇÕES DA EMPRESA', '=', clean_screen=True)
print(f'| Empresa: {info_interprise["nome"]}')
print(f'| Estado: {info_interprise["estado"]}')
print(f'| Cidade: {info_interprise["cidade"]}\n')
decorator_text('dados da empresa', '=')
for info in info_interprise['dados']:
if info.title() == 'Gasto Combustivel' or info.title() == 'Gasto Energia':
text = f'| {info.title()}: R$ {number_formatter(values[count])}'
print(text)
elif info.title() == 'Localizacao Consumidor':
print(f'| {info.title()}: {number_formatter(values[count])}Km')
elif info.title() == 'Peso Frete':
print(f'| {info.title()}: {number_formatter(values[count])}Kg')
elif info.title() == 'Co2 Emitido':
print(f'| {info.title()}: {number_formatter(round(values[count]))}T')
else:
print(f'| {info.title()}: {values[count]}')
count += 1
return False
def receive_number_workers() -> int:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
while True:
workers = input('Quantos funcionarios trabalham na sua empresa: ')
try:
workers = int(workers)
break
except ValueError:
print('\nDigite um valor válido!\n')
return workers
def user_says_informations() -> bool:
# # CHECANDO SETOR DA EMPRESA
company_sector, quit = receive_company_sector()
if quit: return True
# # CHECANDO QUANTIDADE DE FUNCIONÁRIOS NA EMPRESA
workers: int = receive_number_workers()
# # CHECANDO GASTO COM COMBUSTIVEL
annual_emission1, quit = check_fuel_expenditure()
if quit: return True
# # CHECANDO GASTO COM ENERGIA
annual_emission2, quit = check_energy_expenditure(workers, company_sector)
if quit: return True
# # CHECANDO FRETE
deliveries = check_number_deliveries()
if deliveries != 0:
annual_emission3 = check_location_consumers(deliveries, company_sector)
else:
annual_emission3 = 0
# # MOSTRANDO A EMISSÃO DE CARBONO AO USUÁRIO
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
total_annual_emission = round(annual_emission1 + annual_emission2 + annual_emission3)
min_value = round(total_annual_emission * 12)
max_value = round(total_annual_emission * 365)
print(f'Sua pegada de carbono anual é de {number_formatter(total_annual_emission)} Toneladas')
print('\nUm Crédito de Carbono equivale a 1 Tonelada de Carbono.')
print(f'\nLogo, sua empresa terá que comprar {number_formatter(total_annual_emission)} Créditos.')
print('\nO Crédito de Carbono no Brasil varia entre R$12,00 e R$365,00')
print(f'\nSua empresa terá que gastar em torno de R${number_formatter(min_value)},00 e R${number_formatter(max_value)},00')
input('\n\033[1;35mPressione enter para prosseguir...\033[m')
return False
def user_only_sees() -> None:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
result = show_details_enterprise()
if not result: input('\n\033[1;35mPressione enter para continuar...\033[m')
def engine():
while True:
while True:
decorator_text('calculadora de Emissão de Carbono', '=', clean_screen=True)
user_choice = input('''Escolha uma das opções abaixo
[0] Sair
[1] Escolher uma empresa já cadastrada em nosso sistema
[2] Passar as informações
\033[1;36mEscolha do Usuário:\033[m ''') # CHECANDO ESCOLHA INICIAL DO USUÁRIO
if user_choice == '0': # ENCERRANDO O PROGRAMA
break
if user_choice == '2': # PEDINDO OS DADOS PARA O USUÁRIO
result = user_says_informations()
if result: continue
if user_choice == '1': # MOSTRANDO OS DADOS DA EMPRESA ESCOLHIDA PELO USUÁRIO
user_only_sees()
break
if user_choice == '1': continue
text = '\n\033[1;31mObrigado por usar este programa\033[m. \033[1;31mVolte sempre\033[m!'
for letter in text:
print(letter, end='')
sleep(0.0225)
print()
break