Best Python code snippet using lisa_python
machine.py
Source:machine.py
...31 return (self.magic_p + self.size_p + self.data)32 def getstr(self):33 return self.data34 def read_blog_data(self, _read):35 _read(self.getstr() + '\n')36class BlogAsciiFile():37 def __init__(self):38 self.filename = random_word()39 self.magic = 1387340 self.line_width = random.randint(20, 60)41 self.num_lines = random.randint(10, 30)42 self.magic_p = struct.pack('I', self.magic)43 self.line_width_p = struct.pack('H', self.line_width)44 self.num_lines_p = struct.pack('H', self.num_lines)45 self.data = ''46 for x in xrange(self.num_lines):47 self.data += random_word(self.line_width-1, self.line_width-1) + '\n'48 def getfile(self):49 return (self.magic_p + self.line_width_p + self.num_lines_p + self.data)50 def getstr(self):51 return self.data52 def read_blog_data(self, _read):53 _read(self.getstr() + '\n')54class BlogText():55 def __init__(self, text):56 self.text = text57 def read_blog_data(self, _read):58 _read("%s\n" % self.text)59def create_file():60 if random.randint(0,1):61 return BlogPictureFile()62 else:63 return BlogAsciiFile()64class Post():65 def __init__(self, owner, _read, _write):66 done = False67 self.owner = owner68 self.post_data = []69 self.title = random_text(5)70 self.likes = []71 _read("Enter post title: \n\t")72 _write("%s\n" % self.title)73 while not done:74 _read("Enter Post Text (end with: ```):\n")75 text = random_text()76 _write(text)77 _write("```\n")78 self.post_data.append(BlogText(text))79 if owner.is_full_user():80 _read("Upload Image?\n")81 _read("1. Yes\n")82 _read("2. No\n")83 _read("Selection: ")84 if random.randint(0,5) == 0:85 _write("1\n")86 blog_file = create_file()87 _read("Enter Filename: ")88 _write(blog_file.filename + '\n')89 _read("--Begin Uploading File--\n")90 _write(blog_file.getfile())91 self.post_data.append(blog_file)92 else:93 _write("2\n")94 _read("Add more Text?\n")95 _read("1. Yes\n")96 _read("2. No\n")97 _read("Selection: ")98 if random.randint(0,5) == 0:99 _write("1\n")100 else:101 _write("2\n")102 done = True103 def add_like(self, friend, _read):104 for like in self.likes:105 if like.username == friend.username:106 _read("You already liked this\n")107 return108 self.likes.append(friend)109 self.owner.total_likes += 1110 def read_post(self, _read):111 _read("Post by: %s\n" % self.owner.username)112 _read("---%s---\n" % self.title)113 for pdata in self.post_data:114 pdata.read_blog_data(_read)115 def read_title(self, _read):116 _read(self.title)117class User():118 def __init__(self, username, password, friendcode):119 self.username = username120 self.password = password121 self.friendcode = friendcode122 self.profile = ""123 self.total_likes = 0124 self.posts = []125 self.friends = []126 def is_full_user(self):127 if len(self.friends) >= 3 and len(self.posts) >= 5 and self.total_likes > 5:128 return True129 return False130 def update_profile(self, _write):131 self.profile = random_text()132 _write(self.profile)133 _write("```\n")134 def add_friend(self, friend, _read):135 if friend.username == self.username:136 _read("Trying to add self\n")137 return False138 for f in self.friends:139 if f.username == friend.username:140 return False141 self.friends.append(friend)142 return True143 def read_friend_list(self, _read):144 for i, friend in enumerate(self.friends):145 _read("%d - %s\n" % (i+1, friend.username))146 def read_post_list(self, _read):147 for i, post in enumerate(self.posts):148 _read("%d - %s\n" % (i+1, post.title))149 def read_profile(self, _read):150 _read("User %s's profile:\n" % self.username)151 if not self.profile:152 _read("No profile yet!\n")153 else:154 _read("%s\n" % self.profile)155class Blog(Actions):156 def _read(self, string):157 self.read(length=len(string), expect=string)158 def start(self):159 self.users = []160 self.state['logged_in_user'] = None161 def logged_out_menu(self):162 self._read("Select an option\n")163 self._read("1. Register\n")164 self._read("2. Log in\n")165 self._read("3. View a user's posts\n")166 self._read("4. View a user's profile\n")167 self._read("5. Quit\n")168 self._read("Selection: ")169 def register(self):170 self.write('1\n')171 username = random_word()172 password = random_word()173 friendcode = random_word()174 self._read("Enter username: ")175 self.write(username + "\n")176 for user in self.users:177 if user.username == username:178 self._read("Error: Username already exists. Try a different username\n")179 return180 self._read("Enter password: ")181 self.write(password + "\n")182 self._read("Friend code: ")183 self.write(friendcode + "\n")184 self.users.append(User(username, password, friendcode))185 def login(self):186 if not self.users:187 self.register()188 self.logged_out_menu()189 self.write("2\n")190 user = self.users[random.randint(0, len(self.users) - 1)]191 self.read("Username: ")192 self.write(user.username + "\n")193 self.read("Password: ")194 self.write(user.password + "\n")195 self._read("Authenticated\n")196 self.state['logged_in_user'] = user197 def view_user_posts_out(self):198 self.write("3\n")199 self._read("Enter username: ")200 if self.users:201 user = self.users[random.randint(0, len(self.users) - 1)]202 else:203 self.write("\n")204 self._read("Bad Username\n")205 return206 self.write(user.username + "\n")207 self._read("Select a post id to view\n")208 self._read("0. Go Back\n")209 user.read_post_list(self._read)210 self._read("Selection: ")211 if user.posts:212 choice = random.randint(0, len(user.posts))213 self.write("%d\n" % (choice))214 if choice == 0:215 self._read("Returning to main menu\n")216 else:217 post = user.posts[choice - 1]218 post.read_post(self._read)219 else:220 self.write("0\n")221 self._read("Returning to main menu\n")222 def view_user_profile_out(self):223 self.write("4\n")224 self._read("Enter username: ")225 if self.users:226 user = self.users[random.randint(0, len(self.users) - 1)]227 else:228 self.write("\n")229 self._read("Bad Username\n")230 return231 self.write(user.username + "\n")232 user.read_profile(self._read)233 def logged_in_menu(self):234 self._read("Select an option\n")235 self._read("1. Add new blog post\n")236 self._read("2. Delete blog post\n")237 self._read("3. Update profile\n")238 self._read("4. Add a friend\n")239 self._read("5. View my friends\n")240 self._read("6. Unfriend someone\n")241 self._read("7. View my posts\n")242 self._read("8. View my profile\n")243 self._read("9. View a user's posts\n")244 self._read("10. View a user's profile\n")245 self._read("11. Log out\n")246 self._read("Selection: ")247 def add_new_post(self):248 self.write("1\n")249 user = self.state['logged_in_user']250 post = Post(user, self._read, self.write)251 if post:252 user.posts.append(post)253 def delete_blog_post(self):254 self.write("2\n")255 user = self.state['logged_in_user']256 self._read("Select a post to delete:\n")257 self._read("0. Go Back\n")258 user.read_post_list(self._read)259 self._read("Selection: ")260 if user.posts:261 choice = random.randint(0, len(user.posts))262 self.write("%d\n" % (choice))263 if choice == 0:264 self._read("Returning to main menu\n")265 else:266 del user.posts[choice - 1]267 else:268 self.write("0\n")269 self._read("Returning to main menu\n")270 def update_profile(self):271 self.write("3\n")272 user = self.state['logged_in_user']273 self._read("Enter new profile info (end with: ```)\n")274 user.update_profile(self.write)275 def add_friend(self):276 self.write("4\n")277 user = self.state['logged_in_user']278 friend = self.users[random.randint(0, len(self.users) - 1)]279 self.read("Enter friend username: ")280 self.write(friend.username + "\n")281 self.read("Friend code: ")282 self.write(friend.friendcode + "\n")283 if user.add_friend(friend, self._read):284 self._read("Added friend, %s!\n" % friend.username)285 else:286 self._read("Couldn't add as friend.\n")287 def view_friends(self):288 self.write("5\n")289 user = self.state['logged_in_user']290 user.read_friend_list(self._read)291 def unfriend(self):292 self.write("6\n")293 user = self.state['logged_in_user']294 self._read("Select a user to unfriend:\n")295 self._read("0. Go Back\n")296 user.read_friend_list(self._read)297 self._read("Selection: ")298 if user.friends:299 choice = random.randint(0, len(user.friends))300 self.write("%d\n" % (choice))301 if choice == 0:302 self._read("Returning to main menu\n")303 else:304 del user.friends[choice - 1]305 else:306 self.write("0\n")307 self._read("Returning to main menu\n")308 def view_my_posts(self):309 self.write("7\n")310 user = self.state['logged_in_user']311 self._read("Select a post id to view\n")312 self._read("0. Go Back\n")313 user.read_post_list(self._read)314 self._read("Selection: ")315 if user.posts:316 choice = random.randint(0, len(user.posts))317 self.write("%d\n" % (choice))318 if choice == 0:319 self._read("Returning to main menu\n")320 else:321 post = user.posts[choice - 1]322 post.read_post(self._read)323 else:324 self.write("0\n")325 self._read("Returning to main menu\n")326 def view_my_profile(self):327 self.write("8\n")328 user = self.state['logged_in_user']329 user.read_profile(self._read)330 pass331 def view_user_posts_in(self):332 self.write("9\n")333 self._read("Enter username: ")334 cur_user = self.state['logged_in_user']335 if self.users:336 user = self.users[random.randint(0, len(self.users) - 1)]337 else:338 self.write("\n")339 self._read("Bad Username\n")340 return341 self.write(user.username + "\n")342 self._read("Select a post id to view\n")343 self._read("0. Go Back\n")344 user.read_post_list(self._read)345 self._read("Selection: ")346 if user.posts:347 choice = random.randint(0, len(user.posts))348 self.write("%d\n" % (choice))349 if choice == 0:350 self._read("Returning to main menu\n")351 else:352 post = user.posts[choice - 1]353 post.read_post(self._read)354 if cur_user.username != user.username:355 self._read("Like Post?\n")356 self._read("1. Yes\n")357 self._read("2. No\n")358 self._read("Selection: ")359 if random.randint(0,1):360 self.write("1\n")361 post.add_like(cur_user, self._read)362 self._read("Liked!\n")363 else:364 self.write("2\n")365 else:366 self.write("0\n")367 self._read("Returning to main menu\n")368 def view_user_profile_in(self):369 self.write("10\n")370 self._read("Enter username: ")371 if self.users:372 user = self.users[random.randint(0, len(self.users) - 1)]373 else:374 self.write("\n")375 self._read("Bad Username\n")376 return377 self.write(user.username + "\n")378 user.read_profile(self._read)379 def logout(self):380 self.write("11\n")381 self.state['logged_in_user'] = None382 self._read("Logging Out\n")383 def quit(self):384 self.write('5\n')...
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!!