handle float type on contact_all collection
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
import math
|
||||||
import random
|
import random
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ def write_new_contacts_to_excel(valid_contacts: list, file_name=str(datetime.dat
|
|||||||
row = 0
|
row = 0
|
||||||
col = 0
|
col = 0
|
||||||
# Create a workbook and add a worksheet.
|
# Create a workbook and add a worksheet.
|
||||||
workbook = xlsxwriter.Workbook('contact_list_{}.xlsx'.format(file_name))
|
workbook = xlsxwriter.Workbook('contact_list_{}.xlsx'.format(file_name), {'nan_inf_to_errors': True})
|
||||||
header_data = ['name', 'phone', 'passport', 'email', 'store', 'serial', 'ip_country', 'ua',
|
header_data = ['name', 'phone', 'passport', 'email', 'store', 'serial', 'ip_country', 'ua',
|
||||||
'resident_card_number', 'source_from']
|
'resident_card_number', 'source_from']
|
||||||
worksheet = workbook.add_worksheet()
|
worksheet = workbook.add_worksheet()
|
||||||
@@ -80,6 +81,27 @@ def write_new_contacts_to_excel(valid_contacts: list, file_name=str(datetime.dat
|
|||||||
for col_num, data in enumerate(header_data):
|
for col_num, data in enumerate(header_data):
|
||||||
worksheet.write(row, col_num, data, header_format)
|
worksheet.write(row, col_num, data, header_format)
|
||||||
row = row + 1
|
row = row + 1
|
||||||
|
|
||||||
|
def safe_write(row_num, col_num, value):
|
||||||
|
"""Écrire une valeur en la rendant vide si elle est NaN ou INF"""
|
||||||
|
try:
|
||||||
|
# Vérifier si c'est une chaîne contenant nan ou inf (case insensitive)
|
||||||
|
if isinstance(value, str):
|
||||||
|
if value.lower() in ['nan', 'inf', '-inf'] or 'nan' in value.lower() or 'inf' in value.lower():
|
||||||
|
worksheet.write(row_num, col_num, "")
|
||||||
|
else:
|
||||||
|
worksheet.write(row_num, col_num, value)
|
||||||
|
# Vérifier si c'est un nombre et si c'est NaN ou INF
|
||||||
|
elif isinstance(value, (int, float)):
|
||||||
|
if math.isnan(value) or math.isinf(value):
|
||||||
|
worksheet.write(row_num, col_num, "")
|
||||||
|
else:
|
||||||
|
worksheet.write(row_num, col_num, value)
|
||||||
|
else:
|
||||||
|
worksheet.write(row_num, col_num, value if value is not None else "")
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
worksheet.write(row_num, col_num, "")
|
||||||
|
|
||||||
for info in valid_contacts:
|
for info in valid_contacts:
|
||||||
# Iterate over the data and write it out row by row.
|
# Iterate over the data and write it out row by row.
|
||||||
worksheet.write(row, col, "{} {}".format(info.last_name, info.first_name))
|
worksheet.write(row, col, "{} {}".format(info.last_name, info.first_name))
|
||||||
@@ -89,7 +111,7 @@ def write_new_contacts_to_excel(valid_contacts: list, file_name=str(datetime.dat
|
|||||||
worksheet.write(row, col + 4, info.store)
|
worksheet.write(row, col + 4, info.store)
|
||||||
worksheet.write(row, col + 5, info.serial)
|
worksheet.write(row, col + 5, info.serial)
|
||||||
worksheet.write(row, col + 6, info.ip_country)
|
worksheet.write(row, col + 6, info.ip_country)
|
||||||
worksheet.write(row, col + 7, info.ua)
|
safe_write(row, col + 7, info.ua)
|
||||||
worksheet.write(row, col + 8, info.resident_card_number)
|
worksheet.write(row, col + 8, info.resident_card_number)
|
||||||
worksheet.write(row, col + 9, info.source_from)
|
worksheet.write(row, col + 9, info.source_from)
|
||||||
row += 1
|
row += 1
|
||||||
@@ -97,7 +119,7 @@ def write_new_contacts_to_excel(valid_contacts: list, file_name=str(datetime.dat
|
|||||||
|
|
||||||
|
|
||||||
def generate_valid_contact_list_for_day(segment_number=1):
|
def generate_valid_contact_list_for_day(segment_number=1):
|
||||||
_collection_name = "2025-08-14"
|
_collection_name = "2026-03-26"
|
||||||
_valid_contact_list = MONGO_STORE_MANAGER.get_all_successful_items_for_one_day(_collection_name)
|
_valid_contact_list = MONGO_STORE_MANAGER.get_all_successful_items_for_one_day(_collection_name)
|
||||||
_all_contacts = MONGO_STORE_MANAGER.get_all_contacts_to_book()
|
_all_contacts = MONGO_STORE_MANAGER.get_all_contacts_to_book()
|
||||||
_contact_to_save = []
|
_contact_to_save = []
|
||||||
@@ -112,7 +134,8 @@ def generate_valid_contact_list_for_day(segment_number=1):
|
|||||||
_contact.passport = str(_true_contact.resident_card_number)[:9]
|
_contact.passport = str(_true_contact.resident_card_number)[:9]
|
||||||
_contact.first_name = _true_contact.first_name
|
_contact.first_name = _true_contact.first_name
|
||||||
_contact.resident_card_number = str(_true_contact.resident_card_number)[:9]
|
_contact.resident_card_number = str(_true_contact.resident_card_number)[:9]
|
||||||
if _true_contact.source_from is not None and len(_true_contact.source_from) > 0:
|
print("{}:{}".format(_true_contact.mail, _true_contact.source_from))
|
||||||
|
if isinstance(_true_contact.source_from, str) and _true_contact.source_from is not None and len(_true_contact.source_from) > 0:
|
||||||
print(_true_contact.source_from)
|
print(_true_contact.source_from)
|
||||||
_contact.source_from = _true_contact.source_from
|
_contact.source_from = _true_contact.source_from
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user