Float Layout in Kivy

Pradeep Elance
Updated on 12-Jan-2021 13:41:42

611 Views

Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is used to develop the Android application, as well as Desktops applications. In this article we will see how to use the FloatLayout widget to create buttons of at different positions in a window. The position of the button can be absolute or relative with respect to the bigger window.With Absolute positionIn this approach we set the position of the button at a fixed position. So when the resize of the window happens, the size of ... Read More

Python File Parameter in Print

Pradeep Elance
Updated on 12-Jan-2021 13:36:23

1K+ Views

The regular use of the print() function is to display text either in the command-line or in the interactive interpreter. But the same function can also write into a file or an output stream.Printing to fileIn the example we can open a file with a new filename in write mode then mention that filename in the print function. The value to be written to the file can be passed as arguments into the print function.ExampleNewfile= open("exam_score.txt", "w") # variables exam_name = "Degree" exam_date = "2-Nov" exam_score = 323 print(exam_name, exam_date, exam_score, file=Newfile , sep = ", ") ... Read More

Extract Specific Keys from Dictionary in Python

Pradeep Elance
Updated on 12-Jan-2021 13:33:40

2K+ Views

Dictionaries are most extensively used data structures in python. They contain data in form of keys and values. In this example we will see how to get the items form a dictionary specific to a given set of keys.With dictionary comprehensionIn this approach we simply loop through the dictionary using a for loop with in operator. But along with the in operator we also mention the values of the keys when referring to the dictionary keys.ExampledictA = {'Sun': '2 PM', "Tue": '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'} # Given dictionary print("Given dictionary : ", dictA) res ... Read More

Count Set Bits in a Range in Python

Pradeep Elance
Updated on 12-Jan-2021 13:31:45

338 Views

A given positive number when converted to binary, has a number of setbits. Set bits in a binary number is represented by 1. In this article we will see how to get the number of setbits in a given number after it is converted to binary value.Using bin and slicingIn the below example we take a number and apply the bin function to get the binary value. Then we slice it to remove the prefixes added to a binary number and then apply the range function to get the coutn of setbits.Example Live Demodef SetBits_cnt(n, l, r):    bin_val = bin(n) ... Read More

Convert Nested Dictionary into Flattened Dictionary in Python

Pradeep Elance
Updated on 12-Jan-2021 13:28:35

2K+ Views

As the world embraces more unstructured data, we come across many formats of data where the data structure can be deeply nested like nested JSONS. Python has the ability to deal with nested data structure by concatenating the inner keys with outer keys to flatten the data. In this article we will take a nested dictionary and flattened it.Using a recursive approachIn this approach we design a function to recursively process each item in the dictionary. We pass the dictionary, design a place holder for the output dictionary, the key and separator as parameters. We use the isinstance to check ... Read More

Python Checkbox Widget in Kivy

Pradeep Elance
Updated on 12-Jan-2021 13:21:28

621 Views

Kivy is an open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is used to develop the Android application, as well as Desktops applications. In this article we will see how to use the GridLayout and CheckBox.After importing the relevant modules, we create a grid layout with 2 columns. One to hold the label and another to hold the checkboxes.Exampleimport kivy from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.label import Label from kivy.uix.checkbox import CheckBox from kivy.uix.gridlayout import GridLayout # Container class for the app's ... Read More

Check If Suffix Matches with Any String in Given List in Python

Pradeep Elance
Updated on 12-Jan-2021 13:17:24

428 Views

Many times we need to analyze if a given word is present in a given list. That helps us in further processing the business logic for the data. In this article we see how to find if a given suffix which is a string is present in a list with many strings.Using anyThe any() function in python returns True if an item is present in an iterable. If nto it will return False. So in the below program we design if clauses to check for the presence or absence of the given string in the list.Example Live Demo# Given List lstA ... Read More

Memory-Mapped File Support in Python mmap

Pradeep Elance
Updated on 12-Jan-2021 13:14:23

2K+ Views

When you read a file object to a python program and want to modify, it can be done in two ways. First way is to modify the content in the physical storage drive where the file is located and the second way is to modify it directly in the memory or Ram of the system. In this article we will see how to read ,search and modify the content of a file object using the mmap module available in python. Instead of making system calls such as open, read and lseek to manipulate a file, memory-mapping puts the data of ... Read More

HTML Support in Python

Pradeep Elance
Updated on 12-Jan-2021 13:08:28

305 Views

Python has the capability to process the HTML files through the HTMLParser class in the html.parser module. It can detect the nature of the HTML tags their position and many other properties of the tags. It has functions which can also identify and fetch the data present in an HTML file.In the below example we see how to use the HTMLParser class to create a custom parser class which can only process the tags and data that we define in the class. Here we are processing the start tag, end tag and data.Below is the html which is getting processed ... Read More

Get Real-Time Currency Exchange Rate in Python

Pradeep Elance
Updated on 12-Jan-2021 13:04:42

12K+ Views

Python is very good at handling API calls. In this article we will see how we can handle the API calls for currency exchange rates in real time as well as historical.Using forex-pythonThis module provides the most direct way of getting the currency conversion rates. It has functions and parameters which can take inputs for the required currency codes and then give the result for the conversion. The below example gives the live conversion rate.Examplefrom forex_python.converter import CurrencyRates c = CurrencyRates() print(c.get_rate('USD', 'GBP'))OutputRunning the above code gives us the following result −0.7357387755Historical currency ratesWe add a datetime object ... Read More

Advertisements