Skip to content

Instantly share code, notes, and snippets.

@johanste
Created August 23, 2019 16:42
Show Gist options
  • Save johanste/7f2d782826084426c90c904ed12acc3c to your computer and use it in GitHub Desktop.
Save johanste/7f2d782826084426c90c904ed12acc3c to your computer and use it in GitHub Desktop.
Example more pythonic for event hubs load balancer
import collections
partition_ids = range(1, 7)
ownerships = [
{
'last_modified_time': 7,
'partition_id': 1,
'ownership': 1
},
{
'last_modified_time': 7,
'partition_id': 1,
'ownership': 1
},
{
'last_modified_time': 3,
'partition_id': 2,
'ownership': 1
},
{
'last_modified_time': 4,
'partition_id': 3,
'ownership': 2
},
{
'last_modified_time': 7,
'partition_id': 4,
'ownership': 2
},
{
'last_modified_time': 5,
'partition_id': 5,
'ownership': 3
},
{
'last_modified_time': 2,
'partition_id': 6,
'ownership': 5
},
]
by_owner = collections.defaultdict(list)
for o in ownerships:
by_owner[o['ownership']].append(o)
not_owned = [pid for pid in partition_ids if pid not in by_owner]
timed_out = [ownership for ownership in ownersips if ownership['last_modified_time' < 6]]
print(by_owner)
print(not_owned)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment