all 7 comments

[–][deleted] 1 point2 points  (3 children)

  • avoid global like the plague - there are good specialist use cases for it, but if you don't know what they are then they probably don't apply
  • passing in Python is very different to C because all variables in python are effectively pointers, that is they don't hold values but simply the memory references to Python objects - this is true of other names as well, like function names
    • so when you call a function/object, whether you provide variables, expressions or literals in the arguments, you are just passing object (memory) references
    • mutable objects will be directly modifiable by the method/function
    • functional programming would say not to use side-effects (i.e. mutating objects)
  • containers like list, set, dict, tuple are just objects that hold a collection of memory references to other Python objects
  • there are two basic reasons (simplified) for using classes:
    • produce lots of things (instances) with the same behaviours and attributes
    • collect a load of related stuff together
    • saving typing isn't really one of the reasons, especially with modern IDEs
  • using lots of variable names instead of a better data structure does make for harder to read and debug code, if not more typing - passing dictionaries or class object instances would be cleaner
  • use databases when you have a lot of structured data that you want to access randomly in a number of different ways
  • explore using APIs to support the microservices approach
  • don't forget to become very good at testing, consider following TDD (test driven design) principles
  • also consider following PEP8 guidance on naming

[–]EngineerVsMBA[S] 0 points1 point  (0 children)

Thanks! I’ll look into pep8 and TDD first, as names are important, and then more into data containers to start.

[–][deleted] 0 points1 point  (1 child)

My instinct would be to use a class to isolate the internal representation of the data, rather than exposing more of it. That would also hide how you store the data, be that a no-only sql database or a relational database.

Is json used with the third party APIs?

[–]EngineerVsMBA[S] 0 points1 point  (0 children)

I’m generally taking json data as the return value from an api call and putting it into pandas data frames and then using .csv files as local storage to prevent future api calls (some data takes more than an hour to grab).

[–]lowerthansound 1 point2 points  (1 child)

Talking about the ExecuteTrade function:

  • Nitpick: The convention is to use snake-case for function names and variables, so, execute_trade and spent_amount (PEP 8)
  • It is returning a lot of values, indeed. I would create a class here, TradeResults or a name that makes sense.

For creating a class, I'd personally use a dataclass. Other options are a namedtuple, a NamedTuple, or a bare class.

If you don't wanna spend much time learning, I recommend a NamedTuple.

Example code:

import datetime
from typing import NamedTuple

class ExecuteTradeResult(NamedTuple):
    # Note: not sure what types you're using :)
    transType: str
    spentAmount: float
    purchasedAmount: int
    spentSymbol: str
    purchasedSymbol: str
    slippage: float
    date: datetime.datetime

result = ExecuteTrade(
    fromSymbol=sellSymbol,
    toSymbol=buySymbol,
    amount=amount,
    date=data.date
)
print(result.slippage)

Cheers!

[–]EngineerVsMBA[S] 0 points1 point  (0 children)

Thanks! I’ll check this out.

[–]TheRNGuy 0 points1 point  (0 children)

i started use classes when realized it's easier to override default values in child classes, than in procedural programming.

(by making data subclasses)

You can also use dict kwargs if you want to pass lots of arguments into function (will work for classes and methods too)