Feeds

Matt Glaman: Simplifying the frontend developer experience in Drupal with a click of the button

Planet Drupal - Tue, 2023-05-16 09:18

Last year at DrupalCon Portland 2022, Dries announced that "Drupal is for ambitious site builders." It refined the "Drupal is for ambitious digital experiences" vision. It chooses to focus on a specific persona and improve their experience with Drupal. It didn't sit perfectly well with me, so I wrote about how improving the Drupal developer experience empowers the Ambitious Site Builder. And with Drupal 10, there has been a huge shift in the development experience for frontend developers. Drupal 10.1.0 will bring even more improvements to the frontend developer experience. One of those will be the new Development Settings form to manage Twig development mode and markup caching.

Categories: FLOSS Project Planets

Promet Source: GAAD 2023: Integrating Digital Accessibility into DEI

Planet Drupal - Tue, 2023-05-16 07:40
This week, May 18, 2023, marks the 12th Annual Global Accessibility Awareness Day (GAAD).  Here at Promet Source, we view this annual event as an opportunity to celebrate the 1 billion individuals worldwide – 61 million in the Unites States – who are living with a disability and navigate all aspects of their lives with immense determination. More so than ever before, Promet is aligned with the theme of GAAD 2023: “... to get everyone talking, thinking and learning about digital access and inclusion.”
Categories: FLOSS Project Planets

PyPy: RPython-based emulator speeds up RISC-V simulation over 15x

Planet Python - Tue, 2023-05-16 07:22

In cooperation with RISC-V International, who funded a part of this project, we recently created a workflow to use RPython to take a Sail RISC-V model and automatically create a RISC-V ISA emulator from it, which we call Pydrofoil. The simulator sped up booting a linux emulator from 35 minutes (using the standard Sail-generated emulator in C) to 2 minutes, a speedup of 17.5x. More details about the process are in the RISC-V blog post.

A few take-aways from the project:

  • While PyPy has shown it can speed up generic python code about 4x, the technology behind PyPy can really shine in other areas.

  • RPython is malleable and can be molded to many tasks, the RPython meta-JIT is very flexible.

  • A JIT is well-suited for the problem of emulation, because it can perform dynamic binary translation.

PyPy can solve real world performance problems, even somewhat unusual ones. Please get in touch and let us know how we can help you solve yours!

Categories: FLOSS Project Planets

KDE & Google Summer of Code 2023

Planet KDE - Tue, 2023-05-16 06:15

By Benson Muite



This year KDE will mentor 9 projects in the Google Summer of Code (GSoC). GSoC is a program in which contributors new to open source spend between 175 and 350 hours working on an open source project.

Meet the projects and contributors for 2023:

Projects Kalendar



  • Improving Mail Integration in Kalendar - Aakarsh MJ will work on the mail integration in Kalendar. This will make it possible to use Kalendar as a full featured email client integrated with the already existing calendar and address book features.
  • Implement calendar availability - Anant Verma will work on calendar availability in Kalendar. This will allow you to specify your working hours where you are available and can be invited to meetings and events.
  • Calendar Invitations - Vansh K will work on adding support for calendar invitations to Kalendar, allowing you to send invitations to events and to also handle incoming invitations.
digiKam

  • Add Automatic Tags Assignment Tools and Improve Face Recognition Engine for digiKam - TRAN Quoc Hung will develop a deep learning model that will be able to recognize various categories of objects, scenes, and events in digital photos, generate corresponding keywords that can be stored in digiKam’s database and assign them to photos automatically.
  • Improve Items Properties Management Utkarsh Kumar will use machine-learning to improve the property labeling process for images in digiKam. At present, users are facing a host of difficulties when they try to transfer properties such as color, tags, and labels to numerous pictures. This is resulting in an extremely laborious and monotonous task of copying them repeatedly. This project will introduce a more seamless and efficient approach, which will enable users to execute these actions with a single click and drag of the mouse, thus significantly enhancing the user experience.
Krita



Improving the Bundle Creator - Sriruppa Datta will be working on improving and expanding the bundle creator in Krita. Bundles are packages of resources, like brushes or gradients that Krita users can add or swap out.

KDE Eco

Measuring Energy Consumption using Remote Lab - Karanjot will improve remote access to the KDE Eco energy measurement lab by:

  1. automating the energy measurement process, including providing a summary of the results
  2. setting up backend CI/CD integration
  3. setting up a frontend upload portal
Tokodon

Adding moderation tool in Tokodon project under KDE - Rishi Kumar will work on implementing the admin APIs in Tokodon. This will make Tokodon suitable as a Mastodon client for instance moderators and admins.

Okular

Improve Okular For Android - Shivodit will work on improving Okular for Android, bringing in the much needed font rendering improvement when fonts are not embedded in the PDF file among other things. The work will primarily be in the Poppler Freedesktop repository.

Next Steps

Over the next few weeks, candidates will be learning more about the KDE community, after which they will start the coding phase of their projects. Contributors will report on their progress on KDE's Planet.

We look forward to welcoming our new contributors and making their experience with KDE pleasant and fruitful.

Categories: FLOSS Project Planets

Peoples Blog: What to know before you choose a Drupal Hosting Platform?

Planet Drupal - Tue, 2023-05-16 04:30
In this article we are gonna see a few important notes which will be helpful for people who are looking out to host their Drupal application. When selecting a hosting platform for your Drupal website, there are several key factors to consider. Here are some important things to know before making a decision: Compatibility with Drupal: Ensure that the hosting platform is compatible with Drupal&#3
Categories: FLOSS Project Planets

Python Bytes: #336 We found one of your batteries

