Programming languages

How to fix Python enum ‘TypeError: Error when calling the metaclass bases: module.__init__() takes at most 2 arguments (3 given)’

Problem:

You want to inherit a Python class from enum like this:

import enum

class MyEnum(enum):
    X = 1
    Y = 2class

but when you try to run it, you see an error message like this:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    class MyEnum(enum):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

Solution:

You are not trying to inherit from the Enum class (capital E!) but from the enum module!

This is the correct syntax:

from enum import Enum
class MyEnum(Enum):
    X = 1
    Y = 2

 

Posted by Uli Köhler in Python

How to fix Python class ‘NameError: name ‘enum’ is not defined’

Problem:

You are trying to inherit a class from enum in Python:

class MyEnum(enum):
    X = 1
    Y = 2

But when you try to run it, you see this error message:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-ebcfa41a8a7c> in <module>
----> 1 class MyEnum(enum):
      2     X = 1
      3     Y = 2

NameError: name 'enum' is not defined

Solution:

You need to inherit from Enum (capital E!), not from enum! The correct syntax is

from enum import Enum

class MyEnum(Enum):
    X = 1
    Y = 2

 

Posted by Uli Köhler in Python

How to fix Python ‘ImportError: No module named enum’

Problem:

You have code like this in Python:

from enum import Enum

But when you try to run it, you encounter this error:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from enum import Enum
ImportError: No module named enum

Solution:

The enum module is only available in Python 3! You are trying to use it in Python 2.

Try running your code with Python3 (e.g. python3 myscript.py). In case that’s not possible since your project or a library is not compatible with Python 3, you can install enum34 which provides the enum package for Python 2:

sudo pip install enum34

 

Posted by Uli Köhler in Python

How to fix Python ImportError: cannot import name ‘enum’

Problem:

You have a line like this in your Python code:

from enum import Enum

But when you try to run it, you see this error message:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from enum import Enum
ImportError: No module named enum

Solution:

The enum module is only available in Python 3, but you are using Python 2!

You can try to run your script using Python 3. In case that’s not possible (because your project or a library is not compatible with Python 3), you can install the enum34 backport

sudo pip install enum34

After installing this, the ImportError should disappear.

Posted by Uli Köhler in Python

How to create temporary directory in Python

Note: This example shows how to create a temporary directory that is not automatically deleted. Check out How to create a self-deleting temporary directory in Python for an example on how to create a self-deleting temporary directory!

Minimal example:

import tempfile
tempdir = tempfile.mkdtemp()
print(tempdir) # prints e.g. /tmp/tmpvw0936nd

tempfile.mkdtemp() will automatically create that directory. The directory will not automatically be deleted!

Custom prefix (recommended):

import tempfile
tempdir = tempfile.mkdtemp(prefix="myapplication-")
print(tempdir) # prints e.g. /tmp/myapplication-ztcy6s2w

How to delete the directory

In order to delete the temporary directory including all the files in that directory, use

import shutil
shutil.rmtree(tempdir)
Posted by Uli Köhler in Python

How to disable InsecureRequestWarning: Unverified HTTPS request is being made.

If you use requests or urllib3, requests with SSL verification disabled will print this warning:

/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)

If you want to disable this warning and you can’t just enable verification, add this code

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

at the top of your Python file.

Posted by Uli Köhler in Python

Fixing Octave string compare

Problem:

You want to compare two strings in Octave like myString == "foobar" but you see an error message like

error: myscript.m: mx_el_eq: nonconformant arguments (op1 is 1x25, op2 is 1x6)
error: called from
    myscript.m at line 26 column 1

Solution:

You can’t compare strings using == in Octave!

Use strcmp like this: Instead of myString == "foobar" use

strcmp(myString, "foobar") == 1

 

Posted by Uli Köhler in Octave

Python subprocess.check_output(): Set working directory

If you have code that uses subprocess.check_output() to call a command like

subprocess.check_output("ls .", shell=True)

you can use the cwd=... argument of subprocess.check_output() to define the working directory. Example:

subprocess.check_output("ls .", cwd="../", shell=True)

cwd means change working directory and is interpreted relative to the current working directory. However, you can also use absolute paths:

subprocess.check_output("ls .", cwd="/etc/", shell=True)

 

Posted by Uli Köhler in Python

Python bottle minimal redirect example

To redirect in bottle, use this snippet:

response.status = 303
response.set_header('Location', 'https://techoverflow.net')

303 is the HTTP response code See Other. In certain applications you might want to use 301 (permanent redirect) or 307 (temporary redirect) instead.

Full example:

#!/usr/bin/env python3
from bottle import route, run, response

