def check_chars_pinyin(transformer: Dict[str, str], schema: ShuangPinSchema): to_update_items = [] for item in CharPhoneTable.select(): full = item.full if schema == XHE_SP_SCHEMA: shuangpin = item.xhe elif schema == LU_SP_SCHEMA: shuangpin = item.lu elif schema == ZRM_SP_SCHEMA: shuangpin = item.zrm elif schema == BINGJI_SP_SCHEMA: shuangpin = item.bingji else: raise RuntimeError(f"unkonwn schame {schema}") s, y = split_sy(full) sp = transformer[s] + transformer[y] if shuangpin != sp: if schema == XHE_SP_SCHEMA: item.xhe = sp elif schema == LU_SP_SCHEMA: item.lu = sp elif schema == ZRM_SP_SCHEMA: item.zrm = sp elif schema == BINGJI_SP_SCHEMA: item.bingji = sp else: raise RuntimeError(f"unkonwn schame {schema}") to_update_items.append(item) with db.atomic(): if schema == XHE_SP_SCHEMA: CharPhoneTable.bulk_update(to_update_items, fields=['xhe'], batch_size=100) elif schema == LU_SP_SCHEMA: CharPhoneTable.bulk_update(to_update_items, fields=['lu'], batch_size=100) elif schema == ZRM_SP_SCHEMA: CharPhoneTable.bulk_update(to_update_items, fields=['zrm'], batch_size=100) elif schema == BINGJI_SP_SCHEMA: CharPhoneTable.bulk_update(to_update_items, fields=['bingji'], batch_size=100) else: raise RuntimeError(f"unkonwn schame {schema}") print(to_update_items) print(f'update {len(to_update_items)} char items')
if __name__ == "__main__": null_xhe_count = CharPhoneTable.select().where(CharPhoneTable.xhe == '').count() if null_xhe_count != 0: print(f'{null_xhe_count} null xhe phones, please check manually.') null_full_count = CharPhoneTable.select().where(CharPhoneTable.full == '').count() if null_full_count != 0: to_update_full_items = pipe(CharPhoneTable.select().where(CharPhoneTable.full == ''), map(lambda e: (e, ''.join(get_full(e.char)))), map(lambda e: update_full(e[0], e[1])), ) with db.atomic(): CharPhoneTable.bulk_update(to_update_full_items, fields=['full'], batch_size=100) del to_update_full_items null_full_count = CharPhoneTable.select().where(CharPhoneTable.full == '').count() if null_full_count != 0: print(f'{null_full_count} null full full phones, please check manually.') null_zrm_count = CharPhoneTable.select().where(CharPhoneTable.zrm == '').count() if null_zrm_count != 0: full_to_zrm_transformaer = get_full_to_zrm_transformmer() to_update_zrm_items = pipe(CharPhoneTable.select().where(CharPhoneTable.zrm == ''), map(lambda e: fill_zrm(e, full_to_zrm_transformaer)), ) with db.atomic(): CharPhoneTable.bulk_update(to_update_zrm_items, fields=['zrm'], batch_size=100) del to_update_zrm_items del full_to_zrm_transformaer