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

Need advice of refactoring views.py

I setup simple CRUD in Django ...
2
votes
1answer
120 views

Longest-match lookup via nested dicts

I have the following datastructure to perform a longest-match lookup via nested dicts. For example. cs['a b c d'] with a and <...
2
votes
1answer
2k views

Craps dice game simulator

This craps game simulator is based on someone else's posted Python code that I've redone for practice. I'm interested in improving readability and making it more pythonic. I used ...
3
votes
2answers
3k views

Download image links posted to reddit.com

This is a Python script to save imgur pictures posted to reddit.com forums. I'm looking for an assessment on the design of this script and any web security issues that might exist. Obvious ...
2
votes
2answers
2k views

Creating a dictionary of all prefixes of a string in Python

a=raw_input() prefix_dict = {} for j in xrange(1,len(a)+1): prefix = a[:j] prefix_dict[prefix] = len(prefix) print prefix_dict Is there any possibility of ...
1
vote
2answers
222 views

Die-rolling for ordering

I have a little experience with Python and am trying to become more familiar with it. As an exercise, I've tried coding a program where the ordering of a list of players is determined by the roll of a ...
2
votes
2answers
2k views

Python: flatten directories

Here is a simple Python script that I wrote with lots of help from the Python mailing list. I moves all the files of subdirectories to the top level directory. It works well for my use, but if there ...
1
vote
2answers
5k views

Three ways to sum the values of letters in words

I've created these 3 functions to count the value of letters in a word. All these functions are functionally the same. Apparently List comprehensions (count_word3)...
3
votes
2answers
1k views

Is Event.wait necessary here? [closed]

When I read some code,I think the usage of Event is not necessary,is it right? ...
2
votes
1answer
5k views

Custom thread-pooling

Any suggestions/improvements for the following custom thread-pool code? ...
6
votes
4answers
31k views

Pinging a list of hosts

