Skip to content

Commit

Permalink
un-hardcoded a datetime-formatted string
Browse files Browse the repository at this point in the history
  • Loading branch information
MauererM committed Dec 20, 2023
1 parent 24b1af1 commit c46caed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class ProfitConfig:
# Delimiter used in all text- and csv-files throughout PROFIT
DELIMITER = ";"

# Date-format used throughout PROFIT
# Date-format used throughout PROFIT. Make sure to retain the formatting (e.g., %d., %m., %Y) and only change the
# order of these substrings if desired (e.g., in the US, the format "%m.%d.%Y" might be more common).
FORMAT_DATE = "%d.%m.%Y"

# The length of tabs as measured in spaces. In interactive mode, PROFIT tries to keep the formatting of the
Expand Down
17 changes: 15 additions & 2 deletions profit_src/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ class ParsingConfig:

# Strings for asset transactions-headers:
# These are used for accounts and investments:
# This dateformat should be the same as the one specified in config.py # Todo un-hardcode this.
STRING_DATE = "Date(DD.MM.YYYY)"
STRING_DATE = None # This one will be set up in the constructor, as it is format-specific.
STRING_ACTION = "Action"
STRING_AMOUNT = "Amount"
STRING_BALANCE = "Balance"
Expand Down Expand Up @@ -150,6 +149,20 @@ class ParsingConfig:
COLUMN_WIDTHS_INVESTMENTS = [20, 12, 12, 12, 12, 12, 12, 1] # date, action, quantity, price, cost, payout,
# balance, notes

def __init__(self, profit_config):
"""Constructor, to set up some profit-config-specific topics"""

# Convert the datetime-format-string from PROFIT to the appropriate name for the "Date" Column in the
# CSV files:
date_format = profit_config.FORMAT_DATE
format_mappings = {'%d': 'DD', '%m': 'MM', '%Y': 'YYYY'}
for key, value in format_mappings.items():
if key not in date_format:
raise RuntimeError(f"Specifier '{key}' not found in the date format string of config.py.")
date_format = date_format.replace(key, value)
self.STRING_DATE = f"Date({date_format})" # (this will take precedent over the None-defined class attribute)



class AccountFile:
"""Creates the structure/template for an account-file.
Expand Down
2 changes: 1 addition & 1 deletion profit_src/profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def main(config):
storage = MarketDataMain(storage_path, config.FORMAT_DATE, analyzer)

# Initialize the file parser config:
parsing_config = parsing.ParsingConfig()
parsing_config = parsing.ParsingConfig(config)

# Sanity checks:
if len(config.ASSET_GROUPNAMES) != len(config.ASSET_GROUPS):
Expand Down

0 comments on commit c46caed

Please sign in to comment.