Code Duplication    Length = 22-23 lines in 2 locations

myems-normalization/meter.py 1 location

@@ 403-425 (lines=23) @@
400
    ####################################################################################################################
401
    # Step 4: Insert into energy database
402
    ####################################################################################################################
403
    while len(normalized_values) > 0:
404
        insert_100 = normalized_values[:100]
405
        normalized_values = normalized_values[100:]
406
        try:
407
            add_values = (" INSERT INTO tbl_meter_hourly (meter_id, start_datetime_utc, actual_value) "
408
                          " VALUES  ")
409
410
            for meta_data in insert_100:
411
                add_values += " (" + str(meter['id']) + ","
412
                add_values += "'" + meta_data['start_datetime_utc'].isoformat()[0:19] + "',"
413
                add_values += str(meta_data['actual_value']) + "), "
414
            # trim ", " at the end of string and then execute
415
            cursor_energy_db.execute(add_values[:-2])
416
            cnx_energy_db.commit()
417
        except Exception as e:
418
            error_string = "Error in step 4.1 of meter.worker " + str(e) + " for '" + meter['name'] + "'"
419
            if cursor_energy_db:
420
                cursor_energy_db.close()
421
            if cnx_energy_db:
422
                cnx_energy_db.close()
423
424
            print(error_string)
425
            return error_string
426
427
    if cursor_energy_db:
428
        cursor_energy_db.close()

myems-normalization/virtualmeter.py 1 location

@@ 452-473 (lines=22) @@
449
450
    print("saving energy values to table energy virtual meter hourly...")
451
452
    while len(normalized_values) > 0:
453
        insert_100 = normalized_values[:100]
454
        normalized_values = normalized_values[100:]
455
        try:
456
            add_values = (" INSERT INTO tbl_virtual_meter_hourly "
457
                          " (virtual_meter_id, start_datetime_utc, actual_value) "
458
                          " VALUES  ")
459
460
            for meta_data in insert_100:
461
                add_values += " (" + str(virtual_meter['id']) + ","
462
                add_values += "'" + meta_data['start_datetime_utc'].isoformat()[0:19] + "',"
463
                add_values += str(meta_data['actual_value']) + "), "
464
            print("add_values:" + add_values)
465
            # trim ", " at the end of string and then execute
466
            cursor_energy_db.execute(add_values[:-2])
467
            cnx_energy_db.commit()
468
        except Exception as e:
469
            if cursor_energy_db:
470
                cursor_energy_db.close()
471
            if cnx_energy_db:
472
                cnx_energy_db.close()
473
            return "Error in step 4.2 virtual meter worker " + str(e) + " for '" + virtual_meter['name'] + "'"
474
475
    if cursor_energy_db:
476
        cursor_energy_db.close()