python

How to encode HTML non-ASCII data to UTF-8 in Python

I tried to do that, and I found this errors: >>> import re >>> x = 'Ingl\xeas' >>> x 'Ingl\xeas' >>> print x Ingl�s >>> x.decode('utf8') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(...

In Django, what is the best way to manage both a mobile and desktop site?

I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates. Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site. ...

import strategy whithin django applications

I would like to know what is the best import strategy whithin django reusable applications. Say I have an application called usefulapp. Inside my app, I will need to access, say, the models. Should I use an explicit import as: import usefulapp.models or simply, since I am inside this very app, I could use: import models Which one ...

Is the content between anchor tags (a) in html seen as a branch in lxml?

I am trying to get some content in html documents. Some of the documents have a table of contents that very nicely indicates where in the document the content I want to strip out is located. That is either the value or text_content of the tag are easily identifiable and point to what I need. For example I might have two anchor tags in...

How to initialize a two-dimensional array in Python?

I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this: def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): for j in range (0, 10): new.append(foo) twod_list.append(new) ...

Try to install Pippy (python) on my Palm centro mobile

I just started to learn Python few days ago, which is also my first programming language, I'm so excited about it, just loving it, thinking about it any minute. in order to spend more time practice my skills, get familiar with the syntax, I'm seeking if it's possible to setup python environment on my mobile, so that i can program while ...

Web scraping with Python

I'm currently trying to scrape a website that has fairly poorly-formatted HTML (often missing closing tags, no use of classes or ids so it's incredibly difficult to go straight to the element you want, etc.). I've been using BeautifulSoup with some success so far but every once and a while (though quite rarely), I run into a page where ...

Django: using variables as array indices?

I am trying to create a template that will put items in a table. Controller: items = Item.all().order('name').fetch(10) template_values = {'items': items, 'headers': ['Name', 'Price', 'Quantity']} render('Views/table.html', self, template_values) Template: <table> <tr> {% for header in headers...

Web Server frameworks for Python Web Apps

Hi I'd like to get suggestions on the best way to serve python scripts up as web pages. Typically I'd like a way for me and my colleagues to write simple web pages with minimal effort ie we focus on the business logic eg creating simple forms etc. Possibly with some way to manage sessions but this is a nice-to-have. It doesn't have to b...

How to have multiple python programs append rows to the same file?

I've got multiple python processes (typically 1 per core) transforming large volumes of data that they are each reading from dedicated sources, and writing to a single output file that each opened in append mode. Is this a safe way for these programs to work? Because of the tight performance requirements and large data volumes I don't ...

What's the easiest way to convert a list of hex byte strings to a list of hex integers?

I have a list of hex bytes strings like this ['BB', 'A7', 'F6', '9E'] (as read from a text file) How do I convert that list to this format? [0xBB, 0xA7, 0xF6, 0x9E] ...

How can I create an array/list of dictionaries in python?

I have a dictionary as follows: {'A':0,'C':0,'G':0,'T':0} I want to create an array with many dictionaries in it, as follows: [{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] This is my code: weightMatrix = [] for k in range(motifWidth): weightMatrix[k] = {'A':0,'C':0,...

excluding fields from json serialization in python using jsonpickle

I am using jsonpickle to serialize an object to json. The object has certain fields that point to other objects. I'd like to selectively not include those in the serialization, so that the resulting json file is essentially pure human-readable text without any funny representations of objects. Is there a way to make jsonpickle ignore cer...

How can I show figures separately in matplotlib?

Say that I have two figures in matplotlib, with one plot per figure: import matplotlib.pyplot as plt f1 = plt.figure() plt.plot(range(0,10)) f2 = plt.figure() plt.plot(range(10,20)) Then I show both in one shot plt.show() Is there a way to show them separately, i.e. to show just f1? Or better: how can I manage the figures separat...

Using PyQt4 - QTableView with SQLAlchemy using QSqlTableModel (or not)

Hi everyone, i'm starting to learn Qt for python and as i was wondering after reading this post : qt - pyqt QTableView not populating when changing databases. if there was a way to use SQLAlchemy sessions instead of (re-)opening a database connection as a Table Model with Qt's QTableView widget. Something that would work a little bit l...

Why is my Python OpenGL render2DTexture function so slow?

SOLVED: The problem was actually using time.time() every CPU cycle to see whether the next frame should be drawn or not. The time it takes to execute time.time() was having an impact on the FPS. I made this function for drawing 2D textures as images in a 2D view in my OpenGL application. After doing some testing I found that it takes up...

How to use HTTP method DELETE on Google App Engine?

I can use this verb in the Python Windows SDK. But not in production. Why? What am I doing wrong? The error message includes (only seen via firebug or fiddler) Malformed request or something like that My code looks like: from google.appengine.ext import db from google.appengine.ext import webapp class Handler(webapp.RequestHand...

saving and loading objects from file using jsonpickle

I have the following simple methods for writing a python object to a file using jsonpickle: def json_serialize(obj, filename, use_jsonpickle=True): f = open(filename, 'w') if use_jsonpickle: import jsonpickle json_obj = jsonpickle.encode(obj) f.write(json_obj) else: simplejson.dump(obj, f) ...

using os.system in python to run program with parameters

How do I get python to run sudo openvpn --cd /etc/openvpn --config client.ovpn I'm trying the following at the minute without success vpnfile2 = '/etc/init.d/openvpn' cfgFile = 'client.ovpn' os.system('sudo \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"') ...

Test if value is Decimal

In some Python (v3) code I am creating lists of Decimals from user input, like this: input = [] # later populated with strings by user with values like '1.45984000E+001' decimals = [Decimal(c) for c in input] However, sometimes the input list contains strings that cannot be parsed. How can I test if c can be represented as a decimal b...