All Questions
Tagged with meta-programming python
71 questions
3
votes
1
answer
84
views
caching decorator
I came up with a caching decorator for pure functions. Is it ok? Could it be better/simpler/faster?
...
6
votes
2
answers
7k
views
Custom array indexing
Note that this code is not going to be used for anything, it's more of a thought experiment. I've been seeing a lot of NumPy code lately and noticed that they use a custom indexing process, presumably ...
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 ...
4
votes
1
answer
2k
views
login_required decorator in Flask
I have 2 Flask apps (different projects) that work together . One implements some API which uses tokens for auth. The second one consumes the API and makes a web interface for it. Now I have a login ...
2
votes
1
answer
11k
views
Using a decorator to apply an inherited method to all child objects of the inherited type
I wanted to share this and get some feedback. My goal was to be able to have an object with methods:
...
2
votes
0
answers
2k
views
Making a default value a property
I recently asked a question on StackOverflow, looking for a way to more easily turn a class attribute into a @property, but only if no other value has been provided ...
7
votes
1
answer
342
views
Monkeypatching __builtin__ in tests for mocking
I'm testing a little magic behaviour of my script that automatically reads from ~/.ghtoken to do automagic authentication on GitHub API requests. I want to test ...
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.
...
5
votes
1
answer
740
views
Descriptor class with advanced (inspect/metaclass) functionality
Having answered this question over on Programmers.SE, I found myself wondering how much effort it would be to write a descriptor that can automatically figure out what the 'destination' attribute ...
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 ...
4
votes
4
answers
3k
views
Python Decorator - inspecting function argument values
Some of my functions use a "fail_silently" flag. It is used in the following way:
...
3
votes
1
answer
82
views
user_interface module
As writing code to handle error-checked input or to present functions to execute to the user is boring and error-prone, I wrote a module to automate these tasks:
...
4
votes
1
answer
820
views
Meta-class to allow inspection of all __slots__of a class
If classes provide __slots__ and at the same time inherit from another class also providing __slots__, there is no ...
3
votes
2
answers
240
views
Synchronous and asynchronous motor movement
I am working on an API that is mostly intended to be used interactively/little scripts. There are some classes with methods that could act slightly differently depending on the intention of the user, ...
4
votes
1
answer
819
views
Universal memoization decorator
I've just written a simple caching / memoization python decorator. It's purpose is to cache what the function returns for all the arguments combinations it's been ever invoked with.
So, if, say, we ...
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 ...
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.
...
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":
...
3
votes
1
answer
453
views
General way to add persistence to a class in Python
The idea here is to write a function that gives you back a persistent version of a class that you supply. So you if you run
...
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 ...