Skip to main content

Questions tagged [meta-programming]

Meta-programming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyze and/or transform other programs, and even modify itself while running.

Filter by
Sorted by
Tagged with
1 vote
2 answers
168 views

TL;DR This question examines an over-engineered example of python metaclasses and dataclasses to create a LiteralEnum (for validating a stringly-typed keyword ...
SumNeuron's user avatar
  • 291
8 votes
2 answers
1k views

I am writing a java class file parsing & serialization library. As part of that i needed to implement a structure for the JVM bytecode instructions as well as come up with a way to parse & ...
p0sa's user avatar
  • 81
1 vote
3 answers
497 views

I have a list of functions whose parameters in the signature should be validated with the same criteria each time. ...
SeF's user avatar
  • 199
1 vote
1 answer
71 views

In Python you can use print(locals()) to print a dictionary of all the local variables, but this has 3 shortcomings: Double underscore variables will be included ...
Caridorc's user avatar
  • 28.2k
1 vote
1 answer
748 views

Idea is to have an AutoBind decorator which you could append in front of any method to have it preserve its own this, instead of manually binding it when called. <...
Ilija Ivic's user avatar
9 votes
1 answer
836 views

I ran across an issue that proved to be more complicated than I thought: Changing an arbitrary argument of a function purely based upon its name in Python - possibly via a decorator. I tried to ...
s-m-e's user avatar
  • 273
3 votes
1 answer
217 views

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 &...
Roman Susi's user avatar
2 votes
1 answer
232 views

I wrote this method a few years ago in a Ruby on Rails project, which I think I am not proud of. How can I make this code better to show in-depth professionalism in Ruby? lib/merchant/web_csv_importer....
Afolabi Olaoluwa's user avatar
1 vote
1 answer
75 views

I have created a module that validates the credentials against different databases. ...
Rajkaran Mishra's user avatar
3 votes
0 answers
136 views

Tried to do my own implementation of reflection(introspection) for using in my next projects. Is it optimized at compile time? If no, how can I improve it? This macro is good interface for adapting ...
Vladyslav Mozhvylo's user avatar
6 votes
2 answers
527 views

Motivation In the standard Python library enum, a subclass of Enum would create all its members upon class creation. This can ...
user141240's user avatar
4 votes
1 answer
162 views

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 ...
Jeacom's user avatar
  • 125
3 votes
1 answer
183 views

I am following a guide that specifies how to manage different configurations (DB_URI, LOGGER_NAME, etc...) per development environments. I create a config module ...
moshevi's user avatar
  • 270
2 votes
0 answers
107 views

I'm working on a web framework in C++ and there's this part that I need to do something like this: ...
The Moisrex's user avatar
3 votes
1 answer
161 views

I write decorators a lot in my work. Sometimes you need to write "decorator that takes arguments", it usually turns out to be something like this: ...
rabbit.aaron's user avatar
4 votes
0 answers
117 views

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 (...
Andrew's user avatar
  • 61
8 votes
1 answer
825 views

I wanted to detect if I have a member in a simple POD struct and after some searching and merging some methods I found on the web I've come up with this solution: ...
Pangi's user avatar
  • 370
7 votes
2 answers
178 views

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 ...
Ben A's user avatar
  • 10.8k
4 votes
1 answer
14k views

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 ...
Noctis Skytower's user avatar
3 votes
1 answer
1k views

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 : ...
user8426627's user avatar
1 vote
1 answer
1k views

Suppose I have some expensive function expensive_computation which takes a long time to compute but it always returns the same value. I want to avoid paying the ...
cglacet's user avatar
  • 243
4 votes
0 answers
331 views

I recently decided that putting strategically placed print statements scattered about my python programs to debug was getting to be a little messy. I had thought ...
Jebby's user avatar
  • 261
2 votes
1 answer
192 views

Can I improve its typing? Is there any other improvement or pythonic change that you would do? ...
Laurențiu Andronache's user avatar
11 votes
3 answers
1k views

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 ...
Azat Ibrakov's user avatar
4 votes
1 answer
136 views

I need to deserialize an XML file, without using any sort of "reflection" (or meta-programming). Instead, I need to do it via a raw object, that knows of it's structure and can properly identify and ...
Der Kommissar's user avatar
3 votes
0 answers
107 views

I had an idea for a __first__ special method for a class. It would be run after __new__ and before ...
13ros27's user avatar
  • 420
3 votes
0 answers
160 views

I have created a cumulative profiler, meaning it runs a function several times in a row and prints the cumulative profiler results. it's python >= 3.6 specific, but you can remove ...
moshevi's user avatar
  • 270
4 votes
1 answer
83 views

Preface sorted built-in function has key keyword-only parameter. Most of the custom implementations like the following for ...
Azat Ibrakov's user avatar
7 votes
1 answer
318 views

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 ...
Martin of Hessle's user avatar
3 votes
1 answer
709 views

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 ...
igal's user avatar
  • 391
12 votes
1 answer
579 views

I am sub-classing an object in python and I need to overload a bunch of the methods with very similar logic, essentially a call to super() and then some additional operations. I want to avoid ...
Alex's user avatar
  • 223
3 votes
1 answer
622 views

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. ...
Michael Hoff's user avatar
1 vote
2 answers
631 views

Its meant to password protect python scripts by using encryption It should use os.urandom(40)'s output in hex as the salt. The kdf (cryptography.io) is using Scrypt ...
user avatar
3 votes
1 answer
76 views

Consider two classes: First ...
wscourge's user avatar
  • 133
12 votes
1 answer
230 views

I asked this question a while ago, and this was one of the comments: There (mostly) seems to be a one-to-one mapping of a lot of your functions to IRC command verbs. For example, ...
Dan Oberlam's user avatar
  • 8,049
6 votes
2 answers
515 views

After writing far too many if has_permission(user): statements at the head of my methods, I figured I would try my hand at writing a generic enough decorator to do ...
Christopher Sheaf's user avatar
1 vote
1 answer
154 views

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. ...
Brian's user avatar
  • 327
7 votes
1 answer
113 views

I have created a simple profiler in Python, but I feel like there are better ways to do some things. Terminology: Section - a block of code to profile Label - A name for a section to be used as a key ...
internet_user's user avatar
5 votes
0 answers
912 views

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 ...
Noctis Skytower's user avatar
9 votes
2 answers
2k views

In Python, we use __all__ to explicitly denote the public API of a module or package. That looks like this: ...
Aaron Hall's user avatar
  • 1,568
3 votes
0 answers
86 views

I have code written in Scala that reads annotations decorating other Scala classes. These annotations are written in Java. The sticking point is that annotations don't allow advanced data structures, ...
bendl's user avatar
  • 131
3 votes
1 answer
2k views

I have to add some extra logic to about 80 similar stored procedures on my DWH server. I thought that it would take pretty much the same time if I do it with Python and lots of googling (I have no ...
Raphael Müllner's user avatar
1 vote
1 answer
440 views

The code creates a meta class and its implementation to set the values passed in as arguments to methods on a class automatically on the object avoiding a lot of ...
amadain's user avatar
  • 113
7 votes
1 answer
1k views

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 ...
James Schinner's user avatar
10 votes
2 answers
4k views

A question I answered on Stack Overflow asked for a mutable named tuple. The naive answer would probably tell them they just want to define a custom object. However, named tuples take up a lot ...
Aaron Hall's user avatar
  • 1,568
5 votes
0 answers
738 views

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 ...
shadowtalker's user avatar
16 votes
1 answer
25k views

This decorator adds the elapsed time to a function's attributes when applied. My concerns: Is the code pythonic? Could this code be useful? Should I just use the ...
Ricky Wilson's user avatar
  • 1,705
2 votes
1 answer
173 views

Here is an implementation in JavaScript that attempts to resemble Python-like arrays by mimicking slice comprehension for both getters and setters, as well as negative indexing from the end of the ...
Patrick Roberts's user avatar
3 votes
0 answers
244 views

I have a Typescript decorator that traces method calls. The decorator can be applied at both the class level and method level. I want to disable the decorator in production. Is this code the most ...
Learner's user avatar
  • 131
1 vote
2 answers
138 views

This function allows me to write this code ...
Ari Lotter's user avatar