Planet Python

Subscribe to Planet Python feed
Planet Python - http://planetpython.org/
Updated: 1 hour 22 min ago

Montreal Python User Group: Montréal-Python 2024 Elections

Mon, 2024-01-08 00:00

Dear Pythonistas, after many years of commitment, the current Python Montreal board of directors team has decided to pass the torch. We are now opening the doors to new, passionate members of the Python community to continue to make our organization shine.

Election calendar:

  • Application period officially open
  • Announcement of applications: January 22, 2024 on our social networks
  • Opening of the online voting period from January 22 to 31, 2024
  • Physical event to learn more about the candidates and interact with them: January 29, 2024 at MP-103
  • Announcement of the new CA: February 1, 2024

If you share our passion for Python and would like to actively contribute to its development in Montréal, we invite you to fill out this application form.

Categories: FLOSS Project Planets

Python People: Will Vincent - Django, Writing Technical Books

Sun, 2024-01-07 20:21

Will Vincent is a former board member of the Django Software Foundation. He's written 3 books on Django, writes a django newsletter, is a podcast co-host for Django Chat.


★ Support this podcast while learning ★

The Complete pytest Course, is the best way to learn pytest quickly.

  • Python testing with pytest from beginner through advanced.
  • Covers applying pytest to projects, including continuous integration and reporting.
  • Even covers combining features to great effect and effectiveness.
★ Support this podcast on Patreon ★ <p>Will Vincent is a former board member of the Django Software Foundation. He's written 3 books on Django, writes a django newsletter, is a podcast co-host for Django Chat.</p> <br><p><strong>★ Support this podcast while learning ★</strong></p><p><a href="https://courses.pythontest.com/p/complete-pytest-course">The Complete pytest Course</a>, is the best way to learn pytest quickly.</p><ul><li>Python testing with pytest from beginner through advanced.</li><li>Covers applying pytest to projects, including continuous integration and reporting.</li><li>Even covers combining features to great effect and effectiveness.</li></ul> <strong> <a href="https://www.patreon.com/PythonPeople" rel="payment" title="★ Support this podcast on Patreon ★">★ Support this podcast on Patreon ★</a> </strong>
Categories: FLOSS Project Planets

Data School: What are conda, Anaconda, and Miniconda? 🐍

Sun, 2024-01-07 11:57

If you&aposve ever taken one of my data science courses, you&aposve probably noticed that I frequently recommend the Anaconda distribution of Python.

You might be left wondering:

  • What is the Anaconda distribution, and why do people recommend it?
  • How is it related to conda?
  • How is it related to Miniconda?
  • As a Data Scientist, which of these do I need to be familiar with?

I&aposll answer those questions below! &#x1F447;

What is Anaconda?

Anaconda is a Python distribution aimed at Data Scientists that includes 250+ packages (with easy access to 7,500+ additional packages). Its value proposition is that you can download it (for free) and "everything just works." It&aposs available for Mac, Windows, and Linux.

A new Anaconda distribution is released a few times a year. Within each distribution, the versions of the included packages have all been tested to work together.

If you visit the installation page for many data science packages (such as pandas), they recommend Anaconda because it makes installation easy!

What is conda?

conda is an open source package and environment manager that comes with Anaconda.

As a package manager, you can use conda to install, update, and remove packages and their "dependencies" (the packages they depend upon):

  • If Anaconda doesn&apost include a package that you need, you use conda to download and install it.
  • If Anaconda doesn&apost have the version of a package you need, you use conda to update it.

As an environment manager, you can use conda to manage virtual environments:

  • If you&aposre not familiar with virtual environments, they allow you to maintain isolated environments with different packages and versions of those packages.
  • conda is an alternative to virtualenv, pipenv, and other related tools.

conda has a few huge advantages over other tools:

  • It&aposs a single tool to learn, rather than using multiple tools to manage packages, environments, and Python versions.
  • Package installation is predictably easy because you&aposre installing pre-compiled binaries.
  • Unlike pip, you never need to build from source code, which can be especially difficult for some data science packages.
  • You can use conda with languages other than Python.
