skip to navigation
skip to content

Planet Python

Last update: June 18, 2021 04:40 PM UTC

June 18, 2021


death and gravity

When your functions take the same arguments, consider using a class: counter-examples

In a previous article, I talk about this heuristic for using classes in Python:

If you have functions that take the same set of arguments, consider using a class.

Thing is, heuristics don't always work.

To make the most out of them, it helps to know what the exceptions are.

So, let's look at a few real-world examples where functions taking the same arguments don't necessarily make a class.

Counter-example: two sets of arguments #

Consider the following scenario:

We have a feed reader web application. It shows a list of feeds and a list of entries (articles), filtered in various ways.

Because we want to do the same thing from the command-line, we pull database-specific logic into functions in a separate module. The functions take a database connection and other arguments, query the database, and return the results.

def get_entries(db, feed=None, read=None, important=None): ...
def get_entry_counts(db, feed=None, read=None, important=None): ...
def search_entries(db, query, feed=None, read=None, important=None): ...
def get_feeds(db): ...

The main usage pattern is: at the start of the program, connect to the database; depending on user input, repeatedly call the functions with the same connection, but different options.


Taking the heuristic to the extreme, we end up with this:

class Storage:

    def __init__(self, db, feed=None, read=None, important=None):
        self._db = db
        self._feed = feed
        self._read = read
        self._important = important

    def get_entries(self): ...
    def get_entry_counts(self): ...
    def search_entries(self, query): ...
    def get_feeds(self): ...

This is not very useful: every time we change the options, we need to create a new Storage object (or worse, have a single one and change its attributes). Also, get_feeds() doesn't even use them – but somehow leaving it out seems just as bad.

What's missing is a bit of nuance: there isn't one set of arguments, there are two, and one of them changes more often than the other.

Let's take care of the obvious one first.

The database connection changes least often, so it makes sense to keep it on the storage, and pass a storage object around:

class Storage:

    def __init__(self, db):
        self._db = db

    def get_entries(self, feed=None, read=None, important=None): ...
    def get_entry_counts(self, feed=None, read=None, important=None): ...
    def search_entries(self, query, feed=None, read=None, important=None): ...
    def get_feeds(self): ...

The most important benefit of this is that it abstracts the database from the code using it, allowing you to have more than one kind of storage.

Want to store entries as files on disk? Write a FileStorage class that reads them from there. Want to test your application with various combinations of made-up entries? Write a MockStorage class that keeps the entries in in a list, in memory. Whoever calls get_entries() or search_entries() doesn't have to know or care where the entries are coming from or how the search is implemented.

This is the data access object design pattern. In object-oriented programming terminology, a DAO provides an abstract interface that encapsulates a persistence mechanism.


OK, the above looks just about right to me – I wouldn't really change anything else.

Some arguments are still repeating, but it's useful repetition: once a user learns to filter entries with one method, they can do it with any of them. Also, people use different arguments at different times; from their perspective, it's not really repetition.

And anyway, we're already using a class...

Counter-example: data classes #

Let's add more requirements.

There's more functionality beyond storing things, and we have multiple users for that as well (web app, CLI, someone using our code as a library). So we leave Storage to do only storage, and wrap it in a Reader object that has a storage:

class Reader:

    def __init__(self, storage):
        self._storage = storage

    def get_entries(self, feed=None, read=None, important=None):
        return self._storage.get_entries(feed=feed, read=read, important=important)

    ...

    def update_feeds(self):
        # calls various storage methods multiple times:
        # get feeds to be retrieved from storage,
        # store new/modified entries
        ...

Now, the main caller of Storage.get_entries() is Reader.get_entries(). Furthermore, the filter arguments are rarely used directly by storage methods, most of the time they're passed to helper functions:

class Storage:

    def get_entries(self, feed=None, read=None, important=None):
        query = make_get_entries_query(feed=feed, read=read, important=important)
        ...

Problem: When we add a new entry filter option, we have to change the Reader methods, the Storage methods, and the helpers. And it's likely we'll do so in the future.

Solution: Group the arguments in a class that contains only data.

from typing import NamedTuple, Optional

class EntryFilterOptions(NamedTuple):
    feed: Optional[str] = None
    read: Optional[bool] = None
    important: Optional[bool] = None

class Storage:

    ...

    def get_entries(self, filter_options):
        query = make_get_entries_query(filter_options)
        ...

    def get_entry_counts(self, filter_options): ...
    def search_entries(self, query, filter_options): ...
    def get_feeds(self): ...

Now, regardless of how much they're passed around, there are only two places where it matters what the options are:

Note that while we're using the Python class syntax, EntryFilterOptions is not a class in the traditional object-oriented programming sense, since it has no behavior.1 Sometimes, these are known as "passive data structures" or "plain old data".

A plain class or a dataclass would have been a decent choice as well; why I chose a named tuple is a discussion for another article.

I used type hints because it's a cheap way of documenting the options, but you don't have to, not even for dataclasses.

The example above is a simplified version of the code in my feed reader library. In the real world, EntryFilterOptions has more options (with more on the way), and the Reader and Storage get_entries() are a bit more complicated.

Another real-world example of this pattern is Requests:


That's pretty much it for now – hang around for some extra stuff, though ;)

I hope I managed add more nuance to the original article, and that you're now at least a little bit better equipped to use classes. Keep in mind that this is more an art than a science, and that you can always change your mind later.

Learned something new today? Share this with others, it really helps!


Bonus: other alternatives #

Still here? Cool!

Let's look at some of the other options I considered, and why I didn't go that way.

Why not a dict? #

Instead of defining a whole new class, we could've used a dict:

{'feed': ..., 'read': ..., 'important': ...}

But this has a number of drawbacks:

Why not **kwargs? #

Why not pass **kwargs directly to EntryFilterOptions?

class Reader:
    def get_entries(self, **kwargs):
        return self._storage.get_entries(**kwargs)

Because:

Why not EntryFilterOptions? #

Why not take an EntryFilterOptions directly, then?

from reader import make_reader, EntryFilterOptions
reader = make_reader(...)
options = EntryFilterOptions(read=True)
entries = reader.get_entries(options)

Because it makes things verbose for the user: they have to import EntryFilterOptions, and build and pass one to get_entries() for every call. That's not very friendly.

The Reader and Storage method signatures differ because they're used differently:


  1. Ted Kaminski discusses this distinction in more detail in Data, objects, and how we're railroaded into poor design. [return]

June 18, 2021 03:02 PM UTC


Stack Abuse

Matplotlib: Turn Off Axis (Spines, Tick Labels, Axis Labels and Grid)

Introduction

Matplotlib is one of the most widely used data visualization libraries in Python. Much of Matplotlib's popularity comes from its customization options - you can tweak just about any element from its hierarchy of objects.

In this tutorial, we'll take a look at how to turn off a Matplotlib plot's axis. That is to say - how to turn off individual elements, such as tick labels, grid, ticks and individual spines, but also how to turn them all off as a group, collectivelly referred to as the axis.

Most of the approaches we'll be covering work the same for 2D and 3D plots - both the regular Axes and Axes3D classes have the same methods for this. For some, since 3D plots have more than two spines, they won't be the same.

Note: For the remainder of this guide, we'll be using a 3D plot, as this is a more common case where you might want to turn off the spines and ticks - and let the data speak for itself, as well as a 2D plot on the same dataset.

We'll specifically be using a recreation of the CP1919 pulsar's data, as discovered by Jocelyn Bell Burnell in 1967, visualized by by Harold D. Craft, Jr. in his PhD Thesis in 1970 and popularized by Peter Saville in 1979, when the English band Joy Division was on the lookout to release a new album - "Unknown Pleasures":

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

df = pd.read_csv(r"https://raw.githubusercontent.com/StackAbuse/CP1919/master/data-raw/clean.csv")
groups = df.groupby(['line'])

plt.style.use('dark_background')

fig = plt.figure(figsize=(6, 8))

ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('Line Number')
ax.set_ylabel('Time')
ax.set_zlabel('Intensity')

for group in groups:
    line = ax.plot(group[1]['line'], group[1]['x'], group[1]['y'], color='white')

plt.show()

Which results in:

3D Ridge Plot in Matplotlib - CP1919

If you'd like to learn more about Ridge Plots (shown above), build a foundation in Matplotlib and explore advanced usage of the library, make sure to check out our Data Visualization in Python eBoook Bundle. This specific visualization is taken straight out of the book, from a longer section dedicated to this dataset and the history of the plot.

Now, let's take a look at how to remove the spines, tick labels, ticks, grid and axis labels.

Turning off the Axis with ax.axis('off')

The easiest way to turn off everything axis-related is via a convenience function - axis('off') on your Axes or Axes3D instance:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load dataset in and set style...

fig = plt.figure(figsize=(6, 8))

ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('Line Number')
ax.set_ylabel('Time')
ax.set_zlabel('Intensity')
ax.axis('off')

for group in groups:
    line = ax.plot(group[1]['line'], group[1]['x'], group[1]['y'], color='white')

plt.show()

Note: You can also use the axis('off') function on the plt instance if you're plotting using the MATLAB-style API. Though, this approach is discouraged if you're plotting using the OOP API like we are currently.

This change to the code results in a plot without the spines, axis lines, ticks, tick labels, axis labels and grid:

Turn axis off with axis('off')

The axis() function is a convennience function to access various properties.

Turning off the Axis with ax.set_axis_off()

Alternatively, you can use the ax.set_axis_off() function, in conjecture with the ax.set_axis_on() function, which reverses the former's effects.

This is a very useful set of functions to use when updating plots, such as when animating them, or whenever you might want to turn the axis off and on, rather than just disabling it:\

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Load dataset in and set style...

