Skip to content

Commit

Permalink
- Added support for Python >= 3.8
Browse files Browse the repository at this point in the history
- add Dockerfile to test Python 3.8 support
- Updated README and documentation
  • Loading branch information
BennyThadikaran committed Dec 6, 2023
1 parent 46f9904 commit fb9a112
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

An Unofficial Python Api for BSE India stock exchange.

Python version: >= 3.10
Python version: >= 3.8

## Install with PIP

Expand Down
3 changes: 1 addition & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
project = 'bse'
copyright = '2023, Benny Thadikaran'
author = 'Benny Thadikaran'
release = '2.0.0'
release = '2.0.1'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand All @@ -28,7 +28,6 @@
templates_path = ['_templates']
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Welcome to BSE's documentation!

**BSE** is an Unofficial Python Api for BSE India stock exchange

Python version: >= 3.10
Python version: >= 3.8

All network requests through BSE are rate limited or throttled. This allows making large number of requests without overloading the server or getting blocked.

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ build-backend = "hatchling.build"

[project]
name = "bse"
version = "2.0.0"
version = "2.0.1"
authors = [
{ name="Benny Thadikaran" },
]
description = "Unofficial Python Api for BSE India stock exchange"
readme = "README.md"
requires-python = ">=3.10"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
Expand Down
57 changes: 25 additions & 32 deletions src/bse/BSE.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations
from requests import Session
from requests.exceptions import ReadTimeout
from re import search
from pathlib import Path
from zipfile import ZipFile
from mthrottle import Throttle
from typing import Literal
from typing import Literal, Dict, List
from datetime import datetime

throttle_config = {
Expand All @@ -16,7 +17,6 @@
}
}


th = Throttle(throttle_config, 15)


Expand Down Expand Up @@ -80,9 +80,7 @@ def __download(self, url: str, folder: Path):
th.check()

try:
with self.session.get(url,
stream=True,
timeout=10) as r:
with self.session.get(url, stream=True, timeout=10) as r:

if r.status_code == 404:
raise RuntimeError(
Expand All @@ -98,9 +96,7 @@ def __download(self, url: str, folder: Path):

def __req(self, url, params=None, timeout=10):
try:
response = self.session.get(url,
params=params,
timeout=timeout)
response = self.session.get(url, params=params, timeout=timeout)
except ReadTimeout:
raise TimeoutError('Request timed out')

Expand All @@ -114,10 +110,7 @@ def __lookup(self, scrip):

url = f'{self.api_url}/PeerSmartSearch/w'

params = {
'Type': 'SS',
'text': scrip
}
params = {'Type': 'SS', 'text': scrip}

th.check('lookup')

Expand Down Expand Up @@ -209,7 +202,7 @@ def announcements(self,
segment: Literal['equity', 'debt', 'mf_etf'] = 'equity',
scripcode: str | None = None,
category: str = '-1',
subcategory: str = '-1') -> dict[str, list[dict]]:
subcategory: str = '-1') -> Dict[str, List[dict]]:
'''
All corporate announcements
Expand Down Expand Up @@ -303,7 +296,7 @@ def actions(self,
by_date: Literal['ex', 'record', 'bc_start'] = 'ex',
scripcode: str | None = None,
sector: str = '',
purpose_code: str | None = None) -> list[dict]:
purpose_code: str | None = None) -> List[dict]:
'''
All forthcoming corporate actions
Expand Down Expand Up @@ -381,7 +374,7 @@ def actions(self,
def resultCalendar(self,
from_date: datetime | None = None,
to_date: datetime | None = None,
scripcode: str | None = None) -> list[dict]:
scripcode: str | None = None) -> List[dict]:
'''
Corporate result calendar
Expand Down Expand Up @@ -425,7 +418,7 @@ def resultCalendar(self,

return self.__req(url, params=params).json()

def advanceDecline(self) -> list[dict]:
def advanceDecline(self) -> List[dict]:
'''
Advance decline values for all BSE indices
Expand All @@ -443,10 +436,12 @@ def advanceDecline(self) -> list[dict]:

return response.json()

