Python代写:CS313 Metaprogramming

练习Metaprogramming相关编程。

Metaprogramming

Goals

  • Introduction to metaprogramming.

Metaprogramming

Metaprogramming is the practice of writing a program which produces as output another program that can be run.

In this assignment we’ll learn metaprogramming and how we can apply metaprogramming to make Python sorting programs that do not use any loops. This isn’t particularly useful in Python, but it is useful when designing computer hardware, or when programming GPUs. It will also allow us to take a closer look at the way two sorting algorithms work: bubble sort and bitonic sort.

Task 1 - A Python Program to Write Python Programs

Make a python program named a3.py. This is the only file you will turn in! In your a3.py write a function named write_py which takes 3 arguments:

  1. A string name.
  2. A list of parameter names as strings, parameters.
  3. A string statements containing a list of Python statements as strings.

write_py should open and write out a file named based on name but with .py at the end. The file should have one function, whose name is name that contains the statements listed in code indented inside of it and nothing else.

Example 1

For example, write_py(“like”, [“fruit”], [“print(‘I like’, fruit)”]) should create a file like.py which contains only the following:

1
2
def like(fruit):
print('I like', fruit)

After running doesn’t contain anything else at all.

Your write_py should make sure it doesn’t overwrite any of your a3.py code, for example you may want the first line of write_py to be:

1
assert name != 'a3'

Your write_py should use the with…as block to open the output file.
Make sure you open the output file for writing, not appending.

Example 2

Calling write_py:

1
write_py("add", ["a", "b"], ["r = a + b", "return r"])

should produce a file named add.py which contains only the following:

1
2
3
def add(a, b):
r = a + b
return r

Task 2: Doing your own testing…

You can write your own testing in your main(). For this you will need the following code: (This code is also available in check1.py)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from importlib import invalidate_caches
from importlib import import_module

def load_function(name):
"""
load_function - imports a module recently created by name
and returns the function of the same name from inside of it
name - a string name of the module (not including .py at the end)

# invalidate_caches is necessary to import any files created after this
file started!
"""

invalidate_caches()
print(f" Attempting to import {name}...")
module = import_module(name)
print(f" Imported!")
assert hasattr(module, name), f"{name} is missing from {name}.py"
function = getattr(module, name)
assert callable(function), f"{name} in {name}.py isn't a function"
assert type(function) is type(
load_function
), f"{name} in {name}.py isn't a function"
return function

You should write a main() that calls write_py followed by load_function to test your write_py. Have write_py write a different function from any of the examples here. Then call load_function to load it and assign the result to a variable. Then test to make sure you can call it by that variable with some parameters and that it will return the correct result. For example,

1
2
3
4
def main():
write_py("add", ["a", "b"], ["r = a + b", "return r"])
add = load_function("add")
assert add(1, 2) == 3

But you should come up with your own, don’t just use the example above! Remember you need to protect the call to main with the if __name__ == ‘__main__‘: thing to prevent main from running when you run check1.py.