A blog for computing noob

(The Noob Being The Author of This Blog)

Using QT with Python for developing GUI

QT is a popular toolkit to develop GUIs. I was curious to see how it works with Python. Here are the basics:

From doc.qt.io: “Qt for Python offers Python bindings for Qt, enabling the use of Qt5 APIs in Python applications. It lets Python developers utilize the full potential of Qt, using the PySide2 module.”

Installation: https://doc.qt.io/qtforpython/gettingstarted.html

We need: Python 3.5+ and Clang 6.0+. The above step requires that you install clang. To do this, follow these step:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-7 main"
sudo apt-get update
sudo apt-get install -y clang-7

In the above, I am installing clang 7 on Ubuntu 16.04. For other versions of clang or Ubuntu, substitute the link in line 2 with the right one from: https://apt.llvm.org

Now, we do:

sudo pip install PySide2

And the install should complete. To test,

from PySide2 import QtCore

def printQtCoreVersion(QtCoreHandle):
	ver = QtCoreHandle.__version_info__
	print("QtCore Version = %d.%d.%d"%ver)

printQtCoreVersion(QtCore)

Should output (Depending on your version):
QtCoreVersion = 5.12.2

How is the output of ‘ps aux’ formatted?

Like so:

user@machine:~$ ps aux
USER PID %CPU  %MEM    VSZ    RSS  TTY  STAT  START  TIME  COMMAND
root      1       0.4        0.0     120148   6180    ?      Ss       11:46   0:06    /sbin/init

The following packages have unmet dependencies: E: Unable to correct problems, you have held broken packages.

I got a brand new Ubuntu 16.04 LTS. While installing a few packages such as Gimp or gparted, I came across errors such as:

user@machine:~$ sudo apt-get install gparted
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 gparted : Depends: libparted-fs-resize0 (>= 3.1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I tried many suggestions, one that worked was this one (I had no hopes, but this was it):
https://ubuntu-mate.community/t/problem-with-installing-gparted/16291/4

I am including a screenshot of the post in case this post is deleted or archived (Please credit the original author for the solution, not me):
Screenshot from 2019-03-10 17-03-10

Module import error after installing inside a virtual environment using pip

For a number of reasons, you might sometimes encounter a problem with importing modules that you installed within a virtual environment. Typically, after you activate a virtual environment (MLenv below), you can type the following

(MLenv) user@machine:~$ sudo pip3 install numpy

to install numpy. A similar command would install other modules. After the installation, you can type python3 in bash terminal and activate the python interpreter. Within the interpreter, you can type import numpy as below:

user@machine:~$ python3.5
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named 'numpy'

But an error appears.

I am not sure why this happens, when I activated the ML environment, I checked which pip3 and it returned /home/user/MLenv/bin/pip3, this is the correct location. Even then, the pip3 install command doesn’t put the installation in the right place, which should be :/home/user/MLenv/lib/python3.5/site-packages/

Although, upon trying:

(MLenv) user@machine:~$ sudo /home/user/MLenv/bin/pip3 install numpy

I was able to successfully install numpy in the correct directory and import it. So until I figure out the exact problem, I might continue using this fix.

Type check for various floats types in Python

See the snippet below, to evaluate whether the given input is any kind of float (float32, float64 etc).

import numpy as np
a = np.float32(2.1)
b = np.float64(2.1)
a.dtype == b.dtype
False
a.dtype.kind == b.dtype.kind
True
a.dtype.kind == 'f'
True

Note that Python’s inbuilt float is the same type as np.float64, but python’s inbuilt float doesn’t support ‘dtype’ lookups.

Open a program in terminal without holding it up for the running program

Thanks to this post: https://askubuntu.com/questions/429969/how-to-run-a-program-from-terminal-which-allows-to-close-terminal-after-the-pro

The method below works well. See link for explanation.

nohup (command) &> /dev/null & 

Interesting Python Snippets

  1. Typecheck an input to a function:
# In the line below, we are asserting that arg1 be
# either int or tuple
assert isinstance(arg1, (int, tuple))

 

Embed code with formatting using HTML

Go to this website: http://hilite.me/

Type in the code on the left, click “Highlight” button, copy HTML from the right, paste it in your webpage or wordpress blog. That’s it!

Python Special Methods – Write a clean Class

A note to self on how to write a clean Python class with special functions (__func__ ones). The double underscore is called ‘dunder’.

Someone else has done an excellent job already on the topic so I’m just linking to them:

https://rszalski.github.io/magicmethods/

Here is a PDF made generously available by the author of the above linked post: magicmethods

Also see this: https://dbader.org/blog/python-dunder-methods

Visual Studio Code Python Debugger

VS Code documentation here: https://code.visualstudio.com/docs/python/debugging

If you are having issues with VS code debugger not stopping at a breakpoint check these:

  1. I set a breakpoint to “import torch” line on my code, the debugger didn’t stop. Removing this breakpoint, and setting the breakpoint elsewhere seems to work. Perhaps not set a breakpoint at import lines?
  2. Setting a breakpoint at an empty line in the code also causes the execution to fall through to the end. Set a breakpoint at a valid line where the code is doing something.

In the figure below, the first two breakpoints will cause erroneous behavior but in the absence of the first two breakpoints, the debugger should stop at the third one.

Breakpoints in VS Code

 

Create your own website, free on WordPress.com.Create your own website.
Start free