本节目标:
(1) 学习标准库abc, binascii, binhex, builtins, calendar, chunk, collections, formatter,
binascii可以在二进制和ascii中间转换
builtins是不需要加载的嫡系内置函数,已经在前几节仔细看过
日历类calendar,完全是一个小工具
chunk类是用来读取tiff, rmff等类型文件的数据的,是文件的数据块。
集合类collections有许多子类,具体怎么用要仔细研究
批量文件操作,使用fileinput。
fornatter可以设置writer的格式
加载库可以用importlib,但不用也没关系
这些模块只有到必须要用时才会去看它们函数调用的语法,平时一般也用不上。
(1) 学习标准库abc, binascii, binhex, builtins, calendar, chunk, collections, formatter,
fileinput, importlib模块
实现步骤:
abc--抽象基础类可以作为mixin,带着它的功能投入到任何类中间去。
>>> import abc
>>> help(abc)
Help on module abc:
NAME
abc - Abstract Base Classes (ABCs) according to PEP 3119.
CLASSES
builtins.classmethod(builtins.object)
abstractclassmethod
builtins.property(builtins.object)
abstractproperty
builtins.staticmethod(builtins.object)
abstractstaticmethod
builtins.type(builtins.object)
ABCMeta
class ABCMeta(builtins.type)
| Metaclass for defining Abstract Base Classes (ABCs).
|
| Use this metaclass to create an ABC. An ABC can be subclassed
| directly, and then acts as a mix-in class. You can also register
| unrelated concrete classes (even built-in classes) and unrelated
| ABCs as 'virtual subclasses' -- these and their descendants will
| be considered subclasses of the registering ABC by the built-in
| issubclass() function, but the registering ABC won't show up in
| their MRO (Method Resolution Order) nor will method
| implementations defined by the registering ABC be callable (not
| even via super()).
|
| Method resolution order:
| ABCMeta
| builtins.type
| builtins.object
>>> dir(abc)
['ABCMeta', 'WeakSet', '__builtins__', '__cached__', '__doc__', '__file__',
'__initializing__', '__loader__', '__name__', '__package__', 'abstractclassmethod',
'abstractmethod', 'abstractproperty', 'abstractstaticmethod']
binascii可以在二进制和ascii中间转换
>>> import binascii
>>> help(binascii)
Help on built-in module binascii:
NAME
binascii - Conversion between binary data and ASCII
CLASSES
builtins.Exception(builtins.BaseException)
Incomplete
builtins.ValueError(builtins.Exception)
Error
class Error(builtins.ValueError)
| Method resolution order:
| Error
| builtins.ValueError
| builtins.Exception
| builtins.BaseException
| builtins.object
|
>>> dir(binascii)
['Error', 'Incomplete', '__doc__', '__loader__', '__name__', '__package__', 'a2b_base64',
'a2b_hex', 'a2b_hqx', 'a2b_qp', 'a2b_uu', 'b2a_base64', 'b2a_hex', 'b2a_hqx', 'b2a_qp',
'b2a_uu', 'crc32', 'crc_hqx', 'hexlify', 'rlecode_hqx', 'rledecode_hqx', 'unhexlify']
>>> help(binhex)
Help on module binhex:
NAME
binhex - Macintosh binhex compression/decompression.
DESCRIPTION
easy interface:
binhex(inputfilename, outputfilename)
hexbin(inputfilename, outputfilename)
CLASSES
builtins.Exception(builtins.BaseException)
Error
class Error(builtins.Exception)
| Method resolution order:
| Error
| builtins.Exception
| builtins.BaseException
| builtins.object
|
>>> dir(binhex)
['BinHex', 'Error', 'FInfo', 'HexBin', 'LINELEN', 'REASONABLY_LARGE', 'RUNCHAR', '_DID_DATA',
'_DID_HEADER', '_DID_RSRC', '_Hqxcoderengine', '_Hqxdecoderengine', '_Rlecoderengine',
'_Rledecoderengine', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__initializing__', '__loader__', '__name__', '__package__', 'binascii', 'binhex',
'getfileinfo', 'hexbin', 'io', 'openrsrc', 'os', 'struct']
builtins是不需要加载的嫡系内置函数,已经在前几节仔细看过
>>> import builtins
>>> help(builtins)
Help on built-in module builtins:
NAME
builtins - Built-in functions, exceptions, and other objects.
DESCRIPTION
Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.
CLASSES
object
BaseException
Exception
ArithmeticError
FloatingPointError
OverflowError
ZeroDivisionError
AssertionError
AttributeError
BufferError
EOFError
ImportError
LookupError
IndexError
KeyError
MemoryError
NameError
UnboundLocalError
OSError
BlockingIOError
ChildProcessError
ConnectionError
BrokenPipeError
ConnectionAbortedError
ConnectionRefusedError
ConnectionResetError
FileExistsError
FileNotFoundError
InterruptedError
IsADirectoryError
NotADirectoryError
PermissionError
ProcessLookupError
TimeoutError
ReferenceError
RuntimeError
NotImplementedError
StopIteration
SyntaxError
IndentationError
TabError
SystemError
TypeError
ValueError
UnicodeError
UnicodeDecodeError
UnicodeEncodeError
UnicodeTranslateError
Warning
BytesWarning
DeprecationWarning
FutureWarning
ImportWarning
PendingDeprecationWarning
ResourceWarning
RuntimeWarning
SyntaxWarning
UnicodeWarning
UserWarning
GeneratorExit
KeyboardInterrupt
SystemExit
bytearray
bytes
classmethod
complex
dict
enumerate
filter
float
frozenset
int
bool
list
map
memoryview
property
range
reversed
set
slice
staticmethod
str
super
tuple
type
zip
class ArithmeticError(Exception)
| Base class for arithmetic errors.
|
| Method resolution order:
| ArithmeticError
| Exception
| BaseException
| object
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError',
'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError',
'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError',
'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError',
'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning',
'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError',
'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError'
, 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'ReferenceError',
'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError',
'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True',
'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError'
, 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',
'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__',
'__import__', '__loader__', '__name__', '__package__', 'abs', 'all', 'any', 'ascii', 'bin',
'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex',
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec',
'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash',
'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license',
'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord',
'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr',
'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
日历类calendar,完全是一个小工具
>>> import calendar
>>> help(calendar)
Help on module calendar:
NAME
calendar - Calendar printing functions
DESCRIPTION
Note when comparing these calendars to the ones printed by cal(1): By
default, these calendars have Monday as the first day of the week, and
Sunday as the last (the European convention). Use setfirstweekday() to
set the first day of the week (0=Monday, 6=Sunday).
CLASSES
builtins.ValueError(builtins.Exception)
IllegalMonthError
IllegalWeekdayError
class IllegalMonthError(builtins.ValueError)
| # Exceptions raised for bad input
|
| Method resolution order:
| IllegalMonthError
| builtins.ValueError
| builtins.Exception
| builtins.BaseException
| builtins.object
>>> dir(calendar)
['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError',
'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY',
'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD',
'__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__',
'__loader__', '__name__', '__package__', '_colwidth', '_locale', '_localized_day',
'_localized_month', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name',
'different_locale', 'error', 'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays',
'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange',
'prcal', 'prmonth', 'prweek', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday',
'weekheader']
chunk类是用来读取tiff, rmff等类型文件的数据的,是文件的数据块。
>>> import chunk
>>> help(chunk)
Help on module chunk:
NAME
chunk - Simple class to read IFF chunks.
DESCRIPTION
An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
Format)) has the following structure:
+----------------+
| ID (4 bytes) |
+----------------+
| size (4 bytes) |
+----------------+
| data |
| ... |
+----------------+
The ID is a 4-byte string which identifies the type of chunk.
The size field (a 32-bit value, encoded using big-endian byte order)
gives the size of the whole chunk, including the 8-byte header.
Usually an IFF-type file consists of one or more chunks. The proposed
usage of the Chunk class defined here is to instantiate an instance at
the start of each chunk and read from the instance until it reaches
the end, after which a new instance can be instantiated. At the end
of the file, creating a new instance will fail with a EOFError
exception.
Usage:
while True:
try:
chunk = Chunk(file)
except EOFError:
break
chunktype = chunk.getname()
while True:
data = chunk.read(nbytes)
if not data:
pass
# do something with data
The interface is file-like. The implemented methods are:
read, close, seek, tell, isatty.
Extra methods are: skip() (called by close, skips to the end of the chunk),
getname() (returns the name (ID) of the chunk)
The __init__ method has one required argument, a file-like object
(including a chunk instance), and one optional argument, a flag which
specifies whether or not chunks are aligned on 2-byte boundaries. The
default is 1, i.e. aligned.
CLASSES
builtins.object
Chunk
>>> dir(chunk)
['Chunk', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__',
'__loader__', '__name__', '__package__']
集合类collections有许多子类,具体怎么用要仔细研究
>>> import collections
>>> help(collections)
Help on package collections:
NAME
collections
PACKAGE CONTENTS
__main__
abc
CLASSES
builtins.dict(builtins.object)
Counter
OrderedDict
defaultdict
builtins.object
deque
collections.abc.Callable
collections.abc.Container
collections.abc.Hashable
collections.abc.Iterable
collections.abc.Iterator
collections.abc.Sized
collections.abc.Mapping(collections.abc.Sized, collections.abc.Iterable, collections.abc.Container)
collections.abc.MutableMapping
ChainMap
UserDict
collections.abc.MappingView
collections.abc.ItemsView(collections.abc.MappingView, collections.abc.Set)
collections.abc.KeysView(collections.abc.MappingView, collections.abc.Set)
collections.abc.ValuesView
collections.abc.Sequence(collections.abc.Sized, collections.abc.Iterable, collections.abc.Container)
UserString
collections.abc.ByteString
collections.abc.MutableSequence
UserList
collections.abc.Set(collections.abc.Sized, collections.abc.Iterable, collections.abc.Container)
collections.abc.MutableSet
class ByteString(Sequence)
| This unifies bytes and bytearray.
|
| XXX Should add all their methods.
|
| Method resolution order:
| ByteString
| Sequence
| Sized
| Iterable
| Container
| builtins.object
>>> dir(collections)
['ByteString', 'Callable', 'ChainMap', 'Container', 'Counter', 'Hashable', 'ItemsView',
'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping',
'MutableSequence', 'MutableSet', 'OrderedDict', 'Sequence', 'Set', 'Sized', 'UserDict',
'UserList', 'UserString', 'ValuesView', '_Link', '__all__', '__builtins__', '__cached__',
'__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__',
'__path__', '_chain', '_class_template', '_count_elements', '_eq', '_field_template',
'_heapq', '_iskeyword', '_itemgetter', '_proxy', '_recursive_repr', '_repeat',
'_repr_template', '_starmap', '_sys', 'abc', 'collections', 'defaultdict', 'deque',
'namedtuple']
>>> dir(collections.ByteString)
['__abstractmethods__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__',
'__slots__', '__str__', '__subclasshook__', '_abc_cache', '_abc_negative_cache',
'_abc_negative_cache_version', '_abc_registry', 'count', 'index']
>>> dir(collections.Hashable)
['__abstractmethods__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_abc_cache',
'_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry']
批量文件操作,使用fileinput。
>>> import fileinput
>>> help(fileinput)
Help on module fileinput:
NAME
fileinput - Helper class to quickly write a loop over all standard input files.
DESCRIPTION
Typical use is:
import fileinput
for line in fileinput.input():
process(line)
This iterates over the lines of all files listed in sys.argv[1:],
defaulting to sys.stdin if the list is empty. If a filename is '-' it
is also replaced by sys.stdin. To specify an alternative list of
filenames, pass it as the argument to input(). A single file name is
also allowed.
>>> dir(fileinput)
['DEFAULT_BUFSIZE', 'FileInput', '__all__', '__builtins__', '__cached__', '__doc__',
'__file__', '__initializing__', '__loader__', '__name__', '__package__', '_state', '_test',
'close', 'filelineno', 'filename', 'fileno', 'hook_compressed', 'hook_encoded', 'input',
'isfirstline', 'isstdin', 'lineno', 'nextfile', 'os', 'sys']
fornatter可以设置writer的格式
>>> import formatter
>>> help(formatter)
Help on module formatter:
NAME
formatter - Generic output formatting.
DESCRIPTION
Formatter objects transform an abstract flow of formatting events into
specific output events on writer objects. Formatters manage several stack
structures to allow various properties of a writer object to be changed and
restored; writers need not be able to handle relative changes nor any sort
of ``change back'' operation. Specific writer properties which may be
controlled via formatter objects are horizontal alignment, font, and left
margin indentations. A mechanism is provided which supports providing
arbitrary, non-exclusive style settings to a writer as well. Additional
interfaces facilitate formatting events which are not reversible, such as
paragraph separation.
Writer objects encapsulate device interfaces. Abstract devices, such as
file formats, are supported as well as physical devices. The provided
implementations all work with abstract devices. The interface makes
available mechanisms for setting the properties which formatter objects
manage and inserting data into the output.
CLASSES
builtins.object
AbstractFormatter
NullFormatter
NullWriter
AbstractWriter
DumbWriter
class AbstractFormatter(builtins.object)
| The standard formatter.
|
| This implementation has demonstrated wide applicability to many writers,
| and may be used directly in most circumstances. It has been used to
| implement a full-featured World Wide Web browser.
>>> dir(formatter)
['AS_IS', 'AbstractFormatter', 'AbstractWriter', 'DumbWriter', 'NullFormatter', 'NullWriter'
, '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__',
'__name__', '__package__', 'sys', 'test']
>>> dir(formatter.DumbWriter)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'flush', 'new_alignment',
'new_font', 'new_margin', 'new_spacing', 'new_styles', 'reset', 'send_flowing_data',
'send_hor_rule', 'send_label_data', 'send_line_break', 'send_literal_data',
'send_paragraph']
加载库可以用importlib,但不用也没关系
>>> import importlib
>>> help(importlib)
Help on package importlib:
NAME
importlib - A pure Python implementation of import.
PACKAGE CONTENTS
_bootstrap
abc
machinery
util
>>> dir(importlib)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__',
'__initializing__', '__loader__', '__name__', '__package__', '__path__', '_bootstrap',
'_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches',
'machinery', 'sys']
这些模块只有到必须要用时才会去看它们函数调用的语法,平时一般也用不上。
本节到此结束。