How to use remove_args method in autotest

Best Python code snippet using autotest_python

task.py

Source:task.py Github

copy

Full Screen

1"""2task.py3A very basic to-do list command line application. It works by adding and removing tasks from4a text file that is specified by the `filename` variable in the `main` function.5"""6import argparse7import sys8def main():9 filename = '' # tasks file that stores all the tasks10 parser = argparse.ArgumentParser(description='basic command line to-do application')11 group = parser.add_mutually_exclusive_group()12 group.add_argument('-l', '--list', action='store_true',13 help='list all stored tasks')14 group.add_argument('-a', '--add', nargs='+',15 help='tasks to be added separated by whitespace')16 group.add_argument('-r', '--remove', nargs='+', dest='remove_args',17 help='remove by listing individual task numbers separated ' +18 'by whitespace or a range which is of the format a..b where '+19 'a and b are both integers')20 args = parser.parse_args()21 if args.list:22 list_tasks(filename)23 elif args.add:24 add_tasks(filename, args.add)25 elif args.remove_args:26 remove_tasks(filename, args.remove_args)27def list_tasks(filename):28 """29 List all the tasks presently stored in `filename`.30 """31 input_file = open(filename)32 tasks = input_file.read().split('\n')33 tasks.pop()34 input_file.close()35 if len(tasks) == 0:36 print('=> no tasks')37 else:38 counter = 139 for task in tasks:40 print('{} => {}'.format(counter, task))41 counter += 142def add_tasks(filename, tasks):43 """44 Add all the tasks that are stored in `tasks` into the tasks file.45 """46 output_file = open(filename, 'a')47 for task in tasks:48 output_file.write(task + '\n')49 print('=> added task \'{}\''.format(task))50 output_file.close()51def remove_tasks(filename, remove_args):52 """53 Removes tasks depending on what the --remove argument has received from the command line.54 Removes either by task numbers or by a 'task range' that is of the format a..b55 """56 if remove_args[0] == 'all':57 input_file = open(filename)58 tasks = input_file.read().split('\n')59 tasks.pop()60 input_file.close()61 if len(tasks) == 0:62 print('=> no tasks to remove, already empty')63 else:64 output_file = open(filename, 'w')65 output_file.write('')66 output_file.close()67 print('=> removed all tasks')68 else:69 if '..' in remove_args[0]:70 unique_task_numbers = set()71 dot_start_i = remove_args[0].index('..')72 try:73 start_num = int(remove_args[0][0:dot_start_i])74 end_num = int(remove_args[0][dot_start_i+2:])75 if end_num > start_num:76 unique_task_numbers = {str(x) for x in77 range(start_num, end_num + 1)}78 else:79 print("=> warning: invalid range numbers")80 print("=> end number must be greater than start number")81 return 182 except ValueError:83 print("=> warning: only integers allowed for range")84 return 185 else:86 unique_task_numbers = set(remove_args[0:])87 input_file = open(filename)88 tasks = input_file.read().split('\n')89 tasks.pop()90 input_file.close()91 if len(tasks) == 0:92 print('=> no tasks to remove, already empty')93 else:94 for task_num_str in unique_task_numbers:95 try:96 task_num = int(task_num_str)97 except ValueError:98 print("=> warning: '{}' is not a task number nor a range".format(task_num_str))99 continue100 if (task_num < 1):101 print('=> warning: only positive task numbers allowed ({:d})'.format(task_num))102 continue103 try:104 tasks[task_num - 1] = None105 print('=> removed task {:d}'.format(task_num))106 except IndexError:107 print('=> invalid task number: {:d}'.format(task_num))108 tasks = [task for task in tasks if task != None]109 output_file = open(filename, 'w')110 for task in tasks:111 output_file.write(task + '\n')112 output_file.close()113if __name__ == '__main__':...

Full Screen

Full Screen

views.py

Source:views.py Github

copy

Full Screen

