Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.

learn more… | top users | synonyms

3
votes
1answer
260 views

The Observer design pattern in Python in a more pythonic way (plus unit testing best practices)

I'm continuing to work on the Head First Design Patterns book in an effort to become a more efficient and better Python programmer. Code review for the Strategy pattern in Chapter 1 is here with ...
3
votes
2answers
465 views

How can I improve the performance of this code?

I was solving the Find the Min problem on facebook hackercup. The code below works fine for the sample inputs given there, but for input size as big as 10^9 this takes hours to return the solution. ...
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 ...
2
votes
3answers
142 views

A pythonic way of de-interleaving a list (i.e. data from a generator), into multiple lists

I've recently discovered the wonders of the Python world, and am quickly learning. Coming from Windows/C#/.NET, I find it refreshing working in Python on Linux. A day you've learned something new is ...
3
votes
3answers
95 views

How can this function be faster? Solving for a row of Pascal's triangle

I saw a posting on Hacker News this morning that was ranting about people not being able to solve an interview question. I thought I would give it a shot, but I would like to know how my attempt could ...
1
vote
1answer
178 views

Python Twitter parser, looking for general code review

In the interests of improving my Python coding skills, I wanted to post a program I recently built and get it critiqued by you fine folks. Please let me know where you think I might improve this ...
1
vote
1answer
78 views

proxy pattern in Python

