• 0

Python code generation


Question

I'm kind of a newbie in Python and I'd like to know if the following is possible. All of the following happens at run-time. The application's presentation is basically a Python interpreter like the IDLE, where the user types Python code, hits enter and sees the results immediately. The difference is that this interpreter can generate new types on the fly, given a type definition is some arbitrary format (xml for instance). When it receives that type definition, it generates a new python class based on that type definition, with the same methods and fields etc. It also generates some extra code in the methods of that new type. The new type is then available to the user on the console so he can instantiate it and use it.

Basic use case :

User types "load type "MyType.xml"

MyType.xml :

<class>
	<name>MyType</name>
	<fields>
		<field>
			<type>int</type>
			<name>myInt</name>
   .etc

MyType is now a python class loaded in the interpreter.

User types "mt = MyType()", uses mt's fields, methods, etc.

So basically this requires some metaprogramming ability and I wonder if that's possible in Python? If so, using what libraries/types? I've browsed the documentation and I'm not really sure. I understand that I can create a new run-time type using the function type, but I feel like that's insufficient given I also need to be able to plug some more code in there, and all that has to happen automatically, the user just specifies where to find the type definition and it should be available to him.

Thanks for any help.

Link to comment
https://www.neowin.net/forum/topic/775226-python-code-generation/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I understand that my original question is a bit confusing, so I'll try to specify.

Consider the following python code :

def MyMethod(self):
	print "Hello!"
C = type("AClass", (object,), {})
C.method = MyMethod
c = C()
c.method()

This ouputs:

Hello!

What we're doing here is creating a new type at run-time, "AClass". Actually the code name for that type here is C, but we could have named it AClass to the same effect. We are also able to bind a method to that type. When we instantiate a "C", we are then able to call method() on it.

However, this lacks a lot of flexibility. Notice that the method MyMethod is hard-coded. I want to be able to generate a method at run-time based on an arbitrary signature, for example one read from a text file, described in C or XML or whatever.

One solution I'm thinking about is to generate new Python code at run-time as a text file, and then import the file as a module, although I'm not sure about the syntaxical details. Is there a way I can generate a custom type with custom methods at run-time, based on a type and method definitions?

I hope that makes it more clear.

Thanks.

  • 0

In case it's of interest to anyone reading this topic, I found a solution and it is pretty simple :

>>> method1 = '''
def method1(self):
	print "hello from method1 !"
'''
>>> method2 = '''
def method2(self):
	print "hello from method2 !"
'''
>>> classdict = {}
>>> exec method1 in classdict
>>> exec method2 in classdict
>>> MyClass = type("MyClass", (object,), classdict)
>>> myObject = MyClass()
>>> myObject.method1()
hello from method1 !
>>> myObject.method2()
hello from method2 !

We create classes and methods from strings, so they can be defined at run-time. For instance the user can input his own type definition as any format, we just have to convert it to a string of equivalent python code, and then we can generate the python type on-the-fly.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That would be nice, if excel could refresh pivot tables without bugs. Frequently you refresh a pivot and all parameters get lost. Now this will happen automatically. Bravo MS!
    • If one could ever put all the crap of the world in one box, that would be Microsoft.
    • The most corrupt and vile CEO any company ever had ! The only company that makes an insane amount of money decade after decade but doesn't deliver on even barely passable quality to customers, screws over partners and treats employees like trash
    • PSA: Some other versions of Windows are losing support on October 14 too by Usama Jawad All of us here at Neowin likely know pretty well by now that Windows 10 is reaching end of support on October 14, 2025. You can extend support through paid and "free" means, but if you don't, you won't get any more security or feature updates following the aforementioned date. We also highlighted that the Long-Term Servicing Channel (LTSC) version Windows 10 22H2 is also reaching end of support on that date. Now, Microsoft has reminded customers that yet another variant of Windows is reaching end of life on October 14, 2025 too. On the Windows Release Health dashboard, Microsoft has published a reminder that the Enterprise, Education, and IoT SKUs of Windows 11, version 22H2 will hit end of support on October 14 as well. It is important to keep in mind that the Home and Pro variants of Windows 11, version 22H2 already reached end of life on October 8, 2024, and the extra year of lease on life will end for other SKUs within a few months too. Windows 11, version 22H2 for IoT, Enterprise, and Education was released on September 20, 2022, which means that they would have received just over three years of support by the time they "die", compared to the regular two years for Home and Pro. Customers on any of these versions should consider upgrading to version 23H2 or 24H2 as soon as possible, which have end of support dates of November 10, 2026 and October 12, 2027, respectively. Staying on a supported version of Windows is crucial as that allows you to receive regular security updates on your machine. If you're an IT admin, you should immediately begin planning a migration to a supported version of the operating system, and if you're an employee or someone using these versions of Windows in some other scenario, go to Settings > System > About and check out Windows Specifications > Version.
  • Recent Achievements

    • Dedicated
      Profit earned a badge
      Dedicated
    • One Month Later
      hhgygy earned a badge
      One Month Later
    • Week One Done
      hhgygy earned a badge
      Week One Done
    • One Year In
      NIKI77 earned a badge
      One Year In
    • Week One Done
      artistro08 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      632
    2. 2
      ATLien_0
      237
    3. 3
      Xenon
      167
    4. 4
      neufuse
      146
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!