How to use data_as_string method in localstack

Best Python code snippet using localstack_python

ex8.py

Source:ex8.py Github

copy

Full Screen

1"""2Author: Vali Mikayilov3Matr.Nr.: K120370834Exercise 85"""67import numpy as np8import pandas as pd9import sys1011def count_bases_and_subsequence(data_as_string, subsequence):1213 # We can convert the subsequence string to lower case letters to make the14 # subsequence count case insensitive:15 subsequence = subsequence.lower() # this converts everything to lower case1617 # We could also convert data_as_string to lower case but this is less18 # memory efficient (we would duplicate the whole string in memory), so we19 # will use a different approach later.2021 # Split file content string into lines22 lines = data_as_string.split("\n") # alternative: data_as_string.splitlines()2324 # Dictionary to store the counts of individual bases in25 counts = dict(a=0, c=0, g=0, t=0)2627 # Our counter for the subsequence28 subsequence_count = 02930 # Split the subsequence string to a list where each element is a character31 subsequence_list = list(subsequence)3233 # Create a dummy-list which we will later fill with the most recent bases34 # to compare with subsequence_list. We will use None to initialize the35 # list elements. Later we will add None as element instead of a base if36 # the line is invalid.37 recent_bases_list = [None] * len(subsequence_list)3839 # Iterate over all lines40 for line in lines:41 if line == "% End of data": # End of data reached?42 # If end of data is reached, escape from loop43 break44 elif (len(line) == 0) or line.startswith('%'): # Invalid data line?45 # Add None to the end and remove first element for subsequence46 # comparison:47 recent_bases_list = recent_bases_list[1:] + [None]48 # If empty line or comment line, skip to next line49 continue5051 # Get values of columns in this line:52 info, base, quality = line.split(";")5354 if float(quality) < 0.08: # Quality too low?55 # Add None to the end and remove first element for subsequence56 # comparison:57 recent_bases_list = recent_bases_list[1:] + [None]58 # If quality < 0.05, skip to next line59 continue6061 # Remove leading and trailing whitespace characters, just in case62 base = base.strip()6364 # Make sure we are case insensitive65 base = base.lower()6667 # We could use if-elif-else conditions here to increase count-variables68 # for the individual bases. But since we use a dictionary for the69 # counts, we can just use the base itself as key:70 try:71 # Increase counter for the specific base72 counts[base] += 173 # Add the base to the end and remove first element74 recent_bases_list = recent_bases_list[1:] + [base]75 except KeyError: # Current Base does not exist in our dictionary keys76 # Add None to the end and remove first element for subsequence77 recent_bases_list = recent_bases_list[1:] + [None]78 # If the base entry was not in our dictionary keys, go to the next line79 continue8081 # If the recent bases match the subsequence, we increase the82 # subsequence count by 1:83 if recent_bases_list == subsequence_list:84 subsequence_count += 185 # Add None as last list character to avoid over-lapping matches86 # (this is not relevant for the grading but makes more sense in87 # this setting)88 recent_bases_list = recent_bases_list[1:] + [None]8990 # Return the counts91 return [subsequence_count, counts["a"],counts["c"],counts["g"],counts["t"]]92#ex5.py end93import glob94import os9596def get_hamsters(folderpath):97 found_files = glob.glob(os.path.join(folderpath, '**', '*.raw.seq'), recursive=True)98 found_files = sorted(found_files)99 for x in found_files:100 with open(x, 'r') as f:101 file_content = f.read()102 filename = os.path.basename(x)103 yield filename, file_content104#ex6.py end105import re106matches = ["% SeqHeadStart", "% SeqHeadEnd", "% End of data"]107matches_rest = ['ID:', 'Date:','Columns:']108def get_file_metadata(data_as_string):109 split_data = data_as_string.split('\n')110 enum_data = enumerate(split_data,0)111 #set the value of the % SeqHeadEnd to a and % End of data to b112 for x in enum_data:113 if x[1] == '% SeqHeadStart':114 a = x[0]115 if x[1] == '% SeqHeadEnd':116 b = x[0]117 if x[1] == '% End of data':118 c = x[0]119 #if the value has all the matches and End of data doesn't come after SeqHead then return an error120 if not all(x in data_as_string for x in matches):121 raise AttributeError(f"the data doesn't contain % SeqHeadStart, % SeqHeadEnd, % End of data")122 if c<b:123 raise AttributeError(f" the End of data doesn't come after SeqHead")124 if b<a:125 raise AttributeError(f" the SeqEnd doesn't come after SeqHead")126 header_slice = slice(0,b+1)127 header = split_data[header_slice]128 # raise attribute error if matches are not found129 if not all(x in data_as_string for x in matches_rest):130 raise AttributeError(f"AttributeError")131 #ID, date and column name132 id = re.findall(r'ID: (\S+)', data_as_string)[0]133 #raise error if the date is incorrect134 try:135 date = int(re.findall(r'Date: (\d+)', data_as_string)[0])136 except (IndexError) as ex:137 raise TypeError(f"TypeError this one")138 else:139 date = int(re.findall(r'Date: (\d+)', data_as_string)[0])140 columns = re.findall(r'Columns: (\S+)', data_as_string)[0]141 columns_split = columns.split(';')142143 for x in header:144 if not x.startswith('%') and not x == '':145 raise AttributeError(f"AttributeError")146 return [id,date]147148x=0149command_line_args = sys.argv150input_folder = command_line_args[1]151output_file = command_line_args[2]152subsequence = command_line_args[3]153file_reader = get_hamsters(input_folder)154arr = np.zeros(shape=(200, 5), dtype=np.float)155f = open(output_file, "a")156for filename, file_content in file_reader:157 data_as_string = file_content158 meta_data = get_file_metadata(data_as_string)159 i = meta_data[1]160 base_count = count_bases_and_subsequence(data_as_string,subsequence)161 arr[i][0] = arr[i][0] + float((base_count[0])/20)162 arr[i][1] = arr[i][1] + float((base_count[1])/20)163 arr[i][2] = arr[i][2] + float((base_count[2])/20)164 arr[i][3] = arr[i][3] + float((base_count[3])/20)165 arr[i][4] = arr[i][4] + float((base_count[4])/20)166 ...

