12
votes
Change arbitrary arguments of function based on their names with a decorator
You can use inspect.signature to simplify your code.
...
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 ...
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 ...
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 ...
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,...
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), ...
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 ...
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 ...
5
votes
Python simple type checking decorator
To amplify @200_success’s comment:
Python 3.5 introduces type hints. Eg)
...
5
votes
Using Python decorators to do Hoare Logic
You are ignoring the pre_execution and post_execution return values.
...
5
votes
Accepted
Python Export Decorator
Personally, I'd prefer to use @export() or @export(globals()), rather than manually perform the ...
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 ...
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, ...
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:
...
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:
...
4
votes
Class Decorator for Verifying Method-Level Permissions
The mechanism for getting at the user argument does not work:
...
Community wiki
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 ...
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 ...
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 ...
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:
...
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/...
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 ...
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
...
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,...
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 <...
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 ...
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:
...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
meta-programming × 135python × 69
python-3.x × 32
c# × 22
object-oriented × 16
ruby × 16
rubberduck × 13
reflection × 10
error-handling × 9
memoization × 7
roslyn × 7
c++ × 6
unit-testing × 5
java × 4
beginner × 4
parsing × 4
vba × 4
python-2.x × 4
regex × 4
ruby-on-rails × 4
scala × 4
javascript × 3
functional-programming × 3
logging × 3
inheritance × 3