0

I'm currently getting started with the pm4py library by playing around with some event logs. I have an example log that I'm importing like this:

from pm4py.objects.log.importer.xes import importer as xes_importer
log = xes_importer.apply('financial_log.xes')

If I now print a trace of the log the standard output looks like the following:

print(log[0])

{'attributes': {'REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'concept:name': '173688', 'AMOUNT_REQ': '20000'}, 'events': [{'org:resource': '112', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'A_SUBMITTED', 'time:timestamp': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:concept:name': '173688', 'case:AMOUNT_REQ': '20000'}, '..', {'org:resource': '10629', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'W_Valideren aanvraag', 'time:timestamp': datetime.datetime(2011, 10, 13, 10, 37, 37, 26000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:concept:name': '173688', 'case:AMOUNT_REQ': '20000'}]}

However, to better inspect the traces and events I tried to format the output to make it more readible. I tried using the pprint library but without any success. Here is what i tried:

from pprint import pprint
pprint(log, indent=1)

Output looks like this:

[{'attributes': {'REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'concept:name': '173688', 'AMOUNT_REQ': '20000'}, 'events': [{'org:resource': '112', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'A_SUBMITTED', 'time:timestamp': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:concept:name': '173688', 'case:AMOUNT_REQ': '20000'}, '..', {'org:resource': '10629', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'W_Valideren aanvraag', 'time:timestamp': datetime.datetime(2011, 10, 13, 10, 37, 37, 26000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:concept:name': '173688', 'case:AMOUNT_REQ': '20000'}]}, '....', {'attributes': {'REG_DATE': datetime.datetime(2012, 2, 29, 23, 51, 16, 799000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), 'concept:name': '214376', 'AMOUNT_REQ': '15000'}, 'events': [{'org:resource': '112', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'A_SUBMITTED', 'time:timestamp': datetime.datetime(2012, 2, 29, 23, 51, 16, 799000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), 'case:REG_DATE': datetime.datetime(2012, 2, 29, 23, 51, 16, 799000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), 'case:concept:name': '214376', 'case:AMOUNT_REQ': '15000'}, '..', {'org:resource': '11169', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'W_Afhandelen leads', 'time:timestamp': datetime.datetime(2012, 3, 1, 9, 27, 41, 325000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), 'case:REG_DATE': datetime.datetime(2012, 2, 29, 23, 51, 16, 799000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))), 'case:concept:name': '214376', 'case:AMOUNT_REQ': '15000'}]}]

Is there any way to print the object with linebreaks and correct indentation as shown on the pprint documentation site? I'm also open to other suggestion on how to print the traces 'pretty'.

beckersense
  • 310
  • 2
  • 12

1 Answers1

0

This does not work because the dict contains a datetime object. So when if you import datetime, it works.

This works:

import datetime
import pprint

x = {'attributes': {'REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'concept:name': '173688', 'AMOUNT_REQ': '20000'}, 'events': [{'org:resource': '112', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'A_SUBMITTED', 'time:timestamp': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:concept:name': '173688', 'case:AMOUNT_REQ': '20000'}, '..', {'org:resource': '10629', 'lifecycle:transition': 'COMPLETE', 'concept:name': 'W_Valideren aanvraag', 'time:timestamp': datetime.datetime(2011, 10, 13, 10, 37, 37, 26000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))), 'case:concept:name': '173688', 'case:AMOUNT_REQ': '20000'}]}

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(x)

result:

{   'attributes': {   'AMOUNT_REQ': '20000',
                      'REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))),
                      'concept:name': '173688'},
    'events': [   {   'case:AMOUNT_REQ': '20000',
                      'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))),
                      'case:concept:name': '173688',
                      'concept:name': 'A_SUBMITTED',
                      'lifecycle:transition': 'COMPLETE',
                      'org:resource': '112',
                      'time:timestamp': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))},
                  '..',
                  {   'case:AMOUNT_REQ': '20000',
                      'case:REG_DATE': datetime.datetime(2011, 10, 1, 0, 38, 44, 546000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))),
                      'case:concept:name': '173688',
                      'concept:name': 'W_Valideren aanvraag',
                      'lifecycle:transition': 'COMPLETE',
                      'org:resource': '10629',
                      'time:timestamp': datetime.datetime(2011, 10, 13, 10, 37, 37, 26000, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))}]}
D.L
  • 4,339
  • 5
  • 22
  • 45