1# Copyright The IETF Trust 2007-2021, All Rights Reserved2# -*- coding: utf-8 -*-3from django.http import HttpResponsePermanentRedirect, Http404, BadHeaderError4from django.shortcuts import get_object_or_4045import re6import debug # pyflakes:ignore7from ietf.redirects.models import Redirect, Command8def redirect(request, path="", script=""):9 if path:10 script = path + "/" + script11 redir = get_object_or_404(Redirect, cgi=script)12 url = "/" + redir.url + "/"13 (rest, remove) = (redir.rest, redir.remove)14 remove_args = []15 cmd = None16 #17 # First look for flag items, stored in the database18 # as a command with a leading "^".19 if request.method == 'POST':20 rparam = request.POST21 else:22 rparam = request.GET 23 for flag in redir.commands.all().filter(command__startswith='^'):24 fc = flag.command[1:].split("^")25 if len(fc) > 1:26 if rparam.get('command') != fc[1]:27 continue28 if fc[0] in rparam:29 remove_args.append(fc[0])30 num = re.match(r'(\d+)', rparam[fc[0]])31 if (num and int(num.group(1))) or (num is None):32 cmd = flag33 break34 #35 # If that search didn't result in a match, then look36 # for an exact match for the command= parameter.37 if cmd is None:38 try:39 cmd = redir.commands.all().get(command=rparam['command'])40 except Command.DoesNotExist:41 pass # it's ok, there's no more-specific request.42 except KeyError:43 pass # it's ok, request didn't have 'command'.44 except:45 pass # strange exception like the one described in46 # https://trac.ietf.org/trac/ietfdb/ticket/179?47 # just ignore the command string.48 if cmd is not None:49 remove_args.append('command')50 if cmd.url:51 rest = cmd.url + "/"52 else:53 rest = ""54 if cmd.suffix:55 rest = rest + cmd.suffix.rest56 remove = cmd.suffix.remove57 else:58 remove = ""59 try:60 # This throws exception (which gets caught below) if request 61 # contains non-ASCII characters. The old scripts didn't support 62 # non-ASCII characters anyway, so there's no need to handle 63 # them fully correctly in these redirects.64 (rest % rparam).encode('ascii')65 url += (rest % rparam) + "/"66 except:67 # rest had something in it that request didn't have, so just68 # redirect to the root of the tool.69 pass70 # Be generous in what you accept: collapse multiple slashes71 url = re.sub(r'/+', '/', url)72 if remove:73 url = re.sub(re.escape(remove) + "/?$", "", url)74 # If there is a dot in the last url segment, remove the75 # trailing slash. This is basically the inverse of the76 # APPEND_SLASH middleware.77 if '/' in url and '.' in url.split('/')[-2]:78 url = url.rstrip('/')79 # Copy the GET arguments, remove all the ones we were80 # expecting and if there are any left, add them to the URL.81 get = request.GET.copy()82 remove_args += re.findall(r'%\(([^)]+)\)', rest)83 for arg in remove_args:84 if arg in get:85 get.pop(arg)86 if get:87 url += '?' + get.urlencode()88 try:89 return HttpResponsePermanentRedirect(url)90 except BadHeaderError:...

Full Screen

Full Screen

mailmantest.py

Source:mailmantest.py Github

copy

Full Screen

1#!/bin/python2"""3This test program is driven by pytest tool. 4 pip install -U pytest5"""6import sys7import os8sys.path.append('../mailmancli/')9sys.path.append('../mailmanrest/')10import mailmancli as MailCLI11import mailmanrest as MailREST12listname = os.getenv('TEST_LIST_NAME')13passwd = os.getenv('MAILMAN_LIST_PASSWD')14print passwd15def test_001_add_mails_in_args():16 add_args = ['add', 'first@test.com', 'second@test.com', listname]17 remove_args = ['remove', 'first@test.com', 'second@test.com', listname]18 #init env19 MailCLI.send_request(remove_args)20 #do test21 msg = MailCLI.send_request(add_args).message22 assert('Subscribed' in msg)23 #clean up24 msg = MailCLI.send_request(remove_args).message25 assert('Remove Successfully' in msg)26 msg = MailCLI.send_request(remove_args).message27 assert('No such member' in msg)28def test_002_add_mails_in_file():29 mails = ['first@test.com', 'second@test.com']30 mail_file = '/tmp/mailman.test.tmp'31 add_args = ['add', '-f', mail_file, listname]32 remove_args = ['remove', 'first@test.com', 'second@test.com', listname]33 with open(mail_file, 'w') as f:34 for m in mails:35 f.write(m+'\n')36 MailCLI.send_request(remove_args)37 msg = MailCLI.send_request(add_args).message38 for i in mails:39 assert('Subscribed: {0}'.format(i) in msg)40 MailCLI.send_request(remove_args)41def test_003_add_mails_from_both():42 mails = ['first@test.com', 'second@test.com']43 mail_file = '/tmp/mailman.test.tmp'44 add_args = ['add', '-f', mail_file, 'third@test.com', listname]45 remove_args = ['remove', 'first@test.com', 'second@test.com', listname]46 with open(mail_file, 'w') as f:47 for m in mails:48 f.write(m+'\n')49 50 MailCLI.send_request(remove_args)51 msg = MailCLI.send_request(add_args).message52 mails.append('third@test.com')53 for i in mails:54 assert('Subscribed: {0}'.format(i) in msg)55 MailCLI.send_request(['remove'] + mails + [listname])56def test_004_show_and_approve_pending():57 mail = 'apply_for_approval@test.com'58 show_pending_args = ['show-pending', listname]59 approve_pending_args = ['approve', mail, listname]60 61 # clean list62 MailCLI.send_request(['remove', mail, listname])63 # use mailmanrest internal function to subscribe a mail.64 MailREST.subscribe(passwd, listname, mail)65 msg = MailCLI.send_request(show_pending_args).message66 assert(mail in msg)67 68 msg = MailCLI.send_request(approve_pending_args).message69 assert('Subscribed' in msg)70 msg = MailCLI.send_request(show_pending_args).message...

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