lundi 23 août 2021

Python: Values Not Assigned Correctly to Key in Loop

I am trying to create a calibration scheduler in Python. To test the program, I just want to be able generate random dates to assign to a "property" of each instrument. For a reason I can't explain, the output clearly indicates that randdays is correctly randomized yet when assigned to the last_calibrated "property", it indicates identical values for all instruments! Why is this and how can I avoid it?

instrument_props = {"last_calibrated": None,
                 "calibration_due": None,
                 "calibration_expired": None,
                 "installed": None,
                 "replaced_by": None}

instruments = {}

start_date = datetime.date(2020, 1, 1)
end_date = datetime.date(2020, 2, 1)
time_between_dates = end_date - start_date
days_between_dates = time_between_dates.days

for instrument in [4167,4958,4959,4960,4961,4962,4963,4964,4965,4966,4967,4968,4969,4970,4971,4972]:
    instruments[instrument] = instrument_props
    randdays = random.randrange(days_between_dates)
    print(randdays)
    instruments[instrument]['last_calibrated'] = start_date + datetime.timedelta(days=randdays)

pp.pprint(instruments)

Here is the output for the above code:

24
25
30
25
0
24
23
9
23
20
16
30
3
20
21
29

{   4167: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4958: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4959: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4960: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4961: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4962: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4963: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4964: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4965: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4966: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4967: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4968: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4969: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4970: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4971: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None},
    4972: {   'calibration_due': None,
              'calibration_expired': None,
              'installed': None,
              'last_calibrated': datetime.date(2020, 1, 30),
              'replaced_by': None}}



Aucun commentaire:

Enregistrer un commentaire