metaprogramming

Edit attribute in script string with AST

Edit attribute in script string with AST Question: I’m unfamiliar with the AST module and would appreciate any insight. If, for example, I have a string that contains a valid python script such as import sys #Just any module class SomeClass: def __init__(self): self.x = 10 self.b = 15 def a_func(self): print(self.x) I would like …

Total answers: 1

Can a line of Python code know its indentation nesting level?

Can a line of Python code know its indentation nesting level? Question: From something like this: print(get_indentation_level()) print(get_indentation_level()) print(get_indentation_level()) I would like to get something like this: 1 2 3 Can the code read itself in this way? All I want is the output from the more nested parts of the code to be more …

Total answers: 5

How can I dynamically create class methods for a class in python

How can I dynamically create class methods for a class in python Question: If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, ‘func’, classmethod(self._func)) if __name__ == "__main__": a.func I receive …

Total answers: 6

How Pony (ORM) does its tricks?

How Pony (ORM) does its tricks? Question: Pony ORM does the nice trick of converting a generator expression into SQL. Example: >>> select(p for p in Person if p.name.startswith(‘Paul’)) .order_by(Person.name)[:2] SELECT "p"."id", "p"."name", "p"."age" FROM "Person" "p" WHERE "p"."name" LIKE "Paul%" ORDER BY "p"."name" LIMIT 2 [Person[3], Person[1]] >>> I know Python has wonderful introspection …

Total answers: 1

How to pass arguments to the metaclass from the class definition?

How to pass arguments to the metaclass from the class definition? Question: I’m trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object. I’ve read this post, which is awesome, but doesn’t quite answer the question. at the moment I am …

Total answers: 2

Pickling dynamically generated classes?

Pickling dynamically generated classes? Question: I’m using type() to dynamically generate classes that will ultimately be pickled. The problem is that the un-pickling process needs the definition of the class in order to re-construct the object that has been pickled. This is where I’m stuck. I don’t know how to somehow provide the unpickler a …

Total answers: 6

Overriding special methods on an instance

Overriding special methods on an instance Question: I hope someone can answer this that has a good deep understanding of Python 🙂 Consider the following code: >>> class A(object): … pass … >>> def __repr__(self): … return “A” … >>> from types import MethodType >>> a = A() >>> a <__main__.A object at 0x00AC6990> >>> …

Total answers: 5

Valid characters in a python class name

Valid characters in a python class name Question: I’m dynamically creating python classes, and I know not all characters are valid in this context. Is there a method somewhere in the class library that I can use to sanitize a random text string, so that I can use it as a class name? Either that …

Total answers: 4

Python equivalent of Ruby's 'method_missing'

Python equivalent of Ruby's 'method_missing' Question: What is Python’s equivalent of Ruby’s method_missing method? I tried using __getattr__ but this hook applies to fields too. I only want to intercept the method invocations. What is the Python way to do it? Asked By: missingfaktor || Source Answers: Python doesn’t distinguish between methods and attributes (a.k.a. …

Total answers: 4