How to use print_to_tty method in autotest

Best Python code snippet using autotest_python

gref.py

Source:gref.py Github

copy

Full Screen

1#! /usr/bin/env python32# Search a FASTA or FASTQ file for a sequence that matches a pattern. The3# pattern can be in the defline or the sequence letters. Inspired by the 4# 'gref' script in the SEALS package.5# John Conery6# University of Oregon7# 2015-08-228# TBD: 2nd arg optional; if not supplied, read from stdin9import argparse10import os11import sys12import re13from FASTA import *14from FASTQ import *15# When searching a DNA file the script can use a regular expressions based on IUPAC16# ambiguity letter to match the sequence17iupac_map = {18 'A' : r'A', 19 'C' : r'C', 20 'G' : r'G', 21 'T' : r'T',22 'R' : r'[AG]', 23 'Y' : r'[CT]',24 'S' : r'[GC]',25 'W' : r'[AT]',26 'K' : r'[GT]',27 'M' : r'[AC]',28 'B' : r'[CGT]',29 'D' : r'[AGT]',30 'H' : r'[ACT]',31 'V' : r'[ACG]',32 'N' : r'[ACGT]'33}34def iupac_regexp(pat):35 'Create a regular expression for a sequence, replacing IUPAC letters with groups'36 return ''.join(map(lambda ch: iupac_map[ch], pat))37 38complement = {39 'A' : 'T', 40 'T' : 'A', 41 'C' : 'G',42 'G' : 'C',43 'R' : r'[TC]', 44 'Y' : r'[GA]',45 'S' : r'[AT]',46 'W' : r'[GC]',47 'K' : r'[CA]',48 'M' : r'[TG]',49 'B' : r'.',50 'D' : r'.',51 'H' : r'.',52 'V' : r'.',53 'N' : r'.'54}55def reverse_complement(pat):56 return ''.join(map(lambda ch: complement[ch], reversed(pat)))57###58# Parse command line arguments...59def init_api():60 parser = argparse.ArgumentParser(61 description="""Find sequences that match a pattern in FASTA or FASTQ files. If the --seq option62is used the program looks for the pattern in sequence characters, otherwise it looks for the pattern in63deflines. Patterns are specified using PERL regular expression syntax. 64If the inputs are named files the program will infer the sequence type from the filename extension 65(.fa, .fn, or .fasta for FASTA files, .fastq for FASTQ files). If the input is coming from stdin the 66sequence type must be defined with the --fasta or --fastq option. 67"""68)69 parser.add_argument('pattern', help='pattern to find')70 parser.add_argument('files', nargs='*', help='name(s) of sequence file(s)')71 parser.add_argument('-s', '--sequence', action='store_true', help='match sequence characters')72 parser.add_argument('-i', '--iupac', action='store_true', help='pattern contains IUPAC ambiguity letters')73 parser.add_argument('-v', action='store_true', help='print sequences that do not match the pattern')74 parser.add_argument('-r', '--reverse', action='store_true', help='use the reverse complement of the pattern')75 parser.add_argument('--fasta', action='store_true', help='input stream is in FASTA format')76 parser.add_argument('--fastq', action='store_true', help='input stream is in FASTQ format')77 return parser.parse_args()78reader_type = {79 '.fa' : FASTAReader,80 '.fn' : FASTAReader,81 '.fasta' : FASTAReader,82 '.fastq' : FASTQReader,83}84# The string %s inserted into this pattern will be printed in red (or the terminal's ANSI color #1)85ESC = chr(27)86highlight_region = ESC + '[1;31m' + '%s' + ESC + '[0m'87###88# Check the command line arguments89def validate(args):90 # if there are no file names we need either --fasta or --fastq91 if len(args.files) == 0:92 if bool(args.fasta) == bool(args.fastq):93 argparse.ArgumentParser.exit(1, 'specify either --fasta or --fastq when reading from stdin')94 # all file names need to end in a recognized extension95 for fn in args.files:96 if os.path.splitext(fn)[1] not in reader_type:97 argparse.ArgumentParser.exit(1, 'invalid filename extension: ' + fn)98###99# Process a single file100def scan_file(file, pattern, tty, args):101 for seq in file:102 src = seq.sequence() if args.sequence else seq.defline()103 match = re.search(pattern, src)104 if match is not None and not args.v:105 res = repr(seq)106 hit = match.group()107 if print_to_tty:108 res = re.sub(hit, highlight_region % hit, res) 109 print(res)110 elif match is None and args.v:111 print(repr(seq))112 113###114# Main program:115 116if __name__ == "__main__":117 args = init_api()118 validate(args)119 120 if args.iupac:121 pattern = iupac_regexp(args.pattern) 122 elif args.reverse:123 pattern = reverse_complement(args.pattern)124 else:125 pattern = args.pattern126 127 print_to_tty = os.isatty(sys.stdout.fileno())128 129 if len(args.files) == 0:130 reader = FASTAReader(sys.stdin) if args.fasta else FASTQReader(sys.stdin)131 scan_file(reader, pattern, print_to_tty, args)132 else:133 for fn in args.files:134 ext = os.path.splitext(fn)[1]135 cls = reader_type.get(ext)136 scan_file(cls(fn), pattern, print_to_tty, args)...