Full Screen

Full Screen

ex7.py

Source:ex7.py Github

copy

Full Screen

1"""2Author: Vali Mikayilov3Matr.Nr.: K120370834Exercise 75"""6import re78matches = ["% SeqHeadStart", "% SeqHeadEnd", "% End of data"]9matches_rest = ['ID:', 'Date:','Columns:']10def get_file_metadata(data_as_string):11 split_data = data_as_string.split('\n')12 enum_data = enumerate(split_data,0)13 #set the value of the % SeqHeadEnd to a and % End of data to b14 for x in enum_data:15 if x[1] == '% SeqHeadStart':16 a = x[0]17 if x[1] == '% SeqHeadEnd':18 b = x[0]19 if x[1] == '% End of data':20 c = x[0]21 #if the value has all the matches and End of data doesn't come after SeqHead then return an error22 if not all(x in data_as_string for x in matches):23 raise AttributeError(f"the data doesn't contain % SeqHeadStart, % SeqHeadEnd, % End of data")24 if c<b:25 raise AttributeError(f" the End of data doesn't come after SeqHead")26 if b<a:27 raise AttributeError(f" the SeqEnd doesn't come after SeqHead")28 header_slice = slice(0,b+1)29 header = split_data[header_slice]30 # raise attribute error if matches are not found31 if not all(x in data_as_string for x in matches_rest):32 raise AttributeError(f"AttributeError")33 #ID, date and column name34 id = re.findall(r'ID: (\S+)', data_as_string)[0]35 #raise error if the date is incorrect36 try:37 date = int(re.findall(r'Date: (\d+)', data_as_string)[0])38 except (IndexError) as ex:39 raise TypeError(f"TypeError this one")40 else:41 date = int(re.findall(r'Date: (\d+)', data_as_string)[0])42 columns = re.findall(r'Columns: (\S+)', data_as_string)[0]43 columns_split = columns.split(';')4445 for x in header:46 if not x.startswith('%') and not x == '':47 raise AttributeError(f"AttributeError") ...

Full Screen

Full Screen

tcp_control_mode.py

Source:tcp_control_mode.py Github

copy

Full Screen

1import socket2import _thread3from venv.Experiment.constants import *4from venv.Experiment.arm_movement_control import ArmMotorControl5class TcpControlMode:6 def __init__(self):7 self.serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)8 self.serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)9 self.serversock.bind(ADDRESS)10 self.serversock.listen(5)11 self.arm = ArmMotorControl()12 def start(self):13 print('waiting for connection... listening on port', PORT)14 while 1:15 clientsock, address = self.serversock.accept()16 # print('...connected from:', address)17 _thread.start_new_thread(self.handler, (clientsock, address))18 @staticmethod19 def response(key):20 return 'Server response: ' + key21 def handler(self, clientsock, address):22 while 1:23 data_as_byte = clientsock.recv(BUFF)24 if not data_as_byte:25 break26 data_as_string = data_as_byte.decode("utf-8")27 data_as_string = str(data_as_string)28 if data_as_string == "ArmClockWise":29 self.arm.turn_clockwise(MOTOR_NAME)30 print("Clockwise sent")31 elif data_as_string == "ArmCounterClockWise":32 self.arm.turn_counter_clockwise(MOTOR_NAME)33 print("Counter clockwise sent")34 else:35 print(f"Received but not understood: {data_as_string}")36 if "close" == data_as_byte.rstrip():37 print("Closing ...")38 break # type 'close' on client console to close connection from the server side39 clientsock.close()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful