in Education by
Our core application changed from Python 2.6 to Python 2.7 maybe to Python 3 in later release. Range function from python is changed (quote from the Python 2.7 changelog). The range() function processes its arguments more consistently; it will now call __int__() on non-float. We allow users to add expressions / Python code based on results we process further. Now how to change range function? Some of them are using float argument that are failing now in Python 2.7. As code is written by users we cannot change in Python code/expression. There are 1000s of files. Some users may have their own files. Is there is a way to extend the range() function from Python, such that it will take float arguments? Another alternative is to parse python code and change float to int. It is very time consuming as it requires lot of sting manipulation and some range calls have formulas as parameter. Our application build in C++, and we evaluate python expression using C++ python APIS JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You have a serious migration issue. If you want to switch to Python 2.7 and even Python 3, there is basically no easy way around refactoring the existing (user) code base eventually. IMHO, you have the following options. Provide a permanent (non-optional) 2.6 compatible interface to your users. That means they do not have to change anything, but it kind of defeats the purpose of upgrading to Python 2.7 since the users still have to satisfy Python 2.6 semantics. Verdict: Not recommended. Provide a temporary (non-optional) 2.6 compatible interface for a limited amount of time. In that case the existing code needs to be refactored eventually. Verdict: Not recommended. Make users include a flag in their code (e.g. a magic comment that can be identified safely without executing the file like # *$$ supertool-pythonversion: 2.7 $$*), which Python version the code expects to be run with and use 2.6 compatibility only for the files that have not been flagged with Python 2.7. That way, you can do whatever compatibility hacks are needed to run old files and run new files the way they are. Verdict: Increases complexity, but helps you doing the migration. Recommended. However, you are in the convenient position of calling Python from C++. So you can control the environment that scripts are run with via the globals and locals dictionary passed to PyEval_EvalCode. In order to implement scenario 3, after checking the compatibility flag from the file, you can put a custom range function which supports float arguments into the gloabls dictionary before calling PyEval_EvalCode to "enable" the compatibility mode. I am not proficient with Python's C API, but in Python this would look like this (and it is possible to do the same via the C API): range27 = range def range26(start=None, stop=None, step=None): if start is not None and not isinstance(start, int): start = int(start) if stop is not None and not isinstance(stop, int): stop = int(stop) if step is not None and not isinstance(step, int): step = int(step) return range27(start, stop, step) def execute_user_code(user_file): ... src = read(user_file) global_dict = {} local_dict = {} ... if check_magic_version_comment(src) in (None, '2.6'): global_dict['range'] = range26 global_dict['range27'] = range27 # the last line is needed because the call # of range27 will be resolved against global_dict # when the user code is executed eval_code(src, global_dict, local_dict) ...

Related questions

0 votes
    I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I am using Pycharm 2018.2.4 community edition on Ubuntu 18.04 and I have started python 2.7 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am using Pycharm 2018.2.4 community edition on Ubuntu 18.04 and I have started python 2.7 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is get the headers of a given URL so I can determine the MIME type. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I'm using MySqldb with Python 2.7 to allow Python to make connections to another MySQL server import ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    >>> list(range(0, -9,-1)) will print [-1, -2, -3, -4,-5, -6, -7, -8) [0,-1, -2, -3, -4,-5, -6, -7, -8 ... [0,-1, -2, -3, -4,-5, -6, -7] Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    What will be the output of the following Python function? min(max(False,-3,-4), 2,7) a) -4 b) -3 c) 2 d) False...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    I have designed the simple function that looks at an inputted list of numbers, identifies a minimum and maximum ... into the list? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it ... xrange(0, 20): Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Write a program to accept a filename from the user and display all the lines from the file which contain python comment character ‘#’. Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    In Javascript, I can do something like this let list = [1, 2, 3] let list2 = [4, 5, 6, ...list] And ... the += after declaring a list? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I am using Eclipse with Pydev and googleAppengine. I have python 2.7 installed. I am trying to run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Write a program to accept string from user and search a given character from string without using find() function Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Ok. I'm working with the FW1FontWrapper code for use with DirectX : https://archive.codeplex.com/?p=fw1 This has removed ... hex back onto a UNIT32 UINT32 color = 0xFF + (ucolorb...
asked May 1, 2022 in Education by JackTerrance
0 votes
    Ok. I'm working with the FW1FontWrapper code for use with DirectX : https://archive.codeplex.com/?p=fw1 This has removed ... hex back onto a UNIT32 UINT32 color = 0xFF + (ucolorb...
asked May 1, 2022 in Education by JackTerrance
...