Planet Python - Tue, 2023-05-16 04:00
<a href='https://www.youtube.com/watch?v=sFav_lR8his' style='font-weight: bold;'>Watch on YouTube</a><br> <br> <p><strong>About the show</strong></p> <p>Sponsored by <a href="https://pythonbytes.fm/influxdata"><strong>InfluxDB</strong></a> from Influxdata.</p> <p><strong>Connect with the hosts</strong></p> <ul> <li>Michael: <a href="https://fosstodon.org/@mkennedy"><strong>@mkennedy@fosstodon.org</strong></a></li> <li>Brian: <a href="https://fosstodon.org/@brianokken"><strong>@brianokken@fosstodon.org</strong></a></li> <li>Show: <a href="https://fosstodon.org/@pythonbytes"><strong>@pythonbytes@fosstodon.org</strong></a></li> </ul> <p>Join us on YouTube at <a href="https://pythonbytes.fm/stream/live"><strong>pythonbytes.fm/live</strong></a> to be part of the audience. Usually Tuesdays at 11am PT. Older video versions available there too.</p> <p><strong>Brian #1:</strong> <a href="https://martinheinz.dev/blog/96"><strong>Python's Missing Batteries: Essential Libraries You're Missing Out On</strong></a> <a href="https://martinheinz.dev/blog/96"></a></p> <ul> <li>Martin Heinz</li> <li>Fun collection of a bunch of libraries you may not know about (or forgot about), with code examples.</li> <li>Utilities <ul> <li>boltons : iterate through json and dates, quickly grab data out of nested structures, and convert nested data with <code>jsonutils</code>, <code>timeutils</code>, and <code>iterutils</code></li> <li>sh : conveniently call shell funcitons</li> </ul></li> <li>Data Validation <ul> <li>validators : validate email addresses, credit cars, IP addresses, and more.</li> <li>the fuzz : fuzzy string comparisons</li> </ul></li> <li>Debugging <ul> <li>stackprinter : nice stack traces with exception messages higlighted</li> </ul></li> <li>Testing <ul> <li>freezegun : stop time, change dates, …</li> <li>dirty_equals : comparing things that are kinda equal</li> </ul></li> <li>CLI <ul> <li>tqdm : add a progress bar to command line apps</li> </ul></li> </ul> <p><strong>Michael #2:</strong> <a href="https://github.com/ddotta/awesome-polars"><strong>awesome-polars</strong></a></p> <ul> <li>A curated list of Polars talks, tools, examples &amp; articles.</li> <li>Mostly articles and tutorials however. </li> </ul> <p><strong>Brian #3:</strong> <a href="https://coderslegacy.com/running-headless-selenium-in-python-2023"><strong>Running Headless Selenium in Python (2023)</strong></a></p> <ul> <li>Siddiqi</li> <li>First off, if you are doing automated testing with Selenium, I hope you already know about headless. It’s awesome and speeds up testing.</li> <li>Next, there’s changes to how you code headless, as of Selenium 4.8.0 (Jan. 2023).</li> <li>Old: <ul> <li><code>options.headless` `**=**` `True</code></li> </ul></li> <li>New: <ul> <li><code>options.add_argument('--headless=new')</code> for Chrome</li> <li><code>options.add_argument('--headless')</code> for Firefox</li> </ul></li> <li>Reasons: <a href="https://www.selenium.dev/blog/2023/headless-is-going-away/">Read Headless is Going Away!</a> post on Selenium blog. <ul> <li>Subtitle: “Now that we got your attention, headless is not actually going away, just the convenience method to set it in Selenium”</li> </ul></li> </ul> <p><strong>Michael #4:</strong> <a href="https://github.com/guilatrova/gracy"><strong>Gracy</strong></a></p> <ul> <li>Gracy helps you handle failures, logging, retries, throttling, and tracking for all your HTTP interactions. </li> <li><p>Has support for</p> <ul> <li>Parsing per status code</li> <li>Throttling</li> <li>Retries</li> <li>Custom validation</li> <li>Record/replay for testing</li> </ul></li> <li><p>A bit non-pythonic but perhaps inspriation for some out there</p></li> </ul> <p><strong>Extras</strong> </p> <p>Michael:</p> <ul> <li><a href="https://mkennedy.codes/posts/mobile-apps-at-talk-python-python-flutter/"><strong>Mobile apps are finally out</strong></a> <ul> <li>Take the <a href="https://training.talkpython.fm/courses/up-and-running-with-git-a-pragmatic-ui-based-introduction"><strong>git course</strong></a> for free for a limited time.</li> <li><a href="https://mkennedy.codes/posts/mobile-apps-at-talk-python-python-flutter/"><strong>Michael's blog post</strong></a> announcing the apps</li> </ul></li> </ul> <p><strong>Joke:</strong> <a href="https://www.reddit.com/r/programminghumor/comments/13clz5m/its_practice/"><strong>It’s practice</strong></a></p>
Categories: FLOSS Project Planets

Plasma Sprint 2023 in Augsburg

Planet KDE - Tue, 2023-05-16 04:00

Finally a sprint again! The 2019 the Plasma Sprint in Valencia was my first in person KDE event and I was hooked instantly. However something mysterious happened in the next years that and in person meet ups stopped happening. While Akademy 2022 happened in person again, a sprint has different atmosphere and I was awesome to see people that couldn’t make Akademy or for whom it was their first sprint. Other attendees have blogged about the sprint as well, check them out too. Either on the planet or over on discuss Carl created a collection of a bunch of them.

So what did I do? Aside from the usual talking, discussion, planing which the others already blogged about. (And of course the live bug investigations on fellow developers’ machines who always seem to attract the weirdest issues.) I am afraid I spent the rest of the time on boring backend stuff.

For Plasma 6 we want to make use of the layer-shell wayland protocol for positioning and stacking Plasma’s own panels, backgrounds and some other windows such as krunner. This protocol was developed by the awesome wlroots people and is currently proposed for standardization. In the past we used our own plasma-shell protocol and unfortunately not only Plasma is using it at the moment but it also spread to applications with more advanced use cases than just having a normal window. As mixing windows from both systems together will be harder to layout and in general a desire to move on from the plasma-shell protocol for a more streamlined experienced I ported yakuake to use layer shell via our own Layer Shell Qt library.

Still Wayland related but probably even less interesting, I started porting libtaskmanager away from KWayland. KWayland was/is a framework consisting of two libraries (client and server side) wrapping wayland code for more straightforward consumption of Qt programs. As you can imagine this is quite some amount of boring code to maintain and with other good solutions available we would like to stop doing so. The server part of the framework was already moved to KWin in the past and we embraced qtwaylandscanner there for generating code wrapping wayland. For the client side we are now moving in the same direction. KWayland included everything but most of it was not used because Qt handles all the normal interactions with the compositor and almost all the remaining protocol interfaces were used only once because there is only a single place in our stack that needs to communicate the additional information with KWin (for example Klipper or the taskmanager). So it makes sense to move the code to the places where it is used instead of having to maintain a framework with the usual stability guarantees.

But I did not only work towards eliminating a framework, I also created an entire new one. Sorry! Enter KColorScheme. To be fair it’s not entirely new but KColorScheme and friends moved to their own library. KColorScheme was a pain point in our dependency stack since it was very central but its location in the KConfigWidgets library meant that you had to depend on a bunch of unwanted stuff just to read some colors. So we decided at the sprint to split it out to a new library and I implemented that. Afterwards I adjusted some consumers which do not need to longer depend on KConfigWidgets together with Nicolas.

As you can see it was quite the productive sprint which was possible because of the awesome people at Tuxedo Computers who hosted us and the KDE e.V that enables people from all over the world to come together and build awesome things. Consider donating so that we can continue doing so. The last thing left to say, I will be at Akademy which happens soon. See you there!

Categories: FLOSS Project Planets

Django Weblog: Django 5.x Steering Council Election Results

Planet Python - Tue, 2023-05-16 02:52

The Steering Council for the Django 5.x release cycle will be:

  • Simon Charette
  • Andrew Godwin
  • Adam Johnson
  • James Bennett

Congratulations to the new council, and a special thank you for the departing members Thomas Forbes and Florian Apolloner.

Thank you to everyone who participated in the nominations and voting.

Voting breakdown:

  • 268 eligible voters
  • 74 votes received

There were not enough candidates to fill the positions available, so all candidates were successful.

Full voting breakdown available on RankedVote.co.

Categories: FLOSS Project Planets

Specbee: Testing Drupal Websites for Accessibility with WCAG 2.1

Planet Drupal - Tue, 2023-05-16 02:35
Testing Drupal Websites for Accessibility with WCAG 2.1 Nusrat Fathima 16 May, 2023 Subscribe to our Newsletter Now Subscribe Leave this field blank

Did you know that 1 in 4 adults in the US has a disability*? That's a staggering 61 million people who may be impacted by inaccessible websites. 

As a Drupal developer, you have the power to make a difference by ensuring your site meets the Web Content Accessibility Guidelines (WCAG). In this blog, we'll explore the key features and best practices for testing WCAG 2.1 accessibility in Drupal, so you can create websites that are inclusive for all users.

What is Accessibility (A11y)

Accessibility refers to making a website usable by as many people as possible, particularly regarding people with disabilities. What does A11y refer to? A11y simply refers to the word “Accessibility” where 11 denotes the number of characters present between “A” and “Y”

WCAG and its origins

WCAG stands for Web Content Accessibility Guidelines. It is considered to be the standardized benchmark for website accessibility. Created by the World Wide Web Consortium (W3C), WCAG guidelines are the best and the easiest way of making your website usable for everyone. WCAG guidelines are strictly followed by most government and healthcare organizations in order to ensure that the website abides by accessibility laws (for example, section 508).

Principles of WCAG

There are four major principles of accessibility if you want to adopt WCAG compliance. These follow the ‘POUR’ acronym, which stands for Perceivable, Operable, Understandable, and Robust.

Perceivable

In simple words, the content should be detectable to a user’s senses. This means that they must be able to recognize the information that is being presented to them. It includes allowing users to hear audio content clearly and making sure that it is distinguishable from background noise.

Operable

