Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.

learn more… | top users | synonyms

0
votes
0answers
12 views

python httplib snippet

I've written this snippet to extract a review request status from review-board in python. It works well but i'm very new to python and i would like to know if i could write this any better. Should i ...
2
votes
3answers
122 views

Is there any way to make my project Euler#14 solution faster?

I'm solving project euler problems and uploading my solutions to github. Some of my solutions are just based on math and are thanks to that very fast, but #14 is way too slow, and I have no idea how ...
0
votes
1answer
59 views

Is there a better way to count seconds in python

def stopWatchSeconds(secondCnt) : """ counts second for secondCnt seconds """ # is there a better way to do this? startTime = time.clock() ellapsed = 0 for x in range(0, ...
2
votes
2answers
42 views

Two dimensional unit testing

Consider a process composed of three algorithms: makeRuns, meld and traverse (each taking the output of the previous one as input): it is interesting to test each separately. Now consider three (or ...
3
votes
1answer
34 views

Optional argument for which None is a valid value

I have the following function: def item_by_index(iterable, index, default=Ellipsis): """Return the item of <iterable> with the given index. Can be used even if <iterable> is not ...
0
votes
0answers
19 views

Building an interface to search a .txt file depending on the user input, then print the results next to it

I've been playing around with this quite a bit and I feel like I'm making a mess of it. I need an interface where if someone enters a word in the text field, it'll print any lines within a .txt file ...
2
votes
1answer
28 views

Generic test serie for various solutions to the same algorithmic problem

I am learning how to use the module unittest for test-driven development. Below is a simple yet practical example of an issue I will have many times: one single problem (e.g. an Abstract Data Type ...
0
votes
1answer
18 views

Python. Leaving a APScheduler in a while True loop

I want to run a background job on my webserver to do some database maintenance. I am looking at using APScheduler to do this. I am planning on running the below code in a separate process to my main ...
1
vote
0answers
36 views

Any side effects from this Python logging code?

def _getCallerLogger(): caller = inspect.currentframe().f_back.f_back name = "{}-{}".format(caller.f_globals['__name__'], caller.f_code.co_name ) logger = logging.getLogger(name) ...
-2
votes
0answers
24 views

creating a question with multi choice answers? Help *python* [closed]

I am trying to create a set of questions maybe 10-20 with multiple choice answers for example I want to create a program that will help people learn division. eg. 10 ÷ 2 = ? 1. 2 2. 9 3. 5 4. 7 ...
3
votes
0answers
78 views
+100

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
2answers
64 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
52 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
45 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
61 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
58 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
2answers
52 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
36 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
39 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
77 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
25 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
50 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
142 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. ...
3
votes
1answer
83 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
42 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
33 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
35 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
21 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
99 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
48 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
46 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
35 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
70 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
114 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
106 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
75 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
66 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
33 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
188 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
44 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
210 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
216 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
114 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
94 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'], ...

1 2 3 4 5 17