python

How to tell difference between python class and object?

Possible Duplicate: How to identiy whether a variable is a class or an object I have a function which accepts 'things' which it calls. def run_it(thingy): result = thingy(something) However, I'd like run_it() to accept both classes and objects/functions, and if it is a class, instantiate it first: def run_it(thingy): ...

How to save double to file in python?

Let's say I need to save a matrix(each line corresponds one row) that could be loaded from fortran later. What method should I prefer? Is converting everything to string is the only one approach? ...

Writing crawler that stay logged in with any server

I am writing a crawler. Once after the crawler logs into a website I want to make the crawler to "stay-always-logged-in". How can I do that? Is a client (like browser, crawler etc.,) make a server to obey this rule? This scenario could occur when the server allows limited logins in day. ...

Lpr -module in Python

How can you call lpr in Python? It is not in the sys -module which is surprising. I aim to use the lpr as follows shown by pseudo-code 10*i for i in range(77): lpr --pages(i,i+1) file.pdf ...

Playing MP3 files with Python.

I'm trying to write my own media player (like Foobar), and I'm having trouble tracking down a Python library that'll play MP3s. I know Pymedia does mp3s, but it looks outdated - the latest installer is for Python version 2.4, and I'm using 2.6. I've never had much success with Pygame, and Pyglet doesn't look like it has too much in the w...

Appengine reference order

I have declared models in AppEngine's models.py: class Post(db.Model): topic = db.ReferenceProperty(Topic, collection_name='posts', verbose_name=_('Topic')) (..) class Topic(db.Model): (..) last_post = db.ReferenceProperty(Post, collection_name='last_topic_post') Problem is ReferenceProperty must have Model class but Topic class is u...

Scrapy domain_name for spider

From the Scrapy tutorial: domain_name: identifies the Spider. It must be unique, that is, you can’t set the same domain name for different Spiders. Does this mean that domain_name must be a valid domain name, like domain_name = 'example.com' Or can I name domain_name = 'ex1' The problem is I had a spider that worked with...

Reading socket buffer using asyncore

I'm new to Python (I have been programming in Java for multiple years now though), and I am working on a simple socket-based networking application (just for fun). The idea is that my code connects to a remote TCP end-point and then listens for any data being pushed from the server to the client, and perform some parsing on this. The da...

Scrapy spider index error

This is the code for Spyder1 that I've been trying to write within Scrapy framework: from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.selector import HtmlXPathSelector from scrapy.item import Item from firm.items import FirmItem class Spider1(CrawlSpider):...

Python JSON parse_float=decimal.Decimal not working

Hi, I have a string with a floating point number in it, but I can't get JSON to load it as a decimal. x = u'{"14": [4.5899999999999999, "susan"]}' json.loads(x, parse_float = decimal.Decimal) This returns: {u'14': [Decimal('4.5899999999999999'), u'susan']} Any idea how I can make it into the actual "4.59"? ...

Why is (python|ruby) interpreted?

What are the technical reasons why languages like Python and Ruby are interpreted (out of the box) instead of compiled? It seems to me like it should not be too hard for people knowledgeable in this domain to make these languages not be interpreted like they are today, and we would see significant performance gains. So certainly I am mis...

How to write a simple spider in Python?

Hello, I've been trying to write this spider for weeks but without success. What is the best way for me to code this in Python: 1) Initial url: http://www.whitecase.com/Attorneys/List.aspx?LastName=A 2) from initial url pick up these urls with this regex: hxs.select('//td[@class="altRow"][1]/a/@href').re('/.a\w+') [u'/cabel', u'/jac...

Django / Python / PIL / sorl-thumbnail generation in bulk - memory error

hi folks! I'm trying to bulk generate 4 thumnails for each of around 40k images with sorl-thumbnail for my django app. I iterate through all django objects with an ImageWithThumbnailsFieldFile, and then call its generate_thumbnails() function. This works fine, except that after a few hundred iterations, I run out of memory and my loop ...

How to fix value produced by Random?

Hi all, I got an issue which is, in my code,anyone can help will be great. this is the example code. from random import * from numpy import * r=array([uniform(-R,R),uniform(-R,R),uniform(-R,R)]) def Ft(r): for i in range(3): do something here, call r return something however I found that in python shell, every tim...

How to write the grammar for this in pyparsing: match a set of words but not containing a given pattern

I am new to Python and pyparsing. I need to accomplish the following. My sample line of text is like this: 12 items - Ironing Service 11 Mar 2009 to 10 Apr 2009 Washing service (3 Shirt) 23 Mar 2009 I need to extract the item description, period tok_date_in_ddmmmyyyy = Combine(Word(nums,min=1,max=2)+ " " + Word(alphas, exact=3)...

How would you represent a MineSweeper grid in Python?

What datastructure would you use in Python to represent the internal state of a MineSweeper grid? Each x,y position will hold a numerical value which represents its current cell state (unexplored, mine, flag, ?). Should I use nested lists? This seems like the closest thing to a 2D array and it is what I would probably use in any other...

What is the performance cost of named keys or "pre-generated" keys in Google App Engine?

If you used named keys in Google App Engine, does this incur any additional cost? Put another way, is it any more expensive to create a new entity with a named key rather than a randomly generated id? In a similar line of reasoning, I note that you can ask Google App Engine to give you a set of keys that will not be used by Google App E...

Where/How should I do validation and transformations on entities in Google App Engine?

In Ruby on Rails, each model entity has a "validate_on_*something*" hook method, that will be called before the entity is actually persisted to the database. I would like similar functionality in Google App Engine. I am aware that you can do validation on individual Properties by passing arguments to them in their declarations. However, ...

How can I use SQLITE with DJANGO on WIndows 7

I am following the tutorial on the DJango site, which I previsouly did using Windows XP and everything went fine, but on Windows 7 I get the following error: sqlite3.OperationalError: unable to open database file I use the following: python manage.py sql Blog Does any one have any ideas what might be wrong. The database file is lo...

Python asynchronous callbacks and generators

Hello, I'm trying to convert a synchronous library to use an internal asynchronous IO framework. I have several methods that look like this: def foo: .... sync_call_1() # synchronous blocking call .... sync_call_2() # synchronous blocking call .... return bar For each of the synchronous functions (sync_call_*), I have...