def writeCodesToCSV(): nse = Nse() Index_Name = nse.get_index_list() # this return only name all_stock_codes = list(nse.get_stock_codes(cached=True).keys()) all_stock_name = list(nse.get_stock_codes(cached=True).values()) codes = [('SYMBOL', all_stock_codes), ('NAME', all_stock_name)] symbolDF = pd.DataFrame.from_items(codes) symbolDF['INDFLAG'] = 0 symbolDF['FO_FLAG'] = 0 symbolDF.to_csv(p.symbollist)
def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.combo = QComboBox(self) nse = Nse() quoteCodeList = nse.get_index_list() #list_1 = ['Avengers'] self.combo.move(0,60) self.combo.addItem("Select") self.combo.addItems(quoteCodeList) self.button = QPushButton("Ok", self) self.button.clicked.connect(self.save) self.button.move(0,100) self.show()
class Machine(): def __init__(self): self.nse = Nse() def getQuote(self, stockName): """ Fetches Quote for Given Stock """ return self.nse.get_quote(stockName) def showQuote(self, quote): """ Pretty Print a received Quote """ pprint(quote) def getIndexList(self): """ Fetches the available Index list """ return self.nse.get_index_list() def showIndexList(self): """ Pretty Print available Index List """ pprint(self.getIndexList()) def getIndexQuote(self, indexName): """ Fetch Index Quote """ return self.nse.get_index_quote(indexName) def showIndexQuote(self, indexName): """ Pretty Print Available Index Quote """ pprint(self.getIndexQuote(indexName))
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse) , "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): ''' should not raise any exception ''' opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = 'infy' url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { 'a': '10', 'b': '10.0', 'c': '1,000.10', 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': u'10', 'k': u'10.0', 'l': u'1,000.10' } expected_dict = { 'a': 10, 'b': 10.0, 'c': 1000.10, 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': 10, 'k': 10.0, 'l': 1000.10 } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare six.assertCountEqual(self, sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = 'inf' self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = 'infy' resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = 'infy' self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = 'in' self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {'fname':'vivek', 'lname':'jha'} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = 'CNX NIFTY' self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = 'some junk stuff' self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = 'cnx nifty' self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = 'CNX NIFTY' self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = 'wrong code' self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = 'cnx nifty' self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json)) def test_jsadptor(self): buffer = 'abc:true, def:false, ghi:NaN, jkl:none' expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None' ret = js_adaptor(buffer) self.assertEqual(ret, expected_buffer) def test_byte_adaptor(self): if six.PY2: from StringIO import StringIO buffer = 'nsetools' fbuffer = StringIO(buffer) else: from io import BytesIO buffer = b'nsetools' fbuffer = BytesIO(buffer) ret_file_buffer = byte_adaptor(fbuffer) self.assertIsInstance(ret_file_buffer, six.StringIO)
from pprint import pprint from nsetools import Nse nse = Nse() index_codes = nse.get_index_list() pprint(index_codes)
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse) , "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): ''' should not raise any exception ''' opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = 'infy' url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { 'a': '10', 'b': '10.0', 'c': '1,000.10', 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': u'10', 'k': u'10.0', 'l': u'1,000.10' } expected_dict = { 'a': 10, 'b': 10.0, 'c': 1000.10, 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': 10, 'k': 10.0, 'l': 1000.10 } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare self.assertItemsEqual(sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = 'inf' self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = 'infy' resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = 'infy' self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = 'in' self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {'fname':'vivek', 'lname':'jha'} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = 'CNX NIFTY' self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = 'some junk stuff' self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = 'cnx nifty' self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = 'CNX NIFTY' self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = 'wrong code' self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = 'cnx nifty' self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json))
from nsetools import Nse import json nse = Nse() print(nse) while True: main_input = int( input( "\nWhat You Want To Do\n\nPress 1 to Get The List Of Todays Top Gainers\n\nPress 2 to Get The List Of Todays Top Losers\n\nPress 3 to Get index Quote\n\nPress 4 to Get the Information About The Company Stock by Providing the Symbol Of The Stock\n\nPress 5 To Get the List Of Symbols of Every Stock\n\nPress 6 To Get The List Of Advances Declines\n\nPress 7 to get the list of F&O Lot Sizes: " )) index_quote_list = nse.get_index_list() top_gainers = nse.get_top_gainers() top_losers = nse.get_top_losers() all_stock_codes = nse.get_stock_codes() adv_dec = nse.get_advances_declines() lot_sizes = nse.get_fno_lot_sizes() if main_input == 1: print(json.dumps(top_gainers, sort_keys=True, indent=4)) if main_input == 2: print(json.dumps(top_losers, sort_keys=True, indent=4)) if main_input == 3: x = int( input( "\nWhich Type Of Index Quote You Want To See\nPress 1 to get the List Of Index Quote\nPress 2 to Search The Index Quote: " )) if x == 1: print(json.dumps(index_quote_list, sort_keys=True, indent=4))
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse), "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): ''' should not raise any exception ''' opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = 'infy' url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { 'a': '10', 'b': '10.0', 'c': '1,000.10', 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': u'10', 'k': u'10.0', 'l': u'1,000.10' } expected_dict = { 'a': 10, 'b': 10.0, 'c': 1000.10, 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': 10, 'k': 10.0, 'l': 1000.10 } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare six.assertCountEqual(self, sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = 'inf' self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = 'infy' resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = 'infy' self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = 'in' self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {'fname':'vivek', 'lname':'jha'} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = 'NIFTY BANK' self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = 'some junk stuff' self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = 'nifty bank' self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = 'NIFTY BANK' self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = 'wrong code' self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = 'nifty bank' self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json)) def test_jsadptor(self): buffer = 'abc:true, def:false, ghi:NaN, jkl:none' expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None' ret = js_adaptor(buffer) self.assertEqual(ret, expected_buffer) def test_byte_adaptor(self): if six.PY2: from StringIO import StringIO buffer = 'nsetools' fbuffer = StringIO(buffer) else: from io import BytesIO buffer = b'nsetools' fbuffer = BytesIO(buffer) ret_file_buffer = byte_adaptor(fbuffer) self.assertIsInstance(ret_file_buffer, six.StringIO) def test_nse_lot_sizes(self): data = self.nse.get_fno_lot_sizes() self.assertIsInstance(data, dict) def test_6th_Dec_1994(self): data = self.nse.download_bhavcopy('1994-12-06') self.assertIsInstance(self, data, bytes) def test_top_fno_gainers_losers(self): fno_gainer = self.nse.get_top_fno_gainers() self.assertIsInstance(fno_gainer, list) fno_gainer_json = self.nse.get_top_fno_gainers(as_json=True) self.assertIsInstance(fno_gainer_json, str) fno_loser = self.nse.get_top_fno_losers() self.assertIsInstance(fno_loser, list) fno_loser_json = self.nse.get_top_fno_losers(as_json=True) self.assertIsInstance(fno_loser_json, str) def test_statistics(self): active = self.nse.get_active_monthly() self.assertIsInstance(active, list) active_json = self.nse.get_active_monthly(as_json=True) self.assertIsInstance(active_json, str) yr_high = self.nse.get_year_high() self.assertIsInstance(yr_high, list) yr_high_json = self.nse.get_year_high(as_json=True) self.assertIsInstance(yr_high_json, str) yr_low = self.nse.get_year_low() self.assertIsInstance(yr_low, list) yr_low_json = self.nse.get_year_low(as_json=True) self.assertIsInstance(yr_low_json, str) preopen = self.nse.get_preopen_nifty() self.assertIsInstance(preopen, list) preopen_json = self.nse.get_preopen_nifty(as_json=True) self.assertIsInstance(preopen_json, str) preopen_nb = self.nse.get_preopen_niftybank() self.assertIsInstance(preopen_nb, list) preopen_nb_json = self.nse.get_preopen_niftybank(as_json=True) self.assertIsInstance(preopen_nb_json, str) preopen_fno = self.nse.get_preopen_fno() self.assertIsInstance(preopen_fno, list) preopen_fno_json = self.nse.get_preopen_fno(as_json=True) self.assertIsInstance(preopen_fno_json, str)
df = pd.DataFrame(volume) df = df.sort_values(by='Volume') #print(df) #print('data.mean') #print(data['Close'].mean()) meanVolume = data['Volume'].mean() #print(meanVolume) import numpy as np #print(np.median(data['Volume'])) from nsetools import Nse nse = Nse() #print(nse) q = nse.get_quote('infy') var = nse.get_index_list() print(var) from pprint import pprint pprint(q) #pprint(q['symbol']) all_stocks = nse.get_stock_codes() print(all_stocks) final_data = {'SYMBOL': 0} import time all_stocks = { '3MINDIA': '3M India Limited', 'AARTIIND': 'Aarti Industries Limited',
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse), "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): """ should not raise any exception """ opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = "infy" url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { "a": "10", "b": "10.0", "c": "1,000.10", "d": "vsjha18", "e": 10, "f": 10.0, "g": 1000.10, "h": True, "i": None, "j": u"10", "k": u"10.0", "l": u"1,000.10", } expected_dict = { "a": 10, "b": 10.0, "c": 1000.10, "d": "vsjha18", "e": 10, "f": 10.0, "g": 1000.10, "h": True, "i": None, "j": 10, "k": 10.0, "l": 1000.10, } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare six.assertCountEqual(self, sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = "inf" self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = "infy" resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = "infy" self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = "in" self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {"fname": "vivek", "lname": "jha"} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = "CNX NIFTY" self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = "some junk stuff" self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = "cnx nifty" self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = "CNX NIFTY" self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = "wrong code" self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = "cnx nifty" self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json)) def test_jsadptor(self): buffer = "abc:true, def:false, ghi:NaN, jkl:none" expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None' ret = js_adaptor(buffer) self.assertEqual(ret, expected_buffer) def test_byte_adaptor(self): if six.PY2: from StringIO import StringIO buffer = "nsetools" fbuffer = StringIO(buffer) else: from io import BytesIO buffer = b"nsetools" fbuffer = BytesIO(buffer) ret_file_buffer = byte_adaptor(fbuffer) self.assertIsInstance(ret_file_buffer, six.StringIO)
import nsepy from nsetools import Nse nse = Nse() print(nse) all_stock_codes = nse.get_stock_codes(cached=True) print(all_stock_codes) index_codes = nse.get_index_list(cached=True) top_gainers = nse.get_top_gainers() nse.is_valid_code('infy') print(index_codes)
from nsetools import Nse nse = Nse() from pprint import pprint indices = nse.get_index_list() pprint(indices)
# Importing Driver Class for National Stock Exchange from nsetools import Nse # Instantiating the driver class for National Stock Exchange nse = Nse() # Importing Pretty Print from pprint import pprint pprint('Getting Index Quote for Infosys') pprint(nse.get_quote('infy')) pprint('Getting the entire Index List') indices = nse.get_index_list() pprint(indices) pprint('Getting Index Quote for a particular index') pprint(nse.get_index_quote(indices[1])) pprint('Getting Index Quote for all indices') for x in indices: pprint(nse.get_index_quote(x)) pprint('Getting all Stock Codes') all_stocks = nse.get_stock_codes() pprint(all_stocks) pprint('Getting Quotes for each stock') for x in all_stocks: pprint(nse.get_quote(x))