orger icon indicating copy to clipboard operation
orger copied to clipboard

Problem with sorting json state file

Open Tallented-Code-bot opened this issue 3 years ago • 0 comments

When I run this code for the first time, it works properly and creates the org file and a json state file.

from orger import Queue
from orger.inorganic import node, link
from orger.common import todo

from canvas2org import assignments


class CanvasTodos(Queue):
    def get_items(self) -> Queue.Results:
        for assignment in assignments():
            yield assignment.id, todo( #assignment.id is an integer id
                dt=assignment.due_at_date,
                heading=assignment.name)


if __name__=="__main__":
    CanvasTodos.main()

When I run it the second time, it throws this error:

Traceback (most recent call last):
  File "/home/calvin/canvas-orgmode/main.py", line 27, in <module>
    CanvasTodos.main()
  File "/home/calvin/canvas-orgmode/env/lib/python3.9/site-packages/orger/org_view.py", line 258, in main
    inst._run(
  File "/home/calvin/canvas-orgmode/env/lib/python3.9/site-packages/orger/org_view.py", line 235, in _run
    state.feed(
  File "/home/calvin/canvas-orgmode/env/lib/python3.9/site-packages/orger/state.py", line 67, in feed
    self[key] = repr(value)
  File "/home/calvin/canvas-orgmode/env/lib/python3.9/site-packages/orger/state.py", line 48, in __setitem__
    json.dump(current, fo, indent=1, sort_keys=True)
  File "/home/calvin/miniconda3/lib/python3.9/json/__init__.py", line 179, in dump
    for chunk in iterable:
  File "/home/calvin/miniconda3/lib/python3.9/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/calvin/miniconda3/lib/python3.9/json/encoder.py", line 353, in _iterencode_dict
    items = sorted(dct.items())
TypeError: '<' not supported between instances of 'int' and 'str'

However, if I change the assignment.id to an assignment.name, a string value, everything works properly and there are no errors.

The json state file generated by the first code looks like this:

{
 "458483": "OrgNode(heading='Assignment 1', todo='TODO', tags=(), scheduled=datetime.date(2022, 5, 27), properties={'CREATED': '[2022-05-27 Fri 06:59]'}, body=None, children=(), escaped=False)",
 "459994": "OrgNode(heading='Assignment 2', todo='TODO', tags=(), scheduled=datetime.date(2022, 6, 1), properties={'CREATED': '[2022-06-01 Wed 06:59]'}, body=None, children=(), escaped=False)"
}

It looks like the json sorter is having trouble comparing the original integers to the json integers encoded in strings.

Tallented-Code-bot avatar May 26 '22 12:05 Tallented-Code-bot