How to use readline method in fMBT

Best Python code snippet using fMBT_python

test_readline.py

Source:test_readline.py Github

copy

Full Screen

1"""2Very minimal unittests for parts of the readline module.3"""4from contextlib import ExitStack5from errno import EIO6import os7import selectors8import subprocess9import sys10import tempfile11import unittest12from test.support import import_module, unlink, TESTFN13from test.support.script_helper import assert_python_ok14# Skip tests if there is no readline module15readline = import_module('readline')16is_editline = readline.__doc__ and "libedit" in readline.__doc__17@unittest.skipUnless(hasattr(readline, "clear_history"),18 "The history update test cannot be run because the "19 "clear_history method is not available.")20class TestHistoryManipulation (unittest.TestCase):21 """22 These tests were added to check that the libedit emulation on OSX and the23 "real" readline have the same interface for history manipulation. That's24 why the tests cover only a small subset of the interface.25 """26 def testHistoryUpdates(self):27 readline.clear_history()28 readline.add_history("first line")29 readline.add_history("second line")30 self.assertEqual(readline.get_history_item(0), None)31 self.assertEqual(readline.get_history_item(1), "first line")32 self.assertEqual(readline.get_history_item(2), "second line")33 readline.replace_history_item(0, "replaced line")34 self.assertEqual(readline.get_history_item(0), None)35 self.assertEqual(readline.get_history_item(1), "replaced line")36 self.assertEqual(readline.get_history_item(2), "second line")37 self.assertEqual(readline.get_current_history_length(), 2)38 readline.remove_history_item(0)39 self.assertEqual(readline.get_history_item(0), None)40 self.assertEqual(readline.get_history_item(1), "second line")41 self.assertEqual(readline.get_current_history_length(), 1)42 @unittest.skipUnless(hasattr(readline, "append_history_file"),43 "append_history not available")44 def test_write_read_append(self):45 hfile = tempfile.NamedTemporaryFile(delete=False)46 hfile.close()47 hfilename = hfile.name48 self.addCleanup(unlink, hfilename)49 # test write-clear-read == nop50 readline.clear_history()51 readline.add_history("first line")52 readline.add_history("second line")53 readline.write_history_file(hfilename)54 readline.clear_history()55 self.assertEqual(readline.get_current_history_length(), 0)56 readline.read_history_file(hfilename)57 self.assertEqual(readline.get_current_history_length(), 2)58 self.assertEqual(readline.get_history_item(1), "first line")59 self.assertEqual(readline.get_history_item(2), "second line")60 # test append61 readline.append_history_file(1, hfilename)62 readline.clear_history()63 readline.read_history_file(hfilename)64 self.assertEqual(readline.get_current_history_length(), 3)65 self.assertEqual(readline.get_history_item(1), "first line")66 self.assertEqual(readline.get_history_item(2), "second line")67 self.assertEqual(readline.get_history_item(3), "second line")68 # test 'no such file' behaviour69 os.unlink(hfilename)70 with self.assertRaises(FileNotFoundError):71 readline.append_history_file(1, hfilename)72 # write_history_file can create the target73 readline.write_history_file(hfilename)74 def test_nonascii_history(self):75 readline.clear_history()76 try:77 readline.add_history("entrée 1")78 except UnicodeEncodeError as err:79 self.skipTest("Locale cannot encode test data: " + format(err))80 readline.add_history("entrée 2")81 readline.replace_history_item(1, "entrée 22")82 readline.write_history_file(TESTFN)83 self.addCleanup(os.remove, TESTFN)84 readline.clear_history()85 readline.read_history_file(TESTFN)86 if is_editline:87 # An add_history() call seems to be required for get_history_88 # item() to register items from the file89 readline.add_history("dummy")90 self.assertEqual(readline.get_history_item(1), "entrée 1")91 self.assertEqual(readline.get_history_item(2), "entrée 22")92class TestReadline(unittest.TestCase):93 @unittest.skipIf(readline._READLINE_VERSION < 0x0601 and not is_editline,94 "not supported in this library version")95 def test_init(self):96 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not97 # written into stdout when the readline module is imported and stdout98 # is redirected to a pipe.99 rc, stdout, stderr = assert_python_ok('-c', 'import readline',100 TERM='xterm-256color')101 self.assertEqual(stdout, b'')102 auto_history_script = """\103import readline104readline.set_auto_history({})105input()106print("History length:", readline.get_current_history_length())107"""108 def test_auto_history_enabled(self):109 output = run_pty(self.auto_history_script.format(True))110 self.assertIn(b"History length: 1\r\n", output)111 def test_auto_history_disabled(self):112 output = run_pty(self.auto_history_script.format(False))113 self.assertIn(b"History length: 0\r\n", output)114 def test_nonascii(self):115 try:116 readline.add_history("\xEB\xEF")117 except UnicodeEncodeError as err:118 self.skipTest("Locale cannot encode test data: " + format(err))119 script = r"""import readline120is_editline = readline.__doc__ and "libedit" in readline.__doc__121inserted = "[\xEFnserted]"122macro = "|t\xEB[after]"123set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)124if is_editline or not set_pre_input_hook:125 # The insert_line() call via pre_input_hook() does nothing with Editline,126 # so include the extra text that would have been inserted here127 macro = inserted + macro128if is_editline:129 readline.parse_and_bind(r'bind ^B ed-prev-char')130 readline.parse_and_bind(r'bind "\t" rl_complete')131 readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro))132else:133 readline.parse_and_bind(r'Control-b: backward-char')134 readline.parse_and_bind(r'"\t": complete')135 readline.parse_and_bind(r'set disable-completion off')136 readline.parse_and_bind(r'set show-all-if-ambiguous off')137 readline.parse_and_bind(r'set show-all-if-unmodified off')138 readline.parse_and_bind(r'Control-a: "{}"'.format(macro))139def pre_input_hook():140 readline.insert_text(inserted)141 readline.redisplay()142if set_pre_input_hook:143 set_pre_input_hook(pre_input_hook)144def completer(text, state):145 if text == "t\xEB":146 if state == 0:147 print("text", ascii(text))148 print("line", ascii(readline.get_line_buffer()))149 print("indexes", readline.get_begidx(), readline.get_endidx())150 return "t\xEBnt"151 if state == 1:152 return "t\xEBxt"153 if text == "t\xEBx" and state == 0:154 return "t\xEBxt"155 return None156readline.set_completer(completer)157def display(substitution, matches, longest_match_length):158 print("substitution", ascii(substitution))159 print("matches", ascii(matches))160readline.set_completion_display_matches_hook(display)161print("result", ascii(input()))162print("history", ascii(readline.get_history_item(1)))163"""164 input = b"\x01" # Ctrl-A, expands to "|t\xEB[after]"165 input += b"\x02" * len("[after]") # Move cursor back166 input += b"\t\t" # Display possible completions167 input += b"x\t" # Complete "t\xEBx" -> "t\xEBxt"168 input += b"\r"169 output = run_pty(script, input)170 self.assertIn(b"text 't\\xeb'\r\n", output)171 self.assertIn(b"line '[\\xefnserted]|t\\xeb[after]'\r\n", output)172 self.assertIn(b"indexes 11 13\r\n", output)173 if not is_editline and hasattr(readline, "set_pre_input_hook"):174 self.assertIn(b"substitution 't\\xeb'\r\n", output)175 self.assertIn(b"matches ['t\\xebnt', 't\\xebxt']\r\n", output)176 expected = br"'[\xefnserted]|t\xebxt[after]'"177 self.assertIn(b"result " + expected + b"\r\n", output)178 self.assertIn(b"history " + expected + b"\r\n", output)179def run_pty(script, input=b"dummy input\r"):180 pty = import_module('pty')181 output = bytearray()182 [master, slave] = pty.openpty()183 args = (sys.executable, '-c', script)184 proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave)185 os.close(slave)186 with ExitStack() as cleanup:187 cleanup.enter_context(proc)188 def terminate(proc):189 try:190 proc.terminate()191 except ProcessLookupError:192 # Workaround for Open/Net BSD bug (Issue 16762)193 pass194 cleanup.callback(terminate, proc)195 cleanup.callback(os.close, master)196 # Avoid using DefaultSelector and PollSelector. Kqueue() does not197 # work with pseudo-terminals on OS X < 10.9 (Issue 20365) and Open198 # BSD (Issue 20667). Poll() does not work with OS X 10.6 or 10.4199 # either (Issue 20472). Hopefully the file descriptor is low enough200 # to use with select().201 sel = cleanup.enter_context(selectors.SelectSelector())202 sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE)203 os.set_blocking(master, False)204 while True:205 for [_, events] in sel.select():206 if events & selectors.EVENT_READ:207 try:208 chunk = os.read(master, 0x10000)209 except OSError as err:210 # Linux raises EIO when slave is closed (Issue 5380)211 if err.errno != EIO:212 raise213 chunk = b""214 if not chunk:215 return output216 output.extend(chunk)217 if events & selectors.EVENT_WRITE:218 try:219 input = input[os.write(master, input):]220 except OSError as err:221 # Apparently EIO means the slave was closed222 if err.errno != EIO:223 raise224 input = b"" # Stop writing225 if not input:226 sel.modify(master, selectors.EVENT_READ)227if __name__ == "__main__":...

