python

Python re question - sub challenge

I want to add a href links to all words prefixed with # or ! or @ If this is the text Check the #bamboo and contact @Fred re #bamboo #garden should be converted to: Check the <a href="/what/bamboo">#bamboo</a> and contact <a href="/who/fred">@Fred</a> re <a href="/what/bamboo">#bamboo</a> <a href="/what/garden">#garden</a> Note that...

How to map one class against multiple tables with SQLAlchemy?

Lets say that I have a database structure with three tables that look like this: items - item_id - item_handle attributes - attribute_id - attribute_name item_attributes - item_attribute_id - item_id - attribute_id - attribute_value I would like to be able to do this in SQLAlchemy: item = Item('item1') item.foo = 'bar' ses...

Best Python Library for Downloading and Extracting Addresses

Hello I've just been given a project which involves the following steps Grab an email from a POP3 address Open an attachment from the email Extract the To: email address Add this to a global suppression list I'd like to try and do this in Python even though I could it in PHP in half the time (this is because I dont know anywhere ne...

Python - substr

Hi I need to be able to get the last digit of a number. i.e., I need 2 to be returned from: 12. Like this in PHP: $minute = substr(date('i'), -1) but I need this in Python. Any ideas ...

Multidimensional list(array) reassignment problem

Good day coders and codereses, I am writing a piece of code that goes through a pile of statistical data and returns what I ask from it. To complete its task the method reads from one multidimensional array and writes into another one. The piece of code giving me problems is: writer.variables[variable][:, :, :, :] = reader.variables[va...

Writing Python/Django view to "join" across three models/tables

Hi to all! Just begin my Python/Django experience and i have a problem :-) So i have a model.py like this: from django.db import models class Priority(models.Model): name = models.CharField(max_length=100) class Projects(models.Model): name = models.CharField(max_length=30) description = models.CharField(max_length=150) ...

Hierarchical data output from App Engine Datastore to JSON?

I have a large hierarchical dataset in the App Engine Datastore. The hierarchy is preserved by storing the data in Entity groups, so that I can pull a whole tree by simply knowing the top element key like so: query = db.Query().ancestor(db.get(key)) The question: How do I now output this data as JSON and preserve the hierarchy? Googl...

[PyQt4] Load blob image data into QPixmap

Hi, I am writing a program using PyQt4 for front-end GUI and this program accesses a back-end database (which can be either MySQL or SQLite). I need to store some image data in the database and below is the Python code I use to import image files (in JPEG format) to a blob data field in the database: def dump_image(imgfile): i = o...

mod_python problem?

I have been working on a website using mod_python, python, and SQL Alchemy when I ran into a strange problem: When I query the database for all of the records, it returns the correct result set; however, when I refresh the page, it returns me a result set with that same result set appended to it. I get more result sets "stacked" on top o...

Fastest ways to key-wise add a list of dicts together in python

Say I have a bunch of dictionaries a = {'x': 1.0, 'y': 0.5, 'z': 0.25 } b = {'w': 0.5, 'x': 0.2 } There's only two there, but the question is regarding an arbitary amount. What's the fastest way to find the mean value for each key? The dicts are quite sparse, so there will be a lot of cases where lots of keys aren't present in vario...

How to sort digits in a number?

I'm trying to make an easy script in Python which takes a number and saves in a variable, sorting the digits in ascending and descending orders and saving both in separate variables. Implementing Kaprekar's constant. It's probably a pretty noobish question. But I'm new to this and I couldn't find anything on Google that could help me. A...

unable to import libxml2mod from the python script

File "/usr/local/lib/python2.5/site-packages/libxml2.py", line 1, in <module> import libxml2mod ImportError: /usr/local/lib/python2.5/site-packages/libxml2mod.so: undefined symbol:xmlTextReaderSetup >>> import libxml2mod >>> import libxml2 >>> on Python Prompt it works fine !! can anyone has idea why my program is not wor...

What regex can I use to capture groups from this string?

Assume the following strings: A01B100 A01.B100 A01 A01............................B100 ( whatever between A and B ) The thing is, the numbers should be \d+, and in all of the strings A will always be present, while B may not. A will always be followed by one or more digits, and so will B, if present. What regex could I use to capture...

How can I make a Django query for the first occurrence of a foreign key in a column?

Basically, I have a table with a bunch of foreign keys and I'm trying to query only the first occurrence of a particular key by the "created" field. Using the Blog/Entry example, if the Entry model has a foreign key to Blog and a foreign key to User, then how can I construct a query to select all Entries in which a particular User has wr...

Python2.6 xmpp Jabber Error

I am using xmpp with python and I want create a simple client to communicate with a gmail id. #!/usr/bin/python import xmpp login = 'Your.Login' # @gmail.com pwd = 'YourPassword' cnx = xmpp.Client('gmail.com') cnx.connect( server=('talk.google.com',5223) ) cnx.auth(login,pwd, 'botty') cnx.send( xmpp.Message( "[email protected]...

C# equivalent of python slice operation

In python, I can take a list my_list and rotate the contents: >>> my_list = list(range(10)) >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> new_list = my_list[1:] + my_list[:1] >>> new_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] What's the equivalent way in C# to create a new list that is a made up of two slices of an existing C# list? I know ...

The meaning of a single- and a double-underscore before an object name in Python

I want to clear this up once and for all. Can someone please explain the exact meaning of having leading underscores before an object's name in Python? Also explain the difference between a single and a double leading underscore. Also, does that meaning stay the same whether the object in question is a variable, a function, a method, etc...

Python IDE on Linux Console

This may sound strange, but I need a better way to build python scripts than opening a file with nano/vi, change something, quit the editor, and type in python script.py, over and over again. I need to build the script on a webserver without any gui. Any ideas how can I improve my workflow? ...

Python: Why are some of Queue.queue's method "unreliable"?

In the queue class from the Queue module, there are a few methods, namely, qsize, empty and full, whose documentation claims they are "not reliable". What exactly is not reliable about them? I did notice that on the Python docs site, the following is said about qsize: Note, qsize() > 0 doesn’t guarantee that a subsequent get() wi...

Has anybody tried html2pdf in django?

Ok when im gonna make reports with Java I use iReport for JasperReports Template designs. But with python the alternative is html2pdf - pisa. It would be great to see an example of this. Any hint would be appreciated. Tks. ...