Best Python code snippet using localstack_python
test_filesystems.py
Source:test_filesystems.py
1import pytest2from lume_services.services.files.filesystems import (3 MountedFilesystem,4 LocalFilesystem,5 PathNotInMount,6)7from lume_services.files.serializers.text import TextSerializer8@pytest.fixture(scope="module", autouse=True)9def text_serializer():10 return TextSerializer()11class TestLocalFilesystem:12 @pytest.fixture(scope="class")13 def test_local_filesystem(self):14 return LocalFilesystem()15 def test_local_filesystem_dir_exists(self, test_local_filesystem, tmp_path):16 assert test_local_filesystem.dir_exists(tmp_path, create_dir=False)17 def test_local_filesystem_create_dir(self, test_local_filesystem, tmp_path):18 new_tmp_dir = f"{tmp_path}/placeholder_dr"19 test_local_filesystem.create_dir(new_tmp_dir)20 assert test_local_filesystem.dir_exists(new_tmp_dir, create_dir=False)21 def test_local_filesystem_dir_exist_create(self, test_local_filesystem, tmp_path):22 new_tmp_dir = f"{tmp_path}/placeholder_dr"23 # create dir24 assert test_local_filesystem.dir_exists(new_tmp_dir, create_dir=True)25 assert test_local_filesystem.dir_exists(new_tmp_dir, create_dir=False)26 def test_local_filesystem_read_write(27 self, test_local_filesystem, tmp_path, text_serializer28 ):29 tmp_file = f"{tmp_path}/test.txt"30 text = "test text"31 # write32 test_local_filesystem.write(tmp_file, text, text_serializer)33 assert test_local_filesystem.file_exists(tmp_file)34 # read35 new_text = test_local_filesystem.read(tmp_file, text_serializer)36 assert new_text == text37 def test_local_filesystem_create_dir_on_write(38 self, test_local_filesystem, tmp_path, text_serializer39 ):40 tmp_file = f"{tmp_path}/tmp_dir2/test.txt"41 text = "test text"42 # fail on no directory creation43 with pytest.raises(FileNotFoundError):44 test_local_filesystem.write(45 tmp_file, text, text_serializer, create_dir=False46 )47 # succeed on creation48 test_local_filesystem.write(tmp_file, text, text_serializer, create_dir=True)49class TestMountedFilesystem:50 @pytest.fixture()51 def test_mounted_filesystem(self, tmp_path, fs):52 return MountedFilesystem(mount_alias="/test_base", mount_path=str(tmp_path))53 def test_mounted_filesystem_dir_does_not_exist(self, test_mounted_filesystem, fs):54 assert not test_mounted_filesystem.dir_exists(55 test_mounted_filesystem.mount_alias, create_dir=False56 )57 def test_mounted_filesystem_dir_does_not_exist_on_file_read(58 self, test_mounted_filesystem, text_serializer, fs59 ):60 tmp_file = "/some_unknown_dir/test.txt"61 text = "test text"62 # write63 with pytest.raises(PathNotInMount):64 test_mounted_filesystem.write(65 tmp_file, text, text_serializer, create_dir=False66 )67 def test_mounted_filesystem_dir_exists(self, test_mounted_filesystem):68 assert test_mounted_filesystem.dir_exists(69 test_mounted_filesystem.mount_alias, create_dir=True70 )71 def test_mounted_filesystem_create_dir(self, test_mounted_filesystem):72 new_tmp_dir = f"{test_mounted_filesystem.mount_alias}/placeholder_dir"73 test_mounted_filesystem.create_dir(new_tmp_dir)74 assert test_mounted_filesystem.dir_exists(new_tmp_dir, create_dir=False)75 def test_mounted_filesystem_dir_exist_create(self, test_mounted_filesystem):76 new_tmp_dir = f"{test_mounted_filesystem.mount_alias}/placeholder_dir"77 # create dir78 assert test_mounted_filesystem.dir_exists(new_tmp_dir, create_dir=True)79 assert test_mounted_filesystem.dir_exists(new_tmp_dir, create_dir=False)80 def test_mounted_filesystem_read_write(81 self, test_mounted_filesystem, text_serializer82 ):83 tmp_file = f"{test_mounted_filesystem.mount_alias}/test.txt"84 text = "test text"85 # write86 test_mounted_filesystem.write(tmp_file, text, text_serializer, create_dir=True)87 assert test_mounted_filesystem.file_exists(tmp_file)88 # read89 new_text = test_mounted_filesystem.read(tmp_file, text_serializer)90 assert new_text == text91 def test_mounted_filesystem_create_dir_on_write(92 self, test_mounted_filesystem, text_serializer93 ):94 tmp_file = f"{test_mounted_filesystem.mount_alias}/tmp_dir2/test.txt"95 text = "test text"96 # fail on no directory creation97 with pytest.raises(FileNotFoundError):98 test_mounted_filesystem.write(99 tmp_file, text, text_serializer, create_dir=False100 )101 # succeed on creation...
segmentation.py
Source:segmentation.py
1from collections import deque2from itertools import islice3import numpy as np4from scipy.io.wavfile import read, write5import os678class Segmentation:910 def __init__(self):11 self.Y = []12 self.metas = []13 self.new_tmp_dirs = []1415 def segment_all(self, data, window_size, overlap, tmp_dir_id):16 """17 Call segmentation function on all audio clips1819 :param data: input data20 :param window_size: window size21 :param overlap: overlap22 :param tmp_dir_id: hash of temporary directory23 :return: segmented data24 """2526 for i in range(len(data.metas)):27 if os.path.isfile(data.metas[i][1]):28 filename = data.metas[i][1]29 else:30 filename = data.metas[i][1].split(".wav")[0]31 framerate, X = read(filename)32 if len(X.shape) > 1:33 X = X[:, 0]34 self.segmentation(X, data.metas[i], data.Y[i], window_size, overlap, tmp_dir_id)3536 return np.array(self.Y), self.metas, self.new_tmp_dirs3738 def segmentation(self, data, metas, target_class, window_size, overlap, tmp_dir_id):39 """40 Call sliding_window function to segment audio clip and temporarily write them in the specified folder4142 :param data: data for one sound clip43 :param metas: meta data44 :param target_class: sound clip category45 :param window_size: window size46 :param overlap: overlap47 :param tmp_dir_id: hash of temporary directory48 :return: Void -> append data to variables declared in __init__49 """5051 if window_size == "" or window_size == "0":52 window_size = 153 if overlap == "" or overlap == "0":54 overlap = 155 window_size = int(metas[-1] * float(window_size))56 overlap = int(metas[-1] * float(overlap))5758 segments = self.sliding_window(data, window_size, overlap)59 counter = 16061 for i in segments:62 c = []63 for x in i:64 c.append(x)65 tmp = [x for x in c if x != None and x != 0]66 if len(tmp) == 0:67 continue68 array_len = len(c)69 if c.count(None) > 0:70 c = [0 if x is None else x for x in c]71 elif c.count(0) >= array_len / 2:72 continue7374 new_tmp_dir = os.path.dirname(metas[1]) + os.sep + "segmented-" + tmp_dir_id + os.sep75 if not os.path.exists(new_tmp_dir):76 os.makedirs(new_tmp_dir)77 self.new_tmp_dirs.append(new_tmp_dir)7879 filename = new_tmp_dir + metas[0] + "_" + str(counter) + ".wav"80 write(filename, metas[-1], np.array(c))8182 self.metas.append([metas[0] + "_" + str(counter), filename, window_size, float(window_size) / float(metas[-1]), metas[-1]])83 self.Y.append(target_class)8485 counter += 18687 def sliding_window(self, iterable, size=2, step=1, fillvalue=None):88 """89 Implemented sliding window algorithm9091 :param iterable: data92 :param size: window size93 :param step: overlap94 :param fillvalue: fill value95 :return: iterator of segmented audio data96 """9798 if size < 0 or step < 1:99 raise ValueError100 it = iter(iterable)101 q = deque(islice(it, size), maxlen=size)102 if not q:103 return # empty iterable or size == 0104 q.extend(fillvalue for _ in range(size - len(q))) # pad to size105 while True:106 yield iter(q) # iter() to avoid accidental outside modifications107 try:108 q.append(next(it))109 except StopIteration: # Python 3.5 pep 479 support110 return
...
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!!