Found 10476 Articles for Python

Python - Check if k occurs atleast n times in a list

Pradeep Elance
Updated on 28-Dec-2020 11:15:57

334 Views

Many times during data analysis using lists we come across a situation where we need to find out if a given element is present at least N Times in the given list. For example if 5 is present at least three times in the list or not. In this article we will see 2 approaches on how to achieve this.Counting OccurrencesIn the below approach we take the number and it's occurrences as an input. Then we e designer follow to keep the count of the occurrences. If the count value is greater than or equal to the required value then ... Read More

Pygorithm module in Python

Pradeep Elance
Updated on 28-Dec-2020 11:14:03

381 Views

The Pygorithm module is an educational module containing the implementation of various algorithms. The best use of this module is to get the code of an algorithm implemented using python. But it can also be used for actual programming where we can apply the various algorithms to a given data set.Finding the Data StructuresAfter installing the module in the python environment we can find the various data structures that is present in the package.Examplefrom pygorithm import data_structures help(data_structuresRunning the above code gives us the following result −OutputHelp on package pygorithm.data_structures in pygorithm: NAME    pygorithm.data_structures - Collection of data structure ... Read More

OS Path module in Python

Pradeep Elance
Updated on 28-Dec-2020 11:11:29

10K+ Views

The os.path module is a very extensively used module that is handy when processing files from different places in the system. It is used for different purposes such as for merging, normalizing and retrieving path names in python . All of these functions accept either only bytes or only string objects as their parameters. Its results are specific to the OS on which it is being run.os.path.basenameThis function gives us the last part of the path which may be a folder or a file name. Please the difference in how the path is mentioned in Windows and Linux in terms ... Read More

Oracle Database Connection in Python

Pradeep Elance
Updated on 28-Dec-2020 11:10:19

2K+ Views

Python can connect to oracle using a python package called cx_Oracle. Oracle is one of the famous and widely used database and python’s data processing features are leverages well using this connectivity. In this article we will see how we can connect to oracle database and query the DB.Installing cx_OracleWe can use the below command to install the python package which can be used for establishing the connectivity.Examplepip install cx_OracleConnecting to OracleNow using this module we can connect to a oracle database which is accessible through the oracle service name. We create a cursor and execute the SQl query through ... Read More

The netrc file processing using Python

Pradeep Elance
Updated on 28-Dec-2020 11:08:34

1K+ Views

The netrc class in python is used to read the data from the .netrc file presnt in unix systems in user’s home firectory. These are hidden files containing user’s login credential details. This is helpful for tool slike ftp, curl etc to successfully read the ,netrc file and use it for their actions.The below program shows how we can read the .netrc file using python’s netrc module.Exampleimport netrc netrc = netrc.netrc() remoteHostName = "hostname" authTokens = netrc.authenticators(remoteHostName) # Print the access tokens print("Remote Host Name:%s" % (remoteHostName)) print("User Name at remote host:%s" % (authTokens[0])) print("Account Password:%s" % (authTokens[1])) print("Password for ... Read More

Namespaces and Scope in Python

gireesha Devara
Updated on 01-Sep-2025 11:46:57

2K+ Views

In Python we work with variables, functions, libraries and modules, etc. Sometimes, the variable name you choose may already exist as the name of another variable, function, or method. This can be confusing or it might lead to unexpected behavior in your program. In such scenario, we need to learn about how all these names are managed in Python. This is the concept of namespaces and scoping. Namespaces in Python A namespace is a mapping from names to objects, which means stores the names you define in your program (such as, variable names, function names, class names, etc.) and maps ... Read More

Running Selenium Webdriver with a proxy in Python.

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:07:38

4K+ Views

We can run a proxy with Selenium webdriver in Python. A proxy is an essential component to do localization testing. We can take an e-commerce application and check if the language and currency visible is as per the user location.With the help of proxy within tests, we can verify if the website user interface matches with the location. We have to SET a proxy with below steps −Import webdriver from Selenium package.Define proxy server address.Create an object of ChromeOptions classCommunication of proxy with ChromeOptions.Summing options to Chrome() object.ExampleCode Implementation.from selenium import webdriver #proxy server definition py = "128.21.0.0:8080" #configure ChromeOptions ... Read More

Encode and decode XDR data using Python xdrlib

Pradeep Elance
Updated on 28-Dec-2020 11:04:10

606 Views

Encoders and decoders for the External Data Representation (XDR). When we transport data between different external sources, this is the commonly used format that is used. It useful for creation and transfer of complex data structures. XDR provides a service associated with the OSI Presentation Layer.In the below program we see how the data is getting packed and unpacked using the xdrlib module.Exampleimport xdrlib p = xdrlib.Packer() print(type(p)) lst = [1, 2, 3] p.pack_list(lst, p.pack_int) print(p) u = xdrlib.Unpacker(p) print(type(u)) print(lst)Running the above code gives us the following result −Output [1, 2, 3]Read More

Encode and decode uuencode files using Python

Pradeep Elance
Updated on 28-Dec-2020 10:57:25

1K+ Views

It is a common requirement during file transfers to encode and decode them for various reasons like encryption, compression or just because they are going to be processed by different OS or file reading programs. The uuencode module helps us on both encoding and decoding files as shown below.Encode the fileWe will use the below image for encoding and later decoding it to get it back.In the below program we use the encode function to encode the given image and read the content of the file after encoding.Exampleimport uu infile = "E:\tp_logo.JPG" uu.encode(infile, 'encoded_logo.JPG') f = open("E:\TP\encoded_logo.JPG", 'r') ... Read More

Encode and decode MIME quoted-printable data using Python

Pradeep Elance
Updated on 28-Dec-2020 10:55:16

2K+ Views

Many times we need to deal with data which not always has the regular ASCII characters. For example, an email in a different language other than English. Python has mechanism to deal with such characters by using MIME (Multipurpose Internet Mail Extensions) based module. In this article we will see how we can decode such characters in an email or otherwise in some straight inputs.Using email packageThe email package has modules namely mime and charset which can carry out the encoding and decoding work as shown in the below example. We have taken an email message containing Unicode characters and ... Read More

Advertisements