python

Elegant way to remove fields from nested dictionaries

Hi! I had to remove some fields from a dictionary, the keys of this fields are on a list. So I write this function: def delete_keys_from_dict(dict_del, lst_keys): """ Delete the keys present in the lst_keys from the dictionary. Loops recursively over nested dictionaries. """ dict_foo = dict_del.copy()#Used as iterat...

Is there a Python idiom for evaluating a list of functions/expressions with short-circuiting?

I wrote a simple script to solve a "logic puzzle", the type of puzzle from school where you are given a number of rules and then must be able to find the solution for problems like "There are five musicians named A, B, C, D, and E playing in a concert, each plays one after the other... if A goes before B, and D is not last ... what is th...

A thread pool that lets me know when at least 1 has finished?

I need to use a thread pool in python, and I want to be able to know when at least 1 thead out or "maximum threads allowed" has finished, so I can start it again if I still need to do something. I has been using something like this: def doSomethingWith(dataforthread): dostuff() i = i-1 #thread has finished i = 0 poolSize = 5 t...

List web url dir contents

Hi there I wanna list a external webpage's ulr's content. like i wanna list the content of this website example.com/dir/dir/images/ currently i can download an image from a page with urllib.urlretrieve(page_url,save_url ) But I want to list all images in a directory, or anything ells for that matter I wanna use python ...

Python method for storing list of bytes in network (big-endian) byte order to file (little-endian)

My present task is to dissect tcpdump data that includes P2P messages and I am having trouble with the piece data I acquire and write to a file on my x86 machine. My suspicion is I have a simple endian-ness issue with the bytes I write to to file. I have a list of bytes holding a piece of P2P video read and processed using python-pcapy...

Pattern matching using python

In the code below how match the pattern after "answer" and "nonanswer" in the dictionary opt_dict=( {'answer1':1, 'answer14':1, 'answer13':12, 'answer11':6, 'answer5':5, 'nonanswer12':1, 'nonanswer11':1, 'nonanswer4':1, 'nonanswer5':1,}) And if opt_dict: for ii in opt_dict: lo...

Implementing workflows in Django Admin - Django

Hi folks, I have a nice admin panel setup so users can manage the data within the site. Problem is that I need to implement a workflow, so saved models can be approved from and to various stages, to then be finally published. As the model in question is just one, I thougth of adding a boolean 'approved_for_publishing' field and a 'a...

Saving a temporary file.

I'm using xlwt in python to create a Excel spreadsheet. You could interchange this for almost anything else that generates a file; it's what I want to do with the file that's important. from xlwt import * w = Workbook() #... do something w.save('filename.xls') I want to I have two use cases for the file: I stream it out to the user's...

How to understand the sample from python minimock?

How to understand this line? >>> smtplib.SMTP.mock_returns = Mock('smtp_connection')? What is smtp_connection? It seems I can modify it to any name. following is from minimock Here's an example of something we might test, a simple email sender:: ...

using xpath to tell selenium where to click?

I'm new to all of this but I've learned a few things about python not long time ago, could you help me specify the correct the XPath for selenium to click? I've tried this way, but didn't work, obviously :( self.selenium.click("xpath=//html/body/div/div/div/div[4]/ul/li[3]/a") If you're wandering where did i get that ugly XPath, it's...

Iteration order of sets in Python

If I have two identical sets, meaning a == b gives me True, will they have the same iteration order? I tried it, and it works: >>> foo = set("abc") >>> bar = set("abc") >>> zip(foo, bar) [('a', 'a'), ('c', 'c'), ('b', 'b')] My question is, was I lucky, or is this behavior guaranteed? ...

Parsing Snort Logs with PyParsing

Having a problem with parsing Snort logs using the pyparsing module. The problem is with separating the Snort log (which has multiline entries, separated by a blank line) and getting pyparsing to parse each entry as a whole chunk, rather than read in line by line and expecting the grammar to work with each line (obviously, it does not.)...

Paranoia, excessive logging and exception handling on simple scripts dealing with files. Is this normal?

I find myself using python for a lot of file management scripts as the one below. While looking for examples on the net I am surprised about how little logging and exception handling is featured on the examples. Every time I write a new script my intention is not to end up as the one below but if it deals with files then no matter what m...

Pythonistas, please help convert this to utilize Python Threading concepts

Update : For anyone wondering what I went with at the end - I divided the result-set into 4 and ran 4 instances of the same program with one argument each indicating what set to process. It did the trick for me. I also consider PP module. Though it worked, it prefer the same program. Please pitch in if this is a horrible implementation! ...

How to write a server using existing version and wireshark?

I decided to improve my knowledge about python network programming and here is the deal: I have a simple server for Windows, which interacts with a client from a mobile device using wi-fi. Also I have a packet sniffer (Wireshark). Now I want to ask, what do I need to write the Linux version of this server? How to determine the structure ...

"AttributeError: 'module' object has no attribute '__getstate__' " shows up when I use easy_install

For some reason, every time I attempt to install a new module using easy_install, I'm getting the error: AttributeError: 'module' object has no attribute '__getstate__' I'm using setuptools-0.6c11-py2.6 ...

How to delete all files in directory on remote server in python?

I'd like to delete all the files in a given directory on a remote server that I'm already connected to using paramiko. I cannot explicitly give the file names, though, because these will vary depending on which version of file I had previously put there. Here's what I'm trying to do... the line below the #TODO is the call I'm trying wh...

Python regex string to list of words (including words with hyphens)

I would like to parse a string to obtain a list including all words (hyphenated words, too). Current code is: s = '-this is. A - sentence;one-word' re.compile("\W+",re.UNICODE).split(s) returns: ['', 'this', 'is', 'A', 'sentence', 'one', 'word'] and I would like it to return: ['', 'this', 'is', 'A', 'sentence', 'one-word'] ...

Removing things from Python list during for loop

Here is my code: toBe =[] #Check if there is any get request if request.GET.items() != []: for x in DB: try: #This is removing the PMT that is spcific to the name if request.GET['pmtName'] != "None": if not request.GET['pmtName'] in x['tags']: print x['title'], x['...

How to make a Django passthrough view?

I want to make a Django view that does the following: Receive an HttpRequest on api/some/url/or/other Passes this through to another server at some/url/or/other (rewrite the URL, basically) Adding a cookie based on session data in Django Using the same method, data, params, et al, that were in the original request Returns verbatim th...