python

Python NLTK code snippet to train a classifier (naive bayes) using feature frequency

Hello, I was wondering if anyone could help me through a code snippet that demonstrates how to train Naive Bayes classifier using a feature frequency method as opposed to feature presence. I presume the below as shown in Chap 6 link text refers to creating a featureset using Feature Presence (FP) - def document_features(document): ...

Modpython and virtualenv

Is it any way to run django site on virtualenv without administration rights? How can I do it? Virtualenv is already installed. ...

Why do I have to press Ctrl+D twice to close stdin?

I have the following python script that reads numbers and outputs an error if the input is not a number. import fileinput import sys for line in (txt.strip() for txt in fileinput.input()): if not line.isdigit(): sys.stderr.write("ERROR: not a number: %s\n" % line) If I get the input from stdin, I have to press Ctrl+D twice...

Python order of execution

I was wondering if Python has similar issues as C regarding the order of execution of certain elements of code. For example, I know in C there are times say when it's not guaranteed that some variable is initialized before another. Or just because one line of code is above another it's not guaranteed that it is implemented before all th...

Signal handling in Python

In my program I have a bunch of threads running and I'm trying to interrupt the main thread to get it to do something asynchronously. So I set up a handler and send the main process a SIGUSR1 - see the code below: def SigUSR1Handler(signum, frame): self._logger.debug('Received SIGUSR1') return signal.signal(signal.SIGUSR1, Si...

Timeout when using urllib2.urlopen with Django in GAE

When I run this code url = ('http://maps.google.com/maps/nav?'+ 'q=from%3A'+from_address+ '+to%3A'+to_address+ '&output=json&oe=utf8&key='+api_key) request = urllib2.Request(url) response = urllib2.urlopen(request) In a simple view in Django running in google app engine via the Google App Engine Helper for Django ...

Python path in environment.

I want to call a python script from batch script, but I dont want to hard-code path to python executable (python.exe) in my calling script. e.g. c:\python26\python.exe test.py $PYTHONPATH\python.exe test.py Is there any way to have PYTHONPATH like setting ? ...

Python Pexpect getting all output from child

I am trying to get all output from child process in real time. I use spawn for creating child process but I am confused what I should send for pattern parameter in expect function. Actually I don't understand why it's not an optional parameter. I don't know if I should use send(), child doesn't need input. I'm just trying to write outpu...

Running external subprocesses and reading return code

I'm creating a python script to sort a lot of images (game screenshots). I found a way to do that in imagemagick : I know that, if a specific square of the image is the same as the reference crop, then the image is of category one. If not, I check for another crop and another category, and if that doesn't fit either, I put the image in ...

subprocess loops forever

I'm trying to start/stop rsyslog through a python script: RSYSLOG_INIT_SCRIPT='/etc/init.s/rsyslogd' subprocess.call([RSYSLOG_INIT_SCRIPT,'stop']) /etc/init.d/rsyslogd is a regular init script. The problem is that it continues executing this script again and again. (I've added an echo to the script to confirm this). This is the stackt...

How can I run a loop against 2 random elements from a list at a time?

Let's say I have a list in python with several strings in it. I do not know the size. How can I run a loop to do an operation on 2 random elements of this string? What if I wanted to favour a certain subset of the strings in this randomization, to be selected more often, but still make it possible for them to not be chosen? ...

Check if a file is setuid root in Python

Hi there, I'm trying to check if a file has the setuid bit in Python. The stat doc mentions a S_ISUID function but it only works with os.chmod(), not to actually read the setuid bit. It also lists S_IMODE, but I have no idea how to interpret it. How can I easily check if a file as the setuid root bit set? ...

Can I write browser plugins with Python?

Hi, I'm thinking about writing a browser plugin, but I don't know any C. Can I write browser plugins with Java or Python? I was thinking... All those websites store cookies on my browser to identify me. If I wrote a plugin that would supply a browser GUID in the http headers, webservers could identify the browser. I think that would r...

Best way to make Django's login_required the default

I'm working on a large Django app, the vast majority of which requires a login to access. This means that all throughout our app we've sprinkled: @login_required def view(...): That's fine, and it works great as long as we remember to add it everywhere! Sadly sometimes we forget, and the failure often isn't terribly evident. If the...

Multiple constructors in python?

Possible Duplicate: What is a clean, pythonic way to have multiple constructors in Python? Is it not possible to define multiple constructors in Python, with different signatures? If not, what's the general way of getting around it? For example, let's say you wanted to define a class City I'd like to be able to say someCity...

How to get progress of os.walk in python?

I have a piece of code which I'm using to search for the executables of game files and returning the directories. I would really like to get some sort of progress indicator as to how far along os.walk is. How would I accomplish such a thing? I tried doing startpt = root.count(os.sep) and gauging off of that but that just gives how dee...

Reprojecting polar to cartesian grid

I have a polar (r,theta) grid (which means that each cell is an annulus section) containing values of some physical quantity (e.g. temperature), and I would like to re-grid (or re-project) these values onto a cartesian grid. Are there any Python packages that can do this? I am not interested in converting the coordinates of the centers ...

In python django how do you print out an object's inrospection? The list of all public methods of that object (variable and/or functions)?

In python django how do you print out an object's inrospection? The list of all public methods of that object (variable and/or functions)? e.g.: def Factotum(models.Model): id_ref = models.IntegerField() def calculateSeniorityFactor(): return (1000 - id_ref) * 1000 I want to be able to run a command line in the Django shel...

Am I passing the string correctly to the python library?

I'm using a python library called Guess Language: http://pypi.python.org/pypi/guess-language/0.1 "justwords" is a string with unicode text. I stick it in the package, but it always returns English, even though the web page is in Japanese. Does anyone know why? Am I not encoding correctly? §ç©ºéå ¶ä»æ¡å°±æ²æéç¨®å¾ ...

Replacing one character of a string in python

In python, are strings mutable? The line someString[3] = "a" throws the error TypeError: 'str' object does not support item assignment I can see why (as I could have written someString[3] = "test" and that would obviously be illegal) but is there a method to do this in python? ...