I have a simple script that will take a list of hosts and ping each host (there's about 200) a single time before moving on. This is not the most effecient method I am sure, as it is very linear. And ...
2
votes
1answer
278 views

Base class for feed-parsing

I have written a class which I will use as a base class, allowing other classes to extend this class. How can I improve this? Am I breaking any conventions or styling? ...
3
votes
2answers
6k views

Beautifulsoup scraper for sport events

I've written a simple scraper that parses HTML using BeautifulSoup and collects the data (schedule of sports events), then clubs them together in a list of dicts. The code works just fine, but the ...
1
vote
1answer
185 views

What specific optimizations can be made to this BitArray class, and how can they be implemented?

Here is the current code for the BitArray class needing optimization (implemented on big integers): ...
1
vote
1answer
152 views

Pythonic style guide [closed]

Which of these 3 python snippets is more pythonic? This 1 liner list comprehensions which is a little overcomplex ...
2
votes
2answers
181 views

Filtering logic

I have a FileAsset class. I have a set of 'filters' with which I determine if a FileAsset should be written to my database. ...
4
votes
2answers
860 views

A little scheme programming challenge

I am learning scheme to get something new from a programming language and this code below is the solution to Project Euler question 21 but the code runs 10x slower than the listed Python code when I ...
3
votes
1answer
731 views

Does this code use good Python practices for a context-free grammar implementation?

I'm completing exercises in Compilers: Principles, Techniques, and Tools (2nd Edition) in Python. The following code converts: ...
1
vote
1answer
3k views

Element-comparison NumPy matrix

I need a NumPy matrix with comparisons among all elements in a sequence: ...
3
votes
1answer
313 views

Minecraft auto-backup utility

I live in a country where an electricity blackout happens after every hour, which corrupts the Minecraft save files, so I came up with this auto backup utility which backs up the %appData%.minecraft\...
1
vote
1answer
83 views

Attributes with special behaviour in Python

What is the common practice to implement attributes that have side effects in Python? I wrote the following class which supposed to save singletons when a variable is going to get a certain value. The ...
4
votes
1answer
164 views

Counting the number of days worked for all commiters to a git repo

I wrote this script to collect evidence of the number of days worked for the purpose of claiming some government tax credits. I'm looking for some ways to clean it up. I'm especially wondering if ...
1
vote
1answer
4k views

Tackling the TSP with a genetic algorithm

This is my first go at genetic algorithms, using PyGame for the visual representation. Is there a way to improve the speed of the code? Click to place a point, press ↑ to start looking for the ...
2
votes
1answer
1k views

Python interactive console which automatically reload modules

I just started to program Python. As my first application I implemented an interactive console which automatically python modules before a new code block is evaluated. Here is the code: ...
3
votes
1answer
291 views

Isn't this dynamic programming?

I was solving problem 18 and 67 on Project Euler and came up with an algorithm that actually goes through almost all the possibilities to find the answer in O(n) time. However, ProjecEuler claims ...
2
votes
1answer
1k views

Median solving from Interview street in Python

Any suggestions to improve the performance of the following code? This code is exceeding the time limit for the last four cases. This is a solution to the problem https://www.interviewstreet.com/...
15
votes
2answers
27k views

Joining url path components intelligently

I'm a little frustrated with the state of url parsing in python, although I sympathize with the challenges. Today I just needed a tool to join path parts and normalize slashes without accidentally ...
4
votes
2answers
170 views

Timer class, to be taken as a model for other classes

I wanted a timer, rather like the Visual Basic object, and I wanted it with no cumulative error. And I wanted it flexible, so I wouldn't have to write another one. I'm very lazy BTW, so lazy that I ...
3
votes
4answers
2k views

Word Square generation in Python

For reasons unknown I've recently taken up generating word squares, or more accurately double word squares. Below you can see my implementation in Python, in about 40 lines. The code uses this word ...
4
votes
2answers
637 views

Script to drop HTML part of multipart/mixed e-mails

I have a working script that I wrote in Python which for e-mail messages that consist only of a plain text part + an HTML part, discards the HTML and keeps only the plain text part. The script is not ...
9
votes
1answer
18k views

Inversion count using merge sort

...
2
votes
1answer
184 views

How do I add memoization to this problem? [closed]

Problem Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 2020 grid? Solution Here is my ...
8
votes
2answers
35k views

Generating all combinations and permutations of a set of symbols

I am new to Python and want to have a "pythonic" coding style from the very beginning. Following is a piece of codes I wrote to generate all combinations and permutations of a set of symbols, e.g. <...
5
votes
3answers
1k views

Printing a score sheet for baseball batters

I've been learning Python 2.7 for about a week now and have written a quick program to ask for names of batters and printing a score sheet. I have then added the names to a list which I will use more ...
10
votes
1answer
3k views

Implementations of prefix lookup tables

I've got a fairly performance sensitive need for fast prefix lookup, and I've built two different implementations trying to optimize for my use case. The first is built off a Trie implementation, and ...
7
votes
2answers
4k views

Quick sort in Python

How can this be improved? ...
4
votes
5answers
11k views

Taking the first few items of a list until a target sum is exceeded

I'm a rank beginner in Python, and I am working my way through various relevant OpenCourseware modules. In response to the prompt Write a procedure that takes a list of numbers, nums, and a limit, ...
2
votes
2answers
4k views

Speed up two for-loops? [closed]

The code below is slow. Any thoughts on speeding up? ...
3
votes
2answers
20k views

Brute force HTTP with Python

I am playing around with brute force attack on my home network. I wrote the following script with Python. However progress is a little slow. Does anyone have a suggestion how to make this faster? <...
1
vote
2answers
340 views

Finding node paths

I am wondering if anyone has any ideas for improving the following. Personally I am 'happy' with it, apart from the first for loop. I was thinking of another way ...
3
votes
1answer
263 views

Monthly rollover system

I have learned a little Python for a few projects at work. This is my first finished application and I was hoping someone would take a look at it. I would love some tips on the mistakes I've made. <...
3
votes
1answer
717 views

Speeding up an algorithm for finding the number of distinct lines

Is there a way to speed up the following python code: ...
12
votes
1answer
5k views

Simple DNA sequence finder w/ mismatch tolerance

The goal with this function is to find one DNA sequence within another sequence, with a specified amount of mismatch tolerance. For example: ...
2
votes
2answers
454 views

Compound assignment operators in Python's NumPy library

The "vectorizing" of fancy indexing by Python's NumPy library sometimes gives unexpected results. For example: ...
7
votes
1answer
38k views

Reading ints line from a file in Python

I have to list a integers from a text file separated by newlines into a python list. I ended up with the code above which works (for my case) but certainly is far form optimal. ...
2
votes
1answer
85 views

Generic pass_many(vals, pass_one) to unroll and call pass_one(val)

I'm trying to create a generic function that takes a data structure as input and then sends each non-empty item—e.g.: str, int—individually: ...
4
votes
2answers
334 views

Improving runtime of prime generation

I just answered the question on project euler about finding circular primes below 1 million using python. My solution is below. I was able to reduce the running time of the solution from 9 seconds to ...
2
votes
1answer
2k views

Implementation of a Vector3D class

I'm an amateur coder writing a geometry library in Python, and would like some feedback regarding my implementation of a class for 3D Vectors. My priority is to have a really friendly API for ...
1
vote
1answer
250 views

C++ wrapper function for Python's "list.count([character])" method?

Is this a reasonable C++ wrapper function for Python's "list.count([character])" method? ...
4
votes
3answers
2k views

Alphabetizing `import` Statements [closed]

I did a quick google search, and it didn't show up. I haven't noticed it posted in pylint.py, or PEP8.py. My emacs mode doesn't warn me if my imports are listed ...

1
271 272
273
274 275
280