Best Python code snippet using hypothesis
word_count_1.py
Source:word_count_1.py
1#####********************************************************************************#####2##### Functions and Dictionaries #####3#####********************************************************************************#####4import itertools5nums_dict = { '1' : {(0, 2) : 'one', (1,) : {'0' : 'ten',6 '1' : 'eleven',7 '2' : 'twelve',8 '3' : 'thirteen',9 '4' : 'fourteen',10 '5' : 'fifteen',11 '6' : 'sixteen',12 '7' : 'seventeen',13 '8' : 'eighteen',14 '9' : 'ninteen',}},15 '2' : {(0, 2) : 'two', (1,) : 'twenty'},16 '3' : {(0, 2) : 'three', (1,) : 'thirty'},17 '4' : {(0, 2) : 'four', (1,) : 'fourty'},18 '5' : {(0, 2) : 'five', (1,) : 'fifty'},19 '6' : {(0, 2) : 'six', (1,) : 'sixty'},20 '7' : {(0, 2) : 'seven', (1,) : 'seventy'},21 '8' : {(0, 2) : 'eight', (1,) : 'eighty'},22 '9' : {(0, 2) : 'nine', (1,) : 'ninty'}23 }24def get_number(message1, message2):25 """26 message1 : string27 message2 : string28 return : string29 Returns user input string if capable of being cast to integer, and between minus30 2 billon and positive 2 billion, else self.31 """32 user_input = raw_input(message1)33 try:34 if int(user_input) < -2000000000 or int(user_input) > 2000000000:35 print message236 return get_number(message1, message2)37 except ValueError:38 print 'That was not valid input'39 return get_number(message1, message2) 40 return user_input41def zero_padding(user_input):42 """43 user_input : string44 return : string45 Returns user input stripped of a minus sign (if present) and padded to the extent46 necessary with zeros to ensure that the returned string is 12 characters in length.47 """48 if user_input[0] == '-':49 user_input = user_input[1:]50 modified_input = ('0'*(12 - len(user_input))) + user_input51 return modified_input 52def convert_to_tuple_list(modified_input):53 """54 modified_input : string55 return : tuple56 Returns tuple with four elements, each a tuple with three elements.57 Assumes modified_input has length 12.58 """59 tuple_list = tuple((tuple(modified_input[x:x+3]) for x in xrange(0, 10, 3)))60 return tuple_list61def tuple_to_text(single_tuple, unit_string, nums_dict = nums_dict):62 """63 single_tuple : tuple 64 unit_string : string65 nums_dict : dict66 return : list67 Returns list of alpha strings that represent text of numerical string characters found 68 in single_tuple. The final element of the list is the unit_sting.69 """70 word_list = [[],[]]71 if ''.join(single_tuple) == '000': # if all characters are '0' return empty list72 return list(itertools.chain(*word_list))73 if single_tuple[0] != '0': # if the fist element of the tuple is not '0'74 word_list[0].extend([nums_dict[single_tuple[0]][(0, 2)], 'hundred'])75 if single_tuple[1] != '0': # if the second element of the tuple is not '0'76 if single_tuple[1] == '1': # Special case where second character is '1'77 word_list[1].extend(['and', nums_dict['1'][(1,)][single_tuple[2]], unit_string])78 else:79 try: #if third element is zero then this will generate an error below as zero80 #is not in the nums_dict. 81 word_list[1].extend(['and', nums_dict[single_tuple[1]][(1,)], 82 nums_dict[single_tuple[2]][(0, 2)], unit_string])83 except KeyError: 84 word_list[1].extend(['and', nums_dict[single_tuple[1]][(1,)], unit_string]) 85 else:86 if single_tuple[2] != '0': # if first element of tuple is zero but the second is not87 word_list[1].extend(['and', nums_dict[single_tuple[2]][(0, 2)], unit_string])88 else:89 word_list[1].append(unit_string)90 if len(word_list[0]) == 0: # if no 'hundreds' then remove 'and'91 word_list[1].remove('and')92 return list(itertools.chain(*word_list))93def create_text_representation(tuple_list):94 """95 tuple_list : tuple96 return : string97 Returns string of words found in each list created by calling the tuple_to_text98 function.99 """ 100 list1 = tuple_to_text(tuple_list[0], 'billion')101 list2 = tuple_to_text(tuple_list[1], 'million')102 list3 = tuple_to_text(tuple_list[2], 'thousand')103 list4 = tuple_to_text(tuple_list[3], '')104 #If any of the lists 1/2/3 are not empty, but list4 contains no hundred value, 105 #insert an 'and' into list4 at index position 1 if tuple_list[3] does not contain106 #elements all of which are equal to '0'107 if any([len(list1) != 0, len(list2) != 0, len(list3) != 0])\108 and 'hundred' not in list4 and ''.join(tuple_list[3]) != "000":109 list4.insert(0, 'and')110 complete_list = itertools.chain(*[list1, list2, list3, list4])111 complete_list = [elem for elem in complete_list if not type(elem) is list]112 return " ".join(complete_list)113def message(user_input, text_representation):114 """115 user_input : string of numerical characters (possible including the minus sign)116 text_representation : string of alphas117 return : formatted string118 Returns string formatted to include 'minus' where necessary, the original number119 provided, and the textual representation of that number.120 """121 message = \122 """123 The number {0} written as text is : 124 {1}{2}125 """126 if user_input[0] == '-':127 return message.format(user_input, 'minus ', text_representation)128 return message.format(user_input, '', text_representation)129#####********************************************************************************#####130##### Run Method #####131#####********************************************************************************##### 132user_input = get_number("Please enter a number between -2 billion and 2 billion: ",133 "That number is out of range")134modified_input = zero_padding(user_input)135tuple_list = convert_to_tuple_list(modified_input)136text_representation = create_text_representation(tuple_list)...
pratice 2 add.py
Source:pratice 2 add.py
1Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win322Type "copyright", "credits" or "license()" for more information.3>>> sample_list1 = ['eggs', 'butter', 'flour', 'bread', 'cheese']4>>> sample_list15['eggs', 'butter', 'flour', 'bread', 'cheese']6>>> sample_list2 = list([1, 'drink', 10, 'sandwiches', 0.45e-2])7>>> sample_list28[1, 'drink', 10, 'sandwiches', 0.0045]9>>> sample_list1, sample_list210(['eggs', 'butter', 'flour', 'bread', 'cheese'], [1, 'drink', 10, 'sandwiches', 0.0045])11>>> sample_list1[0]12'eggs'13>>> sample_list1[1]14'butter'15>>> sample_list1[0] + ' ' + sample_list1[1]16'eggs butter'17>>> sample_list1[1:3]18['butter', 'flour']19>>> sample_list2[1:3]20['drink', 10]21>>> numbers = list(range(10))22>>> numbers23[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]24>>> numbers[2:5]25[2, 3, 4]26>>> numbers[:]27[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]28>>> numbers[::2]29[0, 2, 4, 6, 8]30>>> numbers * 231[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]32>>> numbers + sample_list233[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 'drink', 10, 'sandwiches', 0.0045]34>>> sample_list3 = [1, 2, 3, ['a', 'b', 'c'], ['Hello', 'Python']]35>>> sample_list336[1, 2, 3, ['a', 'b', 'c'], ['Hello', 'Python']]37>>> sample_list3[3]38['a', 'b', 'c']39>>> sample_list3[4]40['Hello', 'Python']41>>> sample_list3.append(' '.join(sample_list3[4]))42>>> sample_list343[1, 2, 3, ['a', 'b', 'c'], ['Hello', 'Python'], 'Hello Python']44>>> sample_list3.pop(3)45['a', 'b', 'c']46>>> sample_list3.pop(2)47348>>> sample_list349[1, 2, ['Hello', 'Python'], 'Hello Python']50>>> sample_list3.pop(2)51['Hello', 'Python']52>>> sample_list353[1, 2, 'Hello Python']54>>> sample_list3.insert(2, 3)55>>> sample_list356[1, 2, 3, 'Hello Python']57>>> sample_list3.insert(3, ['a', 'b', 'c'])58>>> sample_list359[1, 2, 3, ['a', 'b', 'c'], 'Hello Python']60>>> sample_list3.pop(4)61'Hello Python'62>>> sample_list3.insert(4,['Hello', 'Python'])63>>> sample_list364[1, 2, 3, ['a', 'b', 'c'], ['Hello', 'Python']]65>>> list1 = [1, 1, 2, 3, 5, 5, 7, 9, 1]66>>> list167[1, 1, 2, 3, 5, 5, 7, 9, 1]68>>> set1 = set(list1)69>>> set170set([1, 2, 3, 5, 7, 9])71>>> 1 in set172True73>>> 100 in set174False75>>> set1 = {1, 2, 3, 5, 7}76>>> set2 = {5, 7, 11}77>>> set1 | set278set([1, 2, 3, 5, 7, 11])79>>> set1 & set280set([5, 7])81>>> set1 - set282set([1, 2, 3])83>>> set1 ^ set284set([1, 2, 3, 11])85>>> dic1 = {1:'egg', 2:'ham', 3: 'milk'}86>>> dic187{1: 'egg', 2: 'ham', 3: 'milk'}88>>> dic1.get(2)89'ham'90>>> dic1[3]91'milk'92>>> dic1 = {1:'egg', 2:'ham', 3: 'milk', 4:'egg'}93>>> dic194{1: 'egg', 2: 'ham', 3: 'milk', 4: 'egg'}95>>> dic1 = {1:'egg', 2:'ham', 3: 'milk', 4:'egg', 3:'cheese'}96>>> dic197{1: 'egg', 2: 'ham', 3: 'cheese', 4: 'egg'}98>>> dic1.get(5)99>>> 100>>> dic1[5]101102Traceback (most recent call last):103 File "<pyshell#57>", line 1, in <module>104 dic1[5]105KeyError: 5106>>> dic2 = {'egg':2, 'milk':4, 'spam':10, 'ham':15}107>>> dic2108{'ham': 15, 'egg': 2, 'milk': 4, 'spam': 10}109>>> dic2 = {'egg':2, 'milk':4, 'spam':10, 'ham':15, 'milk':9}110>>> dic2111{'ham': 15, 'egg': 2, 'milk': 9, 'spam': 10}112>>> dic2.get('egg')1132114>>> dic2['egg']1152116>>> dic1 = {1:'egg', 2:'ham', 3: 'milk'}117>>> dic1118{1: 'egg', 2: 'ham', 3: 'milk'}119>>> dic1[4] = 'cheese'120>>> dic1121{1: 'egg', 2: 'ham', 3: 'milk', 4: 'cheese'}122>>> dic1.keys()123[1, 2, 3, 4]124>>> dic1.values()125['egg', 'ham', 'milk', 'cheese']126>>> dic3 = {'orange':5, 'melon':17, 'banana':10}127>>> dic3128{'orange': 5, 'melon': 17, 'banana': 10}129>>> dic3.items()130[('orange', 5), ('melon', 17), ('banana', 10)]131>>> sorted(dic3.items())132[('banana', 10), ('melon', 17), ('orange', 5)]133>>> dic4 = {1:'egg', 2:'ham', 3: 'milk'}134>>> dic5 = {5:'orange', 17:'melon', 10:'banana'}135>>> dic4.update(dic5)136>>> dic4137{1: 'egg', 2: 'ham', 3: 'milk', 5: 'orange', 17: 'melon', 10: 'banana'}138>>> dic6 = {'k1':5, 'k2':[1,2,3,4,5], 'k3':{'a':1, 'b':2, 'c':[1,2,3]}}139>>> dic6140{'k3': {'a': 1, 'c': [1, 2, 3], 'b': 2}, 'k2': [1, 2, 3, 4, 5], 'k1': 5}141>>> dic6.get('k3')142{'a': 1, 'c': [1, 2, 3], 'b': 2}143>>> dic6.get('k3').get('c')144[1, 2, 3]145>>> dic6.get('k3').get('c')[1]1462147>>> single_tuple = (1,)148>>> single_tuple149(1,)150>>> single_tuple = single_tuple + (2,3,4,5)151>>> single_tuple152(1, 2, 3, 4, 5)153>>> single_tuple[3]1544155>>> single_tuple[3] = 100156157Traceback (most recent call last):158 File "<pyshell#88>", line 1, in <module>159 single_tuple[3] = 100160TypeError: 'tuple' object does not support item assignment161>>> tup = (['this', 'is', 'list','1'], ['this', 'is', 'list', '2'])162>>> tup[0]163['this', 'is', 'list', '1']164>>> tup[1]165['this', 'is', 'list', '2']166>>> list1, list2 = tup167>>> list1168['this', 'is', 'list', '1']169>>> list2170['this', 'is', 'list', '2']171>>> list1[3]172'1'173>>> tup[0][3]174'1'
...
data.py
Source:data.py
1from openpyxl import load_workbook, Workbook2from typing import Dict3from datamaps.process import Cleanser4import datetime5from datetime import date6from collections import OrderedDict7from pathlib import Path8import platform9def _platform_docs_dir() -> Path:10 # Cross plaform file path handling11 if platform.system() == "Linux":12 return Path.home() / "Documents" / "ppdd_engagement_db"13 if platform.system() == "Darwin":14 return Path.home() / "Documents" / "ppdd_engagement_db"15 else:16 return Path.home() / "Documents" / "ppdd_engagement_db"17root_path = _platform_docs_dir()18org_dict = {19 "DfT(c)": "",20 "Highways England": "HE",21 "HS2 Ltd": "HS2",22 "Network Rail": "NR",23 "IPA": "IPA",24 "DEFRA": "DEF",25 "MCA": "MCA",26}27def handle_nones(data):28 if data is None: # might have to change None to NULL29 return ''30 else:31 return data32def handle_nulls(data):33 if data is None:34 return 'NULL'35 else:36 return data37def calculate_stakeholder_id(single_tuple, id_list):38 # calculate a composite stakeholder ID39 first_initial = single_tuple[1][0]40 second_initial = single_tuple[2][0]41 org = org_dict[single_tuple[4]]42 group = handle_nones(single_tuple[5])43 stakeholder_id = first_initial + second_initial + org + group44 if stakeholder_id not in id_list:45 single_tuple.insert(0, stakeholder_id)46 id_list.append(stakeholder_id)47 else:48 print(stakeholder_id)49 pass50 return id_list51def calculate_entity_id(single_tuple, id_list):52 # calculate a composite stakeholder ID53 type = single_tuple[1][:4].upper()54 abb = single_tuple[3]55 stakeholder_id = abb + type56 if stakeholder_id not in id_list:57 single_tuple.insert(0, stakeholder_id)58 id_list.append(stakeholder_id)59 else:60 print(stakeholder_id)61 pass62 return single_tuple63def get_data(master_file: str, ws_name: str, last_col: int, last_row: int) -> Dict:64 wb = load_workbook(master_file)65 ws = wb[ws_name]66 full_list = []67 id_list = []68 for row in range(2, last_row):69 single_tuple = [] # starts life as a list70 for col in range(1, last_col):71 data = ws.cell(row=row, column=col).value72 if isinstance(data, datetime.datetime):73 data = data.date()74 # if col == 2:75 # if len(data) == 4:76 # single_tuple.append("ENG" + data[-1])77 # if len(data) == 5:78 # single_tuple.append("ENG" + data[-2:])79 # # single_tuple.append("ENG" + str(data))80 # else:81 single_tuple.append(data)82 # calculate_entity_id(single_tuple, id_list)83 full_list.append(tuple(single_tuple))84 return full_list85def place_data_excel(s_data):86 wb = Workbook()87 ws = wb.active88 for x, t in enumerate(s_data):89 for i, d in enumerate(t):90 ws.cell(row=x+1, column=i+1).value = d91 return wb92# stakeholder_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "Stakeholders", 9, 60)93# project_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "Projects", 7)94# ppdd_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "PPDDs", 7, 17)95engagement_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "Engagements", 6, 81)96# ps_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "ProjectStakeholders", 4)97# p_engagements_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "ProjectEngagements", 4)98# engagements_s_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "EngagementStakeholders", 4)99# engagements_ppdd_data = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "EngagementPPDDs", 4)100# engagement_types = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "EngagementTypes", 4)101# engagement_ws = get_data(root_path / "ppdd_engagement_db_tables.xlsx", "EngagementWorkStreams", 4)102# wb = place_data_excel(engagement_ws)...
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!!