python-3.x

Easy Python reloading module question

I'm trying to reload a module I have already inputted. I understand that you only need to input once and retyping the input command won't produce the same affect without having had exited out of python's command prompt. I just don't understand why this statement: reload (script4.py) is giving me: Traceback (most recent call last): F...

Python 3 doesn't read unicode file on a new server

My webpages are served by a script that dynamically imports a bunch of files with try: with open (filename, 'r') as f: exec(f.read()) except IOError: pass (actually, can you suggest a better method of importing a file? I'm sure there is one.) Sometimes the files have strings in different languages, like # contents of lan...

Python 3, is using sys.stdout.buffer.write() good style?

After I learned about reading unicode files in Python 3.0 web script, now it's time for me to learn using print() with unicode. I searched for writing unicode, for example this question explains that you can't write unicode characters to non-unicode console. However, in my case, the output is given to Apache and I am sure that it is cap...

Writing with Python's built-in .csv module

[Please note that this is a different question from the already answered How to replace a column using Python’s built-in .csv writer module?] I need to do a find and replace (specific to one column of URLs) in a huge Excel .csv file. Since I'm in the beginning stages of trying to teach myself a scripting language, I figured I'd try to i...

What is your wishlist for Python 3.2 (or Python 3.x)?

I've just read the What's New in Python 3.1 text (and I like many things). What's your idea of the thing that didn't go there but you would like to see in the future Python versions? ...

Why print statement is not pythonic?

This question was bugging me for quite a while (as evidenced by my previous question): why exactly is print(x) better (which is defined as being more pythonic) than print x? For those who don't know, the print statement was changed into function in Python 3.0. The formal documentation is in PEP 3105 and motivation is in Guido van Rossum...

Easy way to implement dynamic views?

View are useful constructions of Python 3. For those who never noticed (like me): for a dictionary d you can write k = d.keys() and even if you update d the variable k will still be giving you the updated keys. You can write then k1 & k2 and it will always give you d1.keys() & d2.keys() I want to implement this for my personal todo mana...

How to do windows API calls in Python 3.1?

Has anyone found a version of pywin32 for python 3.x? The latest available appears to be for 2.6. Alternatively, how would I "roll my own" windows API calls in Python 3.1? ...

Is generator.next() visible in python 3.0?

I have a generator that generates a series, for example: def triangleNums(): '''generate series of triangle numbers''' tn = 0 counter = 1 while(True): tn = tn + counter yield tn counter = counter + 1 in python 2.6 I am able to make the following calls: g = triangleNums() # get the generator g.n...

Hello World in Python

I am trying to learn Python, however I tried to run a script that is LITERALLY just: print "Hello, World!" And I get this error: File "hello.py", line 1 print "Hello, World!" ^ SyntaxError: invalid syntax What is going on!? ...

Running both python 2.6 and 3.1 on the same machine

Hi there, I'm currently toying with python at home and I'm planning to switch to python 3.1. The fact is that I have some script that use python 2.6 and I can't convert them since they use some module that aren't available for python 3.1 atm. So I'm considering installing python 3.1 along my python 2.6. I only found people on internet t...

How to swap keys for values in a dictionary

Suppose that I need to swap keys for values in dictionary. This is what I have in mind (Assuming that the values' value is unique): >>> my_dict = {'x':1, 'y':2, 'z':3} >>> my_dict2 = {} >>> for key, val in my_dict.items(): my_dict2[val] = key Is there any other efficient way to do it? ...

want to get mac address of remote PC

Hello, I have my web page in python, i am able to get the ip address of the user, who will be accessing our web page, we want to get the mac address of the user's PC, is it possible in python, we are using linux PC, we want to get it on linux. ...

Improve a IRC Client in Python

Hello, How i can make some improvement in my IRC client made in Python. The improvement is: How i can put something that the user can type the HOST, PORT, NICK, INDENT and REALNAME strings and the message? And here is the code of the program: simplebot.py import sys import socket import string HOST="irc.freenode.net" PORT=6667 NIC...

Why was the 'thread' module renamed to '_thread' in Python 3.x?

Python 3.x renamed the low-level module 'thread' to '_thread' -- I don't see why in the documentation. Does anyone know? ...

Python 2.5 socket._fileobject is what in Python 3.1?

I'm porting some code that runs on Python 2.5 to Python 3.1. A couple of classes subclass the socket._fileobject: class X(socket._fileobject): .... Is there an equivalent to socket._fileobject in Python 3.1? A quick scan of the source code doesn't turn up anything useful. Thanks! ...

Expat parsing in python 3

import xml.parsers.expat def start_element(name, attrs): print('Start element:', name, attrs) def end_element(name): print('End element:', name) def character_data(data): print('Character data: %s' % data) parser = xml.parsers.expat.ParserCreate() parser.StartElementHandler = start_element parser.EndElementHandler = end_e...

Stream/string/bytearray transformations in Python 3

Python 3 cleans up Python's handling of Unicode strings. I assume as part of this effort, the codecs in Python 3 have become more restrictive, according to the Python 3 documentation compared to the Python 2 documentation. For example, codecs that conceptually convert a bytestream to a different form of bytestream have been removed: b...

Should I Start With Python 3.0?

Recently I decided to expand my programming horizons and learn the python programming language. While I have used python a little bit for classes in college and for a project or two at work I am by no means an expert. My question is as follows: should I bother with the 2.x releases or should I jump straight to 3.0? I am leaning towards 3...

Is it necessary or useful to inherit from python's object in Python 3.x?

In older python version when you create a class in python, it can inherit from object which is as far I understand a special built-in python element that allow your object to be a new-style object. What about newer version (> 3.0 and 2.6)? I googled about the class object but I get so much result (for an obvious reasons). Any hint? Tha...