It's all about making sure users can comfortably navigate your website and the interface is user-friendly. There mustn’t be any inaccessible part of the site or any steps that users might be incapable of completing. Should avoid fast-moving and flashing content when designing your web content. It should be made easy to surf the site without a keyboard, also making sure the site is keyboard accessible for users who can’t use a mouse.

Understandable

Users must be able to process the information presented to them and also be able to understand how to use and navigate through the website. This includes making the text clear and readable and predictable to make sure that pages appear in a user-friendly and anticipated way.

Robust

This highlights that the technology could evolve and build your website with future technology evolvements. As a thumb rule, if technology changes and develops, the content should also remain accessible. It also needs to be comprehended by a wide spectrum of users with different disabilities and still remains easy to understand. For example, the site made it compatible to work with the newest version of screen readers.

Why should you comply with WCAG

Below are a few of the advantages of WCAG implementation

  • An accessible website builds goodwill toward your brand and improves search engine optimization. Your site will rank more highly in search engine results.
  • An accessible website enables greater engagement, reach, and retention since you will have efficiently removed the barriers that prevent an extensive range of people from accessing your content.
  • Once WCAG is implemented as part of any website, it is considered standard.
WCAG 2.0/2.1 Success Criteria: The Most Widely Implemented
  1. Every input field on the site has an appropriate label
  2. Decorative content is implemented in a way that it can be ignored by assistive technology. For example, decorative images ignored by screen readers
  3. Captions are provided for all prerecorded audio/video content
  4. Sign language interpretation is provided for the audio content
  5. A meaningful sequence of content, for example the headings on any page are placed in a sequential order
  6. The user is able to pause or stop the audio or control the audio volume independently from the overall system volume for automatically playing videos.
  7. The contrast ratio of text and images of text is 4.5:1, and large-scale text and images are 3:1. The contrast ratio does not apply to decorative content or logo or brand name
  8. Low or no background audio. The pre-recorded audio/video does not contain any background noise
  9. All functionality of the content is accessible via the keyboard interface. There will be no keyword trap.
  10. Providing the users with enough time to read through and use the content
  11. If the content is time-based, the user is allowed to either pause or adjust the time constraint
  12. In case of any fast-moving content such as auto carousels, the user will be presented with options to pause or stop it
  13. When the authenticated user’s session expires, the user will be able to continue with the session without loss of data after re-authentication.
  14. Page titles, headings, and labels will describe the topic of the webpage
  15. Keyboard focus is visible and highlighted well
  16. Focusable components will receive focus in an order that signifies meaning and operability.
  17. Each section of the webpage is accompanied by its section heading
  18. Except for decorative images, all images have meaningful Alt text
  19. Audio, video, forms, dropdown menus, anchor text, URLs, and alt text is accessible to the keyboard and screen reader
  20. Content clarity adapts according to the zoom level of the page. Content is clearly visible, even on the maximum zoom level
  21. The header and footer menu with help context should be available on all pages throughout the site
  22. The sites have proper implementation of hover and focus states
Screen Readers

Many people with visual challenges use screen magnifiers and screen readers. A screen reader is software that reads digital text displayed on the screen aloud. Some screen reader examples include:

  • Paid products like JAWS (Windows) and Dolphin Screen Reader (Windows).
  • Freeware like NVDA (Windows), ChromeVox (Chrome) and Orca (Linux).
  • Software built-in the OS(Operating system), like VoiceOver (macOS, iPadOS, iOS), Narrator (Windows), ChromeVox (on Chrome OS) and TalkBack (Android).
Three levels of accessibility

WCAG 2.1 has 3 levels of success criteria

  • Level A: All 30 essential success criteria defined in WCAG 2.0 are met. At a minimum, all websites should achieve this level of compliance.
  • Level AA: All level A success criteria are met and an additional 28 accessibility success criteria are satisfied. This level achieves a broader accessibility spectrum and is often the goal for most of the development teams to achieve.
  • Level AAA: The website should satisfy all three levels of success criteria, including 28 additional success criteria. This level is typically reserved for special sites such as government organizations.

Most of the WCAG-compatible sites widely fall under the accessibility success criteria level A or AA.

Accessibility Testing a Drupal Website

The following are steps followed to perform an accessibility test for a Drupal website

  1. Run a Google Chrome Lighthouse audit and fix the highlighted accessibility issues
  2. Run a Wave tool audit
  3. Validate the keyboard highlight focus and the tab order of the page manually
  4. Validate the page with the required screen reader software. For example, VoiceOver
  5. Validate the site accessibility on all resolutions such as wide desktop, laptop, tab, and mobile devices
  6. Validate the site code using W3C’s validation tools for Markup and CSS
  7. Validate the color contrast of the site to make sure it meets the standards (4:5:1) by using a Chrome extensions tool like Kontrast or an application like Color Contrast Analyzer
How Drupal Helps Attain Accessibility Standards

Web development must include accessibility to make sure that websites are functional and accessible to those who have disabilities. Drupal is renowned for its steadfast dedication to accessibility. Take a look at how Drupal promotes accessibility and what tools and features it provides to assist websites to be more user-friendly.

1. Building Accessibility into Drupal

The goal of the Drupal development team has always been to build a platform that is usable by as many users as feasible. The Web Content Accessibility Guidelines (WCAG) 2.1 Level AA requirements are followed when developing the core Drupal platform. This indicates that a variety of disabilities, such as those affecting the visual, auditory, physical, verbal, cognitive, and neurological systems, can access Drupal websites.

2. Default Accessibility features
  • With their ability to adapt to various screen sizes and devices thanks to responsive design, Drupal's default themes are usable for those who use mobile devices or assistive technology.
  • Drupal creates semantic HTML markup, which aids screen readers and other assistive technology tools in comprehending the website's structure.
  • Drupal includes a field for adding alternate text to photos, which aids those who are blind in understanding the website's content.
  • Drupal supports keyboard navigation, which is crucial for users who are unable to use a mouse.
  • Forms that are easy to use and navigate with assistive technology are generated by Drupal that are accessible.
3. Drupal Provides Plugins & Modules for Accessibility

To improve a website's accessibility, Drupal provides a variety of accessibility modules and plugins that can be deployed. Several well-liked accessibility modules and plugins are as follows:

  • Editoria11y Accessibility Checker: This module examines a website for problems with accessibility and makes recommendations for solutions.
  • CKEditor Accessibility Plugin: This add-on improves the accessibility of the widely used text editor for Drupal, CKEditor.
  • All-in-one accessibility Widget: This module includes an accessibility widget that enables users to change the website's text size, contrast, and other accessibility-related settings.
  • A11Y: Form helpers: This module helps make all the Drupal webforms meet the accessibility standards
  • Block ARIA Landmark Roles: This module adds additional elements to the block configuration forms that allow users to assign an ARIA landmark role and/or ARIA labels to a block.
4. Strong Community Support for Accessibility

Developers and designers who are committed to accessibility make up a sizable part of the Drupal community. An effort led by the community to make Drupal more accessible to those with disabilities is the Drupal Accessibility Group. The group offers guidance, tools, and best practices for developing ADA-compliant Drupal websites.

References:

https://www.cdc.gov/ncbddd/disabilityandhealth/infographic-disability-impacts-all.html

Final Thoughts

Drupal is an effective and open-source content management system that provides a variety of features, tools, and plugins to improve a website's accessibility. Because of its dedication to accessibility and the strength of its accessibility-focused community, Drupal is a fantastic platform for building websites that are useable and accessible for those with impairments.
I hope this article provides you with the required insight on accessibility testing with WCAG 2.1. Want to build an accessible Drupal website from scratch or want to make your current site more accessible? We’d love to make that happen! Let’s talk!

Author: Nusrat Fathima

Meet Nusrat, a Software Tester by profession. Nusrat loves reading and learning new things. She enjoys road trips and exploring new places. When it comes to food, she loves to indulge in anything and everything chicken.

Email Address Subscribe Leave this field blank Drupal Software Testing Drupal Planet

