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
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
858 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
697 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
311 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
163 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
26k 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
183 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
262 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
452 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
37k 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 ...
5
votes
2answers
667 views

Reading a controll character in a loop in Python [closed]

I have a code snippet below and because of habit from other programming languages I wanted to use a do-while loop instead. But Python offers only the construct ...
13
votes
1answer
6k views

Self-playing Tetris game

I have an extremely simple self-playing Tetris game which I coded up and I am looking to see how it could be improved. It would also be a learning curve for me to see how those much better than I am ...
2
votes
2answers
3k views

Printing nth bell number

Bell number \$B(n)\$ is defined as the number of ways of splitting \$n\$ into any number of parts, also defined as the sum of previous \$n\$ Stirling numbers of second kind. Here is a snippet of ...
1
vote
3answers
158 views

I think I'm not using Try Except properly in this Python code [closed]

Please offer some advice for this code. It all works, but I know that its messy and I don't think the Exception catching is doing what I want it to. Thanks in advance for any and all advice... <...
6
votes
3answers
11k views

Blackjack card game

It's been a long while since I've done any Python programming so I thought I'd start with a simple Blackjack game. Any comments on how I can make this more readable? Use more methods perhaps? ...
4
votes
1answer
1k views

Parse a text file containing settings for neuroimaging software

I would like to refactor a large Python method I wrote to have better practices. I wrote a method that parses a design file from the FSL neuroimaging library. Design files are text files with ...
2
votes
3answers
84 views

Building and getting a form of objects that have multiple properties

I'm building a form dynamically in a web app where I'm using product_id to keep track which product I'm reading and then ...
18
votes
2answers
6k views

Replacing Python Classes with Modules [closed]

I try to avoid using classes in Python as much as possible; if I don't plan on building on it, I don't build it in the first place. It helps me avoid Java-like classes like ...
2
votes
1answer
684 views

How to improve this SmartDownloader?

This is a class that downloads files in a smart way: it tries to download from different sessions. This do speed up things. How can I Improve it? ...
3
votes
2answers
307 views

Performing discrete mathematics operations on conjunctive statements

The program is supposed perform discrete mathematics operations on a number of conjunctive statements (see lines 34 - 50). Currently I just have it so they print out the tables in 0's and 1's, but I'...
0
votes
2answers
229 views

How to Improve This Program, Add Comments, etc.?

Do you have any suggestions to improve or extend this script? Any ideas for removing or adding comments/docstrings? ...
1
vote
1answer
436 views

JPEG metadata processor

I am looking to make the below code a bit more efficient / OOP based. As can be seen by the X, Y & Z variables, there is a bit of duplication here. Any suggestions on how to make this code more ...

1
266 267
268
269 270
275