Planet Python

Subscribe to Planet Python feed
Planet Python - http://planetpython.org/
Updated: 4 hours 28 min ago

Test and Code: 158: TDD in Swift - Gio

Fri, 2021-06-18 18:00

Iterative processes that include writing test code and production code together, such as TDD, help make coding fun.

All of us that care about developing quality code with the help of testing can learn from each other, regardless of programming language.

Today we step outside our normal Python comfort zone and talk with Gio about TDD in Swift.

Gio Lodi, author of TDD in Swift, joins the show to discuss Test Driven Development, software workflows, bottom up vs top down, rapid feedback, developer vs customer facing tests, and more.

Special Guest: Gio Lodi.

Sponsored By:

Support Test & Code : Python Testing

Links:

<p>Iterative processes that include writing test code and production code together, such as TDD, help make coding fun. </p> <p>All of us that care about developing quality code with the help of testing can learn from each other, regardless of programming language.</p> <p>Today we step outside our normal Python comfort zone and talk with Gio about TDD in Swift.</p> <p>Gio Lodi, author of TDD in Swift, joins the show to discuss Test Driven Development, software workflows, bottom up vs top down, rapid feedback, developer vs customer facing tests, and more.</p><p>Special Guest: Gio Lodi.</p><p>Sponsored By:</p><ul><li><a href="https://testandcode.com/datadog" rel="nofollow">Datadog</a>: <a href="https://testandcode.com/datadog" rel="nofollow">Modern end-to-end monitoring & security. See inside any stack, any app, at any scale, anywhere. Get started with a free trial at testandcode.com/datadog and Datadog will send you a free t-shirt. </a></li></ul><p><a href="https://www.patreon.com/testpodcast" rel="payment">Support Test & Code : Python Testing</a></p><p>Links:</p><ul><li><a href="https://tddinswift.com/" title="TDD in Swift with SwiftUI and Combine" rel="nofollow">TDD in Swift with SwiftUI and Combine</a></li><li><a href="https://mokacoding.com/" title="mokacoding - Gio's blog" rel="nofollow">mokacoding - Gio's blog</a></li><li><a href="https://developer.apple.com/documentation/xctest" title="XCTest" rel="nofollow">XCTest</a></li><li><a href="https://kentbeck.substack.com/" title="Software Design: Tidy First? - Kent Beck on Substack" rel="nofollow">Software Design: Tidy First? - Kent Beck on Substack</a></li></ul>
Categories: FLOSS Project Planets

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

Fri, 2021-06-18 11:02

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:

  • in a Reader method, which builds the EntryFilterOptions object
  • where they get used, either a helper or a Storage method

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:

  • Dicts are not type-checked. TypedDict helps, but doesn't prevent using the wrong keys at runtime.
  • Dicts break code completion. TypedDict may help with smarter tools like PyCharm, but doesn't in interactive mode or IPython.
  • Dicts are mutable. For our use case, immutability is a plus: the options don't have much reason to change, so it's useful to disallow it.
Why not **kwargs? #

Why not pass **kwargs directly to EntryFilterOptions?

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

Because:

  • It also breaks code completion.
  • It makes the code less self-documenting: you don't know what arguments get_entries() takes, even if you read the source. Presumably, they're in the docstring, but not everybody writes one all the time.
  • If we introduce another options object (say, for pagination), we still have to write code to split the kwargs between the two.
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:

  • Reader methods are mostly called by external users in many ways
  • Storage methods are mostly called by internal users (Reader) in a few ways
  1. Ted Kaminski discusses this distinction in more detail in Data, objects, and how we're railroaded into poor design. [return]

Categories: FLOSS Project Planets

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

Fri, 2021-06-18 08:30
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:

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:

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:

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:

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:

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

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

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.

Categories: FLOSS Project Planets

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

Fri, 2021-06-18 08:00

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 ]

Categories: FLOSS Project Planets

Mike Driscoll: Creating a Warhol Triptych Image with Pillow and Python

