Best Python code snippet using slash
views.py
Source:views.py
1from django.shortcuts import render2from rest_framework.response import Response3from rest_framework.views import APIView4from .models import Feed, Reply, Like, Bookmark5from uuid import uuid46from user.models import User7import os8from Instagram.settings import MEDIA_ROOT9class Main(APIView):10 # noinspection PyMethodMayBeStatic11 def get(self, request):12 email = request.session.get('email', None)13 if email is None:14 return render(request, "user/login.html")15 user = User.objects.filter(email=email).first()16 if user is None:17 return render(request, "user/login.html")18 feed_object_list = Feed.objects.all().order_by('-id')19 feed_list = []20 for feed in feed_object_list:21 user = User.objects.filter(email=feed.email).first()22 reply_object_list = Reply.objects.filter(feed_id=feed.id)23 reply_list = []24 for reply in reply_object_list:25 user = User.objects.filter(email=reply.email).first()26 reply_list.append(dict(reply_content=reply.reply_content,27 nickname=user.nickname))28 like_count = Like.objects.filter(feed_id=feed.id, is_like=True).count()29 is_liked = Like.objects.filter(feed_id=feed.id, email=email, is_like=True).exists()30 is_marked = Bookmark.objects.filter(feed_id=feed.id, email=email, is_marked=True).exists()31 feed_list.append(dict(32 id=feed.id,33 image=feed.image,34 content=feed.content,35 like_count=like_count,36 profile_image=user.profile_image,37 nickname=user.nickname,38 reply_list=reply_list,39 is_liked=is_liked,40 is_marked=is_marked41 ))42 return render(request, "instagram/main.html", context=dict(feed_list=feed_list, user=user))43class UploadFeed(APIView):44 # noinspection PyMethodMayBeStatic45 def post(self, request):46 file = request.FILES['file']47 uuid_name = uuid4().hex48 save_path = os.path.join(MEDIA_ROOT, uuid_name)49 with open(save_path, 'wb+') as destination:50 for chunk in file.chunks():51 destination.write(chunk)52 image = uuid_name53 content = request.data.get('content')54 email = request.session.get('email', None)55 Feed.objects.create(image=image, content=content, email=email)56 return Response(status=200)57class Profile(APIView):58 def get(self, request):59 email = request.session.get('email', None)60 if email is None:61 return render(request, "user/login.html")62 user = User.objects.filter(email=email).first()63 if user is None:64 return render(request, "user/login.html")65 feed_list = Feed.objects.filter(email=email).all()66 like_list = list(Like.objects.filter(email=email, is_like=True).values_list('feed_id', flat=True))67 like_feed_list = Feed.objects.filter(id__in=like_list)68 bookmark_list = list(Bookmark.objects.filter(email=email, is_marked=True).values_list('feed_id', flat=True))69 bookmark_feed_list = Feed.objects.filter(id__in=bookmark_list)70 return render(request, 'content/profile.html', context=dict(feed_list=feed_list,71 like_feed_list=like_feed_list,72 bookmark_feed_list=bookmark_feed_list,73 user=user))74class UploadReply(APIView):75 def post(self, request):76 feed_id = request.data.get('feed_id', None)77 reply_content = request.data.get('reply_content', None)78 email = request.session.get('email', None)79 Reply.objects.create(feed_id=feed_id, reply_content=reply_content, email=email)80 return Response(status=200)81class ToggleLike(APIView):82 def post(self, request):83 feed_id = request.data.get('feed_id', None)84 favorite_text = request.data.get('favorite_text', True)85 if favorite_text == 'favorite_border':86 is_like = True87 else:88 is_like = False89 email = request.session.get('email', None)90 like = Like.objects.filter(feed_id=feed_id, email=email).first()91 if like:92 like.is_like = is_like93 like.save()94 else:95 Like.objects.create(feed_id=feed_id, is_like=is_like, email=email)96 return Response(status=200)97class ToggleBookmark(APIView):98 def post(self, request):99 feed_id = request.data.get('feed_id', None)100 bookmark_text = request.data.get('bookmark_text', True)101 if bookmark_text == 'bookmark_border':102 is_marked = True103 else:104 is_marked = False105 email = request.session.get('email', None)106 bookmark = Bookmark.objects.filter(feed_id=feed_id, email=email).first()107 if bookmark:108 bookmark.is_marked = is_marked109 bookmark.save()110 else:111 Bookmark.objects.create(feed_id=feed_id, is_marked=is_marked, email=email)...
class2-13.py
Source:class2-13.py
1"""2Clare Minnerath302/13/20204Graph Traversals5"""6def depth_first(G, start, end):7 found = False8 path = []9 adjacency_queue = []10 vertex_stack = [start]11 is_marked = [False]*len(G) 12 13 while vertex_stack and not found:14 v = vertex_stack.pop()15 if v == end:16 path.append(v)17 found = True18 else:19 if not is_marked[v]:20 is_marked[v] = True21 path.append(v)22 23 adjacency_queue.clear()24 # this for loop should use enumerate25 for edge in range(len(G[v])):26 if G[v][edge]!=0:27 adjacency_queue.append(edge)28 29 while adjacency_queue:30 v = adjacency_queue.pop(0)31 if not is_marked[v]:32 vertex_stack.append(v)33 return path34def breadth_first(G, start, end):35 found = False36 path = []37 adjacency_queue = []38 vertex_queue = [start]39 is_marked = [False]*len(G) 40 41 while vertex_queue and not found:42 v = vertex_queue.pop(0)43 if v == end:44 path.append(v)45 found = True46 else:47 if not is_marked[v]:48 is_marked[v] = True49 path.append(v)50 51 adjacency_queue.clear()52 # this for loop should use enumerate53 for edge in range(len(G[v])):54 if G[v][edge]!=0 and G[v][edge]!= x:55 adjacency_queue.append(edge)56 57 while adjacency_queue:58 v = adjacency_queue.pop(0)59 if not is_marked[v]:60 vertex_queue.append(v)61 return path 62G = [[0,1,1,1,0,0,0,0,0,0],63 [1,0,0,0,1,1,0,0,0,0],64 [1,0,0,0,0,0,0,1,0,0],65 [1,0,0,0,0,0,0,0,1,0],66 [0,1,0,0,0,0,1,0,0,0],67 [0,1,0,0,0,0,0,0,0,0],68 [0,0,0,0,1,0,0,0,0,0],69 [0,0,1,0,0,0,0,0,0,0],70 [0,0,0,1,0,0,0,0,0,1],71 [0,0,0,0,0,0,0,0,1,0]]72print(depth_first(G,4,9))73print(breadth_first(G,0,9))74print()75x = 'x'76G2 = [[0,200,x,x,x,x,160],77 [200,0,x,780,x,900,x],78 [x,1300,0,x,600,x,x],79 [x,x,x,0,1400,1000,x],80 [x,x,600,x,0,x,800],81 [x,x,x,1000,x,0,x],82 [x,x,x,x,800,x,0]]83print(depth_first(G2,0,6))...
bingo.py
Source:bingo.py
...7 def __init__(self, state: list[list[str]]):8 self.state: list[list[BingoSpace]] = [[BingoSpace(value) for value in row] for row in state]9 def score(self):10 return sum(11 int(space.value) for row in self.state for space in row if not space.is_marked()12 )13 def mark(self, value):14 for row in self.state:15 for space in row:16 if space.value == value:17 if space.is_marked():18 raise ValueError(f"Attempted to mark space that is already marked: {space.value}")19 else:20 space.set_marked()21 return22 def mark_position(self, x, y):23 if self.state[x][y].is_marked():24 raise ValueError(f"Attempted to mark space that is already marked: {x},{y}")25 else:26 self.state[x][y].set_marked()27 def has_won(self):28 for row in self.state:29 for space in row:30 if not space.is_marked():31 break32 else:33 return True34 for col_index in range(len(self.state[0])):35 for row_index in range(len(self.state[0])):36 if not self.state[row_index][col_index].is_marked():37 break38 else:39 return True40 return False41 def __str__(self):42 return "\n".join([",".join([str(val) for val in row]) for row in self.state])43class BingoSpace:44 def __init__(self, value: str):45 self.value: str = value46 self.marked: bool = False47 def set_marked(self):48 self.marked = True49 def is_marked(self):50 return self.marked51 def __str__(self):52 if self.is_marked():53 return self.value + "*"54 else:...
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!!