Year: 2020

Python Import Error (ModuleNotFoundError)

Python’s ImportError (ModuleNotFoundError) indicates that you tried to import a module that Python doesn’t find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH. If this file (__init__.py) is in the folder, change the position of the import in the file that is …

Python Import Error (ModuleNotFoundError) Read More »

Python Unicode Encode Error

Summary: The UnicodeEncodeError generally occurs while encoding a Unicode string into a certain coding. Only a limited number of Unicode characters are mapped to strings. Thus, any character that is not-represented / mapped will cause the encoding to fail and raise UnicodeEncodeError. To avoid this error use the encode(utf-8) and decode(utf-8) functions accordingly in your …

Python Unicode Encode Error Read More »

Python Dictionary Comprehension: A Powerful One-Liner Tutorial

Dictionary Comprehension is a concise and memory-efficient way to create and initialize dictionaries in one line of Python code. It consists of two parts: expression and context. The expression defines how to map keys to values. The context loops over an iterable using a single-line for loop and defines which (key,value) pairs to include in …

Python Dictionary Comprehension: A Powerful One-Liner Tutorial Read More »

[Dash + Flask] How to Deploy a Python Dash App on Pythonanywhere.com

Here’s the step-by-step approach of how to deploy your Dash app on Pythonanywhere.com using Flask and pip: Create an account on Pythonanywhere.com. Create a Flask application. Create a Dash application. Copy the Dash app into the Flask app. Connect the Flask server with the Dash app. Modify the WSGI configuration file. Install Dash with pip …

[Dash + Flask] How to Deploy a Python Dash App on Pythonanywhere.com Read More »

Python One-Line Password Generator

Can you believe it? People use unknown and potentially insecure websites to generate their random passwords! This works as follows: A website generates a “random” password for them and they copy&paste it and assume this is a safe password because of the randomness of the characters. What a security flaw! Why? Because the website could …

Python One-Line Password Generator Read More »

How to Solve Python “TypeError: ‘int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: ‘int’ object is not iterable”? Read More »

String Formatting Comparison: format() | Percent | f-string

Summary: f-string is more readable and easier to implement than % and .format() string formatting styles. Furthermore, using f-strings is suggested for Python 3.6 and above while .format() is best suited for Python 2.6 and above. Versions prior to Python 2.6 only provide % option for string formatting. In terms of speed, % is the …

String Formatting Comparison: format() | Percent | f-string Read More »

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 »