python-2.x

A number smaller than negative infinity in python?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 15:20:31
This is possible in python2: None < float('-inf') Also, it always returns True However, on python3, this throws TypeError: unorderable types: NoneType() < int() Why is None comparable to integers/floats with python2? Are there any benefits or applications to None being orderable in python2? First of all Python 2 allowed comparing all types of mixed types. This wart was fixed in Python 3 eventually. CPython implementation detail : Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address

Python: How to resize an image using PIL module

给你一囗甜甜゛ 提交于 2019-11-29 14:05:15
I'm trying to resize an image to 500x500px but got this error: File "C:\Python27\lib\site-packages\PIL\Image.py", line 1681, in save save_handler = SAVE[format.upper()] KeyError: 'JPG' This is the code: from PIL import Image img = Image.open('car.jpg') new_img = img.resize((500,500)) new_img.save('car_resized','jpg') You need to set the format parameter in your call to the save function to 'JPEG': from PIL import Image img = Image.open('car.jpg') new_img = img.resize((500,500)) new_img.save("car_resized.jpg", "JPEG", optimize=True) Here is the solution: from PIL import Image img = Image.open(

How to print output from a script in gui called in another Tkinter script?

空扰寡人 提交于 2019-11-29 10:25:13
问题 I have tried using several different similar solutions that I have found online, but none seem to quite do what I am aiming for. I want to call an external script (helloworld.py) into my tkinter gui. I want this called script (helloworld.py) to execute all the functions that are contained in it upon a button press in the gui and print the resulting outputs into the gui, not the console. I have found some solutions which will print the output to the console, but I am unable to get it to

Tutorial for Python - Should I use 2.x or 3.0? [closed]

谁都会走 提交于 2019-11-29 10:18:32
Python 3.0 is in beta with a final release coming shortly. Obviously it will take some significant time for general adoption and for it to eventually replace 2.x. I am writing a tutorial about certain aspects of programming Python. I'm wondering if I should do it in Python 2.x or 3.0? (not that the difference is huge) a 2.x tutorial is probably more useful now, but it would be nice to start producing 3.0 tutorials. anyone have thoughts? (of course I could do both, but I would prefer to do one or the other) Start with 2.x. Most existing libraries will be on 2.x for a long time. Last year, Guido

How to write Russian characters in file?

拈花ヽ惹草 提交于 2019-11-29 09:58:19
In console when I'm trying output Russian characters It gives me ??????????????? Who know why? I tried write to file - in this case the same situation. for example f=open('tets.txt','w') f.write('some russian text') f.close inside file is - ?????????????????????????/ or p="some russian text" print p ????????????? In additional Notepad don't allow me to save file with Russian letters. I give this: This file contains characters in Unicode format which will be lost if you save this file as an ANSI encoded text file. To keep the Unicode information, click Cancel below and then select one of the

How to get source code of function that is wrapped by a decorator?

北慕城南 提交于 2019-11-29 09:41:23
问题 I wanted to print the source code for my_func , that is wrapped by my_decorator : import inspect from functools import wraps def my_decorator(some_function): @wraps(some_function) def wrapper(): some_function() return wrapper @my_decorator def my_func(): print "supposed to return this instead!" return print inspect.getsource(my_func) However, it returns source for wrapper instead: @wraps(some_function) def wrapper(): some_function() Is there a way for it to print the following instead? def my

Python 2 maketrans() function doesn't work with Unicode: “the arguments are different lengths” when they actually are

牧云@^-^@ 提交于 2019-11-29 09:23:27
问题 [Python 2] SUB = string.maketrans("0123456789","₀₁₂₃₄₅₆₇₈₉") this code produces the error: ValueError: maketrans arguments must have same length I am unsure why this occurs because the strings are the same length. My only idea is that the subscript text length is somehow different than standard size characters but I don't know how to get around this. 回答1: No, the arguments are not the same length: >>> len("0123456789") 10 >>> len("₀₁₂₃₄₅₆₇₈₉") 30 You are trying to pass in encoded data ; I

matplotlib color map - predefine mappings to values?

半城伤御伤魂 提交于 2019-11-29 08:38:27
I have an array that I am viewing using imshow() . (imsave() really, but the process should be identical). I know that the values in the array will be between 0-9 and wonder if it is possible to use cmap to set each output to a specific 'color'. Perhaps by mapping these to a dict? Just use a ListedColormap . As a quick (but ugly) example: import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap cmap = ListedColormap(['red', 'green', 'blue', 'black'], 'indexed') fig, ax = plt.subplots() im = ax.imshow([range(4)], interpolation='none', cmap=cmap) fig.colorbar(im) plt.show()

Getting ready to convert from Python 2.x to 3.x

纵然是瞬间 提交于 2019-11-29 07:52:01
问题 As we all know by now (I hope), Python 3 is slowly beginning to replace Python 2.x. Of course it will be many MANY years before most of the existing code is finally ported, but there are things we can do right now in our version 2.x code to make the switch easier. Obviously taking a look at what's new in 3.x will be helpful, but what are some things we can do right now to make the upcoming conversion more painless (as well as make it easier to output updates to concurrent versions if needed)?

Get complete list of all possible Class Attributes

喜夏-厌秋 提交于 2019-11-29 07:46:48
Is there a way, given a simple Class, to output all the possible attributes for it? Standard attributes like __class__ and __doc__ and special read only attributes like __mro__ , __bases__ et al. Generally, all present attributes? Considering the most simplistic case for a Class: class myClass: pass The dir() , vars() and inspect.getmembers() all exclude certain builtin attributes. The most complete list is offered by using myClass.__dir__(MyClass) which, while adding built in attributes, excludes user defined attributes of MyClass , for example: In [3]: set(MyClass.__dir__(MyClass)) - set(dir