Best Python code snippet using slash
printer.py
Source:printer.py  
...101                ws.write(i + rows_count, 10, invoice.diff_netto_value * (invoice.currency_value or 1), style1)        102                103        wb.save(buffer)104        return buffer105    def _add_element(self, document, key, value, cdata = False):106        element = document.createElement(key)107        if cdata:108            text = document.createCDATASection(value)109        else:110            text = document.createTextNode(value)        111        element.appendChild(text)112        return element113    def _add_correct(self, document, invoice):114        """Export invoice correct"""115        invoiceElement = document.createElement('REJESTR_SPRZEDAZY_VAT')116        invoiceElement.appendChild(self._add_element(document, 'MODUL', 'Rejestr VAT'))117        invoiceElement.appendChild(self._add_element(document, 'REJESTR', u'SPRZEDAÅ»', True))118        invoiceElement.appendChild(self._add_element(document, 'DATA_WYSTAWIENIA', str(invoice.correct_date), True))119        invoiceElement.appendChild(self._add_element(document, 'DATA_SPRZEDAZY', str(invoice.sell_date), True))120        invoiceElement.appendChild(self._add_element(document, 'TERMIN', str(invoice.payment_date), True))121        invoiceElement.appendChild(self._add_element(document, 'NUMER', invoice.number, True))122        invoiceElement.appendChild(self._add_element(document, 'KOREKTA', 'Nie'))123        invoiceElement.appendChild(self._add_element(document, 'FISKALNA', 'Nie'))124        invoiceElement.appendChild(self._add_element(document, 'DETALICZNA', 'Nie'))125        invoiceElement.appendChild(self._add_element(document, 'TYP_PODMIOTU', 'kontrahent', True))126        invoiceElement.appendChild(self._add_element(document, 'PODMIOT', invoice.company.shortName, True))127        invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE', '1', True))128        invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ZA_ILE', '1', True))129        invoiceElement.appendChild(self._add_element(document, 'KATEGORIA', u'SPRZEDAÅ»', True))130        invoiceElement.appendChild(self._add_element(document, 'OPIS', invoice.number, True))131        invoiceElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI', invoice.company.payment.value, True))132        if invoice.company.tax.name == 'NPO':133            invoiceElement.appendChild(self._add_element(document, 'EKSPORT', u'WewnÄ
trzunijny', False))134            invoiceElement.appendChild(self._add_element(document, 'DEKLARACJA_VATUE', 'Tak', True))135        positions = document.createElement('POZYCJE')136        for position in invoice.positions:137            positionElement = document.createElement('POZYCJA')138            positionElement.appendChild(self._add_element(document, 'STAWKA_VAT', str(position.tax.value)))139            if invoice.company.tax.name == 'NPO':140                #add currency value141                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value * (invoice.currency_value or 1))))142                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value * (invoice.currency_value or 1))))143                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value * (invoice.currency_value or 1))))144                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(position.tax_value * (invoice.currency_value or 1))))145                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value * (invoice.currency_value or 1))))146                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(position.tax_value * (invoice.currency_value or 1))))147                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'nie podlega'))148            else:149                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value)))150                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value)))151                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value)))152                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(position.tax_value)))153                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))154                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(position.tax_value)))155                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'opodatkowana'))156            positions.appendChild(positionElement)157        if invoice.invoice.tax.name == 'NPO':158            payment = document.createElement('PLATNOSCI')159            paymentElement = document.createElement('PLATNOSC')160            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))161            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))162            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value), True))163            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value * invoice.currency_value), True))164            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))165            paymentElement.appendChild(self._add_element(document, 'WALUTA_PLAT', invoice.currency_symbol, True))166            paymentElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE_PLAT', str(invoice.currency_value), True))167            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.currency_date), True))168            payment.appendChild(paymentElement)169        else:170            payment = document.createElement('PLATNOSCI')171            paymentElement = document.createElement('PLATNOSC')172            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))173            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))174            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value), True))175            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value), True))176            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))177            payment.appendChild(paymentElement)178        invoiceElement.appendChild(payment)179        invoiceElement.appendChild(positions)180        return invoiceElement181    def _add_invoice(self, document, invoice):182        """Export invoice"""183        invoiceElement = document.createElement('REJESTR_SPRZEDAZY_VAT')184        invoiceElement.appendChild(self._add_element(document, 'MODUL', 'Rejestr VAT'))185        invoiceElement.appendChild(self._add_element(document, 'REJESTR', u'SPRZEDAÅ»', True))186        invoiceElement.appendChild(self._add_element(document, 'DATA_WYSTAWIENIA', str(invoice.issueDate), True))187        invoiceElement.appendChild(self._add_element(document, 'DATA_SPRZEDAZY', str(invoice.sellDate), True))188        invoiceElement.appendChild(self._add_element(document, 'TERMIN', str(invoice.payment_date), True))189        invoiceElement.appendChild(self._add_element(document, 'NUMER', invoice.number, True))190        invoiceElement.appendChild(self._add_element(document, 'KOREKTA', 'Nie'))191        invoiceElement.appendChild(self._add_element(document, 'FISKALNA', 'Nie'))192        invoiceElement.appendChild(self._add_element(document, 'DETALICZNA', 'Nie'))193        invoiceElement.appendChild(self._add_element(document, 'TYP_PODMIOTU', 'kontrahent', True))194        invoiceElement.appendChild(self._add_element(document, 'PODMIOT', invoice.company.shortName, True))195        invoiceElement.appendChild(self._add_element(document, 'NIP_KRAJ', invoice.company.nip_code, True))196        invoiceElement.appendChild(self._add_element(document, 'NIP', invoice.company.nip, True))197        invoiceElement.appendChild(self._add_element(document, 'NAZWA1', invoice.company.name, True))198        invoiceElement.appendChild(self._add_element(document, 'ULICA', invoice.company.address, True))199        invoiceElement.appendChild(self._add_element(document, 'MIASTO', invoice.company.city, True))200        invoiceElement.appendChild(self._add_element(document, 'KOD_POCZTOWY', invoice.company.zip, True))201        if invoice.tax.name == 'NPO':202            if invoice.elements[0].currency.value == 'EUR':203                invoiceElement.appendChild(self._add_element(document, 'WALUTA', invoice.elements[0].currency.value, True))204                invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE', str(invoice.currencyValue or 1), True))205                invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ZA_ILE', u'1', True))206                invoiceElement.appendChild(self._add_element(document, 'KURS_WALUTY', u'NBP', True))207                invoiceElement.appendChild(self._add_element(document, 'KURS_DO_KSIEGOWANIA', 'Nie', True))208                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.currencyDate or ''), True))209                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU_2', str(invoice.currencyDate or ''), True))210                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU_PLAT', str(invoice.currencyDate or ''), True))211                invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU_KD', str(''), True))212            invoiceElement.appendChild(self._add_element(document, 'KATEGORIA', u'EKSPORT USÅUG PRZEWO', True))213        else:214            invoiceElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.sellDate), True))215            invoiceElement.appendChild(self._add_element(document, 'KURS_DO_KSIEGOWANIA', 'Nie', True))216            invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE', '1', True))217            invoiceElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ZA_ILE', '1', True))218            invoiceElement.appendChild(self._add_element(document, 'KATEGORIA', u'SPRZEDAÅ» USÅUG KRAJ', True))219        invoiceElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI', invoice.company.payment.value, True))220        221        if invoice.tax.name == 'NPO':222            invoiceElement.appendChild(self._add_element(document, 'EKSPORT', u'WewnÄ
trzunijny', False))223            invoiceElement.appendChild(self._add_element(document, 'DEKLARACJA_VATUE', 'Tak', True))224        positions = document.createElement('POZYCJE')225        for position in invoice.elements:226            positionElement = document.createElement('POZYCJA')227            positionElement.appendChild(self._add_element(document, 'STAWKA_VAT', str(position.tax.value)))228            if invoice.tax.name == 'NPO':229                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value)))230                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value)))231                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value * Decimal(repr((invoice.currencyValue or 1))))))232                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(0)))233                #positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))234                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value * Decimal(repr((invoice.currencyValue or 1))))))235                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(0)))236            else:237                positionElement.appendChild(self._add_element(document, 'NETTO', str(position.netto_value)))238                positionElement.appendChild(self._add_element(document, 'VAT', str(position.tax_value)))239                positionElement.appendChild(self._add_element(document, 'NETTO_SYS', str(position.netto_value)))240                positionElement.appendChild(self._add_element(document, 'VAT_SYS', str(position.tax_value)))241                #positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))242                positionElement.appendChild(self._add_element(document, 'NETTO_SYS2', str(position.netto_value)))243                positionElement.appendChild(self._add_element(document, 'VAT_SYS2', str(position.tax_value)))244            if invoice.company.tax.name == 'NPO':245                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'nie podlega'))246                positionElement.appendChild(self._add_element(document, 'KATEGORIA_POS', u'EKSPORT USÅUG PRZEWO', True))247            else:248                positionElement.appendChild(self._add_element(document, 'STATUS_VAT', 'opodatkowana'))249                positionElement.appendChild(self._add_element(document, 'KATEGORIA_POS', u'SPRZEDAÅ» USÅUG KRAJ', True))250            251            positions.appendChild(positionElement)252        if invoice.tax.name == 'NPO':253            payment = document.createElement('PLATNOSCI')254            paymentElement = document.createElement('PLATNOSC')255            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))256            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))257            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value * Decimal(repr(invoice.currencyValue or 1))), True))258            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value), True))259            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))260            paymentElement.appendChild(self._add_element(document, 'WALUTA_PLAT', invoice.currencySymbol or '', True))261            paymentElement.appendChild(self._add_element(document, 'NOTOWANIE_WALUTY_ILE_PLAT', str(invoice.currencyValue), True))262            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.currencyDate), True))263            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU_PLAT', str(invoice.currencyDate), True))264            payment.appendChild(paymentElement)265        else:266            payment = document.createElement('PLATNOSCI')267            paymentElement = document.createElement('PLATNOSC')268            paymentElement.appendChild(self._add_element(document, 'TERMIN_PLAT', str(invoice.payment_date), True))269            paymentElement.appendChild(self._add_element(document, 'FORMA_PLATNOSCI_PLAT', invoice.company.payment.value, True))270            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLN_PLAT', str(invoice.brutto_value), True))271            paymentElement.appendChild(self._add_element(document, 'KWOTA_PLAT', str(invoice.brutto_value), True))272            paymentElement.appendChild(self._add_element(document, 'KIERUNEK', u'przychód', True))273            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU', str(invoice.sellDate), True))274            paymentElement.appendChild(self._add_element(document, 'DATA_KURSU_PLAT', str(invoice.sellDate), True))275            payment.appendChild(paymentElement)276        invoiceElement.appendChild(payment)277        invoiceElement.appendChild(positions)278        return invoiceElement279    def _add_company(self, document, company):280        element = document.createElement('KONTRAHENT')281        element.appendChild(self._add_element(document, 'AKRONIM', company.shortName, True))282        element.appendChild(self._add_element(document, 'RODZAJ', 'odbiorca dostawca', True))283        element.appendChild(self._add_element(document, 'PLATNIK_VAT', 'Tak', True))284        element.appendChild(self._add_element(document, 'ODBIORCA', company.shortName, True))285        element.appendChild(self._add_element(document, 'INDYWIDUALNY_TERMIN', 'Tak'))286        element.appendChild(self._add_element(document, 'TERMIN', company.paymentForm.value.split()[0], True))287        element.appendChild(self._add_element(document, 'MAX_ZWLOKA', '5', True))288        element.appendChild(self._add_element(document, 'FORMA_PLATNOSCI', company.payment.value, True))        289        addressElement = document.createElement('ADRESY')290        addElement = document.createElement('ADRES')291        addElement.appendChild(self._add_element(document, 'STATUS', 'aktualny'))292        addElement.appendChild(self._add_element(document, 'NAZWA1', company.name, True))293        addElement.appendChild(self._add_element(document, 'ULICA', company.address, True))294        addElement.appendChild(self._add_element(document, 'NR_DOMU', '', True))295        addElement.appendChild(self._add_element(document, 'MIASTO', company.city, True))296        addElement.appendChild(self._add_element(document, 'KOD_POCZTOWY', company.zip, True))297        addElement.appendChild(self._add_element(document, 'REGON', company.regon, True))298        addElement.appendChild(self._add_element(document, 'NIP', company.nip, True))299        addElement.appendChild(self._add_element(document, 'NIP_KRAJ', company.nip_code, True))300        addressElement.appendChild(addElement)301        302        element.appendChild(addressElement)303        return element304    def export_companies(self):305        """Export companies to OPTIMA"""306        doc = Document()307        companies = Company.query.all()308        buffer = StringIO.StringIO()309        root = doc.createElement('ROOT')310        root.setAttribute('xmlns', 'http://www.cdn.com.pl/optima/offline')311        doc.appendChild(root)312        companiesElement = doc.createElement('KONTRAHENCI')313        companiesElement.appendChild(self._add_element(doc, 'WERSJA', '2.00'))314        companiesElement.appendChild(self._add_element(doc, 'BAZA_ZRD_ID', 'SPRZ'))315        companiesElement.appendChild(self._add_element(doc, 'BAZA_DOC_ID', 'SPRZ'))316        for company in companies:317            companiesElement.appendChild(self._add_company(doc, company))318        root.appendChild(companiesElement)319        buffer.write(doc.toprettyxml(indent='', newl=''))        320        return buffer321    def export_to_cdn(self, date_from, date_to):322        """Export invoices to OPTIMA"""323        doc = Document()324        from pytis.model import meta325        buffer = StringIO.StringIO()326        327        invoices = Invoice.query.options(eagerload('elements')).filter(Invoice.issueDate.between(date_from , date_to)).order_by(Invoice.series_number).all()328        corrects = InvoiceCorrect.query.options(eagerload('positions')).filter(InvoiceCorrect.correct_date.between(date_from, date_to)).order_by(InvoiceCorrect.series_number).all()329        root = doc.createElement('ROOT')330        root.setAttribute('xmlns', 'http://www.cdn.com.pl/optima/offline')331        doc.appendChild(root)332        companiesElement = doc.createElement('KONTRAHENCI')333        companiesElement.appendChild(self._add_element(doc, 'WERSJA', '2.00'))334        companiesElement.appendChild(self._add_element(doc, 'BAZA_ZRD_ID', 'SPRZ'))335        companiesElement.appendChild(self._add_element(doc, 'BAZA_DOC_ID', 'SPRZ'))336        root.appendChild(companiesElement)337        companies = []338        for invoice in invoices:339            """fetch sets of companies"""340            if invoice.company not in companies:341                companies.append(invoice.company)342        for correct in corrects:343            """fetch sets of companies"""344            if correct.company not in companies:345                companies.append(correct.company)346        for company in companies:            347            companiesElement.appendChild(self._add_company(doc, company))348        root.appendChild(companiesElement)349        invoicesElement = doc.createElement('REJESTRY_SPRZEDAZY_VAT')350        invoicesElement.appendChild(self._add_element(doc, 'WERSJA', '2.00'))351        invoicesElement.appendChild(self._add_element(doc, 'BAZA_ZRD_ID', 'SPRZ'))352        invoicesElement.appendChild(self._add_element(doc, 'BAZA_DOC_ID', 'SPRZ'))353        for invoice in invoices:354            invoicesElement.appendChild(self._add_invoice(doc, invoice))355            if not invoice.is_exported:356                invoice.mark_as_exported()357        meta.Session.commit()358        359        for correct in corrects:360            invoicesElement.appendChild(self._add_correct(doc, correct))361        root.appendChild(invoicesElement)362        buffer.write(doc.toprettyxml(indent='', newl=''))        ...printserviceform.py
Source:printserviceform.py  
...146    def update_preview_text(self):147        """148        Creates the html text and updates the html of *self.document*.149        """150        html_data = self._add_element('html')151        self._add_element('head', parent=html_data)152        self._add_element('title', self.title_line_edit.text(), html_data.head)153        css_path = AppLocation.get_data_path() / 'serviceprint' / 'service_print.css'154        custom_css = get_text_file_string(css_path)155        if not custom_css:156            custom_css = DEFAULT_CSS157        self._add_element('style', custom_css, html_data.head, attribute=('type', 'text/css'))158        self._add_element('body', parent=html_data)159        self._add_element('h1', html.escape(self.title_line_edit.text()), html_data.body, class_id='serviceTitle')160        for index, item in enumerate(self.service_manager.service_items):161            self._add_preview_item(html_data.body, item['service_item'], index)162        if not self.show_chords_check_box.isChecked():163            # Remove chord row and spacing span elements when not printing chords164            for chord_row in html_data.find_class('chordrow'):165                chord_row.drop_tree()166            for spacing_span in html_data.find_class('chordspacing'):167                spacing_span.drop_tree()168        # Add the custom service notes:169        if self.footer_text_edit.toPlainText():170            div = self._add_element('div', parent=html_data.body, class_id='customNotes')171            self._add_element(172                'span', translate('OpenLP.ServiceManager', 'Custom Service Notes: '), div, class_id='customNotesTitle')173            self._add_element('span', html.escape(self.footer_text_edit.toPlainText()), div, class_id='customNotesText')174        self.document.setHtml(lxml.html.tostring(html_data).decode())175        self.preview_widget.updatePreview()176    def _add_preview_item(self, body, item, index):177        """178        Add a preview item179        """180        div = self._add_element('div', class_id='item', parent=body)181        # Add the title of the service item.182        item_title = self._add_element('h2', parent=div, class_id='itemTitle')183        img = image_to_byte(item.icon.pixmap(20, 20).toImage())184        self._add_element('img', parent=item_title, attribute=('src', 'data:image/png;base64, ' + img))185        self._add_element('span', ' ' + html.escape(item.get_display_title()), item_title)186        if self.slide_text_check_box.isChecked():187            # Add the text of the service item.188            if item.is_text():189                verse_def = None190                verse_html = None191                for slide in item.get_frames():192                    if not verse_def or verse_def != slide['verseTag'] or verse_html == slide['printing_html']:193                        text_div = self._add_element('div', parent=div, class_id='itemText')194                    elif 'chordspacing' not in slide['printing_html']:195                        self._add_element('br', parent=text_div)196                    self._add_element('span', slide['printing_html'], text_div)197                    verse_def = slide['verseTag']198                    verse_html = slide['printing_html']199                # Break the page before the div element.200                if index != 0 and self.page_break_after_text.isChecked():201                    div.set('class', 'item newPage')202            # Add the image names of the service item.203            elif item.is_image():204                ol = self._add_element('ol', parent=div, class_id='imageList')205                for slide in range(len(item.get_frames())):206                    self._add_element('li', item.get_frame_title(slide), ol)207            # add footer208            foot_text = item.foot_text209            foot_text = foot_text.partition('<br>')[2]210            if foot_text:211                foot_text = html.escape(foot_text.replace('<br>', '\n'))212                self._add_element('div', foot_text.replace('\n', '<br>'), parent=div, class_id='itemFooter')213        # Add service items' notes.214        if self.notes_check_box.isChecked():215            if item.notes:216                p = self._add_element('div', class_id='itemNotes', parent=div)217                self._add_element('span', translate('OpenLP.ServiceManager', 'Notes: '), p, class_id='itemNotesTitle')218                self._add_element('span', html.escape(item.notes).replace('\n', '<br>'), p, class_id='itemNotesText')219        # Add play length of media files.220        if item.is_media() and self.meta_data_check_box.isChecked():221            tme = item.media_length222            if item.end_time > 0:223                tme = item.end_time - item.start_time224            title = self._add_element('div', class_id='media', parent=div)225            self._add_element(226                'span', translate('OpenLP.ServiceManager', 'Playing time: '), title, class_id='mediaTitle')227            self._add_element('span', str(datetime.timedelta(seconds=tme)), title, class_id='mediaText')228    def _add_element(self, tag, text=None, parent=None, class_id=None, attribute=None):229        """230        Creates a html element. If ``text`` is given, the element's text will set and if a ``parent`` is given,231        the element is appended.232        :param tag: The html tag, e. g. ``'span'``. Defaults to ``None``.233        :param text: The text for the tag. Defaults to ``None``.234        :param parent: The parent element. Defaults to ``None``.235        :param class_id: Value for the class attribute236        :param attribute: Tuple name/value pair to add as an optional attribute237        """238        if text is not None:239            element = lxml.html.fragment_fromstring(str(text), create_parent=tag)240        else:241            element = lxml.html.Element(tag)242        if parent is not None:...LC_90_subsets.py
Source:LC_90_subsets.py  
1class Solution:2    # def _add_element(self, nums, start_idx, cur_result, results):3    #     results.append(cur_result)4    #     if start_idx >= len(nums):5    #         return6    #     prev_idx = None7    #     for i in xrange(start_idx, len(nums)):8    #         tmp_result = cur_result[:]9    #         tmp_result.append(nums[i])10    #         if prev_idx is not None and nums[i] == nums[prev_idx]:11    #             continue12    #         else:13    #             self._add_element(nums, i+1, tmp_result, results)14    #             prev_idx = i15    def _add_element(self, nums, start_idx, cur_result, results):16        results.append(cur_result)17        if start_idx >= len(nums):18            return19        tmp_result, used = cur_result[:], set()20        for i in xrange(start_idx, len(nums)):21            if nums[i] in used:22                continue23            # print(start_idx, i, tmp_result)24            used.add(nums[i])25            tmp_result.append(nums[i])26            self._add_element(nums, i + 1, tmp_result, results)27            tmp_result = tmp_result[:-1]28    def subsetsWithDup(self, nums):29        if len(nums) == 0:30            return []31        nums.sort()32        results = []33        self._add_element(nums, 0, [], results)34        return results35sol = Solution()36nums = [1, 2, 3]37#nums = [4,1,0]38#nums = [1, 2, 2]39#nums = [1, 5, 5, 5]40nums = [4,4,4]41print(sol.subsetsWithDup(nums))42nums = [4,4,4,1,4]...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!!
