Best Python code snippet using autotest_python
eulerian_path.py
Source:eulerian_path.py
1#!/usr/bin/python23###############################################################################4#5# Author:6#7# Sanyk28 (san-heng-yi-shu@163.com)8#9# Date created:10#11# 5 Dec 201312#13# CODE CHALLENGE: Solve the Eulerian Path Problem.14# Input: The adjacency list of a directed graph that has an Eulerian path.15# Output: An Eulerian path in this graph.16#17# Sample Input:18# 0 -> 219# 1 -> 320# 2 -> 121# 3 -> 0,422# 6 -> 3,723# 7 -> 824# 8 -> 925# 9 -> 626#27# Sample Output:28# 6->7->8->9->6->3->0->2->1->3->429#30############################################################################### 3132import sys33import timeit34import re35import heapq36import random37import numpy as np38from itertools import combinations,product,izip,ifilter,chain39from collections import Counter,defaultdict4041def data_process(line):42 line = [item.strip() for item in line.split('->')]43 if ',' in line[1]:44 for item in line[1].split(','):45 line.append((line[0],item))46 line.pop(0)47 line.pop(0)48 else:49 line = tuple(line)50 return line5152def read_file(input_file):53 '''54 >>> data = read_file('test.eulerian_path.txt')55 >>> data56 [('0', '2'), ('1', '3'), ('2', '1'), ('3', '0'), ('3', '4'), ('6', '3'), ('6', '7'), ('7', '8'), ('8', '9'), ('9', '6')]57 >>> data = read_file('test.eulerian_path.extra.txt')58 '''59 f = open(input_file)60 data = [item.strip() for item in f.readlines()]61 f.close()62 result = []63 for line in data:64 dp = data_process(line)65 if isinstance(dp,tuple):66 result.append(dp)67 elif isinstance(dp,list):68 result += dp69 return result7071def find_ender(data):72 item0 = [item[0] for item in data]73 item1 = [item[1] for item in data]74 c0 = Counter(item0)75 c1 = Counter(item1)76 result = [(item,c0[item],c1[item]) for item in c1 if c0[item] != c1[item]]77 return [item[0] for item in result if item[1] < item[2]][0]7879def find_path(data):80 initial_ender = find_ender(data)81 ender = find_ender(data)82 path = [ender]83 while len(data) != 0:84 try:85 starter = [item1 for (item1,item2) in data if item2 == ender][0]86 data.remove((starter,ender))87 path.append(starter)88 ender = starter89 except:90 break91 return (path[::-1],data,initial_ender)9293def form_cycle(unpath,initial_ender):94 rgk,rgv = [(item1,item2) for (item1,item2) in unpath if item2 == initial_ender][0]95 cycle = [rgv]96 while len(unpath) != 0:97 try:98 cycle.append(rgk)99 unpath.remove((rgk,rgv))100 rgk,rgv = [(item1,item2) for (item1,item2) in unpath if item2 == rgk][0]101 except:102 break103 return (cycle[::-1],unpath)104105def fuse(path,cycle):106 fuse_index = path.index(cycle[0])107 return path[:fuse_index]+cycle+path[fuse_index+1:]108109def eulerian_path(data):110 path,unpath,initial_ender = find_path(data)111 cycle,unpath = form_cycle(unpath,initial_ender)112 path = fuse(path,cycle)113 while len(unpath) != 0:114 print 'len(unpath): '+str(len(unpath))115 potential = [item for item in path if item in chain(*unpath)]116 newStart = random.choice(potential)117 cycle,unpath = form_unCycle(unpath,newStart)118 path = fuse(path,cycle) 119 return path120121def form_unCycle(data,rgk):122 rgv = [item2 for (item1,item2) in data if rgk == item1][0]123 cycle = [rgk]124 while len(data) != 0:125 try:126 cycle.append(rgv)127 data.remove((rgk,rgv))128 rgk = rgv129 rgv = [item2 for (item1,item2) in data if rgv == item1][0]130 except:131 break132 return (cycle,data)133134def result(filename):135 data = read_file(filename)136 results = eulerian_path(data)137 return results138139if __name__ == "__main__":140141 start = timeit.default_timer()142 results = result(sys.argv[-1])143 fw = open('output.'+sys.argv[-1][:-4]+'.txt','w')144 fw.write('->'.join(results))145 fw.close()146 stop = timeit.default_timer()
...
whatsapp_client.py
Source:whatsapp_client.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4Created on Wed Dec 8 13:03:13 20215@author: alex6"""7import socket8import threading9import sys10import random11import os12import subprocess13bufferSize = 102414#Client Code15def ReceiveData(sock):16 while True:17 try:18 data,addr = sock.recvfrom(bufferSize)19 print(data.decode('utf-8'))20 except:21 pass22def RunClient(serverIP):23 isConnected = 024 path =""25 unpath=""26 print("What is your request:")27 while True: 28 request = input()29 if request == 'mount':30 isConnected = 131 host = socket.gethostbyname(socket.gethostname())32 port = random.randint(6000, 10000)33 print('Client IP->' + str(host) + ' Port->' + str(port))34 server = (str(serverIP), 5000)35 UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)36 UDPClientSocket.bind((host, port))37 name = 'make connection to the server'38 UDPClientSocket.sendto(name.encode('utf-8'), server)39 threading.Thread(target=ReceiveData, args=(UDPClientSocket,)).start()40 elif request == 'qqq':41 break42 else:43 if (not isConnected):44 if(not request.split(" ")[0]=="cd"):45 request = request.split(' ')46 k= subprocess.run(request)47 print(k)48 else:49 os.chdir(request.split(" ")[1])50 else:51 if(request.split(" ")[0]=="cd"):52 if(not request.split(" ")[1]==".."):53 path=path+request.split(" ")[1]+"/"54 unpath=unpath+"../"55 else:56 path_arr = path.split("/")57 path = "/".join(path_arr[:-2])58 path = path +"/"59 unpath_arr=unpath.split("/")60 unpath="/".join(unpath_arr[:-2])61 unpath = unpath +"/"62 else:63 if(request.split(" ")[0]=="cp"):64 dest=open(request.split(" ")[2],'w')65 path1 = '1' + ' '+ path66 request = '2' + ' '+ request67 unpath1 = '1' + ' '+ unpath68 UDPClientSocket.sendto(path1.encode('utf-8'), server)69 UDPClientSocket.sendto(request.encode('utf-8'), server)70 UDPClientSocket.sendto(unpath1.encode('utf-8'), server)71 #UDPClientSocket.sendto(data.encode('utf-8'),server)72 dest.close()73 UDPClientSocket.close()74 os._exit(1)75#Client Code Ends Here76if __name__ == '__main__':77 RunClient(sys.argv[1])...
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!!