Questions tagged [python]

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Use the python tag for all Python related questions. If you believe your question may be even more specific, you can include a version specific tag such as python-3.x.

Filter by
Sorted by
Tagged with
5 votes
1 answer
1k views

Reading a binary file containing periodic samples

I have the following code for reading HTK feature files. The code below is working completely correct (verified it with unit tests and the output of the original HTK toolkit). ...
Peter Smit's user avatar
3 votes
1 answer
192 views

Cleaning multiple build directories

I'm cleaning build directories produced by GNOME build tool, JHBuild. This tool either downloads tarballs or clones git repositories, depending on set-up. After that, it proceeds to compilation (and ...
tshepang's user avatar
  • 742
5 votes
2 answers
11k views

Python generator function that yields combinations of elements in a sequence sorted by subset order

In Python, itertools.combinations yields combinations of elements in a sequence sorted by lexicographical order. In the course of solving certain math problems, I ...
Adeel Zafar Soomro's user avatar
3 votes
1 answer
509 views

Extracting lines from a file, the smelly way

I have a section of code I use to extract an event log out of a large text file. It works well, it's just my use of list(itertools.takewhile(...)) that feels a ...
MikeyB's user avatar
  • 133
6 votes
2 answers
653 views

"File Fix-it" challenge

I'm learning Python and have found this problem from Google Code Jam: How many mkdir commands does it take to construct a given directory tree: Input The first ...
gkr's user avatar
  • 63
4 votes
1 answer
2k views

Should I use atexit to close db connection?

I just realized that I could turn: ...
Kit Sunde's user avatar
  • 680
4 votes
1 answer
2k views

Merging two lists from dictionary. How to do this in a better way?

I have the following code: ...
marwy's user avatar
  • 151
4 votes
1 answer
7k views

Three-table queryset (full outer join?) in Django

I've got three tables I need query to display to a user for a web application I've written. Each table (Url, Note, Quote) has a foreign key relation to the User table. For every User, I need to sort ...
mattdeboard's user avatar
8 votes
2 answers
17k views

Splitting an array of numbers into all possible combinations

This takes an array of numbers then splits it into all possible combinations of the number array of size 4 then in another array puts the leftovers. As I want to take the difference in averages of ...
tshauck's user avatar
  • 347
6 votes
2 answers
1k views

python object pool with metaclasses

...
nkint's user avatar
  • 518
5 votes
1 answer
339 views

Suggestions for a Profiler class in Python

Here's what I've right now: ...
Paulo Freitas's user avatar
37 votes
5 answers
4k views

Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
Mauricio Scheffer's user avatar
5 votes
2 answers
2k views

Security of simple client/server desktop app

I am writing an application aimed at the desktop but using web technologies. It will have an HTML interface and a server written in Python using Flask will do all of the filesystem interaction. It ...
david4dev's user avatar
  • 243
5 votes
1 answer
189 views

Substitution of different occurrences of a string with changing patterns

I need to parse an invalid JSON string in which I find many repetitions of the same key, like the following snippet: ...
Andrea Spadaccini's user avatar
10 votes
1 answer
7k views

Conversion from/to roman numbers

I wrote as an exercise in Test-Driven Development a piece of Python code that contains two functions: roman2dec(roman), that converts a roman number (string) into ...
Andrea Spadaccini's user avatar
12 votes
2 answers
349 views

Traversing a multidimensional structure and applying strip() to all strings

I'm trying to apply string.strip() to all the leafs that are strings in a multidimensional collection, but my Python is a bit rusty (to say the least). The ...
Kit Sunde's user avatar
  • 680
9 votes
2 answers
3k views

Getting lists of values from a CSV

I have a function that takes a column title, and a response.body from a urllib GET (I already know the body contains text/csv), and iterates through the data to build a list of values to be returned. ...
Greg Gauthier's user avatar
11 votes
1 answer
1k views

Review the vimrc for a Python programmer [closed]

I am primarily a Python programmer and have finally ditched the IDE in favour of vim and I will admit, I am loving it ! My vimrc file looks like this: ...
user avatar
10 votes
4 answers
730 views

Print sum of numbers 1 to 200 except mults of 4 or 7 in Python

I had a question about using the modulus operator in Python and whether I have used it in a understandable way. This is how I've written the script: ...
KyleWpppd's user avatar
  • 203
14 votes
8 answers
29k views

Two FizzBuzz solutions

Below are two solutions to the FizzBuzz problem in Python. Which one of these is more "Pythonic" and why is it more "Pythonic" than the other? Solution One: ...
Thomas Owens's user avatar
10 votes
1 answer
1k views

Python class w/ Telnet interface to memcached

I haven't had anyone help me out with code review, etc, so I thought I'd post a Python class I put together for interfacing with Telnet to get information from a memcached server. ...
dlrust's user avatar
  • 203
1 vote
4 answers
21k views

Is there a better way than lambda to filter on attributes/methods of objects?

Is there a better (more pythonic?) way to filter a list on attributes or methods of objects than relying on lamda functions? ...
Xavier Nodet's user avatar
14 votes
2 answers
311 views

Executing a program with a temporary directory and measuring the running time

I need a code review for best practices and code correctness of the following piece of code. run executes a program and validates in output. If the output is valid ...
Alexandru's user avatar
  • 409
5 votes
2 answers
502 views

Converting an already-uploaded file and saving it to a model's FileField

Please review this: ...
Chantz's user avatar
  • 213
6 votes
1 answer
41k views

Django query_set filtering in the template

Can I make my template syntax simpler? I'm hoping to eliminate the if and maybe also the for block. This worked in the shell ...
citadelgrad's user avatar
11 votes
2 answers
2k views

An idiom to use the same view function to create or edit an object?

Here is the skeleton of my (first!) Django app: ...
Xavier Nodet's user avatar
0 votes
2 answers
288 views

Generating filesystem paths from a fixed string

It's clever, but makes me vomit a little: file = '0123456789abcdef123' path = os.sep.join([ file[ x:(x+2) ] for x in range(0,5,2) ])
user972's user avatar
  • 27
14 votes
1 answer
785 views

Quasi-random sequences: how to improve style and tests?

The requirements for this one were (original SO question): Generate a random-ish sequence of items. Sequence should have each item N times. Sequence shouldn't have serial runs longer than a given ...
TryPyPy's user avatar
  • 852
8 votes
1 answer
3k views

Randomly sampling a population and keeping means: tidy up, generalize, document?

This is part from an answer to a Stack Overflow question. The OP needed a way to perform calculations on samples from a population, but was hitting memory errors due to keeping samples in memory. ...
TryPyPy's user avatar
  • 852
25 votes
3 answers
885 views

Music info model

I'm learning Django as I go. I know this model is missing user authentication, registration, comments/comment threading, and voting. But this is my starting code for my model. What are some of the ...
dassouki's user avatar
  • 1,101
15 votes
1 answer
2k views

Function for finding longest common path and formatting it along with filenames

This is a function I wrote yesterday and have tested with lots of input. It's been committed and is in use, but no review was done, so it seems like a good candidate for Code Review: it could be more ...
TryPyPy's user avatar
  • 852
61 votes
10 answers
4k views

Using separate functions for Project Euler 1

I started programming with Java and C++, so I'm used to having a 'main' function that calls other functions that do the actual work. At university I was always told that doing actual computation in ...
Basil's user avatar
  • 881
58 votes
9 answers
14k views

Project Euler problem 1 in Python - Multiples of 3 and 5

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to ...
Robert S Ciaccio's user avatar

1
303 304 305 306
307