Best Python code snippet using behave
invoke_cleanup.py
Source:invoke_cleanup.py
...83 print("FAILURE in CLEANUP TASK: %s (GRACEFULLY-IGNORED)" % cleanup_task)84 failure_count += 185 if failure_count:86 print("CLEANUP TASKS: %d failure(s) occured" % failure_count)87def make_excluded(excluded, config_dir=None, workdir=None):88 workdir = workdir or Path.getcwd()89 config_dir = config_dir or workdir90 workdir = Path(workdir)91 config_dir = Path(config_dir)92 excluded2 = []93 for p in excluded:94 assert p, "REQUIRE: non-empty"95 p = Path(p)96 if p.isabs():97 excluded2.append(p.normpath())98 else:99 # -- RELATIVE PATH:100 # Described relative to config_dir.101 # Recompute it relative to current workdir.102 p = Path(config_dir)/p103 p = workdir.relpathto(p)104 excluded2.append(p.normpath())105 excluded2.append(p.abspath())106 return set(excluded2)107def is_directory_excluded(directory, excluded):108 directory = Path(directory).normpath()109 directory2 = directory.abspath()110 if (directory in excluded) or (directory2 in excluded):111 return True112 # -- OTHERWISE:113 return False114def cleanup_dirs(patterns, workdir=".", excluded=None,115 dry_run=False, verbose=False, show_skipped=False):116 """Remove directories (and their contents) recursively.117 Skips removal if directories does not exist.118 :param patterns: Directory name patterns, like "**/tmp*" (as list).119 :param workdir: Current work directory (default=".")120 :param dry_run: Dry-run mode indicator (as bool).121 """122 excluded = excluded or []123 excluded = set([Path(p) for p in excluded])124 show_skipped = show_skipped or verbose125 current_dir = Path(workdir)126 python_basedir = Path(Path(sys.executable).dirname()).joinpath("..").abspath()127 warn2_counter = 0128 for dir_pattern in patterns:129 for directory in path_glob(dir_pattern, current_dir):130 if is_directory_excluded(directory, excluded):131 print("SKIP-DIR: %s (excluded)" % directory)132 continue133 directory2 = directory.abspath()134 if sys.executable.startswith(directory2):135 # -- PROTECT VIRTUAL ENVIRONMENT (currently in use):136 # pylint: disable=line-too-long137 print("SKIP-SUICIDE: '%s' contains current python executable" % directory)138 continue139 elif directory2.startswith(python_basedir):140 # -- PROTECT VIRTUAL ENVIRONMENT (currently in use):141 # HINT: Limit noise in DIAGNOSTIC OUTPUT to X messages.142 if warn2_counter <= 4: # noqa143 print("SKIP-SUICIDE: '%s'" % directory)144 warn2_counter += 1145 continue146 if not directory.isdir():147 if show_skipped:148 print("RMTREE: %s (SKIPPED: Not a directory)" % directory)149 continue150 if dry_run:151 print("RMTREE: %s (dry-run)" % directory)152 else:153 try:154 # -- MAYBE: directory.rmtree(ignore_errors=True)155 print("RMTREE: %s" % directory)156 directory.rmtree_p()157 except OSError as e:158 print("RMTREE-FAILED: %s (for: %s)" % (e, directory))159def cleanup_files(patterns, workdir=".", dry_run=False, verbose=False, show_skipped=False):160 """Remove files or files selected by file patterns.161 Skips removal if file does not exist.162 :param patterns: File patterns, like "**/*.pyc" (as list).163 :param workdir: Current work directory (default=".")164 :param dry_run: Dry-run mode indicator (as bool).165 """166 show_skipped = show_skipped or verbose167 current_dir = Path(workdir)168 python_basedir = Path(Path(sys.executable).dirname()).joinpath("..").abspath()169 error_message = None170 error_count = 0171 for file_pattern in patterns:172 for file_ in path_glob(file_pattern, current_dir):173 if file_.abspath().startswith(python_basedir):174 # -- PROTECT VIRTUAL ENVIRONMENT (currently in use):175 continue176 if not file_.isfile():177 if show_skipped:178 print("REMOVE: %s (SKIPPED: Not a file)" % file_)179 continue180 if dry_run:181 print("REMOVE: %s (dry-run)" % file_)182 else:183 print("REMOVE: %s" % file_)184 try:185 file_.remove_p()186 except os.error as e:187 message = "%s: %s" % (e.__class__.__name__, e)188 print(message + " basedir: "+ python_basedir)189 error_count += 1190 if not error_message:191 error_message = message192 if False and error_message: # noqa193 class CleanupError(RuntimeError):194 pass195 raise CleanupError(error_message)196def path_glob(pattern, current_dir=None):197 """Use pathlib for ant-like patterns, like: "**/*.py"198 :param pattern: File/directory pattern to use (as string).199 :param current_dir: Current working directory (as Path, pathlib.Path, str)200 :return Resolved Path (as path.Path).201 """202 if not current_dir: # noqa203 current_dir = pathlib.Path.cwd()204 elif not isinstance(current_dir, pathlib.Path):205 # -- CASE: string, path.Path (string-like)206 current_dir = pathlib.Path(str(current_dir))207 pattern_path = Path(pattern)208 if pattern_path.isabs():209 # -- SPECIAL CASE: Path.glob() only supports relative-path(s) / pattern(s).210 if pattern_path.isdir():211 yield pattern_path212 return213 # -- HINT: OSError is no longer raised in pathlib2 or python35.pathlib214 # try:215 for p in current_dir.glob(pattern):216 yield Path(str(p))217 # except OSError as e:218 # # -- CORNER-CASE 1: x.glob(pattern) may fail with:219 # # OSError: [Errno 13] Permission denied: <filename>220 # # HINT: Directory lacks excutable permissions for traversal.221 # # -- CORNER-CASE 2: symlinked endless loop222 # # OSError: [Errno 62] Too many levels of symbolic links: <filename>223 # print("{0}: {1}".format(e.__class__.__name__, e))224# -----------------------------------------------------------------------------225# GENERIC CLEANUP TASKS:226# -----------------------------------------------------------------------------227@task(help={228 "workdir": "Directory to clean(up) (default: $CWD).",229 "verbose": "Enable verbose mode (default: OFF).",230})231def clean(ctx, workdir=".", verbose=False):232 """Cleanup temporary dirs/files to regain a clean state."""233 dry_run = ctx.config.run.dry234 config_dir = getattr(ctx.config, "config_dir", workdir)235 directories = list(ctx.config.cleanup.directories or [])236 directories.extend(ctx.config.cleanup.extra_directories or [])237 files = list(ctx.config.cleanup.files or [])238 files.extend(ctx.config.cleanup.extra_files or [])239 excluded_directories = list(ctx.config.cleanup.excluded_directories or [])240 excluded_directories = make_excluded(excluded_directories,241 config_dir=config_dir, workdir=".")242 # -- PERFORM CLEANUP:243 execute_cleanup_tasks(ctx, cleanup_tasks)244 cleanup_dirs(directories, workdir=workdir, excluded=excluded_directories,245 dry_run=dry_run, verbose=verbose)246 cleanup_files(files, workdir=workdir, dry_run=dry_run, verbose=verbose)247 # -- CONFIGURABLE EXTENSION-POINT:248 # use_cleanup_python = ctx.config.cleanup.use_cleanup_python or False249 # if use_cleanup_python:250 # clean_python(ctx)251@task(name="all", aliases=("distclean",),252 help={253 "workdir": "Directory to clean(up) (default: $CWD).",254 "verbose": "Enable verbose mode (default: OFF).",255})256def clean_all(ctx, workdir=".", verbose=False):257 """Clean up everything, even the precious stuff.258 NOTE: clean task is executed last.259 """260 dry_run = ctx.config.run.dry261 config_dir = getattr(ctx.config, "config_dir", workdir)262 directories = list(ctx.config.cleanup_all.directories or [])263 directories.extend(ctx.config.cleanup_all.extra_directories or [])264 files = list(ctx.config.cleanup_all.files or [])265 files.extend(ctx.config.cleanup_all.extra_files or [])266 excluded_directories = list(ctx.config.cleanup_all.excluded_directories or [])267 excluded_directories.extend(ctx.config.cleanup.excluded_directories or [])268 excluded_directories = make_excluded(excluded_directories,269 config_dir=config_dir, workdir=".")270 # -- PERFORM CLEANUP:271 # HINT: Remove now directories, files first before cleanup-tasks.272 cleanup_dirs(directories, workdir=workdir, excluded=excluded_directories,273 dry_run=dry_run, verbose=verbose)274 cleanup_files(files, workdir=workdir, dry_run=dry_run, verbose=verbose)275 execute_cleanup_tasks(ctx, cleanup_all_tasks)276 clean(ctx, workdir=workdir, verbose=verbose)277 # -- CONFIGURABLE EXTENSION-POINT:278 # use_cleanup_python1 = ctx.config.cleanup.use_cleanup_python or False279 # use_cleanup_python2 = ctx.config.cleanup_all.use_cleanup_python or False280 # if use_cleanup_python2 and not use_cleanup_python1:281 # clean_python(ctx)282@task(aliases=["python"])...
base.py
Source:base.py
...272 def __exit__(self, exc_type, exc_val, exc_tb):273 self.close()274DEFAULT_EXCLUDES = fnmatch.translate(".*")275DEFAULT_EXCLUDES_RE = re.compile(DEFAULT_EXCLUDES).match276def make_excluded(excludes=None):277 if excludes is None:278 return DEFAULT_EXCLUDES_RE279 patterns = [DEFAULT_EXCLUDES]280 patterns.extend(fnmatch.translated(x) for x in excludes)281 return re.compile("|".join(patterns)).match282def generate_paths(roots, excludes=None):283 """284 Walk set of paths in local filesystem, and for each file and directory generate a tuple of285 (is directory, absolute path, path relative root used to get to that file)286 """287 excluded = make_excluded(excludes)288 for root in roots:289 base = os.path.basename(root)290 if not excluded(base):291 is_dir = os.path.isdir(root)292 yield is_dir, root, base293 if is_dir:294 prefix_len = len(os.path.dirname(root))295 for dirpath, dirnames, filenames in os.walk(root, topdown=True, followlinks=True):296 relpath = dirpath[prefix_len:].strip('/')297 for is_dir, names in ((False, filenames), (True, dirnames)):298 for name in names:299 if not excluded(name):300 yield is_dir, os.path.join(dirpath, name), "%s/%s" % (relpath, name)301def filter_none_values(dict):...
choose_samples.py
Source:choose_samples.py
1#!/usr/bin/env python2from __future__ import (print_function)3import argparse4from operator import itemgetter5import os.path6import subprocess7import sys8import yaml9parser = argparse.ArgumentParser(description="Choose samples for bin reassembly")10parser.add_argument("-r", "--reads", type=str, help="Directory with reads")11parser.add_argument("-o", "--output", type=str, help="Output path")12parser.add_argument("prof", type=str, help="File with bin profiles")13parser.add_argument("-f", "--filtered", type=str, help="File with filtered bins")14parser.add_argument("--min-sample", type=float, default=4, help="Minimal coverage in a single sample to be used")15parser.add_argument("--min-total", type=float, default=1, help="Minimal total coverage of the bin to be reassembled")16args = parser.parse_args()17BINS = set()18if args.filtered:19 with open(args.filtered) as input:20 for line in input:21 bin = line.split()[0]22 BINS.add(bin)23prof_dict = dict()24make_excluded = True25excluded_dir = os.path.join(args.output, "excluded")26input = open(args.prof)27for line in input:28 exclude = False29 samples = []30 params = line.split()31 bin = params[0]32 profile = list(map(float, params[1:]))33 print("Profile of", bin, ":", profile)34 if BINS and bin not in BINS:35 print(bin, "was excluded from the reassembly")36 exclude = True37 total = 038 #Current strategy: choose all samples larger than soft threshold.39 #If there's not enough, use the lower hard threshold.40 #Sort samples by their abundancies41 weighted_profile = list((i, ab)42 for i, ab in enumerate(profile, start=1) if ab >= args.min_sample)43 weighted_profile.sort(key=itemgetter(1), reverse=True)44 for i, cov in weighted_profile:45 if not os.path.exists(os.path.join(args.reads, bin, "sample{}_1.fastq.gz".format(i))):46 print("WARNING: sample", i, "does not contain reads for", bin)47 continue48 total += cov49 samples.append(i)50 print("Chosen samples are", samples, "with total mean abundance", total)51 prof_dict[bin] = total52 if total < args.min_total:53 print(bin, "is too scarce; excluding from the reassembly")54 exclude = True55 config_dir = args.output56 if exclude:57 if make_excluded and not os.path.isdir(excluded_dir):58 os.mkdir(excluded_dir)59 make_excluded = False60 config_dir = excluded_dir61 config_path = os.path.join(config_dir, bin + ".info")62 print("Dumping config into", config_path)63 with open(config_path, "w") as out:64 print("total", sum(profile), file=out)65 for i, ab in enumerate(profile, start=1):66 line = "sample" + str(i)67 if i in samples:68 line = "+" + line...
complete_lp.py
Source:complete_lp.py
...22 else:23 fout.write('presc: ' + var + ' = 0\n')24 25 fout.write('binary\n' + ' '.join(vars))26def make_excluded(db):27 c = db.cursor()28 c.execute("""29select id from casteller where nickname in ('AE', 'Aina', 'Alaitz', 'Aleix', 'Alvarito', 'Berta', 'Eva', 'Joana', 'Joanet', 'Laia O', 'Lali', 'Marco', 'Martina', 'Montxi', 'Oriolet', 'Rafols', 'Rai', 'Santako', 'Stefano');30""")31 ans = []32 for row in c.fetchall():33 ans.append(int(row[0]))34 return ans35def make_segons(db):36 c = db.cursor()37 c.execute("""38select id from casteller where nickname in ('Abdul', 'Arnau', 'Quim');39""")40 ans = []41 for row in c.fetchall():42 ans.append(int(row[0]))43 print ans44 return ans45def complete_lp():46 db = get_db()47 excluded = make_excluded(db)48 prescribed = dict([(2, 79), (14, 80), (72, 81)]) # Abdul, Arnau, Quim as segons49 print prescribed50 print excluded 51 [castell_id_name, colla_id_name] = ['cvg.3de9f', 'cvg']52 complete_lp_impl(prescribed, excluded, castell_id_name, colla_id_name)53if __name__=="__main__":...
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!!