Dan Oberlam
  • Member for 7 years, 7 months
  • Last seen this week
Python code to retry function
25 votes

I like all of Ev. Kounis' answer, so I'll add some higher level details. Let it be truth-y Right now you aren't strictly requiring func to return True or False, just something truth-y or false-y. ...

View answer
4chan Tripcode Explorer in Python
Accepted answer
21 votes

I know this isn't what you're interested in, but I'm going to talk about style first - as is, I can't read this at all. The biggest issue is that you seem to have some sort of dislike for whitespace -...

View answer
A library to do maths with matrices written from scratch
Accepted answer
20 votes

Compared to Python's NumPy, my matrix multiplication is quite a bit slower. Can it be made faster without using sophisticated matrix multiplication algorithms like Strassen's? One issue is that you ...

View answer
Elementary, Dr. Watson; it's a console adventure
16 votes

First of all, I'd say that lines of code (LOC) isn't a useful metric in most cases - some things just take more code, and having it on multiple lines isn't a bad thing. That being said, here are some ...

View answer
Pokémon style battle game
Accepted answer
14 votes

Magic numbers Right off the bat I see some magic numbers moves = {"tackle": range(18, 26), "thundershock": range(10, 36), "heal": range(10, 20)} What does that mean? Being ...

View answer
Most common letter in a list
Accepted answer
13 votes

There are a few problems with this; mainly that you only support the letters a, b, and c. It isn't necessarily a problem if those are the only letters you'll ever get, but what is a problem is that ...

View answer
Can I draw a square with pixels more efficiently?
10 votes

Disclaimer I don't have a working C compiler on my work computer, nor do I have Xlib available to me (I'm also not familiar with it, at all). That being said, given that we know our image is a square, ...

View answer
Building Slack message
Accepted answer
10 votes

I've come back to this question about 4 or 5 times since it was originally posted, started writing up an answer, started doubting my alternative implementation, scrapped it, and then come back about a ...

View answer
Human-like mouse movement
Accepted answer
10 votes

You can get much cleaner code and more readable code if you do 3 things: Use OOP and custom types to better encapsulate values Use more descriptive names Write more helper methods that encapsulate ...

View answer
Rainfall - August 2016 Community Challenge (Hooray for graphs!)
Accepted answer
10 votes

NetworkX is nice, but you really aren't using a lot of its features, and the features you are using end up not being that pivotal; your implementation won't be changed (much) if you remove NetworkX ...

View answer
Rubik's cube timer & scrambler
10 votes

Typically user-defined types are named with a leading capital, i.e. Times, Split, etc. You should put your interface in a header (hpp) file, and your implementation in a cpp file. I like to ...

View answer
SIMD matrix multiplication
10 votes

Restructure into a function to make it DRYer Right now the structure of your code is pretty unpleasant, not very DRY, etc. The first thing I'd recommend is restructuring this to work in a function. ...

View answer
Email checker ('@' sign, provider, domain extension)
9 votes

My first comment would probably be that you shouldn't try to validate emails beyond checking that they have at least one character before an @ sign, one . after the @ sign, and at least one character ...

View answer
Using decorated methods in a subclass of deque to dump its state to a JSON file
Accepted answer
8 votes

I don't think that what you're doing is good style. I think you're adding a lot of extra complexity and places for things to go wrong, or become hard to understand, or whatever. I don't think you lose ...

View answer
Convert a custom object to a query string
8 votes

In places where the type is obvious (or irrelevant) then feel free to use var instead of the type name. Also, consider not naming your variable after the type; this is also generally not necessary. I ...

View answer
Read stdin like a dictator
Accepted answer
8 votes

I really don't like that you detect which version of _GetchX to use via an ImportError - that isn't obvious to me at all. I also don't like that you keep importing things locally. I think you can ...

View answer
Slot Machine in Python
Accepted answer
8 votes

I'd say you should encapsulate as much of your code as possible into functions and classes, limiting the global state when possible. This serves two purposes - the first is that it improves debugging ...

View answer
Setting the scene for a role-playing game
8 votes

You should always close any file you open. You can guarantee this by using a context manager. with open("some_file_name") as f: x = int(f.readline()) This is a safe (and clean) way to guarantee ...

View answer
Zombie killers ranking
Accepted answer
7 votes

As a heads up - we have very different formatting styles for our code. Feel free to ignore that difference and don't consider it a comment on your style (unless you prefer mine, in which case please ...

View answer
Poor man's f-strings in Python 2.6
Accepted answer
7 votes

The thing most likely, in my mind, to cause problems here is that using frames is not guaranteed to work in any other Python implementation besides CPython. inspect.currentframe() Return the frame ...

View answer
O PC, why are you so slow?
7 votes

It's going to be hard to improve this very much, because your code is likely limited by the network IO you're doping to connect to OPC. On my machine I've disabled the OPC calls (I don't have it ...

View answer
Python decorator for retrying w/exponential backoff
Accepted answer
7 votes

First of all, when I start writing decorators of more than moderate complexity, and especially if they take parameters, I usually transition to writing them as classes - I find that easier to reason ...

View answer
Finding integer with the most natural dividers
6 votes

As far as conciseness, the naive approach is probably about as good as you can get. A slightly simpler solution would use a generator expression + sum def allnums_comprehension(k): return sum(1 ...

View answer
Convert Integer to Binary String
Accepted answer
6 votes

Your implementation seems fine at a glance, but Python actually provides two really easy ways to do this: string.format bin Your function would then become: def integer_to_binary_string(int_value): ...

View answer
Pythonic way to flatten lists or arbitrary depth using assignment expressions
6 votes

Representing lists as a head and a tail is a common method, but honestly not the most Pythonic way to do things. Additionally, everything you're doing is appending lists together - this is generally ...

View answer
Update GIS Project by matching files from directory with files already loaded and apply relevant symbology
Accepted answer
6 votes

Wow. That's a lot of code. The very first thing that needs to be done with it is modularization. You need to structure your code such that named functions and/or classes encapsulate shared ...

View answer
LRU cache in Python
Accepted answer
6 votes

This should be a class - this is what OOP exists to do, abstract away details like this and contain them all in an object. You'll want to subclass collections.MutableMapping. You can also use an ...

View answer
Creating a Kinesis stream, Firehose, and lambda event source from config file
Accepted answer
6 votes

Naming I don't think your names are very descriptive. You call your class Stream, for example, and then have to explain in the docstring that it is a Kinesis Stream. Just call the class KinesisStream ...

View answer
WiFi adapter settings
Accepted answer
6 votes

This seems pretty straightforward, so I don't have a ton of critiques. Naming PEP8 conventions say your function should probably be named wifi (which is not a great name) The names filepath1 and ...

View answer
Writing a Python class that provides default instance succinctly
Accepted answer
6 votes

I'd say a much easier way to accomplish this is to use a module instead of a class. In a language like Java the primary form of encapsulation is a class, while in Python, the primary form of ...

View answer
1
2 3 4 5