Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

2
votes
1answer
64 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
3answers
93 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 ...
4
votes
1answer
55 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 ...
1
vote
0answers
26 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 ...
3
votes
0answers
49 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 ...
3
votes
0answers
44 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 ...
4
votes
1answer
51 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 ...
1
vote
0answers
27 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 ...
5
votes
1answer
84 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 ...
2
votes
0answers
182 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 ...
6
votes
0answers
93 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 ...
3
votes
1answer
140 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. ...
3
votes
1answer
56 views
12
votes
1answer
153 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, ...
6
votes
2answers
138 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 ...
1
vote
1answer
82 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. ...
7
votes
1answer
60 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 ...
1
vote
0answers
75 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 ...
5
votes
0answers
372 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
0answers
668 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 ...
8
votes
2answers
621 views

Python Export Decorator

In Python, we use __all__ to explicitly denote the public API of a module or package. That looks like this: ...
2
votes
0answers
64 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, ...
3
votes
1answer
171 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 ...
1
vote
1answer
151 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 ...
1
vote
0answers
65 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 ...
6
votes
1answer
361 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 ...
10
votes
2answers
2k views

Mutable Named Tuple - or - Slotted Data Structure

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 ...
4
votes
0answers
421 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 ...
11
votes
1answer
10k views

Decorator to measure execution time of a function

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 ...
2
votes
1answer
84 views

JavaScript Python Arrays

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 ...
3
votes
0answers
70 views

Optionally disabling a call-tracing decorator in Typescript

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 ...
1
vote
2answers
76 views

A shim to make make Node GRPC bindings more JavaScript-y

This function allows me to write this code ...
7
votes
1answer
89 views

Mass and length calculator using Perl 6 custom operator

I am studying chemistry in the university, and then I try to write all the things in the textbook with Perl6 or Perl, like balancing the chemical formula or other process! Then I encountered the ...
12
votes
1answer
316 views

Field Can Be Made Readonly

Here is my Field Can Be Made Readonly analyzer and quick fix. I would appreciate tips on how to improve my code, and on missed test cases. First, the rules it works with: It will only fire on ...
2
votes
1answer
70 views

Gem to interact with a third party API

I am writing a gem for users to interact with a third party api. I want users to have easy and natural access to objects and properties delivered by the JSON payload. The collection of entities ...
2
votes
1answer
94 views

Threaded and asyncio API library

In Python I am trying to create an API for a connected device. I want to be available for both threaded (using request) and async applications (using aiohttp). What I've come up with is wrapping the ...
6
votes
0answers
1k views

Improving the Flask cache decorator

In my rumblings developing flask applications I found the caching decorator. The decorator is a short and well-written piece of code, but I feel like it misses the few points bellow: It only caches ...
4
votes
1answer
1k 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 ...
0
votes
1answer
144 views

Generic Int conversion to scalar

I'm new to Scala and I'm trying to write a generic function to convert from Int to any scalar. For example: ...
7
votes
1answer
627 views

Removing XML Doc Comment Nodes

I recently wrote a code fix to handle many of the C# and VB.NET compiler diagnostics that was merged into Roslyn. Because it handles both, I implemented it as an abstract class with only the sections ...
2
votes
2answers
342 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 ...
5
votes
3answers
248 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 ...
2
votes
1answer
3k views

Python decorator for retrying w/exponential backoff

This is my first decorator in Python! I found some of it on the internet but have tweaked it to our needs. Here is the couple concerns of mine: Multiple python version compatibility Is grabbing the ...
3
votes
1answer
441 views

Lombokython - Automatic __eq__, __hash__, __repr__

I recently decided to code some in Python after coding in Java using lombok for quite some time. However, I got bit real hard when I forgot to implement __eq__, ...
2
votes
2answers
355 views

Classes for reading XML from a file

Basically, all I have to do is read some xml from a file. And I have a working solution, although I am not sure that it is the best way of going about doing things, as I have only recently started ...
4
votes
1answer
69 views

Code Explorer View Models

Continuing the series of Code Explorer posts, here is the collection of view models for the tree nodes: This is the interface for nodes with a declaration. ...
7
votes
1answer
118 views

Code Explorer Commands

Rubberduck's Code Explorer was recently redesigned from scratch: Anything from modern features, such as virtual folders (limitation of the VBE--it doesn't support real folders), to ancient features ...
2
votes
2answers
151 views

Decorator that supplies arguments to functions

I have assignment to generate argument based decorator I wrote like this. ...
2
votes
1answer
46 views

Method to act on parameter or from iterable if not parameters are passed

The scenario is as follows. The API of my program will allow the user to perform some method on a particular element (let's say a tuple from a database). If this parameter is not passed to the ...
2
votes
0answers
52 views

Dynamically create instance variables from a list of classes

I have a situation, where I want to add many classes as instance variables of another class, plus perform some action on each class. The purpose is to have an 'app' with many 'views', to represent <...