@route('/')
def redirect():
    response.status = 303
    response.set_header('Location', 'https://techoverflow.net')

run(host='localhost', port=9000)

 

Posted by Uli Köhler in Python

Python bottle minimal example

Minimal hello world-like example using the bottle HTTP server:

#!/usr/bin/env python3
from bottle import route, run

@route('/')
def index():
    return "Hello bottle!"

run(host='localhost', port=9000)

Run this file (and keep it running) and then navigate to http://localhost:9000 to see the Hello bottle! message.

In case you see this error message:

Traceback (most recent call last):
  File "./server.py", line 2, in <module>
    from bottle import route, run
ModuleNotFoundError: No module named 'bottle'

you need to install bottle using

sudo pip3 install bottle

or

sudo pip install bottle

depending on which Python version and configuration you use.

Posted by Uli Köhler in Python

Octave: Use ‘qt’ graphics_toolkit() if available, ‘gnuplot’ else

This code uses the "qt" graphics toolkit only if available and defaults to using "gnuplot" else:

gts = available_graphics_toolkits()
qt_available = any(strcmp(gts, 'qt'))
if qt_available
  graphics_toolkit("qt")
else
  graphics_toolkit("gnuplot")
endif

See Octave: Check if cell array of strings contains string for details on how to check if a string like "qt" is in a cell array of strings like the one returned by available_graphics_toolkits().

Posted by Uli Köhler in Octave

Octave: Check if cell array of strings contains string

If you have a cell array of strings in Octave like this:

arr = {"a", "bc", "def"}

you can check if it contains a specific string like "a" using

contains_a = any(strcmp(arr, 'a'))

Example:

arr = {"a", "bc", "def"}

contains_a = any(strcmp(arr, 'a'))
contains_x = any(strcmp(arr, 'x'))

disp(contains_a) # 1
disp(contains_x) # 0

 

Posted by Uli Köhler in Octave

Octave equivalent to Python’s print

In Python you can use print:

print("foobar")

whereas in Octave you can use disp:

disp("foobar")

You can print numbers using print in Python:

x = 1
print(x)

and also using disp in Octave:

x = 1
disp(x)

Note that you can use multiple argument to print in Python:

x = 1
print("The value of x is", x)

whereas in Octave you need to use disp together with cstrcat:

disp(cstrcat("The value of x is ", num2str(x)))

 

Posted by Uli Köhler in Octave

Computing distance between gp_Pnt and gp_Ax1 in OpenCASCADE

OCCUtils provides convenience functions for computing the orthogonal direction to two directions:

#include <occutils/Axis.hxx>
using namespace OCCUtils;

gp_Ax1 axis = /* ... */;
gp_Pnt pnt = /* ... */;
double distance = Axis::Distance(axis, pnt);

Alternatively, you can also use Point::Distance() which internally just calls Axis::Distance() but might make your code more readable under some circumstances. Note that the argument order is inverted!

#include <occutils/Point.hxx>
using namespace OCCUtils;

gp_Ax1 axis = /* ... */;
gp_Pnt pnt = /* ... */;
double distance = Point::Distance(pnt, axis);

In case you can’t use OCCUtils, here’s the code to do it manually:

double distance = gp_Lin(axis).Distance(pnt);
Posted by Uli Köhler in C/C++, OpenCASCADE

How to check if gp_Ax1 contains gp_Pnt in OpenCASCADE

OCCUtils provides convenience functions for computing the orthogonal direction to two directions:

#include <occutils/Axis.hxx>
using namespace OCCUtils;

gp_Ax1 axis = /* ... */;
gp_Png point = /* ... */;
bool pointIsOnAxis = Axis::Contains(axis, point);

In case you can’t use OCCUtils, here’s the code to do it manually:

gp_Lin(axis).Contains(pnt, Precision::Confusion());
Posted by Uli Köhler in C/C++, OpenCASCADE

How to get gp_Dir orthogonal to two gp_Dirs in OpenCASCADE

OCCUtils provides convenience functions for computing the orthogonal direction to two directions:

#include <occutils/Direction.hxx>
using namespace OCCUtils;

gp_Dir dir1 = /* ... */;
gp_Dir dir2 = /* ... */;
gp_Dir orthogonalDirection = Direction::Orthogonal(dir1, dir2);

In case you can’t use OCCUtils, here’s the code to do it manually:

gp_Dir orthogonalDirection = dir1.Crossed(dir2);

The function used is called Crossed() since the mathematical operation being used is the cross product between the two direction vectors.

 

Posted by Uli Köhler in C/C++, OpenCASCADE

How to get midpoint/center between points in OpenCASCADE

