Wow, drop idiotic `return` inside `finally:`

Can't believe i missed this but any `return` inside a `finally` will
suppress the error from the `try:` part... XD

Thought i was losing my mind when the ledger was mutated and then
an error just after wasn't getting raised.. lul.

Never again...
pptables
Tyler Goodlet 2022-07-21 15:28:04 -04:00
parent ddb195ed2c
commit 5684120c11
1 changed files with 31 additions and 36 deletions

View File

@ -82,18 +82,18 @@ def open_trade_ledger(
ledger = tomli.load(cf) ledger = tomli.load(cf)
print(f'Ledger load took {time.time() - start}s') print(f'Ledger load took {time.time() - start}s')
cpy = ledger.copy() cpy = ledger.copy()
try:
yield cpy
finally:
if cpy != ledger:
# TODO: show diff output?
# https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries
print(f'Updating ledger for {tradesfile}:\n')
ledger.update(cpy)
# we write on close the mutated ledger data yield cpy
with open(tradesfile, 'w') as cf:
return toml.dump(ledger, cf) if cpy != ledger:
# TODO: show diff output?
# https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries
print(f'Updating ledger for {tradesfile}:\n')
ledger.update(cpy)
# we write on close the mutated ledger data
with open(tradesfile, 'w') as cf:
toml.dump(ledger, cf)
class Transaction(Struct, frozen=True): class Transaction(Struct, frozen=True):
@ -764,36 +764,31 @@ def open_pps(
clears=clears, clears=clears,
) )
# orig = pp_objs.copy() yield table
try:
yield table
finally:
# breakpoint()
# if orig != table.pps:
if not write_on_exit: if not write_on_exit:
return return
# TODO: show diff output? # TODO: show diff output?
# https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries # https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries
print(f'Updating ``pps.toml`` for {path}:\n') print(f'Updating ``pps.toml`` for {path}:\n')
pp_entries, closed_pp_objs = table.dump_active(brokername) pp_entries, closed_pp_objs = table.dump_active(brokername)
conf[brokername][acctid] = pp_entries conf[brokername][acctid] = pp_entries
# TODO: why tf haven't they already done this for inline # TODO: why tf haven't they already done this for inline
# tables smh.. # tables smh..
enc = PpsEncoder(preserve=True) enc = PpsEncoder(preserve=True)
# table_bs_type = type(toml.TomlDecoder().get_empty_inline_table()) # table_bs_type = type(toml.TomlDecoder().get_empty_inline_table())
enc.dump_funcs[ enc.dump_funcs[
toml.decoder.InlineTableDict toml.decoder.InlineTableDict
] = enc.dump_inline_table ] = enc.dump_inline_table
config.write( config.write(
conf, conf,
'pps', 'pps',
encoder=enc, encoder=enc,
) )
def update_pps_conf( def update_pps_conf(