python

How do I scroll a wxPython wx.html.HtmlWindow back down to where it was when the user clicked a link?

I am using a wxPython wx.html.HtmlWindow to display part of my interface. The user can scroll down a list of links in a window smaller than the list. When they click on a link, I need to repaint the web page, but I want to return the page position back to where they clicked it. I've tried MouseEvent.GetLogicalPosition() on the event, b...

Python class for pickle- and copy-persistent object?

I'm trying to write a class for a read-only object which will not be really copied with the copy module, and when it will be pickled to be transferred between processes each process will maintain no more than one copy of it, no matter how many times it will be passed around as a "new" object. Is there already something like that? ...

Dictionary of tags in declarative SQLAlchemy?

I am working on a quite large code base that has been implemented using sqlalchemy.ext.declarative, and I need to add a dict-like property to one of the classes. What I need is the same as in this question, but in a declarative fashion. Can anyone with more knowledge in SQLAlchemy give me an example? Thanks in advance... ...

How to empty a list in Python?

It seems so "dirty" emptying a list in this way: while len(alist) > 0 : alist.pop() Does a clear way exist to do that? ...

How to create QString in PyQt4?

str = QtCore.QString('Hello') AttributeError: 'module' object has no attribute 'QString' QtCore.QString._init_(self) AttributeError: 'module' object has no attribute 'QString' Yes, I'm read QString Class Reference if you can, answer with code sample. Thanks ...

How to get Django admin.TabularInline to NOT require some items

class LineItemInline(admin.TabularInline): model = LineItem extra = 10 class InvoiceAdmin(admin.ModelAdmin): model = Invoice inlines = (LineItemInline,) and class LineItem(models.Model): invoice = models.ForeignKey(Invoice) item_product_code = models.CharField(max_length=32) item_description = models.CharF...

Python with matplotlib - drawing multiple figures in parallel

I'm have functions that contribute small parts of a figure generation. What I'm trying to do is to use these functions to generate multiple figures? So something like this: work with Figure 1 do something else work with Figure 2 do something else work with Figure 1 do something else work with Figure 2 If anyone could help, that'd be ...

CRC32 to make short URL for web

I am trying to understand crc32 to generate the unique url for web page. If we use the crc32, what is the maximum number of urls can be used so that we can avoid duplicates? What could be the approximative string length to keep the checksum to be 2^32? When I tried UUID for an url and convert the uuid bytes to base 64, I could reduce ...

Return the number of affected rows from a MERGE with cx_oracle

How can you get the number of affected rows from executing a "MERGE INTO..." sql command within CX_Oracle? When ever I execute the MERGE SQL on cx_oracle, I get a cursor.rowcount of -1. Is there a way to get the number of rows affected by the merge? ...

Performance lost when open a db multiple times in BerkeleyDB

I'm using BerkeleyDB to develop a small app. And I have a question about opening a database multiple time in BDB. I have a large set of text ( corpus ), and I want to load a part of it to do the calculation. I have two pseudo-code (mix with python) here @1 def getCorpus(token): DB.open() DB.get(token) DB.close() @2 #ope...

How do I programmatically check whether an image (PNG, JPEG, or GIF) is corrupted?

Okay. So I have about 250,000 high resolution images. What I want to do is go through all of them and find ones that are corrupted. If you know what 4scrape is, then you know the nature of the images I. Corrupted, to me, is the image is loaded into Firefox and it says The image “such and such image” cannot be displayed, because it c...

Python: copy.deepcopy produces an error

I have been using this copy method for quite a while, in lots of classes that needed it. class population (list): def __init__ (self): pass def copy(self): return copy.deepcopy(self) It has suddenly started producing this error: File "C:\Python26\lib\copy.py", line 338, in _reconstruct state = deepcopy(state, memo) ...

Problem with list of strings in python

Why on Earth doesn't the interpreter raise SyntaxError everytime I do this: my_abc = ['a', 'b', 'c' 'd',] I just wanted to add 'c' to the list of strings, and forgot to append the comma. I would expect this to cause some kind of error, as it's cleary incorrect. Instead, what I got: >>> my_abc ['a', 'b...

Python: List all base classes in a hierarchy

Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes - anywhere in the inheritance hierarchy - it issubclass of? ...

calculate euclidean distance with numpy

I have two points in 3D: (xa,ya,za) (xb,yb,zb) And I want to calculate the distance: dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2) What's the best way to do this with Numpy, or with Python in general? I have: a = numpy.array((xa,ya,za)) b = numpy.array((xb,yb,zb)) ...

Convert list of objects to a list of integers and a lookup table

To illustrate what I mean by this, here is an example messages = [ ('Ricky', 'Steve', 'SMS'), ('Steve', 'Karl', 'SMS'), ('Karl', 'Nora', 'Email') ] I want to convert this list and a definition of groups to a list of integers and a lookup dictionary so that each element in the group gets a unique id. That id should map t...

Why is there no list.clear() method in python?

Inspired by this question. Why is there no list.clear() method in python? I've found several questions here that say the correct way to do it is one of the following, but no one has said why there isn't just a method for it. del lst[:] lst[:] = [] While it may go against the "zen of python" to have more than one way of doing somethin...

Script to connect to a web page

Looking for a python script that would simply connect to a web page (maybe some querystring parameters). I am going to run this script as a batch job in unix. ...

Why can't I upload jpg files to my Django app via admin/ ?

I'm trying to add images to my models in my Django app. models.py class ImageMain(models.Model): product = models.ForeignKey(Product) photo = models.ImageField(upload_to='products') In development mode, every time I try to upload the image via Django admin, I keep getting: Upload a valid image. The file you uploaded was either n...

How do I right-align my text in Python?

I have some data that I am displaying in 3 column format, of the form "key: value key: key: value key: value". Here's an example: p: 1 sl: 10 afy: 4 q: 12 lg: 10 kla: 3 r: 0 kl: 10 klw: 3 s: 67 vw: 9 jes: 3 t: 16 uw: 9 skw: 3 u: 47 ug: 9 mjl: 3 v: 37 mj: 8 lza: 3 w: 119 fv: 8 fxg: 3 x: 16 fl: 8 aew: 3...