Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.
1
vote
0answers
15 views
Python: How do I optimize the solving of ODEs?
I have been trying to figure out a way to optimize the solving of ODEs in Python but haven't been able to achieve this goal. I tried getting help via a bounty on SO using cython but nothing came of ...
2
votes
1answer
50 views
cleaner way to import “izip” for different versions of python
A common idiom that I use for python2-python3 compatibility is:
try:
from itertools import izip
except ImportError: #python3.x
izip = zip
however, a comment on one of my stackoverflow ...
-1
votes
0answers
41 views
Euclid's Algorithm in Python [closed]
I am learning about primes and modulus arithmetic in math class so I decided to try to implement Euclid's algorithm in Python. I can't figure out why this doesn't work, but the Python interpreter does ...
-1
votes
0answers
38 views
I am trying to find the 1000th prime, something off O.o [closed]
Here is my code:
y = 1000
def prime_num(x): return x % 2 != 0
if len(filter(prime_num, range(1, y))) != 1000:
y += 1
else:
print (filter(prime_num, range(1, y)))
1
vote
1answer
57 views
I need my code to be more consise and I don't know what is wrong with it :/ its pretty basic and I need help [closed]
#Goal: Write a program that asks the user to input 10 integers, and then
#prints the largest odd number that was entered. If no odd number was
#entered, it should print a message to that effect.
...
1
vote
0answers
43 views
Python script to parse email and create HTML markup from attachments
Disclaimer: I want to apologize right off the bat since this will most likely be a very long-winded post. For those that do not care about/need the details, links to my code and supporting files are ...
2
votes
1answer
29 views
Global variables in python, bottle microframework
I'm sort of at a loss for what to do with webapps and whether what I'm doing is actually allowed or not. Here is a sample of the type of thing i'm doing at the moment. I need a list that is accessible ...
-1
votes
0answers
29 views
Can't find the memory leak [closed]
This is a simple python script (converted to windows service) that monitors a process to see if its pegging the CPU. If its pegged for 20 iterations it kills the process. The problem is that after it ...
-1
votes
0answers
34 views
Why doesn't this code work? [closed]
"""
we want to:
greet the user, tell them what we're doing**
ask the user to choose an book**
pass the book's url to a function that:
gathers the name, author, start line and ...
2
votes
1answer
66 views
I'm a beginner who could use some help rewriting messy code. Would anyone care to lend assistance?
I know that this is a complete mess and does not even come close to fitting PEP8. That's why I'm posting it here. I need help making it better. I'm trying to become a better programmer. If this is ...
0
votes
0answers
21 views
Python ADO + ODBC function [migrated]
I am writing a small module to help a transfer from M$-Access to SQLite (database needs to be portable), but I'm struggling in interpreting the error message that follows from this code (and of course ...
2
votes
1answer
48 views
Performance and design question
I'm working on a web application using bottle, and it is, at this point, functional.
The gist of it is that i have a database of drinks, with associated IDs, and associated sets of ingredients. the ...
4
votes
1answer
104 views
Optimizing python code for big data sets
I'm trying to optimize a python string that works on big data sets, the way it works is by taking in a with a list of keywords and scores and taking in a file loaded with data from the twitter api. ...
2
votes
1answer
58 views
Computation of Prefix Free Codes with many repeated weight values in reduced space
Does the code below respect Python programming conventions? (and if not, which one does it disrespect?) The objective would be to publish this code as part of a scientific article, as "reproducible ...
2
votes
1answer
41 views
Class for simple python object to be hashed and referenced
I need objects in python that I can compare, reference to and hash.
In principal a tuple or list would be good enough just that
a list I cannot hash and the tuple I cannot change,
except replacing it ...
-1
votes
0answers
32 views
Writing encryption/decryption programs in python 2.7? [closed]
I just started learning Python five weeks ago, so please pardon my complete n00b-ness in regards to my coding. I need to be able to make two functions: one to encrypt a given plaintext file using a ...
-4
votes
0answers
33 views
Help writing a python program which asks the users to input data into a file.! [closed]
Help :x I've never had to use python before and now all of the sudden I need to write this out.
0
votes
0answers
19 views
How can I verify if an input is either a json string or a file containing json in python? [closed]
I've got a small module that reads JSON strings and does something with the data they contain. It's designed to accept both JSON strings and files that contain JSON. Consider this code:
import os
...
3
votes
1answer
75 views
Project Euler 407: Is there any more optimal way to solve this idempotent equation (modulo n ring)?
Project Euler problem 407:
If we calculate a2 mod 6 for 0 ≤ a ≤ 5 we get: 0, 1, 4, 3, 4, 1.
The largest value of a such that a2 mod 6 = a is 4.
Let's call M(n) the largest value of a < n ...
0
votes
0answers
7 views
Overriding __setattr__ at runtime [migrated]
I am trying to override the __setattr__ method of a Python class, since I want to call another function each time a object instance changes its value. However, I don't want this behaviour in the ...
0
votes
1answer
45 views
Is 'try / finally' the best way for me to handle (and close) this long-running IMAP4 connection?
I'm writing a script to archive email messages from one IMAP folder to another. It looks, more or less, like the following. I've obfuscated about 30 lines unrelated to this question:
import imaplib
...
-1
votes
0answers
24 views
Python need help making score keeper [closed]
I am fairly new to coding and the only language I've used is Python. I am making a program for school that is very simple. It it closely related to the classic game "snake". I've been stuck for hours ...
1
vote
0answers
47 views
Changing states of a game character and constructor signature
I have an Actor class who represents the player and the NPCs of an game.
The Actor have an StateMachine object who manages the push and the pop of the states of the Actor object.
The Actor have only ...
1
vote
2answers
44 views
Changing the state of another entity into the constructor method
I have a Clock class who inherites from pyglet.clock.Clock and behaves like a Singleton.
Into the constructor method I have the following line:
pyglet.clock.set_default(self)
Who changes the state ...
2
votes
1answer
31 views
recursive algorithm to obtain grid points inside a n-cube volume or surface
I did wrote in python two functions to return points in the n-dimensional cube of half size K (meaning that the coordinates along each axis go from -K,.. 0.. K). The n-dimensional cube volume should ...
2
votes
1answer
60 views
Made a simple gui string encrypter, just to obfuscate plaintext
I realize this won't stop people who know what they're doing, just a project I decided to undertake while I'm trying to learn Python and Tkinter. Would love some input!
import Tkinter
import base64
...
2
votes
3answers
101 views
Game of Life in Python
I have this interpretation of John Conway's Game of Life in Python 3.3.1:
#JOHN CONWAY'S GAME OF LIFE
def countSurrounding(universe, a, b):
count = 0
surrounding = [[a - 1, b - 1],
...
2
votes
2answers
29 views
Refactoring an interruptable thread
I have a long-running task that processes an input file and then uploads it to a web server. The processing part can be interrupted (with cleanup) but the upload shouldn't be interruptable so that we ...
2
votes
3answers
88 views
Speed up simple Python function that uses list comprehension
I'm extracting 4 columns from an imported CSV file (~500MB) to be used for fitting a scikit-learn regression model.
It seems that this function used to do the extraction is extremely slow. I just ...
3
votes
2answers
68 views
Python: Calculator Improvements
I have some code for a calculator, and I was wondering what I could look at to make the code smaller, easier to read or better in any way :)
Im particulaly looking at my global variable x... Is there ...
0
votes
0answers
6 views
Parsing multiple same formatted xml items [migrated]
I have multiple items within my xml code I want to parse. I'm not quite sure of how to do it, any help would be greatly appreciated. Below is a snippet of my xml and python code and what I'm looking ...
1
vote
1answer
46 views
Create palindrome by rearranging letters of a word
Inspired by a recent question that caught my interest, I wrote a function in Python 3.3 to rearrange the letters of a given string to create a (any!) palindrome:
Count the occurrences of each letter ...
0
votes
1answer
31 views
How to handle returned value if an exception happens in a library code
There is a lib code, trying to parse an Element Tree object. If exception happens, it either returns an empty dict of dict or a partially constructed object of such type. In this case, caller needs to ...
4
votes
2answers
179 views
How can I memoize or otherwise optimize this code?
The code checks many more conditions like the one below. I was thinking to memoize it, but I can't think about how (writers block). How else could I optimize this? I know it seems silly, but my code ...
2
votes
1answer
38 views
Parsing XML with double nested tags using mindom
I want to retrieve id and name per skill. It works but is it well done? I would like to stay with minidom but all advices will be appreciated.
# This is only part of XML that interesting me:
# ...
6
votes
2answers
183 views
python: design of a simple game
I am trying to code Battleship. It should be a text one-player game against computer where computer and human player take turns in shooting at opponent's ships. I decided to start implementation with ...
3
votes
3answers
167 views
Writing beautiful and efficient code in python - how can I improve this code?
I have often read about how important clear and efficient code is. Also often people talk and write about 'beautiful' code. Some hints and critic from experienced developers for the following code ...
4
votes
2answers
105 views
Python line condenser function
This is a function I just wrote that tries to condense a set of strings into grouped lines. It actually works, but looks ugly. Is there a better way to achieve the same thing?
Take 4 filtering empty ...
2
votes
3answers
81 views
Set a default if key not in a dictionary, *or* value is None
Let's say I have a key 'messages' that is usually a list, but might be None, or might not be present. So these are all valid inputs:
{
'date': 'tuesday',
'messages': None,
}
{
'date': ...
1
vote
1answer
67 views
Is there a better way to make a function silent on need?
I got a piece of code I'm not pleased with; Does anyone would have a better idea?
def myFunc(verbose=True):
if not verbose:
print = functools.partial(globals()['print'], ...
6
votes
2answers
283 views
Speed up solution to Project Euler problem 75
I've been programming for a few months now, and have used Stack Overflow a great deal, but this is my first post. Anyway, I wrote this code for Project Euler problem 75, and was curious if anyone knew ...
1
vote
1answer
38 views
Improve file-path finding method
I am using a recursive algorithm to find all of the file-paths in a given directory: it returns a dictionary like this: {'Tkinter.py': 'C:\Python27\Lib\lib-tk\Tkinter.py', ...}.
I am using this in a ...
1
vote
1answer
61 views
Python - get tuple of the first and last days of the last N months
I am trying to get a tuple of date (or datetime) objects over the last N months.
My thought was to use the dateutil package with something like this:
def last_n_months(n=12, ending=None):
...
2
votes
1answer
93 views
Is there a better way to code this text adventure game? (python)
This is my first attempt with a basic text adventure game, and i was wondering if there was a way to make it more efficient, or any other things i could add to make the game better.. It isn't totally ...
0
votes
2answers
77 views
Python: Modularized Programming to find Circumference, Diameter, and Area from circles Radius
I have to create a modularized program that can find out the Diameter, Circumference, and Area of a circles radius. I'm sure many of you can notice I kind of winged this from a example given from my ...
2
votes
0answers
23 views
Accelerate OpenGL 2d on python3
I used OpenGL to draw about 20 circles. Each circle has 2 lines, ~10 segments, and all of them have different colors and lenght. FPS ~=4. How can I do this faster?
I am using Python 3 on Ubuntu
Sorry ...
0
votes
1answer
53 views
Twitter Filter Code Review
I submitted a previous version of this program but I've completely rewritten it in an Object Oriented style. This is only my second attempt at OO programming so I'm interested in hearing how I can ...
2
votes
1answer
97 views
What steps to turn python code into python zen code
My python code is ugly and I'm having difficulty improving it. What kind of steps could be done here to make it. This is an example function that comes from time to time and I can't seem to have a ...
2
votes
2answers
45 views
Wrapping an Exception with context-management, using the with statement
Before I try to make this work, I wondered if anyone had tried this, whether it was a good idea or not, etc.
I find myself doing this a lot:
if some_result is None:
try:
raise ...
5
votes
2answers
152 views
Help refactoring my tic tac toe game
I'm very new to coding and have been diving into the skill with Python as my first language. One of the first programs I wrote for a class was this Tic Tac Toe game. I re-wrote this game using classes ...



