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
3 votes
1 answer
2k views

Review Request: Python code that searches query words in a given text

I have this code which looks for query words in a given text. I preprocess the give text and store it in an inverted-index (dictionary with word as key and position in text as value). Searching for a ...
user avatar
6 votes
2 answers
8k views

Converting JSONP to JSON: Is this Regex correct?

I'm fetching a string that is JSONP, and looking to convert it to JSON. I'm using the following regular expression for matching (for removal) the padding on the JSON. ...
user avatar
  • 163
11 votes
4 answers
2k views

Generating the sequence of the digits that appear in the sequence of natural numbers

This is a revisit to a question already asked here about a year and a half ago. Project Euler's Problem 40 involves generating the sequence of digits that appear in the natural numbers, all ...
user avatar
  • 213
7 votes
1 answer
13k views

Parsing huge xml file with lxml.etree.iterparse in python

After solving the error in SO (like suggested) I return now for codereview. :-) The task is to parse a huge file dblp.xml (~800 MB) presented by DBLP. The records ...
user avatar
  • 267
6 votes
1 answer
3k views

Iterator over row and columns

I am writing a simple raytracer just for fun. I want to iterate over rows, and each row must be an iterator over columns. A strategy I produced is the following: ...
user avatar
5 votes
1 answer
232 views

all the ways to fit some pegs into given holes

Suppose you have a list of the diameters of some circular holes, and a list of the diameters of some cylindrical pegs. Now the problem is finding all the ways you can fit the pegs in the holes, where ...
user avatar
4 votes
1 answer
2k views

File-serving Twisted resource

I've just written this Twisted resource base class for serving files. It's part of a larger application, this is just for serving its images, JavaScript files, etc. Is it okay? I wonder if checking ...
user avatar
10 votes
1 answer
4k views

Python implementation of a wrapped Conway's Game Of Life board

...
user avatar
  • 203
9 votes
1 answer
5k views

Binary tree in Python

As a Python newbie, I have coded this binary tree as best as I could to follow Python idioms. However, I know it is still very verbose and not up to standard. I would like Python experts to tear this ...
user avatar
7 votes
1 answer
8k views

Becoming root from Python

In order to avoid the user having to explicitly prefix a script with sudo or su --command, I wrote the following: ...
user avatar
  • 712
7 votes
1 answer
900 views

Merge sort and selection sort comparison taking too long

I have this python code that compares merge sort and selection sort, but it is taking "forever." ...
user avatar
11 votes
4 answers
3k views

Change upper or lowercase vowels to [0,1,2,3,4] respectively and leave the rest the same

The objective was the following to be solved in Python: Given a string, def a function that returns another string with the vowels (upper or lowercase) replaced by 0,1,2,3,4 respectively. ...
user avatar
  • 221
11 votes
7 answers
4k views

Rock Papers Scissors in Python

I am a newbie to Python and have started object-oriented programming recently. I have implemented a "Rock Paper Scissors" application in OOP. I would like for you to evaluate my code, and tell me ...
user avatar
  • 450
8 votes
1 answer
2k views

Web-uploading of Cyrillic-named images

For web-uploading of Cyrillic-named images, I've written the script below: ...
user avatar
5 votes
3 answers
605 views

Inch <-> cm conversion program

I just wrote this small inch <-> cm conversion program. It works just fine, although I know that I've done some things in a rather stupid manner and that it could be improved. ...
user avatar
8 votes
2 answers
6k views

Linux backup script in Python

I've written a backup script to make a backup of the latest modified files in 12 hours. Basically, it searches two directories for modified files (statically coded) then the ...
user avatar
3 votes
1 answer
669 views

Intersection-detection algorithm

This is an intersection-detection algorithm I developed as an alternative method to the one developed for my coursework. I won't post some of the other functions as they are used in the coursework too,...
user avatar
4 votes
2 answers
627 views

Review request: My App Engine library (python)

UPDATE: I didn't write the library pasted below I just make use of it. I'm unable to paste the part I wrote here because the message exceeds 30k characters when I try to do so. I have been working ...
user avatar
6 votes
2 answers
233 views

Is it okay to 'abuse' re-assignment? [closed]

Because I don't like using \ to break long lines (to comply with PEP8), I tend to do something like this: ...
user avatar
  • 712
5 votes
3 answers
1k views

Reducing memory usage when comparing two iterables

I'm changing code that writes data to a DB, so I have a dump (a text file) of an earlier run to compare against, to ensure that my changes don't screw things up. Here goes: ...
user avatar
  • 712
5 votes
1 answer
6k views

TreeMap implementation

I've just tried to do some code for TreeMap. The TreeMap concept can be found here. As an exercise I've tried to find the ...
user avatar
  • 508
7 votes
1 answer
2k views

Generating a random maze using disjoint sets

I've had a go at writing something that generates a random maze using disjoint sets using the grouping class I found here. The way I'm generating the maze is by generating all the cells, with each ...
user avatar
  • 285
4 votes
1 answer
2k views

Lua comment remover

I had a job to remove all kinds of comments from the Lua file. I tried to find a usable Python script to this on the net, but Google did not help. Therefore, I made one. This script recognizes all ...
user avatar
2 votes
1 answer
141 views

Serialising missing values in trie

There are two aspects to this question that are inter-related. Change I've made to the trie module in python (PyPI, GitHub) to make it serialisable (and ...
user avatar
20 votes
4 answers
19k views

Finding all k-subset partitions

The following code generates all \$k\$-subsets of a given array. A \$k\$-subset of set \$X\$ is a partition of all the elements in \$X\$ into \$k\$ non-empty subsets. Thus, for ...
user avatar
5 votes
1 answer
709 views

Using tweet as command to shut down Ubuntu

I want to use Tweets sent out by a twitter account as a command to shut down Ubuntu. For this, I've written a Python script using tweepy, a Python library for Twitter. I scheduled the script to run ...
user avatar
  • 231
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). ...
user avatar
3 votes
1 answer
188 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 ...
user avatar
  • 712
5 votes
2 answers
10k 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 ...
user avatar
3 votes
1 answer
507 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 ...
user avatar
  • 133
6 votes
2 answers
647 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 ...
user avatar
  • 63
4 votes
1 answer
2k views

Should I use atexit to close db connection?

I just realized that I could turn: ...
user avatar
  • 690
4 votes
1 answer
2k views

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

I have the following code: ...
user avatar
  • 51
4 votes
1 answer
6k 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 ...
user avatar
7 votes
2 answers
16k 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 ...
user avatar
  • 337
6 votes
2 answers
1k views

python object pool with metaclasses

...
user avatar
  • 508
5 votes
1 answer
336 views

Suggestions for a Profiler class in Python

Here's what I've right now: ...
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: ...
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 ...
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: ...
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 ...
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 ...
user avatar
  • 690
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. ...
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
585 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: ...
user avatar
  • 203
14 votes
8 answers
28k 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: ...
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. ...
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? ...
user avatar
14 votes
2 answers
310 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 ...
user avatar
  • 409
5 votes
2 answers
482 views

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

Please review this: ...
user avatar
  • 213