Best Python code snippet using lisa_python
views.py
Source:views.py  
...148    listing.description = request.POST['description']149    listing.list_date = request.POST['list_date']150    listing.is_published = request.POST['is_published'] == 'on'151    return listing152def remove_image(image):153    if image != '' and os.path.exists(image.path):154        os.remove(image.path)155def image_to_remove(request, listing):156    if 'photo_main-clear' in request.POST:157        remove_image(listing.photo_main)158        listing.photo_main = ''159    if 'photo_1-clear' in request.POST:160        remove_image(listing.photo_1)161        listing.photo_1 = ''162    if 'photo_2-clear' in request.POST:163        remove_image(listing.photo_2)164        listing.photo_2 = ''165    if 'photo_3-clear' in request.POST:166        remove_image(listing.photo_3)167        listing.photo_3 = ''168    if 'photo_4-clear' in request.POST:169        remove_image(listing.photo_4)170        listing.photo_4 = ''171    if 'photo_5-clear' in request.POST:172        remove_image(listing.photo_5)173        listing.photo_5 = ''174    return listing175def listing_images(request, listing):176    if 'photo_main' in request.FILES:177        remove_image(listing.photo_main)178        listing.photo_main = request.FILES['photo_main']179    if 'photo_1' in request.FILES:180        remove_image(listing.photo_1)181        listing.photo_1 = request.FILES['photo_1']182    if 'photo_2' in request.FILES:183        remove_image(listing.photo_2)184        listing.photo_2 = request.FILES['photo_2']185    if 'photo_3' in request.FILES:186        remove_image(listing.photo_3)187        listing.photo_3 = request.FILES['photo_3']188    if 'photo_4' in request.FILES:189        remove_image(listing.photo_4)190        listing.photo_4 = request.FILES['photo_4']191    if 'photo_5' in request.FILES:192        remove_image(listing.photo_5)193        listing.photo_5 = request.FILES['photo_5']194    return listing195def listing_edit(request, listing_id):196    user = auth.get_user(request)197    if user.is_superuser and user.is_authenticated:198        listing = Listing.objects.get(id=listing_id)199        if user != listing.realtor:200            return redirect('agent_listing')201        if request.method == 'POST':202            listing = listing_details(request, listing)203            listing = image_to_remove(request, listing)204            if request.FILES != 0:205                listing = listing_images(request, listing)206            listing.save()207            messages.success(request, 'Listing has been saved')208            return redirect('agent_listing')209        form = ListingForm(request=request, listing=listing)210        context = {'form': form, 'listing': listing}211        return render(request, 'users/listing_edit.html', context)212    else:213        return redirect('index')214def listing_create(request):215    user = auth.get_user(request)216    if user.is_superuser and user.is_authenticated:217        if request.method == 'POST':218            listing = listing_details(request)219            if request.FILES != 0:220                listing = listing_images(request, listing)221            listing.save()222            messages.success(request, 'Listing has been saved')223            return redirect('agent_listing')224        form = ListingForm(request=request)225        context = {'form': form}226        return render(request, 'users/listing_edit.html', context)227    else:228        return redirect('index')229def listing_delete(request, listing_id):230    user = auth.get_user(request)231    if user.is_superuser and user.is_authenticated:232        listing = Listing.objects.get(id=listing_id)233        remove_image(listing.photo_main)234        remove_image(listing.photo_1)235        remove_image(listing.photo_2)236        remove_image(listing.photo_3)237        remove_image(listing.photo_4)238        remove_image(listing.photo_5)239        listing.delete()240        messages.success(request, 'Listing successfully deleted')241        return redirect('agent_listing')242    else:...test_nvidia.py
Source:test_nvidia.py  
...18from . import utilities19##############################################################################20# Methods21##############################################################################22def remove_image(name: str):23    docker_client = groot_rocker.core.get_docker_client()24    docker_client.remove_image(image=name)25##############################################################################26# Tests27##############################################################################28class Nvidia(utilities.ExtensionTestCase):29    """30    By default, this test cleans up images and containers so that the user31    does not have to deal with dangling images (inadvertantly that also32    means that it's not caching between runs, but will make use of cached33    images in a single run).34    To debug via images, comment out the relevant explicit docker_client.remove_image35    command in remove_image() or instead, comment them individually below.36    """37    @classmethod38    def setUpClass(self):39        client = groot_rocker.core.get_docker_client()40        self.dockerfile_tags = []41        for distro_version in ['18.04', '20.04']:42            dockerfile = (43                f"FROM ubuntu:{distro_version}\n"44                "RUN apt-get update && apt-get install glmark2 -y && apt-get clean\n"45                "CMD glmark2 --validate\n"46            )47            dockerfile_tag = "groot:" + f"test_nvidia_{distro_version}"48            iof = io.BytesIO((dockerfile % locals()).encode())49            image_id = client.build(fileobj=iof, tag=dockerfile_tag, forcerm=True)50            for unused_e in image_id:51                pass52            self.dockerfile_tags.append(dockerfile_tag)53    @classmethod54    def tearDownClass(self):55        # For quick debugging, comment out this method56        for image_name in self.dockerfile_tags:57            print(f"Image Name: {image_name}")58            # If the os detector images were made59            print(f"Teardown {image_name}")60            remove_image(name=image_name)61        try:62            remove_image("groot:os_detect_builder")63        except docker.errors.ImageNotFound:64            # some test methods do not build the os_detect_builder, that's ok65            pass66    def test_glmark2_validate_nvidia_not_enabled(self):67        for tag in self.dockerfile_tags:68            docker_tag = "groot:test_no_glmark2"69            dig = groot_rocker.core.DockerImageGenerator(70                active_extensions=[],71                cliargs={},72                base_image=tag73            )74            result = dig.build(image_name=docker_tag)75            utilities.assert_details(text="No Nvidia GLMark2 Build Result", expected=0, result=result)76            self.assertEqual(result, 0)77            result = dig.run()78            utilities.assert_details(text="No Nvidia GLMark2 Run Result", expected=1, result=result)79            self.assertEqual(result, 1)80            remove_image(name=docker_tag)81    def test_glmark2_validate_nvidia_enabled(self):82        extensions = groot_rocker.core.list_plugins()83        desired_extensions = ['nvidia', 'user']84        active_extensions = [e() for e in extensions.values() if e.get_name() in desired_extensions]85        for tag in self.dockerfile_tags:86            docker_tag = "groot:test_glmark2"87            dig = groot_rocker.core.DockerImageGenerator(88                active_extensions=active_extensions,89                cliargs={},90                base_image=tag91            )92            result = dig.build(image_name=docker_tag)93            utilities.assert_details(text="Nvidia GLMark2 Build Result", expected=0, result=result)94            self.assertEqual(result, 0)95            result = dig.run()96            utilities.assert_details(text="Nvidia GLMark2 Run Result", expected=0, result=result)97            self.assertEqual(result, 0)98            remove_image(name=docker_tag)99    def test_base_image_does_not_exist(self):100        mock_cli_args = {'base_image': 'ros:does-not-exist'}101        print(console.green + f"Checking base image '{mock_cli_args['base_image']}'" + console.reset)102        with self.assertRaises(SystemExit) as cm:103            self.extension.get_environment_subs(mock_cli_args)104        self.assertEqual(cm.exception.code, 1)105        utilities.assert_details(text="Base image does not exist", expected="SystemExit", result="SystemExit")106    def test_unsupported_base_image_version(self):107        mock_cli_args = {'base_image': 'ubuntu:17.04'}108        print(console.green + f"Checking base image '{mock_cli_args['base_image']}'" + console.reset)109        with self.assertRaises(SystemExit) as cm:110            self.extension.get_environment_subs(mock_cli_args)111        self.assertEqual(cm.exception.code, 1)112        utilities.assert_details(text="Unsupported Version", expected="SystemExit", result="SystemExit")113        remove_image("groot:os_detect_" + mock_cli_args['base_image'].replace(":", "_"))114    def test_unsupported_base_image_os(self):115        mock_cli_args = {'base_image': 'fedora'}116        print(console.green + f"Checking base image '{mock_cli_args['base_image']}'" + console.reset)117        with self.assertRaises(SystemExit) as cm:118            self.extension.get_environment_subs(mock_cli_args)119        self.assertEqual(cm.exception.code, 1)120        utilities.assert_details(text="Unsupported OS", expected="SystemExit", result="SystemExit")...ablation_test.py
Source:ablation_test.py  
1import train2import eval_auc3import tensorflow as tf4print "remove everything"5model = train.get_trained_model(tag='remove_all_lr001_with_val_short2', remove_image = True, remove_wind = True, remove_hcad = True)6eval_auc.evaluate_auc(model)7tf.reset_default_graph()8print "include everything"9model = train.get_trained_model(tag='include_all_lr001_with_val_short2', remove_image = False, remove_wind = False, remove_hcad = False)10eval_auc.evaluate_auc(model)11tf.reset_default_graph()12print "remove wind"13model = train.get_trained_model(tag='no_wind_short2', remove_image = False, remove_wind = True, remove_hcad = False)14eval_auc.evaluate_auc(model)15tf.reset_default_graph()16print "remove image"17model = train.get_trained_model(tag='no_image_short2', remove_image = True, remove_wind = False, remove_hcad = False)18eval_auc.evaluate_auc(model)19tf.reset_default_graph()20print "remove hcad"21model = train.get_trained_model(tag='no_hcad_short2', remove_image = False, remove_wind = False, remove_hcad = True)22eval_auc.evaluate_auc(model)...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!!
