英语原文共 57 页,剩余内容已隐藏,支付完成后下载完整资料
毕业设计(论文)
外文资料翻译
说明:
封面之后放英文原文;英文原文之后放译文。
原文:
Chapter 4
Jelf Implementation
We have irnplemented Jelf as a web framework in Python, extending the Django web frarnework [1] on top of a standard SQL database. Our code is available at https://github.com/jeanqasaur/jeeves.We first describe the Python Jeeves implementation and then describe how we use this to implernent, the Jelf database layer.
4.1 Python Jeeves Implementation
Our Python embedding of Jeeves consists of 1) the definition of a data type for faceted objects, 2) overloading Python operators to support evaluation of faceted objects, and 3) a source transformation to handle Python functionality that cannot be overloaded (e.g., assignments). In Python Jeeves, the programmer can write code that looks like Python code but, operates according to the Jeeves semantics simply by importing the JeevesLib library and annotating classes and functions with @jeeves.
4.1.1 Faceted Data
Python represents data as objects, so embedding Jeeves in Python involves figuring out how to represent and execute with faceted objects. The problem of executing faceted data has two parts: marking data as faceted and perforrning the faceted execution. Our Jeeves implementation defines a Facet class which stores a label and two child values. A Jeeves faceted value is represented as a tree of Facet objects whose leaves are the possible facets.
In Python, an object is effectively a dictionary mapping field names to values. During faceted execution, an objects fields might be faceted values, either faceted primitive values like ints or bools or faceted references to other objects. In some cases, a field may even exist only in some execution paths. In this case, we use a special object Unassigned() to mark that the field does not actually exist. For example, a field might have the faceted value (x ? 0 : Unassigned()) . This means the field has value 0 under x = high and does not exist under x = low.
Any code run with faceted objects needs to support the faceted execution. The programmer uses the @jeeves decorator which performs a source code transformation, adding support for the faceted values. All methods of all objects that the programmer uses need to be facet-aware. Naturally, instances of any Jelf model are facet-aware.A prograrnmer-defined object can be made facet-aware with the @jeeves decorator. Unfortunately, it is not possible to use objects from arbitrary libraries without working to make them support Jeeves. To do so, the programmer needs to modify the source code, adding @jeeves annotations. Of course, the code involved would have to be within the Python subset that Jeeves supports.
4.1.2 Python Source Transformation
The @jeeves macro performs dynamic source transformation to handle the Python constructs that we cannot handle with overloading.
First of all, we need the faceted values and objects to evaluate correctly under all Python operators. For example, a b needs to evaluate correctly when either a or b is a faceted value. For many operators such as , this can be done by implementing rnethods such as _add_ and _radd _.If one of a or b is a faceted value, evaluation of a b will call special code to handle the faceted semantics. However, this approach does not generalize to operators like in or and, which Python does not allow us to overload.Therefore, the source transformation transforms expressions like a and b into calls to Jeeves library functions, like jand(lambda : a, lambda : b). (It is important to lazily evaluate b with the lambda, due to pythons short-circuiting behavior with the and operator..
Conditionals (including the inline if-else 'ternary operator' need special support. For example, consider an if-statement like the one in Figure 4-1 (a). Suppose cond is a faceted value, say (x ? True : False). Then, the resulting value of a should depend on the label x. Somehow. we need to change the semantics of the if-statement so that it executes the body with a path condition, specifying which labels should be assumed to be high or low for the current execution. In this example, it should evaluate a = o with the path condition = high and a = 1 with the path condition = low. We replace if-statements with the Jeeves library function jif, as in Figure 4-1 (a), which calls the if-body and the else-body with the appropriate path-conditions. We handle for-loops in a similar way, as shown in Figure 4-1 (b).
The semantics of the assignment operator need to be changed because an assignment needs to take into account the path conditions as well. Note that we can overload assignment to object fields by implementing an objects _setattr_ method. However, assignments to local variables are more complicated, since ordinary, local variable assignment cannot be overloaded. Therefore, we simply replace a functions local scope with a Namespace object, which can implement the _setattr_ method. This is shown in Figure 4-1 (c). Our source transformation determines to which scope each local variable belongs and makes it an attribute of the corresponding Namespace.
To implement this source transformation, we use the library MacroPy, which enables source transformation macros. MacroPy allows us to define the @jeeves source transformation. When a source file is imported, MacroPys import hook searches the file for the @jeeves decorator and applies our source transformation. MacroPy decorators like our @jeeves are therefore not traditional Python decorators, which execute at runtime rather than import time. Our transformation supports a subse
剩余内容已隐藏,支付完成后下载完整资料
资料编号:[254055],资料为PDF文档或Word文档,PDF文档可免费转换为Word
以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。