Finding the index of an item given a list containing it in Python

Finding the index of an item given a list containing it in Python

For a list ["foo", "bar", "baz"] and an item in the list "bar", what's the cleanest way to get its index (1) in Python? 1:>>> ["foo", "bar", "baz"].index("bar")1Reference: Data Structures > More on Lists 2:One thing that is really h

How do you do polymorphism in Ruby?

How do you do polymorphism in Ruby?

In C#, I can do this:class Program{ static void Main(string[] args) { List animals = new List(); animals.Add(new Dog()); animals.Add(new Cat()); foreach (Animal a in animals) {

Python 2.5 dictionary 2 key sort

Python 2.5 dictionary 2 key sort

I have a dictionary of 200,000 items (the keys are strings and the values are integers).What is the best/most pythonic way to print the items sorted by descending value then ascending key (i.e. a 2 key sort)? a={ 'keyC':1, 'keyB':2, 'keyA':

Parsing a string which represents a list of tuples

Parsing a string which represents a list of tuples

I have strings which look like this one:"(8, 12.25), (13, 15), (16.75, 18.5)"and I would like to convert each of them into a python data structure. Preferably a list (or tuple) of tuples containing a pair of float values.I could do that wit

multiprocessing pool.map call functions in certain order

multiprocessing pool.map call functions in certain order

How can I make multiprocessing.pool.map distribute processes in numerical order?More Info:I have a program which processes a few thousand data files, making a plot of each one. I'm using a multiprocessing.pool.map to distribute each file t

I need Selenium to open it's web browser in a larger resolution ( preferably maximized)

I need Selenium to open it's web browser in a larger resolution ( preferably maximized)

I am using Selenium WebDriver and coding in PythonI have looked all over the place and the best I could find were things written in different languages. I also tried to use the export tool on Selenium IDE but when I look at the data says th

Python - Extracting inner most lists

Python - Extracting inner most lists

Just started toying around with Python so please bear with me :)Assume the following list which contains nested lists:[[[[[1, 3, 4, 5]], [1, 3, 8]], [[1, 7, 8]]], [[[6, 7, 8]]], [9]]In a different representation:[ [ [ [

Python: what are the nearest Linux and OSX equivalents of winsound.Beep?

Python: what are the nearest Linux and OSX equivalents of winsound.Beep?

If one wishes to beep the speaker on Windows, Python 2 apparently provides a useful function: winsound.Beep(). The neat thing about this function is that it takes arguments specifying the exact frequency and duration of the beep. This is ex

profiling a method of a class in Python using cProfile?

profiling a method of a class in Python using cProfile?

I'd like to profile a method of a function in Python, using cProfile. I tried the following:import cProfile as profile# inside class method...profile.run("self.myMethod()", "output_file")but it does not work. How can I call a self.method w

Concatenate generator and item

Concatenate generator and item

I have a generator (numbers) and a value (number). I would like to iterate over these as if they were one sequence:i for i in tuple(my_generator) + (my_value,)The problem is, as far as I undestand, this creates 3 tuples only to immediately

Creating multiple records for a model in a single view in Rails

Creating multiple records for a model in a single view in Rails

I have a model named Book, and I want to adjust the standard Book#new to show the fields for 10 books, so that 10 can be created at once instead of just one.I looked at this question and found that I could repeat the form if I added 10.time

Return values from one script to another script

Return values from one script to another script

I have the following script that will run each script (sequentially) in a directory:import osdirectory = []for dirpath, dirnames, filenames in os.walk("path\to\scripts"): for filename in [f for f in filenames if f.endswith(".py")]:

Flask AJAX Autocomplete

Flask AJAX Autocomplete

I'm trying to make the jQuery UI autocomplete widget work with the Flask framework.http://flask.pocoo.org/docs/patterns/jquery/http://jqueryui.com/autocomplete/#remoteThis is my HTML: .ui-autocomplete-loading { background: whi

ScrollView widget not scolling in kivy

ScrollView widget not scolling in kivy

I'm having some issues using the ScollView Widget in an attempt to make a dynamically-generated stack of labels scrollable. It's possible that I'm misunderstanding the way ScrollView should be utilized, so I'm hoping somebody could clarify

Paperclip attachments with dynamic style sizes from Model

Paperclip attachments with dynamic style sizes from Model

Using Rails 2, I try to separate different, dynamic image sizes trough another Model from the Paperclip-Model. My current approach, using a Proc, looks the following:class File "FileSize" has_attached_file( :attachment, :styles => P

Multiple iPhone APN messages, single connection

Multiple iPhone APN messages, single connection

I have a strange issue. I'm trying to get Apple Push Notifications working with Python. I can connect and send individual messages without a problem. The issues pop up when I start sending more than one message, but it's more bizarre than e

Can Gradle jar multiple projects into one jar?

Can Gradle jar multiple projects into one jar?

Can Gradle jar multiple projects into one jar ?I know you can do it for a single project using a method like this:task packageTests(type: Jar) { from sourceSets.test.classes}But how does a person zip up multiple sub-projects into one jar?

sub-domain cookies, sent in a parent domain request?

sub-domain cookies, sent in a parent domain request?

Are sub-domain cookies sent in a parent domain HTTP request?For instance, say I have the cookies:Name Value Domain (not https)ABC 1 .example.comXYZ 0 foo.example.comDEF 0 bar.example.comWould X

Devise skip_confirmation! not working

Devise skip_confirmation! not working

My app is set up so that if a user signs in with Oauth or Openid, they don't have to confirm their email address. However, Devise is still sending email confirmations. When I call User.skip_confirmation! I get an undefined method error.

Rails clone copy or duplicate

Rails clone copy or duplicate

I have a nested form and once I save, I want to be able to click a link on the show page to copy or clone that form and open a new one. From there I should be able to make edits (like a new id) and save as a new record. I have seen some exa

Exclude fields in Django admin for users other than superuser

Exclude fields in Django admin for users other than superuser

I have a simple MyUser class with PermissionsMixin. user.is_superuser equals True only for superusers. I'd like to be able to do something similar to this in my admin.py: if request.user.is_superuser: fieldsets = ( (Non

Python Threads - Critical Section

Python Threads - Critical Section

What is the "critical section" of a thread (in Python)? A thread enters the critical section by calling the acquire() method, which can either be blocking or non-blocking. A thread exits the critical section, by calling the release()

Use omniauth-facebook when app id and app secret are different for each request?

Use omniauth-facebook when app id and app secret are different for each request?

The omniauth-facebook README mentions how to set it up in an initializer and how to set options like scope per request only. I wonder if it is possible to set app id and app secret per request as well. 1:You can do this:On your omniauth.

How to test a value on being AnyVal?

How to test a value on being AnyVal?

Tried this:scala> 2.isInstanceOf[AnyVal]:8: error: type AnyVal cannot be used in a type pattern or isInstanceOf test 2.isInstanceOf[AnyVal] ^and this:scala> 12312 match { | case _: AnyVal => true

Unit testing helper or non-interface traits in Scala

Unit testing helper or non-interface traits in Scala

This question is about dealing with testing of classes which mix in non-interface traits, that is traits containing some functionality. When testing, the class functionality should be isolated from the functionality provided by the mix-in t