python

How do I translate Ruby's IO.popen calls into Python's subprocess.Popen calls?

I've read the documentation and I've tried lots of things in the REPL, and Googled, but I can't for the life of me understand how subprocess.Popen works in Python. Here is some Ruby code I am using: IO.popen("some-process") do |io| while(line = io.gets) # do whatever with line end end How do I translate this into Python using...

Matplotlib canvas drawing

Let's say I define a few functions to do certain matplotlib actions, such as def dostuff(ax): ax.scatter([0.],[0.]) Now if I launch ipython, I can load these functions and start a new figure: In [1]: import matplotlib.pyplot as mpl In [2]: fig = mpl.figure() In [3]: ax = fig.add_subplot(1,1,1) In [4]: run functions # run the f...

CentOS: make python 2.6 see django

In a harrowing attempt to get mod_wsgi to run on CentOS 5.4, I've added python 2.6 as an optional library following the instructions here. The configuration seems fine except that when trying to ping the server the Apache log prints this error: mod_wsgi (pid=20033, process='otalo', application='127.0.0.1|'): Loading WSGI script '...

Convert binary information to regular data type without outside modules in python.

Hello World. I'm tasked with reading a poorly formatted binary file and taking in the variables. Although I need to do it in C++ (ROOT, specifically), I've decided to do it in python because python makes sense to me, but my plan is to get it working in python and then tackle re-writing in in C++, so using easy to use python modules won't...

Hide deprecated methods from tab completion

I would like to control which methods appear when a user uses tab-completion on a custom object in ipython - in particular, I want to hide functions that I have deprecated. I still want these methods to be callable, but I don't want users to see them and start using them if they are inspecting the object. Is this something that is possib...

Apply relative URL to an absolute URL

I have an absolute URL, and the URL that a link on that page points to. Is there a builtin function to apply a relative URL to an absolute URL? Ie. "http://example.com/some/url", "/some/url/I/want/to/go/to" => "http://example.com/some/url/I/want/to/go/to" ...

I am having issues with django test

I have this test case def setUp(self): self.user = User.objects.create(username="tauri", password='gaul') def test_loginin_student_control_panel(self): c = Client() c.login(username="tauri", password="gaul") response = c.get('/student/') self.assertEqual(response.status_code, 200) the view associated with the test cas...

In Python, what are some examples of when decorators greatly simplify a task?

Trying to find examples of when decorators might be really beneficial, and when not so much. Sample code is appreciated. ...

Key word extraction in Python

I'm building a website in django that needs to extract key words from short (twitter-like) messages. I've looked at packages like topia.textextract and nltk - but both seem to be overkill for what I need to do. All I need to do is filter words like "and", "or", "not" while keeping nouns and verbs that aren't conjunctives or other parts ...

Multiple Unpacking Assignment in Python when you don't know the sequence length

The textbook examples of multiple unpacking assignment are something like: import numpy as NP M = NP.arange(5) a, b, c, d, e = M # so of course, a = 0, b = 1, etc. M = NP.arange(20).reshape(5, 4) # numpy 5x4 array a, b, c, d, e = M # here, a = M[0,:], b = M[1,:], etc. (ie, a single row of M is assigned each to a through e) (My Q ...

How can I catch for a connection timeout error in Python's SMTPlib?

The error being thrown is: error: [Errno 110] Connection timed out I'm not sure what to except for? try: smtpObj = smtplib.SMTP('smtp.example.com') smtpObj.starttls() smtpObj.login('user','pass') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except smtplib...

How should I do custom sort in Python 3?

In Python 2.x, I could pass custom function to sorted and .sort functions >>> x=['kar','htar','har','ar'] >>> >>> sorted(x) ['ar', 'har', 'htar', 'kar'] >>> >>> sorted(x,cmp=customsort) ['kar', 'htar', 'har', 'ar'] Because, in My language, consonents are comes with this order "k","kh",....,"ht",..."h",...,"a" But In Python 3.x, lo...

Why won't numpy matrix let me print its rows?

Okay this is probably a really dumb question, however it's really starting to hurt. I have a numpy matrix, and basically I print it out row by row. However I want to make each row be formatted and separated properly. >>> arr = numpy.matrix([[x for x in range(5)] for y in range(5)]) >>> arr matrix([[0, 1, 2, 3, 4], [0, 1, 2, 3,...

validate hostname string in Python

Following up to Regular expression to match hostname or IP Address? and using Restrictions on valid host names as a reference, what is the most readable, concise way to match/validate a hostname/fqdn (fully qualified domain name) in Python? I've answered with my attempt below, improvements welcome. ...

What is the best library in python to use to interface with a SQL database?

I am currently processing text/html data and I wish to store my results in some sort of database. My current setup is Pydev with Eclipse. What is the best non-distributed database to use with my current development environment? What is the best library in python to use to interface with suggested database? ...

How to print gantt-charts generated on web using python?

I want to print or save gantt-chart(in pdf format). These charts are generated on web after a particular input. Our chart is a plug-in for Trac. I have used Genshi library to generate charts. ...

unittest tests reuse for family of classes

I have problem organizing my unittest based class test for family of tests. For example assume I implement a "dictionary" interface, and have 5 different implementations want to testing. I do write one test class that tests a dictionary interface. But how can I nicely reuse it to test my all classes? So far I do ugly: DictType = hashta...

In Google's Protocol Buffers, what is a suitable protocol file/model for Exceptions?

Protocol Buffers doesn't have a native Exception type. What would a suitable .proto file for cross-language exceptions look like? ...

motion computation from video using pyglet in python

Hi, I am writing a simple motion detection program but i want it to be cross platform so im using python and the pyglet library since it provides a simple way to load videos in different formats (specially wmv and mpeg). So far i have the code given below which loads the movie and plays it in a window. Now i need to: 1) grab frame at tim...

How to change db in django depending on request.get_host()?

I am creating multisites platform. Anybody can make simple site, with my platform. I plan to use django multidb support. One db for one site. And i need to change db settings depending on request.get_host(). I think that i's not good idea. Prompt other decisions? How it is realised on various designers of sites? ...