6
votes
4answers
191 views

Efficient, clean, readable, Python code

Me and a friend setup a competition between ourselves with the goal: Print out all numbers in the Fibonacci sequence that are less than or equal to 3000 using Python. The winner was whoever could ...
6
votes
2answers
214 views

Seconds between datestimes excluding weekends and evenings

The basic idea here is that I want to measure the number of seconds between two python datetime objects. However, I only want to count hours between 8:00 and 17:00, as well as skipping weekends ...
4
votes
1answer
67 views

Nicely tabulate a list of strings for printing

I have a list of strings of variable lengths, and I want to pretty-print them so they are lining up in columns. I have the following code, which works as I want it to currently, but I feel it is a ...
4
votes
1answer
92 views

Should I keep my import namespace absolute or give in to “from foo import bar” (lxml/etree stuff)

It seems that lxml/etree are generally imported as such from lxml import etree -- why is that? It keeps the code tidier, and while the potential namespace ambiguity might not be a concern, I don't ...
3
votes
3answers
119 views

Blackjack card game

It's been a long while since I've done any Python programming so I thought I'd start with a simple blackjack game. Any comments on how I can make this more readable? Use more methods perhaps? from ...
3
votes
2answers
604 views

Python implementation of the longest increasing subsequence problem

Prompted by this question on Stackoverflow, I wrote an implementation in Python of the longest increasing subsequence problem. In a nutshell, the problem is: given a sequence of numbers, remove the ...
3
votes
1answer
97 views

How can I improve this python script that counts the number of days worked for all commiters to a git repo?

I wrote this script to collect evidence of the number of days worked for the purpose of claiming some government tax credits. I'm looking for some ways to clean it up, I'm especially wondering if ...
2
votes
2answers
112 views

Python class review - wiki api getter

First, I know there are other Python wiki API classes out there. I'm writing this one because I don't need all the bells and whistles, no edits, no talks, etc. I just need to be able to search for ...
2
votes
1answer
199 views

PEG parser in Python

Any suggestions to make this code clearer, more Pythonic, or otherwise better? I'm open to changes to the design as well as the code (but probably won't drop features or error checking since ...
2
votes
3answers
63 views

Building and getting a form of objects that have multiple properties

I'm building a form dynamically in a web app where I'm using product_id to keep track which product I'm reading and then product_<id>_property to grab the value of it. Then in my view I end up ...
2
votes
1answer
129 views

Logic/Code duplication, improve readability?

I have a function that takes a point, and given its velocity, and acceleration, calculates the time(s) for the point to collide with the line: def ParabolaLineCollision(pos, vel, acc, line): """A ...
2
votes
1answer
48 views

Readability: literal dictionary lookup vs if-else

I'm having concerns about readability of this piece of code: messages_dict = {'error':errors, 'warning':warnings}[severity] messages_dict[field_key] = message and I'm to use this instead: if ...
1
vote
1answer
43 views

Create palindrome by rearranging letters of a word

Inspired by a recent question that caught my interest, I wrote a function in Python 3.3 to rearrange the letters of a given string to create a (any!) palindrome: Count the occurrences of each letter ...
1
vote
1answer
350 views

Dictionary-based dispatch in Python with multiple parameters

Is there a better way to express the dispatch (switch) code without using a lambda? I think I need the lambda to act as an intermediary because the functions have varying numbers of parameters. def ...