python

BeautifulSoup not taking in a string?

So I am trying to scrape a web page but am getting some funky errors. html = urllib2.urlopen("http://sis.rpi.edu/reg/zs201101.htm").read() # 1 html = re.sub("(<script)(.+\n)+(.+)(</script>)","", html) # 2 print type(html) # 3 (Returns: <type 'str'>) soup = BeautifulSoup(html) # 4 With line 2 commented out, it tries to parse 'html' wit...

CSRF error in Django; How can I add CSRF to my login view?

I have a simple form I want users to be able to log into; here is the template code with the CSRF tag in it: <html> <head><title>My Site</title></head> <body> <form action="" method="post">{% csrf_token %} <label for="username">User name:</label> <input type="text" name="username" value="" id="username"> <la...

Is there any reason for using classes in Python if there is only one class in the program?

I've seen some people writing Python code by creating one class and then an object to call all the methods. Is there any advantage of using classes if we don't make use of inheritance, encapsulation etc? Such code seems to me less clean with all these 'self' arguments, which we could avoid. Is this practice an influence from other progra...

Matching a datetime against a date in django

I have a django model with a DateTimeField called when which I want to match against a Date object. Is there a way to do that in django's queryset language better than Samples.objects.filter( when__gte = mydate, when__lt = mydate + datetime.timedelta(1) ) ...

How to Append data to a combobox

I need to insert data to a combobox so i do an append as defined in this lines : fd = open(files,'rb') data=fd.readlines() for i in data[]: item=i.strip() if item is not None: combobox.Append(item) fd.close Even data insert the selection still void please can you tell me how to set a selection a value from the items read...

Python 2.7, sqlite3, ValueError: could not convert BLOB to buffer

I am trying to save a BLOB created from an integer array (that is, a packed array of integers) in an SQLite DB. The script shown below gives the following traceback. As far as I can see from the Python 2.7 sqlite3 documentation, it should be possible to insert a buffer object into a table, where it is supposed to be saved as a BLOB. Howe...

sqlalchemy many-to-many, but inverse?

I'm sorry if inverse is not the preferred nomenclature, which may have hindered my searching. In any case, I'm dealing with two sqlalchemy declarative classes, which is a many-to-many relationship. The first is Account, and the second is Collection. Users "purchase" collections, but I want to show the first 10 collections the user hasn't...

Django template: Why block in included template can't be overwritten by child template?

To illustrate my question more clearly, let's suppose I have a include.html template with content: {% block test_block %}This is include{% endblock %} I have another template called parent.html with content like this: This is parent {% include "include.html" %} Now I create a templated called child.html that extends parent.html: ...

How do you generate xml from non string data types using minidom?

How do you generate xml from non string data types using minidom? I have a feeling someone is going to tell me to generate strings before hand, but this is not what I'm after. from datetime import datetime from xml.dom.minidom import Document num = "1109" bool = "false" time = "2010-06-24T14:44:46.000" doc = Document() Submission = d...

Get active window title in X

I'm trying to get the title of the active window. The application is a background task so if the user has Eclipse open the function returns "Eclipse - blabla", so it's not getting the window title of my own window. I'm developing this in Python 2.6 using PyQt4. My current solution, borrowed and slightly modified from an old answer here ...

Adding methods to a dbus object in python

I need to create a dbus object in python with method names that are decided at runtime. The code I've tried is basically this: import dbus import dbus.service from dbus.mainloop.glib import DBusGMainLoop import gobject DBusGMainLoop(set_as_default=True) gobject.threads_init() class greg(dbus.service.Object): def __init__(self...

Keyboard/Mouse hooks in python in X

What's a good way of getting keyboard/mouse events in Python in X? I don't the actual character, just the event that any key or mouse button has been pressed. I'm developing this using Python 2.6 and PyQt4. On windows pyHook was dead easy to get going, seems harder to get something working on Linux. I read an old answer about XGrabKeyb...

How to extract PDF fields from a filled out form in Python?

I'm trying to use python to processes some PDF forms that were filled out and signed using acrobat reader. I've tried: The pdfminer demo and it didn't dump any of the filled out data. pyPdf, it maxed a core for 2 min when I tried to load the file with PdfFileReader(f) and I just gave up and killed it. Jython and PDFBox, got that worki...

DJango Dev Server strange output ppcfinder.net/judge.php

Hi all, I wonder if anyone has seen this. I am developing a web app and the dev server just output the following when I was doing some testing. logging on [21/Oct/2010 13:42:56] "POST /members/logon/ HTTP/1.1" 302 0 [21/Oct/2010 13:42:57] "GET / HTTP/1.1" 200 20572 [21/Oct/2010 13:42:59] "GET http://ppcfinder.net/judge.php HTTP/1.1" 40...

Java framework for social network

Is there a Java analogue to Pinax/Django? (Perhaps an extension to Jboss Seam and/or functionality already built into Seam?) Please analyse and compare Pinax/Django, Seam, and any other good Java/Python frameworks in the following criteria (ranked in order of importance): Security (sensitive financial information) Ability to interact ...

using python to encapsulate part of a string after 3 commas

I am trying to create a python script that adds quotations around part of a string, after 3 commas So if the input data looks like this: 1234,1,1/1/2010,This is a test. One, two, three. I want python to convert the string to: 1234,1,1/1/2010,"This is a test. One, two, three." The quotes will always need to be added after 3 commas ...

Parsing a list into a url string

I have a list of tags that I would like to add to a url string, separated by commas ('%2C'). How can I do this ? I was trying : >>> tags_list ['tag1', ' tag2'] >>> parse_string = "http://www.google.pl/search?q=%s&amp;restofurl" % (lambda x: "%s," %x for x in tags_list) but received a generator : >>> parse_string 'http://&lt;generator...

How to stop Python SUDS 0.4 library including unused fields?

I'm using the Python SUDS library to talk with ExactTarget. I've been using various versions (SUDS 0.3.6 under Python 2.5, SUDS 0.3.8 and 0.4 under Python 2.6) and each version seems to have its own little quibbles. I'd prefer using 0.4 (since it's the latest and seems to have good caching), but I'm getting an error with my web service ...

Python: use regular expression to remove the white space each line

^(\s+) only removes the whitespace from frist line, how to remove the front whitespace from all the lines? ...

Detecting when the ball hits the paddle in pong?

I have been reading through this tutorial. I am in a chapter where you can build a pong game. However I am having trouble trying to make a function that can detect when the ball hits the paddle? It says I should do this: Make a boolean function hit(bx, by, r, px, py, h) that returns True when the vertical coordinate of the ball (by)...