python-2.x

Neat way to port nonpolyglot code from Py2 to Py3

非 Y 不嫁゛ 提交于 2020-07-16 04:26:45
问题 In many of my python modules, I am using from itertools import izip_longest But now I am moving my codebase to Py3 (compatible with Py2 at the same time). And in Py3 izip_longest is renamed to zip_longest. One answer to fix this is recommended in Failing to import itertools in Python 3.5.2, i.e. to change import statement to following. try: # Python 3 from itertools import zip_longest as izip_longest except ImportError: # Python 2 from itertools import izip_longest However, changing this in

How to dynamically create a derived type in the Python C-API

心已入冬 提交于 2020-06-24 00:01:05
问题 Assume we have the type Noddy as defined in the tutorial on writing C extension modules for Python. Now we want to create a derived type, overwriting only the __new__() method of Noddy . Currently I use the following approach (error checking stripped for readability): PyTypeObject *BrownNoddyType = (PyTypeObject *)PyType_Type.tp_alloc(&PyType_Type, 0); BrownNoddyType->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; BrownNoddyType->tp_name = "noddy.BrownNoddy"; BrownNoddyType->tp_doc =

Python 2 vs Python 3 - Difference in map behavior with three arguments?

点点圈 提交于 2020-06-16 02:40:26
问题 The following code behaves differently in Python 2 vs Python 3: all(map(lambda x,y: x, [1, 2], [1, 2, 3])) Python 2 gives False whereas Python 3 gives True . The documentation for Python 2 says that it will supply None if the shorter list is exhausted but Python 3 doesn't do that. I am working on a code that really needs the length to be maintained for some reason. What is the cleanest way to get the old behavior? I know I can use from past.builtin import map as old_map , but is there a more

Python 2 vs Python 3 - Difference in map behavior with three arguments?

烂漫一生 提交于 2020-06-16 02:40:07
问题 The following code behaves differently in Python 2 vs Python 3: all(map(lambda x,y: x, [1, 2], [1, 2, 3])) Python 2 gives False whereas Python 3 gives True . The documentation for Python 2 says that it will supply None if the shorter list is exhausted but Python 3 doesn't do that. I am working on a code that really needs the length to be maintained for some reason. What is the cleanest way to get the old behavior? I know I can use from past.builtin import map as old_map , but is there a more

Why does ord() fail when porting from Python 2 to Python 3?

岁酱吖の 提交于 2020-05-29 10:36:26
问题 I am trying to port a Python library called heroprotocol from Python 2 to Python 3. This library is used to parse replay files from an online game called Heroes of the Storm, for the purpose of getting data from the file (i.e. who played against who, when did they die, when did the game end, who won, etc). It seems that this library was created for Python 2, and since I am using Python 3 (specifically Anaconda, Jupyter notebook) I would like to convert it to Python 3. The specific issue I am

How can I use Python 2 on google colab?

♀尐吖头ヾ 提交于 2020-05-16 06:31:57
问题 Can someone guide me how can I use the earlier version of python i.e version2.x? Under change runtime option - I can see option for selecting hardware accelerator. Thanks 回答1: You can use this shortcut to create a Python 2 Colab. bit.ly/colabpy2 It will forward to this URL. https://colab.research.google.com/notebook#create=true&language=python2 回答2: Python 2 reached its end of life on January 1, 2020, and is no longer supported by the Python developer community. Because of that, Colab is in

Brew - reinstalling python@2

孤街醉人 提交于 2020-04-29 10:47:24
问题 I have been having issues with openssl and python@2 with brew, which have explained here (unresolved). The documented workaround to reinstall Python and openssl was not working, so I decided I would uninstall and reinstall Python. The problem is, when you try to install Python 2 with brew, you receive this message: brew install python@2 Error: No available formula with the name "python@2" ==> Searching for a previously deleted formula (in the last month)... Warning: homebrew/core is shallow