Thu, 2021-06-17 16:36

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 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:

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.

Categories: FLOSS Project Planets

Codementor: HACKERRANK HOURGLASS CHALLENGE USING PYTHON

Thu, 2021-06-17 14:28
A Solution To A Hackerrank Challenge
Categories: FLOSS Project Planets

Python Insider: Python 3.10.0b3 is available

Thu, 2021-06-17 13:38

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 

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:

  • PEP 623 – Deprecate and prepare for the removal of the wstr member in PyUnicodeObject.
  • PEP 604 – Allow writing union types as X | Y
  • PEP 612 – Parameter Specification Variables
  • PEP 626 – Precise line numbers for debugging and other tools.
  • PEP 618 – Add Optional Length-Checking To zip.
  • bpo-12782: Parenthesized context managers are now officially allowed.
  • PEP 632 – Deprecate distutils module.
  • PEP 613 – Explicit Type Aliases
  • PEP 634 – Structural Pattern Matching: Specification
  • PEP 635 – Structural Pattern Matching: Motivation and Rationale
  • PEP 636 – Structural Pattern Matching: Tutorial
  • PEP 644 – Require OpenSSL 1.1.1 or newer
  • PEP 624 – Remove Py_UNICODE encoder APIs
  • PEP 597 – Add optional EncodingWarning
  • (Hey, fellow core developer, if a feature you find important is missing from this list, let Pablo know.)

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

bpo-38605: from __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 resourcesAnd 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

Categories: FLOSS Project Planets

Python Engineering at Microsoft: Python in Visual Studio Code – June 2021 Release

Thu, 2021-06-17 11:35

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.

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…).

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.

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, {}.

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.

Categories: FLOSS Project Planets

Python for Beginners: String to Integer in Python

Thu, 2021-06-17 10:59

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.

Categories: FLOSS Project Planets

PyCharm: Release Candidate for PyCharm 2021.1.3 Is Out

Thu, 2021-06-17 10:54

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:

  • User interface: Ctrl+Shift+Arrow shortcut selects text in a dialog window rather than stretching the window. [IDEA-267294]
  • Quick documentation: .jpeg and .svg images are rendered correctly. [IDEA-256001]
  • Plugins: disabled plugins that have been updated to the latest version don’t cause a false notification saying a plugin update is required. [IDEA-267878]
  • VCS: the “Compare branch” popup is displayed correctly. [IDEA-251381]
  • Projector: PyCharm doesn’t crash on restart when Projector is used. [IDEA-269591]
  • Python Console: “Run Cell and Select Below” shortcut (Ctrl+Enter) in SciView now moves the caret to the next cell after execution. [PY-48157]
  • Django: Code Completion for Django Models now works correctly. [PY-48252]
  • Debugger: when the “Attach to Node.js/Chrome” Run/Debug configuration is used, the debugger reconnects automatically upon node restart. [WEB-50455]
  • Databases: code completion for table synonyms works correctly for databases in MS SQL. [DBE-4531]

DOWNLOAD PYCHARM 2021.1.3 RC

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

Categories: FLOSS Project Planets

EuroPython: EuroPython 2021: Schedule published

Thu, 2021-06-17 10:47

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

EuroPython 2021 ScheduleSeven full days of Python

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

  • Two workshop/training days (Monday, Tuesday):
    training sessions and workshops
  • Three conference days (Wednesday, Thursday, Friday):
    keynotes, talks, lightning talks, poster session
  • Two sprint days (Saturday, Sunday):
    code sprints / hackathons
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:

  • 6 exciting keynotes,
  • 97 talks,
  • 18 training sessions,
  • 3 fun lightning talk blocks,
  • overall, more than 130 sessions in total,
  • presented by more than 130 speakers from around the world,
  • 4 all-day conference tracks,
  • with a whole track dedicated to data science topics,
  • a poster track,
  • interactive sessions,
  • a virtual social event,
  • an after party,
  • and lots of socializing on our conference platform.

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

