def rank(request): if request.method == 'POST': data = json.loads(request.body) rank_methods = [ method for method in data['rank_methods'] if method['is_selected']] filter_methods = [ method for method in data['filter_methods'] if method['is_selected']] if 'limit' in data: limit = int(data['limit']) else: limit = 50 data = cache.get(hash(request.body)) if not data: ranker = Ranker( rank_methods, filter_methods, limit) data = ranker.process() cache.set( hash(request.body), data, 60*60*24) rank_results = data.to_json(orient='records') else: rank_results = json.dumps([]) return HttpResponse(rank_results, content_type='application/json')
def rank(request): if request.method == 'POST': data = json.loads(request.body) rank_methods = [ method for method in data['rank_methods'] if method['is_selected'] ] filter_methods = [ method for method in data['filter_methods'] if method['is_selected'] ] if 'limit' in data: limit = int(data['limit']) else: limit = 50 data = cache.get(hash(request.body)) if not data: ranker = Ranker(rank_methods, filter_methods, limit) data = ranker.process() cache.set(hash(request.body), data, 60 * 60 * 24) rank_results = data.to_json(orient='records') else: rank_results = json.dumps([]) return HttpResponse(rank_results, content_type='application/json')
def testRankByPEReturnsLowestFirst(self): pe_rank = { 'name': 'pe', 'min': { 'changeable': True, 'value': 5 }, 'ascending': True } ranker = Ranker([pe_rank], [], limit=50) results = ranker.process() self.assertTrue(results.code__code[0:1].values[0] == 'ARB')
def testRankByROEReturnHighestFirst(self): roe_rank = { 'name': 'roe', 'max': { 'changeable': True, 'value': 0.70 }, 'ascending': False } ranker = Ranker([roe_rank], [], limit=50) results = ranker.process() self.assertTrue(results.code__code[0:1].values[0] == 'SRX')
def testRankROEAvg5ReturnsCorrectTop(self): roe_rank = { 'name': 'roe', 'max': { 'changeable': True, 'value': 0.80, }, 'average': { 'changeable': True, 'value': 5 }, 'ascending': False } ranker = Ranker([roe_rank], [], limit=50) results = ranker.process() self.assertTrue(results.code__code[0:1].values[0] == 'ARB') self.assertTrue(results.code__code[-1:].values[0] == 'ANZ')
def testRankAndFilterRemovesTooSmallCompanies(self): market_cap_filter = { 'name': 'market_cap', 'min': { 'changeable': True, 'value': 5000000 }, } roe_rank = { 'name': 'roe', 'max': { 'changeable': True, 'value': 0.70 }, 'ascending': False } ranker = Ranker([roe_rank], [market_cap_filter], limit=50) results = ranker.process() self.assertTrue(results.code__code[0:1].values[0] == 'CBA')
def testRankROEAndPEReturnsCorrectTop(self): roe_rank = { 'name': 'roe', 'max': { 'changeable': True, 'value': 0.70 }, 'ascending': False } pe_rank = { 'name': 'pe', 'min': { 'changeable': True, 'value': 5 }, 'ascending': True } ranker = Ranker([pe_rank, roe_rank], [], limit=50) results = ranker.process() self.assertTrue(results.code__code[0:1].values[0] == 'SRX')