python

How can I remove (chomp) a newline in Python?

This is one of my most common questions when I am coding Python (I was fed Perl as a baby and am forever trying to get rid of that affliction) and I wanted to put it out there on stack overflow so that next time I search for 'chomp python' on google, I get a useful answer. ...

What is the regular expression for /urlchecker/http://www.google.com

I'm writing a url rewrite in django that when a person goes to http://mysite.com/urlchecker/http://www.google.com it sends the url: http://ww.google.com to a view as a string variable. I tried doing: (r'^urlchecker/(?P<url>\w+)/$', 'mysite.main.views.urlchecker'), But that didn't work. Anyone know what I'm doing wrong? Also, gene...

How do I perform HTML decoding/encoding using Python/Django?

I have a string that is html encoded: &lt;img class=&quot;size-medium wp-image-113&quot; style=&quot;margin-left: 15px;&quot; title=&quot;su1&quot; src=&quot;http://blah.org/wp-content/uploads/2008/10/su1-300x194.jpg&amp;quot; alt=&quot;&quot; width=&quot;300&quot; height=&quot;194&quot; /&gt; I want to change that to: <img...

Python ctypes and function calls

My friend produced a small proof-of-concept assembler that worked on x86. I decided to port it for x86_64 as well, but I immediately hit a problem. I wrote a small piece of program in C, then compiled and objdumped the code. After that I inserted it to my python script, therefore the x86_64 code is correct: from ctypes import cast, CFU...

information seemingly coming out of mysqldb incorrectly, python django

In a latin-1 database i have '\222\222\223\225', when I try to pull this field from the django models I get back u'\u2019\u2019\u201c\u2022'. from django.db import connection (Pdb) cursor = connection.cursor() (Pdb) cursor.execute("SELECT Password from campaignusers WHERE UserID=26") (Pdb) row = cursor.fetchone() So I step into that an...

Database change underneath SQLObject

I'm starting a web project that likely should be fine with SQLite. I have SQLObject on top of it, but thinking long term here -- if this project should require a more robust (e.g. able to handle high traffic), I will need to have a transition plan ready. My questions: How easy is it to transition from one DB (SQLite) to another (MySQ...

How do you get a thumbnail of a movie using IMDbPy?

Using IMDbPy it is painfully easy to access movies from the IMDB site: import imdb access = imdb.IMDb() movie = access.get_movie(3242) # random ID print "title: %s year: %s" % (movie['title'], movie['year']) However I see no way to get the picture or thumbnail of the movie cover. Suggestions? ...

What's the difference between all of the os.popen() methods?

I was looking at the Python documentation and saw that there are 4-5 different versions of popen(), e.g. os.popen(), os.popen2(), etc. Apart from the fact that some include stderr while others don't, what are the differences between them and when would you use each one? The documentation didn't really explain it very well. ...

import mechanize module to python script

I tried to import mechanize module to my python script like this, from mechanize import Browser But, Google appengine throws HTTP 500 when accessing my script. To make things more clear, Let me give you the snapshot of my package structure, root ....mechanize(where all the mechanize related files there) ....main.py ....app.yaml ....

Remote debugging of multi threaded Python Applications

How can I do remote debugging of a multi threaded Python application, running on an Embedded Linux based system, from Windows XP or Vista? So far I have only come across PyScripter based remote debugging. How does it perform? ...

How to get current CPU and RAM usage in Python?

What's your preferred way of getting current system status (current CPU, RAM, free disk space, etc.) in Python? Bonus points for *nix and Windows platforms. There seems to be a few possible ways of extracting that from my search: Using a library such as PSI (http://www.psychofx.com/psi/trac/wiki/ -- that currently seems not actively d...

WCF and Python

Is there any example code of a cpython (not IronPython) client which can call Windows Communication Foundation (WCF) service? ...

What's a cross platform way to play a sound file in python?

Hey guys, I tried playing a .wav file using pyaudio. It works great on windows, but doesn't work in Ubuntu when another device is using sound. The error is "IOError: [Errorno Invalid output device (no default output device)] -9996 Is there another library I could try to use? Another method? ...

CPU Usage Per Process in Python

Is it possible for me to see the amount of processor usage (% of maximum) that the current, python, app is using? Scenario: My host will allow me to run my app as long as it does not consume more then X% of the CPU power, so I would like it to 'keep an eye on itself' and slowdown. So how can I know how much CPU the app is using? Targ...

How to show the visitor a moved web page AND return a 301 redirect HTTP response status code in Django?

When a webpage has moved to a new location, how do I show the moved web page AND return a 301 permanent redirect HTTP response status code in Django? ...

Syntax error whenever I put Python code inside a Django template

I'm trying to do the following in my Django template: {% for embed in embeds %} {% embed2 = embed.replace("&lt;", "<") %} {{embed2}}<br /> {% endfor %} However, I always get an invalid block or some syntax error when I do anything like that (by that I mean {% %} code inside a loop). Python doesn't have {} to si...

Converting PDF to HTML with Python

How can I convert PDF files to HTML with Python? I was thinking something alone the lines of what Google does (or seems to do) to index PDF files. My final goal is to setup Apache to show the HTML for the PDF files, so anything leading me in that direction would also be appreciated. ...

Can you achieve a case insensitive 'unique' constraint in Sqlite3 (with Django)?

So let's say I'm using Python 2.5's built-in default sqlite3 and I have a Django model class with the following code: class SomeEntity(models.Model): some_field = models.CharField(max_length=50, db_index=True, unique=True) I've got the admin interface setup and everything appears to be working fine except that I can create two Som...

How to import a python file in python script more than once

Is it possible to import a python file more than once in a python script because i run a loop back to my driver file in a function by using the import command but it only works once? thanks edit: Resolved myself thanks ...

Exposing a C++ API to Python

I'm currently working on a project were I had to wrap the C++ classes with Python to be able to script the program. So my specific experience also involved embedding the Python interpreter in our program. The alternatives I tried were: Boost.Python I liked the cleaner API produced by Boost.Python, but the fact that it would have requ...