Questions tagged [python-2.x]

Python 2 is the predecessor of Python 3 and its last version, Python 2.7 was formally released on July 3, 2010. Use this tag along with the main python tag to denote programs that are meant to be run on a Python 2 interpreter only. Do not mix this tag with the python-3.x tag.

Filter by
Sorted by
Tagged with
6
votes
3answers
6k views

Create SQLite backups

I have this script for creating SQLite backups, and I was wondering whether you'd have any suggestions on how to improve this. I was thinking that maybe it should create the backup ...
4
votes
1answer
638 views

Universal memoization decorator

I've just written a simple caching / memoization python decorator. It's purpose is to cache what the function returns for all the arguments combinations it's been ever invoked with. So, if, say, we ...
10
votes
1answer
294 views

Visualize Parts of Song as Analyzed by Echonest

This is the a bit of code that works with Echonest API's pyechonest and remix libraries combined with matplotlib.pyplot to offer a simple visual representation of the start and end "parts" of a music ...
3
votes
1answer
831 views

Iterate over coordinates and correct constraint violations

I am creating an algorithm to take a series of labelled points placed randomly and move them until they fulfil a set of constraints. The constraints have been pre-computed and are each a lower and ...
3
votes
1answer
68 views

Parse data into three logs

In the bit of code below: How can I make add_day, add_ week and add_month work as a single ...
5
votes
4answers
3k views

Squaring an integer by repetitive addition

I've written two functions: is_integer(), which tries to get a positive or a negative integer as input and returns the same value. ...
2
votes
1answer
64 views

Fetching and displaying the provisioning status of a phone number

I have the following function: ...
4
votes
2answers
2k views

Number of files with specific file size ranges

I am trying to write a script that will traverse through my directory and sub directory and list the number of files in a specific size. For example, 0kb-1kb: 3, 1kb-4kb:4, 4-16KB: 4, 16kb-64-kb:11. ...
4
votes
5answers
2k views

Getting a 4.0 GPA

This is my code, for calculating a GPA for 7 subjects. It works, but is there a better way? Any hints on making it more flexible? ...
7
votes
5answers
1k views

Interactive dice simulator

After finishing my course on CodeAcademy, I wanted to write a program. Please provide opinions. ...
4
votes
1answer
23k views

Ping function in Python

Below is a piece of code which takes a list of IPs, pings the hosts and returns (prints) the list for "Alive" and "Dead". Any feedback on what can be done better is welcomed, mainly, speed and ...
9
votes
3answers
3k views

Bulls and Cows in Python

I have recently written this little Bulls and Cows game in Python: ...
2
votes
1answer
248 views

Python Koans multiplayer Greed Game

I finished the Python Koans because I wanted to practise Python and I completed the extra assignment which is the Greed Game (see the rules here). I think there are no flaws but you never know. <...
5
votes
1answer
727 views

Edit configuration files script

Script designed to avoid manually edit configuration files by support. Config file example: ...
5
votes
2answers
413 views

Reflecting emotion classification based on the Lövheim cube

Background I created a simple class to reflect emotion classification based on the Lövheim cube. The code is not scientific at all, and I just did it for fun, but I want all code I write to be as ...
3
votes
3answers
333 views

Python greed roll

I am doing the Python Koans to practise Python a bit more and I got to the greed dice project. It is a very simple dice game that scores a set of up to five rolls. These are the rules: ...
13
votes
3answers
6k views

Optimizing an Anagram Solver

I've built an anagram solver in Python 2.7. It goes through a text file with over 100,000 words and checks if any permutations of the input matches the line. It works great, except it is very slow. I'...
6
votes
1answer
9k views

Filtering with multiple inclusion and exclusion patterns

I have a requirement to be able to filter a list of strings by both inclusion and exclusion patterns (using fnmatch-style wildcards), of which there can be many. ...
2
votes
3answers
287 views

Function to combine list elements

I was working on a little project that at the end of the script would write to a CSV file. I had to create a function that would combine multiple list elements of the same size into a single one. I ...
5
votes
6answers
1k views

Hangman game in Python

While I'm not new to Python, this is my first 'bigger' Python program I created, also my first game in Python. ...
1
vote
4answers
12k views

Merge two dict in separate list by id

I have following data structure >>> a = [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}] >>> b = [{'id': 1, 'age': 12}, {'id': 2, 'age': 21}] I ...
1
vote
2answers
820 views

Class for multithreaded insert into database

Here I have class that supposed to take dictionaries of product objects and their articles (kind of unique identifier) from queue and insert or update them in database table. Received dictionary ...
7
votes
5answers
1k views

Finding the occurrences of all words in movie scripts

I was wondering if someone could tell me things I could improve in this code. This is one of my first Python projects. This program gets the script of a movie (in this case Interstellar) and then ...
7
votes
3answers
2k views

Checking for intersection points

The aim of the program is to find those points which comes under the intersection of at least 2 circles.(space is a 1000x1000 matrix) ...
3
votes
2answers
1k views

Process zip files

I have zip bundle, for example, abcd.zip, contains more zips like 1.zip, 2.zip etc. Inside of each child zip there is a .jpg file like 1.jpg, 2.jpg etc. There are so many other files but I need only ....
11
votes
3answers
11k views

Counting Vowels

