Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.
4
votes
1answer
22 views
Review a Python module i18n
To start, I am a developer Perl and Python. I am passionate and I love to write code. I discovered this site through stackoverflow and I think it's great. I love the code review, peer programming, ...
1
vote
0answers
16 views
Alternative to Python's Naive Bayes Classifier for Twitter Sentiment Mining
At the advice of someone on SO I'm posting this here.
I am doing sentiment analysis on tweets. I have code that I developed from following an online tutorial (found here: ...
3
votes
2answers
48 views
I know I'm not doing these methods right
I'm having difficulty understanding the implementation of methods in classes. I'm pretty sure I'm not doing this the correct way. Can anyone show me the way it should be done.
This code works by ...
0
votes
0answers
9 views
AnimCurve creator for Maya
Im currently working on rewriting some of my old scripts, in order to brush up my portfolio. I started with this old pymel tool I wrote a while back for maya.
I have both a question about it, as ...
1
vote
1answer
44 views
Python code snippet could probably be more elegant
I'm looking to let people define arbitrarily nested lists of lists and return a random json document for them. Right now the assumption is that if you have a structure like this:
[item1, item2, ...
2
votes
1answer
44 views
Critique my JSON-optimizing Python script
Here's a Python script to optimize JSON documents:
#!/usr/bin/python3
"""jopo - Optimize JSON documents."""
from collections import defaultdict
def optimize(data):
"""Optimize a JSON-like ...
0
votes
0answers
31 views
What can I use for speed, efficency and clean code instead of Python subprocess?
In my python app I call subprocess.Popen alot to open bash + python scripts
but I would like to keep everything Python for speed and efficency...
What are my alternatives to using subprocess.Popen to ...
0
votes
0answers
34 views
Why does this prime test work? [migrated]
i found this python snippet for testing if a number is prime and i cant figure out why the algorithm works
def isprime(n):
"""Returns True if n is prime"""
if n == 2: return True
if n == 3: ...
3
votes
3answers
65 views
How do i increase performance in my simple gcf calculator app?
I am trying to learn some basic python and am trying to find tricks to write speedy code.
import sys
if len(sys.argv) < 3:
print("You left out the arguments")
sys.exit()
gcf = 0
numbers ...
4
votes
5answers
125 views
New list from the elements of two lists popping sequentially
Can this be shortened, optimised or made more pythonic ?
a = [1,9,'foo',5,7]
b = ['bar',4,8,6]
c = []
min_len = min(len(a),len(b))
for i in range(min_len):
c.extend([a.pop(0), b.pop(0)])
...
3
votes
1answer
64 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 ...
0
votes
1answer
45 views
why does my code not append the variable like it should? python [on hold]
This is a brief description of what it is suppose to do.
I am trying to classify a data set with 21 columns and a lot of rows. I want to classify the rows of data based off of columns 4 and 5. These ...
0
votes
2answers
85 views
Why is my code for Kasoraju's algorithm so slow?
For Coursera's Algorithms course I have written Kosaraju's algorithms which calculates strongly connected components in a directed graph using depth first search.
The code itself is correct but ...
3
votes
2answers
144 views
Can someone help me optimize my DFS implementation in Python?
def dfs(graph, node):
"""Run DFS through the graph from the starting
node and return the nodes in order of finishing time.
"""
stack = [[node, True]]
while True in [x[1] for x in stack]:
i = 0
...
1
vote
0answers
35 views
Python files sync
I'm trying to do a one-way directory sync. Given a list of existing files in a dir, I'd like to make the files in the dir equal a new list of files. There will be subdirs under the dir. Because ...
4
votes
1answer
33 views
Finding unique/obsolete binary Debian packages
I have a script I use to prune packages that have gone stale from my custom Debian repository (i.e. they no longer exists on any of the official repositories):
import apt_pkg
import gzip
import ...
4
votes
2answers
88 views
General feedback on Conway's Game of Life implementation
I've just written an implementation of Conway's Game of Life as an exercise and I'd love to get some feedback on how I can improve this program and hopefully my style in general.
I've tested the code ...
3
votes
1answer
95 views
How to make code more pythonic?
Good day! I am writing my first programs on Python and want to be sure my code is good and not too C++ed. Consider the problem of finding all common 'associations' for set of 'keys'.
Data forman is
...
1
vote
1answer
48 views
python function compression
I have written this big function to do some formatting in my python code. Would you be able to suggest anyways to make this smaller ?
def disfun(String1,String2,String3):
if String3 == "A" ...
1
vote
1answer
78 views
Optimising If statement and for loops
I have two solutions (in jython) to take two pictures and copy and paste alternate sections of one picture to the other, like so:
for x in range (0,w):
for y in range (0,h):
...
1
vote
0answers
41 views
Pixel interpolation(binning?) algorithm
I am trying to write up a pixel interpolation(binning?) algorithm (I want to, for example, take four pixels and take their average and produce that average as a new pixel). I've had success with ...
3
votes
1answer
106 views
Is there a way to shorten this down?
scalemult=0
calibrate=convertStr(calibrate)
choice=convertStr(calibrate)
if(calibrate==1): #DENIS I-band
if(data[RowStar][17]=='------'or ...
4
votes
2answers
98 views
In-Place Quicksort in Python
Code is below. I'd like to know if this is a solid/efficient implementation of the in-place quicksort algorithm and if my Python style is good (I know there are no comments for now). Code is also on ...
4
votes
2answers
135 views
counting letters in text file
As a beginner Python programmer, I wrote a simple program that counts how many times each letter appears in a text file. It works fine, but I'd like to know if it's possible to improve it. Thanks :)
...
0
votes
0answers
21 views
Python Mongo cross collection efficiency
I've got a bit of code that I feel code be improved and I'm just throwing it out there for others to browse....
Background
I have two collections, Blocks and Items. Blocks can contain any number of ...
2
votes
2answers
154 views
How to improve this Python function?
This code works nicely for my purposes, but I would like to improve it exponentially.
What are some steps I can take? I'm interested in optimization, refactoring, and clean-code.
def ...
4
votes
3answers
152 views
Is my overall Python script design the right way to go?
So I'm relatively new to programming in general, and I've finally strapped in and started learning Python. This is my first significant project using it and I'm trying to build good habits with layout ...
2
votes
3answers
80 views
Python : Loop in a csv file with IF cases
I am new to Python and can't quite make my if-cases any shorter. Any ideas how to do that?
import csv
fileheader = csv.reader(open("test.csv"), delimiter=",")
# Defines the header of the file ...
2
votes
2answers
80 views
Trying to use classes in my python code. Not sure if being done correctly
The point of my code is to:
Read in the observed data from the a catalog of stars (whitespace separated table).
Do some unit conversions on the observed data (math stuff).
Apply an interstellar ...
0
votes
1answer
41 views
Kindly review this email sending class for GAE
class KoolDailyReminderAfterWeek(webapp2.RequestHandler):
def get(self):
logging.info('sending reminders')
timeline = datetime.now () - timedelta (days = 7)
edge = ...
1
vote
2answers
75 views
Review Python Script for creating a hierarchy of directories
This script creates an hierarchy of directories. I wanted to know whether this is a good approach or can it be made better? I am mainly concerned with maintainability not efficiency here. Suppose I ...
0
votes
0answers
41 views
Django Celery Task for all objects in model with datetime operations
Hi I created quite important piece of code. But I am not pretty sure is it good enough.
Description:
I have big task for database and decided to use django-celery. I want to go through all objects ...
1
vote
2answers
113 views
Is there a more efficient way for code reuse in Python - Largest prime factor?
I was doing timing with variations of function largest_prime_factor. I was able to write a better one largest_prime_factor2. There were some repetitions in the new function so I wrote another function ...
2
votes
2answers
73 views
Deep iterations with side effects
Suppose I need to process a list of list of list of many objects, where the data usually comes from third party APIs.
For the sake of an example, lets assume we have many users, and for each user we ...
0
votes
2answers
56 views
Code review for naive Bayes classifier
Greetings fellow programmers,
I tried to write a naive Bayes classifier to classify OkCupid profiles, and I was wondering if you could give me feedback on my code. The classifier performs no better ...
1
vote
1answer
76 views
Codereview of small python function
I've written a little function that iterates over two iterables. The first one returns an object which is used to convert an object of the second iterable. I've got a working implementation, but would ...
1
vote
1answer
46 views
Format multi-line python expression
I am porting a linear optimization model for power plants from GAMS to Pyomo. Models in both frameworks are a collection of sets (both elementary or tuple sets), parameters (fixed values, defined over ...
0
votes
3answers
56 views
How to improve the computation speed of my shell script/program?
I have written a shell script to process my huge data files (each one having around 7,000,000 lines ~ a weeks data in a single file). Below is the sample of my data file (i.e., input file) structure:
...
2
votes
1answer
41 views
Checking if CLI arguments are valid files/directories in Python
I frequently write Python scripts that require three command line arguments:
config_file — A CSV configuration file
input_dir — The directory containing the input files to be processed
output_dir — ...
2
votes
1answer
84 views
What would be the optimal way to address these arrays
I think the arrays here are slowing down my code due to incorrect usage.
There is a lot of stuff going on here, it's understandable if nobody responds. Just looking for some pointers. Thanks.
for ...
1
vote
0answers
29 views
How to make python knapsack program faster
I have knapsack problem:
I have data:
List of product prices = MENU
Most popluar product count = MOST_POPULAR_COCKTAIL
Knapsack price = CASH
Knapsack length = CHECK_LEN
I need get all knapsack ...
4
votes
4answers
271 views
better code for generating primes
After considerable effort, I've come up with the following code to generate a list of primes of given length.
I would be very interested to see how an experienced coder would modify my code to make it ...
1
vote
1answer
75 views
Simple code that finds number of factors crashes my computer. Is there a workaround for a large number?
This is my attempt at problem 12 from Project Euler. However, my code freezes my old, linux computer when I run it. I've only got 512MB of RAM and an old Intel Core 2 Duo processor on my machine ...
2
votes
5answers
99 views
Python Clean way to get whether a number is above/below two values and increment accordingly
Consider (simplified)
low_count = 0
high_count = 0
high = 10
low = 5
value = 2
What is a clean way to check a number value versus a min low and a max high, such that if value is below low low_count ...
4
votes
1answer
67 views
Faster code for SVG path parsing? Perhaps improved regex?
I have a module in Python for dealing with SVG paths. One of the problems with this is that the SVG spec is obsessed with saving characters to a pointless extent. As such, this path is valid:
...
0
votes
2answers
87 views
How make this algorithm faster?
I have python code.
import itertools
from decorators import timer
from settings import MENU, CASH
class Cocktail(object):
def __init__(self, group, name, sell, count=0):
self.group = ...
2
votes
3answers
80 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 obvious equivalent:
if ...
0
votes
0answers
48 views
Python mocking: raise on second call, but let first call be real
I'm writing a test to ensure rollback is executed properly.
setup test: 1st call to save will save to db, 2nd call to save will raise
stimulate the application (~ user press "Proceed")
check that ...
1
vote
2answers
101 views
Is there a better, more efficient way to write this loop?
While working with someone else's code I've isolated a time-killer in the (very large) script. Basically this program runs through a list of thousands and thousands of objects to find matches to an ...
2
votes
4answers
168 views
Sort a list in Python
My aim is to sort a list of strings where words have to be sorted alphabetically. Words starting with s should be at the start of the list (they should be sorted as well), followed by the other words.
...