In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Jython, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Jython application example 1, echo, echo.py!
Try an example with some added functionality, which is a Mini Program that allows you to echo command-line arguments. For the program echo, use the following code (which is in echo.py):
Listing 1. A sample program that echoes command line parameters
Import sys count = 1 for arg in sys.argv [1:]: print 'Argument% iTunes% s'% (count, arg) count + = 1
Again, this complete Jython program reveals some key features of Jython. The thing to note is that each line is a statement-- you don't need a semicolon (;) to end those lines-- and the variable is not declared.
You should also note that command-line arguments are accessed through the built-in symbol sys.argv, which is a series of strings. Sys is a standard module that contains useful values and functions, and there are many other standard modules available. * the argv element (0) is the program name (that is, echo.py). To skip it, take the fragment of the argv list, actually start at one (1), and then iterate until the list is *.
Loops are implemented through for statement groups. The body of the for statement is the lines indented after for. The print statement demonstrates the use of the string formatting capabilities of Jython (similar to the new print features of printf of Cpica Clipper + and Java 1.5).
The above code can also be written more accurately in the following form:
From sys import argv for i in range (1, len (argv)): print 'Argument% iTunes% s'% (I, argv [I])
In this example, range is used to index the sys.argv list. Because the argv variable is imported directly, there is no need to qualify. Note that the length of the argv list is obtained through the len function rather than as a method, which is a use of Jython and is common in many languages. There are many other functions available.
Jython application example 2, using factorial.py for factorial!
We use the cached factorial calculator factorial.py as an exercise for class definition, as shown in listing 2.
Listing 2. Cache factorial calculator
Class Factorial:''A factorial calculator''seen = {} # cache of prior calculated values def _ init__ (self, value): self.__value = value def compute (self, value=None):' 'calculate the result' if value is None: value= self.__value # default if value < 0: # bad arg! Raise ValueError 'arg < 0' elif value < 2: # base case return 1L # insure long integers are used else: # need to calculate if not value in Factorial.seen.keys (): # not done before? # calculate this value and cache it Factorial.seen [value] = Value *\ Factorial (value-1). Compute () return Factorial.seen [value] # get value from the cache
You can see many new features of Jython here. First, introduce comments with a clear (#) logo. A class or method can have a documentation comment string (which can be processed using a tool similar to JavaDoc) as its * * line. Unlike assignment, statements are introduced with keywords. Class and method bodies are not enclosed in delimiters, but are represented by indented text.
Also, classes are declared with class statements. The def statement introduces the method. Class properties are created with assignments in the class, while instance properties are created with assignments in the constructor method _ _ init__. The seen variable is a dictionary. An instance is created with a class object as a function call. Method is called with the dot (.) operator.
You will also notice that the self variable is the receiver of the method (like this in the Java language), where all references to instance properties or methods of the class must be qualified with self. Class variables (as in the Java language) are qualified by the class name.
A function can have default parameters, as shown in the value argument of the compute function. If no parameter value is given, the _ _ value instance property is used.
Now, it's obvious that Jython supports all the features of the Java language, but sometimes a different syntax is used. For example, the raise statement of Jython is the same as the throw statement of the Java language.
Test factorial.py
You can test the Factorial class with the following code, which can be found in the file factorial.py (see Resources):
If _ _ name__ = = "_ main__": from sys import argv if len (argv) = = 1: vals = range (10) elif len (argv) = = 2: vals = range (int (argv [1])) elif len (argv) = 3: vals = range (int (argv [1]), int (argv [2])) else: print "Incorrect range" Vals = () for i in vals: print "Factorial (% I) =% I"% (I, Factorial (I). Compute ()) print "Cache:", Factorial.seen
In Jython, you can combine class definitions with test cases. The above if _ _ name__... The test causes the test case code to run only when the file is run as a command. You can also import this file with another file to reuse the Factorial class without having to run the test case. The test case consists of several simple command parameter processing, followed by a loop that calculates the factorial of the specified value, if any. * print out the cached value. The jython factorial.py 5 10 command produces the following output:
Factorial (5) = 120 Factorial (6) = 720 Factorial (7) = 5040 Factorial (8) = 40320 Factorial (9) = 362880 Cache: {9: 362880L, 8: 40320L, 7: 5040L, 6: 720L, 5: 120L, 4: 24L, 3: 6L, 2: 2L}
You may also notice that in the above example, you can use the Jython long (# L) type to calculate integers of variable length.
The above is all the contents of this article "how to use Jython". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.