• 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

    • Windows is a scam for many decades, it's good if some people are realizing it now
    • Latest Patch Tuesday has broken DHCP Server in all Windows Server editions by Usama Jawad Microsoft released Patch Tuesday updates for Windows and Windows Server releases roughly a week ago, on June 10. In the latter, the release contained new functions for Narrator scan mode, along with tons of fixes for File Explorer, GDI+, Hyper-V, and more. At that time, Microsoft noted a known issue for Noto fonts, where Chinese, Japanese, and Korean text appears blurry or unclear under certain conditions. Now, it has highlighted another known issue following user reports. For the past couple of days, users on Reddit have been reporting issues with the DHCP service after applying the latest Patch Tuesday update. Apparently, rebooting the system fixes the issue, which can be understandably annoying. Now, Microsoft has updated the documentation for the June 10 release notes across Windows Server 2016, 2019, 2022, and 2025, to mention that the DHCP Server service may "intermittently" stop working after installing Patch Tuesday's update, which will impact IP renewal processes on client machines. Redmond has assured customers that it is working on a fix, which will likely be rolled out within the next few days. Of course, this problem is rather frustrating for IT admins, and that annoyance is reflected in the Reddit thread where customers are blaming Microsoft's software quality assurance practices. The fact that it's currently unclear when a fix will be rolled out may further add to customer grievances. For those unaware, the DHCP Server service in Windows is responsible for network management such as allocation and configuration of IP addresses in a centralized environment with robust logging and monitoring mechanisms. And for those keeping track, this is not the only Windows Server issue that has cropped up in recent memory. Last month, Microsoft pushed an out-of-band (OOB) hotfix to patch a Hyper-V bug that was plaguing the platform. Of course, one can argue that problems like these are happening because of incomplete testing, but it is also important to remember that Windows is running on hundreds of millions of devices all over the globe, so comprehensive test case coverage is very difficult, if not impossible.
    • And still, no Windows Hello support for protected "tabs" in the "old"/Win32 OneNote app, a feature that was available for a long time in the UWP version. Please, Microsoft, get back to feature parity...
    • Like jupe, for me, the most exciting news from Windows for a few weeks ( / months ?) now is the return of the clock in the taskbar calendar. I filed a feedback hub post so long ago about this. Finally!
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      617
    2. 2
      ATLien_0
      281
    3. 3
      +FloatingFatMan
      174
    4. 4
      Michael Scrip
      153
    5. 5
      Steven P.
      124
  • Tell a friend

    Love Neowin? Tell a friend!