Skip to content

Commit

Permalink
Use with to automatically close opened file
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrukman committed Aug 2, 2024
1 parent 10501df commit 8b25b0d
Showing 1 changed file with 48 additions and 48 deletions.
96 changes: 48 additions & 48 deletions interactive_brokers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,54 +71,54 @@ def isFileForBroker(cls, filename):

@classmethod
def parseFileToTxnList(cls, filename, tax_year):
f = open(filename)
# First 2 lines are headers.
f.readline()
f.readline()
txns = csv.reader(f, delimiter=',', quotechar='"')
with open(filename) as f:
# First 2 lines are headers.
f.readline()
f.readline()
txns = csv.reader(f, delimiter=',', quotechar='"')

txn_list = []
part = None
box = None
entry_code = None
txn_list = []
part = None
box = None
entry_code = None

for row in txns:
if row[0] == 'Part' and len(row) == 3:
box = None
if row[1] == 'I':
part = 1
elif row[1] == 'II':
part = 2
else:
utils.Warning('unknown part line: "%s"\n' % row)
elif row[0] == 'Box' and len(row) == 3:
if row[1] == 'A' or row[1] == 'B' or row[1] == 'C':
box = row[1]
entry_code = cls.DetermineEntryCode(part, box)
else:
utils.Warning('unknown box line: "%s"\n' % row)
elif row[0] == 'Data' and len(row) == 9:
if not entry_code:
utils.Warning(
'ignoring data: "%s" as the code is not defined\n')
continue
txn = utils.Transaction()
txn.desc = row[1]
txn.buyDateStr = row[3]
txn.sellDateStr = row[4]
year = cls.TryParseYear(txn.sellDateStr)
txn.saleProceeds = cls.ParseDollarValue(row[5])
txn.costBasis = cls.ParseDollarValue(row[6])
if row[7]:
txn.adjustment = cls.ParseDollarValue(row[7])
txn.entryCode = entry_code
if tax_year and year and year != tax_year:
utils.Warning('ignoring txn: "%s" as the sale is not from %d\n' %
(txn.desc, tax_year))
else:
txn_list.append(txn)
txn = None
elif (row[0] != 'Header' and row[0] != 'Footer') or len(row) != 9:
utils.Warning('unknown line: "%s"\n' % row)
for row in txns:
if row[0] == 'Part' and len(row) == 3:
box = None
if row[1] == 'I':
part = 1
elif row[1] == 'II':
part = 2
else:
utils.Warning('unknown part line: "%s"\n' % row)
elif row[0] == 'Box' and len(row) == 3:
if row[1] == 'A' or row[1] == 'B' or row[1] == 'C':
box = row[1]
entry_code = cls.DetermineEntryCode(part, box)
else:
utils.Warning('unknown box line: "%s"\n' % row)
elif row[0] == 'Data' and len(row) == 9:
if not entry_code:
utils.Warning(
'ignoring data: "%s" as the code is not defined\n')
continue
txn = utils.Transaction()
txn.desc = row[1]
txn.buyDateStr = row[3]
txn.sellDateStr = row[4]
year = cls.TryParseYear(txn.sellDateStr)
txn.saleProceeds = cls.ParseDollarValue(row[5])
txn.costBasis = cls.ParseDollarValue(row[6])
if row[7]:
txn.adjustment = cls.ParseDollarValue(row[7])
txn.entryCode = entry_code
if tax_year and year and year != tax_year:
utils.Warning('ignoring txn: "%s" as the sale is not from %d\n' %
(txn.desc, tax_year))
else:
txn_list.append(txn)
txn = None
elif (row[0] != 'Header' and row[0] != 'Footer') or len(row) != 9:
utils.Warning('unknown line: "%s"\n' % row)

return txn_list
return txn_list

0 comments on commit 8b25b0d

Please sign in to comment.