python

Setting the encoding for sax parser in Python

When I feed a utf-8 encoded xml to an ExpatParser instance: def test(filename): parser = xml.sax.make_parser() with codecs.open(filename, 'r', encoding='utf-8') as f: for line in f: parser.feed(line) ...I get the following: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "te...

List Element without iteration

Hello everybody, I want to know how to find an element in list without iteration ...

Dead-simple web authentication for a single user

I wrote a small internal web app using (a subset of) pylons. As it turns out, I now need to allow a user to access it from the web. This is not an application that was written to be web facing, and it has a bunch of gaping security holes. What is the simplest way I can make sure this site is securely available to that user, but no one e...

How to save django FileField to user folder?

I've got a model like this def upload_location(instance, filename): return 'validate/%s/builds/%s' % (get_current_user(), filename) class MidletPair(models.Model): jad_file = models.FileField(upload_to = upload_location) jar_file = models.FileField(upload_to = upload_location) upload_to=tempfile.gettempdir() How...

Datetime issue in Django

I am trying to add the datetime object of a person. Whenever the birth year is less than year 1942, I get a strange error DataError: unable to parse time when reading the data back from the DB. class Person(models.Model): """A simple class to hold the person info """ name = models.CharField(max_length=100) born = models....

How to recognize whether a script is running on a tty?

I would like my script to act differently in an interactive shell session and when running with redirected stdout (for example when piped to some other command). How do I recognize which of these two happen in a Python script? Example of such behavior in existing program: grep --color=auto highlights matches when running in interactive...

getopts Values class and Template.Substitute don't (immediately) work together

I have python code something like: from string import Template import optparse def main(): usage = "usage: %prog options outputname" p = optparse.OptionParser(usage) p.add_option('--optiona', '-a', default="") p.add_option('--optionb', '-b', default="") options, arguments = p.parse_args() t = Template('Option a is ${optiona...

How to redirect python warnings to a custom stream?

Let's say I have a file-like object like StreamIO and want the python's warning module write all warning messages to it. How do I do that? ...

About 20 models in 1 django app

I have started work on a local app for myself that runs through the browser. Having recently gone through the django tutorial I'm thinking that it might be better to use django rather than just plain python. There's one problem, I have at least 20 models and each will have many functions. Quite simply it's going to create one huge model...

Getting pdb-style caller information in python

Let's say I have the following method (in a class or a module, I don't think it matters): def someMethod(): pass I'd like to access the caller's state at the time this method is called. traceback.extract_stack just gives me some strings about the call stack. I'd like something like pdb in which I can set a breakpoint in someMeth...

Getting the template name in django template

For debugging purposes, I would like to have a variable in all my templates holding the path of the template being rendered. For example, if a view renders templates/account/logout.html I would like {{ template_name }} to contain the string templates/account/logout.html. I don't want to go and change any views (specially because I'm re...

How do I GROUP BY on every given increment of a field value?

I have a Python application. It has an SQLite database, full of data about things that happen, retrieved by a Web scraper from the Web. This data includes time-date groups, as Unix timestamps, in a column reserved for them. I want to retrieve the names of organisations that did things and count how often they did them, but to do this for...

Can't find my PYTHONPATH

I'm trying to change my PYTHONPATH. I've tried to change it in "My Computer" etc, but it doesn't exist there. I searched in the registry in some places, and even ran a whole search for the word 'PYTHONPATH', but to no avail. However, it Python I can easily see it exists. So where is it? ...

A Python equivilent to Java Robot

Hi, does anyone know of a Python class similar to Java Robot? Specifically I would like the perform a screen grab in Ubuntu, and eventually track mouse clicks and keyboard presses (although that's a slightly different question) Thanks, Stuart ...

whoami in python

Duplicate of: Is there a portable way to get the current username in Python? What is the best way to find out the user that a python process is running under? I could do this: name = os.popen('whoami').read() But that has to start a whole new process. os.environ["USER"] works sometimes, but sometimes that environment variable i...

django and mod_wsgi having database connection issues

I've noticed that whenever I enable the database settings on my django project (starting to notice a trend in my questions?) it gives me an internal server error. Setting the database settings to be blank makes the error go away. Here are the apache error logs that it outputs. mod_wsgi (pid=770): Exception occurred processing WSGI scrip...

SQLAlchemy is convoluted?

This may seems rather argumentative, but I just went through SQLAlchemy's ORM tutorial and ended up with the following code: from sqlalchemy import create_engine from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker eng...

Emacs function to add symbol to __all__ in Python mode?

Is there an existing Emacs function that adds the symbol currently under the point to __all__ when editing Python code? E.g., say the cursor was on the first o in foo: # v---- cursor is on that 'o' def foo(): return 42 If you did M-x python-add-to-all (or whatever) it would add 'foo' to __all__. I didn't see one when I google...

GUI app spawned from a LocalSystem Service (via CreateProcessAsUser) does not have focus

I have created a service which display a sort of splash screen on the desktop of a specific user and only when that user is logged in (kiosk user). That splash screen, once entered a valid code, will tell that to the service and the service goes to sleep for an x amount of time (depending of the code). The splash screen simply quits. N...

Lay out import pathing in Python, straight and simple?

If a group of Python developers wants to put their shared code somewhere, in a hierarchical structure, what's the structure, and what's the related "import" syntax? Does java-style reference work in Python also? I.e., do directories correspond to dots? What is standard setup for an internal-use-only library of Python code, and what'...