首页 > 代码库 > jython学习笔记3

jython学习笔记3

1.os.environ["HOME"] 为什么这句话在我的STS中打印不出东西,还报错

MethodDescription
close()Close file
fileno()Returns integer file descriptor
flush()Used to flush or clear the output buffers and write content to the file
isatty()If the file is an interactive terminal, returns 1
next()This allows the file to be iterated over. Returns the next line in the file. If no line is found, raises StopIteration
read(x)Reads x bytes
readline(x)Reads single line up to x characters, or entire line if x is omitted
readlines(size)Reads all lines in file into a list. If size > 0, reads that number of characters
seek()Moves cursor to a new position in the file
tell()Returns the current position of the cursor
truncate(size)Truncates file’s size. Size defaults to current position unless specified
write(string)Writes a string to the file object
writelines(seq)Writes all strings contained in a sequence with no separator

 

AttributeDescription
closedReturns a boolean to indicate if the file is closed
encodingReturns a string indicating encoding on file
modeReturns the I/O mode for a file(i.e., ‘r’, ‘w’, ‘r+,’rb’, etc.)
nameReturns the name of the file
newlinesReturns the newline representation in the file. This keeps track of the types of newlines encountered while reading the file. Allows for universal newline support.

 在定义类时,Car(object),指定义的是object的一个子类。类的属性可以不用实例化而直接用。self的变量只能用在单一的object中,而类的属性则属于该类的所有实例,也就是说,改变类的可改变变量(例如LIST)会影响到该类的所有实例,改变类的某一个实例中该类的不可改变属性,例如integer,本实例中的该类属性的值改变,其他实例中该类的属性的值不变。

2.jython使用left first depth first

3.这段代码感觉看不懂

from __future__ import with_statement
from contextlib import closing
from pickle import dumps, loads

def write_object(fout, obj):
    data = dumps(obj)
    fout.write("%020d" % len(data))
    fout.write(data)

def read_object(fin):
    length = int(fin.read(20))
    obj = loads(fin.read(length))
    return obj

class Simple(object):
    def __init__(self, value):
        self.value = value
    def __unicode__(self):
        return "Simple[%s]" % self.value

with closing(open(‘simplefile‘,‘wb‘)) as fout:
    for i in range(10):
        obj = Simple(i)
        write_object(fout, obj)

print "Loading objects from disk!"
print ‘=‘ * 20

with closing(open(‘simplefile‘,‘rb‘)) as fin:
    for i in range(10):
        print read_object(fin)
3.chapter6看得有点不太懂。先跳过,等回过头再看一遍

Table 7-1. Exceptions

ExceptionDescription
BaseExceptionThis is the root exception for all others
GeneratorExitRaised by close() method of generators for terminating iteration
KeyboardInterruptRaised by the interrupt key
SystemExitProgram exit
ExceptionRoot for all non-exiting exceptions
StopIterationRaised to stop an iteration action
StandardErrorBase class for all built-in exceptions
ArithmeticErrorBase for all arithmetic exceptions
FloatingPointErrorRaised when a floating-point operation fails
OverflowErrorArithmetic operations that are too large
ZeroDivisionErrorDivision or modulo operation with zero as divisor
AssertionErrorRaised when an assert statement fails
AttributeErrorAttribute reference or assignment failure
EnvironmentErrorAn error occurred outside of Python
IOErrorError in Input/Output operation
OSErrorAn error occurred in the os module
EOFErrorinput() or raw_input() tried to read past the end of a file
ImportErrorImport failed to find module or name
LookupErrorBase class for IndexError and KeyError
IndexErrorA sequence index goes out of range
KeyErrorReferenced a non-existent mapping (dict) key
MemoryErrorMemory exhausted
NameErrorFailure to find a local or global name
UnboundLocalErrorUnassigned local variable is referenced
ReferenceErrorAttempt to access a garbage-collected object
RuntimeErrorObsolete catch-all error
NotImplementedErrorRaised when a feature is not implemented
SyntaxErrorParser encountered a syntax error
IndentationErrorParser encountered an indentation issue
TabErrorIncorrect mixture of tabs and spaces
SystemErrorNon-fatal interpreter error
TypeErrorInappropriate type was passed to an operator or function
ValueErrorArgument error not covered by TypeError or a more precise error
WarningBase for all warnings

 4.

Table 7-2. Python Warning Categories

WarningDescription
WarningRoot warning class
UserWarningA user-defined warning
DeprecationWarningWarns about use of a deprecated feature
SyntaxWarningSyntax issues
RuntimeWarningRuntime issues
FutureWarningWarns that a particular feature will be changing in a future release

Importing the warnings module into your code gives you access to a number of built-in warning functions that can be used. If you’d like to filter a warning and change its behavior then you can do so by creating a filter. Table 7-3 lists functions that come with the warnings module.

Table 7-3. Warning Functions

FunctionDescription
warn(message[, category[, stacklevel]])Issues a warning. Parameters include a message string, the optional category of warning, and the optional stack level that tells which stack frame the warning should originate from, usually either the calling function or the source of the function itself.
warn_explicit(message, category, filename, lineno[, module[, registry]])This offers a more detailed warning message and makes category a mandatory parameter. filename, lineno, and module tell where the warning is located. registry represents all of the current warning filters that are active.
showwarning(message, category, filename, lineno[, file])Gives you the ability to write the warning to a file.
formatwarning(message, category, filename, lineno)Creates a formatted string representing the warning.
simplefilter(action[, category[, lineno[, append]]])Inserts simple entry into the ordered list of warnings filters. Regular expressions are not needed for simplefilter as the filter always matches any message in any module as long as the category and line number match. filterwarnings() described below uses a regular expression to match against warnings.
resetwarnings()Resets all of the warning filters.
filterwarnings(action[, message[, category[, module[, lineno[, append]]]]])This adds an entry into a warning filter list. Warning filters allow you to modify the behavior of a warning. The action in the warning filter can be one from those listed in Table 7-4, message is a regular expression, category is the type of a warning to be issued, module can be a regular expression, lineno is a line number to match against all lines, append specifies whether the filter should be appended to the list of all filters.

Table 7-4. Python Filter Actions

Filter Actions 
‘always’Always print warning message
‘default’Print warning once for each location where warning occurs
‘error’Converts a warning into an exception
‘ignore’Ignores the warning
‘module’Print warning once for each module in which warning occurs
‘once’Print warning only one time

jython学习笔记3