python

How to get filename of the __main__ module in Python?

Suppose I have two modules: a.py: import b print __name__, __file__ b.py: print __name__, __file__ I run the "a.py" file. This prints: b C:\path\to\code\b.py __main__ C:\path\to\code\a.py Question: how do I obtain the path to the __main__ module (a.py in this case) from within the "b.py" library? ...

Can I use VS2005 to build extensions for a Python system built with VS2003

RDFLib needs C extensions to be compiled to install on ActiveState Python 2.5; as far as I can tell, there's no binary installer anywhere obvious on the web. On attempting to install with python setup.py install, it produces the following message: error: Python was built with Visual Studio 2003; extensions must be built with a compiler...

Model and Validation Confusion - Looking for advice

I'm somewhat new to Python, Django, and I'd like some advice on how to layout the code I'd like to write. I have the model written that allows a file to be uploaded. In the models save method I'm checking if the file has a specific extension. If it has an XML extension I'm opening the file and grabbing some information from the file to ...

How does Python's "super" do the right thing?

I'm running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the derived-most class, then its parent classes as listed from left to right, then the grandp...

Preserving last new line when reading a file

I´m reading a file in Python where each record is separated by an empty new line. If the file ends in two or more new lines, the last record is processed as expected, but if the file ends in a single new line it´s not processed. Here´s the code: def fread(): record = False for line in open('somefile.txt'): if line.starts...

Integrate postfix mail into my (python)webapp

I have a postfix server listening and receiving all emails received at mywebsite.com Now I want to show these postfix emails in a customized interface and that too for each user To be clear, all the users of mywebsite.com will be given mail addresses like [email protected] who receives email on my production machine but he sees them...

Python parsing

I'm trying to parse the title tag in an RSS 2.0 feed into three different variables for each entry in that feed. Using ElementTree I've already parsed the RSS so that I can print each title [minus the trailing )] with the code below: feed = getfeed("http://www.tourfilter.com/dallas/rss/by_concert_date") for item in feed: print rep...

Django email

I am using the Gmail SMTP server to send out emails from users of my website. These are the default settings in my settings.py EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'pwd' EMAIL_PORT = 587 EMAIL_USE_TLS = True SERVER_EMAIL = EMAIL_HOST_USER DEFAULT_FROM_EMAIL = EMAIL_HOST_USER If I...

What will I lose or gain from switching database APIs? (from pywin32 and pysqlite to QSql)

I am writing a Python (2.5) GUI Application that does the following: Imports from Access to an Sqlite database Saves ui form settings to an Sqlite database Currently I am using pywin32 to read Access, and pysqlite2/dbapi2 to read/write Sqlite. However, certain Qt objects don't automatically cast to Python or Sqlite equivalents when...

is there COMMIT analog in python for writing into a file?

I have a file open for writing, and a process running for days -- something is written into the file in relatively random moments. My understanding is -- until I do file.close() -- there is a chance nothing is really saved to disk. Is that true? What if the system crashes when the main process is not finished yet? Is there a way to do k...

Python port binding

Hey everyone, I've recently been learning python and I just started playing with networking using python's socket library. Everything has been going well until recently when my script terminated without closing the connection. The next time I ran the script, I got: File "./alert_server.py", line 9, in <module> s.bind((HOST, PORT)) Fi...

Monitoring user idle time

Developing a mac app, how can I tell whether the user is currently at their computer or not? Or how long ago they last pressed a key or moved the mouse? ...

django- run a script from admin

I would like to write a script that is not activated by a certain URL, but by clicking on a link from the admin interface. How do I do this? Thanks! ...

Getting the name of the active window

I want to write a python script on Windows that saves the title of the program that the user uses at a given time like the http://www.rescuetime.com . I don't want to use rescuetime because of privacy consideration but instead write a script that does something similar myself to capture data about how much I use my computer. Is there so...

python PIL install on shared hosting

I'm going to deploy a django app on a shared hosting provider. I installed my own python in my home, it works fine. My promblem comes with the installation of PIL, i have no support for JPEG after the compile process. I know that the compiler dont find "libjpeg", so i tried to install it at my home, i download the tar.gz and compile it ...

Excluding a top-level directory from a setuptools package

I'm trying to put a Python project into a tarball using setuptools. The problem is that setuptools doesn't appear to like the way that the source tree was originally setup (not by me, I must add). Everything that I actually want to distribute is in the top-level directory, rather than in a subdirectory like the setuptools docs talk abo...

Creating lists using yield in Ruby and Python

I'm trying to come up with an elegant way of creating a list from a function that yields values in both Python and Ruby. In Python: def foo(x): for i in range(x): if bar(i): yield i result = list(foo(100)) In Ruby: def foo(x) x.times {|i| yield i if bar(i)} end result = [] foo(100) {|x| result << x} Although I love ...

[Python] Is there anything that cannot appear inside parentheses?

I was intrigued by this answer to my question about getting vim to highlight unmatched brackets in python code. Specifically, I'm talking about the second part of his answer where he mentions that the C syntax highlighting is actually flagging as an error any instance of curly braces inside parens. It is an unobtrusive cue that you hav...

How can I stop IDLE from printing giant lists?

Sometimes I'll be working with, say, a list of thousands of items in IDLE, and accidently print it out to the shell. When this happens, it crashes or at least very significaly slows down IDLE. As you can imagine, this is extremely inconvenient. Is there a way to make it, rather than printing the entire thing, just give me a summarised [1...

PY: Url Encode without variable name

Hi Is there a way in python to url encode list without variables names? for example q=['with space1', 'with space2'] into qescaped=['with%20space1', 'with%20space2'] ...