Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.
0
votes
0answers
6 views
Calendar in python (embedded) [on hold]
I have a sloppy first program I want to learn how to simplify. DSM suggested using dictionary lookups to simplify complex if statements. I will read about that on my own but any advice would be ...
0
votes
0answers
11 views
Calendar in Python [on hold]
I have a sloppy first program I want to learn how to simplify. DSM suggested using dictionary lookups to simplify complex if statements. I will read about that on my own but any advice would be ...
3
votes
0answers
10 views
Produce bitcoin private key from 31 playing cards
I've written some Python code to generate a random hexadecimal string using 31 playing cards drawn without replacement (so no card appears more than once). This is sufficient to provide more than 160 ...
4
votes
1answer
15 views
Solving the Reve's puzzle
I'm trying to solve the Reve's puzzle (a variant of the Tower of Hanoi puzzle with four pegs). This is what I've come up with so far:
def solve(disk, source, temppeg1, temppeg2, destination):
if ...
0
votes
0answers
28 views
First Chess project
I'd made a project using pygame in python around 3 months ago. This was my first big project after I started learning how to program in college.
Now I'd like to know just how good my programming ...
4
votes
4answers
288 views
Sieve of Eratosthenes - Python
So I've been doing a lot of Project Euler lately and just wanted to make sure my implementation was as good as it could be. Does anyone have any suggestions to speed this up?
def sieve(upperlimit):
...
0
votes
0answers
5 views
Usage of __ while declaring any variables or class member in python [migrated]
Is it good practice to use __ convention while declaring the member variable?
This also imparts private kind of feature of that data member. There have been cases when I found that its good to have ...
12
votes
2answers
73 views
Condorcet voting method in OOP Python
I'm currently learning Python and having a great time so far (coming from PHP, I have a feeling of suddenly seeing the light).
At the same time, I'm studying voting algorithms. So, for training ...
3
votes
3answers
73 views
Checking if multiple user-entered numbers are in the right range
str, ski = int(input("text here")), int(input("text here"))
if str < 1 or str > 50 or ski < 1 or ski > 50:
print("did not enter between 1 and 50")
Is there anyway to shorten ...
5
votes
3answers
42 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 ...
6
votes
3answers
93 views
Refactoring if-else structure with elements of dict
I have code which gets data from a POST request (request.form dict), compares with corresponding attribute in object accident and does some action. The code looks needlessly verbose. How can I ...
6
votes
1answer
89 views
Simple console roguelike game
I have written this small game for the "learn python the hard way" book. I ask for some opinions about it and how to improve it.
I know about pygame and others game engines but all the game runs in ...
3
votes
1answer
60 views
Does this tkinter-based web browser widget use a well implemented class?
I have fully functional code in class format, and as far as I am aware from my previous question, it conforms to PEP 8 and the code logic and implementation is suitably pythonic. My main concerns here ...
3
votes
0answers
27 views
A mongodb inventory system — More efficient db calls?
I am trying to develop a simple inventory system that takes JSON data gathered from a powershell script, inputs the data into a mongodb, and displays the info in an interesting manner for the user. It ...
3
votes
1answer
30 views
Tornado quiz app
I am writing a simple quiz app using Tornado and MongoDB. The user authentication is done using Facebook and each user is uniquely identified by their Facebook ID. When a user first creates an ...
1
vote
0answers
43 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
31 views
Class-based signal generator [on hold]
I'm creating a class-based signal generator that, given a buy threshold, sell threshold and a list of list of indicators, creates a 'buy' of 'sell' signal on a given day if the indicator from that day ...
16
votes
4answers
2k views
Simple card game to learn OOP
My goal was to get my hands dirty in OOP by designing and using classes and getting started with inheritance and other OOP concepts.
I have written a very small code to play a card Game called ...
0
votes
1answer
57 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 ...
9
votes
2answers
82 views
Improve performance of math function applied to arrays
This block of code is already somewhat optimized thanks to some answers given over at Stack Overflow. In the last question I made (Improve performance of function without parallelization) the code was ...
4
votes
2answers
39 views
Proxy evasion browser in Python
This code works. It is designed to retrieve website data directly, parse it, and open it as normal. I will develop it further later. Right now I want to know if there is any way to improve efficiency, ...
6
votes
2answers
107 views
Exercism assignment word-count in Python
Can someone review my code which I put on exercism.io for the word-count exercise (found here)?
class Phrase:
def __init__(self, phrase):
self.phrase = phrase.strip().lower().split()
...
2
votes
1answer
60 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
148 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 ...
6
votes
2answers
158 views
Performance of collision detection in a grid
I have fairly simple collision checking and handling Python code which is currently bottlenecking my performance quite a bit. I haven't done too much coding in python and I'm quite sure there's ...
2
votes
1answer
31 views
Loop filling a set with logging
After watching Raymond Hettingers talk Transforming Code into Beautiful, Idiomatic Python I got back to a function I wrote.
I'm not quite sure how to make it more pythonic but I think this might be a ...
4
votes
1answer
68 views
Byte by byte directory comparison ignoring folder structures and file name differences
I haven't been able to find an existing tool that does this, so I'm attempting to create one. If anyone knows of one that already exists, I'd appreciate a pointer to it. I plan on using this primarily ...
-1
votes
0answers
29 views
Why is this code not working? [closed]
This is driving me crazy, I am working on a circuit simulation program, and every time I ask a question about it it gets closed, and I'm beginning to get very angry.
I seriously need help, but my ...
0
votes
0answers
12 views
Class being incorrectly identified? [closed]
This code definitely needs some reviewing. I seriously can't figure out my error here.
I am working a virtual circuit creation program, where you lay down wires and other components to create small ...
5
votes
1answer
64 views
Case study with a biological populations. A list of lists of lists
I have a population (Pop) which has an attribute which is a list of individuals (Ind) where each individual has an attribute which is a list of chromosomes (Chromo). Each chromosome is a list of ...
8
votes
1answer
107 views
Rotating greyscale images
For educational purposes I wrote a little piece of code to rotate greyscale images as "low level" as possible, that is, not using any rotate() function, but doing the math. I was wondering if it could ...
0
votes
0answers
59 views
Script to import repositories to Bitbucket
I needed a script to import repositories from other Bitbucket account to my own Bitbucket account. I have some 250 repositories in my account and I wanted them to move under Team (which is same as ...
0
votes
1answer
49 views
Naming for a variable in before and after status and different type of variable
I'm not a native English speaker, so I'm unsure about this. Is it ok to name a variable and put the prefix "before" and "after"? If not, how could I give it a better name?
before_len = ...
17
votes
4answers
2k views
Is it a good practice to put an exception handler in every method?
I always add an exception handler when a new method is created, and I don't know if it is a good or bad practice. I thought it may be a bit of an annoyance when viewing the source code.
Is it a good ...
6
votes
1answer
97 views
Strange performance difference
For exercise, I wrote this code:
def BigPrime(numP, plist = [], x = 1, counter =0):
while len(plist) <= numP:
if x == 1 or x == 2 or x == 5:
plist.append(x)
...
6
votes
0answers
52 views
What is the most pythonic way to solve a Differential equation using the euler method?
For example with this Differential Equation how can I do it in a pythonic way?
dx/dt=f(x,t)
I have a way to do it but I think this is not the most pythonic way to do it. Here is how I do it:
...
6
votes
1answer
48 views
Python compress and send
The following two functions are used to compress arbitrary Python objects and send them safely via socket or email, using only printable chars. In my specific message protocol, '=' signs are also not ...
2
votes
1answer
70 views
Run a TCP server listening for incoming messages
I've written the following code, but I need some review on design style. I want to develop a library/module and expose an API to client programs. How do I do that?
The code just runs a TCP server ...
9
votes
3answers
307 views
Installing “mods” with Python
A little disclaimer: you're going to have a field day with this. It's horrible. Looking at it makes me want to vomit, and don't ask me to explain it, because I've long forgotten what I was thinking ...
3
votes
1answer
46 views
Insertion sort with binary search in Python
def binary_search(A, value, start, end):
# we need to distinugish whether we should insert
# before or after the left boundary.
# imagine [0] is the last step of the binary search
# ...
4
votes
1answer
48 views
Dice game in Python
I was wondering if this script in Python could be shortened?
import random
def var():
strength = 10
skill = 10
dice4 = 0
dice12 = 0
dice_score = 0
character_name = ""
...
6
votes
1answer
29 views
Script to checkout multiple repositories to a certain commit hash
I am writing a script which checkout a git repo to certain commit hash, do something and switch back to master. The purpose of this script is to take homework solutions of students from bitbucket. ...
7
votes
2answers
65 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 ...
7
votes
1answer
36 views
Review implementation for Partial Ordering of Vectors
Partial ordering may be useful in some scientific contexts. This is quick prototype in Python, which I believe works correctly.
Do you notice any flaws? Can you provide a counter-example of wrong ...
0
votes
0answers
21 views
Using module level declared global “singletons” in python [migrated]
Ok, I know that using singletons is generally a bad practice, but if I do it (for db connection, logging et al.) am I allowed to go (in respect of clean design) with a module defined variable that is ...
5
votes
1answer
62 views
Python script using distutils to copy files on a Mac
I have a Python script I have written to copy files to a mounted Windows SMB share on a Mac.
import os
import distutils.core
# Create a local site for the mount to reside
directory = ...
4
votes
1answer
57 views
Fastest Way to Write Multiple CSV's
I'm writing a program to process log files. Currently, I have a script that will extract the desired information from each log and put it into a list - I have about nine different lists all ...
3
votes
2answers
50 views
One-file script that detects changes in file-systems via polling and Pythonic idiomatic-ness
This is a (relatively) cleaned-up version of a Python script I've been working on for the past day or two based on Tim Golden's first solution to the above problem. I know about watchdog but I'm ...
4
votes
1answer
124 views
Why does this python code take so long?
I don't understand what is taking the program so long:
#!/bin/python
primes=[]
i=0
j=0
k=0
for i in range(2,2000000): #fill in the list
primes.append(i)
i=0
while i<len(primes):
...
4
votes
1answer
60 views
Cutting strings into smaller ones based on specific criteria
So, I've got this largish (for me) script, and I want to see if anybody could tell me if there are any ways to improve it, both in terms of speed, amount of code and the quality of the code. I still ...