python

Backslashes being added into my cookie in Python

Hey, I am working with Python's SimpleCookie and I ran into this problem and I am not sure if it is something with my syntax or what. Also, this is classwork for my Python class so it is meant to teach about Python so this is far from the way I would do this in the real world. Anyway, so basically I am keeping information input into ...

[Django Templates] 'for' loop through form fields and excluding one of the fields with 'if'.

Hi, The problem I'm struggling with is as follows: I have: {% for field in form %} {{ field }} {% end for %} What I want is to put an 'if' statement to exclude a field which .label or whatever is provided. Like: {% for field in form%} {% if field == title %} {% else %} {{ field }} {% endif %} {% endfor %} I...

case-insensitive alphabetical sorting of nested lists

i'm trying to sort this nested list by inner's list first element: ak = [ ['a',1],['E',2],['C',13],['A',11],['b',9] ] ak.sort(cmp=lambda x, y: cmp(x[0], y[0])) for i in ak: { print i } by default python considers A > a, hence the output i get is: ['A', 11] ['C', 13] ['E', 2] ['a', 1] ['b', 9] i've tried converting all list values ...

Python 3.0 Windows/COM

How to access a COM object from a python file using python 3.0. And, yes, I know that not a lot of people are using Python 3.0. Switching back to 2.6 is a huge hassle for me, so I don't want to unless I absolutely have to. I appreciate your time, and any assistance! ...

'import module' or 'from module import'

I've tried to find a comprehensive guide on whether it is best to use 'import module' or 'from module import'. I've just started with Python, with the intention for developing web applications with Django, and I'm trying to start off with best practices in mind. Basically, I was hoping if anyone could share their experiences, what prefe...

Checking python module version at runtime

Many 3rd party python modules have an attribute which holds the version info for the module (usually something like module.VERSION or module.__version__), however some do not. Particular examples of such modules are libxslt and libxml2. I need to check that the correct version of these modules are being used at runtime. Is there a way ...

How to deliver instance of object to instance of SocketServer.BaseRequestHandler?

This is problem. My primary work is : deliver "s" object to "handle" method in TestRequestHandler class. My first step was : deliver "s" object through "point" method to TestServer class, but here im stuck. How to deliver "s" object to TestRequestHandler? Some suggestions? import threading import SocketServer from socket import * clas...

calling execfile() in custom namespace executes code in '__builtin__' namespace

When I call execfile without passing the globals or locals arguments it creates objects in the current namespace, but if I call execfile and specify a dict for globals (and/or locals), it creates objects in the __builtin__ namespace. Take the following example: # exec.py def myfunc(): print 'myfunc created in %s namespace' % __nam...

Using Urllib with TOR

How can I route urllib requests through the TOR network? I have not been able to find any decent examples on the internet, can anyone help me? ...

Changing element value with BeautifulSoup returns empty element.

from BeautifulSoup import BeautifulStoneSoup xml_data = """ <doc> <test>test</test> <foo:bar>Hello world!</foo:bar> </doc> """ soup = BeautifulStoneSoup(xml_data) print soup.prettify() make = soup.find('foo:bar') print make # prints <foo:bar>Hello world!</foo:bar> make.contents = ['Top of the world Ma!'] print make # prints <foo:b...

generating plural forms into a .pot file

I'm internationalizing a python program and cant get plural forms into the .pot file. I have marked string that require plural translations with a _pl() eg. self.write_info(_pl("%(num)d track checked", "%(num)d tracks checked", song_obj.song_count) % {"num" : song_obj.song_count}) Then I'm running: xgettext --lang...

Python naming conventions for modules

I have a module whose purpose is to define a class called "nib". (and a few related classes too.) How should I call the module itself? "nib"? "nibmodule"? Anything else? ...

List all Tests Found by Nosetest

I use nosetests to run my unittests and it works well. I want to get a list of all the tests nostests finds without actually running them. Is there a way to do that? ...

Barchart sizing of text & barwidth with matplotlib - python

I'm creating a bar chart with matplotlib-0.91 (for the first time) but the y axis labels are being cut off. If I increase the width of the figure enough they eventually show up completely but then the output is not the correct size. Any way to deal with this? ...

Is there a good python module that does HTML encoding/escaping in C?

There is cgi.escape but that appears to be implemented in pure python. It seems like most frameworks like Django also just run some regular expressions. This is something we do a lot, so it would be good to have it be as fast as possible. Maybe C implementations wouldn't be much faster than a series of regexes for this? ...

Going nuts with executing pyton script via crontab on debian!

This is what my crontab file looks like: * * * * * root /usr/bin/python /root/test.py >> /root/classwatch.log 2>&1 This is what my python script looks like: #!/usr/bin/python print "hello" The cronjob creates the log file. But it is empty. I am also pretty certain that the python file is not being executed. Appreciate any help! I...

How to work around needing to update a dictionary

I need to delete a k/v pair from a dictionary in a loop. After getting RuntimeError: dictionary changed size during iteration I pickled the dictionary after deleting the k/v and in one of the outer loops I try to reopen the newly pickled/updated dictionary. However, as many of you will probably know-I get the same error-I think when it...

Is it possible to get a timezone in python given a utc timestamp and a utc offset?

I have data that is the utc offset and the utc time, given that is it possible to get the users local timezone (mainly to figure if it is dst etc. probably using pytz), similar to the function in php timezone_name_from_abbr ? For example: If my epoch time is: 1238720309 I can get the utc time as: d = datetime.utcfromtim...

Interpreting Number Ranges in Python

In a Pylons webapp, I need to take a string such as "<3, 45, 46, 48-51, 77" and create a list of ints (which are actually IDs of objects) to search on. Any suggestions on ways to do this? I'm new to Python, and I haven't found anything out there that helps with this kind of thing. The list would be: [1, 2, 3, 45, 46, 48, 49, 50, 51, 7...

Using CSV as a mutable database?

Yes, this is as stupid a situation as it sounds like. Due to some extremely annoying hosting restrictions and unresponsive tech support, I have to use a CSV file as a database. While I can use MySQL with PHP, I can't use it with the Python backend of my program because of install issues with the host. I can't use SQLite with PHP bec...