python-2.x

unicode_literals and doctest in Python 2.7 AND Python 3.5

本小妞迷上赌 提交于 2019-12-01 18:51:42
Consider the following demo script: # -*- coding: utf-8 -*- from __future__ import division from __future__ import unicode_literals def myDivi(): """ This is a small demo that just returns the output of a divison. >>> myDivi() 0.5 """ return 1/2 def myUnic(): """ This is a small demo that just returns a string. >>> myUnic() 'abc' """ return 'abc' if __name__ == "__main__": import doctest extraglobs = {} doctest.testmod(extraglobs=extraglobs) The doctest passes on Python 3.5, but fails on Python 2.7.9. The strange thing is, the divison test works, but the unicode test fails. I have seen various

Python how to decode unicode with hex characters

一曲冷凌霜 提交于 2019-12-01 17:47:05
I have extracted a string from web crawl script as following: u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' I want to decode u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' with utf-8. With http://ddecode.com/hexdecoder/ , I can see the result is '【中字】' I tried using the following syntax but failed. msg = u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' result = msg.decode('utf8') Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True)

map in Python 3 vs Python 2 [duplicate]

为君一笑 提交于 2019-12-01 16:47:39
问题 This question already has an answer here : Python: calling 'list' on a map object twice (1 answer) Closed last year . I'm a Python newbie reading an old Python book. It's based on Python 2, so sometimes I got little confused about detail. There is a code L=map(lambda x:2**x, range(7)) so it doesn't return the list in python 3, and I googled it and found that list(L ) works. But the problem is, first list(L) works fine, but when I use it again, list(L) list(L) second one returns [ ] Can

Why are 008 and 009 invalid keys for Python dicts?

為{幸葍}努か 提交于 2019-12-01 15:13:13
Why is it that I can't have 008 or 009 be keys for a Python dict, but 001-007 are fine? Example: some_dict = { 001: "spam", 002: "eggs", 003: "foo", 004: "bar", 008: "anything", # Throws a SyntaxError 009: "nothing" # Throws a SyntaxError } Update : Problem solved. I wasn't aware that starting a literal with a zero made it octal. That seems really odd. Why zero? In python and some other languages, if you start a number with a 0, the number is interpreted as being in octal (base 8), where only 0-7 are valid digits. You'll have to change your code to this: some_dict = { 1: "spam", 2: "eggs", 3:

python equality precedence

我们两清 提交于 2019-12-01 14:59:46
class L(object): def __eq__(self, other): print 'invoked L.__eq__' return False class R(object): def __eq__(self, other): print 'invoked R.__eq__' return False left = L() right = R() With this code, left side gets the first shot at comparison, as documented in the data model: >>> left == right invoked L.__eq__ False But if we make a slight modification on line 6 (everything else the same): class R(L): Now the right side gets to have the first shot at comparison. >>> left == right invoked R.__eq__ False Why is that? Where is it documented, and what's the reason for the design decision? This is

Pagination doesn't accept dict as data - unhashable type

时光总嘲笑我的痴心妄想 提交于 2019-12-01 11:30:53
I'm trying to use Django pagination Pagination Docs . But I'm receiving this error: TypeError at / unhashable type Which is basically because I'm using a dictionary as my object and not a queryset. I would like to know if there is a way to turn my dictionary a hashable object. This is my dict in template: {% for key, values in prodmatrix.items %} <li class="span3"> <div class="product-box"> <span class="sale_tag"></span> <p><a href="{% url 'product_detail' slug=values.3.0 %}"><img src="{{ STATIC_URL }}{{values.1.0}}" alt="" /></a></p> <a href="{% url 'product_detail' slug=values.3.0 %}" class=

Python - What type is flask.request.files.stream supposed to be?

馋奶兔 提交于 2019-12-01 09:22:42
In Flask (Flask-0.10.1 installed via pip) I've tried to handle uploaded files like this f = flask.request.files[input_name] stream = f.stream # then use the stream But it's confusing that in some cases stream is a BytesIO instance, but also a chance to be a file object. I've tested in this way from flask import Flask, request import cStringIO app = Flask('test') @app.route("/", methods=['POST']) def index(): if request.method == 'POST': f = request.files['file'] print type(f.stream) def send_file(client, name): with open(name, 'rb') as f: client.post('/', data={'file': (cStringIO.StringIO(f

Import some python3 modules in Python2

二次信任 提交于 2019-12-01 07:31:31
Is there a way to import Python 3 modules into Python 2 scripts? I want to use some Python 3 modules in a Django application and haven't seen anything on the Internet. Any clues? I don't think it's really possible, no. The same instance of the interpreter has to handle every module imported in a given app, so there's no obvious way to mix and match interpreters. If you need to accomplish a discrete task with a Python 3 module, you could try making a command-line script to accomplish your task and then calling that script as a subprocess from your Python 2 app, but that would be awkward to say

Why does Python change the value of an integer when there is a 0 in front of it?

孤人 提交于 2019-12-01 07:19:46
问题 I implemented a function converting an integer number to its representation as a string intToStr() (code below). For testing I've passed in some values and observed an unexpected output: print intToStr( 1223) # prints 1223 as expected print intToStr(01223) # prints 659, surprisingly Now, I've tried to debug it, and the the integer I've passed in has indeed turned out to be 659 . Why does this happen and how can I get python to ignore leading zeros of the integer literal? Here's the code for

Python - What type is flask.request.files.stream supposed to be?

牧云@^-^@ 提交于 2019-12-01 06:42:09
问题 In Flask (Flask-0.10.1 installed via pip) I've tried to handle uploaded files like this f = flask.request.files[input_name] stream = f.stream # then use the stream But it's confusing that in some cases stream is a BytesIO instance, but also a chance to be a file object. I've tested in this way from flask import Flask, request import cStringIO app = Flask('test') @app.route("/", methods=['POST']) def index(): if request.method == 'POST': f = request.files['file'] print type(f.stream) def send