pythonic

Pythonic way to convert a list of integers into a string of comma-separated ranges

I have a list of integers which I need to parse into a string of ranges. For example: [0, 1, 2, 3] -> "0-3" [0, 1, 2, 4, 8] -> "0-2,4,8" And so on. I'm still learning more pythonic ways of handling lists, and this one is a bit difficult for me. My latest thought was to create a list of lists which keeps track of paired numbers: ...

SSIS 2008, I am adding a SQL task and am trying to concat, need syntax

using a SSIS 2008. I am adding a SQL task and am trying to convert a varchar(25) into a concat of a varchar field and a integer seq number. Do you have the syntax? ...

Communicating multiple times with a subprocess

I'm trying to pipe input to a program opened as a subprocess in Python. Using communicate() does what I want, but it only does so once, then waits for the subprocess to terminate before allowing things to continue. Is there a method or module similar to communicate() in function, but allows multiple communications with the child proces...

Ordering in python dictionary

r_dict={'answer1': "value1",'answer11': "value11",'answer2': "value2",'answer3': "value3",'answer4': "value4",} for i in r_dict: if("answer" in i.lower()): print i Result is answer11,answer2,snswer4,answer3 I am using python 2.4.3 I there any way to get the order in which it is populated Or is there a wa...

Sorting a python array

opt=[] opt=["opt3","opt2","opt7","opt6","opt1"] for i in range(len(opt)): print opt[i] Output for the above is opt3,opt2,opt7,opt6,opt1 How to sort the above array in ascending order.. ...

Number Guesser: Please Review and Make it More Pythonic

I'm working on learning python, here is a simple program, I wrote: def guesser(var, num1,possible): if var == 'n': cutoff = len(possible)/2 possible = possible[0:cutoff] cutoff = possible[len(possible)/2] #print possible if (len(possible) == 1): print "Your Number is:", possible ...

Get a count for a pattern using python

In the following how to count the number of times that __TEXT__ appears in the variable sing python a="This is __TEXT__ message to test __TEXT__ message" ...

Split a string by a delimiter in python

How to split this string ans $$TEXT$$ is the delimiter. 1.MATCHES$$TEXT$$STRING 2.MATCHES $$TEXT$$ STRING ...

Pythonic way of repeating a method call on different finite arguments

I was staring at a piece of Python code I produced, which, though correct, is ugly. Is there a more pythonic way of doing this? r = self.get_pixel(x,y, RED) g = self.get_pixel(x,y, GREEN) b = self.get_pixel(x,y, BLUE) t = function(r,g,b) if t: r2, g2, b2 = t self.set_pixel(x,y,RED, r2) self...

idiomatic way of taking action on attempt to loop over an empty iterable

Suppose that I am looping over a iterable and would like to take some action if the iterator is empty. The two best ways that I can think of to do this are for i in iterable: # do_something if not iterable: # do_something_else and empty = True for i in iterable: empty = False # do_something if empty: # do_someth...

reason why there is no "if Empty" in python

the zen of python says "Explicit is better than implicit." i find that an is Empty to check whether some sequence is empty is so much more explicit than implicit booleanness if some_sequence is Empty: some_sequence.fill_sequence() compare with if not some_sequence: some_sequence.fill_sequence() this gets even more confusing wi...

Import all modules in django

In django is there way to import all modules ex: from project.models import * ex: from project1.models import * Can this be done with one statement ...

Python-Check a variable is integer or not

In python how to check whether a variable is integer or not.. ...

Is there a way to iterate a specified number of times without introducing an unnecessary variable?

If I want to iterate n times in Java, I write: for (i = 0; i < n; i++) { // do stuff } In Python, it seems the standard way to do this is: for x in range(n): # do stuff As always, Python is more concise and more readable. But the x bothers me, as it is unnecessary, and PyDev generates a warning, since x is never used. Is ...

Replace a pattern in python

How to replace the pattern in the string with decoded_str=" Name(++info++)Age(++info++)Adress of the emp(++info++)" The first pattern "(++info++)" needs to replaced with (++info a++) The second pattern "(++info++)" needs to replaced with (++info b++) The third pattern "(++info++)" needs to replaced with (++info c++) If there m...

python string format suppress/silent keyerror/indexerror

Hi, Is there a way to use python string.format such that no exception is thrown when an index is missing, instead an empty string is inserted. result = "i am an {error} example string {error2}".format(hello=2,error2="success") here,result should be : "i am an example string success" Right now, python throws a keyerror and stops ...

Constructing a random string

How to construct a string to have more than 5 characters and maximum of 15 characters using random function in python import string letters = list(string.lowercase) ...

Store user defined data after inputted

I am making a python program, and I want to check if it is the users first time running the program (firstTime == True). After its ran however, I want to permanently change firstTime to False. (There are other variables that I want to take input for that will stay if it is the first run, but that should be solved the same way). Is there...

Moving Beyond Factories in Python

Coming to Python from Java, I've been told that factories are not Pythonic. Thus, I'm looking for a the Python way to do something like the following. (I'm oversimplifying my goal so that I don't have to describe my entire program, which is very complicated). My script will read in names of people (along with some information about them...

Python: 'Private' module in a package

I have a package mypack with modules mod_a and mod_b in it. I intend the first two to be imported freely: import mypack import mypack.mod_a However, I'd like to keep mod_b for the exclusive use of mypack. That's because it exists merely to organize the latter's internal code. My first question is, is this -- 'private' modules -- an ...