way to infer python version requirement? - python-3.x

When publishing a package, I need to tell users which version of python they require. I've been developing against Python 3.6, but I don't know how far backwards compatible my code is (or my dependencies).
Is there a static analysis tool / version guide to help me figure this out?
I'm sure this is impossible to do with regard to 3rd party dependencies, but is there a static analysis tool that might help? This seems like the sort of thing pylint could help with, but it doesn't appear to.
This answer seems to imply I might set up, for example, a conda environment for each sub version of python 3, install and run pylint from each. Maybe that's the best I can do?

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

Should I use Anaconda on Ubuntu WSL for other reasons than datascience?

I've been away from Python for a while (just a normal guy trying to learn it) and wanted to start learning again. I came across Anaconda and I am trying to figure out whether to use it or stick to pip. There are several questions for this, but they are all related to people using Python for Datascience, which I use R for.
So, is there any reason for me to use Anaconda if I am not planning on working with Datascience?
Sorry if this question seems easy to answer by searching, but I cannot find any information about this that isn't related to Datascience.
Off-hand, I'd say no. Anaconda simply clumps many (not all) of the standard data science-y packages with Python. If you go base Python, you get a much smaller install. Now one thing that is useful: conda. I find it's pretty good at managing package versions, which can be pretty problematic otherwise. But you can probably get by without it.

Is there a way to use the deap library in grasshopper

Is there a way I can use the deap library inside grasshopper's Python node
I want to run a genetic algorithm but the fitness function is to be calculated by grasshopper (only the fitness function, all the other things are to be taken of by deap inside the python node)
can it be done?
I am having problem with
importing the deap library in grasshopper's Python interface(I think I will be able to solve it by copying the files manually from Python path)
(major problem) grashopper doesn't allow closed loops so I cant seem to find a way to feed the fitness back into the Python node with the main code
couldnt get it to work, had to make do with the grasshopper pluggins
the problem was that you can only install iron python libraries for grasshopper
These are two well known issues with 'out-of-the-box' grasshopper but there are several plugins that can help overcome them.
Question One
The basic GHPython component uses Iron Python and can limit which libraries are compatible and able to be used. To get around this constraint there is a plugin called 'GH_CPython'. It allows you to set a locally installed python interpreter for your code, and then have access to any libraries available to that local interpreter. So if you install deap Libary locally then it will be available within the grasshopper GH_Cpython editor. Here is a link to download and install GH_CPython: https://www.food4rhino.com/en/app/ghcpython
Question Two
As you noted, Grasshopper is procedural and has limited support for recursive routines. To get around this there are several plugins that support recursion and may be able to help with your implementation. Which plugin would be best for your situation is difficult to say without a deeper description of your goals. Here are several options, each option provides recursive functionality that would allow for 'closed loops' where results of a script can be fed back as input.
Hoopsnake - very basic and has been around the longest
Anemone - A little more flexible and uses multiple components for loop start and end for cleaner-looking scripts. It also has a 'record history' functionality.
Octopus - Has a 'Loop' component that is similar to Hoopsnake. It also has a 'record history' functionality.

Writing a python script which asks to upgrade version

Is there any way to write a script and run it which asks the version of python being currently used, and if it's 2, install python3.
Also is there any way to install pip using script in the similar way.
New to python, sorry if there is any mistake.
In general, the answer is no. First of all, you would have to answer hard questions like whether it is acceptable to install a new runtime on the user's computer, and whether they would appreciate that.
One option is to simply bundle the Python interpreter you need in your installer. Never rely on the user's installed Python, unless perhaps they ask you to. Another option is to make your programs compatible with both Python 2 and Python 3--for some programs this is not a ton of work.

How to Be Python 3 Ready?

What are the current rules for writing python code that will pass cleanly through 2to3 and what are the practices that seem to be best suited to writing code that will not become mired forever in version 2.
I have read from the SciPy/NumPy forums that "100% test coverage" (unit testing) is important for many people, and I am not sure if that would apply to everybody. Certainly having a reasonable set of unit tests to try your code out with after conversion, seems a sane step.
Are there other things? What are skilled Pythonistas doing if they are writing 2.x code that they hope to have come through "cleanly" in the 2to3 process.
I am looking for specific instances of "[don't] do this" as well as some more general "best-practices", but specific instances of "do's and don'ts" are helpful.
Let's assume that frameworks, libraries (Django, SciPy/NumPy), and every other C Extension we need gets ported to Python3 eventually, and I'm asking about how you write and maintain the pure python language code that you write yourself.
Update: It's possible that what I really want is the "style guide" and list of deprecated features that everybody was already staying away from. I cut my teeth on Python 1.5 and moved to 2.0, and then have not really followed much of the 2.5/2.6 era, used them but really my code is more 2.1 era.
I'd say:
Read the "What's new for Python 3.0". Very informative.
In particular, if you care about Unicode or text encodings at all, take the time to understand what has changed for 3.x. That's probably one of the trickier things to change for Python 3.x.
Get Python 2.6 or 2.7, and run your code with the -3 flag. It will tell you about things in your code that will need changing.
Before using 3rd-party packages, check to see if they have a Python 3.x version. If not, check the package web site, mailing lists, version control repositories etc to see how actively the package is being developed and whether there is a roadmap towards Python 3.x support.
Download Python 3.x and try it out! Admittedly, that might not be practical if you care about code that currently depends on packages that don't yet support Python 3.x (e.g. wxPython or Django).

Resources