fig = plt.figure(figsize=(6, 8))

ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('Line Number')
ax.set_ylabel('Time')
ax.set_zlabel('Intensity')
ax.set_axis_off()

for group in groups:
    line = ax.plot(group[1]['line'], group[1]['x'], group[1]['y'], color='white')

plt.show()

This results in the exact same plot:

Turn axis off with set_axis_off()

Turning off Ticks and Tick Labels on Axes

Instead of turning all of these elements off, and making them invisible, you can also turn them off individually if you'd like to keep some.

This is where we'll be switching back to a 2D plot, since some of these functions don't work with 3D plots, given the fact that they've got more than just the X-axis and Y-axis.

For example, you can turn off individual axes (ticks and tick labels). You can use the get_yaxis() or get_xaxis() to get the respective axes and turn them off by using set_visible(False):

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv(r"https://raw.githubusercontent.com/StackAbuse/CP1919/master/data-raw/clean.csv")
groups = df.groupby(['line'])

plt.style.use('dark_background')
fig = plt.figure(figsize=(6, 8))

ax = fig.add_subplot(111)
ax.set_xlabel('Time')
ax.set_ylabel('Intensity')

ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

for group in groups:
    line = ax.plot(group[1]['x'], group[1]['y'], color='white')

plt.show()

This turns off the tick labels and ticks themselves, but leaves the frame (spines) on:

Turn ticks and tick labels off matplotlib

Turning off Individual Spines on an Axes

To turn the spines off - you can access them via the ax.spines dictionary. Using their keys, top, bottom, left, and right, you can select each one, and using the set_visible() function, turn them off.

Let's turn of the top and right spine:

import pandas as pd
import matplotlib.pyplot as plt

# Load data and stylize

fig = plt.figure(figsize=(6, 8))

ax = fig.add_subplot(111)
ax.set_xlabel('Time')
ax.set_ylabel('Intensity')

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# To turn off the bottom or left
#ax.spines['bottom'].set_visible(False)
#ax.spines['left'].set_visible(False)

for group in groups:
    line = ax.plot(group[1]['x'], group[1]['y'], color='white')

plt.show()

This turns off the top and right spine, but leaves the bottom and left intact - as well as the ticks and their labels:

turn individual spines off in matplotlib

You can also iterate through them, and turn them all off:

for key, spine in ax.spines.items():
    spine.set_visible(False)

turn all spines off in matplotlib

Conclusion

In this tutorial, we've gone over several ways to turn off the axis, as well as individual axis compoents in Matplotlib.

If you're interested in Data Visualization and don't know where to start, make sure to check out our bundle of books on Data Visualization in Python:

Data Visualization in Python

Become dangerous with Data Visualization

✅ 30-day no-question money-back guarantee

✅ Beginner to Advanced

✅ Updated regularly for free (latest update in April 2021)

✅ Updated with bonus resources and guides

Data Visualization in Python with Matplotlib and Pandas is a book designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and allow them to build a strong foundation for advanced work with theses libraries - from simple plots to animated 3D plots with interactive buttons.

It serves as an in-depth, guide that'll teach you everything you need to know about Pandas and Matplotlib, including how to construct plot types that aren't built into the library itself.

Data Visualization in Python, a book for beginner to intermediate Python developers, guides you through simple data manipulation with Pandas, cover core plotting libraries like Matplotlib and Seaborn, and show you how to take advantage of declarative and experimental libraries like Altair. More specifically, over the span of 11 chapters this book covers 9 Python libraries: Pandas, Matplotlib, Seaborn, Bokeh, Altair, Plotly, GGPlot, GeoPandas, and VisPy.

It serves as a unique, practical guide to Data Visualization, in a plethora of tools you might use in your career.

June 18, 2021 12:30 PM UTC


Real Python

The Real Python Podcast – Episode #65: Expanding the International Python Community With the PSF

The popularity of Python is continuing to grow Developers across the globe are embracing the language. How is Python being used in all of these different countries? How does an organization like the Python Software Foundation (PSF) work toward the goals in its mission statement for supporting and growing this international community? This week on the show, we have Marlene Mhangami, a PSF board member and part of the Diversity and Inclusion Work Group.


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

June 18, 2021 12:00 PM UTC

June 17, 2021


TestDriven.io

Basic and Full-text Search with Django and Postgres

This article looks at how to add basic and full-text search to a Django app with Postgres.

June 17, 2021 10:28 PM UTC


Mike Driscoll

Creating a Warhol Triptych Image with Pillow and Python

Andy Warhol is a well-known artist who created a famous image where there are multiple copies of the same face but with different background colors.

You can do a similar trick with software using Python and the Pillow package. You will also need to have NumPy installed to make this work.

Let’s find out how this all works!

Getting Started

The first thing you need to do is make sure you have Pillow and NumPy installed. If you use pip, you can try running the following command:

python3 -m pip install numpy Pillow

This will install NumPy and Pillow if you don’t already have them installed.

If you are running Anaconda instead, then both of these packages should already be installed.

Now you’re ready to create some art!

Creating a Triptych

Creating a Triptych with Python doesn’t take a lot of code. All you need is a little know-how and some experimentation. The first step is to learn why you need NumPy.

NumPy isn’t so much a replacement for Pillow as it is a way to enhance Pillow’s capabilities. You can use NumPy to do some of the things that Pillow does natively. For the examples in this section, you will use this photo of the author:

Michael DriscollMichael Driscoll

To get a feel for how you might use NumPy with Pillow, you will create a Python program that concatenates several images together. This will create your Triptych image! Open up your Python editor and create a new file named concatenating.py. Then enter this code in it:

# concatenating.py

import numpy as np
from PIL import Image


def concatenate(input_image_path, output_path):
    image = np.array(Image.open(input_image_path))

    red = image.copy()
    red[:, :, (1, 2)] = 0

    green = image.copy()
    green[:, :, (0, 2)] = 0

    blue = image.copy()
    blue[:, :, (0, 1)] = 0

    rgb = np.concatenate((red, green, blue), axis=1)
    output = Image.fromarray(rgb)
    output.save(output_path)

if __name__ == "__main__":
    concatenate("author.jpg", "stacked.jpg")

This code will open up the image using Pillow. However, rather than saving that image off as an Image object, you pass that object into a Numpy array(). Then you create three copies of the array and use some matrix math to zero out the other color channels. For example, for red, you zero out the green and blue channels, leaving the red channel alone.

When you do this, it will create three tinted versions of the original image. You will now have a red, green, and blue version of the photo. Then you use NumPy to concatenate the three images together into one.

To save this new image, you use Pillow’s Image.fromarray() method to transform the NumPy array back into a Pillow Image object.

After running this code, you will see the following result:

Python Triptych

hat’s a neat effect!

NumPy can do other things that Pillow doesn’t do easily, like Binarization or denoising. You can do even more when you combine NumPy with other scientific Python packages, such as SciPy or Pandas.

Wrapping Up

Python is powerful. You can do many things with Python, Pillow and NumPy. You should try doing this with your own image(s) and see what you can come up with. For example, instead of lining the images up left-to-right, you could create four images and put them together in a square!

Related Reading

The post Creating a Warhol Triptych Image with Pillow and Python appeared first on Mouse Vs Python.

June 17, 2021 08:36 PM UTC


Codementor

HACKERRANK HOURGLASS CHALLENGE USING PYTHON

A Solution To A Hackerrank Challenge

June 17, 2021 06:28 PM UTC


Python Insider

Python 3.10.0b3 is available

Summer is almost here (at least in half of the planet) and Python 3.10 is finishing baking in the oven. For those of you that want to taste it before is finally ready (and if you are a library developer, you certainly do!) you can have the second-to-last beta now, but be careful as is very hot :wink:

https://www.python.org/downloads/release/python-3100b3/

This is a beta preview of Python 3.10

Python 3.10 is still in development. 3.10.0b3 is the third of four planned beta release previews. Beta release previews are intended to give the wider community the opportunity to test new features and bug fixes and to prepare their projects to support the new feature release.

