python

How to get all the minimum elements according to its first element of the inside list in a nested list?

Simply put! there is this list say LST = [[12,1],[23,2],[16,3],[12,4],[14,5]] and i want to get all the minimum elements of this list according to its first element of the inside list. So for the above example the answer would be [12,1] and [12,4]. Is there any typical way in python of doing this? Thanking you in advance. ...

os.walk() caching/speeding up

I have a prototype server[0] that's doing an os.walk()[1] for each query a client[0] makes. I'm currently looking in to ways of caching this data in memory, speeding up queries and hopefully allowing for expansion in to storing metadata and data persistence later on. I've experimented with SQLite before, but I find SQL complicated for ...

Cannot create new Django model object within Ajax post request

This is kind of "I already lost x hours debugging this" kind of problem/question :( Following jQuery js code is initiating POST request upon button click $("#btn_create_tag").click(function(evt) { $.post("/tag/createAjax", { tagname: $("#txt_tag_name").val() }, function(data) { } ); }); Django code that is per...

Python Reverse Find in String

ok, I've got a string and an arbitrary index into the string. Now I want find the first occurrence of a substring before the index. A example: I want to find the index of the 2nd I by using the index and str.rfind() s = "Hello, I am 12! I like plankton but I don't like Baseball." index = 34 #points to the 't' in 'but' index_of_2nd_I = ...

python import error

what's wrong with my imports? App folder structure: myapp - models/models.py contains SpotModel() - tests/tests.py contains TestSpotModel(unittest.TestCase). tests.py imports "from myapp.models.models import *" which works like a charm - scripts/import.py contains "from myapp.models.models import *" the problem is that import.py w...

How to reconstruct python source from loaded modules (sys.modules)?

Is it possible to construct human readable source for loaded modules if you have access to sys.modules? People tell me you cannot, but I'm sure it is possible in Python. ...

Any one have an example that uses the element.sourceline method from lxml.html

I hope I asked that correctly. I am trying to figure out what element.sourceline does and if there is some way I can use its features. I have tried building my elements from the html a number of ways but every time I iterate through my elements and ask for sourceline I always get None. When I tried to use the built-in help I done't ge...

Many-to-many data structure in Python

I have a data set of books and authors, with a many-to-many relationship. There are about 10^6 books and 10^5 authors, with an average of 10 authors per book. I need to perform a series of operations on the data set, such as counting the number of books by each author or deleting all books by a certain author from the set. What would ...

Does python have a robust pop3, smpt, mime library where I could build a webmail interface?

Does python have a full fledged email library with things for pop, smtp, pop3 with ssl, mime? If I want to create a web mail interface that pulls emails from email servers, and then shows the emails, along with attachments, can display the sender, subject, etc. (handles all the encoding issues etc). update its one thing to be available...

How to detect if a Blobstore entry is an image so get_serving_url would work?

I have a general purpose file storage backed by Google App Engine Blobstore, when I show users it's contents I would like to differentiate images from other files — I would like to show thumbnail for each image. Python get_serving_url function does not care (at least at dev server) if given blob is in fact an image, java's getServingUrl...

Casting sockets to subtypes

I'm trying to create my own subclass of socket.socket that will be able to handle custom messages. So far my code looks like this: self._sockets.append(s) logging.debug("Waiting for incoming connections on port %d" % (port)) while not self.shutdown: inputready,outputready,exceptready = select(self._sockets,[],[]) ...

What does Instance() do in Python assignments?

var = Instance(object)? ...

How do you draw a grid and rectangles in Python?

What Python-related code (PyGTK, Glade, Tkinter, PyQT, wxPython, Cairo, ...) could you easily use to create a GUI to do some or all of the following? Part of the GUI has an immovable square grid. The user can press a button to create a resizable rectangle. The user can drag the rectangle anywhere on the grid, and it will snap to the gr...

Why don't Python's list comprehensions make copies of arguments so actual objects can't be mutated?

Maybe I've been drinking too much of the functional programming Kool Aid, but this behavior of list comprehensions seems like a bad design choice: >>> d = [1, 2, 3, 4, 5] >>> [d.pop() for _ in range(len(d))] [5, 4, 3, 2, 1] >>> d [] Why is d not copied, and then the copied lexically-scoped version not mutated (and then lost)? The poin...

Python PyHANDLE object in win32gui

I've been playing around trying to create a transparent window that will overlay another application. I'm finding the win32 extensions are a bit of a mess, there's win32ui, win32gui, winxpgui, etc. somewhat confusing. Anyway, I'm trying to pass a handle to a window to this function win32gui.UpdateLayeredWindow the first argument it wa...

Obtaining an invertible square matrix from a non-square matrix of full rank in numpy or matlab

Assume you have an NxM matrix A of full rank, where M>N. If we denote the columns by C_i (with dimensions Nx1), then we can write the matrix as A = [C_1, C_2, ..., C_M] How can you obtain the first linearly independent columns of the original matrix A, so that you can construct a new NxN matrix B that is an invertible matrix with a n...

Python is printing more than I want

Why is it that below code prints 'None' as well as what I tell it to write when no match is found? def subStringMatchExact(target,key): list=[] for fsi in range (len(target)): if key==target[fsi:fsi+len(key)]: list=list+[fsi,] #return list if list!=[]: return list else: pri...

Python: rewinding one line in file when iterating with f.next()

Python's f.tell doesn't work as I expected when you iterate over a file with f.next(): >>> f=open(".bash_profile", "r") >>> f.tell() 0 >>> f.next() "alias rm='rm -i'\n" >>> f.tell() 397 >>> f.next() "alias cp='cp -i'\n" >>> f.tell() 397 >>> f.next() "alias mv='mv -i'\n" >>> f.tell() 397 Looks like it gives you the position of the buff...

Using Cython with Django. Does it make sense ??

Hello Everyone, Is it possible to optimize speed of a mission critical application developed in Django with Cython Sorry in advance if it doesn't make sense......as i am new to django. Recently i have read on the internet that you can use cython and turn a python code to c like speed......so i was wondering is this possible with djan...

Serve static files through a view in Django

Hello, I am writng a Django application that let's you download a file after some requirements have been met (you have to log on, for example). The file needs to be inaccessible otherwise. Serve the file through Apache won't work: I have to check in the database for the user's permissions. Furthermore, don't have permission to change ...