Leave us a Comment

  Recent Blogs Image Testing Drupal Websites for Accessibility with WCAG 2.1 Image Understanding Update and Post Update Hooks for Successful Drupal Site Updates Image How to Setup a Drupal Site on Pantheon from Scratch Want to extract the maximum out of Drupal? TALK TO US Featured Case Studies

Upgrading the web presence of IEEE Information Theory Society, the most trusted voice for advanced technology

Explore

A Drupal powered multi-site, multi-lingual platform to enable a unified user experience at SEMI

Explore

Great Southern Homes, one of the fastest growing home builders in the US, sees greater results with Drupal

Explore
View all Case Studies
Categories: FLOSS Project Planets

death and gravity: Why you should still read the docs

Planet Python - Mon, 2023-05-15 21:42

Do you feel you're fighting your tools?

Do you feel you're relying too much on autocomplete and inline documentation? ...always kinda guessing when using libraries?

Or maybe not, but getting things done just seems harder than it should be.

This can have many causes, but I've repeatedly seen junior developers struggle with a specific one, not even fully aware they're struggling in the first place.

This is a story about:

  • why you should still read the docs
  • finding the right way of doing things
  • command-line interfaces

tl;dr: Most good documentation won't show up in your IDE – rather, it is about how to use the library, and the problem the library is solving.

An example #

OK, instead of telling you, let me show you – we'll go through one possible path of developing something, and stop along the way to point out how we're doing things.

Say you're writing a command-line tool that gets data from an API and saves it in a CSV file. It might look something like this:

import csv import time import click def get_data(): # pretend some API calls happen here time.sleep(2) return [ {"name": "Chaos", "short_name": "Chs"}, {"name": "Discord", "short_name": "Dsc"}, {"name": "Confusion", "short_name": "Cfn"}, {"name": "Bureaucracy", "short_name": "Bcy"}, {"name": "The Aftermath", "short_name": "Afm"}, ] def write_csv(data): with open("calendar.csv", 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['name', 'short_name']) for row in data: writer.writerow([row['name'], row['short_name']]) @click.command() def main(): """Retrieve the names of the Discordian seasons to calendar.csv.""" data = get_data() write_csv(data) if __name__ == '__main__': main()

Here it is in action:

$ python seasons.py --help Usage: seasons.py [OPTIONS] Retrieve the names of the Discordian seasons to calendar.csv. Options: --help Show this message and exit. $ python seasons.py $ cat calendar.csv name,short_name Chaos,Chs Discord,Dsc Confusion,Cfn Bureaucracy,Bcy The Aftermath,Afm

You're using Click because it’s powerful but comes with sensible defaults.

This is pretty good code – instead of calling get_data() from write_csv(), you're passing its result to write_csv() – in other words, instead of hiding the input, you've decoupled it.1

As a result, write_csv() doesn't need to change when adding new arguments to get_data(), and we can test it without monkeypatching get_data():

import seasons DATA = [ {"name": "one", "short_name": "1"}, {"name": "two", "short_name": "2"}, ] CSV_BYTES = b"name,short_name\r\none,1\r\ntwo,2\r\n" def test_write_csv(tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) seasons.write_csv(DATA) assert tmp_path.joinpath('calendar.csv').read_bytes() == CSV_BYTES The path is hardcoded #

But, what if you need to run it twice, and save the output in different files? This becomes useful as soon as the output can change. Also, having to be in the right directory when running it is not cool.

The obvious solution is to pass the path in:

@click.command() @click.option('--path', required=True) def main(path): """Retrieve the names of the Discordian seasons.""" data = get_data() write_csv(data, path) def write_csv(data, path): with open(path, 'w', newline='') as f:

We're definitely on the right track here – the test is shorter, and we don't even need to monkeypatch the current working directory anymore:

def test_write_csv(tmp_path): out_path = tmp_path.joinpath('out.csv') seasons.write_csv(DATA, out_path) assert out_path.read_bytes() == CSV_BYTES The path might not be valid #

But, what if you pass a directory as the path?

$ time python seasons.py --path . Traceback (most recent call last): ... IsADirectoryError: [Errno 21] Is a directory: '.' python seasons.py --path . 0.07s user 0.02s system 4% cpu 2.104 total

OK, it fails with a traceback. Worse, we waited for the API calls only to throw the output away. Thankfully, Click has fancy parameter types that can take care of that:

@click.command() @click.option('--path', type=click.Path(exists=False, dir_okay=False, writable=True), required=True) def main(path): $ time python seasons.py --path . Usage: seasons.py [OPTIONS] Try 'seasons.py --help' for help. Error: Invalid value for '--path': File '.' is a directory. python seasons.py --path . 0.08s user 0.02s system 92% cpu 0.112 total

Not only do we get a nice error message, we get it instantly!

The path to what, exactly? #

Thing is, --path isn't very descriptive. I wonder if there's a better name for it...

One thing I like to do is to look at what others are doing.

$ curl --help Usage: curl [options...] <url> -o, --output <file> Write to file instead of stdout -O, --remote-name Write output to a file named as the remote file ... $ wget --help GNU Wget 1.21.2, a non-interactive network retriever. Usage: wget [OPTION]... [URL]... -o, --output-file=FILE log messages to FILE -O, --output-document=FILE write documents to FILE $ sort --help Usage: sort [OPTION]... [FILE]... -o, --output=FILE write result to FILE instead of standard output $ pandoc --help pandoc [OPTIONS] [FILES] -o FILE --output=FILE

Better yet, sometimes there's a comprehensive guide on a topic, like Command Line Interface Guidelines ...and under Arguments and flags, we find this:

Use standard names for flags, if there is a standard. If another commonly used command uses a flag name, it’s best to follow that existing pattern. That way, a user doesn’t have to remember two different options (and which command it applies to), and users can even guess an option without having to look at the help text.

Here’s a list of commonly used options: [...]

  • -o, --output: Output file. For example, sort, gcc.
Standard output #

Further down below, there's another guideline:

If input or output is a file, support - to read from stdin or write to stdout. This lets the output of another command be the input of your command and vice versa, without using a temporary file. [...]

I wonder how we could achieve this.

One way would be to check the value and use sys.stdout if it's -.

But let's pause and look at the Path docs a bit:

allow_dash (bool) – Allow a single dash as a value, which indicates a standard stream (but does not open it). Use open_file() to handle opening this value.

Encouraging; following along, open_file() says:

Open a file, with extra behavior to handle '-' to indicate a standard stream, lazy open on write, and atomic write. Similar to the behavior of the File param type.

What's this File, eh?

Declares a parameter to be a file for reading or writing. [...] The special value - indicates stdin or stdout depending on the mode. [...]

See File Arguments for more information.

Which finally takes us away from the API reference to the actual documentation:2

Since all the examples have already worked with filenames, it makes sense to explain how to deal with files properly. Command line tools are more fun if they work with files the Unix way, which is to accept - as a special file that refers to stdin/stdout.

Click supports this through the click.File type which intelligently handles files for you. [...]

OK, OK, I get it, let's use it:

@click.command() @click.option('-o', '--output', type=click.File('w', lazy=False), required=True) def main(output): """Retrieve the names of the Discordian seasons.""" data = get_data() # click.File doesn't have a newline argument output.reconfigure(newline='') write_csv(data, output)

With passing a file object to write_csv(), the output part of I/O is also decoupled:

def write_csv(data, file): writer = csv.writer(file) writer.writerow(['name', 'short_name']) for row in data: writer.writerow([row['name'], row['short_name']])

Interestingly enough, csv.writer already takes an open file.

Anyway, it works:

$ python seasons.py --output - name,short_name Chaos,Chs Discord,Dsc Confusion,Cfn Bureaucracy,Bcy The Aftermath,Afm

Once again, the test gets simpler too – instead of writing anything to disk, we can use an in-memory stream:

def test_write_csv(): file = io.StringIO() seasons.write_csv(DATA, file) assert file.getvalue().encode() == CSV_BYTES ...by default #

