Python's exec Functions: Execute Dynamically Generated Code

 

 

# encoding: utf-8
# 版權所有 2024 ©塗聚文有限公司
# 許可資訊查看:言語成了邀功的功臣,還需要行爲每日來值班嗎?
# 描述: 主、子表單 窗體傳值  Parent-child form operations
# Author    : geovindu,Geovin Du 塗聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# Datetime  : 2024/10/27 13:09
# User      : geovindu
# Product   : PyCharm
# Project   : IctGame
# File      : ui/main.py
# explain   : 學習
# Python's exec Functions: Execute Dynamically Generated Code  

  
import sys
import os
import io
import re
from math import *
import asyncio


class AsyncMange(object):
    """
    
    """
    def __init__(self):
        self.__xNmae=None
    
    @property
    def xName(self):
        """
        
        :return: 
        """
        return self.__xNmae
    
    @xName.setter
    def xNmae(self,xname):
        """
        
        :param xname: 
        :return: 
        """
        self.__xNmae=xname
    
    async def Add(self):
        """
        
        :return: 
        """
        print('add',self.xName)
        pass
    
    async  def Edit(self):
        """
        
        :return: 
        """
        self.__xNmae=44
        print('edit',self.xName)
        pass
    
    async def __aenter__(self):
        """
        
        :return: 
        """
        return self
    
    async def __aexit__(self,ty,val,tb):
        """
        
        :param exc_type: 
        :param exc_val: 
        :param exc_tb: 
        :return: 
        """
        pass
async def main():
    """
    
    :return: 
    """
    async with AsyncMange() as m:
        m.xNmae=23
        await m.Add()
        await m.Edit()
        
asyncio.run(main())

numbers = [2, 3, 7, 4, 8]
exec("result = sum( number**2 for number in numbers if number % 2 == 0)")
print(result)
exec("print(dir())", {})
exec('print(fact(5))', {'fact': factorial})
exec("name = input('Your name: ')\nprint(f'Hello, {name}!')")

stringInput = """
def sumSquares(numbers):
    return sum(number**2 for number in numbers if number % 2 == 0)

print(sumSquares(numbers))
 """
compiled_code = compile(stringInput, "<string>", "exec")
exec(compiled_code)
numbers = [12, 23, 37, 44, 58]
exec(compiled_code)
sumSquares(numbers)
print(sumSquares(numbers))

with open("geovindu.py", mode="r", encoding="utf-8") as hello:
     code = hello.read()
exec(code)

  

posted @ 2024-10-27 14:55  ®Geovin Du Dream Park™  阅读(5)  评论(0编辑  收藏  举报