Programming Articles - Page 2108 of 3363

Combination Sum in Python

Arnab Chakraborty
Updated on 04-May-2020 05:44:46

2K+ Views

Suppose we have a set of candidate numbers (all elements are unique) and a target number. We have to find all unique combinations in candidates where the candidate numbers sum to the given target. The same repeated number may be chosen from candidates unlimited number of times. So if the elements are [2, 3, 6, 7] and the target value is 7, then the possible output will be [[7], [2, 2, 3]]Let us see the steps −We will solve this in recursive manner. The recursive function is named as solve(). This takes an array to store results, one map to ... Read More

Next Permutation in Python

Arnab Chakraborty
Updated on 04-May-2020 05:42:40

4K+ Views

Suppose we want to implement the next permutation method, that method rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, this method will rearrange it as the lowest possible order (That is actually, sorted in ascending order). The replacement must be in-place and do not use any extra memory. For example, if the Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1, 2, 3 → 1, 3, 2 3, 2, 1 → 1, 2, 3 1, 1, 5 → 1, 5, 1Let us see the steps −found ... Read More

Tkinter Programming in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:34:19

3K+ Views

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.Creating a GUI application using Tkinter is an easy task. All you need to do is perform the following steps −Import the Tkinter module.Create the GUI application main window.Add one or more of the above-mentioned widgets to the GUI application.Enter the main event loop to take action against each event triggered by the user.Example#!/usr/bin/python import Tkinter top = Tkinter.Tk() # Code to add widgets will go here... top.mainloop()This would ... Read More

Parsing XML with DOM APIs in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:32:17

2K+ Views

The Document Object Model ("DOM") is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents.The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time. If you are looking at one SAX element, you have no access to another.Here is the easiest way to quickly load an XML document and to create a minidom object using the xml.dom module. The minidom object provides a simple parser method that quickly creates a DOM tree from the XML file.The sample phrase calls the ... Read More

Parsing XML with SAX APIs in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:30:33

8K+ Views

SAX is a standard interface for event-driven XML parsing. Parsing XML with SAX generally requires you to create your own ContentHandler by subclassing xml.sax.ContentHandler.Your ContentHandler handles the particular tags and attributes of your flavor(s) of XML. A ContentHandler object provides methods to handle various parsing events. Its owning parser calls ContentHandler methods as it parses the XML file.The methods startDocument and endDocument are called at the start and the end of the XML file. The method characters(text) is passed character data of the XML file via the parameter text.The ContentHandler is called at the start and end of each element. If the parser is not in namespace mode, ... Read More

Multithreaded Priority Queue in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:24:30

1K+ Views

The Queue module allows you to create a new queue object that can hold a specific number of items. There are following methods to control the Queue −get() − The get() removes and returns an item from the queue.put() − The put adds item to a queue.qsize() − The qsize() returns the number of items that are currently in the queue.empty() − The empty( ) returns True if queue is empty; otherwise, False.full() − the full() returns True if queue is full; otherwise, False.Example#!/usr/bin/python import Queue import threading import time exitFlag = 0 class myThread (threading.Thread):    def __init__(self, threadID, name, q): ... Read More

Synchronizing Threads in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:21:11

11K+ Views

The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock.The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread waits to acquire the lock.If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released.The release() method ... Read More

The Threading Module in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:18:04

11K+ Views

The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module discussed in the previous section.The threading module exposes all the methods of the thread module and provides some additional methods −threading.activeCount() − Returns the number of thread objects that are active.threading.currentThread() − Returns the number of thread objects in the caller's thread control.threading.enumerate() − Returns a list of all thread objects that are currently active.In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows −run()  − The run() method is the entry point ... Read More

Starting a New Thread in Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:08:18

488 Views

To spawn another thread, you need to call following method available in thread module −thread.start_new_thread ( function, args[, kwargs] )This method call enables a fast and efficient way to create new threads in both Linux and Windows.The method call returns immediately and the child thread starts and calls function with the passed list of args. When function returns, the thread terminates.Here,  args is a tuple of arguments; use an empty tuple to call function without passing any arguments. kwargs is an optional dictionary of keyword arguments.Example#!/usr/bin/python import thread import time # Define a function for the thread def print_time( threadName, delay):    count = 0   ... Read More

Sending Attachments as an E-mail using Python

Mohd Mohtashim
Updated on 31-Jan-2020 10:09:56

736 Views

To send an e-mail with mixed content requires to set Content-type header to multipart/mixed. Then, text and attachment sections can be specified within boundaries.A boundary is started with two hyphens followed by a unique number, which cannot appear in the message part of the e-mail. A final boundary denoting the e-mail's final section must also end with two hyphens.Attached files should be encoded with the pack("m") function to have base64 encoding before transmission.ExampleFollowing is the example, which sends a file /tmp/test.txt as an attachment. Try it once −#!/usr/bin/python import smtplib import base64 filename = "/tmp/test.txt" # Read a file and ... Read More

Advertisements