Skip to main content
12 votes

Change arbitrary arguments of function based on their names with a decorator

You can use inspect.signature to simplify your code. ...
Peilonrayz's user avatar
  • 44.6k
8 votes
Accepted

Using decorated methods in a subclass of deque to dump its state to a JSON file

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 ...
Dan Oberlam's user avatar
  • 8,049
8 votes

JVM bytecode instruction struct with serializer & parser

std::variant vs union Avoid blatant memory waste: One of the most common ways i saw other projects implement their instruction ...
G. Sliepen's user avatar
  • 69.3k
7 votes
Accepted

Mutable Named Tuple - or - Slotted Data Structure

Here are some issues I noticed in your implementation. Currently you're simply iterating over __slots__ everywhere, but this doesn't handle the case when ...
Ashwini Chaudhary's user avatar
6 votes
Accepted

Decorate instance method with (arbitrary) meta data in python

From your three approaches, I think MacroWithoutSuperclass is probably the cleanest. I wanted to just comment a bit, but it turned out I had too much to say... Thus,...
Sebastian Höffner's user avatar
6 votes
Accepted

Detecting existence of a class member

Your detector fails in the face of overloading and templating. Also, it will only detect (static) member-variables and (static) member-functions. While you can extend it to types (and type-aliases), ...
Deduplicator's user avatar
  • 19.9k
6 votes
Accepted

How can I make this CSV Importer code better of any code smell?

I like that you already extracted a WebCsvImporter Plain Old Ruby Object which makes the code already a lot easier to understand and refactor. Here is a refactored ...
Christian Bruckmayer's user avatar
6 votes

JVM bytecode instruction struct with serializer & parser

Some style critique from me (the principles seem sound). Since the standard headers are independent of each other, we can include them in a consistent order. I prefer alphabetical, but you could ...
Toby Speight's user avatar
  • 88.3k
5 votes

Python simple type checking decorator

To amplify @200_success’s comment: Python 3.5 introduces type hints. Eg) ...
AJNeufeld's user avatar
  • 35.3k
5 votes

Using Python decorators to do Hoare Logic

You are ignoring the pre_execution and post_execution return values. ...
AJNeufeld's user avatar
  • 35.3k
5 votes
Accepted

Python Export Decorator

Personally, I'd prefer to use @export() or @export(globals()), rather than manually perform the ...
Peilonrayz's user avatar
  • 44.6k
5 votes
Accepted

Transforming stored procedures using Python regular expressions

In the interest of keeping it 'interesting', clear and (finally) short, I have the following suggestions: Consolidate the calls to your database: in total, you execute ...
act's user avatar
  • 426
4 votes

Mass and length calculator using Perl 6 custom operator

This is an interesting use case of postfixes. Here's how I would go about it. Unfortunately, because postfixes can't take regexen, you need to specify each one individually, but it's not horrible, ...
user0721090601's user avatar
4 votes
Accepted

Decorator to return default argument values

I am not sure how critical this use case is, but when any of your arguments have a dictionary value, you have that dictionary items used as keyword arguments for the function: ...
alecxe's user avatar
  • 17.5k
4 votes

Mutable Named Tuple - or - Slotted Data Structure

This Python is mostly over my head — and Ashwini just gave a more complete answer — but I do notice this infelicity: ...
Quuxplusone's user avatar
  • 19.7k
4 votes

Class Decorator for Verifying Method-Level Permissions

The mechanism for getting at the user argument does not work: ...
4 votes
Accepted

Python class compressor

That's a difficult problem to attack. Without going into those difficulties, here are some comments on your code. Global variables are generally frowned upon. They are basically a hidden function ...
RootTwo's user avatar
  • 10.7k
4 votes

Python decorator to support static properties

You've masked a bug, in __setattr__ a property raises an AttributeError if the setter hasn't been defined. This causes you to ...
Peilonrayz's user avatar
  • 44.6k
4 votes

LazyEnum with validation

This right here is where I would start asking myself if this is a good idea or if there isn't a better way to achieve this: The first non-self parameter of ...
Graipher's user avatar
  • 41.7k
4 votes
Accepted

LazyEnum with validation

High-level LazyEnum should be separate from the underlying datatype. You should allow a similar interface like the following: ...
Peilonrayz's user avatar
  • 44.6k
3 votes

Python decorator for retrying w/exponential backoff

Use the backoff library instead: https://pypi.org/project/backoff/ Also use jitters to improve performance. See here: https://aws.amazon.com/blogs/architecture/...
wasserholz's user avatar
3 votes
Accepted

Encryption scripts using the module cryptography

The good news is that as far as I can tell, the cryptography is correct (password-based key derivation function, random salt). The bad news is that your code is hard to read, and so it's hard to make ...
Gilles 'SO- stop being evil''s user avatar
3 votes
Accepted

Two java classes with different annotation values only

You can parameterize the @path annotation with regular expressions to specify one class/method that accepts multiple paths ...
Sharon Ben Asher's user avatar
3 votes
Accepted

Generic framework to handle parameterized commands

Might as well post what I did to improve this. Don't use __class__ if you don't have to I was already taking advantage of enum values being instances of the class,...
Dan Oberlam's user avatar
  • 8,049
3 votes

clean approach to decorator that takes optional keyword arguments

Nice little snippet! A couple of minor points: It's usually better to put the shorter block first in an if else. This stops the <...
QuantumChris's user avatar
  • 1,405
2 votes

Encryption scripts using the module cryptography

fscript.close doesn't do anything. Perhaps you meant to call fscript.close(). In any case, the preferred way to open a file is ...
200_success's user avatar
2 votes
Accepted

Class Decorator for Verifying Method-Level Permissions

For a class more complex than this, I would think that per-method decorators are more readable and provide better documentation: ...
Graipher's user avatar
  • 41.7k
2 votes
Accepted

Defining many HTTP request methods in a class using partialmethods

It is certainly possible to do what was outlined that lies outside the class definition within the class definition itself through the usage of locals, and with ...
metatoaster's user avatar
2 votes
Accepted

Basic profiler - Python

I would make more use of your context manager. While it is clear from the docstring of end_section that it ends the most recent section, this is not so clear just ...
Graipher's user avatar
  • 41.7k
2 votes

Python Export Decorator

If there's too much magic, I worry about static linters and new users not understanding the code, so I would want to support proper global variable assignment. On the other hand, Peilonrayz's answer ...
Aaron Hall's user avatar
  • 1,568

Only top scored, non community-wiki answers of a minimum length are eligible