[Learn Python with me] python with statement advanced understanding, pythonstatement
Because a previous project always needs to open the file, and then use pickle. load (file) to process it... Finally, close the file, so it is a bit complicated and the code is not concise. Therefore, seek a solution from python with statement.
I have read an article on the Internet: http://effbot.org/zone/python-with-statement.htmwhich introduces with. I have understood it by referring to the example.
If there are such code segments, you can use the following methods to improve them:
Code segment:
Set thing up
Try:
Do something
Except t:
Handle exception
Finally:
Tear thing down
Case 1:
If you want to implement this function now, you can open the file, read data from the file, print the data to the terminal, and then close the file.
Logically speaking, you can extract "print to terminal" as the data processing part and use it as a function independently. Other files such as opening and closing should be the same.
File Name: for_test.txt
Method 1:
Use functions to extract public parts.
Python code
- #! /Usr/bin/env python
- From _ future _ import with_statement
- Filename = 'for_test.txt'
- Def output (content ):
- Print content
- # Functio solution
- Def controlled_execution (func ):
- # Prepare thing
- F = None
- Try:
- # Set thing up
- F = open (filename, 'R ')
- Content = f. read ()
- If not callable (func ):
- Return
- # Deal with thing
- Func (content)
- Handle t IOError, e:
- Print 'error % s' % str (e)
- Finally:
- If f:
- # Tear thing down
- F. close ()
- Def test ():
- Controlled_execution (output)
- Test ()
Method 2:
Use yield to implement a generator that generates only one item. Loop through for-in.
The code snippet is as follows:
Python code
- # Yield solution
- Def controlled_execution ():
- F = None
- Try:
- F = open (filename, 'R ')
- Thing = f. read ()
- # For thing in f:
- Yield thing
- Handle t IOError, e:
- Print 'error % s' % str (e)
- Finally:
- If f:
- F. close ()
- Def test2 ():
- For content in controlled_execution ():
- Output (content)
Method 3:
Add the with implementation as a class.
The code snippet is as follows:
Python code
- # Class solution
- Class controlled_execution (object ):
- Def _ init _ (self ):
- Self. f = None
- Def _ enter _ (self ):
- Try:
- F = open (filename, 'R ')
- Content = f. read ()
- Return content
- Handle t IOError, e:
- Print 'error % s' % str (e)
- # Return None
- Def _ exit _ (self, type, value, traceback ):
- If self. f:
- Print 'Type: % s, value: % s, traceback: % s' % \
- (Str (type), str (value), str (traceback ))
- Self. f. close ()
- Def test3 ():
- With controlled_execution () as thing:
- If thing:
- Output (thing)
Method 4:
Use. However, exception handle is not supported.
Python code
- Def test4 ():
- With open (filename, 'R') as f:
- Output (f. read ())
- Print f. read ()
The last print statement is used to test whether f has been disabled.
Finally, the purpose of writing this article is to be stimulated by a single sentence: "Use good language features, do not use those bad features "! Python has a lot of elegant features. It's a long way to go, so I will go up and down...
How can I learn Python directly in this case?
C is not a high-level language, not an object oriented, but also a static typed language. python is a high-level language, an object oriented, or a dynamic typed language. The two are quite different in syntax. python is much simpler. However, the syntax in programming is very secondary. If you learn python, instead of C, you can focus on your thoughts, it's not a waste of remembering the cumbersome syntax and having no creativity.
How can I use python to learn the web path of gis?
For me, I have some experience. For gis, python can be used to write some gis tools. These tools are generally used together with the arcgis toolbox. for gis alone, there are not many things in python, but there is no need to associate it with learning webgis in three dimensions. Therefore, if you want to learn more things, you can consider python. If you have limited energy, you can do whatever you want, my personal feelings.