Best Python code snippet using fMBT_python
importcharges.py
Source:importcharges.py  
1## DEPRECATED - use import tasks in instances2from django.core.management.base import BaseCommand, CommandError3from lxml import etree4from os import path5from open_municipio.people.models import Person, Sitting, CityCouncil6from open_municipio.votations.models import Votation, ChargeVote, InstitutionCharge7import sys, traceback8from open_municipio.settings_import import XML_TO_OM_INST, IMPORT_NS, BIRTH_DATE_DEF, SEX_DEF, START_DATE_DEF9class Command(BaseCommand):10    args = "<filename filename ...>"11    help = "Import the charges contained in the specified XML documents"12    def revLookup(self, choicelist, value):13        for (key,currvalue) in choicelist:14            if currvalue == value:15                return key16        return None17    def handlePerson(self, xml_person):18        first_name = xml_person.get("first_name")19        last_name = xml_person.get("last_name")20        try:21            om_person = Person.objects.all().get(first_name=first_name,22                last_name=last_name)23            print("Person %s %s already imported" % (first_name,last_name))24            return om_person25        except Exception:26            # do nothing, continue to import it27            om_person = None28        om_person = Person()29        om_person.first_name = first_name30        om_person.last_name = last_name31        om_person.birth_date = BIRTH_DATE_DEF32        om_person.sex = SEX_DEF33        om_person.save()34        return om_person35    36    def lookupCharge(self, om_person, om_institution):37        try:38            om_charge = InstitutionCharge.objects.all().get(person=om_person,39                institution=om_institution)40        except Exception:41            om_charge = None42        return om_charge43        44    def handleCharge(self, xml_charge, om_institution):45        charge_id = xml_charge.get("id")46        try:47            om_charge = InstitutionCharge.objects.all().get(pk=charge_id)48            print("Charge (pk=%d) already imported" % charge_id)49            return50        except Exception:51            # not found, continue with importing52            om_charge = None53        persons = xml_charge.xpath("./om:Person", namespaces=IMPORT_NS)54        if len(persons) > 1:55            print("Alert! More than one Person found for this Charge")56        elif len(persons) == 1:57            print("Found Person data in XML document")58        else:59            print("No Person data found")60        for xml_person in persons:61            om_person = self.handlePerson(xml_person)62            charge_desc = xml_charge.get("description")63            charge_type = self.revLookup(InstitutionCharge.CHARGE_TYPES, charge_desc)64            if charge_type == None:65                print("Unable to find charge_type for '%s'. Interrupt import current Charge" % charge_desc)66                continue67            om_charge = self.lookupCharge(om_person, om_institution)68            if om_charge != None:69                print("Charge already present (%s %s at %s), no need to import" % 70                    (om_person.first_name, om_person.last_name, om_institution.name))71                continue72            charge_pk = xml_charge.get("id")73            om_charge = InstitutionCharge()74            om_charge.pk = int(charge_pk)75            om_charge.person = om_person76            om_charge.institution = om_institution77            om_charge.start_date = START_DATE_DEF78            om_charge.charge_type = charge_type79            om_charge.save()80            print("Imported Charge (%s %s at %s) successfully (pk=%d)" % 81                    (om_person.first_name, om_person.last_name, om_institution.name,82                    om_charge.pk, charge_pk))83    def handleCouncil(self, xml_council):84        charges = xml_council.xpath("./om:Charge", namespaces=IMPORT_NS)85        om_institution = CityCouncil().as_institution86        print("Found %d Charges to import" % len(charges))87        for xml_charge in charges:88            self.handleCharge(xml_charge, om_institution)89    def handleFile(self, filename):90        if not path.isfile(filename):91            raise IOError("File %s does not exist" % filename)92        tree = etree.parse(filename)93        councils = tree.xpath("/om:People/om:Institutions/om:Council", 94            namespaces=IMPORT_NS);95        if len(councils) > 1:96            print("More than one element Council found, this should not happen")97        elif len(councils) == 1:98            print("Found Council element to import")       99        for xml_council in councils:100            self.handleCouncil(xml_council)101    def print_help(self):102        self.stdout.write("Command syntax:\n")103        self.stdout.write("importcharges %s\n" % self.args)104        self.stdout.write("%s\n" % self.help)105    def handle(self, *args, **options):106        try:107            if len(args) == 0:108                self.print_help()109                return110            for filename in args:111                self.handleFile(filename)112        except Exception as e:113            traceback.print_exc()...ntestdirectx.py
Source:ntestdirectx.py  
1import loader_native, unittest2wgd = loader_native.import_ns("Windows.Graphics.DirectX")3wgdd = loader_native.import_ns("Windows.Graphics.DirectX.Direct3D11")4class TestDirectX(unittest.TestCase):5    def test_struct_containing_enum_pos(self):6        msd = wgdd.Direct3DMultisampleDescription(1, 2)7        sd = wgdd.Direct3DSurfaceDescription(4, 8, 10, msd) # 10 = DirectXPixelFormat.R16G16B16A16Float8        self.assertEqual(sd.width, 4)9        self.assertEqual(sd.height, 8)10        self.assertEqual(sd.format, 10)11        msd2 = sd.multisample_description12        self.assertEqual(msd.count, 1)13        self.assertEqual(msd.quality, 2)14    15    def test_struct_containing_enum_kwd(self):16        msd = wgdd.Direct3DMultisampleDescription(1, 2)17        sd = wgdd.Direct3DSurfaceDescription(format=10, width=4, multisample_description=msd, height=8)...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!!
