pythonic

Most Pythonic way equivalent for: while ((x = next()) != END)

What's the best Python idiom for this C construct? while ((x = next()) != END) { .... } I don't have the ability to recode next(). update: and the answer from seems to be: for x in iter(next, END): .... ...

What defines "pythonian" or "pythonic"?

I want to begin to learn Python, and I've seen that phrase come up here before, but I don't know exactly what it means. I've read some websites on Python scripting, but I don't recall ever seeing that (but I could have just glanced over it). What exactly makes something "pythonian" or "pythonic"? ...

Which is more pythonic, factory as a function in a module, or as a method on the class it creates ?

I have some Python code that creates a Calendar object based on parsed VEvent objects from and iCalendar file. The calendar object just has a method that adds events as they get parsed. Now I want to create a factory function that creates a calendar from a file object, path, or URL. I've been using the iCalendar python module, which i...

Beginner wondering if his code is 'Pythonic'

This is really the first thing that I have written in python. I come from Java background. I don't want to just learn how to program java code with Python syntax. I want to learn how to program in a pythonic paradigm. Could you guys please comment on how I can make the following code more pythonic? from math import sqrt # recursive...

I'm using Python regexes in a criminally inefficient manner

My goal here is to create a very simple template language. At the moment, I'm working on replacing a variable with a value, like this: This input: <%"TITLE"="This Is A Test Variable"%>The Web <%"TITLE"%> Should produce this output: The Web This Is A Test Variable I've got it working. But looking at my code, I'm running multi...

What is an easy way to create a trivial one-off Python object?

I would like to create a trivial one-off Python object to hold some command-line options. I would like to do something like this: options = ?????? options.VERBOSE = True options.IGNORE_WARNINGS = False # Then, elsewhere in the code... if options.VERBOSE: ... Of course I could use a dictionary, but options.VERBOSE is more readabl...

What application you recommend to start peeking to learn Python style?

Do you know any application, the more interesting/useful the better, to introduce a new person to Python language and the Python code style, but not necessarily to OO programing, so as to learn the subtleties and idioms of the language and surrounding community? I'm thinking along the lines of people that has worked with JavaScript, Jav...

Is this idiom pythonic? (someBool and "True Result" or "False Result")

I just came across this idiom in some open-source Python, and I choked on my drink. Rather than: if isUp: return "Up" else: return "Down" or even: return "Up" if isUp else "Down" the code read: return isUp and "Up" or "Down" I can see this is the same result, but is this a typical idiom in Python? If so, is it some perf...

What is the pythonic way to avoid default parameters that are empty lists?

Sometimes it seems natural to have a default parameter which is an empty list. Yet Python gives unexpected behavior in these situations. If for example, I have a function: def myFunc(working_list = []): working_list.append("a") print working_list The first time it is called with the default will work, but calls after that w...

Differences between Python game libraries Pygame and Pyglet?

I've had some experience with Pygame, but there seems to be a lot of buzz around Pyglet these days. How do these two libraries compare? What would be the advantage of using one over the other, both in features and ease of use? Finally, would you say that one is more Pythonic than the other? ...

Checking if two strings are permutations of each other in Python

I'm checking if two strings a and b are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways: sorted(a) == sorted(b) and all(a.count(char) == b.count(char) for ch...

Python: C++-like stream input

Hi! Is there a pythonic way of reading - say - mixed integer and char input without reading the whole input at once and without worrying about linebreaks? For example I have a file with whitespace-separated data of which I only know that there are x integers, then y chars and then z more integers. I don't want to assume anything about l...

What's the most pythonic way of access C libraries - for example, OpenSSL?

I need to access the crypto functions of OpenSSL to encode Blowfish data in a CBC streams. I've googled and found some Blowfish libraries (hand written) and some OpenSSL wrappers (none of the seem complete.) In the end, I need to access the certain OpenSSL functions, such as the full blowfish.h library of commands. What's the pythonic/r...

What is the most "pythonic" way to iterate over a list in chunks?

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples. Currently, I'm iterating over it this way: for i in xrange(0, len(ints), 4): # dummy op for example code ...

What does "pythonic" mean?

exact duplicate: http://stackoverflow.com/questions/58968/what-defines-pythonian-or-pythonic I have no python experience at all. Help me to understand this term. Please provide code examples and explanations as to what makes a particular sample "pythonic" and why. ...

Most pythonic way to extend a potentially incomplete list

What I'm looking for is the best way to say, 'If this list is too short, lengthen it to 9 elements and add 'Choice 4', 'Choice 5', etc, as the additional elements. Also, replace any 'None' elements with 'Choice x'.' It is ok to replace "" and 0 too. An example transformation would be ['a','b',None,'c'] to ['a','b','Choice 3','c','Ch...

Obtaining all possible states of an object for a NP-Complete(?) problem in Python

Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available. Say you have : class Person: def __init__(self): self.status='unknown' def set(self,value): if value: self.status='happy' else :...

How to dynamically load a Python class

Given a string of a Python class, e.g. 'my_package.my_module.MyClass', what is the best possible way to load it? In other words I am looking for a Class.forName() function in Python. It needs to work on Google App Engine. Preferably this would be a function that accepts the FQN of the class as a string, and returns a reference to the c...

Can anyone provide a more pythonic way of generating the morris sequence?

I'm trying to generate the morris sequence in python. My current solution is below, but I feel like I just wrote c in python. Can anyone provide a more pythonic solution? def morris(x): a = ['1', '11'] yield a[0] yield a[1] while len(a) <= x: s = '' count = 1 al = a[-1] for i in range(0,le...

Python code to find if x is following y on twitter. More Pythonic way please

I wrote a twitter application in Python. Following is the code I used for a module where I find if x is following y. This code can be obviously improved upon. A pythonic way to do that? import urllib2 import sys import re import base64 from urlparse import urlparse import simplejson def is_follows(follower, following): theurl = 'h...