Best Python code snippet using autotest_python
urls.py
Source:urls.py
1from django.conf.urls import url, include2from django.contrib.auth.views import LoginView3from django.http import HttpResponse4from . import views5import rest_framework.urls6import social_django.urls7urlpatterns = [8 url(r'^$',9 views.ShareaboutsAPIRootView.as_view(),10 name='api-root'),11 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/places/(?P<thing_id>\d+)/attachments$',12 views.AttachmentListView.as_view(),13 name='place-attachments'),14 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/places/(?P<place_id>\d+)/(?P<submission_set_name>[^/]+)/(?P<thing_id>\d+)/attachments$',15 views.AttachmentListView.as_view(),16 name='submission-attachments'),17 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/actions$',18 views.ActionListView.as_view(),19 name='action-list'),20 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/metadata$',21 views.DataSetMetadataView.as_view(),22 name='dataset-metadata'),23 # bulk data snapshots24 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/(?P<submission_set_name>[^/]+)/snapshots$',25 views.DataSnapshotRequestListView.as_view(),26 name='dataset-snapshot-list'),27 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/(?P<submission_set_name>[^/]+)/snapshots/(?P<data_guid>[^/.]+)(:?\.(?P<format>[^/]+))?$',28 views.DataSnapshotInstanceView.as_view(),29 name='dataset-snapshot-instance'),30 # ad-hoc data31 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/places/(?P<place_id>\d+)/(?P<submission_set_name>[^/]+)/(?P<submission_id>\d+)$',32 views.SubmissionInstanceView.as_view(),33 name='submission-detail'),34 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/places/(?P<place_id>\d+)/(?P<submission_set_name>[^/]+)(?:/(?P<pk_list>(?:\d+,)+\d+))?$',35 views.SubmissionListView.as_view(),36 name='submission-list'),37 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/places/(?P<place_id>\d+)$',38 views.PlaceInstanceView.as_view(),39 name='place-detail'),40 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/places(?:/(?P<pk_list>(?:\d+,)+\d+))?$',41 views.PlaceListView.as_view(),42 name='place-list'),43 url(r'^~/datasets$',44 views.AdminDataSetListView.as_view(),45 name='admin-dataset-list'),46 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/keys$',47 views.ApiKeyListView.as_view(),48 name='apikey-list'),49 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/origins$',50 views.OriginListView.as_view(),51 name='origin-list'),52 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)/(?P<submission_set_name>[^/]+)(?:/(?P<pk_list>(?:\d+,)+\d+))?$',53 views.DataSetSubmissionListView.as_view(),54 name='dataset-submission-list'),55 url(r'^(?P<owner_username>[^/]+)/datasets/(?P<dataset_slug>[^/]+)$',56 views.DataSetInstanceView.as_view(),57 name='dataset-detail'),58 url(r'^(?P<owner_username>[^/]+)/datasets$',59 views.DataSetListView.as_view(),60 name='dataset-list'),61 # profiles and user info62 url(r'^(?P<owner_username>[^/]+)$',63 views.UserInstanceView.as_view(),64 name='user-detail'),65 url(r'^(?P<owner_username>[^/]+)/password$',66 lambda *a, **k: None,67 name='user-password'),68 url(r'^users/current$',69 views.CurrentUserInstanceView.as_view(),70 name='current-user-detail'),71 # authentication / association72 url(r'^users/login/error/$', views.remote_social_login_error, name='remote-social-login-error'),73 url(r'^users/login/(?P<backend>[^/]+)/$', views.remote_social_login, name='remote-social-login'),74 url(r'^users/logout/$', views.remote_logout, name='remote-logout'),75 url('^users/', include(social_django.urls, namespace='social')),76 url(r'^forms/', include(rest_framework.urls)),77 # Utility routes78 url(r'^utils/send-away', views.redirector, name='redirector'),79 url(r'^utils/session-key', views.SessionKeyView.as_view(), name='session-key'),80 url(r'^utils/noop/?$', lambda request: HttpResponse(''), name='noop-route'),81]82#places_base_regex = r'^(?P<dataset__owner__username>[^/]+)/datasets/(?P<dataset__slug>[^/]+)/places/'83#urlpatterns = patterns('sa_api_v2',84# url(r'^$',85# views.OwnerCollectionView.as_view(),86# name='owner_collection'),87# url(r'^(?P<owner__username>[^/]+)/datasets/$',88# views.DataSetCollectionView.as_view(),89# name='dataset_collection_by_user'),90# url(r'^(?P<owner__username>[^/]+)/datasets/(?P<slug>[^/]+)/$',91# views.DataSetInstanceView.as_view(),92# name='dataset_instance_by_user'),93# url(r'^(?P<datasets__owner__username>[^/]+)/datasets/(?P<datasets__slug>[^/]+)/keys/$',94# views.ApiKeyCollectionView.as_view(),95# name='api_key_collection_by_dataset'),96# url(places_base_regex + '$',97# views.PlaceCollectionView.as_view(),98# name='place_collection_by_dataset'),99# url(places_base_regex + 'table$',100# views.TabularPlaceCollectionView.as_view(),101# name='tabular_place_collection_by_dataset'),102# url(places_base_regex + r'(?P<pk>\d+)/$',103# views.PlaceInstanceView.as_view(),104# name='place_instance_by_dataset'),105# url(places_base_regex + r'(?P<thing_id>\d+)/attachments/$',106# views.AttachmentView.as_view(),107# name='place_attachment_by_dataset'),108# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/$',109# views.SubmissionCollectionView.as_view(),110# name='submission_collection_by_dataset'),111# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/table$',112# views.TabularSubmissionCollectionView.as_view(),113# name='tabular_submission_collection_by_dataset'),114# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/(?P<pk>\d+)/$',115# views.SubmissionInstanceView.as_view(),116# name='submission_instance_by_dataset'),117# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/(?P<thing_id>\d+)/attachments/$',118# views.AttachmentView.as_view(),119# name='submission_attachment_by_dataset'),120# url(r'^(?P<data__dataset__owner__username>[^/]+)/datasets/(?P<data__dataset__slug>[^/]+)/actions/$',121# views.ActionListView.as_view(),122# name='action_collection_by_dataset'),123# url(r'^(?P<dataset__owner__username>[^/]+)/datasets/(?P<dataset__slug>[^/]+)/(?P<submission_type>[^/]+)/$',124# views.AllSubmissionCollectionsView.as_view(),125# name='all_submissions_by_dataset'),126# url(r'^(?P<dataset__owner__username>[^/]+)/datasets/(?P<dataset__slug>[^/]+)/(?P<submission_type>[^/]+)/table$',127# views.TabularAllSubmissionCollectionsView.as_view(),128# name='tabular_all_submissions_by_dataset'),129# url(r'^(?P<owner__username>[^/]+)/password$',130# views.OwnerPasswordView.as_view(),131# name='owner_password'),132#)133#places_base_regex = r'^datasets/(?P<dataset__owner__username>[^/]+)/(?P<dataset__slug>[^/]+)/places/'134#urlpatterns += patterns('sa_api_v2',135# ###############################################136# # URL patterns with 'datasets/' before user name. Deprecate.137# url(r'^datasets/(?P<owner__username>[^/]+)/$',138# views.DataSetCollectionView.as_view(),139# name='dataset_collection_by_user_1'),140# url(r'^datasets/(?P<owner__username>[^/]+)/(?P<slug>[^/]+)/$',141# views.DataSetInstanceView.as_view(),142# name='dataset_instance_by_user_1'),143# url(r'^datasets/(?P<datasets__owner__username>[^/]+)/(?P<datasets__slug>[^/]+)/keys/$',144# views.ApiKeyCollectionView.as_view(),145# name='api_key_collection_by_dataset_1'),146# url(places_base_regex + '$',147# views.PlaceCollectionView.as_view(),148# name='place_collection_by_dataset_1'),149# url(places_base_regex + 'table$',150# views.TabularPlaceCollectionView.as_view(),151# name='tabular_place_collection_by_dataset_1'),152# url(places_base_regex + r'(?P<pk>\d+)/$',153# views.PlaceInstanceView.as_view(),154# name='place_instance_by_dataset_1'),155# url(places_base_regex + r'(?P<thing_id>\d+)/attachments/$',156# views.AttachmentView.as_view(),157# name='place_attachment_by_dataset_1'),158# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/$',159# views.SubmissionCollectionView.as_view(),160# name='submission_collection_by_dataset_1'),161# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/table$',162# views.TabularSubmissionCollectionView.as_view(),163# name='tabular_submission_collection_by_dataset_1'),164# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/(?P<pk>\d+)/$',165# views.SubmissionInstanceView.as_view(),166# name='submission_instance_by_dataset_1'),167# url(places_base_regex + r'(?P<place_id>\d+)/(?P<submission_type>[^/]+)/(?P<thing_id>\d+)/attachments/$',168# views.AttachmentView.as_view(),169# name='submission_attachment_by_dataset_1'),170# url(r'^datasets/(?P<data__dataset__owner__username>[^/]+)/(?P<data__dataset__slug>[^/]+)/action/$',171# views.ActionListView.as_view(),172# name='action_collection_by_dataset_1'),173# url(r'^datasets/(?P<dataset__owner__username>[^/]+)/(?P<dataset__slug>[^/]+)/(?P<submission_type>[^/]+)/$',174# views.AllSubmissionCollectionsView.as_view(),175# name='all_submissions_by_dataset_1'),176# url(r'^datasets/(?P<dataset__owner__username>[^/]+)/(?P<dataset__slug>[^/]+)/(?P<submission_type>[^/]+)/table$',177# views.TabularAllSubmissionCollectionsView.as_view(),178# name='tabular_all_submissions_by_dataset_1'),179# ###############################################180# # Views with no specified dataset. Deprecate.181# url(r'^places/(?P<pk>\d+)/$',182# views.PlaceInstanceView.as_view(),183# name='place_instance'),184# url((r'^places/(?P<place_id>\d+)/'185# r'(?P<submission_type>[^/]+)/$'),186# views.SubmissionCollectionView.as_view(),187# name='submission_collection'),188# url((r'^places/(?P<place_id>\d+)/'189# r'(?P<submission_type>[^/]+)/(?P<pk>\d+)/$'),190# views.SubmissionInstanceView.as_view(),191# name='submission_instance'),192# url(r'^action/$',193# views.ActionListView.as_view(),194# name='action_collection'),...
views.py
Source:views.py
1import aiohttp2from aiohttp import web3from aiohttp_session import get_session4routers_chat = web.RouteTableDef()5async def close_all(app):6 for owner_username in app['chat']['user_web_sockets']:7 for ws in owner_username.values():8 ws.close()9 for owner_username in app['chat']['anon_web_sockets']:10 for ws in owner_username:11 ws.close()12@routers_chat.view('/{user_name}')13class UserChat(web.View):14 async def broadcast(self, message):15 """16 Send message to all users of current chat17 """18 owner_username = self.request.match_info['user_name']19 # Send fo all authorized peers20 if owner_username in self.request.app['chat']['user_web_sockets']:21 for ws in self.request.app['chat']['user_web_sockets'][owner_username].values():22 response = dict(username=self.request.user.name, message=message)23 await ws.send_json(response)24 # Send fo all unauthorized peers25 if owner_username in self.request.app['chat']['anon_web_sockets']:26 for ws in self.request.app['chat']['anon_web_sockets'][owner_username]:27 response = dict(username=self.request.user.name, message=message)28 await ws.send_json(response)29 async def get(self):30 # Init WebSockets31 ws = web.WebSocketResponse()32 await ws.prepare(self.request)33 # Get owner of page name34 owner_username = self.request.match_info['user_name']35 # Check peer authentication36 if self.request.user.is_authenticated:37 # Init user non anon webSocket38 owner_wsdict = self.request.app['chat']['user_web_sockets']39 # Collect webSocket40 if owner_username not in owner_wsdict:41 owner_wsdict[owner_username] = {self.request.user.name: ws}42 else:43 owner_wsdict[owner_username].update({self.request.user.name: ws})44 # Send message45 async for msg in ws:46 if msg.type == aiohttp.WSMsgType.TEXT:47 await self.broadcast(msg.data)48 # Close connection49 owner_wsdict[owner_username].pop(self.request.user.name)50 if len(owner_wsdict[owner_username]) == 0:51 del owner_wsdict[owner_username]52 return ws53 else:54 # Init user anon webSocket55 owner_wsdict = self.request.app['chat']['anon_web_sockets']56 # Collect websocket57 if owner_username not in owner_wsdict:58 owner_wsdict[owner_username] = [ws]59 else:60 owner_wsdict[owner_username].append(ws)61 # Send error response62 async for msg in ws:63 if msg.type == aiohttp.WSMsgType.TEXT:64 response = dict(username='SERVER', message='You must be authorize!')65 await ws.send_json(response)66 # Close connection67 owner_wsdict[owner_username].remove(ws)68 if len(owner_wsdict[owner_username]) == 0:69 del owner_wsdict[owner_username]...
account.py
Source:account.py
1"""2Instances of this class each model a single bank account3@author: Andrew Curry4"""5class Account():6 """7 Instances of this class each model a single bank account8 """9 def __init__(10 self, id: int = -1, 11 is_approved: bool = False, 12 owner_username: str = "", 13 funds: int = 0) -> None:14 """15 Does NOT validate params16 """17 self.id: int = id18 self.is_approved: bool = is_approved19 self.owner_username: str = owner_username20 self.funds: int = funds21 def __eq__(self, other):22 return isinstance(other, Account) \23 and self.id == other.id \24 and self.is_approved == other.is_approved \25 and self.owner_username == other.owner_username \26 and self.funds == other.funds27 # ----------28 # SERIALIZATION / DESERIALIZATION29 # ----------30 31 def encode(self) -> bytes:32 """33 Returns a bytes representation of this account.34 """35 code: str = str(id) \36 + " " + str(self.is_approved) \37 + " " + self.owner_username \38 + " " + str(self.funds)39 return code.encode()40 def decode(code: bytes):# -> Account:41 """42 Returns a new Account based on the information in the given bytes43 """44 vals: list[str] = str(bytes).split(' ')45 result: Account = Account()46 result.id = int(vals[0])47 result.is_approved = bool(vals[1])48 result.owner_username = vals[2]49 result.funds = int(vals[3])50 return result51 52 def to_dict(self) -> dict:53 """54 Returns a dict representation of this account.55 """56 d: dict = {57 "id" : self.id,58 "is_approved" : self.is_approved,59 "owner_username" : self.owner_username,60 "funds" : self.funds,61 }62 return d63 64 def from_dict(d: dict):65 """66 Returns a new Account based on the information in the given dict67 """68 if d:69 result: Account = Account()70 result.id = int(d["id"])71 result.is_approved = bool(d["is_approved"])72 result.owner_username = d["owner_username"]73 result.funds = int(d["funds"])74 return result75 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!!