python-consul icon indicating copy to clipboard operation
python-consul copied to clipboard

sort kv.get by ModifyIndex

Open freeseacher opened this issue 8 years ago • 1 comments

hi It will be perfect if results returned by kv.get can be sorted by ModifyIndex Currently i have such code

index, responce = self.consul_api.kv.get(path, recurse=True)
if not responce:
    return
for resp in responce:
    if resp['Value']:
        try:
            metadata = json.loads(resp['Value'])
            if not isinstance(metadata, dict):
                raise ValueError
        except (ValueError, AttributeError):
            key = resp['Key'].split("/")[-1]
            metadata = {
                key: resp['Value']
            }
        for k, v in metadata.items():
            self.add_metadata(node_data, k, v)

and such structure in consul

  {
    "Key": "some_key",
    "CreateIndex": 4344,
    "ModifyIndex": 4412,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "{\"weight\":\"12\"},
    "Session": ""
  },
  {
    "Key": "some_key/weight",
    "CreateIndex": 4401,
    "ModifyIndex": 4401,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "4",
    "Session": ""
  }

looking to ModifyIndex we understand that value should be 4. but we got 12 instead.

freeseacher avatar Dec 21 '16 07:12 freeseacher

Hi there. I don't quite understand what you're trying to do, but I think your best bet is to sort the list after you get the response from Consul

https://wiki.python.org/moin/HowTo/Sorting#Operator_Module_Functions

cablehead avatar Dec 28 '16 22:12 cablehead