dballe icon indicating copy to clipboard operation
dballe copied to clipboard

Python api do not implement an --rejected option as in dbadb import

Open pat1 opened this issue 4 years ago • 1 comments

in:

 with db.transaction() as tr:
     with importer.from_file(totalbodyfile) as f:
          tr.import_messages(f,overwrite=True, update_station=True,import_attributes=True)

I cannot use --rejected option as in "dbadb import". Is the same to develop

for message in f:
  try:
    tr.import_messages(message,overwrite=True, update_station=True,import_attributes=True)
  except:
    print ("rejected")

for perfomance too?

pat1 avatar May 09 '21 19:05 pat1

Would something like this work?

    importer = dballe.Importer("BUFR")
    exporter = dballe.Exporter("BUFR")
    with open("rejected.bufr", "wb") as out:
        with db.transaction() as tr:
            with dballe.File(pathname) as f:
                for binmsg in f:
                    for msg in importer.from_binary(binmsg):
                        try:
                            tr.import_messages(msg)
                        except Exception as e:
                            print("Error importing message:", e)
                            binmsg = exporter.to_binary(msg)
                            out.write(binmsg)

If it can help, I just added examples to the documentation of various ways to import messages

spanezz avatar Nov 23 '21 13:11 spanezz