What is the difference between Python's unittest and unittest2 modules? - python-3.x

I currently work on some code that uses unittest2 module. I suspect this code was meant for the python2. Can one use python3 unittest as a drop in replacement for unittest2? What is the difference between the two?

According to the Python 2.7 unittest docs:
unittest2: A backport of new unittest features for Python 2.4-2.6 Many
new features were added to unittest in Python 2.7, including test
discovery. unittest2 allows you to use these features with earlier
versions of Python.
So moving from unittest2 under Python 2 to unittest under Python 2.7 or Python 3 should do exactly what you want

Related

Can I transpile python 3.7 code to a lower version?

In a python-based project, I would like to use features data classes and libraries such as the current RxPY version.
However, we are bound to an environment (a Yocto-system) that only has Python 3.5.5
From TypeScript/JavaScript I know that some languages allow code to be transpiled to a lower language version.
Can Python 3.7 code be transpiled to a lower version?
Many thanks
You can't just use a tool to convert Python code to earlier versions, because those versions would sometime lack the libraries that you need. Using Python 3.7's dataclass is a good example.
A workaround for you would be to just install a port of dataclasses back in your 3.5 environment. This would be tricky. If you had python 3.6 you could just install this through
pip install dataclasses
But you can see in this issue that they never ported it back for python 3.5. If you check out their GitHub, maybe there are manual changes you could do to the source code to make it work. Maybe.
The easier way would be to find an alternative to dataclasses

Is it possible to cythonize some of the built-in Python Packages?

Note: I know how to cythonize my own Python modules.
So, I have managed to cythonize some of my Python modules, and I've gained some speed. The question is, since cython:
The goal of cython is to be compatible with python, i.e. that you can cythonize any python code and it will work as before. Currently, a large fraction of python code already works. Cython furthermore allows you to optimize parts of your code and compile it into more efficient C code.
Source: What does Cython do with imports?
Now, the biggest issue in my code evolve around re.py, the built-in Python Regular Expression module, despite the fact, that I am per-compiling my regexes.

What is the real current status of Twisted on Python3?

Had used some twisted using Python 2.6 few years back, and since then stopped using Python. Recently starting picking up on Python 3, and was checking status of twisted support for Python 3 which was very thin back when I left.
Introduction section of the Latest document on the topic says this:
Twisted is currently being ported to work with Python 3.4+. This
document covers Twisted-specific issues in porting your code to Python
3.
Most, but not all, of Twisted has been ported, and therefore only a
subset of modules are installed under Python 3. You can see the
remaining modules that need to be ported at
twisted.python._setup.notPortedModules, if it is not listed there,
then most of all of that module will be ported.
And clicking on the twisted.python._setup.notPortedModules shows no module listed.
Does that mean that twisted is now fully supported on Python 3 ? Or just the list is incorrect ? If so, are the samples / examples converted for Python 3 ?
The most likely definitive resource on this topic is the continuous integration system.
According to https://travis-ci.org/twisted/twisted, a recent run had 9957 passing tests on Python 3.6 compared to 9933 passing tests on Python 2.7. There is some slop in these numbers because the test suite includes some tests which are only relevant to Python 2.x and others which are only relevant to Python 3.x (therefore we would not expect exactly the same number of tests to run on each runtime) however these numbers are so close that I would say that Twisted has basically been complete ported to Python 3.6. Problems that remain are probably more likely to be "regular bugs" rather than unported code.

Should Python 3.4 programs use __slots__ in classes?

The __slots__ attribute for classes was made with Python 2 or earlier, and according to comment for answer to Python __slots__ it appears that Python 3.3 has improved so the advantage on memory size may not be the reason for using __slots__ in for example Python 3.4 programs.
So should I clean up my code and remove the use of __slots__ in classes, or is there still some good reason for using __slots__ even in Python 3.4 programs?
By using __slots__ you are telling Python not to have a __dict__ attribute on your instances. This is to save memory in cases where you may have thousands upon thousands of instances. This is the reason to use them in Python 2, and it is the reason to use them in Python 3.

Python 3 and PyQt 4 recommendations

Is the combination of Python 3 and PyQt 4 recommended? Are there any alternatives?
I don't see why not, there is a version available for Python 3 which works normally, and the only alternative if you really need Qt would be PySide, which is far from being compatible with Python 3.
Other GUI alternatives would be wxPython (not in Python 3 yet AFAIK) and the "native" Tkinter (which is something else...).
If PyQt4 is the only non-native module you need, there should be no problem.
Check if all modules you need are available for Py3k!
PyQt4 for Py3k is not yet integrated into all distributions.
I.e. on Debian PyQt4 only works with Python 2 currently.
Have a look at 3to2! A tool to convert Py3 to Py2 code.
That is just better than coding in Py2 and using 2to3.

Resources