I’m having issues with the .upper
, .split
, and .isalpha
methods in my Python function. These methods are not working as expected. Normally, in Visual Studio Code, these methods are highlighted with a specific color when they are recognized correctly. However, this time they are not highlighted. Additionally, when I hover over these methods with my cursor, it shows “(function) upper: Any”, which indicates a problem. How can I resolve this issue in VS Code?
def crypto(crypt_input):
crypt_input = crypt_input.upper()
if len(crypt_input) == 0:
raise Exception("Missing Argument")
individual_symbols = crypt_input.split(",")
if not all(symbol.isalpha() for symbol in individual_symbols):
raise Exception("Input must be Alphabetical")
else:
symbols = ",".join(individual_symbols) #Comes in handy in cases where we want to serach for than one Coin
url = f"https://production.api.coindesk.com/v2/tb/price/ticker?assets={symbols}" # url saved
while True:
result = get(url) #http request made
if result.status_code != 200:
raise Exception(f"Failed to retrieve data:{result.status_code}")
json_data = result.json() #data from url then parsed into Json
results = []
if "data" in json_data and isinstance(json_data["data"] , dict): # makes sure key "data" is actually existant in Parsed JSOn api and is a dictionary
for symbol in individual_symbols:
symbol = symbol.upper()
try:
coin_data = json_data["data"][symbol] #Value:"BTC "of Key:"data" in this case would be another key to a sub dictionary
if coin_data is None:
print(f"No data available for symbol{symbol}")
continue
if all(key in coin_data for key in ('iso' , 'name' , 'ohlc')) and "c" in coin_data['ohlc'] and "ts" in coin_data:
results.append({
'iso':coin_data['iso'],
'name':coin_data['name'],
'Current_price':coin_data['ohlc']['c'],
"Timestamp": str(coin_data['ts'])
})
else:
print(f"Missing expected keys in coin_data: {coin_data}")
except IndexError as e:
print(f"keyerror acessing data for symbol {symbol}: {e}")
time.sleep(3)
else:
print("Invalid or missing 'data' key in JSON response")
for result in results:
print(jsonify(results))