What is Miniconda?

Miniconda is a Python distribution that only includes Python, conda, their dependencies, and a few other useful packages.

Miniconda is a great choice if you prefer to only install the packages you need, and you&aposre sufficiently familiar with conda. (Here&aposs how to choose between Anaconda and Miniconda.)

Summary:
  • Anaconda and Miniconda are both Python distributions.
  • Anaconda includes hundreds of packages, whereas Miniconda includes just a few.
  • conda is an open source tool that comes with both Anaconda and Miniconda, and it functions as both a package manager and an environment manager.

Personally, I make extensive use of conda for creating environments and installing packages. And since I&aposm comfortable with conda, I much prefer Miniconda over Anaconda.

Do you have questions about conda, Anaconda, or Miniconda? Let me know in the comments section below! &#x1F447;

Categories: FLOSS Project Planets

CodersLegacy: Switching between Multiple Screens in Tkinter (dynamically)

Sat, 2024-01-06 05:22

The following article demonstrates a useful trick in Tkinter to create multiple “screens” (e.g. a login screen, register screen, main page screen) within a single window! Instead of creating a new window for each screen, we will use a single tkinter window, which swaps dynamically between multiple “screens” (represented by frames).

This is more efficient and faster than creating a new window every time.

Complete Code:

The core idea is simple, we have multiple classes, each of which represents a window. These classes inherit from the Frame class, essentially making them frames as well. To “swap” between screens, we destroy the existing frame (including all of its children objects) and then create the new frame to take its place.

A YouTube video explaining this code step-by-step is included at the bottom of this article.

