Month: September 2020

Python Application Translation With Qt Linguist

The translation of an application is usually a fastidious and painful process. Hopefully, Qt provides excellent support for translating into local language applications developed in C++ or Python.  Qt lets you load language translation files into an application at run-time. This means that application texts (i.e., menus, screen titles, field labels, help files, etc.) are …

Python Application Translation With Qt Linguist Read More »

Calculating Entropy with SciPy

Problem: How to calculate the entropy with the SciPy library? Solution: Import the entropy() function from the scipy.stats module and pass the probability and the base of the logarithm into it. Try It Yourself: Run this code in the interactive code shell! Exercise: Change the probabilities. How does the entropy change? Let’s start slowly! You’ll …

Calculating Entropy with SciPy Read More »

Regex to Match Dollar Amounts with Optional Cents

Problem: How to match dollar amounts in a given string? If cent amounts are given, those should match as well. Solution: You can use regular expression pattern ‘(\$[0-9]+(.[0-9]+)?)’ to find all numbers that start with a dollar symbol, followed by an arbitrary number of numerical digits, followed by an optional decimal point and an arbitrary …

Regex to Match Dollar Amounts with Optional Cents Read More »

Python Global in One Line

To update a global variable in one line of Python, retrieve the global variable dictionary with the globals() function, and access the variable by passing the variable name as a string key such as globals()[‘variable’]. Then overwrite the global variable using the equal symbol, for example in globals()[‘variable’] = 42 to overwrite the variable with …

Python Global in One Line Read More »

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with …

How To Split A String And Keep The Separators? Read More »

Python Webscraper Regex [Free Book Chapter Tutorial]

This tutorial is a chapter excerpt drafted for my new book “Python One-Liners” (to appear in 2020, No Starch Press, San Francisco). Are you an office worker, student, software developer, manager, blogger, researcher, author, copywriter, teacher, or self-employed freelancer? Most likely, you are spending many hours in front of your computer, day after day. In …

Python Webscraper Regex [Free Book Chapter Tutorial] Read More »

How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”

If you run the following code, you’ll experience a special ValueError: The output will be this error message: Try It Yourself in our interactive browser shell: Solution: Using the Numpy functions called logical_and() and logical_or() instead of Python’s logical operators (“and” and “or”) is the solution. Master the basics and join the “Python Built-in Functions” …

How to Fix “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()” Read More »

Python Comments — 2-Minute Guide with Exercise

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term! There are two types of comments: one-line comments and multi-line …

Python Comments — 2-Minute Guide with Exercise Read More »

Python One Line Class

Do you hate those lengthy class definitions with __init__ and too many whitespaces and newlines? Python One-Liners to the rescue! Luckily, you can create classes in a single line—and it can even be Pythonic to do so! Sounds too good to be true? Let’s dive right into it! Problem: How to create a Python class …

Python One Line Class Read More »

How To Extract Numbers From A String In Python?

Summary: To extract numbers from a given string in Python you can use one of the following methods: Use the regex module. Use split() and append() functions on a list. Use a List Comprehension with isdigit() and split() functions. Use the num_from_string module. Extracting digits or numbers from a given string might come up in …

How To Extract Numbers From A String In Python? Read More »