I'd like to convert the output of pycaching.cache.Cache.load_logbook() into a list of pycaching.log.Log objects

Question:
I’d like to convert the output of pycaching.cache.Cache.load_logbook() into a list of pycaching.log.Log objects. How would I do this?
API reference — pycaching 4.2.3 documentation

@Firepup650 I know you previously helped me with someone similar within Pycaching, if you could help a bit more, that’d be great!

After reading up a bit on how “generator” functions work, I think this should work:

logs = list(pycaching.cache.Cache.load_logbook())
1 Like

@Firepup650 Tried that already, it looks like it should work, but it raises the error raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable") TypeError: Object of type Log is not JSON serializable

Weird, but what about this:

logs = []
for _log in pycaching.cache.Cache.load_logbook():
    logs.append(_log)

This should work fine.

Assuming you are using flask, this would probably mean that you are trying to return the list of Logs, which flask tries to convert into a response object using jsonify so it can be sent. pycaching.log.Log is not JSON serializable.
So you’ll have to convert the logs into, for example, a dict.

So I cannot convert logs into a list?
If I can’t, how do I convert it into a dict, as you mentioned?

Do you know the structure of each Log by chance?

The docs don’t really explain it :confused:

Could you send the output of this by chance?

logs = list(pycaching.cache.Cache.load_logbook())
print(logs[0])

The problem is that the components of Log need to be serialized too.
If you are not concerned about deserializing, then converting it all to strings is enough.

def to_dict(log):
  return {
    name: str(getattr(log, name, 'null'))
    for name in 'author text type uuid visited'.split()
  }

logs = [to_dict(log) for log in pycaching.cache.Cache.load_logbook()]
1 Like

@NuclearPasta0 Great! It’s working! Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.