mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 05:21:48 +00:00
* Add separate view for finbrain for crypto curreny sentiment analysis * Move json with symbols to separate directory, little refactoring, update readme * Add screenshot of finbrain for PolkaDot * Add bitquery model and view * Move bitquery to onchain module * Bitquery added to onchain controller * Apply long number formatting for big numbers in bitquery commands * Update README with bitquery API key * Add error handling for cases when there is no token found, or dextrades response is empty. Use os library to take care of paths to files * Add helper method to extract dexTrades object from bitquery response. Add timout exception to request function. Add mapping for DECENTRALIZED_EXCHANGES to support lowercase choices: * Refactor bitquery model and views, prettify column names, remove redundant columns, change command names. Add test for api key * Update website docs for bitquery * Add difflib implementation for exchanges in bitquery ttcp command * Remove menu() from code * Remove tokens with less than 3 trades (doesn't make sense to show them, as most of them are scams). Add difflib for -coin parameter in baas command
140 lines
3.3 KiB
Python
140 lines
3.3 KiB
Python
"""Dataframe helpers"""
|
|
__docformat__ = "numpy"
|
|
|
|
import re
|
|
from typing import Union, Any
|
|
import textwrap
|
|
import pandas as pd
|
|
from gamestonk_terminal.helper_funcs import long_number_format
|
|
|
|
|
|
def wrap_text_in_df(df: pd.DataFrame, w: int = 55) -> pd.DataFrame: # pragma: no cover
|
|
"""
|
|
Parameters
|
|
----------
|
|
df: pd.DataFrame
|
|
Data Frame with some data
|
|
w: int
|
|
length of text in column after which text is wrapped into new line
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame
|
|
"""
|
|
|
|
return df.applymap(
|
|
lambda x: "\n".join(textwrap.wrap(x, width=w)) if isinstance(x, str) else x
|
|
)
|
|
|
|
|
|
def percent_to_float(s: str) -> float:
|
|
"""Helper method to replace string pct like "123.56%" to float 1.2356
|
|
Parameters
|
|
----------
|
|
s: string
|
|
string to replace
|
|
Returns
|
|
-------
|
|
float
|
|
"""
|
|
|
|
s = str(float(s.rstrip("%")))
|
|
i = s.find(".")
|
|
if i == -1:
|
|
return int(s) / 100
|
|
if s.startswith("-"):
|
|
return -percent_to_float(s.lstrip("-"))
|
|
s = s.replace(".", "")
|
|
i -= 2
|
|
if i < 0:
|
|
return float("." + "0" * abs(i) + s)
|
|
return float(s[:i] + "." + s[i:])
|
|
|
|
|
|
def create_df_index(df: pd.DataFrame, name: str = "rank") -> None:
|
|
"""Helper method that creates new index for given data frame, with provided index name
|
|
Parameters
|
|
----------
|
|
df:
|
|
pd.DataFrame
|
|
name: str
|
|
index name
|
|
"""
|
|
|
|
df.index = df.index + 1
|
|
df.reset_index(inplace=True)
|
|
df.rename(columns={"index": name}, inplace=True)
|
|
|
|
|
|
def long_number_format_with_type_check(x: Union[int, float]) -> Union[str, Any]:
|
|
"""Helper which checks if type of x is int or float and it's smaller then 10^18.
|
|
If yes it apply long_num_format
|
|
|
|
Parameters
|
|
----------
|
|
x: int/float
|
|
number to apply long_number_format method
|
|
Returns
|
|
-------
|
|
Union[str, Any]
|
|
"""
|
|
|
|
if isinstance(x, (int, float)) and x < 10 ** 18:
|
|
return long_number_format(x)
|
|
return x
|
|
|
|
|
|
def replace_underscores_in_column_names(string: str) -> str:
|
|
return string.title().replace("_", " ")
|
|
|
|
|
|
def very_long_number_formatter(num: Union[str, int, float]) -> str:
|
|
"""Apply nice string format for very big numbers like Trillions, Quadrillions, Billions etc.
|
|
|
|
Parameters
|
|
----------
|
|
num: Union[str, int, float]
|
|
number to format
|
|
Returns
|
|
-------
|
|
str:
|
|
formatted number
|
|
"""
|
|
|
|
if isinstance(num, str):
|
|
try:
|
|
num = float(num)
|
|
except (TypeError, ValueError):
|
|
return str(num)
|
|
|
|
if isinstance(num, (int, float)):
|
|
num = int(num)
|
|
magnitude = 0
|
|
while abs(num) >= 1000 and magnitude <= 3:
|
|
magnitude += 1
|
|
num /= 1000.0
|
|
num = round(num, 1)
|
|
formatted_num = f"{num:f}".rstrip("0").rstrip(".")
|
|
|
|
return f'{formatted_num}{["", "K", "M", "B", "T"][magnitude]}'
|
|
|
|
return num
|
|
|
|
|
|
def prettify_column_names(columns: list) -> list:
|
|
"""Helper method that change column names into more human readable format. E.g.
|
|
- tradeAmount => Trade amount,
|
|
- tokenValue => Token value
|
|
- mediumGasPrice => Medium Gas Price
|
|
|
|
Parameters
|
|
----------
|
|
columns: list
|
|
list of column names
|
|
|
|
Returns
|
|
-------
|
|
list with reformatted columns
|
|
"""
|
|
return [" ".join(re.findall(".[^A-Z]*", val)).capitalize() for val in columns]
|