glourencoffee / pycvm

Python library for processing data from CVM
MIT License
2 stars 0 forks source link

Access accounts in the DMPL by using attributes instead of column names #7

Closed glourencoffee closed 2 years ago

glourencoffee commented 2 years ago

Description

As a library user, I want to read columns of a DMPL statement as known attributes instead of looking up its full name.

Challenge

Having to access accounts of a DMPL column by its full name, which may raise KeyError.

Benefit

Ease of use, explicitness

Acceptance criteria

Additional context

In order to read accounts from a column in the DMPL, I have to specify its full name:

dfpitr.consolidated.last.dmpl.columns['Patrimônio Líquido Consolidado'] # may raise KeyError
dfpitr.consolidated.last.dmpl.columns['Patrimônio Líquido']             # never raises KeyError

I want it to be like this:

dfpitr.consolidated.last.dmpl.consolidated_equity # may be None
dfpitr.consolidated.last.dmpl.equity              # is never None
glourencoffee commented 2 years ago

This feature was implemented in a slightly different way than proposed. Instead of having many attributes in the class DMPL for each column, attributes were created in a new class DMPLAccount:

@dataclasses.dataclass(init=True)
class DMPLAccount(BaseAccount):
    share_capital: int
    ...
    consolidated_equity: typing.Optional[int]

DMPLAccount is similar to Account, with the difference being that Account.quantity is replaced with each column of a DMPL statement, that is, DMPLAccount.share_capital, DMPLAccount.consolidated_equity, etc.

This implementation was preferred over the initially proposed one because it results in a simpler and clearer usage. Compare:

dmpl = DMPL(...)
count = len(dmpl.equity)

for i in range(count):
    dmpl.share_capital[i]
    dmpl.equity[i]
    ...

with:

dmpl = DMPL(...)

for account in dmpl.accounts:
    account.share_capital
    account.equity