python

Suppose I have 2 vectors. What algorithms can I use to compare them?

Company 1 has this vector: ['books','video','photography','food','toothpaste','burgers'] ... ... Company 2 has this vector: ['video','processor','photography','LCD','power supply', 'books'] ... ... Suppose this is a frequency distribution (I could make it a tuple but too much to type). As you can see...these vectors have things tha...

Calculate percent at runtime

I have this problem where I have to "audit" a percent of my transtactions. If percent is 100 I have to audit them all, if is 0 I have to skip them all and if 50% I have to review the half etc. The problem ( or the opportunity ) is that I have to perform the check at runtime. What I tried was: audit = 100/percent So if percent is...

Scrapy BaseSpider: How does it work?

This is the BaseSpider example from the Scrapy tutorial: from scrapy.spider import BaseSpider from scrapy.selector import HtmlXPathSelector from dmoz.items import DmozItem class DmozSpider(BaseSpider): domain_name = "dmoz.org" start_urls = [ "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", "http...

mechanize python click a button

I have a form with <input type="button" name="submit" /> button and would like to be able to click it. I have tried mech.form.click("submit") but that gives the following error: ControlNotFoundError: no control matching kind 'clickable', id 'submit' mech.submit() also doesn't work since its type is button and not submit. Any ideas? ...

Convert fraction to float?

Kind of like this question, but in reverse. Given a string like 1, 1/2, or 1 2/3, what's the best way to convert it into a float? I'm thinking about using regexes on a case-by-case basis, but perhaps someone knows of a better way, or a pre-existing solution. I was hoping I could just use eval, but I think the 3rd case prevents that. ...

"Permission Denied" in Django template using Djapian

I've followed the Djapian tutorial and setup everything "by the book" so that the indexshell commandline supplied by Djapian shows successful queries. However, when integrating the sample search from the Djapian tutorial I get this nonsense error: TemplateSyntaxError at /search/ Caught an exception while rendering: (13, 'Permis...

Navigation menu using Django templates

Trying to work with a trivial navigation menu using django templates, I'm having trouble setting the current class on a particular menu item. Here's my base template: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; <html xmlns="http://www.w3.org/1999/xhtml"&gt; ...

Alternatives to FontForge

I use python bindings for FontForge under Ubuntu. It constantly runs into crash without any clues about the reason, e.g. segmentation fault, memory mapping errors, etc. All what I need is to read font file's (.ttf and .otf) meta data (font name, family name, version, unique id, copyright, license, designer, designer url, etc) and count ...

python: how can i avoid " list index out of range" in this simple while loop

my L1 array contains numbers like 0.029999999999999999 which i want to print off as 0.03 my code works, but gives an error at the end because the last count is out of range. i understand why it breaks, but dont know how to fix it. thanks count = 1 while L1: print "%.2f" %L1[count] count = count + 1 ...

GUI designer for managing Django models

Is there a GUI tool using which I can design new Django models graphically? For example, drawing lines between fields in different models to indicate a foreign key. Modifying existing models graphically would also be nice. ...

Grouping by Nested Object Keys in MongoDB

with mongodb, is it possible to group results by a key found in an array of objects in a list? for example, lets say i have a table of survey responses (survey_responses), and each entry represents a single response. one or more of the questions in the survey is a multiple choice, so the answers stored could resemble: survey_responses....

Python: urllib2 multipart/form-data and proxies

The Objective: A script which cycles through a list of proxies and sends a post request, containing a file to a PHP page on my server, which then calculates delivery time. It's a pretty useless script, but I am using it to teach myself about urllib2. The Problem: So far I have got multipart/form-data sending correctly using Poster, but ...

Read from source.sql write to destiantion.sql with python script ?

I Have a file source.sql INSERT INTO `Tbl_ABC` VALUES (1, 0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, 0) INSERT INTO `Tbl_ABC` VALUES (2, 12, '3D STRUCTURES', '3D STRUCTURES', NULL, NULL, 0) INSERT INTO `Tbl_ABC` VALUES (2, 0, '2 STRUCTURES', '2D STRUCTURES', NULL, NULL, 0) INSERT INTO `Tbl_ABC` VALUES (2, 111, '2D STRUCTURES', '3D STRU...

How to find out the nested class hierarchy?

I have a python code like this. File named mymodule.py class MyBase(object): pass File named data.py from mymodule import MyBase class A: class NestA(MyBase): pass class NestB(MyBase): pass class B: class NestA(MyBase): pass class NestB(MyBase): pass if I have a = A.NestA (not ...

Why do I need to save this model before adding it to another one?

In django, I'm trying to do something like this: # if form is valid ... article = form.save(commit=False) article.author = req.user product_name = form.cleaned_data['product_name'] try: article.product = Component.objects.get(name=product_name) except: article.product = Component(name=product_name) article.save() # do some more form...

Scrapy spider is not working

Since nothing so far is working I started a new project with python scrapy-ctl.py startproject Nu I followed the tutorial exactly, and created the folders, and a new spider from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.selector import HtmlXPathSelect...

Initialize a list of objects in Python

Hi all, I'm a looking to initialize an array/list of objects that are not empty -- the class constructor generates data. In C++ and Java I would do something like this: Object lst = new Object[100]; I've dug around, but is there a Pythonic way to get this done? This doesn't work like I thought it would(I have 100 references to the s...

Extract files with invalid characters in filename with Python

I use python's zipfile module to extract a .zip archive (Let's take this file at http://img.dafont.com/dl/?f=akvaleir for example.) f = zipfile.ZipFile('akvaleir.zip', 'r') for fileinfo in f.infolist(): print fileinfo.filename f.extract(fileinfo, '.') Its output: Akval�ir_Normal_v2007.ttf Akval�ir, La police - The Font - Fr -...

How to wait for messages on multiple queues using py-amqplib

I'm using py-amqplib to access RabbitMQ in Python. The application receives requests to listen on certain MQ topics from time to time. The first time it receives such a request it creates an AMQP connection and a channel and starts a new thread to listen for messages: connection = amqp.Connection(host = host, userid = "guest", pass...

Open a second window in PyQt

I'm trying to use pyqt to show a custom QDialog window when a button on a QMainWindow is clicked. I keep getting the following error: $ python main.py DEBUG: Launch edit window Traceback (most recent call last): File "/home/james/Dropbox/Database/qt/ui_med.py", line 23, in launchEditWindow dialog = Ui_Dialog(c) File "/home/jam...