Best Python code snippet using lisa_python
views.py
Source:views.py
1import datetime2from django.core.exceptions import ValidationError3from django.http import Http404, HttpResponse, HttpResponseRedirect4from django.shortcuts import render, redirect, get_object_or_4045from django.http import HttpResponseForbidden6from django.views.generic.base import TemplateView7from django.views.generic.edit import FormView8from django.views.static import serve9from django.urls import reverse10import hashlib11import os, csv, numpy12import pprint131415from honeypeartea.forms import cmaInput, ngaInput, louvreInput,rijksInput, randomInput1617PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))1819# Create your views here.20def index(request):21 return render(request, 'index.html')2223def cma(request):24 return render(request, 'cma.html')2526def nga(request):27 return render(request, 'nga.html')2829def louvre(request):30 return render(request, 'louvre.html')3132def rijksmuseum(request):33 return render(request, 'rijksmuseum.html')3435def random(request):36 return render(request, 'random.html')3738394041class cma_form(TemplateView):42 templatename = 'cma.html'4344 def csv2list(self, csvpath):45 with open(csvpath, newline='') as f:46 reader = csv.reader(f)47 header = next(reader)48 data = list(reader)49 return data5051 def get(self, request):52 form = cmaInput()53 return render(request, self. templatename, {'form': form})5455 def post(self, request):56 form = cmaInput(request.POST)57 if form.is_valid():58 dict = {}59 raw_data = []60 art_name = form.cleaned_data['art_name']61 artists = form.cleaned_data['artists']62 all = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/cleveland_with_rec.csv'))63 if artists != 'none':64 data = self.csv2list(os.path.join(PROJECT_ROOT, 'static/artist/cleveland_artists.csv'))65 for row in data:66 if row[0] == artists:67 raw_data = row68 break69 elif art_name != "":70 raw_data = []71 for row in all:72 if row[1] == art_name:73 raw_data = row[9:]74 break7576 if raw_data != []:77 dict = {78 'a': {'name': 'Art name: ' + all[int(raw_data[1])][1], 'year': 'Year: ' + all[int(raw_data[1])][2],79 'text': "Description: " + (80 all[int(raw_data[1])][8] if all[int(raw_data[1])][8] != "" else all[int(raw_data[1])][5]),81 'link': all[int(raw_data[1])][6]},82 'b': {'name': 'Art name: ' + all[int(raw_data[2])][1], 'year': 'Year: ' + all[int(raw_data[2])][2],83 'text': "Description: " + (84 all[int(raw_data[2])][8] if all[int(raw_data[2])][8] != "" else all[int(raw_data[2])][5]),85 'link': all[int(raw_data[2])][6]},86 'c': {'name': 'Art name: ' + all[int(raw_data[3])][1], 'year': 'Year: ' + all[int(raw_data[3])][2],87 'text': "Description: " + (88 all[int(raw_data[3])][8] if all[int(raw_data[3])][8] != "" else all[int(raw_data[3])][5]),89 'link': all[int(raw_data[3])][6]},90 'd': {'name': 'Art name: ' + all[int(raw_data[4])][1], 'year': 'Year: ' + all[int(raw_data[4])][2],91 'text': "Description: " + (92 all[int(raw_data[4])][8] if all[int(raw_data[4])][8] != "" else all[int(raw_data[4])][5]),93 'link': all[int(raw_data[4])][6]},94 'e': {'name': 'Art name: ' + all[int(raw_data[5])][1], 'year': 'Year: ' + all[int(raw_data[5])][2],95 'text': "Description: " + (96 all[int(raw_data[5])][8] if all[int(raw_data[5])][8] != "" else all[int(raw_data[5])][5]),97 'link': all[int(raw_data[5])][6]},98 'f': {'name': 'Art name: ' + all[int(raw_data[6])][1], 'year': 'Year: ' + all[int(raw_data[6])][2],99 'text': "Description: " + (100 all[int(raw_data[6])][8] if all[int(raw_data[6])][8] != "" else all[int(raw_data[6])][5]),101 'link': all[int(raw_data[6])][6]},102 'g': {'name': 'Art name: ' + all[int(raw_data[7])][1], 'year': 'Year: ' + all[int(raw_data[7])][2],103 'text': "Description: " + (104 all[int(raw_data[7])][8] if all[int(raw_data[7])][8] != "" else all[int(raw_data[7])][5]),105 'link': all[int(raw_data[7])][6]},106 'h': {'name': 'Art name: ' + all[int(raw_data[8])][1], 'year': 'Year: ' + all[int(raw_data[8])][2],107 'text': "Description: " + (108 all[int(raw_data[8])][8] if all[int(raw_data[8])][8] != "" else all[int(raw_data[8])][5]),109 'link': all[int(raw_data[8])][6]},110 'i': {'name': 'Art name: ' + all[int(raw_data[9])][1], 'year': 'Year: ' + all[int(raw_data[9])][2],111 'text': "Description: " + (112 all[int(raw_data[9])][8] if all[int(raw_data[9])][8] != "" else all[int(raw_data[9])][5]),113 'link': all[int(raw_data[9])][6]},114 'j': {'name': 'Art name: ' + all[int(raw_data[10])][1],115 'year': 'Year: ' + all[int(raw_data[10])][2],116 'text': "Description: " + (117 all[int(raw_data[10])][8] if all[int(raw_data[10])][8] != "" else all[int(raw_data[10])][118 5]), 'link': all[int(raw_data[10])][6]},119 }120121 if dict =={}:122 result = "Please enter valid artwork name or select artist"123 else:124 result = dict125126 args = {'form': form, 'result': result}127 return render(request, self.templatename, args)128129130class nga_form(TemplateView):131 templatename = 'nga.html'132133 def csv2list(self, csvpath):134 with open(csvpath, newline='') as f:135 reader = csv.reader(f)136 header = next(reader)137 data = list(reader)138 return data139140 def get(self, request):141 form = ngaInput()142 return render(request, self. templatename, {'form': form})143144 def post(self, request):145 form = ngaInput(request.POST)146 if form.is_valid():147 dict = {}148 raw_data = []149 art_name = form.cleaned_data['art_name']150 artists = form.cleaned_data['artists']151 all = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/nga_with_rec.csv'))152 if artists != 'none':153 data = self.csv2list(os.path.join(PROJECT_ROOT, 'static/artist/nga_artists.csv'))154 for row in data:155 if row[0] == artists:156 raw_data = row157 break158 elif art_name != "":159 raw_data = []160 for row in all:161 if row[1] == art_name:162 raw_data = row[10:]163 break164165 if raw_data != []:166 dict = {167 'a': {'name': 'Art name: ' + all[int(raw_data[1])][1], 'year': 'Year: ' + all[int(raw_data[1])][2],168 'text': "Description: " + (169 all[int(raw_data[1])][10] if all[int(raw_data[1])][10] != "" else all[int(raw_data[1])][8]),170 'link': all[int(raw_data[1])][9]},171 'b': {'name': 'Art name: ' + all[int(raw_data[2])][1], 'year': 'Year: ' + all[int(raw_data[2])][2],172 'text': "Description: " + (173 all[int(raw_data[2])][10] if all[int(raw_data[2])][10] != "" else all[int(raw_data[2])][8]),174 'link': all[int(raw_data[2])][9]},175 'c': {'name': 'Art name: ' + all[int(raw_data[3])][1], 'year': 'Year: ' + all[int(raw_data[3])][2],176 'text': "Description: " + (177 all[int(raw_data[3])][10] if all[int(raw_data[3])][10] != "" else all[int(raw_data[3])][8]),178 'link': all[int(raw_data[3])][9]},179 'd': {'name': 'Art name: ' + all[int(raw_data[4])][1], 'year': 'Year: ' + all[int(raw_data[4])][2],180 'text': "Description: " + (181 all[int(raw_data[4])][10] if all[int(raw_data[4])][10] != "" else all[int(raw_data[4])][8]),182 'link': all[int(raw_data[4])][9]},183 'e': {'name': 'Art name: ' + all[int(raw_data[5])][1], 'year': 'Year: ' + all[int(raw_data[5])][2],184 'text': "Description: " + (185 all[int(raw_data[5])][10] if all[int(raw_data[5])][10] != "" else all[int(raw_data[5])][8]),186 'link': all[int(raw_data[5])][9]},187 'f': {'name': 'Art name: ' + all[int(raw_data[6])][1], 'year': 'Year: ' + all[int(raw_data[6])][2],188 'text': "Description: " + (189 all[int(raw_data[6])][10] if all[int(raw_data[6])][10] != "" else all[int(raw_data[6])][8]),190 'link': all[int(raw_data[6])][9]},191 'g': {'name': 'Art name: ' + all[int(raw_data[7])][1], 'year': 'Year: ' + all[int(raw_data[7])][2],192 'text': "Description: " + (193 all[int(raw_data[7])][10] if all[int(raw_data[7])][10] != "" else all[int(raw_data[7])][8]),194 'link': all[int(raw_data[7])][9]},195 'h': {'name': 'Art name: ' + all[int(raw_data[8])][1], 'year': 'Year: ' + all[int(raw_data[8])][2],196 'text': "Description: " + (197 all[int(raw_data[8])][10] if all[int(raw_data[8])][10] != "" else all[int(raw_data[8])][8]),198 'link': all[int(raw_data[8])][9]},199 'i': {'name': 'Art name: ' + all[int(raw_data[9])][1], 'year': 'Year: ' + all[int(raw_data[9])][2],200 'text': "Description: " + (201 all[int(raw_data[9])][10] if all[int(raw_data[9])][10] != "" else all[int(raw_data[9])][8]),202 'link': all[int(raw_data[9])][9]},203 'j': {'name': 'Art name: ' + all[int(raw_data[10])][1],204 'year': 'Year: ' + all[int(raw_data[10])][2],205 'text': "Description: " + (206 all[int(raw_data[10])][10] if all[int(raw_data[10])][10] != "" else all[int(raw_data[10])][207 8]), 'link': all[int(raw_data[10])][9]},208 }209210 if dict =={}:211 result = "Please enter valid artwork name or select artist"212 else:213 result = dict214215 args = {'form': form, 'result': result}216 return render(request, self.templatename, args)217218219class louvre_form(TemplateView):220 templatename = 'louvre.html'221222 def csv2list(self, csvpath):223 with open(csvpath, newline='') as f:224 reader = csv.reader(f)225 header = next(reader)226 data = list(reader)227 return data228229 def get(self, request):230 form = louvreInput()231 return render(request, self. templatename, {'form': form})232233 def post(self, request):234 form = louvreInput(request.POST)235 if form.is_valid():236 dict = {}237 raw_data = []238 art_name = form.cleaned_data['art_name']239 artists = form.cleaned_data['artists']240 all = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/louvre_with_rec.csv'))241 if artists != 'none':242 data = self.csv2list(os.path.join(PROJECT_ROOT, 'static/artist/louvre_artists.csv'))243 for row in data:244 if row[0] == artists:245 raw_data = row246 break247 elif art_name != "":248 raw_data = []249 for row in all:250 if row[1] == art_name:251 raw_data = row[11:]252 break253254 if raw_data != []:255 dict = {256 'a': {'name': 'Art name: ' + all[int(raw_data[1])][1], 'year': 'Year: ' + all[int(raw_data[1])][3],257 'text': "Description: " + (258 all[int(raw_data[1])][11] if all[int(raw_data[1])][11] != "" else all[int(raw_data[1])][6]),259 'link': all[int(raw_data[1])][5]},260 'b': {'name': 'Art name: ' + all[int(raw_data[2])][1], 'year': 'Year: ' + all[int(raw_data[2])][3],261 'text': "Description: " + (262 all[int(raw_data[2])][11] if all[int(raw_data[2])][11] != "" else all[int(raw_data[2])][6]),263 'link': all[int(raw_data[2])][5]},264 'c': {'name': 'Art name: ' + all[int(raw_data[3])][1], 'year': 'Year: ' + all[int(raw_data[3])][3],265 'text': "Description: " + (266 all[int(raw_data[3])][11] if all[int(raw_data[3])][11] != "" else all[int(raw_data[3])][6]),267 'link': all[int(raw_data[3])][5]},268 'd': {'name': 'Art name: ' + all[int(raw_data[4])][1], 'year': 'Year: ' + all[int(raw_data[4])][3],269 'text': "Description: " + (270 all[int(raw_data[4])][11] if all[int(raw_data[4])][11] != "" else all[int(raw_data[4])][6]),271 'link': all[int(raw_data[4])][5]},272 'e': {'name': 'Art name: ' + all[int(raw_data[5])][1], 'year': 'Year: ' + all[int(raw_data[5])][3],273 'text': "Description: " + (274 all[int(raw_data[5])][11] if all[int(raw_data[5])][11] != "" else all[int(raw_data[5])][6]),275 'link': all[int(raw_data[5])][5]},276 'f': {'name': 'Art name: ' + all[int(raw_data[6])][1], 'year': 'Year: ' + all[int(raw_data[6])][3],277 'text': "Description: " + (278 all[int(raw_data[6])][11] if all[int(raw_data[6])][11] != "" else all[int(raw_data[6])][6]),279 'link': all[int(raw_data[6])][5]},280 'g': {'name': 'Art name: ' + all[int(raw_data[7])][1], 'year': 'Year: ' + all[int(raw_data[7])][3],281 'text': "Description: " + (282 all[int(raw_data[7])][11] if all[int(raw_data[7])][11] != "" else all[int(raw_data[7])][6]),283 'link': all[int(raw_data[7])][5]},284 'h': {'name': 'Art name: ' + all[int(raw_data[8])][1], 'year': 'Year: ' + all[int(raw_data[8])][3],285 'text': "Description: " + (286 all[int(raw_data[8])][11] if all[int(raw_data[8])][11] != "" else all[int(raw_data[8])][6]),287 'link': all[int(raw_data[8])][5]},288 'i': {'name': 'Art name: ' + all[int(raw_data[9])][1], 'year': 'Year: ' + all[int(raw_data[9])][3],289 'text': "Description: " + (290 all[int(raw_data[9])][11] if all[int(raw_data[9])][11] != "" else all[int(raw_data[9])][6]),291 'link': all[int(raw_data[9])][5]},292 'j': {'name': 'Art name: ' + all[int(raw_data[10])][1],293 'year': 'Year: ' + all[int(raw_data[10])][3],294 'text': "Description: " + (295 all[int(raw_data[10])][11] if all[int(raw_data[10])][11] != "" else all[int(raw_data[10])][296 6]), 'link': all[int(raw_data[10])][5]},297 }298299 if dict =={}:300 result = "Please enter valid artwork name or select artist"301 else:302 result = dict303304 args = {'form': form, 'result': result}305 return render(request, self.templatename, args)306307308class rijks_form(TemplateView):309 templatename = 'rijksmuseum.html'310311 def csv2list(self, csvpath):312 with open(csvpath, newline='') as f:313 reader = csv.reader(f)314 header = next(reader)315 data = list(reader)316 return data317318 def get(self, request):319 form = rijksInput()320 return render(request, self. templatename, {'form': form})321322 def post(self, request):323 form = rijksInput(request.POST)324 if form.is_valid():325 dict = {}326 raw_data = []327 art_name = form.cleaned_data['art_name']328 artists = form.cleaned_data['artists']329 all = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/rijks_with_rec.csv'))330 if artists != 'none':331 data = self.csv2list(os.path.join(PROJECT_ROOT, 'static/artist/rijks_artists.csv'))332 for row in data:333 if row[0] == artists:334 raw_data = row335 break336 elif art_name != "":337 raw_data = []338 for row in all:339 if row[1] == art_name:340 raw_data = row[10:21]341 break342343 if raw_data != []:344345 dict = {346 'a': {'name': 'Art name: ' + all[int(raw_data[1])][1], 'year': 'Year: ' + all[int(raw_data[1])][3],347 'text': "Description: " + (348 all[int(raw_data[1])][9] if all[int(raw_data[1])][9] != "" else all[int(raw_data[1])][5]),349 'link': all[int(raw_data[1])][-1]},350 'b': {'name': 'Art name: ' + all[int(raw_data[2])][1], 'year': 'Year: ' + all[int(raw_data[2])][3],351 'text': "Description: " + (352 all[int(raw_data[2])][9] if all[int(raw_data[2])][9] != "" else all[int(raw_data[2])][5]),353 'link': all[int(raw_data[2])][-1]},354 'c': {'name': 'Art name: ' + all[int(raw_data[3])][1], 'year': 'Year: ' + all[int(raw_data[3])][3],355 'text': "Description: " + (356 all[int(raw_data[3])][9] if all[int(raw_data[3])][9] != "" else all[int(raw_data[3])][5]),357 'link': all[int(raw_data[3])][-1]},358 'd': {'name': 'Art name: ' + all[int(raw_data[4])][1], 'year': 'Year: ' + all[int(raw_data[4])][3],359 'text': "Description: " + (360 all[int(raw_data[4])][9] if all[int(raw_data[4])][9] != "" else all[int(raw_data[4])][5]),361 'link': all[int(raw_data[4])][-1]},362 'e': {'name': 'Art name: ' + all[int(raw_data[5])][1], 'year': 'Year: ' + all[int(raw_data[5])][3],363 'text': "Description: " + (364 all[int(raw_data[5])][9] if all[int(raw_data[5])][9] != "" else all[int(raw_data[5])][5]),365 'link': all[int(raw_data[5])][-1]},366 'f': {'name': 'Art name: ' + all[int(raw_data[6])][1], 'year': 'Year: ' + all[int(raw_data[6])][3],367 'text': "Description: " + (368 all[int(raw_data[6])][9] if all[int(raw_data[6])][9] != "" else all[int(raw_data[6])][5]),369 'link': all[int(raw_data[6])][-1]},370 'g': {'name': 'Art name: ' + all[int(raw_data[7])][1], 'year': 'Year: ' + all[int(raw_data[7])][3],371 'text': "Description: " + (372 all[int(raw_data[7])][9] if all[int(raw_data[7])][9] != "" else all[int(raw_data[7])][5]),373 'link': all[int(raw_data[7])][-1]},374 'h': {'name': 'Art name: ' + all[int(raw_data[8])][1], 'year': 'Year: ' + all[int(raw_data[8])][3],375 'text': "Description: " + (376 all[int(raw_data[8])][9] if all[int(raw_data[8])][9] != "" else all[int(raw_data[8])][5]),377 'link': all[int(raw_data[8])][-1]},378 'i': {'name': 'Art name: ' + all[int(raw_data[9])][1], 'year': 'Year: ' + all[int(raw_data[9])][3],379 'text': "Description: " + (380 all[int(raw_data[9])][9] if all[int(raw_data[9])][9] != "" else all[int(raw_data[9])][5]),381 'link': all[int(raw_data[9])][-1]},382 'j': {'name': 'Art name: ' + all[int(raw_data[10])][1],383 'year': 'Year: ' + all[int(raw_data[10])][3],384 'text': "Description: " + (385 all[int(raw_data[10])][9] if all[int(raw_data[10])][9] != "" else all[int(raw_data[10])][386 5]), 'link': all[int(raw_data[10])][-1]},387 }388389 if dict =={}:390 result = "Please enter valid artwork name or select artist"391 else:392 result = dict393394 args = {'form': form, 'result': result}395 return render(request, self.templatename, args)396397class random_form(TemplateView):398 templatename = 'random.html'399400 def csv2list(self, csvpath):401 with open(csvpath, newline='') as f:402 reader = csv.reader(f)403 header = next(reader)404 data = list(reader)405 return data406407 def get(self, request):408 form = randomInput()409 return render(request, self. templatename, {'form': form})410411 def post(self, request):412 form = randomInput(request.POST)413 if form.is_valid():414 dict = {}415 raw_data = []416 text = form.cleaned_data['text']417 color = form.cleaned_data['color']418 cma = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/cleveland_with_rec.csv'))419 nga = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/nga_with_rec.csv'))420 louvre = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/louvre_with_rec.csv'))421 rijks = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/rijks_with_rec.csv'))422423 if text != "":424 raw_data = numpy.random.randint(low = 0, high = len(cma), size = 11)425 raw_data = [int(i) for i in raw_data]426 if raw_data != []:427 dict = {428 'a': {'name': 'Art name: ' + cma[int(raw_data[1])][1],429 'year': 'Year: ' + cma[int(raw_data[1])][2],430 'text': "Description: " + (431 cma[int(raw_data[1])][8] if cma[int(raw_data[1])][8] != "" else cma[int(raw_data[1])][432 5]),433 'link': cma[int(raw_data[1])][6]},434 'b': {'name': 'Art name: ' + cma[int(raw_data[2])][1],435 'year': 'Year: ' + cma[int(raw_data[2])][2],436 'text': "Description: " + (437 cma[int(raw_data[2])][8] if cma[int(raw_data[2])][8] != "" else cma[int(raw_data[2])][438 5]),439 'link': cma[int(raw_data[2])][6]},440 'c': {'name': 'Art name: ' + nga[int(raw_data[3])][1],441 'year': 'Year: ' + nga[int(raw_data[3])][2],442 'text': "Description: " + (443 nga[int(raw_data[3])][10] if nga[int(raw_data[3])][10] != "" else444 nga[int(raw_data[3])][8]),445 'link': nga[int(raw_data[3])][9]},446 'd': {'name': 'Art name: ' + nga[int(raw_data[4])][1],447 'year': 'Year: ' + nga[int(raw_data[4])][2],448 'text': "Description: " + (449 nga[int(raw_data[4])][10] if nga[int(raw_data[4])][10] != "" else450 nga[int(raw_data[4])][8]),451 'link': nga[int(raw_data[4])][9]},452 'e': {'name': 'Art name: ' + louvre[int(raw_data[5])][1],453 'year': 'Year: ' + louvre[int(raw_data[5])][3],454 'text': "Description: " + (455 louvre[int(raw_data[5])][11] if louvre[int(raw_data[5])][11] != "" else456 louvre[int(raw_data[5])][457 6]),458 'link': louvre[int(raw_data[5])][5]},459 'f': {'name': 'Art name: ' + louvre[int(raw_data[6])][1],460 'year': 'Year: ' + louvre[int(raw_data[6])][3],461 'text': "Description: " + (462 louvre[int(raw_data[6])][11] if louvre[int(raw_data[6])][11] != "" else463 louvre[int(raw_data[6])][464 6]),465 'link': louvre[int(raw_data[6])][5]},466 'g': {'name': 'Art name: ' + louvre[int(raw_data[7])][1],467 'year': 'Year: ' + louvre[int(raw_data[7])][3],468 'text': "Description: " + (469 louvre[int(raw_data[7])][11] if louvre[int(raw_data[7])][11] != "" else470 louvre[int(raw_data[7])][471 6]),472 'link': louvre[int(raw_data[7])][5]},473 'h': {'name': 'Art name: ' + louvre[int(raw_data[8])][1],474 'year': 'Year: ' + louvre[int(raw_data[8])][3],475 'text': "Description: " + (476 louvre[int(raw_data[8])][11] if louvre[int(raw_data[8])][11] != "" else477 louvre[int(raw_data[8])][478 6]),479 'link': louvre[int(raw_data[8])][5]},480 'i': {'name': 'Art name: ' + rijks[int(raw_data[9])][1],481 'year': 'Year: ' + rijks[int(raw_data[9])][3],482 'text': "Description: " + (483 rijks[int(raw_data[9])][9] if rijks[int(raw_data[9])][9] != "" else484 rijks[int(raw_data[9])][5]),485 'link': rijks[int(raw_data[9])][-1]},486 'j': {'name': 'Art name: ' + rijks[int(raw_data[10])][1],487 'year': 'Year: ' + rijks[int(raw_data[10])][3],488 'text': "Description: " + (489 rijks[int(raw_data[10])][9] if rijks[int(raw_data[10])][9] != "" else490 rijks[int(raw_data[10])][491 5]), 'link': rijks[int(raw_data[10])][-1]},492 }493 elif color != 'none':494 color_nga = self.csv2list(os.path.join(PROJECT_ROOT, 'static/resource/color_nga.csv'))495 if color == 'red':496 raw_data = color_nga[0]497 elif color == 'blue':498 raw_data = color_nga[1]499 elif color == 'green':500 raw_data = color_nga[2]501 if raw_data != []:502 all = nga503 dict = {504 'a': {'name': 'Art name: ' + all[int(raw_data[1])][1],505 'year': 'Year: ' + all[int(raw_data[1])][2],506 'text': "Description: " + (507 all[int(raw_data[1])][10] if all[int(raw_data[1])][10] != "" else508 all[int(raw_data[1])][8]),509 'link': all[int(raw_data[1])][9]},510 'b': {'name': 'Art name: ' + all[int(raw_data[2])][1],511 'year': 'Year: ' + all[int(raw_data[2])][2],512 'text': "Description: " + (513 all[int(raw_data[2])][10] if all[int(raw_data[2])][10] != "" else514 all[int(raw_data[2])][8]),515 'link': all[int(raw_data[2])][9]},516 'c': {'name': 'Art name: ' + all[int(raw_data[3])][1],517 'year': 'Year: ' + all[int(raw_data[3])][2],518 'text': "Description: " + (519 all[int(raw_data[3])][10] if all[int(raw_data[3])][10] != "" else520 all[int(raw_data[3])][8]),521 'link': all[int(raw_data[3])][9]},522 'd': {'name': 'Art name: ' + all[int(raw_data[4])][1],523 'year': 'Year: ' + all[int(raw_data[4])][2],524 'text': "Description: " + (525 all[int(raw_data[4])][10] if all[int(raw_data[4])][10] != "" else526 all[int(raw_data[4])][8]),527 'link': all[int(raw_data[4])][9]},528 'e': {'name': 'Art name: ' + all[int(raw_data[5])][1],529 'year': 'Year: ' + all[int(raw_data[5])][2],530 'text': "Description: " + (531 all[int(raw_data[5])][10] if all[int(raw_data[5])][10] != "" else532 all[int(raw_data[5])][8]),533 'link': all[int(raw_data[5])][9]},534 'f': {'name': 'Art name: ' + all[int(raw_data[6])][1],535 'year': 'Year: ' + all[int(raw_data[6])][2],536 'text': "Description: " + (537 all[int(raw_data[6])][10] if all[int(raw_data[6])][10] != "" else538 all[int(raw_data[6])][8]),539 'link': all[int(raw_data[6])][9]},540 'g': {'name': 'Art name: ' + all[int(raw_data[7])][1],541 'year': 'Year: ' + all[int(raw_data[7])][2],542 'text': "Description: " + (543 all[int(raw_data[7])][10] if all[int(raw_data[7])][10] != "" else544 all[int(raw_data[7])][8]),545 'link': all[int(raw_data[7])][9]},546 'h': {'name': 'Art name: ' + all[int(raw_data[8])][1],547 'year': 'Year: ' + all[int(raw_data[8])][2],548 'text': "Description: " + (549 all[int(raw_data[8])][10] if all[int(raw_data[8])][10] != "" else550 all[int(raw_data[8])][8]),551 'link': all[int(raw_data[8])][9]},552 'i': {'name': 'Art name: ' + all[int(raw_data[9])][1],553 'year': 'Year: ' + all[int(raw_data[9])][2],554 'text': "Description: " + (555 all[int(raw_data[9])][10] if all[int(raw_data[9])][10] != "" else556 all[int(raw_data[9])][8]),557 'link': all[int(raw_data[9])][9]},558 'j': {'name': 'Art name: ' + all[int(raw_data[10])][1],559 'year': 'Year: ' + all[int(raw_data[10])][2],560 'text': "Description: " + (561 all[int(raw_data[10])][10] if all[int(raw_data[10])][10] != "" else562 all[int(raw_data[10])][563 8]), 'link': all[int(raw_data[10])][9]},564 }565566567568569570 if dict =={}:571 result = "Please enter valid text or select color"572 else:573 result = dict574575 args = {'form': form, 'result': result}
...
capping.py
Source:capping.py
1# Takes in the total market cap as a float and a dictionary of dictionaries2# with at least the attributes mentioned below, if more attributes are passed3# in, they are ignored and returned as is, if there were calculations based on4# any of the attributes mentioned below prior to passing them in, they will5# need to be updated.6#7# There should be an entry with the following structure for each market in the8# index (regardless of the need for capping):9#10# sample = {index/symbol1: {'market_cap': float,11# 'price': float,12# 'circulating_supply': int }, (repeat x times)}13INITIAL_INVESTMENT = 100000014class RussellCapping:15 def __init__(self, total_mc=None, raw_data=None, capping_percent=None):16 self.initial_total_mc = 702863579317 self.raw_data = {'1': {'symbol': 'btc', 'name': 'bitcoin', 'market_cap': 6487950808, 'price': 431.45, 'circulating supply': 15037467, '24hr volume': '28221886', 'percent_mc': 0.9230739789450348, 'weighted_dollar_mc': 923073.9789450348, 'num_coins': 2139.4691828602035}, '2': {'symbol': 'xrp', 'name': 'ripple', 'market_cap': 202989110, 'price': 0.006053, 'circulating supply': 33537439933, '24hr volume': '273625', 'percent_mc': 0.028880299958373445, 'weighted_dollar_mc': 28880.299958373445, 'num_coins': 4771237.396063678}, '3': {'symbol': 'ltc', 'name': 'litecoin', 'market_cap': 153184845, 'price': 3.49, 'circulating supply': 43878995, '24hr volume': '1948999', 'percent_mc': 0.021794392185260296, 'weighted_dollar_mc': 21794.392185260294, 'num_coins': 6244.81151440123}, '4': {'symbol': 'eth', 'name': 'ethereum', 'market_cap': 72409494, 'price': 0.953277, 'circulating supply': 75958536, '24hr volume': '241462', 'percent_mc': 0.010302069438868135, 'weighted_dollar_mc': 10302.069438868135, 'num_coins': 10807.005140025549}, '5': {'symbol': 'dash', 'name': 'dash', 'market_cap': 19820453, 'price': 3.25, 'circulating supply': 6107434, '24hr volume': '55320', 'percent_mc': 0.002819957326532654, 'weighted_dollar_mc': 2819.957326532654, 'num_coins': 867.6791773946627}, '6': {'symbol': 'doge', 'name': 'dogecoin', 'market_cap': 15091783, 'price': 0.000147, 'circulating supply': 102497368315, '24hr volume': '105352', 'percent_mc': 0.0021471852354379064, 'weighted_dollar_mc': 2147.1852354379066, 'num_coins': 14606702.28189052}, '7': {'symbol': 'ppc', 'name': 'peercoin', 'market_cap': 9788940, 'price': 0.427738, 'circulating supply': 22885358, '24hr volume': '28242', 'percent_mc': 0.001392722611939725, 'weighted_dollar_mc': 1392.7226119397249, 'num_coins': 3256.01796412693}, '8': {'symbol': 'bts', 'name': 'bitshares', 'market_cap': 8687533, 'price': 0.003424, 'circulating supply': 2537052805, '24hr volume': '9096', 'percent_mc': 0.0012360197989846249, 'weighted_dollar_mc': 1236.019798984625, 'num_coins': 360987.09082494886}, '9': {'symbol': 'xlm', 'name': 'stellar', 'market_cap': 8386835, 'price': 0.001734, 'circulating supply': 4837356606, '24hr volume': '42572', 'percent_mc': 0.0011932379549887428, 'weighted_dollar_mc': 1193.2379549887428, 'num_coins': 688141.8425540616}, '10': {'symbol': 'nxt', 'name': 'nxt', 'market_cap': 7329199, 'price': 0.007329, 'circulating supply': 999997096, '24hr volume': '11515', 'percent_mc': 0.0010427626663056491, 'weighted_dollar_mc': 1042.7626663056492, 'num_coins': 142278.98298617126}, '11': {'symbol': 'maid', 'name': 'maidsafecoin', 'market_cap': 6560545, 'price': 0.014497, 'circulating supply': 452552412, '24hr volume': '2090', 'percent_mc': 0.0009334023263139935, 'weighted_dollar_mc': 933.4023263139935, 'num_coins': 64385.895448299205}, '12': {'symbol': 'fct', 'name': 'factom', 'market_cap': 5666723, 'price': 0.647339, 'circulating supply': 8753873, '24hr volume': '410320', 'percent_mc': 0.0008062336941179447, 'weighted_dollar_mc': 806.2336941179448, 'num_coins': 1245.4582438535988}, '13': {'symbol': 'nmc', 'name': 'namecoin', 'market_cap': 5581503, 'price': 0.420126, 'circulating supply': 13285307, '24hr volume': '44570', 'percent_mc': 0.0007941090084022796, 'weighted_dollar_mc': 794.1090084022796, 'num_coins': 1890.1686836860363}, '14': {'symbol': 'bcn', 'name': 'bytecoin', 'market_cap': 5532261, 'price': 3.1e-05, 'circulating supply': 178306134649, '24hr volume': '8936', 'percent_mc': 0.0007871030969494424, 'weighted_dollar_mc': 787.1030969494425, 'num_coins': 25390422.48224008}, '15': {'symbol': 'xmr', 'name': 'monero', 'market_cap': 5516579, 'price': 0.521568, 'circulating supply': 10576902, '24hr volume': '57544', 'percent_mc': 0.0007848719385195778, 'weighted_dollar_mc': 784.8719385195778, 'num_coins': 1504.831466883662}, '16': {'symbol': 'grc', 'name': 'gridcoin', 'market_cap': 3261647, 'price': 0.008778, 'circulating supply': 371581656, '24hr volume': '755', 'percent_mc': 0.0004640512179117829, 'weighted_dollar_mc': 464.0512179117829, 'num_coins': 52865.25608473262}, '17': {'symbol': 'nsr', 'name': 'nushares', 'market_cap': 3144968, 'price': 0.003773, 'circulating supply': 833502552, '24hr volume': 'Low Vol', 'percent_mc': 0.0004474506992000005, 'weighted_dollar_mc': 447.4506992000005, 'num_coins': 118592.81717466221}, '18': {'symbol': 'emc', 'name': 'emercoin', 'market_cap': 2886544, 'price': 0.078433, 'circulating supply': 36802553, '24hr volume': '4231', 'percent_mc': 0.0004106833936216732, 'weighted_dollar_mc': 410.6833936216732, 'num_coins': 5236.104619505479}, '19': {'symbol': 'rby', 'name': 'rubycoin', 'market_cap': 2830918, 'price': 0.125475, 'circulating supply': 22561620, '24hr volume': '3158', 'percent_mc': 0.0004027691978035602, 'weighted_dollar_mc': 402.7691978035602, 'num_coins': 3209.9557505762914}, '20': {'symbol': 'blk', 'name': 'blackcoin', 'market_cap': 2015105, 'price': 0.026803, 'circulating supply': 75182257, '24hr volume': '4367', 'percent_mc': 0.000286699305433765, 'weighted_dollar_mc': 286.699305433765, 'num_coins': 10696.537903733351}}18 self.temp_total_mc = 702863579319 # self.temp_raw_data = raw_data20 self.total_weighted_mc = 021 self.capping_percent = 1022 for crypto in self.raw_data:23 self.raw_data[crypto]['percent_mc'] = self.raw_data[crypto]['market_cap'] / self.temp_total_mc24 self.raw_data[crypto]['capped'] = False25 def russell_capping(self):26 to_cap = self.determine_constituents_to_cap()27 if to_cap is None:28 return self.raw_data.copy(), self.temp_total_mc29 uncapped_percent, uncapped_mc_sum = self.uncapped_info()30 # print('here', to_cap)31 for crypto in to_cap:32 capping_factor = self.calculate_capping_factor(crypto, uncapped_percent, uncapped_mc_sum)33 self.temp_total_mc -= self.raw_data[crypto]['market_cap']34 self.raw_data[crypto]['market_cap'] *= capping_factor35 self.temp_total_mc += self.raw_data[crypto]['market_cap']36 self.raw_data[crypto]['percent_mc'] = self.raw_data[crypto]['market_cap'] / self.temp_total_mc37 self.raw_data[crypto]['weighted_dollar_mc'] = self.raw_data[crypto]['percent_mc'] * INITIAL_INVESTMENT38 self.raw_data[crypto]['num_coins'] = self.raw_data[crypto]['weighted_dollar_mc'] / self.raw_data[crypto]['price']39 self.raw_data[crypto]['capped'] = True40 while True:41 uncapped_percent, uncapped_mc_sum, to_cap = self.recalculate_caps()42 if not to_cap:43 break44 # return self.raw_data.copy(), self.temp_total_mc45 # print('here2', to_cap)46 for crypto in to_cap:47 capping_factor = self.calculate_capping_factor(crypto, uncapped_percent, uncapped_mc_sum)48 self.temp_total_mc -= self.raw_data[crypto]['market_cap']49 self.raw_data[crypto]['market_cap'] *= capping_factor50 self.temp_total_mc += self.raw_data[crypto]['market_cap']51 self.raw_data[crypto]['percent_mc'] = self.raw_data[crypto]['market_cap'] / self.temp_total_mc52 self.raw_data[crypto]['weighted_dollar_mc'] = self.raw_data[crypto]['percent_mc'] * INITIAL_INVESTMENT53 self.raw_data[crypto]['num_coins'] = self.raw_data[crypto]['weighted_dollar_mc'] / self.raw_data[crypto]['price']54 self.raw_data[crypto]['capped'] = True55 for crypto in self.raw_data:56 self.raw_data[crypto]['weighted_dollar_mc'] = self.raw_data[crypto]['percent_mc'] * INITIAL_INVESTMENT57 self.raw_data[crypto]['num_coins'] = self.raw_data[crypto]['weighted_dollar_mc'] / self.raw_data[crypto]['price']58 self.total_weighted_mc += self.raw_data[crypto]['weighted_dollar_mc']59 return self.raw_data, self.temp_total_mc, self.total_weighted_mc60 def recalculate_caps(self):61 uncapped_percent = 062 uncapped_crypto_mc_sum = 063 to_cap = set()64 for crypto in self.raw_data:65 self.raw_data[crypto]['percent_mc'] = self.raw_data[crypto]['market_cap'] / self.temp_total_mc66 if self.raw_data[crypto]['percent_mc'] > 0.1:67 uncapped_percent += self.raw_data[crypto]['percent_mc']68 uncapped_crypto_mc_sum += self.raw_data[crypto]['market_cap']69 to_cap.add(crypto)70 if len(to_cap) == 0:71 return False, False, False72 return (uncapped_percent * 100), uncapped_crypto_mc_sum, to_cap73 def check_uncapped(self):74 pass75 def calculate_capping_factor(self, index, uncapped_percent, uncapped_mc_sum):76 capping_factor = ((self.capping_percent /77 (self.raw_data[index]['market_cap'] * uncapped_percent))78 * uncapped_mc_sum)79 # print(index, capping_factor)80 return capping_factor81 def determine_constituents_to_cap(self):82 to_cap = set()83 for crypto in self.raw_data:84 if self.raw_data[crypto]['percent_mc'] > 0.10:85 to_cap.add(crypto)86 if len(to_cap) == 0:87 return None88 return to_cap.copy()89 def uncapped_info(self):90 # Returns the percent and market cap sum of all the uncapped cryptos91 uncapped_percent = 092 uncapped_crypto_mc_sum = 093 for x in self.raw_data:94 if not self.raw_data[x]['capped']:95 uncapped_percent += self.raw_data[x]['percent_mc']96 uncapped_crypto_mc_sum += self.raw_data[x]['market_cap']97 return (uncapped_percent * 100), uncapped_crypto_mc_sum98 def reset_capped_attribute(self):99 for crypto in self.raw_data:...
model.py
Source:model.py
1import os2from pathlib import Path3import pandas as pd4import numpy as np5from sklearn.linear_model import LinearRegression6from sklearn.model_selection import train_test_split7from sklearn.preprocessing import StandardScaler8from sklearn.metrics import mean_absolute_error, r2_score9import joblib10class Model():11 """12 A class that manage the model. 13 """14 def __init__(self) -> None:15 """16 A function that initialize the class.17 """18 self.df = pd.read_csv("data/raw_data.csv")19 self.model_name:str = "model.pkl"20 self.model_column_name:str = "model_columns.pkl"21 self.poly_feature_name:str = "poly_features.pkl"22 self.model_path:str = Path("./model/").joinpath(self.model_name)23 self.poly_features_model_path:str = Path("./model/").joinpath(self.poly_feature_name)24 self.model_column_path:str = Path("./model/").joinpath(self.model_column_name)25 26 def check_model_not_exist(self):27 """28 A Function that check the model file is exist or not. If not exist, create a new model file.29 """30 if not (os.path.exists(self.model_path)):31 self.train_model()32 else:33 print("Model already exist.")34 35 def manipulate_dataset(self):36 """37 A Function that manipulate the dataset.38 """39 40 raw_data: pd.DataFrame = self.df41 # Remove Unexpected rows42 raw_data.drop(raw_data[raw_data['Price'] == 'None' ].index, inplace=True)43 raw_data.drop(raw_data[raw_data['Type of property'] == 'new-real-estate-project-apartments'].index,inplace=True)44 raw_data.drop(raw_data[raw_data['Type of property'] == 'new-real-estate-project-houses'].index,inplace=True)45 raw_data.drop(['Locality'], axis=1, inplace=True)46 # Delete dublicates47 raw_data = raw_data.drop_duplicates()48 # Price 49 raw_data['Price'] = raw_data['Price'].str.replace(',','')50 raw_data['Price'] = raw_data['Price'].astype(np.int64)51 #Bedrooms52 raw_data[raw_data['Type of property'] =='flat-studio']['Bedrooms'].all = 053 raw_data.drop(raw_data[raw_data["Bedrooms"] == "None"].index, inplace=True)54 raw_data["Bedrooms"] = raw_data['Bedrooms'].astype(int)55 # Living Area 56 raw_data.drop(raw_data[raw_data["Living area"] == "None"].index, inplace=True)57 raw_data["Living area"] = raw_data["Living area"].astype(int)58 # Building Condition59 raw_data['Building condition'].replace({60 'As new': 1,61 'Good': 2,62 'Just renovated': 3,63 'To renovate': 4,64 'To be done up': 5,65 'To restore': 6,66 'None': 2,67 }, inplace=True)68 raw_data['Building condition'] = raw_data['Building condition'].astype(int)69 # Kitchen type70 raw_data["Kitchen type"].replace({71 "Installed":2,72 "Hyper equipped":1,73 "None":0,74 "Semi equipped":3,75 "USA hyper equipped":1,76 "Not installed":0,77 "USA installed":2,78 "USA semi equipped":3,79 "USA uninstalled":0,80 }, inplace=True)81 raw_data['Kitchen type'] = raw_data['Kitchen type'].astype(int)82 # Furnished83 raw_data['Furnished'] = raw_data['Furnished'].replace('None', 0).astype(int)84 # Number of Frontage85 apt_median_value = raw_data[raw_data['Number of frontages'] != 'None']['Number of frontages'].median()86 raw_data['Number of frontages'] = raw_data['Number of frontages'].replace('None',2).astype(int)87 # Swimming Pool88 raw_data['Swimming pool'] = raw_data['Swimming pool'].replace('None', 0).astype(int)89 #Type of property90 #print(raw_data['Type of property'].unique())91 raw_data["Type of property"].replace({92 "apartment":0,93 "house":1,94 "duplex":2,95 "villa":3,96 "mixed-use-building":4,97 "exceptional-property": 5,98 "ground-floor": 6,99 "penthouse": 7,100 "loft": 8,101 "apartment-block": 9,102 "town-house": 10,103 "mansion": 11,104 "service-flat": 12,105 "castle": 13,106 "bungalow": 14,107 "triplex": 15,108 "flat-studio": 16,109 "farmhouse": 17,110 "other-property": 18,111 "kot": 19,112 "manor-house": 20,113 "chalet": 21,114 "country-cottage": 22115 }, inplace=True)116 raw_data['Type of property'] = raw_data['Type of property'].astype(int)117 # Terrace surface118 raw_data['Terrace surface'] = raw_data['Terrace surface'].replace('None', 0).astype(int)119 #Garden surface120 raw_data['Garden surface'] = raw_data['Garden surface'].replace('None', 0).astype(int)121 # surface of the plot 122 raw_data['Surface of the plot'] = raw_data['Surface of the plot'].replace('None', None)123 raw_data['Surface of the plot'] = raw_data['Surface of the plot'].fillna(raw_data['Living area'])124 raw_data['Surface of the plot'] = raw_data['Surface of the plot'].astype(int)125 # rename the column126 raw_data = raw_data.rename(columns={"Price": "price", "Bedrooms": "bedrooms",127 "Living area": "living_area", "Kitchen type": "kitchen_type",128 "Furnished": "furnished", "Terrace surface": "terrace_surface",129 "Garden surface": "garden_surface", "Surface of the plot": "plot_surface",130 "Number of frontages": "frontage", "Swimming pool": "swimming_pool",131 "Building condition": "condition", "Type of property": "property_type",132 })133 # Save the dataset to a csv file.134 raw_data.to_csv("./data/dataset.csv", index=False)135 def train_model(self):136 """137 A Function that train the model.138 """139 df = pd.read_csv('./data/dataset.csv')140 141 y = df["price"]142 X = df.drop(["price"], axis=1)143 X_train, X_test, y_train, y_test = train_test_split(X, y , test_size=0.2, random_state=0)144 scaler = StandardScaler()145 scaler.fit(X_train)146 features_scal = scaler.transform(X_train)147 regressor = LinearRegression()148 regressor.fit(X_train, y_train)149 print(regressor.score(X_train, y_train))150 predict = regressor.predict(X_train)151 print(regressor.score(X_test, y_test))152 joblib.dump(regressor, "model/model.pkl")153 model_columns = list(X.columns)154 joblib.dump(model_columns, "model/model_columns.pkl")155 print(X.columns)156 def create_model_file(self):157 """158 A Function that create a model file.159 """160 pass161 def check_performance(self):162 """163 A Function that check the performance of the model.164 """165 pass166model = Model()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!