-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve search and symbols_is_namespace
- search accepts namespace as an argument - search and symbols_is_namespace return DataFrame (default) or json
- Loading branch information
1 parent
babbc12
commit 0a2d493
Showing
5 changed files
with
84 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
import numpy as np | ||
import okama as ok | ||
|
||
asset_list = ok.AssetList(assets=['MSFT.US'], ccy='USD') | ||
|
||
print(asset_list.dividends_annual) | ||
|
||
|
||
x = asset_list.get_dividend_mean_growth_rate(period=20) | ||
# x.replace([np.inf, -np.inf], 0, inplace=True) | ||
|
||
print(f'Growth rate:', x) | ||
print(ok.search('aeroflot', namespace=None, response_format='frame')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
import json | ||
from typing import Optional | ||
|
||
import pandas as pd | ||
|
||
from .api_methods import API | ||
from .namespaces import symbols_in_namespace | ||
|
||
|
||
def search(search_string: str) -> json: | ||
def search(search_string: str, namespace: Optional[str] = None, response_format: str = 'frame') -> json: | ||
# search for string in a single namespace | ||
if namespace: | ||
df = symbols_in_namespace(namespace.upper()) | ||
condition1 = df['name'].str.contains(search_string, case=False) | ||
condition2 = df['ticker'].str.contains(search_string, case=False) | ||
frame_response = df[condition1 | condition2] | ||
if response_format.lower() == 'frame': | ||
return frame_response | ||
elif response_format.lower() == 'json': | ||
return frame_response.to_json(orient='records') | ||
else: | ||
raise ValueError('response_format must be "json" or "frame"') | ||
# search for string in all namespaces | ||
string_response = API.search(search_string) | ||
return json.loads(string_response) | ||
json_response = json.loads(string_response) | ||
if response_format.lower() == 'frame': | ||
df = pd.DataFrame(json_response[1:], columns=json_response[0]) | ||
return df | ||
elif response_format.lower() == 'json': | ||
return json_response | ||
else: | ||
raise ValueError('response_format must be "json" or "frame"') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Tests the search | ||
""" | ||
import json | ||
|
||
import pytest | ||
|
||
from okama.api.search import search | ||
|
||
|
||
def test_search_namespace_json(): | ||
x = search( | ||
"aeroflot", | ||
namespace="MOEX", | ||
response_format="json", | ||
) | ||
assert json.loads(x)[0]['symbol'] == 'AFLT.MOEX' | ||
|
||
|
||
def test_search_namespace_frame(): | ||
x = search( | ||
"aeroflot", | ||
namespace="MOEX", | ||
response_format="frame", | ||
) | ||
assert x['symbol'].values[0] == 'AFLT.MOEX' | ||
|
||
|
||
def test_search_all_json(): | ||
x = search( | ||
"lkoh", | ||
response_format="json", | ||
) | ||
assert x[1][0] == 'LKOH.MOEX' | ||
|
||
|
||
def test_search_all_frame(): | ||
x = search( | ||
"lkoh", | ||
response_format="frame", | ||
) | ||
assert x['symbol'].iloc[0] == 'LKOH.MOEX' | ||
|
||
|
||
def test_search_error(): | ||
with pytest.raises(ValueError): | ||
search("arg", response_format='txt') |