Full Screen

Full Screen

readline.ts

Source:readline.ts Github

copy

Full Screen

1import * as readline from 'readline';2import * as stream from 'stream';3import * as fs from 'fs';4const rl: readline.ReadLine = readline.createInterface(new stream.Readable());5{6 const options: readline.ReadLineOptions = {7 input: new fs.ReadStream()8 };9 const input: NodeJS.ReadableStream = new stream.Readable();10 const output: NodeJS.WritableStream = new stream.Writable();11 const completer: readline.Completer = str => [['asd'], 'asd'];12 const terminal = false;13 let result: readline.ReadLine;14 result = readline.createInterface(options);15 result = readline.createInterface(input);16 result = readline.createInterface(input, output);17 result = readline.createInterface(input, output, completer);18 result = readline.createInterface(input, output, completer, terminal);19 result = readline.createInterface({20 input,21 completer(str: string): readline.CompleterResult {22 return [['test'], 'test'];23 }24 });25 result = readline.createInterface({26 input,27 completer(str: string, callback: (err: any, result: readline.CompleterResult) => void): any {28 callback(null, [['test'], 'test']);29 }30 });31}32{33 rl.setPrompt("prompt");34}35{36 rl.prompt();37 rl.prompt(true);38}39{40 rl.question("query", (answer: string) => {});41}42{43 let result: readline.ReadLine;44 result = rl.pause();45}46{47 let result: readline.ReadLine;48 result = rl.resume();49}50{51 rl.close();52}53{54 const data: string | Buffer = "asd";55 const key: readline.Key = {};56 rl.write(data);57 rl.write('asd', key);58}59{60 const strm: NodeJS.WritableStream = new stream.Writable();61 const x = 1;62 const y = 1;63 readline.cursorTo(strm, x);64 readline.cursorTo(strm, x, y);65}66{67 const strm: NodeJS.ReadableStream = new stream.Readable();68 const readLineInterface: readline.ReadLine = readline.createInterface(new stream.Readable());69 readline.emitKeypressEvents(strm);70 readline.emitKeypressEvents(strm, readLineInterface);71}72{73 const strm: NodeJS.WritableStream = new stream.Writable();74 const dx: number | string = 1;75 const dy: number | string = 1;76 readline.moveCursor(strm, dx, dy);77}78{79 const strm: NodeJS.WritableStream = new stream.Writable();80 readline.clearLine(strm, 1);81}82{83 const strm: NodeJS.WritableStream = new stream.Writable();84 readline.clearScreenDown(strm);85}86{87 let _rl = readline.createInterface({88 input: process.stdin,89 });90 let _boolean: boolean;91 _rl = _rl.addListener("close", () => { });92 _rl = _rl.addListener("line", (input) => {93 const _input: string = input;94 });95 _rl = _rl.addListener("pause", () => { });96 _rl = _rl.addListener("resume", () => { });97 _rl = _rl.addListener("SIGCONT", () => { });98 _rl = _rl.addListener("SIGINT", () => { });99 _rl = _rl.addListener("SIGTSTP", () => { });100 _boolean = _rl.emit("close", () => { });101 _boolean = _rl.emit("line", () => { });102 _boolean = _rl.emit("pause", () => { });103 _boolean = _rl.emit("resume", () => { });104 _boolean = _rl.emit("SIGCONT", () => { });105 _boolean = _rl.emit("SIGINT", () => { });106 _boolean = _rl.emit("SIGTSTP", () => { });107 _rl = _rl.on("close", () => { });108 _rl = _rl.on("line", (input) => {109 const _input: any = input;110 });111 _rl = _rl.on("pause", () => { });112 _rl = _rl.on("resume", () => { });113 _rl = _rl.on("SIGCONT", () => { });114 _rl = _rl.on("SIGINT", () => { });115 _rl = _rl.on("SIGTSTP", () => { });116 _rl = _rl.once("close", () => { });117 _rl = _rl.once("line", (input) => {118 const _input: any = input;119 });120 _rl = _rl.once("pause", () => { });121 _rl = _rl.once("resume", () => { });122 _rl = _rl.once("SIGCONT", () => { });123 _rl = _rl.once("SIGINT", () => { });124 _rl = _rl.once("SIGTSTP", () => { });125 _rl = _rl.prependListener("close", () => { });126 _rl = _rl.prependListener("line", (input) => {127 const _input: any = input;128 });129 _rl = _rl.prependListener("pause", () => { });130 _rl = _rl.prependListener("resume", () => { });131 _rl = _rl.prependListener("SIGCONT", () => { });132 _rl = _rl.prependListener("SIGINT", () => { });133 _rl = _rl.prependListener("SIGTSTP", () => { });134 _rl = _rl.prependOnceListener("close", () => { });135 _rl = _rl.prependOnceListener("line", (input) => {136 const _input: any = input;137 });138 _rl = _rl.prependOnceListener("pause", () => { });139 _rl = _rl.prependOnceListener("resume", () => { });140 _rl = _rl.prependOnceListener("SIGCONT", () => { });141 _rl = _rl.prependOnceListener("SIGINT", () => { });142 _rl = _rl.prependOnceListener("SIGTSTP", () => { });143}144{145 (async () => {146 const result = readline.createInterface({147 input: process.stdin,148 });149 // Pending lib upgrade150 // for await (const line of result) {151 //152 // }153 });...

Full Screen

Full Screen

py11e.py

Source:py11e.py Github

copy

Full Screen

1file = open("py11.py","r")2# line = file.readline()3# print(line)4# line = file.readline()5# print(line)6# line = file.readline()7# print(line)8# line = file.readline()9# print(line)10# line = file.readline()11# print(line)12# line = file.readline()13# print(line)14# line = file.readline()15# print(line)16# line = file.readline()17# print(line)18# line = file.readline()19# print(line)20# line = file.readline()21# print(line)22# line = file.readline()23# print(line)24# line = file.readline()25# print(line)26# line = file.readline()27# print(line)28# line = file.readline()29# print(line)30# line = file.readline()31# print(line)32# line = file.readline()33# print(line)34# line = file.readline()35# print(line)36# line = file.readline()37# print(line)38# line = file.readline()39# print(line)40# line = file.readline()41# print(line)42# line = file.readline()43# print(line)44# line = file.readline()45# print(line)46# line = file.readline()47# print(line)48# line = file.readline()49# print(line)50# line = file.readline()51# print(line)52# line = file.readline()53# print(line)54# line = file.readline()55# print(line)56# line = file.readline()57# print(line)58# line = file.readline()59# print(line)60# line = file.readline()61# print(line)62# line = file.readline()63# print(line)64# line = file.readline()65# print(line)66# line = file.readline()67# print(line)68# line = file.readline()69# print(line)70# line = file.readline()71# print(line)72# line = file.readline()73# print(line)74# line = file.readline()75# print(line)76# line = file.readline()77# print(line)78# line = file.readline()79# print(line)80# line = file.readline()81# print(line)82# line = file.readline()83# print(line)84# line = file.readline()85# print(line)86# line = file.readline()87# print(line)88# line = file.readline()89# print(line)90# line = file.readline()91# print(line)92# line = file.readline()93# print(line)94# line = file.readline()95# print(line)96# line = file.readline()97# print(line)98# line = file.readline()99# print(line)100# line = file.readline()101# print(line)102# line = file.readline()103# print(line)104# line = file.readline()105# print(line)106# line = file.readline()107# print(line)108# line = file.readline()109# print(line)110# line = file.readline()111# print(line)112# line = file.readline()113# print(line)114# line = file.readline()115# print(line)116# line = file.readline()117# print(line)118# line = file.readline()119# print(line)120#Read line, reads a lines121#Then it reads next line122line=file.readlines()...

Full Screen

Full Screen

readline.js

Source:readline.js Github

copy

Full Screen

1var readline = require("readline");2var promisify = require("./_promisify.js");3var bind = function(c, f) { return f && f.bind(c); };4Object.defineProperties(module.exports, {5 Interface: { enumerable: true, value: readline.Interface },6 clearLine: { enumerable: true, value: bind(readline, readline.clearLine) },7 clearScreenDown: { enumerable: true, value: bind(readline, readline.clearScreenDown) },8 codePointAt: { enumerable: true, value: bind(readline, readline.codePointAt) },9 createInterface: { enumerable: true, value: bind(readline, readline.createInterface) },10 cursorTo: { enumerable: true, value: bind(readline, readline.cursorTo) },11 emitKeypressEvents: { enumerable: true, value: bind(readline, readline.emitKeypressEvents) },12 getStringWidth: { enumerable: true, value: bind(readline, readline.getStringWidth) },13 isFullWidthCodePoint: { enumerable: true, value: bind(readline, readline.isFullWidthCodePoint) },14 moveCursor: { enumerable: true, value: bind(readline, readline.moveCursor) },15 stripVTControlCharacters: { enumerable: true, value: bind(readline, readline.stripVTControlCharacters) },...

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