Categories: FLOSS Project Planets

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

Thu, 2021-06-17 09:08
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.
Categories: FLOSS Project Planets

Python Anywhere: June system update brings easier task management

Thu, 2021-06-17 06:00

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.

Categories: FLOSS Project Planets

Full Stack Python: Setting Up Python 3, Django &amp; Gunicorn on Linux Mint 17.3

Thu, 2021-06-17 00:00

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.

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

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

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.

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.

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.

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

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.

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.

Categories: FLOSS Project Planets

Brett Cannon: How I rate films

Wed, 2021-06-16 23:31

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).

Categories: FLOSS Project Planets

CodersLegacy: Comparison of Sorting Algorithms

Wed, 2021-06-16 23:24

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.

  • Time Complexity
  • Space Complexity
  • Stable/Unstable
  • Actual Fields Tests

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 CaseBubble 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 ComplexityBubble 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 SortYesInsertion SortYesSelection SortNoQuick SortNoMerge SortYesHeap SortNoCounting SortYesRadix SortYesBucket 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 secondsMin: 1.0242 seconds
Max: 1.0558 secondsMin: 100.922 seconds
Max: 102.475 secondsInsertion SortMin: 0.00306 seconds
Max: 0.00650 secondsMin: 0.0369 seconds
Max: 0.0562 secondsMin: 100.422 seconds
Max: 102.344 secondsSelection SortMin: 0.00556 seconds
Max: 0.00946 secondsMin: 0.4740 seconds
Max: 0.4842 secondsMin: 40.831 seconds
Max: 41.218 secondsQuick SortMin: 0.00482 seconds
Max: 0.01141 secondsMin: 0.0370 seconds
Max: 0.0383 secondsMin: 0.401 seconds
Max: 0.420 secondsMerge SortMin: 0.00444 seconds
Max: 0.00460 secondsMin: 0.0561 seconds
Max: 0.0578 secondsMin: 0.707 seconds
Max: 0.726 secondsHeap SortMin: 0.00489 seconds
Max: 0.00510 secondsMin: 0.0704 seconds
Max: 0.0747 secondsMin: 0.928 seconds
Max: 0.949 secondsCounting SortMin: 0.01929 seconds
Max: 0.02052 secondsMin: 0.0354 seconds
Max: 0.0400 secondsMin: 0.195 seconds
Max: 0.203 secondsRadix SortMin: 0.00315 seconds
Max: 0.00394 secondsMin: 0.0294 seconds
Max: 0.0309 secondsMin: 0.313 seconds
Max: 0.338 secondsBucket SortMin: 0.00225 seconds
Max: 0.00241 secondsMin: 0.0335 seconds
Max: 0.0369 secondsMin: 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 secondsInsertion SortMin: 0.790 seconds
Max: 0.821 secondsSelection SortMin: 0.434 seconds
Max: 0.464 secondsQuick SortMin: 0.812 seconds
Max: 0.872 secondsMerge SortMin: 0.0289 seconds
Max: 0.0364 secondsHeap SortMin: 0.0604 seconds
Max: 0.0661 secondsCounting SortMin: 0.0055 seconds
Max: 0.0124 secondsRadix SortMin: 0.0119 seconds
Max: 0.0145 secondsBucket 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.

Categories: FLOSS Project Planets

CodersLegacy: Introduction to Sorting Algorithms

Wed, 2021-06-16 23:24

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 CaseBubble 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.

Categories: FLOSS Project Planets

CodersLegacy: Types of Sorting Algorithms (Comparison, Recursive, Inplace)

Wed, 2021-06-16 23:09

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.

Categories: FLOSS Project Planets

TestDriven.io: Basic and Full-text Search with Django and Postgres

Wed, 2021-06-16 21:40
This article looks at how to add basic and full-text search to a Django app with Postgres.
Categories: FLOSS Project Planets

Moshe Zadka: Post that PR

Wed, 2021-06-16 21:00

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.

Categories: FLOSS Project Planets

Pages