We **strongly encourage** maintainers of third-party Python projects to **test with 3.10** during the beta phase and report issues found to [the Python bug tracker](https://bugs.python.org/) as soon as possible. While the release is planned to be feature complete entering the beta phase, it is possible that features may be modified or, in rare cases, deleted up until the start of the release candidate phase (Monday, 2021-08-02). Our goal is to have no ABI changes after beta 4 and as few code changes as possible after 3.10.0rc1, the first release candidate. To achieve that, it will be **extremely important** to get as much exposure for 3.10 as possible during the beta phase.

Please keep in mind that this is a preview release and its use is **not** recommended for production environments.

Major new features of the 3.10 series, compared to 3.9

Many new features for Python 3.10 are still being planned and written. Among the new major new features and changes so far:

The next pre-release of Python 3.10 will be 3.10.0b4, currently scheduled for Saturday, 2021-07-10.

bpo-38605from __future__ import annotations (PEP 563) used to be on this list in previous pre-releases but it has been postponed to Python 3.11 due to some compatibility concerns. You can read the Steering Council communication about it here to learn more.

More resources

And now for something completely different

There are no green stars. Why? In general, objects don't emit a single wavelength of light when they shine. Instead, they emit photons in a range of wavelengths. If you were to use some sort of detector that is sensitive to the wavelengths of light emitted by an object, and then plotted the number of them versus wavelength, you get a lopsided plot called a blackbody curve. For an object as hot as the Sun, that curve peaks at blue-green, so it emits most of its photons there. But it still emits some that are bluer, and some that are redder. When we look at the Sun, we see all these colors blended together. Our eyes mix them up to produce one color: white. A warmer star will put out more blue, and a cooler one redder, but no matter what, our eyes just won't see that as green. Due to how we perceive color, the only way to see a star as being green is for it to be only emitting green light. But as starts always emit radiation following the blackbody curve, that's pretty much impossible.

We hope you enjoy those new releases!

Thanks to all of the many volunteers who help make Python Development and these releases possible! Please consider supporting our efforts by volunteering yourself or through organization contributions to the Python Software Foundation.

Your friendly release team,

Pablo Galindo @pablogsal

Ned Deily @nad

Steve Dower @steve.dower

June 17, 2021 05:38 PM UTC


Python Engineering at Microsoft

Python in Visual Studio Code – June 2021 Release

We are pleased to announce that the June 2021 release of the Python Extension for Visual Studio Code is now available. You can download the Python extension from the Marketplace, or install it directly from the Extensions View in Visual Studio Code. If you already have the Python extension installed, you can also get the latest update by restarting Visual Studio Code. You can learn more about Python support in Visual Studio Code in the documentation.

This release includes support for VS Code’s Workspace Trust, Jump-To-Source code with the PyTorch Profiler and completions for dictionary keys with Pylance.

If you’re interested, you can check the full list of improvements in our changelog.

Workspace Trust

The newest release of VS Code introduced a Workspace Trust feature, that allows you to decide if you trust the project folders you open it in VS Code or not. If you don’t trust a folder or workspace, you are still able to browse your files in restricted mode.

Workspace Trust window in VS Code

When in a trusted workspace, the Python extension will automatically search for Python environments in your machine so you can select one for your project, and it will provide all of its features such as auto completions, code navigation, linting, formatting, testing, etc.

When opening folders that you don’t trust, you can browse the files in restricted mode. In this case, the Python extension will not load – and that is to prevent automatic, potentially malicious code execution.

Because you can always enable trust later, we recommend you open folders in restricted mode when in doubt. That way you can always inspect the files and content in the folder before you make a decision.

To manage Workspace Trust, you can run the “Workspace: Manage Workspace Trust” command in the command palette (View > Command Palette…).

Workspace trust management window in VS Code

Out of an abundance of caution, we strongly advise you to upgrade VS Code to version 1.57 for the protections provided by workspace trust. You can learn more about the Workspace Trust feature in the documentation.

PyTorch Profiler Jump-To-Source Code

One of the great benefits of having both TensorBoard and the PyTorch Profiler integrated directly in VS Code is having both your code and data science productivity tooling in the same space.

With the latest release of the Python extension, along with the latest PyTorch Profiling Plugin 0.2.0, we’ve added the ability to directly jump back to the source code (file and line) from the profiler stack traces.  When you click on a stack trace from the PyTorch Profiler, VS Code will automatically open the corresponding file side by side and highlight/focus directly on the line of code of interest. This allows you to quickly make actionable optimizations and changes to your code based on the Profiler results and suggestions.

Jumping to source from Tensorboard in VS Code

Completions for Dictionary Keys with Pylance

As of this release, Pylance now supports dictionary key completions. These completions will be provided for dictionaries created using dict() or literal syntax, {}.

Auto completions for dictionary keys with Pylance in VS Code

Other Changes and Enhancements

We have also added small enhancements and fixed issues requested by users that should improve your experience working with Python in Visual Studio Code. Some notable changes include:

  1. The Jupyter extension is now an optional dependency, which means you can keep using the Python extension if you uninstall or disable the Jupyter one. (#16102)
  2. Fix for bandit linter bug where messages weren’t being propagated to the editor. (thanks Anthony Shaw) (#15561)
  3. Fix for bug causing blank TensorBoard panes on Windows. (#16072)
  4. Enable controlled rollout for deprecating python.pythonPath and adding python.defaultInterpreterPath to the workspace level.  (#2125).

Be sure to download the Python extension for Visual Studio Code now to try out the above improvements. If you run into any problems or have suggestions, please file an issue on the Python VS Code GitHub page.

The post Python in Visual Studio Code – June 2021 Release appeared first on Python.

June 17, 2021 03:35 PM UTC


Python for Beginners

String to Integer in Python

During programming in Python, We often need to convert a string to an integer in Python. This is because the standard input in Python is always read as a string independent of the type of input. To use integer data in our program when it is passed as space separated integers, we need to convert the string input to integer after splitting them using Python string split operation . In this article, we will look at how we can convert a string to integer without any errors and will implement the programs in Python.

How to convert string to integer in Python?

We can use int() function to convert a string to integer in Python. The string which has to be converted to integer is passed to the int() function as input argument and the function returns the corresponding integer value if the string passed as input is in proper format and no error occurs during conversion of the string to integer. We can convert a string to integer using int() function as follows.

print("Input String is:")
myInput= "1117"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
1117
Output Integer is:
1117

When the input string is not in correct format, the int() function raises ValueError. This can be seen in the following example.

print("Input String is:")
myInput= "aditya1117"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
aditya1117
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-3-c8793975130e>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int() with base 10: 'aditya1117'

For which inputs ValueError will occur while converting a string to integer?

There may be several cases in which int() function will raise ValueError while converting a string to integer. Some of the cases are discussed below.

When we pass a string containing alphabets instead of numeric literals, ValueError will occur and input string will not be converted to integer. This can be seen in the following example.

print("Input String is:")
myInput= "aditya1117"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
aditya1117
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-10-c8793975130e>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int()

When the passed string contains any space characters along with numeric literals, ValueError will occur and input string will not be converted to integer. This can be seen in the following example.

print("Input String is:")
myInput= "11 17"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
11 17
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-4-46d411efb04b>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int() with base 10: '11 17'

When the passed string contains any punctuation marks such as period character (.) or comma (,) along with numeric literals, ValueError will occur and input string will not be converted to integer. This can be seen in the following example.


print("Input String is:")
myInput= "11.17"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
11.17
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-5-97993fa7ba5b>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int() with base 10: '11.17'

How to avoid ValueError while converting string to integer?

While converting a string to integer in Python, we can either preemptively check if the passed string consists of only the digits or not so that we can avoid the occurrence of error or we can use Python try except to handle the ValueError after it has been raised by the int() function. Both the methods have been discussed below.

We can use the isdigit() method to check if a string consists of only numeric characters or not. The isdigit() method when invoked on a string returns true if the string consists of only numeric digits. Otherwise it returns false. This can be implemented as follows.

print("Input String is:")
myInput= "1117"
print(myInput)
if myInput.isdigit():
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
else:
    print("Input cannot be converted into integer.")

Output:

Input String is:
1117
Output Integer is:
1117

If the input string contains characters other than numbers, output will be as follows.


print("Input String is:")
myInput= "aditya1117"
print(myInput)
if myInput.isdigit():
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
else:
    print("Input cannot be converted into integer.") 

Output:

Input String is:
aditya1117
Input cannot be converted into integer.

To handle the ValueError after it has occurred, we can use exception handling using  Python try except to handle the ValueError and show a proper message to the user as follows.


print("Input String is:")
myInput= "1117"
print(myInput)
try:
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
except ValueError:
    print("Input cannot be converted into integer.")

Output:

Input String is:
1117
Output Integer is:
1117

If the input string contains characters other than numbers, output will be as follows.

print("Input String is:")
myInput= "aditya1117"
print(myInput)
try:
    myInt=int(myInput)
    print("Output Integer is:")
    print(myInt)
except ValueError:
    print("Input cannot be converted into integer.")

Output:

Input String is:
aditya1117
Input cannot be converted into integer.

Conclusion

In this article, we have seen how we can convert a string to integer in Python and what problems can occur during conversion. We have also seen how to avoid and handle the ValueError raise by int() function during conversion of string to integer. Stay tuned for more informative articles. 

The post String to Integer in Python appeared first on PythonForBeginners.com.

June 17, 2021 02:59 PM UTC


PyCharm

Release Candidate for PyCharm 2021.1.3 Is Out

This Release Candidate for the minor PyCharm 2021.1.3 release contains a list of fixes to make your PyCharm 2021.1 experience smoother. Note that the following functionality is only available in PyCharm Professional: support for Django, frontend, and databases.

Thank you for all the feedback you’ve shared with us in our issue tracker!

DOWNLOAD PYCHARM 2021.1.3 RC

Here are the major bug fixes:

DOWNLOAD PYCHARM 2021.1.3 RC

For the full list of resolved issues, please see the release notes.

June 17, 2021 02:54 PM UTC


EuroPython

EuroPython 2021: Schedule published

After two weeks of hard work by our program workgroup, we are very excited to announce the EuroPython 2021 schedule:

altEuroPython 2021 Schedule

Seven full days of Python

EuroPython 2021 will be held online in the week of July 26:

Lots of engaging content waiting for you

The conference will be packed with interesting Python content, provided in many different formats and presented by our fantastic team of volunteers, speakers and sponsors:

We would like to thank all speakers and sponsors who submitted session proposals to the conference.

Never miss a talk

All talks will be made available to the attendees via streaming to our Matrix based conference platform, with easy switching between tracks and chat. We’ll also make the streams available with rewind functionality and give you access to the streams after the conference.

Conference Tickets

Conference tickets are available on our registration page. We have several ticket types available to make the conference affordable for everyone and we're also offering financial aid to increase our reach even more.

EuroPython is your conference

EuroPython has always been a completely volunteer based effort. The organizers work hundreds of hours to make the event happen and will try very hard to create an inspiring and exciting event.

However, we can only provide the setting. You, as our attendees, are the ones who fill it with life and creativity.

We are very much looking forward to having you at the conference !

Enjoy,
EuroPython 2021 Team
EuroPython Society
EuroPython 2021 Website

June 17, 2021 02:47 PM UTC


Codementor

5+ Best Front-end And Back-end Frameworks For Developers

Hi guys, if you want to become a full stack developer, you have a great choice between frontend and backend frameworks. While frontend frameworks are dominated by JavaScript, the backend has many features for building server-side applications, such as Java, Python, Ruby, and even JavaScript.

June 17, 2021 01:08 PM UTC


Python Anywhere

June system update brings easier task management

The most recent system upgrade brings a new way to organize scheduled and always-on tasks. A new optional description field, combined with sorting, allows you to manage big herds of multiple tasks.

Under the hood there were some more, larger changes, but we’ll be announcing the details of those later.

June 17, 2021 10:00 AM UTC


Full Stack Python

Setting Up Python 3, Django & Gunicorn on Linux Mint 17.3

Linux Mint 17.3 "Rosa" is December 2015 release of the polished and widely-used Linux distribution. This Mint release includes both Python 2.7 and 3.4 by default, but in this tutorial we will download and install the latest Python 3.5.1 version to run our Django application.

If you want to use a different Linux distribution such as Ubuntu instead of Mint, check out the tutorial for Ubuntu 16.04 "Xenial Xerus". If Mint is your desired development environment though, let's get started!

Tools We Need

Our setup will use several system packages and code libraries to get up and running. Do not worry about installing these dependencies just yet, we will get to them as we progress through the tutorial. The tools and their current versions as of June 2016 are:

If you are on Mac OS X or Windows, my recommendation is to use virtualization software such as Parallels or VirtualBox with the Linux Mint Cinnamon desktop .iso.

We should see a desktop screen like this one when we boot up the operating system for the first time.

Linux Mint default desktop

Open up terminal to proceed with the configuration.

System Packages

We can see the Python version Linux Mint comes with, as well as where its executable is stored.

python3 --version
which python3

The output of those two commands should be (these are not commands to run):

Python 3.4.3
/usr/bin/python3

Output of 'python --version' and 'which python3' commands.

We really want to use the latest Python release instead of the default 3.4 when starting a new Python project, so let's download and install 3.5.1 now.

Run these commands in the terminal to download Python 3.5.1 source code:

cd ~/Downloads
wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

wget Python source code output.

Extract the Python source code:

tar -xvf Python-3.5.1.tgz

Linux Mint is not configured by default to build the Python source code. We need to update our system package lists and install several packages to make building the Python source code possible. If you have a password on your user account, enter it when prompted to allow the installation to proceed.

sudo apt update
sudo apt install build-essential checkinstall
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev 
sudo apt install libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
sudo apt install python3-dev

Once the packages are installed, we can configure and install Python from source.

cd Python-3.5.1
./configure
sudo make install

Test that the installation worked properly by starting up the Python REPL:

python3.5

If the REPL starts up properly with Python 3.5.1 in the output then we're good to go.

wget Python source code output.

The basic system packages we need are now installed so we can proceed to our Python-specific dependencies.

Virtual environment and pip

Python 3.5 comes with the virtual environment and pip applications so we can use them to handle our application dependencies.

Create a directory to store virtual environments then create a virtualenv for our Django project.

# the tilde "~" specifies the user's home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
python3.5 -m venv djangoproj

Activate the virtualenv.

source ~/venvs/djangoproj/bin/activate

Our virtual environment is properly activated when we see (djangoproj) prepended to our prompt.

Output from the virtualenv environment activation.

Our virtualenv with Python 3.5.1 is activated so we can install whatever dependencies we want, such as Django and Gunicorn. Our default python command is also set to use the Python 3.5.1 installation instead of the Python 2.7 version that comes with Linux Mint.

Django and Gunicorn

Now we can install Django and Green Unicorn into our virtual environment.

pip install django==1.9.7 gunicorn==19.6

If there are no errors in the pip output then that is a good sign we can proceed.

Django and Gunicorn properly install via the pip command.

Create a new Django project named djangoproj, or whatever you want to name your project. Change into the directory for the new project.

cd ~
django-admin startproject djangoproj
cd djangoproj

We can run Django using the development server with the python manage.py runserver command. However, start Django up with Gunicorn instead.

gunicorn djangoproj.wsgi

Result of running gunicorn djangoproj.wsgi on the command line.

Awesome, we can bring up our shell project in the web browser at the http://localhost:8000 or http://127.0.0.1:8000 address.

Django project running in the Firefox web browser.

Now you're ready for Django development!

Ready for Development

Those are the first few steps for beginning development with Django and Gunicorn on Linux Mint 17.3 "Rosa". If you need an even more in-depth walkthrough for deploying your Python web application to a production environment, check out the Full Stack Python Guide to Deployments book.

To figure out what to do next for your Python project, read the topics found on the table of contents page.

Questions? Contact me via Twitter @fullstackpython or @mattmakai. I'm also on GitHub with the username mattmakai.

See something wrong in this post? Fork this page's source on GitHub and submit a pull request.

June 17, 2021 04:00 AM UTC


Brett Cannon

How I rate films

During the prerecording banter on the "Every commit is a gift" episode of the Changelog, we talked about films (which is no surprise considering I was an episode of Backstage all about the John Wick trilogy). I mentioned the rating system I used for films and Adam requested I blog about it so that he could have a permanent reference to it.

  1. ★☆☆☆☆☆☆☆☆☆ I didn&apost get past the first 15 minutes
  2. ★★☆☆☆☆☆☆☆☆ I gave the film an hour, then I gave up
  3. ★★★☆☆☆☆☆☆☆ I walked away before the end
  4. ★★★★☆☆☆☆☆☆ I let my attention wander while I watched, but I still finished it
  5. ★★★★★☆☆☆☆☆ No regrets watching the film for free, but I would have been fine skipping it as well
  6. ★★★★★★☆☆☆☆ No regrets renting the film, but I wouldn&apost spend money on it again
  7. ★★★★★★★☆☆☆ No regrets paying a full-price movie ticket, but I wouldn&apost spend the money to rent it again
  8. ★★★★★★★★☆☆ Paid for a full-price movie ticket to see the film and I&aposm willing to pay to rent it again if friends want to watch it
  9. ★★★★★★★★★☆ I would buy the film if it wasn&apost available for streaming in high-quality on a service I have a subscription to; no film can go higher than this based on a single viewing
  10. ★★★★★★★★★★ I have watched the film multiple times and I have no major qualms with it (and "major" can be awkward dialog or cinematography in a couple of scenes); I very likely own it in the highest fidelity possible

If you want to see how I have rated various films, you can see my ratings on my Letterboxd profile (I also have a Trakt profile, but that&aposs only used for TV shows now).

June 17, 2021 03:31 AM UTC


CodersLegacy

Comparison of Sorting Algorithms

Welcome to our Comparison on Sorting Algorithms Article. Here, we’ll be comparing the various sorting Algorithms out there on the basic of several factors.

We’ll top it all of by trying to describe where each Algorithm is best suited, and their strong and weak points. Every Algorithm is unique, and performs best under certain circumstances unique to it.


Comparison of Time Complexity

A table that show’s the time complexities for some of the most commonly used Sorting Algorithms. Time complexity is the first thing that you need to be checking when comparing two sorting algorithms. The lower the time complexity, the better.

Sorting AlgorithmAverage CaseBest CaseWorst Case
Bubble SortO(n2)O(n)O(n2)
Insertion SortO(n2)O(n)O(n2)
Selection SortO(n2)O(n2)O(n2)
Quick SortO(n.log(n))O(n.log(n))O(n2)
Merge SortO(n.log(n))O(n.log(n))O(n.log(n))
Heap SortO(n.log(n))O(n.log(n))O(n.log(n))
Counting SortO(n+k)O(n+k)O(n+k)
Radix SortO(n*k)O(n*k)O(n*k)
Bucket SortO(n+k)O(n+k)O(n2)

We’ve used a color scheme in the table above, to help with our Comparison of Sorting Algorithms. Red is the worst, under which the O(n2) Algorithms lie. The O(n.log(n)) Algorithms are next, which are the middle ground. The best time complexity is O(n), which is the fastest Algorithm can be.

Later when we do the actual field tests, you can use this table as reference. You will notice how much of an impact time complexity has on performance.


Comparison of Space Complexity

While speed is important and usually your top priority, sometimes in places with memory constraints, Algorithms with low memory costs are preferred.

The below table shows the Space Complexity for the various Sorting Algorithms. You might notice, that the Algorithms with higher space complexities are those which are “out of place” and the ones with the lowest, are in-place. This is of course, because Out of Place Algorithms create extra arrays to store the data in, while In-place uses the same array.

It goes without saying, that the best Space Complexity is O(1).

Sorting AlgorithmSpace Complexity
Bubble SortO(1)
Insertion SortO(1)
Selection SortO(1)
Quick SortO(log(n))
Merge SortO(n)
Heap SortO(1)
Counting SortO(k)
Radix SortO(n + k)
Bucket SortO(n)

Stable and Unstable Algorithms

This is a rather niche use, and only makes an actual difference in certain types of data. However, it remains an important requirement that is needed for these certain scenarios.

Sorting AlgorithmStable Sort?
Bubble SortYes
Insertion SortYes
Selection SortNo
Quick SortNo
Merge SortYes
Heap SortNo
Counting SortYes
Radix SortYes
Bucket SortYes

Is it important to note however, that you can usually create stable versions of the above Algorithms. The ones being referred to in the image above, are the “Classic” versions of the Algorithm.


You can also check out our YouTube series on Sorting Algorithms!


Sorting Algorithms – Fields Tests

Finally, we are going to be measuring the main component, performance. We have tested the 9 Algorithms featured here under a variety of circumstances. From 100 numbers to 10,000 as well as tests using already sorted data, these tests will reveal quite a bit.

Testing Method

I’ve used Google Collab for running these tests, to ensure a constant and fair testing environment. To be clear, the code for these Sorting Algorithms was written in Python and written in a fairly standard manner. No heavy optimizations were applied, and only standard (classic) versions of the Algorithms were used.

The Python timeit and random library were used to generate the random data, and perform continuous and repeated tests for each Algorithm. This is again, to ensure fair results. The Random library is used to generate numbers from 1 to 10,000 and the timeit library performs each test 5 times in total, and returns a list of all 5 times. We’ve displayed both the max and min values for the 5 tests, so you can see the displacement of times.

Each one of the 5 tests is actually running the code 10 times, (the number parameter, default value 1,000,000). This increases the accuracy by doing alot of tests and adding together the values to average it out. If you want the individual time for one single sort, divide the min/max value by 10. The number of repetitions is controlled by the repeat parameter (default value 5).

You can see the code for the testing function in the code below. If you follow the links for the timeit and random library, you can learn more about what’s going on here.

import random
import timeit
import sys

def test():
  SETUP_CODE = '''
from __main__ import sort
from random import randint'''
  
  TEST_CODE = '''
array = []
for x in range(1000):
  array.append(x)
sort(array)
  '''
  times = timeit.repeat( setup = SETUP_CODE,
                          stmt = TEST_CODE,  
                          number = 10,
                          repeat = 5) 

  print('Min Time: {}'.format(min(times)))
  print('Max Time: {}'.format(max(times)))

Sorting Algorithms – Performance Comparison

In this section, we are going to conduct three sets of tests. The first will have 100 random numbers, the second will have 1000 and the third will have 10,000. Take a good look at the table, compare the time complexities, and make your own observations. I’ll share my observations right after this.

Sorting AlgorithmTest 1 (100)Test 2 (1000)Test 3 (10000)
Bubble SortMin: 0.01008 seconds
Max: 0.0206 seconds
Min: 1.0242 seconds
Max: 1.0558 seconds
Min: 100.922 seconds
Max: 102.475 seconds
Insertion SortMin: 0.00306 seconds
Max: 0.00650 seconds
Min: 0.0369 seconds
Max: 0.0562 seconds
Min: 100.422 seconds
Max: 102.344 seconds
Selection SortMin: 0.00556 seconds
Max: 0.00946 seconds
Min: 0.4740 seconds
Max: 0.4842 seconds
Min: 40.831 seconds
Max: 41.218 seconds
Quick SortMin: 0.00482 seconds
Max: 0.01141 seconds
Min: 0.0370 seconds
Max: 0.0383 seconds
Min: 0.401 seconds
Max: 0.420 seconds
Merge SortMin: 0.00444 seconds
Max: 0.00460 seconds
Min: 0.0561 seconds
Max: 0.0578 seconds
Min: 0.707 seconds
Max: 0.726 seconds
Heap SortMin: 0.00489 seconds
Max: 0.00510 seconds
Min: 0.0704 seconds
Max: 0.0747 seconds
Min: 0.928 seconds
Max: 0.949 seconds
Counting SortMin: 0.01929 seconds
Max: 0.02052 seconds
Min: 0.0354 seconds
Max: 0.0400 seconds
Min: 0.195 seconds
Max: 0.203 seconds
Radix SortMin: 0.00315 seconds
Max: 0.00394 seconds
Min: 0.0294 seconds
Max: 0.0309 seconds
Min: 0.313 seconds
Max: 0.338 seconds
Bucket SortMin: 0.00225 seconds
Max: 0.00241 seconds
Min: 0.0335 seconds
Max: 0.0369 seconds
Min: 1.854 seconds
Max: 1.892 seconds

I wanted to also include tests for 100,000 and 1,000,000 numbers, but the O(n2) Algorithms were taking forever to complete, so I gave up.

Observations

  1. The O(n2) Algorithms (Bubble and Insertion Sort) reacted very poorly as the number of tests went up to 10,000. At 10,000 numbers the other Algorithms were on average, over 100x times faster.
  2. On the test cases with just 100 numbers, the O(n2) Algorithms were faster than the O(n.log(n)) Algorithms.
  3. With every 10x increase in the amount of numbers, the O(n2) Algorithms completion time increased by 100x.
  4. Radix Sort and Counting Sort were on average, the fastest Algorithms.
  5. Heapsort is fastest Algorithm with a space complexity of O(1).

Sorted Data Comparison

Another very interesting case is when Sorted Data is used, instead of random data. This test is mainly to show which Sorting Algorithms perform with sorted/partially sorted data and which perform worse.

Sorting AlgorithmSorted Data (1000)
Bubble SortMin: 0.542 seconds
Max: 0.556 seconds
Insertion SortMin: 0.790 seconds
Max: 0.821 seconds
Selection SortMin: 0.434 seconds
Max: 0.464 seconds
Quick SortMin: 0.812 seconds
Max: 0.872 seconds
Merge SortMin: 0.0289 seconds
Max: 0.0364 seconds
Heap SortMin: 0.0604 seconds
Max: 0.0661 seconds
Counting SortMin: 0.0055 seconds
Max: 0.0124 seconds
Radix SortMin: 0.0119 seconds
Max: 0.0145 seconds
Bucket SortMin: 0.0183 seconds
Max: 0.0247 seconds

Observations

  1. Surprise, Surprise. Quick Sort Algorithm doesn’t live up to it’s name, and is the slowest out of all the above algorithms for a 1000 sorted numbers. This is because Quick Sort doesn’t respond well to degenerate cases like this, and requires special optimizations such as “randomized pivots”.
  2. With the exception of Quick Sort, the time required dropped for all Algorithms.
  3. Counting Sort performs the best, followed by Radix and Bucket Sort.

This marks the end of the Sorting Algorithms Comparison article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

The post Comparison of Sorting Algorithms appeared first on CodersLegacy.

June 17, 2021 03:24 AM UTC

Introduction to Sorting Algorithms

In this Introduction to Sorting Algorithms Article, we’ll be covering all the important and commonly used Sorting Algorithms. We’ll explain each one of them briefly, their worst and best scenarios, Big O notations and then finally compare their performance.

Not all of them directly compete with each other, some being better under certain circumstances or having certain niches uses. Generally speaking though, some sorting algorithms are just better than others. Though they are usually much harder to understand than the simpler ones. Complexity for faster speeds is a fair enough trade however, especially in performance-critical tasks.

This is just an introduction article, that aims to briefly touch on many topics. For the detailed explanations with worked examples, refer to the individual tutorials for each algorithm with the included links.

You can also check out our YouTube series on Sorting Algorithms!


Sorting Algorithm Terminology

Big O Notation: A special kind of notation used to represent time complexity and growth patterns of Algorithms. Common examples of Big O Notation are O(n2) and O(n.logn). The “O” is part of the Big O Notation format.

In-place Sorting: Sorting that takes place without allocating any extra memory, typically within the same array/list. Typically done by swapping values. Examples of such algorithms are Quicksort and Insertion Sort.

Out-of-place Sorting: Sorting Algorithms that need extra memory while sorting. They typically create a new array/list into which the output is sorted.

Recursive: This is when a function calls itself within it’s body. Typically Recursion takes extra memory due to the stacking of recursive calls in the stack memory. However, modern Compilers also have optimizations that automatically that can convert Recursive code, to iterative during compile time, which removes the extra memory issue. Recursive solutions are popular due to their short and effective ways of sorting.

Iterative: Uses Regular loops to do the sorting. Fairly standard way of sorting.

You can learn more about Sorting Algorithms and it’s various types in this article.


Bubble sort

Methodology: The simplest kind of sort, that is often taught to beginners. It involves swapping two values in a list if the preceding value is greater than the next one. Two (nested) for loops are set up, which iterate through the entire list of values, making the above comparison between two values, and swapping them if required. After an n - 1 number of passes, (where n is length of array) the array/list becomes fully sorted.

(Average) Big O notation: n2

Type of Sort: In-place

Learn more about the Bubble Sort Algorithm!


Quick Sort

Methodology: As the name implies, it’s a way of quickly sorting a list of values. Typically done with a recursive solution, Quick sort uses the concept of a pivot around which the values are sorted. The pivot is any arbitrary value picked out from the list of values (usually the first value). We then “figuratively” divide the list into two parts around the pivot.

After a series of complex comparisons and swapping, we recursively call the QuickSort function on each half of the list (with the pivot as the middle value).

Quicksort can be rather unstable (worst case scenario), but this can be mostly avoided using Randomized Quicksort which minimizes the change of encountering the worst case. Randomized Quicksort involves either shuffling or picking a ranom pivot in an attempt to find a value near the median value. The median value of an array is the most ideal pivot for the Quick Sort Algorithm.

Type of Sort: In-place

(Average) Big O notation: n * log(n)

Learn more about the QuickSort Algorithms + Code.


Insertion sort

The Insertion sort algorithm works off the concept of dividing the array into two parts. The first part is sorted, while the second is not. The first step in any Insertion sort algorithm is to treat the first element in the array, as the first part. Basically you can envision a separating line between the first element, and the rest of the values.

We then begin inserting values from the unsorted part, into the sorted part. We do so by picking the first element of the unsorted part, and pushing it back into the end (after the last element in the sorted part. If the new element is less than the last value in the sorted part, we move it back once more.

We keep moving it like this until the value behind it is greater than the new inserted value. We repeat this process till there are no elements left in the unsorted part.

Type of Sort: In-Place

(Average) Big O notation: n2

Learn more about the Bubble Sort Algorithm + Code!


Selection Sort

The Selection Sort Algorithm is one of the simpler Algorithms out there that you can use for sorting purposes. It uses a very simple logic pattern, which involves finding the minimum value of the items being sorted, and then moving it to the start of the of the list. In the next iteration, we narrow our range to exclude the first (now sorted) minimum value and proceed to locate the next minimum. We then place this minimum at the start of our search range, which means it’s after the first minimum value we found. This process continues till all values have been sorted.

Type of Sort: In-Place

(Average) Big O notation: O(n2)

Learn more about the Selection Sort Algorithm + Code!


Merge Sort

Methodology: A rather unique type of sort which takes two lists (if you have one, then just split it around the center) and then simultaneously merges them in a sorted manner. The list is basically continuously halved repeatedly until only one element remains. These individuals elements are then compared to each other, and then placed in a new list, starting from the smallest element.

Despite being more stable than sorts like quicksort (better in worst case scenarios), Merge sort uses more memory as it needs to create a new array/list for the final values. (It doesn’t sort in place)

Type of Sort: Out of Place

(Average) Big O notation = n * log(n)

Learn more about the Merge Sort Algorithm + Code!


Bucket Sort

Bucket Sort has several unique features. For one, it requires the use of another sorting algorithm to completely sort the data. Secondly, it’s also not a comparison based algorithm. Thirdly, it’s very similar to Radix Sort, and the initial steps are actually the same.

The basic premise of Bucket Sort, is to create a certain number of buckets, and then distribute all the values (according to a small formula), into the buckets. Another Sorting Algorithm is then applied to each one of these Buckets, which in reality are just 1D lists. The sorting process is pretty fast as it’s much faster to sort many smaller lists than one large list. (This is because sorting algorithms have time complexities like n2 which increase drastically with n values)

Type of Sort: Out-of-Place

(Average) Big O notation: O(n + k)

Learn more about the BucketSort Algorithm + Code!


Radix Sort

Methodology: One interesting thing about Radix Sort is that unlike most the sorting algorithms, it is not comparison based. Meaning, that there are no direct comparisons carried out amongst the values being sorted. It uses the concept of “Passes” based on the number of digits of the max number, and the creation of Buckets. The number of Buckets creation depend on the type of data being sorted. 10 Buckets for numbers, and 26 for characters.

In Radix sort, we sort digit by digit, rather than numbers. In the first pass, the unit digit (first from the right) is found and the number is placed in the correct bucket (0 – 9). The second pass, the are placed in Buckets based of the ten-digit (second from the right) and so on.

Type of Sort: In-Place

(Average) Big O notation: O(n*k)

Learn more about the Radix Sort Algorithm + Code!


Heap Sort

A Max Heap is a type of data structure where we create a binary-tree like pattern, comprising of nodes which have two children each. The rule is that each node must be larger than it’s children. The node at the top is known as the root node, as it’s the root (parent) of everything in the heap. Before beginning the actual sorting, we swap the values in the list until we get a perfect Max heap.

The Heap Sort algorithm uses the Max Heap concept to sort the array. This is possible as we know the first element in the array, also known as the root node is the largest. By continuously moving the root nodes to the end of the array, and recreating the heap (while ignoring this value),

Type of Sort: In-Place

(Average) Big O notation: n * logn

Learn more about the Heap Sort Algorithm + Code!


Counting Sort Algorithm

Another one of the non-comparison based sorting Algorithms.

It makes several passes over the list of numbers through out the sorting process. The first pass is required to find the max number in the list. We then use this number to generate a list of size of max number + 1. We then count the number of times instances of each number that appears in the array, and store this count value in the list we just created. After completing the counts array, we iterate over the whole thing, cumulatively summing up all the count values inside it.

Finally, by using a little formula we are able to correctly determine the index of each number, within a newly created array of size n, where n is the number of digits in the original array. This new array, is a fully sorted version of the original array of unsorted numbers.

Type of Sort: In-Place

(Average) Big O notation: O(n+k)

Learn more about the Counting Sort Algorithm + Code!


Sorting Comparison (Table)

A table that show’s the time complexities for some of the most commonly used Sorting Algorithms. Time complexity is the first thing that you need to be checking when comparing two sorting algorithms. The lower the time complexity, the better.

Sorting AlgorithmAverage CaseBest CaseWorst Case
Bubble SortO(n2)O(n)O(n2)
Insertion SortO(n2)O(n)O(n2)
Selection SortO(n2)O(n2)O(n2)
Quick SortO(n.log(n))O(n.log(n))O(n2)
Merge SortO(n.log(n))O(n.log(n))O(n.log(n))
Heap SortO(n.log(n))O(n.log(n))O(n.log(n))
Counting SortO(n+k)O(n+k)O(n+k)
Radix SortO(n*k)O(n*k)O(n*k)
Bucket SortO(n+k)O(n+k)O(n2)

There are many other factors on the basis of which Sorting Algorithms are compared, such as Space Complexity, Stable or Unstable, in-place or out-of-place etc. For a more detailed comparison, check out our separate dedicated article on Comparison of Sorting Algorithms, which also includes actual real world tests to compare speed.

You can also use this site to visually see the computing time difference and sorting process between all the types of sorting mentioned in this article. Give it a look as it will help improve your concepts.


This marks the end of the Introduction to Sorting Algorithm article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

The post Introduction to Sorting Algorithms appeared first on CodersLegacy.

June 17, 2021 03:24 AM UTC

Types of Sorting Algorithms (Comparison, Recursive, Inplace)

Most people are aware of the various Sorting Algorithms and even their respective time complexities, best case, worst case etc. However, they have little knowledge about the types of Sorting Algorithms. Knowing about these different types is important because it reveals important information regarding the workings of the Sorting Algorithm, as well as it’s limitations.

There are three main categories that we will be discussing. Recursive and Iterative, Stable and Non-Stable, and in-place and out-of-place.


Recursive and Iterative

Sorting Algorithms are either of recursive nature, or iterative.

Recursive: This is when a function calls itself within it’s body. Typically Recursion takes extra memory due to the stacking of recursive calls in the stack memory. However, (most) modern Compilers also have optimizations that automatically that can convert Recursive code, to iterative during compile time, which removes the extra memory issue. Recursive solutions are often popular due to their short and effective ways of sorting.

Iterative: Uses Regular loops to do the sorting. A fairly standard way of sorting.

Note: This is a pretty debated topic, with results varying significantly in various tests performed.


In-Place and Out-of-Place

In-place Sorting: Sorting that takes place without allocating any extra memory, typically within the same array/list. Typically done by swapping values around in the same array.

Out-of-place Sorting: Sorting Algorithms that need extra memory while sorting. They typically create a new array/list into which the output is sorted.

In general, In-place sorting algorithms are the preferred type due to the lower memory requirements. Sorting Algorithms with both Recursion and Out-of-place types, have the highest memory requirements.

Examples of Out-of-place Sorting Algorithms is Merge Sort, which creates a new “result” array into which the individual elements are sorted. Example of a In-place sorting Algorithm is Bubble Sort, which merely swaps two values after checking to see if they are in sorted order or not.


Stable and non-Stable Algorithms

Another important thing we keep in mind is whether a Sorting Algorithm in stable or not. This is not to be confused with the more common usage of stable, which implies consistency. Rather, it takes on a slightly different meaning when applied to sorting Algorithms.

Let us consider the following list of numbers. We’re going to give them index numbers so we can differentiate between the duplicate elements present there.

3  (0)
8  (1)
4  (2)
4  (3)
1  (4)

Now, when we sort these numbers, we may end up with the following result.

1  (4)
3  (0)
4  (3)
4  (2)
8  (1)

The key point here, is that the duplicate “4's” index positions have now been changed.

On the other hand, if the Algorithm being used was stable, we would end up with the following result.

1  (4)
3  (0)
4  (2)
4  (3)
8  (1)

This may not be important for most use cases, but if a situation arises where the order of items while being sorted is important, you will have to use a stable sorting algorithm. This is typically important where we have data in a “key-pair” format, like the one above.

The “Classic” version of most Algorithms are unstable, but can be modified into stable variants.


If you want to learn more about the various Sorting Algorithms out there, you can check out our Introduction to Sorting Algorithms guide. Likewise, if you are interested in the comparisons between all the sorting algorithms, you can check out our Comparison of Sorting Algorithms guide.

You can also check out our YouTube series on Sorting Algorithms!


This marks the end of the Types of Sorting Algorithms tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

The post Types of Sorting Algorithms (Comparison, Recursive, Inplace) appeared first on CodersLegacy.

June 17, 2021 03:09 AM UTC


Moshe Zadka

Post that PR

Sometimes you will be working on hairy and complicated feature in a shared repository. Maybe it's for work. Maybe it's an open source project.

As a responsible person, you are working on a branch. The usual way of working involves a lot of "intermediate" check-ins. Those serve, if nothing else, as a form of backup.

If you really enjoy backing up your code, you are probably already pushing to a remote branch on your source control server.

This is a great workflow. Responsible, safe, and resistant to local failures.

What could be better?

Well, it is often the case that people hesitate to open the pull request (or merge request, as known in some systems) before they are "ready". Time to post the PR, already!

If the PR is not ready, you can mark it as not ready for merging. Most modern systems allow an explicit flag to make pull requests as "draft". If nothing else, you can use a dedicated label like "do not merge".

There are a few benefits. One is that when your Continuous Integration system runs, this gives an obvious place to keep the results. This avoids the need to dig in the guts of the CI system to find the latest tests.

Speaking of digging through guts, most of these systems allow an easy search of "all my open PRs". This means that to find out the branches you have been working on, for example when getting back to the office from the weekend, you can just open the handy-dandy link and immediately see the list. (This list is also useful as "what do I need to clean up because it has become irrelevant.")

For some teams, this requires a culture adjustment. People need to allow for the code's state in their review, if they review at all. Ideally, this encourages the team to have a serious conversation on when code is reviewed, by whom, and according to what criteria.

After this conversation happens, and assuming people open PRs early in the process, magic starts happening. Because now, when needing localized feedback (for example, "am I using this function correctly") you can link to the right place in the PR and ask for specific feedback.

This feedback, given constructively and helpfully, allows the entire team to learn. Working in public, at least among your team, is helpful to everyone.

So don't delay, post the PR, and start working better with your colleagues.

June 17, 2021 01:00 AM UTC

June 16, 2021


AI Pool

Migrate code from tensorflow 1 to tensorflow 2

I've got a couple of projects in TensorFlow 1 (1.4, 1.9, 1.13, etc.). As Tensorflow 2.0 is released now, how can I migrate my projects to TensorFlow 2.0?...

June 16, 2021 05:48 PM UTC

Pytorch vs Tensorflow

I'm using Keras with the TensorFlow backend, but I'm thinking about switching to TensorFlow or PyTorch. Could you suggest to me what to take? What pros and cons they have?...

June 16, 2021 05:36 PM UTC

Cyclegan in tensorflow

I'm looking for an implementation of CycleGan in Tensorflow . I've used one, but it's not as good as I wanted. Could you post the links to repositories of the implementations?...

June 16, 2021 05:33 PM UTC

Tensorflow Lite Speed Report

I've got a model in TensorFlow lite , it's not as fast as I wanted to be. I would like to know what the problem is, which layer makes it to be so slow. Is there a way, that I can test the speed for each layer?...

June 16, 2021 04:06 PM UTC


Stack Abuse

Redis Queues & Redis Queue Dashboards for Python API Development

Introduction

The in-memory data store - Redis is extensively used by developers as a database, cache layer, to manage job queues, and more.

It comes in handy when you are building APIs with a job queue mechanism to handle tasks like running memory-intensive jobs in the background, counting page visits, or sending bulk email campaigns.

If you are building an API in Python, the Redis Queue (RQ) module gives you functions to queue, schedule, and process these jobs by deploying workers.

In this article, you will be exploring the RQ module to set up job queues and workers, as well as the RQ-Dashboard module to visualize them.

Setting Up Redis

If you don't have an API already, or any code you can apply Redis RQ to - you can clone our GitHub repository with sample code.

Let's clone our repo and install its dependencies:

$ git clone git@github.com:StackAbuse/redis-queues-redis-queue-dashboards.git
$ cd redis-queues-redis-queue-dashboards
$ python -m venv env
$ . env/bin/activate
$ pip install -r requirements.txt

We also have to install Redis, which can actually be a bit tricky if you're not using a Linux-based OS. The easiest way to install it on non-Linux operating systems is via Docker Compose:

$ docker-compose up -d

Our docker-compose file is configured to download a Redis image, and that command will run it in the background. Alternatively, you can install Redis locally.

For Ubuntu, that installation looks like this:

$ sudo apt-get install redis
$ sudo service redis-server start
Starting redis-server: redis-server.
$ redis-cli -v       
redis-cli 4.0.9

Now that our environment is created, let's take a look at Redis Queues.

Redis Queue (RQ)

The Redis Queue (RQ) is a queuing module that runs on top of Redis. It acts as the producer to submit the jobs to the queue. The module also comes with workers which act as consumers to process the submitted jobs from the queue asynchronously. But, what is a job anyway?

Jobs are references to Python functions that are pushed to the queue.

Multiple queues to process jobs can exist, and these queues can be named in any way you'd like. The jobs submitted to the queues can be monitored using their job IDs.

Let's write a simple script to enqueue a job in a Redis Queue, in say, test.py:

# Imported to assign redis as the backend to rq
from redis import Redis
# Imported to initialize the queue object
from rq import Queue
# Functions from the __main__ module can't be processed by workers
# Hence, we have a separate Python file containing the function
from test_job import i_am_a_job

# Create the queue object by passing in the redis object
q = Queue(connection=Redis())
 
# Run the job asynchronously
job = q.enqueue(i_am_a_job, 1)
# Return the function output

The function which you want to enqueue has to be imported from a separate Python file. We've imported it from test_job.py:

# A Function (or) a job
def i_am_a_job(arg1):
    # Perform some function
    return arg1

Now that the Redis Queue and the function is set, let's execute the Python script:

$ python test.py

Running this command should create a job but return no output. If you get an error, review the setup steps again before continuing.

If everything worked, let's use RQ-Dashboard to manage our job.

Managing Redis Queue Jobs with RQ-Dashboard

You can inspect the state of your jobs in a Redis Queue by using RQ-Dashboard, a lightweight Flask app used to monitor Redis Queues. Let's run RQ-Dashboard to monitor the job we've just created.

In a separate Terminal, navigate to the folder where you cloned the repo. There, we'll spin up RQ-Dashboard:

$ . env/bin/activate
$ rq-dashboard
RQ Dashboard version 0.5.2
 * Serving Flask app 'rq_dashboard.cli' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://192.168.1.10:9181/ (Press CTRL+C to quit)
192.168.1.10 - - [11/Jun/2021 15:30:12] "GET / HTTP/1.1" 200 -

You can access RQ-Dashboard at http://localhost:9181. When you open the link, you'll notice that your job is still in the queue and there aren't any workers assigned yet:

Screenshot of RQ-Dashboard running for the first time, it has not workers and one job

Redis Queue Workers

Workers pick a job from the queue to execute them. In another Terminal (you can use the first as well), let's create a worker:

$ . env/bin/activate # For new Terminals when you don't install the dependencies locally
$ rq worker --with-scheduler
15:42:38 Worker rq:worker:a33eb6277eda4969921cc8e3f1e857c0: started, version 1.8.1
15:42:38 Subscribing to channel rq:pubsub:a33eb6277eda4969921cc8e3f1e857c0
15:42:38 *** Listening on default...
15:42:38 Trying to acquire locks for default
15:42:38 Cleaning registries for queue: default
15:42:38 Scheduler for default started with PID 1093
15:42:38 default: test_job.i_am_a_job(1) (b92bf928-48dd-4fb9-a551-427866c46a38)
15:42:38 default: Job OK (b92bf928-48dd-4fb9-a551-427866c46a38)
15:42:38 Result is kept for 500 seconds

The job that you submitted has been executed and the result is kept in Redis for 500 seconds. In addition to the immediate job executions, jobs can also be scheduled to be run at a future time, similar to a CRON job. The enqueue statement can be written as a scheduled one by:

job = queue.enqueue_at(datetime(2021, 7, 7, 13, 15), i_am_a_job, 1)

These operations are the basis of using Redis Queues, monitoring them and assigning workers. Now, let's write up a small practical application, that counts the number of page visits.

Redis Queue Demo Application - Counting Site Visits

The code in the repository that you downloaded earlier includes a Flask application:

from flask import Flask
from redis import Redis
from rq import Queue
from counter import visit

app = Flask(__name__)
q = Queue(connection=Redis())


@app.route('/visit')
def count_visit():
    count = q.enqueue(visit)
    return "Visit has been registered"


@app.route('/')
def return_visit_count():
    count = Redis().get('count').decode('utf-8') if Redis().get('count') else '0'
    return (f'<h1> Congrats! Your are the visitor no.: {count} </h1>')

In your Terminal, let's run this Flask app:

$ . env/bin/activate # Unless it's already running
$ flask run

This launches the Flask app in app.py. This app contains two routes: / and /visit.

Every time the http://localhost:5000/visit endpoint is hit, the count key in Redis is incremented by 1 and the following webpage is returned.

Screenshot of initial website visitor acknowledgement page

The incrementing function is queued as a job. The number of visits is visible in the endpoint: http://localhost:5000 as:

Screenshot of website displaying the visitor count

Let's try visiting the endpoint http://localhost:5000/visit three times. This will submit our job thrice. Let's then check the status of our jobs on RQ-Dashboard. Visit http://localhost:9181 and you can observe the following webpage where our jobs are successfully submitted but no workers are processing them:

Screenshot of RQ-Dashboard showing new jobs being run

To start the Redis Queue worker and scheduler, open another Terminal, and key in the command to start a worker. Observe that the submitted jobs are getting executed one after the other:

$ . env/bin/activate # For new Terminals when you don't install the dependencies locally
$ rq worker --with-scheduler
23:40:06 Worker rq:worker:f5a178b0931b42859699ce57696ed402: started, version 1.8.1
23:40:06 Subscribing to channel rq:pubsub:f5a178b0931b42859699ce57696ed402
23:40:06 *** Listening on default...
23:40:06 Trying to acquire locks for default
23:40:06 Cleaning registries for queue: default
23:40:06 Scheduler for default started with PID 2889
23:40:06 default: counter.visit() (d23c4df8-d638-476b-b70a-dbb4b6f091f2)
23:40:06 default: Job OK (d23c4df8-d638-476b-b70a-dbb4b6f091f2)
23:40:06 Result is kept for 500 seconds
23:40:06 default: counter.visit() (f4ca10c4-16f2-4578-b1b7-67dfce3cee5a)
23:40:06 default: Job OK (f4ca10c4-16f2-4578-b1b7-67dfce3cee5a)
23:40:06 Result is kept for 500 seconds
23:40:06 default: counter.visit() (956b7b39-0b82-4ac6-b29e-fe3f0706431e)
23:40:06 default: Job OK (956b7b39-0b82-4ac6-b29e-fe3f0706431e)
23:40:06 Result is kept for 500 seconds

You can check the dashboard again and you may find that the jobs have been executed. This can be checked by pointing to the URL on your browser to http://localhost:9181. Notice that the worker is now up and running and the jobs have been processed successfully.

Screenshot of RQ-Dashboard showing new working that's registered

Let's check the number of visits by opening or refreshing the app on http://localhost:5000. Voila! The page visit counter has been incremented by 3.

Screenshot of visitor count website, the count was incremented by 3 since the last screenshot

Think of a website with high traffic and one wishes to monitor the site visits and page visits. In this case, multiple instances of this API are served under a load balancer and the count is done based on the jobs submitted in the queue asynchronously.

Conclusion

In this article, we have explored the importance of job queues and how RQ and RQ-Dashboards can serve as a minimalistic job queue stack to your web apps. The practical example can be expanded to other real-world applications where the possibilities are endless.

June 16, 2021 03:30 PM UTC


Python for Beginners

ValueError: Invalid Literal For int() With Base 10

Python ValueError: invalid literal for int() with base 10 is an exception which can occur when we attempt to convert a string literal to integer using int() method and the string literal contains characters other than digits. In this article, we will try to understand the reasons behind this exception and will look at different methods to avoid it in our programs.

What is “ValueError: invalid literal for int() with base 10” in Python?

A ValueError is an exception in python which occurs when an argument with the correct type but improper value is passed to a method or function.The first part of the message i.e. “ValueError” tells us that an exception has occurred because an improper value is passed as argument to the int() function. The second part of the message “invalid literal for int() with base 10”  tells us that we have tried to convert an input to integer but the input has characters other than digits in the decimal number system.

Working of int() function

The int() function in python takes a string or a number as first argument and an optional argument base which denotes the number format. The base has a default value 10 which is used for decimal numbers but we can pass a different value  for base such as 2 for binary number or 16 for hexadecimal number. In this article, we will use the int() function with only the first argument and the default value for base will always be zero.  This can be seen in the following examples.

We can convert a floating point number to integer as given in the following example.When we convert a floating point number into integer using int() function, the digits after the decimal are dropped from the number in the output. 


print("Input Floating point number is")
myInput= 11.1
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input Floating point number is
11.1
Output Integer is:
11

We can convert a string consisting of digits to an integer as given in the following example. Here the input consists of only the digits and hence it will be directly converted into an integer. 

print("Input String is:")
myInput= "123"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
123
Output Integer is:
123

The two input types shown in the above two examples are the only input types for which int() function works properly. With other types of inputs, ValueError will be generated with the message ”invalid literal for int() with base 10” when they are passed as arguments to the int() function. Now , we will look at various types of inputs for which ValueError can be generated in the int() function.

When does “ValueError: invalid literal for int() with base 10” occur?

As discussed above, “ValueError: invalid literal for int()” with base 10 can occur when input with an inappropriate value is passed to the int() function. This can happen in the following conditions.

1.Python ValueError: invalid literal for int() with base 10 occurs when input to int() method is alphanumeric instead of numeric and hence the input cannot be converted into an integer.This can be understood with the following example.

In this example, we pass a string containing alphanumeric characters to the int() function due to which ValueError occurs showing a message “ ValueError: invalid literal for int() with base 10”  in the output.

print("Input String is:")
myInput= "123a"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:


Input String is:
123a
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-9-36c8868f7082>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int() with base 10: '123a'

2. Python ValueError: invalid literal for int() with base 10 occurs when the input to int() function contains space characters and hence the input cannot be converted into an integer. This can be understood with the following example.

In this example, we pass a string containing space to the int() function due to which ValueError occurs showing a message “ ValueError: invalid literal for int() with base 10”  in the output.

print("Input String is:")
myInput= "12 3"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
12 3
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-10-d60c59d37000>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int() with base 10: '12 3'

3. Python ValueError: invalid literal for int() with base 10 occurs when the input to int() function contains any punctuation marks like period “.” or comma “,”.  Hence the input cannot be converted into an integer.This can be understood with the following example.

In this example, we pass a string containing period character “.” to the int() function due to which ValueError occurs showing a message “ ValueError: invalid literal for int() with base 10”  in the output.

print("Input String is:")
myInput= "12.3"
print(myInput)
print("Output Integer is:")
myInt=int(myInput)
print(myInt)

Output:

Input String is:
12.3
Output Integer is:
Traceback (most recent call last):

  File "<ipython-input-11-9146055d9086>", line 5, in <module>
    myInt=int(myInput)

ValueError: invalid literal for int() with base 10: '12.3'

How to avoid “ValueError: invalid literal for int() with base 10”?

We can avoid the ValueError: invalid literal for int() with base 10 exception using preemptive measures to check if the input being passed to the int() function consists of only digits or not. We can use several ways to check if the input being passed to int() consists of only digits or not as follows.

1.We can use regular expressions to check if the input being passed to the int() function consists of only digits or not. If the input contains characters other than digits, we can prompt the user that the input cannot be converted to integer. Otherwise, we can proceed normally.

In the python code given below, we have defined a regular expression “[^\d]” which matches every character except digits in the decimal system. The re.search() method searches for the pattern and if the pattern is found, it returns a match object. Otherwise re.search() method returns None. 

Whenever, re.search() returns None, it can be accomplished that the input has no characters other than digits and hence the input can be converted into an integer as follows.

import re
print("Input String is:")
myInput= "123"
print(myInput)
matched=re.search("[^\d]",myInput)
if matched==None:
    myInt=int(myInput)
    print("Output Integer is:")
    print(myInt)
else:
    print("Input Cannot be converted into Integer.")

Output:

Input String is:
123
Output Integer is:
123

If the input contains any character other than digits,re.search() would contain a match object and hence the output will show a message that the input cannot be converted into an integer.

import re
print("Input String is:")
myInput= "123a"
print(myInput)
matched=re.search("[^\d]",myInput)
if matched==None:
    myInt=int(myInput)
    print("Output Integer is:")
    print(myInt)
else:
    print("Input Cannot be converted into Integer.")

Output:

Input String is:
123a
Input Cannot be converted into Integer.

2.We can also use isdigit() method to check whether the input consists of only digits or not. The isdigit() method takes a string as input and returns True if the input string passed to it as an argument consists only of digital in the decimal system  . Otherwise, it returns False. After checking if the input string consists of only digits or not, we can convert the input into integers.

In this example, we have used isdigit() method to check whether the given input string consists of only the digits or not. As the input string ”123” consists only of digits, the isdigit() function will return True and the input will be converted into an integer using the int() function as shown in the output.

print("Input String is:")
myInput= "123"
print(myInput)
if myInput.isdigit():
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
else:
    print("Input cannot be converted into integer.")

Output:

Input String is:
123
Output Integer is:
123

If the input string contains any other character apart from digits, the isdigit() function will return False. Hence the input string will not be converted into an integer.

In this example, the given input is “123a” which contains an alphabet due to which isdigit() function will return False and the message will be displayed in the output that the input cannot be converted into integer as shown below.

print("Input String is:")
myInput= "123a"
print(myInput)
if myInput.isdigit():
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
else:
    print("Input cannot be converted into integer.")    

Output:

Input String is:
123a
Input cannot be converted into integer.

3.It may be possible that the input string contains a floating point number and has a period character “.” between the digits. To convert such inputs to integers using the int() function, first we will check if the input string contains a floating point number i.e. it has only one period character between the digits or not using regular expressions. If yes, we will first convert the input into a floating point number which can be passed to int() function and then we will show the output. Otherwise, it will be notified that the input cannot be converted to an integer.

In this example,  “^\d+\.\d$” denotes a pattern which starts with one or more digits, has a period symbol ”.” in the middle and ends with one or more digits which is the pattern for floating point numbers. Hence, if the input string is a floating point number, the re.search() method will not return None and the input will be converted into a floating point number using float() function and then it will be converted to an integer as follows. 

import re
print("Input String is:")
myInput= "1234.5"
print(myInput)
matched=re.search("^\d+\.\d+$",myInput)
if matched!=None:
    myFloat=float(myInput)
    myInt=int(myFloat)
    print("Output Integer is:")
    print(myInt)
else:
    print("Input is not a floating point literal.")

Output:

Input String is:
1234.5
Output Integer is:
1234

If the input is not a floating point literal, the re.search() method will return a None object and  the message will be shown in the output that input is not a floating point literal as follows.

import re
print("Input String is:")
myInput= "1234a"
print(myInput)
matched=re.search("^\d+\.\d$",myInput)
if matched!=None:
    myFloat=float(myInput)
    myInt=int(myFloat)
    print("Output Integer is:")
    print(myInt)
else:
    print("Input is not a floating point literal.")

Output:

Input String is:
1234a
Input is not a floating point literal.

For the two approaches using regular expressions, we can write a single program using groupdict() method after writing named patterns using re.match() object. groupdict() will return a python dictionary of named captured groups in the input and thus can be used to identify the string which can be converted to integer.

4.We can also use exception handling in python using python try except to handle the ValueError whenever the error occurs. In the try block of the code, we will normally execute the code. Whenever ValueError occurs, it will be raised in the try block and will be handled by the except block and a proper message will be shown to the user.

 If the input consists of only the digits and is in correct format, output will be as follows.

print("Input String is:")
myInput= "123"
print(myInput)
try:
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
except ValueError:
    print("Input cannot be converted into integer.")

Output:

Input String is:
123
Output Integer is:
123

If the input contains characters other than digits such as alphabets or punctuation, ValueError will be thrown from the int() function which will be caught by the except block and a message will be shown to the user that the input cannot be converted into integer.

print("Input String is:")
myInput= "123a"
print(myInput)
try:
    print("Output Integer is:")
    myInt=int(myInput)
    print(myInt)
except ValueError:
    print("Input cannot be converted into integer.")

Output:

Input String is:
123a
Output Integer is:
Input cannot be converted into integer.

Conclusion

In this article, we have seen why “ValueError: invalid literal for int() with base 10” occurs in python and have understood the reasons and mechanism behind it. We have also seen that this error can be avoided by first checking if the input to int() function consists of only digits or not using different methods like regular expressions and inbuilt functions. 

The post ValueError: Invalid Literal For int() With Base 10 appeared first on PythonForBeginners.com.

June 16, 2021 02:12 PM UTC