Full Screen

Full Screen

test_printer.py

Source:test_printer.py Github

copy

Full Screen

...37]38@pytest.fixture39def printer():40 return ErrorPrinter(files=TEST_FILES, issues=TEST_ISSUES)41def test_print_to_tty(capfd, printer):42 expected_stdout = '\n'.join([43 'In /bad_file line 10:',44 'foo',45 ' ^-- WSC001: Bad line ending \'\\r\\n\'',46 '',47 'In /bad_file line 187:',48 'exit 0',49 ' ^-- WSC005: No newline at end of file',50 '',51 'In /bad_file line 188:',52 'echo \'foo--->bar\' ',53 ' ^-- WSC002: Tailing whitespace',54 ''55 ])56 printer.print_to_tty(False)57 out, err = capfd.readouterr()58 assert '' == err59 assert expected_stdout == out60def test_colored_print_to_tty(capfd, printer):61 bold = '\033[1m'62 yellow = '\033[33m'63 reset = '\033[0m'64 expected_stdout = '\n'.join([65 bold + 'In /bad_file line 10:' + reset,66 'foo',67 ' ' + yellow + '^-- WSC001: Bad line ending \'\\r\\n\'' + reset,68 '',69 bold + 'In /bad_file line 187:' + reset,70 'exit 0',71 ' ' + yellow + '^-- WSC005: No newline at end of file' + reset,72 '',73 bold + 'In /bad_file line 188:' + reset,74 'echo \'foo--->bar\' ',75 ' ' + yellow + '^-- WSC002: Tailing whitespace' + reset,76 ''77 ])78 printer.print_to_tty(True)79 out, err = capfd.readouterr()80 assert '' == err81 assert expected_stdout == out82def assert_equal_xml(generated_xml, expected_xml):83 """84 :type generated_xml: str85 :type expected_xml: str86 """87 __tracebackhide__ = True # noqa88 differences = []89 def append_to_messages(message):90 """91 :type message: str92 """...

Full Screen

Full Screen

async-main.py

Source:async-main.py Github

copy

Full Screen

...9parser.add_argument('--server', default=False, action='store_true')10parser.add_argument('--ip', default='127.0.0.1', type=str)11parser.add_argument('--port', default=5555, type=int)12args = parser.parse_args()13async def print_to_tty(reader: asyncio.StreamReader):14 while True:15 data = await reader.read(1024)16 if data:17 os.write(sys.stdout.fileno(), data)18 else:19 # cannot stop another coroutine20 # so just exit -_-21 sys.exit()22def blocking_io():23 return os.read(sys.stdin.fileno(), 1024)24 25async def read_from_tty(writer: asyncio.StreamWriter):26 loop = asyncio.get_running_loop()27 while True:28 with concurrent.futures.ThreadPoolExecutor() as pool:29 data = await loop.run_in_executor(30 pool, blocking_io)31 if data:32 writer.write(data)33 await writer.drain()34 else:35 writer.close()36 return37async def handle_netcat_reqeust(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):38 await asyncio.gather(39 print_to_tty(reader),40 read_from_tty(writer),41 )42async def main():43 if args.server == True:44 server = await asyncio.start_server(handle_netcat_reqeust, args.ip, args.port)45 async with server:46 await server.serve_forever()47 else:48 reader, writer = await asyncio.open_connection(args.ip, args.port)49 await asyncio.gather(50 print_to_tty(reader),51 read_from_tty(writer),52 )...

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 autotest 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