2
\$\begingroup\$

I'm currently learning some aspects of PEP8 and Python programming in general. Could you please code-review this little piece of code for me? I would be glad to hear any, even the smallest comments, thanks!

from array import array
from typing import NamedTuple
from pycoingecko import CoinGeckoAPI

cg = CoinGeckoAPI()
savedTokensArray = ['bitcoin', 'ethereum']

class CryptoCurrency(NamedTuple):
    currencyName: str
    currencyPrice: float


def get_tokens_price(tokenIds: array) -> dict[str, float]:
    tokens = None
    for token in tokenIds:
        tokens = f'{tokens}, {token}'
    tokensList = cg.get_price(ids=tokens, vs_currencies='usd')
    data = [CryptoCurrency(currencyName=currency, currencyPrice=tokensList[currency]['usd']) for currency in tokensList]
    currencysDict = {}
    for token in data:
        currencysDict[token.currencyName] = token.currencyPrice

    return currencysDict


resultDict = get_tokens_price(savedTokensArray)
print(resultDict)
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

First, PEP8 suggests using snake_case instead of camelCase for regular variable names, so you should rename those.

def get_tokens_price(tokenIds: array) -> dict[str, float]:

array is the wrong type annotation. This is not an array.array1 - this is a list of strings. list[str] .


tokens = None
for token in tokenIds:
    tokens = f'{tokens}, {token}'
tokensList = cg.get_price(ids=tokens, vs_currencies='usd')

No need for a for loop here - just use str.join.

tokens_list = cg.get_price(ids=",".join(token_ids), vs_currencies='usd')

You also construct a list of NamedTuples, just to use it to construct the dictionary that you return. Just construct the dictionary directly, and get rid of CryptoCurrency. You can also use dict.items() to iterate over (key, value) pairs directly.

currencies = {currency: prices['usd'] for currency, prices in tokens_list.items()}

Full code:

from typing import NamedTuple
from pycoingecko import CoinGeckoAPI

cg = CoinGeckoAPI()
saved_tokens = ['bitcoin', 'ethereum']

def get_token_prices(token_ids: list[str]) -> dict[str, float]:
    tokens_list = cg.get_price(ids=",".join(token_ids), vs_currencies='usd')
    currencies = {name: prices['usd'] for name, prices in tokens_list.items()}
    return currencies

prices = get_token_prices(saved_tokens)
print(prices)


1 arrays are a specialized type that are used for storing large sequences of numerical data.

\$\endgroup\$
2
  • \$\begingroup\$ Hello! Thanks a lot for this answer. The most helpful part was about str.join ;) In this case, I'm using NamedTuple incorrectly, but now my project is larger and all function inputs and outputs use the "CryptoCurrency" class, as far as I understand, is this the right move now? As for arrays, this was of course a mistake, the list was meant and used, thanks, I read about their differences. \$\endgroup\$
    – T1mQa
    Jun 22 at 12:00
  • \$\begingroup\$ It depends on whether the NamedTuple is being used meaningfully. If it helps to clarify your code structure, then go for it. If it instead jjust acts as an unnecessary intermediate that complicates the code (as in the question's code), then I would suggest against using it. \$\endgroup\$ Jun 24 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.