Some of the help texts we looked at say "write to FILE instead of stdout"...

Why instead?

Using commands in a pipeline is common enough that most Unix commands write to stdout by default; some don't even bother with an --output option, since you can always redirect stdout to a file:

$ tail -n+2 calendar.csv | head -n2 Chaos,Chs Discord,Dsc $ tail -n+2 calendar.csv > calendar-no-heading.csv $ head -n2 calendar-no-heading.csv Chaos,Chs Discord,Dsc

It is so common that CLI Guidelines makes it the third thing in The Basics:

Send output to stdout. The primary output for your command should go to stdout. Anything that is machine readable should also go to stdout—this is where piping sends things by default.

We could remove --output, but with Click, doing both is trivially easy:

@click.option('-o', '--output', type=click.File('w', lazy=False), default='-') $ python seasons.py | tail -n+2 | head -n2 Chaos,Chs Discord,Dsc Discussion #

Looking at our journey, I can't help but notice a few things:

  • The Click API reference seems to constantly funnel readers to the user guide part of the documentation, the part that tells you how and why to use stuff.
  • Click mentions supporting - for stdout, and more than once.
  • The Click user guide discusses File arguments before Path (in part, because it's more commonly used, but also because it's more useful out of the box).
  • csv.writer takes an open file, just like our own write_csv() does at the end.

These all hint that it may be a good idea to structure code a certain way; the more we use things the way they "want" to be used, the cleaner the code and the tests get.

Good libraries educate #

I am tempted to call Click a great library.

Yes, it does its job more than well, and has lots of features.

But, more importantly, its documentation educates the reader about the subject matter – it goes beyond API docs into how to use it, how to get specific things done with it, and the underlying problem it is solving.

Here are a few more examples:

  • Classics – other libraries in the Flask/Pallets ecosystem (like Werkzeug and Jinja), Requests, pytest, and many others – popular and old enough libraries usually have great documentation.
  • Python itself – you would be amazed at how many developers use Python every day without having gone through the tutorial even once.
  • feedparser teaches you about all kinds of Atom and RSS quirks, encodings on the web, HTTP, and how to be nice to servers you're making requests to.
    • feedparser is close to my heart because I'm using it for my feed reader library, reader, where I too am doing my best to have docs worth reading.
  • Stepping away from Python a bit, I've learned a lot from both Dagger and Mockito (and for the latter, the API docs for the "main" class pack way more than just API, and that will show up in your IDE).
  • Beancount, a command-line accounting tool, does a great job of teaching double-entry bookkeeping (it taught me almost all I know about the topic).
Read the docs #

In a way, this whole article is a ruse to tell you to RTFM, but with a bit of nuance.

Read the documentation to find out how, and more importantly, why – a mental model of the thing you're using will make you more productive, and will improve any guesses you make.

Aside from the user guide part, I also like to skim the reference for libraries I use a lot; this gives me an idea of what's available, and often I remember later that "hey, there was something in the docs about X".

But the docs suck #

Sometimes, documentation sucks or is missing entirely (doubly so for internal code). It's easy to get a habit of not bothering to look, when most of the time it's not there.

Still, always check. Worst case, you'll have wasted a minute.

On the other hand, docs should definitely be one of the criteria when picking a library.

But I'm using lots of libraries #

Quoting a friend:

Well, for you, it might be "a library", but for them it's 10 new libraries, and a 2-day deadline.

Like with most things, the Pareto principle applies:

  • Read the docs for big stuff you use often – language, standard library, test framework, web framework or database of choice, and so on; these have an outsized return on investment, and start paying out quite quickly.
  • Don't read all the docs; skimming is often enough.

On the other hand, there is value in not using that many libraries.

Conclusion #

To sum up:

  • Good libraries educate on the subject matter.
  • Read the documentation of the things you are using a lot.
  • If you feel you're fighting your tools, maybe you're using them wrong (read the docs!), but maybe they're not all that good; it's OK to look for better ones.

I'll end with a related idea from a post called Why I Don’t Use Autocomplete:

But be sure that if you do use autocomplete that you’re not leaning on it as a crutch. Be sure that you’re not deciding what to write as you write it [...] and not just using a method because the IDE tells you it’s available.

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

Want to know when new articles come out? Subscribe here to get new stuff straight to your inbox!

  1. For a detailed example about decoupling I/O, how it affects affects testing, plus interesting historical background, check out Brandon Rhodes' great talk The Clean Architecture in Python. [return]

  2. Is it not written, "A few hours of trial and error can save you five minutes of reading the docs"? [return]

Categories: FLOSS Project Planets

Django Weblog: May 2023 Grants

Planet Python - Mon, 2023-05-15 20:18

I wanted to share today some of the great things that DSF donations go towards. This month was a particularly big month in grant approval, so I wanted to highlight some of those, and with it, some of the exciting things happening in the Django community.

$6,000 to DjangoCon Africa

The first DjangoCon Africa was scheduled to take place in 2020. However, with the global health situation as it was, 2023 is now the inaugural DjangoCon Africa, and the DSF is excited to be part of that. This grant is to help DjangoCon Africa get started, and to fund smaller grants help attendees attend DjangoCon Africa. DjangoCon Africa will be November 6-11. https://2023.djangocon.africa/

$6,000 to DjangoCon US

This year, DjangoCon US is a hybrid conference, taking place in Durham, NC, and online. This was a great success last year, making the conference more accessible than ever, and there has been special focus this year on enhancing both conference experiences. 100% of the DSF’s donation for DjangoCon US will go to Opportunity Grants, to help people who would be unable to afford the conference, travel, or lodging, to attend the conference. DjangoCon US will take place October 16-20, and a special online tutorials day will happen the week before the conference, on October 8th. Tutorial tickets are separate from conference tickets. https://2023.djangocon.us/

While we are talking about DjangoCons, I didn’t want to go without talking about DjangoCon Europe and DjangoCon Australia, even though grants for these were approved in previous months.

DjangoCon Europe is coming up soon! May 29th- June 2 in Edinburgh! https://2023.djangocon.eu/ Tickets are on sale now!

DjangoCon Australia is a one day track that runs alongside PyCon US. It will be August 18, and PyCon AU will continue August 19-22. The CFP is open now: https://2023.djangocon.com.au/

$1,000 Other

The DSF also vets and approves smaller grants for Django groups all over the world. This month, $1000 in other grants was given out.

$10,000 to DjangoGirls

Finally, I wanted to highlight the DSF’s work with DjangoGirls. DjangoGirls is an organization that provides the reference materials and other resources to empower local groups around the world to host workshops, with a focus on teaching women Python and Django. The DSF supports the organization as a whole, as well as directly sponsoring local groups.

If you are in a position to support these efforts, please consider donating to the DSF. None of this can happen without community support.

For individual and smaller company donations, donate here: https://www.djangoproject.com/fundraising/

If you wish, your name and a link of your choice can appear on our website for one year. Donations can be one-time, monthly, quarterly, or yearly.

You can also donate through GitHub: https://github.com/sponsors/django

For companies able to make a larger commitment ($2000+), please look at corporate membership: https://www.djangoproject.com/foundation/corporate-membership/ and consider making an application: https://www.djangoproject.com/foundation/corporate-membership/join/

Thank you,

Catherine Holmes

DSF Assistant

Categories: FLOSS Project Planets

Freexian Collaborators: Monthly report about Debian Long Term Support, April 2023 (by Roberto C. Sánchez)

Planet Debian - Mon, 2023-05-15 20:00

Like each month, have a look at the work funded by Freexian’s Debian LTS offering.

Debian LTS contributors

