I'm trying to write this in the most pythonic way possible, but I'm just now learning about lambda's (and what they can be combined with) and I'm having some difficulty.
Using the mechanize
module I'm scraping a website for a select control and trying to submit each option value to the form and read the resulting content (maybe I'm trying to do too much for one little 'ol statement). Here is what I've got:
f = "ctl00$holdSection$selSubAgentFilter" #the select box field name
self.br.select_form(nr=0) #make the form selection in the browser instance
#here's where the magic is done
map( (lambda item,f=f: print(self.br.form.find_control( f ).items[ item[0] ].selected.submit().read() ) ), enumerate( self.br.find_control( f ).items ) )
So, to break it apart, I'm trying to do the following:
- iterate over each of the options in the select field, by using enumerate and getting the items from the control and submitting that enumerated list to the
map
function to basically "walk" over each list item - use a lambda function for the
map
executed function - submit the items list (enumerated to get indices) to the lambda function
- using the enumerate index from the passed item, select the item in the form control and select it
- submit the form
- read the contents of the new browser window and
print()
it back to the stdout
Of course in this case the selected
property of the chained methods evaluates to a boolean and emits the following error:
AttributeError: 'bool' object has no attribute 'submit'
But you get the idea of what I'm trying to accomplish, I'm just not sure how, and accomplish it with a minimal footprint--I can get it to work using several lines and for loops, I was just trying to stay away from that if possible. I'm trying to branch out and overcome issues in the most pythonic way, rather than the easiest.
Any thoughts?