python-2.x

Can a from __future__ import … guarantee Python 2 and 3 compatibility?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 19:12:08
问题 I'm not interested in warming up the "Python 2 or Python 3?" questions (even though the most recent one I found is over one year old), but I stumbled upon this claim: You can write the Python 3 code under Python 2 if your file begins with the line: from __future__ import absolute_import, division, generators, unicode_literals, print_function, nested_scopes, with_statement With that line in place, your code will work with either Python 2 or Python 3. There may be rare cases in which it doesn't

how to investigate python2 segfault on imp.load_module

妖精的绣舞 提交于 2019-12-06 11:55:36
I am trying to install and use dolfin on Arch Linux, with Python 2.7.3. What is the best way to find out what is causing segmentation faults such as these? $ python2 -c "import dolfin; print dolfin.__version__" [3] 6491 segmentation fault (core dumped) python2 -c "import dolfin; print dolfin.__version__" I have tried inspecting the core file through gdb: $ sudo systemd-coredumpctl gdb 6491 but they are always truncated: Reading symbols from /usr/bin/python2.7...(no debugging symbols found)...done. BFD: Warning: /var/tmp/coredump-wSi8DV is truncated: expected core file size >= 39694336, found:

Python strings and str() method encoding and decoding

喜夏-厌秋 提交于 2019-12-06 11:04:20
I see that the Python manual mentions .encode() and .decode() string methods. Playing around on the Python CLI I see that I can create unicode strings u'hello' with a different datatype than a 'regular' string 'hello' and can convert / cast with str() . But the real problems start when using characters above ASCII 127 u'שלום' and I am having a hard time determining empirically exactly what is happening. Stack Overflow is overflowing with examples of confusion regarding Python's unicode and string-encoding/decoding handling . What exactly happens (how are the bytes changed, and how is the

Python 2.x sorted puzzlement

久未见 提交于 2019-12-06 07:50:37
I have a partially sorted tuple in Python 2.x. Why Python reverse it instead of sort it? >>> data = (u'a', (1,), 'b ', u'b', (2,), 'c ', u'c', (3,), 'd ', u'd', (4,), 'e') >>> sorted(data) == list(reversed(data)) True I look forward to Python 3. It fails because the sorting algorithm depends on a total ordering of the elements, which implies transitive < . The ordering of unicode strings, tuples, and strings isn't transitive: >>> a = 'x' >>> b = (1,) >>> c = u'x' >>> a < b True >>> b < c True >>> a < c False I.e., there exists no valid sort for your list. At least not with the default

sublime text 3 use python 3.3 as compiler

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 07:14:58
Just purchased sublime 3. I'm wondering how I can compile in python 3 and not in python 2. When I execute import sys print(sys.version) I receive "2.7.3 |EPD_free 7.3-2 (32-bit)| (default, Apr 12 2012, 14:30:37) [MSC v.1500 32 bit (Intel)]" I've been stuck for the last 2 or 3 hours. I know it has something to do with builds but it just doesn't seem to be clicking. Any help would be greatly appreciated. Thanks! The Sublime Text Python builder uses the Python executable found in your PATH. Instructions for Windows could be found here: http://pythoncentral.org/how-to-install-python-2-7-on-windows

Do variables defined inside list comprehensions leak into the enclosing scope? [duplicate]

半城伤御伤魂 提交于 2019-12-06 07:14:40
This question already has answers here : List comprehension rebinds names even after scope of comprehension. Is this right? (5 answers) Closed last year . I can't find anywhere that defines this behaviour: if [x for x in [0, 1, -1] if x > 0]: val = x How safe is this code? Will val always be assigned to the last element in the list if any element in the list is greater than 0? In Python 2.x, variables defined inside list comprehensions leak into their enclosing scope, so yes, val will always be bound to the last value bound to x during the list comprehension (as long as the result of the

unpack_from requires a buffer of at least 4 bytes

自古美人都是妖i 提交于 2019-12-06 06:11:52
I am receiving a packet from client, consisting of many fields. I read all fields successfully, but when it comes to the last field which is tag_end, python gives me an error: unpack_from requires a buffer of at least 4 bytes not found. this is the code: def set_bin(self, buf): """Reads a vector of bytes (probably received from network or read from file) and tries to construct the packet structure from it, by reading each packet member from the buffer. This is somehow like deserializing the packet. """ assert isinstance(buf, bytearray), 'buffer type is not valid' offset = 0 print("$$$$$$$$$$$$

Python's handling of shell strings

随声附和 提交于 2019-12-06 06:03:25
问题 I still do not understand completely how python's unicode and str types work. Note: I am working in Python 2, as far as I know Python 3 has a completely different approach to the same issue. What I know : str is an older beast that saves strings encoded by one of the way too many encodings that history has forced us to work with. unicode is an more standardised way of representing strings using a huge table of all possible characters, emojis, little pictures of dog poop and so on. The decode

Appending a range to a list

假如想象 提交于 2019-12-06 05:27:41
I have some basic code that I'm not grasping the behaviour of: L = [ 'a', 'bb', 'ccc' ] L.append(range(2)) print len(L) print len(L + range(1)) print len(L) The output of which is 4 5 4 This is confusing to me, as my thought process is that the length of the initial list is 3, and appending range(2) to the end brings it to length of 5. Therefore I'd expect the output to be 5 6 5 . I'm sure it's a simple quirk, but I'm a bit lost and having a hell of a time trying to find an answer online. Would anyone be able to point me in the right direction? Martijn Pieters You appended a single list object

How to install python-tk in my docker image

拥有回忆 提交于 2019-12-06 02:46:24
I get this error when I run my python script in my docker image ImportError: No module named _tkinter, please install the python-tk package So I tried 'pip install python-tk' root@43d2222b15c8:/tf_files# pip install python-tk Collecting python-tk Could not find a version that satisfies the requirement python-tk (from versions: ) No matching distribution found for python-tk But that does not fix my issue. Can you please tell me how can I fix my issue? Thank you. Use import Tkinter . It should not be required via pip. It's built into python2.7 (Assuming you are using python2.7 since this post is