def gainers(self,
by: Literal['group', 'index', 'all'] = 'group',
name: str | None = None,
pct_change: Literal['all', '10', '5', '2', '0'] = 'all') -> list[dict]:
def gainers(
self,
by: Literal['group', 'index', 'all'] = 'group',
name: str | None = None,
pct_change: Literal['all', '10', '5', '2',
'0'] = 'all') -> List[dict]:
'''
List of top gainers
Expand Down Expand Up @@ -508,10 +503,12 @@ def gainers(self,

return self.__req(url, params=params).json()['Table']

def losers(self,
by: Literal['group', 'index', 'all'] = 'group',
name: str | None = None,
pct_change: Literal['all', '10', '5', '2', '0'] = 'all') -> list[dict]:
def losers(
self,
by: Literal['group', 'index', 'all'] = 'group',
name: str | None = None,
pct_change: Literal['all', '10', '5', '2',
'0'] = 'all') -> List[dict]:
'''
List of top losers
Expand Down Expand Up @@ -577,7 +574,7 @@ def losers(self,

def near52WeekHighLow(self,
by: Literal['group', 'index', 'all'] = 'group',
name: str | None = None) -> dict[str, list[dict]]:
name: str | None = None) -> Dict[str, List[dict]]:
'''
Get stocks near 52 week highs and lows
Expand Down Expand Up @@ -645,7 +642,7 @@ def near52WeekHighLow(self,

return data

def quote(self, scripcode) -> dict[str, float]:
def quote(self, scripcode) -> Dict[str, float]:
'''
Get OHLC quotes for given scripcode
Expand Down Expand Up @@ -688,11 +685,7 @@ def quoteWeeklyHL(self, scripcode) -> dict:
:rtype: dict
'''

params = {
'Type': 'EQ',
'flag': 'C',
'scripcode': scripcode
}
params = {'Type': 'EQ', 'flag': 'C', 'scripcode': scripcode}

th.check()

Expand All @@ -717,7 +710,7 @@ def listSecurities(self,
scripcode: str = '',
group: str = 'A',
segment: str = 'Equity',
status: str = 'Active') -> list[dict]:
status: str = 'Active') -> List[dict]:
'''
List all securities and their meta info like symbol code, ISIN code, industry, market cap, segment, group etc.
Expand Down
4 changes: 1 addition & 3 deletions src/examples/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
if len(argv) == 1:
exit('Pass an stock symbol')


with BSE() as bse:
with BSE('./') as bse:
code = bse.getScripCode(argv[1])
data = bse.actions(scripcode=code)


for item in data:
sym = item['short_name']
purpose = item['Purpose']
Expand Down
8 changes: 2 additions & 6 deletions src/examples/advances.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ def adRatioFormatted(adv, dec):
return f'{s.ljust(10)}{C.ENDC} {sRatio}'.ljust(11)


broad = {
'100': '100',
'midcap': 'Midcap',
'smallcap': 'Smallcap'
}
broad = {'100': '100', 'midcap': 'Midcap', 'smallcap': 'Smallcap'}

sector = {
'auto': 'Auto',
Expand All @@ -67,7 +63,7 @@ def adRatioFormatted(adv, dec):
'utilities': 'Utilities'
}

with BSE() as bse:
with BSE('./') as bse:
data = bse.advanceDecline()

broad_out, sector_out = '', ''
Expand Down
3 changes: 1 addition & 2 deletions src/examples/get_all_announcements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from bse import BSE
import json

'''
How to use BSE.announcements get all announcements.
Expand All @@ -15,7 +14,7 @@
total_count = 1000 # arbitary number to suppress pylint warnings
page_count = 1

with BSE() as bse:
with BSE('./') as bse:
while True:
# Returns Nth page of announcements for the day upto current time.
res = bse.announcements(page_no=page_count)
Expand Down
15 changes: 15 additions & 0 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.8.18-slim-bullseye

WORKDIR /app

RUN pip install -U requests mthrottle

RUN echo 'from bse import BSE\n\
with BSE("./") as bse:\n\t\
print(bse.getScripCode("hdfcbank"))\n\
print("all working well")' > test.py

COPY bse/ bse/
COPY __init__.py .

CMD ["python3", "test.py"]

0 comments on commit fb9a112

Please sign in to comment.