Tagged Questions
3
votes
2answers
82 views
Simplify an if else construct with assignment before if and else
I want to simplify an if: .. else: .. construct:
...
7
votes
1answer
108 views
Observer pattern implementation without subclassing
I have not yet seen an implementation of the observer pattern in Python which satisfies the following criteria:
A thing which is observed should not keep its observers alive if all other references ...
5
votes
1answer
110 views
Increasing readability of maximum path sum algorithm
I had written a solution for Project Euler problem 18 in Python 3. It is good in its efficiency but the problem is the readability and I think reuse.
The algorithm used is that at every iteration ...
1
vote
1answer
81 views
Commenting in simple programs [duplicate]
I am struggling with commenting and variable-naming. My teacher said I need to add comments to my below code. I am not sure where I could add comments (I feel it is self explanatory) or what I ...
5
votes
1answer
445 views
Newton's method to solve cubic equations
I have deleted the previous question I had posted, and am reposting the program with the new version of the code without the error present in the previous one.
I have used the Newton-Raphson method ...
6
votes
2answers
210 views
python neural network: arbitrary number of hidden nodes
I'm trying to write a neural network that only requires the user to specify the dimensionality of the network. Concretely, the user might define a network like this:
...
7
votes
1answer
140 views
Improving readability of Boolean adder generator?
Today I've noticed that the source code for a Boolean adder is very repetitive, so I wrote a Python script to generate the C source code. The script generates an adder of varying size depending on ...
5
votes
4answers
375 views
2
votes
1answer
196 views
3
votes
1answer
145 views
Calculus program — Code review and suggestions
I have written a simple command line program in python3 that accepts a function (in the mathematical sense) from a user and then does various "calculus things" to it. I did it for my own purposes ...
2
votes
3answers
150 views
Is this chained comparison readable?
Is this chained comparison easily readable and understandable?
if result == problem.solution is not None:
...
Or should it be changed to its possibly more ...
3
votes
1answer
1k views
Create palindrome by rearranging letters of a word
Inspired by a recent question that caught my interest (now deleted), I wrote a function in Python 3 to rearrange the letters of a given string to create a (any!) palindrome:
Count the occurrences of ...
4
votes
1answer
475 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 ...
2
votes
1answer
109 views
Readability concerns with 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
...
2
votes
1answer
367 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 ...
11
votes
2answers
503 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 ...
2
votes
2answers
204 views
Wiki API getter
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 titles and ...
3
votes
1answer
111 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 ...
5
votes
3answers
282 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?
...
2
votes
3answers
70 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 ...
2
votes
1answer
150 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:
...
3
votes
2answers
1k 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 ...
1
vote
1answer
662 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.
...
4
votes
1answer
124 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 ...
6
votes
0answers
229 views
Efficient, clean, readable, Python code [closed]
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 ...