import tkinter as tk def center_window(width, height): x = (root.winfo_screenwidth() // 2) - (width // 2) y = (root.winfo_screenheight() // 2) - (height // 2) root.geometry(f'{width}x{height}+{x}+{y}') class WelcomeWindow(tk.Frame): def __init__(self, master): super().__init__() self.master = master self.master.title("Welcome") center_window(200, 150) login_button = tk.Button(self, text="Login", width=10, command = self.on_login) login_button.pack(padx=20, pady=(20, 10)) register_button = tk.Button(self, text="Register", width=10, command = self.on_register) register_button.pack(pady=10) self.pack() def on_login(self): for widget in self.winfo_children(): widget.destroy() self.destroy() LoginWindow(self.master) def on_register(self): for widget in self.winfo_children(): widget.destroy() self.destroy() RegisterWindow(self.master) class LoginWindow(tk.Frame): def __init__(self, master): super().__init__() self.master = master self.master.title("Login") self.master.resizable(False, False) center_window(250, 150) tk.Label(self, text="Username:").grid(row=0, column=0) self.username_entry = tk.Entry(self) self.username_entry.grid(row=0, column=1, padx=10, pady=10) tk.Label(self, text="Password:").grid(row=1, column=0) self.password_entry = tk.Entry(self, show="*") self.password_entry.grid(row=1, column=1, padx=10, pady=10) submit_button = tk.Button(self, text="Submit", width=8,command = self.on_successful_login) submit_button.grid(row=2, column=1, sticky="e", padx=10, pady=(10, 0)) submit_button = tk.Button(self, text="Back", width=8, command = self.on_back) submit_button.grid(row=2, column=0, sticky="w", padx=10, pady=(10, 0)) self.pack() def on_back(self): for widget in self.winfo_children(): widget.destroy() self.destroy() WelcomeWindow(self.master) def on_successful_login(self): for widget in self.winfo_children(): widget.destroy() self.destroy() MainWindow(self.master) class RegisterWindow(tk.Frame): def __init__(self, master): super().__init__() self.master = master self.master.title("Register") self.master.resizable(False, False) center_window(300, 250) tk.Label(self, text="First Name:").grid(row=0, column=0, sticky="w") self.first_name_entry = tk.Entry(self, width=26) self.first_name_entry.grid(row=0, column=1, padx=10, pady=10, sticky="e") tk.Label(self, text="Last Name:").grid(row=1, column=0, sticky="w") self.last_name_entry = tk.Entry(self, width=26) self.last_name_entry.grid(row=1, column=1, padx=10, pady=10, sticky="e") tk.Label(self, text="Password:").grid(row=2, column=0, sticky="w") self.password_entry = tk.Entry(self, show="*", width=26) self.password_entry.grid(row=2, column=1, padx=10, pady=10, sticky="e") tk.Label(self, text="Email:").grid(row=3, column=0, sticky="w") self.email_entry = tk.Entry(self, width=26) self.email_entry.grid(row=3, column=1, padx=10, pady=10, sticky="e") submit_button = tk.Button(self, text="Submit", width=8) submit_button.grid(row=7, column=1, padx=10, pady=10, sticky="e") submit_button = tk.Button(self, text="Back", width=8, command = self.on_back) submit_button.grid(row=7, column=0, sticky="w", padx=10, pady=(10, 10)) self.pack() def on_back(self): for widget in self.winfo_children(): widget.destroy() self.destroy() WelcomeWindow(self.master) class MainWindow(tk.Frame): def __init__(self, master): super().__init__() self.master = master center_window(500, 500) self.pack() root = tk.Tk() root.eval('tk::PlaceWindow . center') WelcomeWindow(root) root.mainloop()

This marks the end of the Switching between multiple Screens in Tkinter 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 Switching between Multiple Screens in Tkinter (dynamically) appeared first on CodersLegacy.

Categories: FLOSS Project Planets

Matt Layman: Fun With Scrapy Link Validation on CI

Fri, 2024-01-05 19:00
Here’s my scenario: I have a static site generator that is building HTML pages for a community project that I’m working on. How can I make sure, automatically, that all the links to other internal pages within the site continue to work? In this article, I’ll show you how I managed to do that using Scrapy, a web scraping tool, and GitHub Actions, the project’s Continuous Integration system. To solve this problem, I decided to use a web scraper.
Categories: FLOSS Project Planets

TechBeamers Python: Comparing Two Lists in Python

Fri, 2024-01-05 07:21

Comparing two lists is a common task in programming, and it becomes crucial when you need to find differences, and intersections, or validate data consistency. In this tutorial, we will explore various methods to compare two lists in Python. We’ll cover simple equality checks, membership testing, element-wise comparisons, and advanced techniques using built-in functions and […]

The post Comparing Two Lists in Python appeared first on TechBeamers.

Categories: FLOSS Project Planets

Real Python: The Real Python Podcast – Episode #186: Exploring Python in Excel

Fri, 2024-01-05 07:00

Are you interested in using your Python skills within Excel? Would you like to share a data science project or visualization as a single Office file? This week on the show, we speak with Principal Architect John Lam and Sr. Cloud Developer Advocate Sarah Kaiser from Microsoft about Python in Excel.

[ 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

PyCharm: New Low-Impact Monitoring API in Python 3.12

Fri, 2024-01-05 06:12
People love PyCharm’s debugger, and we work hard to make it both fast and easy to use. But what if it could be even faster and easier to use? Python 3.12 adds the new low-impact monitoring API described in PEP 669, enabling debuggers, profilers, and similar tools to run code at almost full speed. As […]
Categories: FLOSS Project Planets

TechBeamers Python: How to Make a Unique List in Python

Fri, 2024-01-05 02:32

In the world of Python, handling data is a big deal. We often work with lists, and sometimes, we need to make sure they only have unique items. In this detailed guide, we’ll explore different ways to make a unique list in Python. From using sets to more advanced stuff like functools.reduce(), we’re going to […]

The post How to Make a Unique List in Python appeared first on TechBeamers.

Categories: FLOSS Project Planets

TechBeamers Python: 25 Sampling Interview Questions and Answers to Remember

Fri, 2024-01-05 02:07

Find out 25 commonly asked sampling interview questions distributed across different categories. These questions cover a range of topics related to sampling and can be used to assess the interviewee’s understanding of fundamental concepts, methods, and applications in various fields. 25 Sampling Interview Questions and Answers We have prepared this quick guide of sampling interview […]

The post 25 Sampling Interview Questions and Answers to Remember appeared first on TechBeamers.

Categories: FLOSS Project Planets

TechBeamers Python: Python Syntax – Quick Start Guide

Fri, 2024-01-05 00:16

Python, the language known for its simplicity and friendliness, has a way of writing code called syntax. It’s like the grammar of Python, telling you how to structure your code so the computer can understand and execute it. In this guide, we’re going to break down different parts of Python syntax, with clear explanations and […]

The post Python Syntax – Quick Start Guide appeared first on TechBeamers.

Categories: FLOSS Project Planets

TechBeamers Python: How to Achieve Python String indexOf Using Find() and Index()

Thu, 2024-01-04 12:10

The indexOf method helps locate a substring in a string, pointing to its first appearance. In Python, there isn’t a direct string indexOf method. But we can achieve the same functionality using various string indexing and search methods. In this tutorial, we’ll delve into these methods, providing multiple examples and additional information to help readers […]

The post How to Achieve Python String indexOf Using Find() and Index() appeared first on TechBeamers.

Categories: FLOSS Project Planets

Django Weblog: Unlock Early Savings: Early Bird Tickets for DjangoCon Europe 2024 Now Available!

Thu, 2024-01-04 11:52

You can take advantage of our Early Bird ticket sale for DjangoCon Europe 2024. By purchasing your tickets early, you not only guarantee your attendance at one of the most exciting Django events but also enjoy significant savings.

Buy tickets on the conference website

Why Go Early?

You can secure your tickets at a special Early Bird rate, providing exceptional value for your conference experience.

Also, your early commitment goes a long way in supporting the success of DjangoCon Europe 2024. It helps us plan better and ensure a seamless event.

Act now and secure your Early Bird tickets before the sale closes on April 31st. Don't miss out on the chance to save and be a part of this exciting event.

We can't wait to welcome you to DjangoCon Europe 2024!

Categories: FLOSS Project Planets

TechBeamers Python: A Beginner’s Guide to Python Random Sampling

Thu, 2024-01-04 09:07

Random sampling might sound complicated, but it’s a super useful skill when working with data in Python. It helps you pick out bits of information without going through everything. Python’s got this cool toolbox called the random module that makes random sampling a breeze. In this tutorial, we’re going to explore different ways to use […]

The post A Beginner’s Guide to Python Random Sampling appeared first on TechBeamers.

Categories: FLOSS Project Planets

Mark Dufour: Fast DOOM WAD renderer in 999 lines of Python

Wed, 2024-01-03 20:02

For the longest time, I've wanted to re-implement the original DOOM engine in Python, and compile it with Shedskin to get reasonable performance. So when I finally ran across a pretty small engine written in Java, by Leonardo Ono, I decided to convert his version to Python.

I left out some optimizations (most notably "visplanes"), for readability and in order to keep everything under 1000 lines of code (while following PEP8). So it could run quite a bit faster still. This is including a WAD loader and interactive Pygame wrapper, that makes it almost feel like you are playing DOOM.

It's interesting how using Python (using just CPython) seems to be about as fast as using assembler back in the day.

This video shows the day-and-night difference in FPS after compilation using Shedskin.

The source code can be found in the Shedskin examples directory.

I may revisit the code now and then, to try and improve readability by adding comments and/or refactoring parts. Patches in this direction very much welcome of course.

In general, we are also always looking to receive more feedback and contributions from the community! While not the 'final' solution to Python performance, Shed Skin is great fun to hack on. See some potential issues for the next release to start looking into.

Categories: FLOSS Project Planets

Matt Layman: Legal and Stripe - Building SaaS with Python and Django #179

Wed, 2024-01-03 19:00
In this episode, we took care of the legal obligations of the site by setting up Terms of Service and a Privacy Policy page. Then we moved on to the next portion of signup, which is to configure Stripe to create customers and prepare, ultimately, to accept subscription payments.
Categories: FLOSS Project Planets

Ben Cook: Saving Utility Companies Years with Computer Vision

Wed, 2024-01-03 13:15

How do utility companies monitor thousands of miles of electrical wire to find small imperfections that threaten the entire system? For the entire history of electrical infrastructure, the only answer has been ‘very slowly.’

Now, Sparrow’s computer vision capabilities, combined with Fast Forward’s thermal imaging system, can accomplish what used to take over a decade in less than a month. Here’s how they do it.

How it Started

Dusty Birge, CEO of Fast Forward began inspecting power lines using a drone and crowd-sourced labor. He knew that automation would be needed to take the next step forward, but he found people in the artificial intelligence space to be overconfident and unreliable. That is until he was introduced to Ben Cook, the founder of Sparrow Computing.

Within a month, Ben provided a computer vision model that could accurately identify utility poles, and that has been continually improving ever since.

The Project

Fast Forward rigged a system of 5 thermal cameras to a car and set them to take photos every 15 feet. Sparrow’s computer vision model then ran through the nearly 2 million photos taken and extracted about 50,000 relevant images, detecting any abnormalities in the utility lines.

“Right out of the gate, we presented data to the utilities and just blew them away,” Dusty said in an interview. “The first test runs proved a scalable model that automatically monitored a company’s entire system in under a month, where traditional methods would take over a decade. These test runs successfully spotted anomalies that would have caused blackouts, but were instead able to be fixed promptly.”

The numbers speak plainly for themselves, not just in time but in cost as well. There was one hotspot that Sparrow identified, but that the utility company did not address in time. The power line failed as a result, costing the company around $800. The inspection itself cost only $4. The potential return of this technology is astronomical, and it is already being put to good use.

Going Forward

Fast Forward is already looking ahead to the next phase of this project, translating the same process to daytime cameras. Having had such success in the initial tests, Dusty is pleased to continue working with Ben and Sparrow Computing.

Ben is excited to find more real world problems that can be solved as cameras become cheaper and more ubiquitous. He wants to create more computer vision models that interact with the physical environment and revolutionize companies throughout the Midwest and the world.

To get a closer look at Sparrow Computing, reach Ben at ben@sparrow.dev.

The post Saving Utility Companies Years with Computer Vision appeared first on Sparrow Computing.

Categories: FLOSS Project Planets

Mirek Długosz: Playwright - accessing page object in event handler

Wed, 2024-01-03 12:37

Playwright exposes a number of browser events and provides a mechanism to respond to them. Since many of these events signal errors and problems, most of the time you want to log them, halt program execution, or ignore and move on. Logging is also shown in Playwright documentation about network, which I will use as a base for examples in this article.

Problem statement

Documentation shows event handlers created with lambda expressions, but lambda poses significant problems once you leave the territory of toy examples:

  • they should fit in single line of code
  • you can’t share them across modules
  • you can’t unit test them in isolation

Usually you want to define event handlers as normal functions. But when you attempt that, you might run into another problem - Playwright invokes event handler with some event-related data, that data does not contain any reference back to page object, and page object might contain some important contextual information.

In other words, we would like to do something similar to code below. Note that this example does not work - if you run it, you will get NameError: name 'page' is not defined.

from playwright.sync_api import sync_playwright from playwright.sync_api import Playwright def request_handler(request): print(f"{page.url} issued request: {request.method} {request.url}") def response_handler(response): print(f"{page.url} received response: {response.status} {response.url}") def run_test(playwright: Playwright): browser = playwright.chromium.launch() page = browser.new_page() page.goto("https://mirekdlugosz.com") page.on("request", request_handler) page.on("response", response_handler) page.goto("https://httpbin.org/status/404") browser.close() with sync_playwright() as playwright: run_test(playwright)

I can think of three ways of solving that: by defining a function inside a function, with functools.partialand with a factory function. Let’s take a look at all of them.

Defining a function inside a function

Most Python users are so used to defining functions at the top level of module or inside a class (we call these “methods”) that they might consider function definitions to be somewhat special. In fact, some other programming languages do encumber where functions can be defined. But in Python you can define them anywhere, including inside other functions.

from playwright.sync_api import sync_playwright from playwright.sync_api import Playwright def run_test(playwright: Playwright): def request_handler(request): print(f"{page.url} issued request: {request.method} {request.url}") def response_handler(response): print(f"{page.url} received response: {response.status} {response.url}") browser = playwright.chromium.launch() page = browser.new_page() page.goto("https://mirekdlugosz.com") page.on("request", request_handler) page.on("response", response_handler) page.goto("https://httpbin.org/status/404") browser.close() with sync_playwright() as playwright: run_test(playwright)

This works because function body is not evaluated until function is called and functions have access to names defined in their encompassing scope. So Python will look up page only when event handler is invoked by Playwright; since it’s not defined in function itself, Python will look for it in the function where event handler was defined (and then next function, if there is one, then module and eventually builtins).

I think this solution solves the most important part of the problem - it allows to write event handlers that span multiple lines. Technically it is also possible to share these handlers across modules, but you won’t see that often. They can’t be unit tested in isolation, as they depend on their parent function.

functools.partial

functools.partial documentation may be confusing, as prose sounds exactly like a description of standard function, code equivalent assumes pretty good understanding of Python internals, and provided example seems completely unnecessary.

I think about partial this way: it creates a function that has some of the arguments already filled in.

To be fair, partial is rarely needed. It allows to write shorter code, as you don’t have to repeat the same arguments over and over again. It may also allow you to provide saner library API - you can define single generic and flexible function with a lot of arguments, and few helper functions intended for external use, each with a small number of arguments.

But it’s invaluable when you have to provide your own function, but you don’t have control over arguments it will receive. Which is exactly the problem we are facing.

from functools import partial from playwright.sync_api import sync_playwright from playwright.sync_api import Playwright def request_handler(request, page=None): print(f"{page.url} issued request: {request.method} {request.url}") def response_handler(response, page=None): print(f"{page.url} received response: {response.status} {response.url}") def run_test(playwright: Playwright): browser = playwright.chromium.launch() page = browser.new_page() page.goto("https://mirekdlugosz.com") local_request_handler = partial(request_handler, page=page) local_response_handler = partial(response_handler, page=page) page.on("request", local_request_handler) page.on("response", local_response_handler) page.goto("https://httpbin.org/status/404") browser.close() with sync_playwright() as playwright: run_test(playwright)

Notice that our function takes the same arguments as Playwright event handler, and then some. When it’s time to assign event handlers, we use partial to create a new function, one that only needs argument that we will receive from Playwright - the other one is already filled in. But when function is executed, it will receive both arguments.

Factory function

Functions in Python may not only define other functions in their bodies, but also return functions. They are called “higher-order functions” and aren’t used often, with one notable exception of decorators.

from playwright.sync_api import sync_playwright from playwright.sync_api import Playwright def request_handler_factory(page): def inner(request): print(f"{page.url} issued request: {request.method} {request.url}") return inner def response_handler_factory(page): def inner(response): print(f"{page.url} received response: {response.status} {response.url}") return inner def run_test(playwright: Playwright): browser = playwright.chromium.launch() page = browser.new_page() page.goto("https://mirekdlugosz.com") page.on("request", request_handler_factory(page)) page.on("response", response_handler_factory(page)) page.goto("https://httpbin.org/status/404") browser.close() with sync_playwright() as playwright: run_test(playwright)

The key here is that inner function has access to all of enclosing scope, including values passed as arguments to outer function. This allows us to pass specific values that are only available in the place where outer function is called.

Summary

The first solution is a little different than other two, because it does not solve all of the problems set forth. On the other hand, I think it’s the easiest to understand - even beginner Python programmers should intuitively grasp what is happening and why.

In my experience higher-order functions takes some getting used to, while partial is not well-known and may be confusing at first. But they do solve our problem completely.

Categories: FLOSS Project Planets

Programiz: Python Dictionary

Wed, 2024-01-03 11:22
In this tutorial, you will learn about Python dictionaries - how they are created, how to access, add, and remove elements from them, and the various built-in methods associated with dictionaries.
Categories: FLOSS Project Planets

Programiz: Python for loop

Wed, 2024-01-03 10:58
In this article, we'll learn how to use a for loop in Python with the help of examples.
Categories: FLOSS Project Planets

Pages