OCCUtils provides convenience functions for getting the midpoint (also called the center) between 2 or more points:

#include <occutils/Point.hxx>
using namespace OCCUtils;

gp_Pnt p1 = /* ... */;
gp_Pnt p2 = /* ... */;
gp_Pnt midpointOfP1AndP2 = Point::Midpoint({p1, p2});

You can also call Point::Midpoint() with a std::vector<gp_Pnt>.

In case you can’t use OCCUtils, here’s the code to do it manually:

double x = 0.0, y = 0.0, z = 0.0;
for (const gp_Pnt &pnt : points) {
    x += pnt.X();
    y += pnt.Y();
    z += pnt.Z();
}
size_t size = points.size();
gp_Pnt midpoint(x / size, y / size, z / size);

 

 

Posted by Uli Köhler in C/C++, OpenCASCADE

How to compute volume of TopoDS_Shape / TopoDS_Solid in OpenCASCADE

OCCUtils provides a convenience function to do this:

#include <occutils/Shape.hxx>

using namespace OCCUtils;

TopoDS_Shape myShape = /* ... */;

double volume = Shape::Volume(myShape);

In case you need to do this without OCCUtils, use

GProp_GProps gprops;
BRepGProp::VolumeProperties(shape, gprops);
double volume = gprops.Mass();

 

Posted by Uli Köhler in C/C++, OpenCASCADE

Towards a docker-based build of C/C++ applications

Note: Based on this post I have published buildock on GitHub.

Many C/C++ programmers and project managers know the pain of creating a reproducible build environment for all developers: Works for me is a common meme not without a reason.

My approach is to dockerize not neccessarily the application itself but the build system, encapsulating both the specific compiler version and the system around it plus all required system-level libraries in a Docker image.

Take the obligatory Hello World in C++:

// main.cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

and the corresponding Makefile:

all:
        g++ -o helloworld main.cpp

How can we compile this simple project without installing a compiler and GNU make on the local computer (no cheating by using build servers allowed)?

Actually it’s rather simple:

docker run --user $(id -u):$(id -g) -v $PWD:/app -it ulikoehler/ubuntu-gcc-make make

Breaking it down:

  • docker run: Create a new docker container and run a command in it
  • --user $(id -u):$(id -g): This makes the docker container run with the current user’s ID and the current user’s group – both for preventing the compiler to create output files as root and to safeguard against some IT security risks. Also see How to run docker container as current user & group
  • -v $PWD:/app: Mount the current directory ($PWD) on /app in the container. Since the Dockerfile used to build the container contains the WORKDIR /app directive, whatever command we run in the container will by default be executed in the /app directory – and therefore in the current local directory on the host.
  • -it runs the container in interactive mode, i.e. keypressed are passed down to the command you are running. Also, this means that our command will only finish when the container has finished executing.
  • ulikoehler/ubuntu-gcc-make: This is the image we’re using for that example. It’s nothing more than an ubuntu:18.04 base image with build-essentials and make installed and WORKDIR set to /app
  • make: This is the command we’ll run in the container. You can use any command here, even no command is possible (in which case the container’s default command will be used – in case of ulikoehler/ubuntu-gcc-make that is CMD [ "/usr/bin/make" ])

Here’s the complete Dockerfile used to generate ulikoehler/ubuntu-gcc-make:

FROM ubuntu:18.04
RUN apt update && apt -y install build-essential make && rm -rf /var/lib/apt/lists/*
WORKDIR /app
CMD [ "/usr/bin/make" ]
Posted by Uli Köhler in Build systems, C/C++, Container, Docker

How to compute surface normal in OpenCASCADE

OCCUtils provides a convenient utility to compute the Normal of a surface (represented by a GeomAdaptor_Surface) in OpenCASCADE:

#include <occutils/Surface.hxx>

using namespace OCCUtils;

GeomAdaptor_Surface surf = /* ... */;

gp_Ax1 surfaceNormal = Surface::Normal(surf);

// ...or just get the direction
gp_Dir surfaceDirection = Surface::NormalDirection(surf);

This function computes the normal vector at specific U/V coordinates which default to (0,0). You can also give custom U/V coordinates:

gp_Ax1 normal = Surface::Normal(surf, 1.0 /* u */, -4.5 /* v */);

In case you can’t use OCCUtils and you need to do it manually, here’s how you can do it:

#include <GeomLProp_SLProps.hxx>

GeomLProp_SLProps props(surf.Surface(), u, v, 1 /* max 1 derivation */, precision);
gp_Ax1 axis(props.Value(), props.Normal());

props.Value() returns the point on the surface at the given U/V coordinates.

Posted by Uli Köhler in C/C++, OpenCASCADE