Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.
1
vote
3answers
150 views
More efficient way to retrieve first occurrence of every unique value from a csv column?
A large .csv file I was given has a large table of flight data. A function I wrote to help parse it iterates over the column of Flight ID's, and then returns a dictionary containing the index and ...
1
vote
0answers
34 views
Review streamed encrypt then mac construction
I've been porting python Keyczar to work under 2to3 conversion (Github). I wanted to consolidate it's streaming aes encrypt/decrypt backend interface with it's string decrypt/encrypt. So I wrote a new ...
1
vote
0answers
31 views
Global appIncluder function to include sub-applications dynamically
I have implemented a global appIncluder function which is imported as includeme with the __init__.py of the package to be included.
includeme (ApplicationIncluder) receives the config object, so it ...
0
votes
1answer
61 views
Progressbar in Python
i wish to improve my Progressbar in Python
from __future__ import division
import sys
import time
class Progress(object):
def __init__(self, maxval):
self._seen = 0.0
self._pct = ...
1
vote
2answers
115 views
How can I make my XML parser more pythonic?
I have a function that takes an XML document and parses out specific XML elements and appends those to a list for use by an Oracle executemany call. The placement of the elements matters as the ...
3
votes
2answers
185 views
Given an array find any three numbers which sum to zero
Looking for optimizations and cleaner, more pythonic ways of implementing the following code.
#Given an array find any three numbers which sum to zero.
import unittest
def sum_to_zero(a):
...
0
votes
3answers
105 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
2answers
86 views
improve the design of class “accuracy” in Python
I am learning about the class and methods in Python.
The class Accuracy is a class of several (13 in total) statistic values between a reference polygon and one or more segmented polygons based on ...
1
vote
2answers
101 views
Database calls with Python Flask and SQLAlchemy
I am creating a Python Flask webservice and this is basically how I am doing all of my database calls if a webservice needs to interact with the database:
@event.route("/somepath/<value>", ...
4
votes
4answers
171 views
how can I improve this code and how do I add memory to it?
So I created this code were its basically a game and I need help, because I know there are many ways to improve easier than what I am doing. I am doing it the basic way I know I can but I know there ...
-1
votes
2answers
68 views
Count the total pairs which have a constant difference (python) [closed]
Given N numbers [N<=10^5], count the total pairs of numbers that have a difference of K.
This is my code.Some thing's wrong...
Any help??Is there any pythonic way to solve this?
def ...
7
votes
3answers
229 views
Noughts and Crosses - Python
So I wrote a two player Noughts and Crosses game in Python 3, and came here to ask the standard question. How can I improve this code:
import random
cell = ['1', '2', '3', '4', '5', '6', '7', '8', ...
3
votes
4answers
130 views
download an image from a webpage
I originally posted this on stackoverflow and was recommended that I post here, so I apologize if you see this twice:)
I am trying to write a python script that download an image from a webpage.on ...
0
votes
1answer
224 views
Implementation of Battleship in Python TKinter
I am very new to GUI programming, and I'm pretty sure I'm doing it wrong. Please take a look at my code, and suggest some changes. I feel it's way too complicated for what it's actually doing.
For ...
2
votes
0answers
111 views
Python reversed in C++11
I was playing with C++11 wonderful new features and tried to reimplement a function reversed that would behave just like the one in Python to allow reverse iteration on bidirectional iterables.
And to ...
3
votes
2answers
127 views
Generating random sequences while keeping sum fixed
The output is a random sequence, and the input is the sum of the sequence.
My solution is generating a random number *rand_num* from (0, sum_seq) at first, then draw another number randomly from (0, ...
7
votes
3answers
262 views
Guessing Game: computer generated number between 1 and 100 gussed by user. Better code?
The required output is stated just before the main() function. Is there a better way to achieve it? I feel like i am repeating code unnecessarily but, am unable to find a better way to get the result. ...
1
vote
1answer
100 views
Hangman in Python
I created this version of Hangman in Python 3, anyone got any tips for optimization or improvement?
import random
HANGMAN = (
'''
-------+''',
'''
+
|
|
|
|
...
3
votes
1answer
168 views
Improve efficiency for finding word frequency in text
Premise: Write a program that counts the frequencies of each word in a text, and
output each word with its count and line numbers where it appears. We
define a word as a contiguous sequence of ...
2
votes
1answer
37 views
How can I speed up this correlation function Python
I am writing a specialized version of the cross correlation function as used in neuroscience. The function below is supposed to take a time series data and ask how many of its values fall in specified ...
4
votes
1answer
52 views
this code returns environment varibles or passed enverionment variable with values
#!/usr/bin/env python
import os, sys
variable = sys.argv
def enVar(variable):
"""
This function returns all the environment variable set on the machine or in active project.
...
4
votes
3answers
187 views
How to increase the performance of loop in Python?
I need to run over a log file with plenty of entries (25+GB) in Python to extract some log times.
The snippet below works.
On my test file, I'm ending up with roughly 9:30min (Python) and 4:30min ...
2
votes
1answer
57 views
Pythonic type coercion from input
My context is a very simple filter-like program that streams lines from an input file into an output file, filtering out lines the user (well, me) doesn't want. The filter is rather easy, simply ...
2
votes
1answer
109 views
Is this a good encryption algorithm?
I've never had a serious attempt at writing an encryption algorithm before, and haven't read much on the subject. I have tried to write an encryption algorithm (of sorts), I would just like to see if ...
3
votes
1answer
113 views
Are these list-comprehensions written the fastest possible way?
This is a simple repeatable question regarding the usage of Python3's comprehensions:
Could the Python3 syntax be used in another way to speed up the process, further so the gap between Python3 and ...
7
votes
1answer
216 views
Project Euler #3 - how inefficient is my code?
So I am essentially a complete newbie to programming, and have been attempting to learn Python by completing the Project Euler problems. I haven't gotten very far, and this is my code for problem #3 :
...
1
vote
2answers
50 views
URLs extraction from specific block
This script extracts all urls from a specific HTML div block (with BeautifulSoup 4) :
raw_data = dl("http://somewhere")
links = []
soup = BeautifulSoup(raw_data)
data = str(soup.find_all('div', ...
2
votes
1answer
46 views
Shorten Python/Maya Window UI code
I'd like to know any ways of shortening this UI window for Maya, at the moment, because I couldn't find very detailed documentation and not very skilled with python, I have just built this UI with the ...
2
votes
3answers
110 views
Project Euler - Shortening Problem 2
You might have read my question on shortening my code to a one-liner for Problem 1. So, I was wondering, is there any more tricks of the trade to shorten my Problem 2 solution:
fib = [0, 1]
final = 1
...
3
votes
1answer
99 views
Project Euler Problem 1
This is my python solution to the first problem on Project Euler:
n = 1
rn = 0
while n < 1000:
if n%3 == 0 or n%5 == 0:
rn += n
n = n + 1
print(rn)
I would like to find a way to ...
1
vote
2answers
82 views
Making a hangman game, one of my functions is correct but a mess
I got a ton of helpful tips last time I posted some code so I thought I would come back to the well.
Anywho, my function is deciding whether or not the secretWord has been guessed in my hangman game. ...
2
votes
1answer
193 views
Get metadata from an Icecast radio stream
I am new to Python and not very familiar with advanced Python data structures.
I have written a function to receive data from a socket in Python and perform string manipulations on it. The basic ...
1
vote
1answer
34 views
Any way to optimize or improve this python combinations/subsets functions?
I believe this is essentially the same method as the itertools.combinations function, but is there any way to make this code more more perfect in terms of speed, code size and readability :
def ...
0
votes
1answer
33 views
Generalize subsets function for any order subset, concisest way possible? [closed]
Implementing a toy Apriori algorithm for a small-data association rule mine, I have a need for a function to return all subsets.
The length of the subsets is given by parameter i. I need to ...
4
votes
2answers
64 views
Recursive XML2JSON parser
I made a Python function that takes an XML file as a parameter and returns a JSON.
My goal is to convert some XML get from an old API to convert into Restful API.
This function works, but it is ...
2
votes
1answer
55 views
Efficiency of Recursive Checkers Legal Move Generator
I'm implementing a checkers engine for a scientific experiment. I found out through profiling that this is one of the functions that takes up a lot of time. I'm not looking for an in-depth analysis, ...
0
votes
0answers
33 views
Merge all text files in a directory and save a temp file. Suggestion to improve my code
i coded this function where you read all text file in a directory and you save in a temporary file all values. The text files are x,y, and z format. The function returns the name of the temporary ...
3
votes
1answer
57 views
Trying to get output of ffprobe into variable
I am trying to grab the ffprobe values from the video file into a variable that I can compare against others or move the value into a database. The question I have; Is there a better way of doing it ...
2
votes
1answer
62 views
Performance problems with 1D cellular automata experiment in Python with Numpy
I'm an experienced programmer but relatively new to Python (a month or so) so it's quite likely that I've made some gauche errors. I very much appreciate your time spent taking a look at my code.
My ...
4
votes
1answer
90 views
Bitwise Flag code for Python
I am looking for feedback on this compact Python API for defining bitwise flags and setting/manipulating them.
Under the hood, FlagType extends int and all operations are true bitwise, so there is ...
2
votes
2answers
103 views
How to make this python function and its inverse more beautiful and symmetric?
I like the radix function and found a really neat way to compute it's inverse in python with a lambda and reduce (so I really feel like I'm learning my cool functional programming stuff) :
def ...
2
votes
1answer
38 views
Python: Combing two programs that analyze strings
I am trying to combine two programs that analyze strings into one program. The result should look at a parent string and determine if a sub-string has an exact match or a "close" match within the ...
2
votes
1answer
59 views
python unique_list class
Please review unique_list:
unique_list implements a list where all items are unique.
Functionality can also be described as set with order.
unique_list should behave as a python list except:
Adding ...
4
votes
2answers
122 views
Strategy Design Pattern in Python
I'm reading the awesome Head First Design Patterns book. As an exercise I converted first example (or rather a subset of it) to Python. The code I got is rather too simple, i.e. there is no abstract ...
1
vote
1answer
44 views
Pythonic indenting `with` statement blocks
Python doesn't remove the variables once a scope is closed (if there's such a thing as scope in Python, I'm not sure). I mean, once I finish a for variable in range(1, 100), the variable is still ...
1
vote
2answers
143 views
Can this function be better written? It returns a list of divisors for a number
This function takes in a number and returns all divisors for that number. list_to_number() is a function used to retrieve a list of prime numbers up to a limit, but I am not concerned over the code ...
1
vote
2answers
54 views
Python Line continuation dictionary
I have a function that needs to return the call to another function that have some parameters. Let's say, for example, the following:
def some_func():
iss = do_something()
example = ...
0
votes
4answers
116 views
How can this python code be improved?
I'm trying to become more proficient in Python and have decided to run through the Project Euler problems.
In any case, the problem that I'm on (17) wants me to count all the letters in the English ...
1
vote
2answers
75 views
Starting to learn python, trying to optimize bisection search game
I was wondering how i could opti
high = 100
low = 0
guess = (high + low)/2
print('Please think of a number between 0 and 100!')
guessing = True
while guessing:
print('Is your secret ...
2
votes
1answer
44 views
Python - Strip whitespace from strings in list that contains elements of various types
Let's say I have a list that contains strings and integers. The strings can contain leading and trailing whitespace that I'd like to remove. I can't just strip() all elements because some are ...




