python

BytesIO with python v2.5

Question: How do I get a byte stream that works like StringIO for Python 2.5? Application: I'm converting a PDF to text, but don't want to save a file to the hard disk. Other Thoughts: I figured I could use StringIO, but there's no mode parameter (I guess "String" implies text mode). Apparently the io.BytesIO class is new in v2.6,...

How do I use extended characters in Python's curses library?

I've been reading tutorials about Curses programming in Python, and many refer to an ability to use extended characters, such as line-drawing symbols. They're characters > 255, and the curses library knows how to display them in the current terminal font. Some of the tutorials say you use it like this: c = ACS_ULCORNER ...and some sa...

sqlalchemy - grouping items and iterating over the sub-lists

Consider a table like this: | Name | Version | Other | | ---------------------|-------| | Foo | 1 | 'a' | | Foo | 2 | 'b' | | Bar | 5 | 'c' | | Baz | 3 | 'd' | | Baz | 4 | 'e' | | Baz | 5 | 'f' | -------------------------------- I would lik...

Multiple Datacenters

I am finding a lack of information regarding handling multiple datacenters. What tools and techniques are available for taking advantage of multiple datacenters? A requirement is that the databases become consistent very quickly. ...

Default route doesnt work

Hey I'm using the standard routing module with pylons to try and setup a default route for the home page of my website. I've followed the instructions in the docs and here http://routes.groovie.org/recipes.html but when I try http://127.0.0.1:5000/ I just get the 'Welcome to Pylons' default page. My config/routing.py file looks like...

Django - SQL Query - Timestamp

Can anyone turn me to a tutorial, code or some kind of resource that will help me out with the following problem. I have a table in a mySQL database. It contains an ID, Timestamp, another ID and a value. I'm passing it the 'main' ID which can uniquely identify a piece of data. However, I want to do a time search on this piece of data(th...

SQLAlchemy - Trying Eager loading.. Attribute Error

I access a a postgres table using SQLAlchemy. I want a query to have eagerloading. from sqlalchemy.orm import sessionmaker, scoped_session, eagerload from settings import DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, DATABASE_PORT, DATABASE_NAME from sqlalchemy import create_engine from sqlalchemy import Table, Column, Integer, Strin...

What is an ORM and where can I learn more about it?

Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it? ...

OCR Playing Cards

I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anywhere from 20 to 200% a...

remove duplicates from nested dictionaries in list

Hello, quick and very basic newbie question. If i have list of dictionaries looking like this: L = [] L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4}) Let's say there exists multiple entries where value3 and value4 are identical to other nested dictionaries. How can i quick and easy find and remove ...

exec() bytecode with arbitrary locals?

Suppose I want to execute code, for example value += 5 inside a namespace of my own (so the result is essentially mydict['value'] += 5). There's a function exec(), but I have to pass a string there: exec('value += 5', mydict) and passing statements as strings seems strange (e.g. it's not colorized that way). Can it be done...

How to classify users into different countries, based on the Location field

Most web applications have a Location field, in which uses may enter a Location of their choice. How would you classify users into different countries, based on the location entered. For eg, I used the Stackoverflow dump of users.xml and extracted users' names, reputation and location: ['Jeff Atwood', '12853', 'El Cerrito, CA'] ['Jarr...

Ruby to python one-liner conversion

I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.: def copyright_dates(start_year = Date.today().year) [start_year, Date.today().year].sort.uniq.join(" - ") end I'm moving the app over to Django, and while I love it, I miss a bit of the conciseness. The same method i...

wxPython: Items in BoxSizer don't expand horizontally, only vertically.

I have several buttons in various sizers and they expand in the way that I want them to. However, when I add the parent to a new wx.BoxSizer that is used to add a border around all the elements in the frame, the sizer that has been added functions correctly vertically, but not horizontally. The following code demonstrates the problem: ...

detecting two simultaneous keys in pyglet (python)

Hi there... I wanted to know how to detect when two keys are simultaneously pressed using pyglet. I currently have def on_text_motion(self, motion): (dx,dy) = ARROW_KEY_TO_VERSOR[motion] self.window.move_dx_dy((dx,dy)) But this only gets arrow keys one at a time... I'd like to distinguish between the combination UP+LEFT an...

In python is there an easier way to write 6 nested for loops?

This problem has been getting at me for a while now. Is there an easier way to write nested for loops in python? For example if my code went something like this: for y in range(3): for x in range(3): do_something() for y1 in range(3): for x1 in range(3): do_something_else() would there be an easier ...

How can I list the methods in a Python 2.5 module?

I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables) within a module? I...

Scraping Ajax - Using python

I'm trying to scrap a page in youtube with python which has lot of ajax in it I've to call the java script each time to get the info. But i'm not really sure how to go about it. I'm using the urllib2 module to open URLs. Any help would be appreciated. ...

What's the Ruby equivalent of Python's os.walk?

Does anyone know if there's an existing module/function inside Ruby to traverse file system directories and files? I'm looking for something similar to Python's os.walk. The closest module I've found is Find but requires some extra work to do the traversal. The Python code looks like the following: for root, dirs, files in os.walk('.'...

Why can't I set a global variable in Python?

I'm trying to understand how global variables work in Python. (Please, no lectures about how global variables are evil; I'm just experimenting). Anyway, this doesn't work: G = None def foo(): if G is None: G = 1 foo() I get: UnboundLocalError: local variable 'G' referenced before assignment What am I doing wrong? ...