Как получить новые данные из списка словарей?

Я делаю запрос к API и получаю в ответ список словарей:

[{'fullName': 'd KsrionohC', 'reportedTitle': 'iagtcpcocOinPfrf lirinAnce u', 'effectiveDate': 1638397365511, 'tranShares': -4499, 'tranPrice': 320.6, 'tranValue': -1437179.49, 'directIndirect': 'D', 'tranCode': 'S'}, 
 {'fullName': 'neAarJun dg', 'reportedTitle': 'irtceoDr', 'effectiveDate': 1617488519947, 'tranShares': -9899, 'tranPrice': None, 'tranValue': None, 'directIndirect': 'D', 'tranCode': 'M'}, 
 {'fullName': 'uenAaJ dngr', 'reportedTitle': 'ecoDrtri', 'effectiveDate': 1591113566625, 'tranShares': 9854, 'tranPrice': 50.29, 'tranValue': 491036.1, 'directIndirect': 'I', 'tranCode': 'M'}]

Я делаю несколько таких запросов и в какой-то момент в этот список может добавиться еще один словарь. Мне нужно получить этот словарь. У API нет WebSoket, нет WebHook. Есть SSE, но он не поддерживает нужный мне метод. Поэтому, обновления я могу получать только через GET-запросы.

Как мне без записи в БД получать только обновлённые данные?


Ответы (1 шт):

Автор решения: MaxU

Можно сравнивать хеш кортежа значений словарей.

Пример данных:

In [24]: data1 = \
    ...: [{'fullName': 'd KsrionohC',
    ...:   'reportedTitle': 'iagtcpcocOinPfrf lirinAnce u',
    ...:   'effectiveDate': 1638397365511,
    ...:   'tranShares': -4499,
    ...:   'tranPrice': 320.6,
    ...:   'tranValue': -1437179.49,
    ...:   'directIndirect': 'D',
    ...:   'tranCode': 'S'},
    ...:  {'fullName': 'neAarJun dg',
    ...:   'reportedTitle': 'irtceoDr',
    ...:   'effectiveDate': 1617488519947,
    ...:   'tranShares': -9899,
    ...:   'tranPrice': None,
    ...:   'tranValue': None,
    ...:   'directIndirect': 'D',
    ...:   'tranCode': 'M'}]

In [25]: data2 = \
    ...: [{'fullName': 'd KsrionohC',
    ...:   'reportedTitle': 'iagtcpcocOinPfrf lirinAnce u',
    ...:   'effectiveDate': 1638397365511,
    ...:   'tranShares': -4499,
    ...:   'tranPrice': 320.6,
    ...:   'tranValue': -1437179.49,
    ...:   'directIndirect': 'D',
    ...:   'tranCode': 'S'},
    ...:  {'fullName': 'neAarJun dg',
    ...:   'reportedTitle': 'irtceoDr',
    ...:   'effectiveDate': 1617488519947,
    ...:   'tranShares': -9899,
    ...:   'tranPrice': None,
    ...:   'tranValue': None,
    ...:   'directIndirect': 'D',
    ...:   'tranCode': 'M'},
    ...:  {'fullName': 'uenAaJ dngr',
    ...:   'reportedTitle': 'ecoDrtri',
    ...:   'effectiveDate': 1591113566625,
    ...:   'tranShares': 9854,
    ...:   'tranPrice': 50.29,
    ...:   'tranValue': 491036.1,
    ...:   'directIndirect': 'I',
    ...:   'tranCode': 'M'}]

решение:

new_hashes = set([hash(tuple(x.values())) for x in data2]) - \
             set([hash(tuple(x.values())) for x in data1])

new_records = [x for x in data2 if hash(tuple(x.values())) in new_hashes]

результат:

In [46]: new_records
Out[46]:
[{'fullName': 'uenAaJ dngr',
  'reportedTitle': 'ecoDrtri',
  'effectiveDate': 1591113566625,
  'tranShares': 9854,
  'tranPrice': 50.29,
  'tranValue': 491036.1,
  'directIndirect': 'I',
  'tranCode': 'M'}]
→ Ссылка