In April, 18 contributors have been paid to work on Debian LTS, their reports are available:

  • Abhijith PA did 6.0h (out of 0h assigned and 14.0h from previous period), thus carrying over 8.0h to the next month.
  • Adrian Bunk did 18.0h (out of 16.5h assigned and 24.0h from previous period), thus carrying over 22.5h to the next month.
  • Anton Gladky did 8.0h (out of 9.5h assigned and 5.5h from previous period), thus carrying over 7.0h to the next month.
  • Bastien Roucariès did 17.0h (out of 17.0h assigned and 3.0h from previous period), thus carrying over 3.0h to the next month.
  • Ben Hutchings did 16.0h (out of 12.0h assigned and 12.0h from previous period), thus carrying over 8.0h to the next month.
  • Chris Lamb did 18.0h (out of 18.0h assigned).
  • Dominik George did 0.0h (out of 0h assigned and 20.34h from previous period), thus carrying over 20.34h to the next month.
  • Emilio Pozuelo Monfort did 4.5h (out of 11.0h assigned and 9.5h from previous period), thus carrying over 16.0h to the next month.
  • Guilhem Moulin did 8.5h (out of 8.0h assigned and 12.0h from previous period), thus carrying over 11.5h to the next month.
  • Helmut Grohne did 5.0h (out of 2.5h assigned and 7.5h from previous period), thus carrying over 5.0h to the next month.
  • Lee Garrett did 0.0h (out of 31.5h assigned and 9.0h from previous period), thus carrying over 40.5h to the next month.
  • Markus Koschany did 40.0h (out of 40.0h assigned).
  • Ola Lundqvist did 12.5h (out of 0h assigned and 24.0h from previous period), thus carrying over 11.5h to the next month.
  • Roberto C. Sánchez did 8.5h (out of 4.75h assigned and 15.25h from previous period), thus carrying over 11.5h to the next month.
  • Stefano Rivera did 1.0h (out of 0h assigned and 28.0h from previous period), thus carrying over 27.0h to the next month.
  • Sylvain Beucler did 35.0h (out of 40.5h assigned), thus carrying over 5.5h to the next month.
  • Thorsten Alteholz did 14.0h (out of 14.0h assigned).
  • Tobias Frost did 15.0h (out of 15.0h assigned and 1.0h from previous period), thus carrying over 1.0h to the next month.
Evolution of the situation

In April, we have released 35 DLAs.

The LTS team would like to welcome our newest sponsor, Institut Camille Jordan, a French research lab. Thanks to the support of the many LTS sponsors, the entire Debian community benefits from direct security updates, as well as indirect improvements and collaboration with other members of the Debian community.

As part of improving the efficiency of our work and the quality of the security updates we produce, the LTS has continued improving our workflow. Improvements include more consistent tagging of release versions in Git and broader use of continuous integration (CI) to ensure packages are tested thoroughly and consistently. Sponsors and users can rest assured that we work continuously to maintain and improve the already high quality of the work that we do.

Thanks to our sponsors

Sponsors that joined recently are in bold.

Categories: FLOSS Project Planets

Nonprofit Drupal posts: May Drupal for Nonprofits Chat

Planet Drupal - Mon, 2023-05-15 17:05

Join us on Thursday, May 18 at 1pm ET / 10am PT, for our regularly scheduled call to chat about all things Drupal and nonprofits. (Convert to your local time zone.)

In this month's informal chat, we'll be talking about local development environments.  Got something specific on your mind? Feel free to share ahead of time in our collaborative Google doc: https://nten.org/drupal/notes!

All nonprofit Drupal devs and users, regardless of experience level, are always welcome on this call.

This free call is sponsored by NTEN.org and open to everyone. 

  • Join the call: https://us02web.zoom.us/j/81817469653

    • Meeting ID: 818 1746 9653
      Passcode: 551681

    • One tap mobile:
      +16699006833,,81817469653# US (San Jose)
      +13462487799,,81817469653# US (Houston)

    • Dial by your location:
      +1 669 900 6833 US (San Jose)
      +1 346 248 7799 US (Houston)
      +1 253 215 8782 US (Tacoma)
      +1 929 205 6099 US (New York)
      +1 301 715 8592 US (Washington DC)
      +1 312 626 6799 US (Chicago)

    • Find your local number: https://us02web.zoom.us/u/kpV1o65N

  • Follow along on Google Docs: https://nten.org/drupal/notes

View notes of previous months' calls.

Categories: FLOSS Project Planets

PyCharm: The Early Access Program for PyCharm 2023.2 Is Open!

Planet Python - Mon, 2023-05-15 15:53

The Early Access Program starts today! This program offers you an early glimpse of the new features and improvements expected to land in the next version. If you’re not yet familiar with the EAP concept, check out this blog post for more details.

Over the next few weeks, join us in exploring the newest additions to PyCharm, and share your feedback after testing out the new features.

The first PyCharm 2023.2 EAP build introduces a reworked Python Run/Debug Configurations dialog, initial support for Polars, better support for Jinja templates, support for pytest fixture override, a new solution for seamlessly generating shared indexes for projects, along with a few UI improvements. 

You can download the build from our website, get it from the free Toolbox App, or update to it using snaps if you’re an Ubuntu user.

Download PyCharm 2023.2 EAP

Let’s take a look at what other new features and improvements are available to try in this build.

User experience Reworked hamburger menu in the main toolbar on Windows and Linux

We’ve refined the behavior of the hamburger menu located in the main toolbar for Windows and Linux. Once you click on the menu icon, the elements now appear horizontally over the toolbar.

In addition, you can now turn this menu into a separate toolbar by going to View | Appearance | Main menu as a Separate Toolbar.

Updated window controls on macOS 

When using the new UI in Full Screen mode on macOS, the window controls are now displayed right in the main toolbar – not in the floating bar as before.

Reworked Python Run/Debug Configurations dialog

PyCharm 2023.2 will display a reworked Run/Debug Configurations dialog for Python run configurations. The main focus for us while working on it was to provide a simple way to access the main settings. All others, such as the Run with Python Console, Emulate terminal in output console, and Before Launch options can be found under the Modify options menu. 

You can access the Run/Debug Configurations dialog from the main menu by selecting Run | Edit Configurations. Alternatively, you can use the shortcut:  ^⌥ R on macOS or Alt+Shift+F10 on Windows, then press 0.

Please try the updated dialog and share your feedback with us.

Bundled OpenAPI Specifications plugin

The OpenAPI Specifications plugin will be bundled with PyCharm 2023.2 Professional Edition. This plugin provides essential functionality for web and microservices development. It provides support for both OpenAPI and Swagger specification files. The main features include validation by JSON Schema, navigation, completion and rename refactoring for URLs, code generation with Swagger Codegen and OpenAPI Generator, structural diff action for specifications, URL completion in the HTTP Client by specifications, and Structure view for OpenAPI files.

You can set up your OpenAPI or Swagger specifications by going to Settings | Languages & Frameworks | OpenAPI Specifications.

Polars framework support

The Polars DataFrame library has recently become popular because of its impressive high-performance capabilities.

As an initial step, we’ve added support for column-name completion in Polars functions, making it easier for you to work with the library and data in PyCharm.

Additionally, PyCharm now offers interactive tables for Polars DataFrames in Jupyter notebooks, allowing you to sort, export, and view data with just a few clicks.

Enhanced support for Jinja templates

According to its documentation, Jinja templates don’t require any specific file extension, as they can generate any text-based format. But sometimes it is convenient to use composite extensions like user.html.jinja. This is extremely useful for Ansible templates, where such conventions are used.

In these cases, PyCharm 2023.2 will provide proper syntax highlighting and code completion for both template language and data language. Among the recognized text-based formats are .xml, .yaml, .properties, and others. *-playbook.yaml files are now also detected as Jinja2 files with YAML data language.

Along with .j2 and .jinja2, PyCharm 2023.2 will automatically recognize files with .jinja file extension as Jinja templates and have respective syntax highlighting and code completion.

Testing Support for fixture override in pytest

For PyCharm 2023.2, we will be looking into improving your experience with pytest. The first EAP build introduces better support for fixture override. For the overridden pytest fixtures, PyCharm 2023.2 will provide correct type inference, navigation, and code completion.

