firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.28k stars 199 forks source link

Python code generator: Use fstrings and drop support for Python < v3.6 #1381

Open DougRzz opened 4 years ago

DougRzz commented 4 years ago

Use fstrings available in Python v3.6+ to improve readability

Change from

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))   

Change to

for matchNum, match in enumerate(matches, start=1):
    print (f"Match {matchNum} was found at {match.start()}-{match.end()}: {match.group()}")

    for groupNum in range(1,len(match.groups())+1):
        print (f"Group {groupNum} found at {match.start(groupNum)}-{match.end(groupNum)}: {match.group(groupNum)}")
working-name commented 4 years ago

Python 2.7 is still being used, I don't know if it's the right time to drop support now. However, I'm for optimizing or providing shorter code (as a comment maybe?) that's python 3.x compatible.

SuperSandro2000 commented 4 years ago

Python 2 is deprecated and you should have already switched years ago.

working-name commented 4 years ago

I don't use python.

Maybe swapping focus and having 3.x code by default and comments for 2.7 might be the way to go?

gtackett commented 3 years ago

I'm sure there is still a lot of Python 2.7 code floating around and still in use. Some of it is probably at sites with locked-down configurations that can't easily be upgraded to Python 3, or where doing so would require a mountain of administrative headache, extensive testing, etc., possibly involving legally binding contracts with customers.

There's also Jython 2.7,2 which is not deprecated but uses the same re module as Python 2.7.2

And Jython 2.7.2 is available as a scripting engine for Ghidra, a very popular tool these days in the reverse engineering (another kind of "re" 😄 ).

Does it make good sense to stop helping people in such situations, who at some time need to tweak a regex in such code?

firasdib commented 3 years ago

Great discussion!

This is a complex problem. The current engine implementation reflects that of python 2.7, but as mentioned, the majority of users should have upgraded to 3.x. In order to properly support 3.x I would have to re-write the engine though, which might warrant a new flavor altogether. This will make things a bit more complex to maintain, so I'm not entirely sure.

Does anyone have any statistics that might help make a decision on this?

gtackett commented 3 years ago

Does anyone have any statistics that might help make a decision on this?

I think that mere casually collected statistics—too small a sample size, a non representative sample, etc., are probably no better than isolated anecdotes would be, as a basis for decisions about what direction to take here.

Instead, a cost/benefit analysis for continuing to support Python 2.7 code generation may be preferable to statistics.

Support for generating Python 3 is certainly worth adding, because you want users to have support for current standards.

But preserving the ability to generate 2.7 syntax, for the sake of Jython, or for users who are stuck unavoidably with Python 2.7, is also arguably a good thing. If preserving that while adding 3.x code generation isn’t overly difficult, is anything really gained by ripping out 2.7 code generation?

SuperSandro2000 commented 3 years ago

I'm sure there is still a lot of Python 2.7 code floating around and still in use. Some of it is probably at sites with locked-down configurations that can't easily be upgraded to Python 3, or where doing so would require a mountain of administrative headache, extensive testing, etc., possibly involving legally binding contracts with customers.

And I am pretty sure those kinds of people are allowed to paste their complex business related regex's in some unaudited random website on the internet.

If no one drops python 2 support than it will never die. NixOS almost completely removed in there next release and other distros will follow that in the next years.

firasdib commented 3 years ago

I think that mere casually collected statistics—too small a sample size, a non representative sample, etc., are probably no better than isolated anecdotes would be, as a basis for decisions about what direction to take here.

I was thinking more in general terms, i.e., how many still download 2.7, how many on the SO surveys that use it, etc. Just to help guide us here.

The broader issue is that the website is tailored for 2.7, that includes the quickref, the engine, etc. If we start adding 3.x support, I want to know to which degree it needs to be done.

b-morgan commented 3 years ago

I believe Python 3 support is needed here because official Python 2 support has ended. If it is possible, my suggestion would be to add Python 3 as a separate flavor (or some other switch) so that eventually, the Python 2 stuff on this site can be deprecated.

Another (open source) project I use, OctoPrint, is going through the Python 2 to Python 3 migration. You might want to contact Gina (if you can't find an email address, use the Discord server or the forums) to see if she has any guidance to share.

gtackett commented 3 years ago

I believe Python 3 support is needed here because official Python 2 support has ended. If it is possible, my suggestion would be to add Python 3 as a separate flavor (or some other switch) so that eventually, the Python 2 stuff on this site can be deprecated.

Support for generating code in Python 3 is certainly needed.

I think "deprecating" generation of Python 2.7 might be better than completely removing the ability to generate Python 2.7. I think it can be argued that there are reasons not to completely remove the latter ability, since as I've noted previously in this same issue, there are several reasons a user of regex101 might need to generate Python 2.7 code.

@firasdib:

Everyone:

Weighing the answers to those two questions is what I meant when I referred to "cost/benefit analysis" in my earlier comments.

evgfilim1 commented 3 years ago

What about making generated code work on Python 3, but with Python 2 compatibility?

https://python-future.org/compatible_idioms.html

rootsmusic commented 11 months ago

Versions older than 3.8 have reached end of life. Instead of dropping support for older versions, issue #1219 proposes an alternative.