I'm learning to program and I've chosen Python as my first language. I've written a function to count vowels in a string and print the number of occurrences of each of them. Please review my code and ...
1
vote
1answer
76 views

Write to a new, modified Excel file from an original one

I noticed that I've a HUGE bottleneck in this following function of mine. I can't see how to make it faster. These are the profiling test results(keep in mind that I'm using a PyQt GUI so times can ...
0
votes
1answer
8k views

Find a string in a Excel cell in the fastest way

I have to implement a "filter" in a Excel file which deletes/shows particular strings found in specified columns. I save the rows numbers that have to be deleted in a list. I have my filters in ...
6
votes
2answers
464 views

Low-level methods with temperature conversion program

This is my first program to convert temperatures. I have a very basic knowledge of Python and I tried using only what I already knew to make the program. What are some improvements I can make, or how ...
4
votes
1answer
77 views

Representation of the formula P(n) = (n!)(6^n)

I'm trying to represent the following mathematical expression in Python: $$P(n) = (n!)(6^n)$$ The program should compute the answer to expression when n = 156. I ...
2
votes
1answer
57 views

Split CSV by Repeated cells python

Any time that a row ID (oddly placed in column 8, i.e. row[7]) is repeated after the first instance, I want to write those rows into a second file. The code I'm using is extremely slow -- it's a 40-...
2
votes
3answers
866 views

Simple shared resource in Python

I want a 4-bit resource to share status between processes and to retain status after shutdown and reboot. If the system were a microprocessor, I'd use a 4-bit register in non-volatile memory. ...
4
votes
2answers
4k views

Twin Prime Algorithm Optimization

I have wrote the following algorithm to print out the Nth twin prime numbers: ...
1
vote
1answer
283 views

A `get_context_data` method for selecting and updating images displayed to a user over a multi page form

Below are the get_context_data method and the done method of a SesionWizardView. A SessionWizardView is designed to split a form over multiple pages. The ...
3
votes
1answer
36 views

URL selection function that is designed to randomly assign users to different surveys until full

I'm trying to condense some of my code to make it more maintainable and to learn a few new tricks. Below is a Python 2.7 / Django 1.6 function which is designed to assign users to one of three ...
1
vote
1answer
9k views

Parse a csv file and create a dictionary of partial results

I have a bunch of .csv files which I have to read and look for data. The .csv file is of the format: ...
2
votes
1answer
160 views

Random Distribution to fit an Average Python

This is from my own question on Stack Overflow. I want to get average numbers from a range such that they fit an intended average. I expect something bell-curve-like, but asymmetrical unless the ...
15
votes
3answers
735 views

Virtual machine management with decorated selfies

I have a class which provides various methods for retrieving information from a VMware vSphere environment. Over the time my class got extended and became large, so I thought I should spend some ...
5
votes
2answers
6k views

Testing whether a given string has unique characters

Here is my code which tests whether a given string has unique characters. The function is_unique which does a linear search for each character in the string is \$O(...
1
vote
2answers
205 views

Sieve of Erathosthenes speedup

While reading HackerNews, I learned about a data set of the first 50 million primes. This would be a lot, but his website has disclaimer: n this directory I have the first fifty million primes in ...
7
votes
4answers
43k views

Count occurrence of words in a .txt file

I'm taking an intro to programming class and although I've learned some things I didn't know before (I've been using Python for about 1.5 years) I feel like I've not progressed much in writing "...
6
votes
2answers
35k views

Function to return percentages in Python

I created a function for giving me percentages from two integers. I want two decimal places in the result. ...
38
votes
4answers
4k views

Someone thinks poorly of my server log parser

I have just been informed that the following code written by me is extremely poor. I have absolutely no idea why. It is memory efficient, and looks clean to me. But still the feedback is very poor. I ...
2
votes
2answers
98 views

Subclassing Profile.Profile to use it as a context manager

I was profiling my code and annoyed by the continuous calls of Profile.run(my_function()) Profile.run(my_next_function()) so I decided to make a context manager ...
2
votes
0answers
1k views

Optimize QuadTree to find K Nearest Neighbors

I'm looking a way to make my k nearest neighbors search more efficient. The context of the question is that I'm given a list of topics that have a unique ID (integer) and a (x,y) coordinate (floats) ...
4
votes
2answers
2k views

Custom sorting a list of objects efficiently

I'm trying to make sorting of objects in my program as fast as possible. I know that sorting with a cmp function is deprecated in python 3.x and that using keys is faster, but I'm not sure how to get ...
9
votes
5answers
5k views

Python odd number diamond

I worked up a quick diamond program that takes only odd numbers while returning an error with even numbers or 0. The code looks rather ugly to me, especially with the recurring ...
3
votes
1answer
2k views

jQuery slider in a Python/Django SessionWizardView

I have created a Data Verification page in a Django SessionWizardView where a user has to confirm an earlier submitted rating of an image using a jQuery UI Slider (-...
3
votes
1answer
326 views

Python code to write simple VPK archive files

I'm new to Python, but have a lot of experience with C and C++. I would appreciate some feedback on my implementation of a packer for this archive format. Specifically, any feedback on how idiomatic ...
6
votes
3answers
35k views

Padding a hexadecimal string with zeros

I'm given a hexadecimal number in string form with a leading "0x" that may contain 1-8 digits, but I need to pad the number with zeros so that it always has 8 ...