Use this tag if you are specifically using Python 2.7. Such questions should be tagged with [python] as well.
3
votes
1answer
46 views
Ping Pong Pi - A ping pong score and serving manager
I have spent a few hours on this code, but think it could be improved a bit. It requires eSpeak to be installed to run the speech synthesis, but the voice can be toggled on or off using the ...
5
votes
3answers
72 views
Use up all characters to form words
Task:
Given a dictionary of words, and a set of characters, judge if all the
characters can form the words from the dictionary, without any
characters left. For example, given the dictionary ...
4
votes
2answers
123 views
Best way to display big data
I would like to display this data, callable at any time, but I'm not sure if this is the best practice to do so.
countries = [
dict(Name = "China", Population = "1,363,560,000"),
dict(Name = ...
6
votes
1answer
76 views
Creating an object in Python with lots of long arguments
date = self.talkDetailsWidget.dateEdit.date()
time = self.talkDetailsWidget.timeEdit.time()
presentation = Presentation(
unicode(self.talkDetailsWidget.titleLineEdit.text()).strip(),
...
8
votes
2answers
312 views
Virtual machine using RPython and PyPy
I'm writing a virtual machine in Python using RPython and the PyPy toolchain. The RPython will still work in the ordinary Python 2 interpreter, it's just a bit slow unless it's compiled to C code with ...
1
vote
1answer
48 views
Script scraper for outputting variables and functions to a text file
I've been programming with Python 2.7 for about six months now and if possible, I'd like some critique to see what I might have done better or what I might be doing wrong.
My objective was to make a ...
6
votes
3answers
205 views
Streamlined for-loop for comparing two lists
I'm new to Python and I'm wondering is there a way to streamline this clunky code I've written. Maybe a built-in function I've never come across before?
I run through two lists of binary numbers and ...
1
vote
1answer
54 views
Depth first search, use of global variables, and correctness of time
As I was coding this algorithm I found that some variables that are meant to be global behave as they are intended to behave, but others do not. Specifically, my boolean arrays, e.g. processed and ...
6
votes
1answer
107 views
How can this python quadtree be improved?
Here's an attempt I've made of implementing the Barnes-Hut nbody algorithm, or its initial stage - The quadtree. That there're lots of lengthy doc strings might excuse the lack of detailed explanation ...
0
votes
1answer
46 views
Review code for sorted dictionary [closed]
I want to iterate through the dictionary which is sorted with values. One thing I could do is used OrderedDict, which means I need to initialise the dictionary with sorted values.
Here's a minimal ...
4
votes
1answer
82 views
Fallout-style homework game
I've been going through LPHW (learn Python the hard way) lessons and I am now at exercise No36 where I have to create a similar game.
Could you please review it and point out beginner mistakes?
...
3
votes
1answer
69 views
Improving a triangulation test script
I am a relative beginner to Python and as such I've been working on little things here and there in the office that strike me as something interesting that might be fun to try and code a solution.
...
6
votes
2answers
114 views
Python Hangman program
To learn Python, I've been working through Learn Python the Hard Way, and to exercise my Python skills I wrote a little Python Hangman game (PyHangman - creative, right?):
#! /usr/bin/env python2.7
...
2
votes
0answers
45 views
Optimize Python script for memory which opens and reads multiple times the same files
I have code that works perfectly, but it uses too much memory.
Essentially this code takes an input file (lets call it an index, that is 2 column tab-separated) that searches in a second input file ...
1
vote
1answer
41 views
Use of multiple value errors in Python
Review this code.
mode = "PARALLE"
try:
if mode == "PARALLEL":
# some code - this will not raise ValueError
elif mode == "SERIES":
# some code
else:
raise ...
1
vote
0answers
71 views
Checking multiple keys in a dictionary [closed]
1st file:
for loop:
if all(k in data for k in ('current', 'resistance')):
current = data["current"]
resistance = data['resistance']
MyClass.myfunction(1st_arg, 2nd_arg, current, ...
5
votes
3answers
60 views
Extracting changesets (or commits) between last two tags in Mercurial with Python
I have written the following script:
#!/usr/bin/python
import os
os.system('hg tags > tags.txt')
file = 'tags.txt'
path = os.path.join(os.getcwd(), file)
fp = open(path)
for i, line in ...
2
votes
1answer
76 views
Copy a directory structure to another, but only copying specific files
script.py:
#!/usr/bin/python
import os
srcDir = os.getcwd()
dirName = 'target_directory'
dstDir = os.path.abspath(dirName)
def ignore_list(path, files):
filesToIgnore = []
for fileName ...
0
votes
1answer
118 views
bottle.py upload document to mongodb
Simple post and download web app in bottle.
server.py
import sys
import bottle
from bottle import request, response
import pymongo
import gridfs
from bson.objectid import ObjectId
import ...
3
votes
1answer
73 views
Is it a good style to run external commands in Python?
I have lots of external shell commands to run.
So I gave every command a name and then run it.
tar_report_cmd = 'zip -r {0} {1} '%(report_file, _export_dir)
exec_proc(tar_report_cmd)
mv_report_cmd ...
3
votes
1answer
177 views
Simple in-memory database
How can I improve this code (elegance, best practices, etc)?
""" Simple in-memory database as a response to the Thumbtack coding challenge. """
class SimpleDb(object):
""" Simple in-memory ...
7
votes
2answers
82 views
Long constructors, inheritance, class method constructors
My class takes functions as a parameter. There are 6 of them, along with numerous other parameters. Almost all of the fields have default values.
It also has a class method that creates an instance ...
9
votes
1answer
64 views
Shortcuts and imports for large RPG basic code
I decided to work on putting together an Arena-style (very basic) text-based RPG as a way to help myself learn Python. Unfortunately, after about 1,000 lines of pieced-together code, I realize that ...
3
votes
1answer
58 views
Paragraph Matching in Python
So I developed some code as part of a larger project. I came upon a problem with how to match paragraphs, and wasn't sure how to proceed, so I asked on Stack Overflow here. You can find an in-depth ...
6
votes
1answer
148 views
Faster computation of barycentric coordinates for many points
I'm just starting to understand the Python syntax and I created a module that does what I wanted, but really slow.
Here are the stats of cProfile, top 10 ordered by internal time:
ncalls tottime ...
4
votes
1answer
120 views
Making this Pygame code object-oriented
PYTHON 2.7-
I want to make my code OOP. I also want feedback from you on correctness, tidiness, design patterns and so on.
Here's the download link. It's not permanent but it's the best I have ...
8
votes
3answers
200 views
Model cars as classes
I am learning about object oriented programming in Python using classes. I have attached an image of what I had in mind when structuring my classes. This is supposed script model cars. I want to know ...
1
vote
1answer
37 views
Function to find midpoint of two floats where one of the floats might be an empty string
There must surely be a nicer way of writing this.
The wrinkle of complexity is that prev_seq or next_seq might be an empty string if the user moves the question to the first or last position, but it ...
3
votes
2answers
96 views
Python coding style from Java background
I am pretty new to Python and just wanted to make sure I am going in the right direction with my coding style.
After reading this, I'm not so sure.
The following is a convenience class that has ...
5
votes
3answers
171 views
if statements or eval?
I'm wondering about the following code:
def ValidateGoto(placeToGo):
conditions = {}
conditions["not placeToGo"] = "Enter a positive integer in the text box"
conditions[
...
5
votes
1answer
138 views
Improvements on Python game?
This is a game I made in Python. While it is not my first, I am not happy with the result. I would like suggestions on ways I can make it better, more user-friendly, and more enjoyable. Also, if ...
2
votes
1answer
35 views
Produce product of lists
I want to combine elements in different lists.
For example, here are 3 lists.
list_a : [ 1,2,3 ]
list_b : [ 4,5,6 ]
list_c : [ 7,8 ]
Selecting elements from list_b and list_c is mutually exclusive. ...
6
votes
2answers
162 views
A small Python text adventure “frame”
I've been working on a small "frame" for a text adventure in Python.
So it's less of a real adventure and more of a small testing location for all the commands.
I pretty much just started learning ...
1
vote
0answers
132 views
Python urllib proxy access function - Coverage of possible proxy scenarios
Several of my Python 2.7 programs need to access server tasks via our server apache instance.
The 'client' programs will run on Windows in various environments. They may be run on systems that do not ...
16
votes
4answers
1k views
Regex to parse semicolon-delimited fields is too slow
I have a file with just 3500 lines like these:
filecontent= "13P397;Fotostuff;t;IBM;IBM lalala 123|IBM lalala 1234;28.000 things;;IBMlalala123|IBMlalala1234"
Then I want to grab every line from the ...
2
votes
2answers
267 views
Optimizing Edit Distance Solution
I was doing a standard problem of DP(Dynamic programming) on spoj Edit Distance using python.
t = raw_input()
for i in range(int(t)):
a,b = raw_input(),raw_input()
r = len(a)
c = ...
3
votes
2answers
107 views
Text file reading and printing data
MyText.txt
This is line 1
This is line 2
Time Taken for writing this# 0 days 0 hrs 1 min 5 sec
Nothing Important
Sample Text
Objective
To read the text file and find if "Sample Test is ...
2
votes
1answer
122 views
Opinions/Improvements on a run periodically/timer function in Python 2.7
I'm looking for some opinions on this bit of code that I wrote and if there are any ways it can be improved.
The scripts aim is to run the function runme() every 60 seconds with a random interval ...
1
vote
3answers
175 views
How to optimize this simple Python program?
Here is a simple piece of code in Python. It is a solution to the SPOJ's JPESEL problem. Basically the problem was to calculate cross product of 2 vectors modulo 10. If there is a positive remainder, ...
3
votes
1answer
136 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, ...
3
votes
2answers
2k views
Multiple Choice Quiz with Stat Tracking
I was hoping I could get some of you to look over a program I wrote. I am just beginning to learn about Python (for the last month or so). Therefore, I may not be aware of techniques that would make ...
5
votes
3answers
510 views
Python 2.7: Good practice or bad practice? (particularly the use of global)
I've been learning Python 2.7 for about a week now and written a quick program to ask for a names of batters then print a score sheet. I have then added the names to a list which I will use more in ...
0
votes
0answers
88 views
Python 2.7 - Abstract Programming [duplicate]
Possible Duplicate:
Can I make the class more abstract?
import math
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def move(self,x,y):
self.x += x
self.y += y
...
3
votes
1answer
185 views
Can I make the class more abstract?
import math
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def move(self,x,y):
self.x += x
self.y += y
def __str__(self):
return ...
1
vote
3answers
453 views
Long-Winded code (multiple 'if' statements)
Any thoughts on my code? It's a simple hashing code written in Python. The code runs from the command line.
There just appears to be a lot of condition "if / elif" entries in both of the main ...
7
votes
1answer
600 views
Welcoming any comments on this Python program that keeps remote folders in sync with local ones
This started as a hacked-together tool to remove annoyances I was facing with experimenting with code on live remote servers, then getting that code into my development environment after ...
3
votes
1answer
880 views
Refactoring - Pythonic, speed and memory optimization
I have fair concept in a programming (learner) but not an expert to refactor code at the highest level. I am trying to read huge (100MB-2GB) XML file and parse necessary element (attributes )from file ...
2
votes
1answer
256 views
Can you review my i18n/Jinja2/python code?
I'm learning to Jinja2 and what's important in this case isn't speed but i18n translations and functionality. With python 2.7 Jinja2 and not django seems to preferred way to go so I'm rewriting much ...
2
votes
1answer
450 views
Is this a safe/correct way to make a python LogHandler asynchronous?
I'm using some slow-ish emit() methods in Python (2.7) logging (email, http POST, etc.) and having them done synchronously in the calling thread is delaying web requests. I put together this function ...