python-3.x

Python 3 and static typing

I didn't really pay as much attention to Python 3's development as I would have liked, and only just noticed some interesting new syntax changes. Specifically from this SO answer function parameter annotation: def digits(x:'nonnegative number') -> "yields number's digits": # ... Not knowing anything about this, I thought it could ...

exec() bytecode with arbitrary locals?

Suppose I want to execute code, for example value += 5 inside a namespace of my own (so the result is essentially mydict['value'] += 5). There's a function exec(), but I have to pass a string there: exec('value += 5', mydict) and passing statements as strings seems strange (e.g. it's not colorized that way). Can it be done...

Python integer division yields float

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 2/2 1.0 Is this intended? I strongly remember earlier versions returning int/int=int? What should I do, is there a new division operator or must I always cast? ...

Getting a map() to return a list in python 3.1

Hello world, Im trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy: A: python 2.6: >>> map(chr,[66,53,0,94]) ['B', '5', '\x00', '^'] However, on 3.1, the above returns a map object. B: python 3.1: >>> map(chr,[66,53,0,94]) <map object at 0x00AF5570> How do i retrieve the mapped list (as...

python 3.1 with pydev

I am now moving to eclipse for my python development. I have pydev installed but it is showing grammar support up to python version 3.0. My question is can I use python 3.1 with 3.0 grammar? Has the grammar changed from version 3.0 to 3.1? I am using eclipse 3.4.2 and pydev 1.4.7 ...

What is the oldest time that can be represented in python?

Hi, I have written a function comp(time1, time2) which will return true when time1 is lesser than time2. I have a scenario where time1 should always be lesser than time2. I need time1 to have the least possible value(date). How to find this time and how to form the corresponding object. ...

XPath Query in XML using Python

Hi, Is it possible to use XPath Query in Python while processing XML. I am using minidom which doesn't support that. Is there any other module for that? ...

Python 3 unittest simulate user input

How can I simulate user input in the middle of a function called by a unit test (Using python 3's unittest)? For example, I have a function foo() who's output I'm testing. In the foo() function, it asks for user input: x = input(msg) And the output is based on the input: print("input: {0}".format(x)) I would like my unit tes...

Python design patterns, cross importing

I am using Python for automating a complex procedure that has few options. I want to have the following structure in python. - One "flow-class" containing the flow - One helper class that contains a lot of "black boxes" (functions that do not often get changed). 99% of the time, I modify things in the flow-class so I only want code ther...

"Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3.

I am using python 3.1, on a windows 7 machines. Russian is the default system language, and utf-8 is the default encoding. Looking at the answer to a previous question, I have attempting using the "codecs" module to give me a little luck. Here's a few examples: >>> g = codecs.open("C:\Users\Eric\Desktop\beeline.txt", "r", encoding="utf...

Python 3.1.1 on Mac OS X 10.6 Snow Leopard

I've spent some time today playing with getting the source for python 3.1.1 to build on my MacBook Pro using the --enable-framework and --enable-universalsdk options with no success. I will humbly admit that I have no real clue why I can't compile 3.1.1 on Snow Leopard, I did make sure to get the new Xcode version for Snow Leopard, and m...

dbm.error: db type is dbm.bsd, but the module is not available (Python 3.0)

I'm trying to open a shelve file that I created in 2.5, but I get the error I listed in the question title. The data aren't essential, but I really want them. Looking at the lib\dbm\__init__.py file it recognizes 4 types of dbm modules but only finds dbm.dumb on my system. Does anyone know how I can open this? Is there a bsd.p...

How to call super() in Python 3.0?

Hi all, I have the strangest error I have seen for a while in Python (version 3.0). Changing the signature of the function affects whether super() works, despite the fact that it takes no arguments. Can you explain why this occurs? Thanks, Chris >>> class tmp: ... def __new__(*args): ... super() ... >>> tmp() Traceb...

Finding set difference between two complex dictionaries

Hi, I have two dictionaries of the following structure: a) dict1 = {'a':[ [1,2], [3,4] ], 'b':[ [1,2],[5,6] ]} b) dict2 = {'a':[ [1,2], [5,6] ], 'b':[ [1,2],[7,8] ]} I need to find the set difference between each key in the dictionary i.e., dict1['a'] - dict2['a'] should return [3,4]. Any thought is appreciated. ...

Generating a list from complex dictionary

Him I have a dictionary dict1['a'] = [ [1,2], [3,4] ]. I need to generate a list out of this dictionary as l1 = [2, 4] i.e., a list out of the second element of each inner list. It can be a separate list or even the dictionary can be modified as dict1['a'] = [2,4]. Any idea would be appreciated. ...

Python 3.1 inline division override

I don't know if this is a bug in 3.1, but if I remember correctly "inline" division worked like this in pre-3k versions: Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class A: ... def __init__(self, x): ... sel...

String formatting

Hi, I have a list filter = ['a', 'b', 'c']. I need to frame the following string out of the list "item -a item -b item -c". Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 - 150. Wouldn't that lead to overflow? And what is the maximum length of the string supp...

Edit python31 file and it opens notepad and starts python26

I am in python31, then I go to file open i left click to open file and it opens in notepad(simple text editor)python31 The moment it opens the notepad, it starts python26 I thought it has something to open with, and I have changed that to python31 And it still opens python26 EDIT: The file is created by python26, but it is not exec...

Why do I have this TypeError when using tkinter?

so I upgraded to python 3.1.1 from 2.6 and i ran an old program of mine which uses tkinter. I get the following error message which I don't recall getting in the 2.6 version. Exception in Tkinter callback Traceback (most recent call last): File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__ return self.func(*args) ...

Should I use pgreloaded? Or subversion of pygame?

I'm using pygame with python 2.6 right now, But I want to use python 3.1.1 instead. The normal pygame only works with 2.x, but the subversion ones work with 3.x I think. But what about pgreloaded? Is that useable yet? The normal pygame actually works with 3.1 too, but not for os x (at least there isn't a download). Does anyone actually u...