python

Fetching a random record from the Google App Engine Datastore?

I have a datastore with around 1,000,000 entities in a model. I want to fetch 10 random entities from this. I am not sure how to do this? can someone help? ...

Anyone Know a Great Sparse One Dimensional Array Library in Python?

I am working on an algorithm in Python that uses arrays of int64s heavily. The arrays are typically sparse and are read from and written to constantly. I am currently using relatively large native arrays and the performance is good but the memory usage is high (as expected). I would like to be able to have the array implementation not ...

In Python, how do I remove the "root" tag in an HTML snippet?

Suppose I have an HTML snippet like this: <div> Hello <strong>There</strong> <div>I think <em>I am</em> feeing better!</div> <div>Don't you?</div> Yup! </div> What's the best/most robust way to remove the surrounding root element, so it looks like this: Hello <strong>There</strong> <div>I think <em>I am</em> feeing better!</...

metaclass multiple inheritance inconsistency

Why is this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyList(list, MyMixin): pass okay, and works as expected: created <class '__main__.MyMixin'> created <class '__main__.MyList'> But this: class MyType(type): def __init__(cls, n...

Python: Access dictionary value inside of tuple and sort quickly by dict value

I know that wasn't clear. Here's what I'm doing specifically. I have my list of dictionaries here: dict = [{int=0, value=A}, {int=1, value=B}, ... n] and I want to take them in combinations, so I used itertools and it gave me a tuple (Well, okay it gave me a memory object that I then used enumerate on so I could loop over it and enume...

Adding a small photo/image to a large graph in Matplotlib/Python

I have a large graph that I am generating in matplotlib. I'd like to add a number of icons to this graph at certain (x,y) coordinates. I am wondering if there is any way to do that in matplotlib Thank you ...

Type validation for cx_Oracle.OracleCursor

This could be very basic question. I'd like to validate a variable is type of cx_Oracle.OracleCursor, so I tried a couple of trials but all failed: import cx_Oracle def execute_sql(cursor): """Execute a SQL using cursor(cx_Oracle.OracleCursor).""" if not cursor: logging.error('cursor is null.') return None # TO...

What is a more efficient way in Python to return list elements which are not in a second list?

Is there a faster way to do this in python? [f for f in list_1 if not f in list_2] list_1 and list_2 both consist of about 120.000 strings. It takes about 4 minutes to generate the new list. ...

Web2py controllers with parameters?

I am building an app using Web2py framework... I don't want to have to use the request object to get all of the querystring parameters, instead I'd like to build my controller with named parameters and have the router unpack the querystring (or form data) dictionary into the named parameters and call my controller. so instead of a contr...

How to detect non-graceful disconnect of Twisted on Linux?

I wrote a server based on Twisted, and I encountered a problem, some of the clients are disconnected not gracefully. For example, the user pulls out the network cable. For a while, the client on Windows is disconnected (the connectionLost is called, and it is also written in Twisted). And on the Linux server side, my connectionLost of t...

Specifying custom URL schema in appengine using app.yaml?

I am trying to have a custom URL which looks like this: example.com/site/yahoo.com which would hit this script like this= example.com/details?domain=yahoo.com can this be done using app.yaml? the basic idea is to call "details" with the input "yahoo.com" ...

KindError: Property r must be an instance of SecondModel, why ?

class FirstModel(db.Model): p = db.StringProperty() r=db.ReferenceProperty(SecondModel) class SecondModel(db.Model): r = db.ReferenceProperty(FirstModel) class sss(webapp.RequestHandler): def get(self): a=FirstModel() a.p='sss' a.put() b=SecondModel() b.r=a b.put() a.r=b a.put() self.r...

facing problem when trying to send an email using python

I wrote the code like this import smtplib server=smtplib.SMTP('localhost') Then it raised an error like error: [Errno 10061] No connection could be made because the target machine actively refused it I am new to SMTP, can you tell what exactly the problem is? ...

ANT doesn't get exit code return by a python script

Hi, I'm currently using ant for building my java project on a Windows XP machine. I have different tasks defined in the build.xml and one of this is the exec of a Python script for analyzing the application output. I would like to make ANT failing when a particolar tag is discovered by script. I'm trying using: sys.exit(1) or os.syst...

Problem with TCP server in Twisted

Hi all, I'm trying to make a simple TCP server using Twisted ,which can do some interaction between diffirent client connections.The main code is as below: #!/usr/bin/env python from twisted.internet import protocol, reactor from time import ctime #global variables PORT = 22334 connlist = {} #store all the connections ids = {} #...

how to confine to write a string in specific list ,on google-app-engine

class FirstModel(db.Model): p = db.StringProperty({option:['aa','bb','cc']}) the error is : NameError: name 'option' is not defined what should i do ,thanks ...

How to get progress bar to time Class exectution

Hello, I am trying to use progress bar to show the progress of a script. I want it increase progress after every function in a class is executed. The code I have tried is below: import progressbar from time import sleep class hello(): def no(self): print 'hello!' def yes(self): print 'No!!!!!!' def pro(): ...

Encrypt and Decrypt information in a cookie.

I need to securely crypt and decrypt information about users ( name, surname and user_id ) in cookies. What is the best way to do this ? What encryption and decryption function do I need ? Thanks ^_^ ...

Django admin urls return INVALID REQUEST! - Django

Hi folks, my admin urls are sat behind a prefix by doing the following. 1# (r'^admin/', include(admin.site.urls)), is placed within urls_core.py 2# (r'^api/', include('project.urls_core')), is palced within urls.py All admin URLs work fine except app indexes. If I go to any URL such as: /api/admin/core/ /api/admin/registr...

Connect to an existing process

Hole thing is happening on the mac os x. Let's assume that I've opened an program by clicking on an .app icon. It's a python program with GUI which has a separate process that waits for a user input. But as I've opened it by clickin .app icon I dont have access to it's input as I would have if I opened it in Terminal. And the question ...