PyCharm 2023.2 will resolve the fixtures in the same way pytest does. It will start with checking the fixture inside the test class (if there is one), then it’ll check the fixture inside the current file, after that in the import statements, and finally in the nearest conftest.py file.

Please try it and share your feedback under this post or in our issue tracker.

Notable bug fixes

We fixed connectivity issues affecting the Python console and debugger while working with a WSL interpreter and VPN. [PY-59608]

Dataclasses 

PyCharm no longer raises an error if redeclared fields in the dataclass’s subclass follow the parent’s dataclass field with the default value. [PY-45381]

PyCharm now recognizes the ‘kw_only’ notation in dataclasses. [PY-49946]

Python / Debug console

We fixed various bugs related to the Python and Debug consoles, including: 

  • Issues with showing Quick Documentation in the Python console. One of the fixes was provided by an external contributor, Daniel Schmidt. [PY-57950], [PY-55889].
  • Issues that had been occurring when executing multi-line commands in the console. [PY-20363], [PY-42035]
  • The Debug console now automatically becomes the active window when the user’s input is required in the debugging code. [PY-26762]

These are the most notable updates for this week. To see the full list of changes in this EAP build, please refer to the release notes.

If you encounter any bugs while working with this build, please submit a report using our issue tracker. If you have any questions or feedback, let us know in the comments below or get in touch with our team on Twitter.

Categories: FLOSS Project Planets

Marcos Dione: unintuitive-expressions

Planet Python - Mon, 2023-05-15 14:52

One of the aphorisms of the Zen of Python says:

Explicit is better than implicit.

Go dehl writes this code to solve FizzBuzz:

for i in range(1, 101): print(&aposFB&apos if not i % 15 else &aposF&apos if not i % 3 else &aposB&apos if not i % 5 else i)

Read that expression out loud: “the string 'FB' If not i modulus 15; else 'F' if no i modulus 3; else 'B' if not i modulus 5; else i”. It sounds exactly like the opposite of the solution! Why? Because Go dehl is (ab)using the fact that 0 is false-y. A number a is a multiple of another number b if a % b is 0, and 0 evaluates to False in boolean context[1], so he's negating that instead of writing if i % 15 == 0 . It's a tendency that I also see in other languages where they write a && b instead of if a: b. Sometimes we get used to these shorthands, but in this case, to me, it was completely confusing. So, please, “Explicit is better than implicit”.

I would have written this as a comment on their blog, but it only works if I login with a Google account, which I don't have; hence, this post.

python

[1] See my previous take on this

Categories: FLOSS Project Planets

Talking Drupal: Talking Drupal #399 - Working Remotely

Planet Drupal - Mon, 2023-05-15 14:00

Today we are talking about Working Remotely with Alan Sherry & Jordan Graham.

For show notes visit: www.talkingDrupal.com/399

Topics
  • Why is remote work great
  • Why is remote work hard
  • Tips
  • Working internationally
  • Large vs small company
  • Why do you like working from home
  • Why do you think companies are pushing back to the office
  • What do you miss about the office
Resources Guests

Alan Sherry - Drupal Profile

Hosts

Nic Laflin - www.nLighteneddevelopment.com @nicxvan John Picozzi - www.epam.com @johnpicozzi Jordan Graham - @jordanlgraham

MOTW Correspondent

Martin Anderson-Clutz - @mandclu oEmbed Providers Extends Drupal’s ability to embed content from third-party sites.

Categories: FLOSS Project Planets

CodersLegacy: How to Install Webdriver_manager Chrome in Selenium Python?

Planet Python - Mon, 2023-05-15 13:11

In this Python tutorial, we will learn how to install Selenium for Chrome using the Webdriver_manager module.

Selenium is a popular open-source framework used for automating web browsers. It provides a convenient way to interact with web elements, perform actions, and extract data from websites. To use Selenium with Python, you need to have the appropriate WebDriver installed for the browser you wish to automate.

One of the challenges with Selenium is managing the WebDriver executables and keeping them up to date. WebDriver executables are required to establish a connection between Selenium and the web browser. They vary depending on the browser and the operating system you are using.

Fortunately, there is a handy Python library called webdriver_manager that simplifies the process of managing WebDriver executables. It automatically downloads the required webdriver executables by detecting the installed browsers you have. All we need is a few extra lines of code in our python programs.

In this article, we will guide you through the installation process of webdriver_manager for Chrome in Selenium Python.

Install Selenium for Chrome using Webdriver_manager in Python

We will be explaining how to setup your Python, Selenium, and Webdriver_manager environments in the following steps. You may already have some of these installed (e.g Python or Selenium), so skip the steps you have already completed.

Step 1: Set up a Python environment

Before we begin, make sure you have Python installed on your system. You can download and install Python from the official Python website (https://www.python.org/) if you haven’t already. It is recommended to use Python version 3.6 or above for compatibility with newer Selenium versions.

You can check your Python version using the following command:

python -V Step 2: Install Selenium

Next, you need to install the Selenium Python package. Open a terminal or command prompt and run the following command:

pip install selenium

This command will download and install the latest version of Selenium from the Python Package Index (PyPI).

Step 3: Install webdriver_manager

Once Selenium is installed, you can proceed to install webdriver_manager by running the following command:

pip install webdriver_manager

This command will fetch the webdriver_manager package from PyPI and install it in your Python environment.

Step 4: Import webdriver_manager and use it with Selenium

Now that you have installed webdriver_manager, you can import it in your Python script and use it to manage WebDriver executables. The syntax and functions involved are bit different based on the browser you are planning to use.

Here is the code for using the webdriver_manager in Python Selenium for Chrome.

First we make the required imports. There are some additional imports here needed from the webdriver_manager module.

from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium import webdriver

Next, we need to create the “driver” object which we will be using to extract and parse data from websites. We are using the ChromeDriverManager import here from the webdriver_manager module to automatically download and install the required files.

service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=service)

Now we can begin extracting data from websites, by making a “get” call to the URL of your choice.

driver.get('https://example.com')

Once this “get” method has been called, we now have access to all the raw HTML data from the URL passed into this

Finally, we will call the quit() method on the driver object to close the browser window.

driver.quit()

If you do not want the browser window to appear, you can activate “headless” mode. This keeps the selenium driver running “under the hood”. Other than the obvious benefit of not spawning an annoying window each time, “headless” mode is also faster and more efficient.

This marks the end of the How to Install Webdriver_manager Chrome in Selenium Python? 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 How to Install Webdriver_manager Chrome in Selenium Python? appeared first on CodersLegacy.

Categories: FLOSS Project Planets

Chromatic Insights: Building Bridges in Backdrop with Jen Lampton

Planet Drupal - Mon, 2023-05-15 12:12
Jen Lampton joins us! As the co-founder of Backdrop, Jen provides plenty of insight into why Drupal 7’s EOL requires a multi-pronged solution for different customer bases. Mark and Jen dive deep into Drupal alternatives and the challenges behind them, taking a closer look at where Backdrop may fill the gap for some users.
Categories: FLOSS Project Planets

CodersLegacy: Running Headless Selenium in Python (2023)

Planet Python - Mon, 2023-05-15 11:13

In the world of web testing and automation, Selenium has become a popular tool for developers and testers alike. It allows us to simulate user interactions with web applications, automate repetitive tasks, and perform end-to-end testing. But have you ever wondered if there’s a way to run Selenium tests without a visible browser window? That’s where headless Selenium in Python comes into play.

In this article, we’ll explore the concept of running Selenium in a headless mode and its benefits. We’ll focus on using headless Selenium in Python, assuming that you already have a basic understanding of Selenium and have it installed. So, let’s dive in and uncover the wonders of headless Selenium testing!

Setting Up the Environment

Before we embark on our headless Selenium journey, let’s ensure our environment is properly set up. Here are the prerequisites you’ll need:

1. Python: Ensure that you have Python installed on your system. You can download the latest version from the official Python website.

2. Selenium: Install the Selenium library using pip, the Python package manager. Open your terminal or command prompt and run the following command:

