Skip to content
Advertisement

Detect last iteration over dictionary.iteritems() in python

Is there a simple way to detect the last iteration while iterating over a dictionary using iteritems()? Answer This is a special case of this broader question. My suggestion was to create an enumerate-like generator that returns -1 on the last item: Add gen = iter(gen) if you want it to handle sequences as well as generators.

Python file parsing: Build tree from text file

I have an indented text file that will be used to build a tree. Each line represents a node, and indents represent depth as well as node the current node is a child of. For example, a file might look like ROOT Node1 Node2 Node3 Node4 Node5 Node6 Which indicates that ROOT contains three children: 1, 5, and 6, Node1

generate video pixel by pixel, programmatically

I’d like to generate an animation pixel by pixel programmatically. Preferably in Hi-Def, in Python or in Ruby. I thought about using PIL to make each frame and then convert the frames into video. Is there a better way to do this? EDIT: Clarification, this is 2D and I need the pixels to be precise. EDITEDIT: Something like this: Would

Python – Unflatten dict

I have this multi-dimensional dict: And written simple function to flatten that dict: After call this function with dict a i get in result: Now, after making few instructions on this flattened dict i need to build new, multi-dimensional dict from that flattened. Example: The only problem I have is that i do not have a function unflatten :) Can

Python equivalent for MySQL’s IFNULL

Is there a function in Python that checks if the returned value is None and if it is, allows you to set it to another value like the IFNULL function in MySQL? Answer Not really, since you can’t rebind arguments. or

Should I return an empty dict instead of None?

I have a method that currently returns None or a dict. The caller currently has to check for the existence of two keys to decide what kind of object was returned. Because result can be None, I’m thinking of returning an empty dict instead, so the caller does not need to check for that. What do you think ? For

function is not defined error in Python

I am trying to define a basic function in python but I always get the following error when I run a simple test program; Here is the code I am using for this function; UPDATE: I have the script called pyth.py open, and then I am typing in pyth_test(1,2) in the interpreter when it gives the error. Thanks for the

Advertisement