All Questions
Tagged with meta-programming python-3.x
34 questions
1
vote
2
answers
121
views
Python 3.10+ deconstructing an over engineered solution to better understand how metaclasses work with properties, static methods, and classmethods
TL;DR
This question examines an over-engineered example of python metaclasses and dataclasses to create a LiteralEnum (for validating a stringly-typed keyword ...
1
vote
3
answers
443
views
Decorator to validate that Python function arguments are positive
I have a list of functions whose parameters in the signature should be validated with the same criteria each time.
...
3
votes
1
answer
164
views
Lightweight interface implementation in Python 3 using abstract classes
Here is my take at a lightweight interface implementation, where I focus on discoverability of suitable classes from strings (for simplicity, class name is used as an id). Each interface has it's own &...
6
votes
2
answers
478
views
LazyEnum with validation
Motivation
In the standard Python library enum, a subclass of Enum would create all its members upon class creation. This can ...
4
votes
1
answer
140
views
A self contained parser generator implementation
This is a recreational project, I was trying to make a parser generator with a grammar inspired from: https://docs.python.org/3/reference/grammar.html
Unfortunately, understanding that specific ...
4
votes
0
answers
102
views
Inherit docstrings from specified parent
I was recently working on some python where I was working with multiple inheritance and mixins and stuff like that, and I wanted to inherit docstrings for specific functions, from specific parents (...
1
vote
0
answers
84
views
Using metaclass to decorate derived classes to be `attrs` classes
I am currently developing a framework to facilitate writing controller programs for scientific instruments. I use attrs extensively in the internal parts of the ...
7
votes
2
answers
155
views
Python class compressor
After watching this presentation, I decided that it would be a good project for me to undergo to practice my python skills. A summary of the presentation:
Jack ...
4
votes
1
answer
14k
views
Python decorator to support static properties
Classes in Python do not have native support for static properties. A meta-class can rather easily add this support as shown below. Are there any problems programmers might experience if they use this ...
3
votes
1
answer
1k
views
Python simple type checking decorator
I'd like to know your opinions on this minimal type-checking decorator (with @ annotations) to make type checking of a method while debugging like :
...
2
votes
1
answer
150
views
A simple decorator written as a class, which counts how many times a function has been called
Can I improve its typing? Is there any other improvement or pythonic change that you would do?
...
11
votes
3
answers
1k
views
Simple generic auto __repr__
Preface
At some point I was tired of writing & supporting __repr__ methods, so I've decided to write it once and reuse everywhere in my classes.
Since I'm ...
3
votes
0
answers
83
views
Metaclass to run a __first__ method for the first instantiation of a class
I had an idea for a __first__ special method for a class. It would be run after __new__ and before ...
4
votes
1
answer
68
views
Decorator to add key parameter support to an existing sort function
Preface
sorted built-in function has key keyword-only parameter. Most of the custom implementations like the following for ...
7
votes
1
answer
295
views
Using Python decorators to do Hoare Logic
I'm relatively new to Python from a strong Java background. I am trying to create a base class to do Hoare Logic. Function decorators seem to be the way to go with this. I've produced the following ...
3
votes
1
answer
666
views
Function overloading in Python
Motivation
As an exercise, I wanted to try implementing function overloading in Python 3, i.e. to implement a way of defining multiple functions with the same name and then calling the appropriate ...
3
votes
1
answer
595
views
Decorate instance method with (arbitrary) meta data in python
Requirements
What I need: attach meta information to methods, such that
it is 'easy' to retrieve all such methods for a given class instance
methods can still be called 'in a normal way', e.g. ...
1
vote
1
answer
125
views
Defining many HTTP request methods in a class using partialmethods
I am using an object that expects a keyword argument for every call, but I never intend to change it. In this case a timeout of 10. What is the best way to add a partialmethods for each attributes.
...
5
votes
0
answers
891
views
Writing a metaclass to ensure a class is thread-safe
Python allows the creation of classes to be modified via metaclasses. They can perform all sorts of operations that might appear to be magical to languages that do not have the capability. The goal of ...
1
vote
0
answers
1k
views
Marking a class as final or sealing a class
Which of these metaclasses should be preferred, and is there room for improvement?
Some people are not familiar with the philosophies that come with working in Python and have a desire embedded in ...
7
votes
1
answer
1k
views
Decorator to return default argument values
I'm using Python 3.6. This code, used as a decorator to a python function, will cause the function to return a dict of POSITION_OR_KEYWORD arguments.
I used this ...
5
votes
0
answers
715
views
Implement OO interfaces in Python
Edit: Whoops! I just remembered there's already an long-lived package for this, zope.interface. I'd like a review all the same.
This is a proof-of-concept ...
4
votes
1
answer
4k
views
Class decorator in Python to set variables for the constructor
I personally don't like the boilerplate of most __init__ methods:
self.a = a
self.b = b
...
So I thought this would be a nice ...
2
votes
2
answers
735
views
Memoizing decorator with retries - now with backoff added! (Part 3)
A continuation of Memoizing decorator with retries, part 2, and related to https://codereview.stackexchange.com/a/133493/47529. I liked my decorator before, but especially in my original use case of a ...
6
votes
3
answers
583
views
Memoizing decorator with retries, part 2
A while ago I asked this question Memoizing decorator that can retry and then promptly forgot about it. I more recently saw Python decorator for retrying w/exponential backoff and wanted to add ...
8
votes
2
answers
902
views
Strict types decorator (works only with Python 3.5)
I wrote a decorator which makes Python 3.5 raise exceptions if the arguments that are passed to a type-hinted function are of the wrong type.
...
12
votes
2
answers
2k
views
Memoizing decorator that can retry
I have some tasks that I'd like to memoize because they connect to a rather slow network and have to wait for the data. Unfortunately this network can be a little finnicky and we get occasional ...
11
votes
1
answer
4k
views
Try-except decorator for classmethods
This question is related to the Cactus Text Game Engine.
For easy error handling in Cactus, we've put together this small decorator. Essentially, the decorator wraps any class method in a ...
4
votes
1
answer
3k
views
Dictionary with restricted keys
I'm currently building some software that works with systemd unit files, and I've been trying to improve how we construct the unit files.
This is all running in Python 3.4
This first block is the ...
3
votes
2
answers
6k
views
Failing fast by raising an exception with a custom message
This is a script that utilizes lazy exception throwing. What I do is decorate a function that might throw an exception to throw an exception with a formatted string.
...
2
votes
1
answer
2k
views
Python decorator for optional arguments decorator
I want my Python decorators to have optional arguments and not be called when not necessary.
The accepted answer in here doesn't accept named arguments, and I don't want to add boilerplate code ...
9
votes
1
answer
832
views
Multiple dispatch decorator classes in Python
This is based on my first review and the suggested points in "Multiple dispatch decorator in Python":
...
6
votes
1
answer
2k
views
Hooking with Python3 Decorators
I wrote this prototype after reading the Wikipedia article on hooking. I didn't bother to read any of the code examples listed there because, well, I just didn't. I don't have a good excuse.
The ...
3
votes
1
answer
359
views
Streamlining repetitive class definitions in python with a class_factory() function
I forked this repo to be more concise. The code is here. I'll paste it below since that seems to be the style. I removed the class definitions at the bottom that I didn't change -- the edit I'm ...