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
1 answer
91 views

AutoBind TypeScript decorator

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. <...
user avatar
8 votes
1 answer
324 views

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

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 ...
user avatar
  • 263
0 votes
0 answers
53 views

Class to save the parameters of every function call

I wrote a function saving class which basically saves data of every function that's being called ...
user avatar
1 vote
0 answers
43 views

Higher-order function to do throttling, written two ways

I have used below method for throttling which is a Higher Order Function (HOF), but find some difficulties to choose which one is better way to write it. Below are both methods. return of the ...
user avatar
  • 141
3 votes
1 answer
72 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 &...
user avatar
2 votes
1 answer
105 views

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

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....
user avatar
1 vote
0 answers
50 views

Metaprogamming elixir for module proxy

I'm working on my first real Elixir application, after dabbling for a few years. In my elixir/phoenix application I have to work with data over an external REST API. For my purposes, I can tolerate ...
user avatar
  • 159
1 vote
1 answer
53 views

Ruby: Database Authentication Module

I have created a module that validates the credentials against different databases. ...
user avatar
3 votes
0 answers
50 views

Yet another reflection library

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 ...
user avatar
6 votes
2 answers
340 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 ...
user avatar
1 vote
0 answers
45 views

Simplified Bash getopts flow

I've always found the boilerplate needed to use getopts correctly fairly fiddly and verbose, and was motivated to try to abstract it away. I wanted to try ...
user avatar
  • 365
4 votes
1 answer
98 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 ...
user avatar
  • 125
3 votes
1 answer
68 views

config files for different development environments (Production, Dev, Testing)

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 ...
user avatar
  • 270
2 votes
0 answers
80 views

Compile time logical operations for web framework in C++

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

clean approach to decorator that takes optional keyword arguments [closed]

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: ...
user avatar
4 votes
0 answers
61 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 (...
user avatar
  • 61
1 vote
0 answers
68 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 ...
user avatar
8 votes
1 answer
350 views

Detecting existence of a class member

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: ...
user avatar
  • 115
7 votes
2 answers
117 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 ...
user avatar
  • 9,466
3 votes
1 answer
5k 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 ...
user avatar
3 votes
1 answer
743 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 : ...
user avatar
1 vote
1 answer
577 views

Lazy evaluation function decorator

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 ...
user avatar
  • 233
4 votes
0 answers
260 views

Python breakpoints in Vim (Vimscript)

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 ...
user avatar
  • 261
2 votes
1 answer
102 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? ...
user avatar
11 votes
3 answers
430 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 ...
user avatar
4 votes
1 answer
79 views

Deserializing an XML Object without Reflection / Meta-Programming

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 ...
user avatar
1 vote
0 answers
59 views

Python script to add unittest decorators to source code files

This is a simple Python script to add @unittest.skipUnless() decorators in a specified format above every function starting with ...
user avatar
3 votes
0 answers
67 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 ...
user avatar
  • 370
3 votes
0 answers
112 views

Cumulative profiler for a function

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 ...
user avatar
  • 270
4 votes
1 answer
58 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 ...
user avatar
1 vote
0 answers
166 views

@bound decorator for TypeScript

I'm working on an angular codebase that very frequently uses constructs like: <c-whatever [action]='doSomething.bind(this)'> to pass reference to the ...
user avatar
7 votes
1 answer
220 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 ...
user avatar
3 votes
1 answer
472 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 ...
user avatar
  • 361
12 votes
1 answer
408 views

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

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 ...
user avatar
  • 223
3 votes
1 answer
238 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. ...
user avatar
1 vote
2 answers
492 views

Encryption scripts using the module cryptography

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
64 views

Two java classes with different annotation values only [closed]

Consider two classes: First ...
user avatar
  • 133
12 votes
1 answer
178 views

Generic framework to handle parameterized commands

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, ...
user avatar
  • 7,673
6 votes
2 answers
292 views

Class Decorator for Verifying Method-Level Permissions

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 ...
user avatar
1 vote
1 answer
105 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. ...
user avatar
  • 297
7 votes
1 answer
77 views

Basic profiler - Python

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 ...
user avatar
1 vote
0 answers
710 views

Sealing class attributes in Python

Over two years ago, someone asked a question on Stackoverflow about how to seal classes and attributes in Python after coming from a background in C#. The best answer to the question is that Pythonic ...
user avatar
5 votes
0 answers
779 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 ...
user avatar
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 ...
user avatar
9 votes
2 answers
2k views

Python Export Decorator

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

Scala code that loops through Java annotations decorating Scala classes

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, ...
user avatar
  • 131
3 votes
1 answer
937 views

Transforming stored procedures using Python regular expressions

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 ...
user avatar
1 vote
1 answer
365 views

Method arguments set object attributes

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 ...
user avatar
  • 113
1 vote
0 answers
83 views

Dependency resolving functions

Written in Python 3.6 and I must admit, this might be too far... Tell me what you think of this code. There must be a better way... It's (another decorator...) that allows you to compose functions ...
user avatar
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 ...
user avatar