This is my try at the proxy pattern. What do you Pythoneers think of my attempt? class Image: def __init__( self, filename ): self._filename = filename def load_image_from_disk( self ...
-2
votes
1answer
119 views

Checking for possible errors. Calendar functions

def days_difference(day1, day2): ''' (int, int) -> int Return the number of days from the first parameter to the second parameter, which are both in the range 1-365 (and thus indicate a day of the ...
3
votes
1answer
337 views

The Strategy design pattern for Python in a more Pythonic way

I've recently picked up the Head First Design Patterns book in an effort to become a more efficient and better Python programmer. Unfortunately, the code examples in this book are in Java. I'm not ...
3
votes
4answers
212 views

How can I make this python code shorter?

My son (9) is learning Python and wrote this temperature conversion program. He can see that there is a lot of repetition in it and would like to get feedback on ways to make it shorter. def ...
0
votes
1answer
48 views

Another way to write this program

I have the following block of codes. it computes the 'Four color theorem'. I converted it from a java program. is there any way to write the name of the countries in map, instead of their id? from ...
10
votes
3answers
272 views

First Python program (Automatically turn on my pc when my smartphone is at home from my Raspberry PI)

This has been my 1st (and lovely) Python (and more in general programming) experience. I know the code is full of useless data and stylistic and analysis errors and I'd really love and appreciate to ...
0
votes
0answers
49 views

Python pattern searching using re standard lib, str.find()

I'm trying to improve some code in order to get a better perfomance, I have to do a lot of pattern matching for little tags on medium large strings, for example: import re STR = ...
4
votes
1answer
87 views

Python Adventure . In need of debugging and improvement finding

I'm a python coder [beginner] and this is the first program that i have ever programmed. Please suggest improvements/bugs Here's the link to the code : PythonProgram global hp hp = 20 global dice ...
1
vote
1answer
36 views

How improve Python datetime based code

I have this code into a class that get a string, my goal is parse the string , do some adjusts to get a valid datetime format from the string. An input string for this function could be = ...
1
vote
1answer
957 views

Dynamic programming solution to knapsack problem

I wrote a solution to the Knapsack problem in Python, using a bottom-up dynamic programming algorithm. It correctly computes the optimal value, given a list of items with values and weights, and a ...
1
vote
1answer
57 views

POST data handler

I have written a function to handle post data received from a web page. The Emphasis is on making getting post data easy: using the function allows the coder to specify the required data, type, and ...
3
votes
1answer
54 views

Python: Improve csv script efficiency

I'm tasked with getting emails from a .csv file and using them to submit a form. I am using the csv and mechanize Python libraries to achieve this. This is my code: import re import mechanize ...
5
votes
1answer
144 views

A* search algorithm: open set and heap

I'm working on an A* search algorithm implemantion and this is what I came up with for the open list: from heapq import heappush, heappop class OpenList(set): ''' This uses a heap for fast ...
1
vote
2answers
92 views

Accessing the contents of a project's root directory in Python

This morning I was trying to find a good way of using os.path to load the content of a text file into memory, which exists in the root directory of my current project. This approach strikes me as a ...
2
votes
1answer
71 views

Python parallelization using Popen

I frequently run a script similar to the one below to analyze an arbitrary number of files in parallel on a computer with 8 cores. I use Popen to control each thread, but sometimes run into problems ...
1
vote
1answer
102 views

Help me simplify this gigantic class

I'm trying to think of ways to separate things out. I'm open to ideas, or if you see anything blatantly wrong, I'd like to know that too. Generally, I'm happy with this, but the sheer size of the ...
1
vote
1answer
297 views

Finding the longest common subsequence algorithm using hash table Slow

I've designed an algorithm to find the longest common subsequence. these are steps: Starts with i = 0 Picks the first letter from the first string start from ith letter. Go to the second string ...
0
votes
1answer
62 views

(revised) Python script to access genealogy api for list of users and attributes

The problem the code is solving This is a script to access the stackexchange api for the genealogy site and create a list of user dictionaries. Ie each user dictionary contains the info about that ...
1
vote
1answer
41 views

Are these set-uid scripts/binaries secure?

I have a system that needs to be able to reboot a different piece of hardware partway through a script that programs it. It used to wait for me to come and reboot the hardware halfway through, but ...
1
vote
1answer
55 views

Refactor Python code with for-loops and continue

I wrote a code that queries a data structure that contains various triples - 3-item tuples in the form subject-predicate-object. I wrote this code based on an exercise from Programming the Semantic ...
1
vote
1answer
63 views

Is there a cleaner way of building a list of the previous, next months than this?

At first I tried using a timedelta but it doesn't accept months as an argument, so my next working example is this: from datetime import datetime current_date = datetime.now() months = [] for ...
3
votes
1answer
384 views

How can I do this the most pythonic way

I'm developing model for web application. The database that I use is Redis. I created special DbField objects like HashDbField, SortedSetDbField, ScalarDbField and so on, for each data type in Redis. ...
5
votes
1answer
517 views

Python Code Review - time and temperature displaying program for Raspberry Pi

If possible, can someone please review my code? Especially the use of the Global variables, indentation and comments. This is my first attempt at python and it took me a month to come up with this. ...
1
vote
4answers
140 views

New to Python and trying to make a game want to be sure im not doing anything that should be avoided

As the title says, I am just learning to code in python and have been reading the book "Learn python the hard way". In chapter 45 he basically says: "Ok, go figure out how to make a text based game" ...
2
votes
1answer
60 views

More efficient way to find the mode of an array/iterable? (Python 2.7)

I'm currently using scipy's mode function to find the most occurring item in different iterable objects. I like the mode function because it works on every object type I've thrown at it (strings, ...
1
vote
0answers
216 views

flask-SQLAlchemy models and unit-testing

I believe this post belongs here vs stackoverflow or Database administrators but let me know if i'm wrong. This is my first time building anything remotely like a robust database. I'm midway through ...
2
votes
3answers
122 views

Python function speedup/optimization

everybody...I am working on a p2p streaming sim (which I think is correct), however I've discovered a huge bottleneck in my code: During the run of my sim, the function I include below gets called ...
0
votes
1answer
50 views

Python Help: no conditionals, no loops, nothing but simple code problem [closed]

I need help doing this problem without any conditionals or loops: Write the function getInRange which takes 3 values (which you may assume are all numeric) -- x, bound1, and bound2, where bound1 is ...
2
votes
1answer
215 views

Simple crypto library in Python - correct and secure?

Apologies if this is too broad. Is the code below (also available at github with tests, example, and description of algorithms) correct and secure? It follows the recommendations at ...
4
votes
1answer
134 views

Building a list of dates between two dates

My solution to this feels 'icky' and I've got calendar math falling out of my ears after working on similar problems for a week so I can't think straight about this. Is there a better way to code ...
1
vote
1answer
56 views

Django how to make this view faster?

I have view and it works correct, but very slow class Reading(models.Model): meter = models.ForeignKey(Meter, verbose_name=_('meter')) reading = models.FloatField(verbose_name=_('reading')) ...
1
vote
0answers
25 views

DOMDocument field class Python

What do you think of this class? Is this simple to understand how it works and what it does? Do you think it is good piece of code? class Field(object): """Class which contains logic of ...
0
votes
1answer
49 views

Exclusive Queue Implementation from Little book of semaphores

I tried implementing the exclusive queue as given in the Little book of semaphores. But I suspect there's a deadlock occurring as i see no progress. Not able to figure out the issue. Any pointers ...
2
votes
0answers
148 views

Is there better way to read from a file and connect all data in one big data than to use generators?

Is there better way to read from a file and connect all data in one big data than to use generators? At the moment, I do the following: use generators to read data from files. use numpy to pack all ...
4
votes
1answer
214 views

Meeting Point problem from interviewstreet.com

I am trying to solve the "Meeting Point" problem from interviewstreet.com: There is an infinite integer grid at which N people have their houses on. They decide to unite at a common meeting place, ...
3
votes
1answer
125 views

Authentication for a Flask API

I've written a couple of functions to check if a consumer of the API should be authenticated to use it or not. Have I done anything blatantly wrong here? Would appreciate some feedback. Config ...
2
votes
1answer
191 views

python alternative to complex if-else structure

I've been on stackoverflow looking for an alternative to the ugly if-elif-else structure shown below. The if-else structure takes three values as input and returns a pre-formatted string used for an ...
0
votes
1answer
35 views

Python Boto Script - Sorting based on date

I had a question regarding my code which downloads a file from S3 with the highest (most recent) timedated filename format: YYYYMMDDHHMMSS.zip from boto.s3.connection import S3Connection from ...
4
votes
4answers
93 views

default arguments and parameter values in python

I have a python file with several function definitions in it. One of the functions, named 'main' will be called as soon as the python file is run. ex: myFile.py import sys def main(arg1....): ...
3
votes
3answers
129 views

How can I clean up this python code?

I am very new to programming and this is my first functional code. It works fine but I'm sure that I could use a lot of optimization. If you see any blunders or would be able to help condense the ...
7
votes
2answers
503 views

Python novice - TicTac Toe GUI code review

I'm a code banger from way back trying to learn good Python style. Here is my attempt at the classic game. I'm trying to learn Tk, because I have a couple codes that need GUIs. Any suggestions or ...
1
vote
2answers
61 views

Http url validating

What do you think about this? #utils.py def is_http_url(s): """ Returns true if s is valid http url, else false Arguments: - `s`: """ if ...
3
votes
2answers
173 views

Removing the massive amount of for-loops in this code

Are there ways to avoid this triply nested for-loop? def add_random_fields(): from numpy.random import rand server = couchdb.Server() databases = [database for database in server if not ...
2
votes
1answer
81 views

Python3: find sub-list

Pythonic way of expressing the simple problem: Tell if the list needle is sublist of haystack #!/usr/bin/env python3 def sublist (haystack, needle): def start (): i = ...

1 3 4 5 6 7 17