Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.
0
votes
0answers
7 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>", ...
3
votes
2answers
109 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
44 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 ...
1
vote
3answers
61 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
107 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
71 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 ...
1
vote
0answers
77 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
89 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, ...
6
votes
3answers
153 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
84 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
84 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 ...
1
vote
1answer
28 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
45 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
130 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
54 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
88 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
90 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 ...
6
votes
1answer
170 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
1answer
29 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', ...
1
vote
0answers
34 views
Shorten 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
86 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
85 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
74 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
50 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
32 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
31 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 ...
3
votes
0answers
59 views
Looking for revisions of previously written code [closed]
I've noticed that lately my assignment grades have been getting lower as the class has advanced in Python, and I was wondering if anyone could help me see what I did wrong with these code snippets and ...
4
votes
1answer
47 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
33 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
16 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
35 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
40 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 ...
3
votes
1answer
47 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
79 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
33 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
51 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
101 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
41 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
100 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
33 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
105 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
38 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
37 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 ...
0
votes
1answer
24 views
Python right position for a Error message in a class
first of all sorry for this easy question. I have a class
class Grid(object):
__slots__= ("xMin","yMax","nx","ny","xDist","yDist","ncell","IDtiles")
def ...
1
vote
1answer
29 views
Function to generate nearly sorted numbers
I wrote function to generate nearly sorted numbers:
def nearly_sorted_numbers(n):
if n >= 100:
x = (int)(n/10)
elif n >= 10:
x = 9
else:
x = 4
numbers = ...
2
votes
1answer
42 views
python Improve a function in a elegant way
I have a grid as
>>> data = np.zeros((3, 5))
>>> data
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]]
i wrote a function in ...
2
votes
1answer
52 views
Can this small snippet of code be better written? It organizes a long string into a list of lists
This code loads the data for Project Euler problem 18, but I feel that there must be a better way of writing it. Maybe with a double list comprehension, but I couldn't figure out how I might do that. ...
4
votes
1answer
111 views
Unique short string for URL
I needed short, unique, seemingly random URLs for a project I'm working on, so I put together the following after some research. I believe it's working as expected, but I want to know if there's a ...
1
vote
1answer
48 views
What parts of my Python code are adopting a poor style and why?
Here I have some simple code that worries me, look at the second to last line of code, I have to return spam in order to change spam in the global space. Also is it bad to be writing code in this ...
3
votes
4answers
112 views
Is there any downside to define a function that use one dictionary as an argument instead of several (+10) arguments?
I tried to refactoring someone else's code and I found that there is alot of this pattern
def train(a, b, c, d, e, f, g, h, i, j, k): #Real code has longer name and around 10-20 arguments
x = ...