pip install selenium

3. WebDriver: Selenium requires a WebDriver to interact with web browsers. The WebDriver acts as a bridge between Selenium and the browser. Depending on the browser you wish to use, you’ll need to download the appropriate WebDriver. You can read this introductory tutorial on selenium for instructions on how to set this up if you haven’t already.

Once you have Python, Selenium, and the WebDriver installed, we’re ready to configure headless options for our Selenium tests.

Changes in Selenium Headless Mode Configuration

In the world of Selenium, running automated scripts in headless mode has been a convenient way to execute tests without a visible browser window. However, there have been recent changes in the configuration of headless mode in Selenium 4.8.0 (Jan. 2023) that users need to be aware of.

Previously, Selenium provided a convenience method to set the headless mode while configuring browser options. This method allowed users to easily enable headless mode with a simple boolean parameter. However, starting from Selenium 4.8.0, this convenience method will be deprecated, and users will need to set the headless mode through arguments when configuring the browser options.

So, why is Selenium making this change?

It turns out that Chromium-based browsers now have two different headless modes available. The original headless mode, and a newer mode with enhanced capabilities introduced in 2022 (version 109). When users used the convenience method in Selenium to enable headless mode, it would utilize the original method provided by Chromium-based browsers.

This left users with no way of accessing the newer headless mode offered by Chrome, since the only options were to either “enable” or “disable” headless mode.

By deprecating and eventually removing the convenience method (scheduled for Selenium 4.10.0), Selenium is giving users full control to choose which headless mode they want to use. This allows users to leverage the newer headless mode, which offers enhanced browser functionality and the ability to run extensions.

We will be including both the old and new ways of configuring headless mode in the next section.

Headless Mode before Selenium 4.8.0

Here is how to configure headless mode in both the Chrome and Firefox browsers before selenium 4.8.0.

Chrome from selenium import webdriver options = webdriver.ChromeOptions() options.headless = True driver = webdriver.Chrome(options=options) driver.get('https://quotes.toscrape.com/') driver.quit()

If you have configured Selenium using the webdriver manager module (like we have), your code should look like this:

from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium import webdriver service = ChromeService(executable_path=ChromeDriverManager().install()) options = webdriver.ChromeOptions() options.headless = True driver = webdriver.Chrome(options=options, service=service) driver.get('https://quotes.toscrape.com/') driver.quit() Firefox from webdriver_manager.firefox import GeckoDriverManager from selenium.webdriver.firefox.service import Service as FirefoxService from selenium import webdriver service = FirefoxService(executable_path=GeckoDriverManager().install()) options = webdriver.FirefoxOptions() options.headless = True driver = webdriver.Firefox(options=options, service=service) driver.get('https://quotes.toscrape.com/') driver.quit() Headless Mode from Selenium 4.8.0 onwards

Here is how to configure selenium headless mode in both the Chrome and Firefox browsers from selenium 4.8.0 onwards in Python.

Chrome from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium import webdriver service = ChromeService(executable_path=ChromeDriverManager().install()) options = webdriver.ChromeOptions() options.add_argument('--headless=new') driver = webdriver.Chrome(options=options, service=service) driver.get('https://quotes.toscrape.com/') driver.quit()

If you want to use the old Chrome version (prior to 109) you can use '--headless'.

Firefox service = FirefoxService(executable_path=GeckoDriverManager().install()) options = webdriver.FirefoxOptions() options.add_argument('--headless') driver = webdriver.Firefox(options=options, service=service) driver.get('https://quotes.toscrape.com/') driver.quit()

This marks the end of the “Running Headless Selenium in Python” 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 Running Headless Selenium in Python (2023) appeared first on CodersLegacy.

Categories: FLOSS Project Planets

Real Python: Using and Creating Global Variables in Your Python Functions

Planet Python - Mon, 2023-05-15 10:00

A global variable is a variable that you can use from any part of a program, including within functions. Using global variables inside your Python functions can be tricky. You’ll need to differentiate between accessing and changing the values of the target global variable if you want your code to work correctly.

Global variables can play a fundamental role in many software projects because they enable data sharing across an entire program. However, you should use them judiciously to avoid issues.

In this tutorial, you’ll:

  • Understand global variables and how they work in Python
  • Access global variables within your Python functions directly
  • Modify and create global variables within functions using the global keyword
  • Access, create, and modify global variables within your functions with the globals() function
  • Explore strategies to avoid using global variables in Python code

To follow along with this tutorial, you should have a solid understanding of Python programming, including fundamental concepts such as variables, data types, scope, mutability, functions, and classes.

Free Download: Click here to get the sample code that will help you understand when and how to work with global variables in your Python functions.

Using Global Variables in Python Functions

Global variables are those that you can access and modify from anywhere in your code. In Python, you’ll typically define global variables at the module level. So, the containing module is their scope.

Note: You can also define global variables inside functions, as you’ll learn in the section Creating Global Variables Inside a Function.

Once you’ve defined a global variable, you can use it from within the module itself or from within other modules in your code. You can also use global variables in your functions. However, those cases can get a bit confusing because of differences between accessing and modifying global variables in functions.

To understand these differences, consider that Python can look for variables in four different scopes:

  • The local, or function-level, scope, which exists inside functions
  • The enclosing, or non-local, scope, which appears in nested functions
  • The global scope, which exists at the module level
  • The built-in scope, which is a special scope for Python’s built-in names

To illustrate, say that you’re inside an inner function. In that case, Python can look for names in all four scopes.

When you access a variable in that inner function, Python first looks inside that function. If the variable doesn’t exist there, then Python continues with the enclosing scope of the outer function. If the variable isn’t defined there either, then Python moves to the global and built-in scopes in that order. If Python finds the variable, then you get the value back. Otherwise, you get a NameError:

>>>>>> # Global scope >>> def outer_func(): ... # Non-local scope ... def inner_func(): ... # Local scope ... print(some_variable) ... inner_func() ... >>> outer_func() Traceback (most recent call last): ... NameError: name 'some_variable' is not defined >>> some_variable = "Hello from global scope!" >>> outer_func() Hello from global scope!

When you launch an interactive session, it starts off at the module level of global scope. In this example, you have outer_func(), which defines inner_func() as a nested function. From the perspective of this nested function, its own code block represents the local scope, while the outer_func() code block before the call to inner_func() represents the non-local scope.

If you call outer_func() without defining some_variable in either of your current scopes, then you get a NameError exception because the name isn’t defined.

If you define some_variable in the global scope and then call outer_func(), then you get Hello! on your screen. Internally, Python has searched the local, non-local, and global scopes to find some_variable and print its content. Note that you can define this variable in any of the three scopes, and Python will find it.

This search mechanism makes it possible to use global variables from inside functions. However, while taking advantage of this feature, you can face a few issues. For example, accessing a variable works, but directly modifying a variable doesn’t work:

>>>>>> number = 42 >>> def access_number(): ... return number ... >>> access_number() 42 >>> def modify_number(): ... number = 7 ... >>> modify_number() >>> number 42

The access_number() function works fine. It looks for number and finds it in the global scope. In contrast, modify_number() doesn’t work as expected. Why doesn’t this function update the value of your global variable, number? The problem is the scope of the variable. You can’t directly modify a variable from a high-level scope like global in a lower-level scope like local.

Internally, Python assumes that any name directly assigned within a function is local to that function. Therefore, the local name, number, shadows its global sibling.

In this sense, global variables behave as read-only names. You can access their values, but you can’t modify them.

Note: The discussion about modifying global variables inside functions revolves around assignment operations rather than in-place mutations of mutable objects. You’ll learn about the effects of mutability on global variables in the section Understanding How Mutability Affects Global Variables.

Getting an UnboundLocalError exception is another common issue when you try to modify a global variable inside a function. Consider the following demonstrative function that attempts to use some global variables:

Read the full article at https://realpython.com/python-use-global-variable-in-function/ »

[ 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

Pages