第七章:Python基础のXML操作和面向对象(一)

本課主題

  • XML介绍与操作实战
  • shutil 模块介绍与操作实战
  • subprocess 模块介绍与操作实战
  • 初探面向对象与操作实战
  • 本周作业

 

XML介绍和操作实战

對於浏览器返回的字符串有以下幾種:

  1. HTML
  2. JSON,表现是列表和字典的格式
  3. XML

XML的應用

一个属性里包含多过属性,一类是页面上做展示的时候可以用到XML,就是为别人来调用(这也是字符串类型的一个XML格式数据);另外一类是配置文件。你可以从文件里打开一个XML文件,它返回的是一个XML形式的字符串;也可以通过发送 HTTP请求,返回一个 XML 形式的字符串

XML函数功能

class Element:
    """An XML element.

    This class is the reference implementation of the Element interface.

    An element's length is its number of subelements.  That means if you
    want to check if an element is truly empty, you should check BOTH
    its length AND its text attribute.

    The element tag, attribute names, and attribute values can be either
    bytes or strings.

    *tag* is the element name.  *attrib* is an optional dictionary containing
    element attributes. *extra* are additional element attributes given as
    keyword arguments.

    Example form:
        text...tail

    """

    当前节点的标签名
    tag = None
    """The element's name."""

    当前节点的属性

    attrib = None
    """Dictionary of the element's attributes."""

    当前节点的内容
    text = None
    """
    Text before first subelement. This is either a string or the value None.
    Note that if there is no text, this attribute may be either
    None or the empty string, depending on the parser.

    """

    tail = None
    """
    Text after this element's end tag, but before the next sibling element's
    start tag.  This is either a string or the value None.  Note that if there
    was no text, this attribute may be either None or an empty string,
    depending on the parser.

    """

    def __init__(self, tag, attrib={}, **extra):
        if not isinstance(attrib, dict):
            raise TypeError("attrib must be dict, not %s" % (
                attrib.__class__.__name__,))
        attrib = attrib.copy()
        attrib.update(extra)
        self.tag = tag
        self.attrib = attrib
        self._children = []

    def __repr__(self):
        return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))

    def makeelement(self, tag, attrib):
        创建一个新节点
        """Create a new element with the same type.

        *tag* is a string containing the element name.
        *attrib* is a dictionary containing the element attributes.

        Do not call this method, use the SubElement factory function instead.

        """
        return self.__class__(tag, attrib)

    def copy(self):
        """Return copy of current element.

        This creates a shallow copy. Subelements will be shared with the
        original tree.

        """
        elem = self.makeelement(self.tag, self.attrib)
        elem.text = self.text
        elem.tail = self.tail
        elem[:] = self
        return elem

    def __len__(self):
        return len(self._children)

    def __bool__(self):
        warnings.warn(
            "The behavior of this method will change in future versions.  "
            "Use specific 'len(elem)' or 'elem is not None' test instead.",
            FutureWarning, stacklevel=2
            )
        return len(self._children) != 0 # emulate old behaviour, for now

    def __getitem__(self, index):
        return self._children[index]

    def __setitem__(self, index, element):
        # if isinstance(index, slice):
        #     for elt in element:
        #         assert iselement(elt)
        # else:
        #     assert iselement(element)
        self._children[index] = element

    def __delitem__(self, index):
        del self._children[index]

    def append(self, subelement):
        为当前节点追加一个子节点
        """Add *subelement* to the end of this element.

        The new element will appear in document order after the last existing
        subelement (or directly after the text, if it's the first subelement),
        but before the end tag for this element.

        """
        self._assert_is_element(subelement)
        self._children.append(subelement)

    def extend(self, elements):
        为当前节点扩展 n 个子节点
        """Append subelements from a sequence.

        *elements* is a sequence with zero or more elements.

        """
        for element in elements:
            self._assert_is_element(element)
        self._children.extend(elements)

    def insert(self, index, subelement):
        在当前节点的子节点中插入某个节点,即:为当前节点创建子节点,然后插入指定位置
        """Insert *subelement* at position *index*."""
        self._assert_is_element(subelement)
        self._children.insert(index, subelement)

    def _assert_is_element(self, e):
        # Need to refer to the actual Python implementation, not the
        # shadowing C implementation.
        if not isinstance(e, _Element_Py):
            raise TypeError('expected an Element, not %s' % type(e).__name__)

    def remove(self, subelement):
        在当前节点在子节点中删除某个节点
        """Remove matching subelement.

        Unlike the find methods, this method compares elements based on
        identity, NOT ON tag value or contents.  To remove subelements by
        other means, the easiest way is to use a list comprehension to
        select what elements to keep, and then use slice assignment to update
        the parent element.

        ValueError is raised if a matching element could not be found.

        """
        # assert iselement(element)
        self._children.remove(subelement)

    def getchildren(self):
        获取所有的子节点(废弃)
        """(Deprecated) Return all subelements.

        Elements are returned in document order.

        """
        warnings.warn(
            "This method will be removed in future versions.  "
            "Use 'list(elem)' or iteration over elem instead.",
            DeprecationWarning, stacklevel=2
            )
        return self._children

    def find(self, path, namespaces=None):
        获取第一个寻找到的子节点
        """Find first matching element by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return the first matching element, or None if no element was found.

        """
        return ElementPath.find(self, path, namespaces)

    def findtext(self, path, default=None, namespaces=None):
        获取第一个寻找到的子节点的内容
        """Find text for first matching element by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *default* is the value to return if the element was not found,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return text content of first matching element, or default value if
        none was found.  Note that if an element is found having no text
        content, the empty string is returned.

        """
        return ElementPath.findtext(self, path, default, namespaces)

    def findall(self, path, namespaces=None):
        获取所有的子节点
        """Find all matching subelements by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Returns list containing all matching elements in document order.

        """
        return ElementPath.findall(self, path, namespaces)

    def iterfind(self, path, namespaces=None):
        获取所有指定的节点,并创建一个迭代器(可以被for循环)
        """Find all matching subelements by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return an iterable yielding all matching elements in document order.

        """
        return ElementPath.iterfind(self, path, namespaces)

    def clear(self):
        清空节点
        """Reset element.

        This function removes all subelements, clears all attributes, and sets
        the text and tail attributes to None.

        """
        self.attrib.clear()
        self._children = []
        self.text = self.tail = None

    def get(self, key, default=None):
        获取当前节点的属性值
        """Get element attribute.

        Equivalent to attrib.get, but some implementations may handle this a
        bit more efficiently.  *key* is what attribute to look for, and
        *default* is what to return if the attribute was not found.

        Returns a string containing the attribute value, or the default if
        attribute was not found.

        """
        return self.attrib.get(key, default)

    def set(self, key, value):
        为当前节点设置属性值
        """Set element attribute.

        Equivalent to attrib[key] = value, but some implementations may handle
        this a bit more efficiently.  *key* is what attribute to set, and
        *value* is the attribute value to set it to.

        """
        self.attrib[key] = value

    def keys(self):
        获取当前节点的所有属性的 key

        """Get list of attribute names.

        Names are returned in an arbitrary order, just like an ordinary
        Python dict.  Equivalent to attrib.keys()

        """
        return self.attrib.keys()

    def items(self):
        获取当前节点的所有属性值,每个属性都是一个键值对
        """Get element attributes as a sequence.

        The attributes are returned in arbitrary order.  Equivalent to
        attrib.items().

        Return a list of (name, value) tuples.

        """
        return self.attrib.items()

    def iter(self, tag=None):
        在当前节点的子孙中根据节点名称寻找所有指定的节点,并返回一个迭代器(可以被for循环)。
        """Create tree iterator.

        The iterator loops over the element and all subelements in document
        order, returning all elements with a matching tag.

        If the tree structure is modified during iteration, new or removed
        elements may or may not be included.  To get a stable set, use the
        list() function on the iterator, and loop over the resulting list.

        *tag* is what tags to look for (default is to return all elements)

        Return an iterator containing all the matching elements.

        """
        if tag == "*":
            tag = None
        if tag is None or self.tag == tag:
            yield self
        for e in self._children:
            yield from e.iter(tag)

    # compatibility
    def getiterator(self, tag=None):
        # Change for a DeprecationWarning in 1.4
        warnings.warn(
            "This method will be removed in future versions.  "
            "Use 'elem.iter()' or 'list(elem.iter())' instead.",
            PendingDeprecationWarning, stacklevel=2
        )
        return list(self.iter(tag))

    def itertext(self):
        在当前节点的子孙中根据节点名称寻找所有指定的节点的内容,并返回一个迭代器(可以被for循环)。
        """Create text iterator.

        The iterator loops over the element and all subelements in document
        order, returning all inner text.

        """
        tag = self.tag
        if not isinstance(tag, str) and tag is not None:
            return
        if self.text:
            yield self.text
        for e in self:
            yield from e.itertext()
            if e.tail:
                yield e.tail

节点功能一览表
节点功能一览表

每一个节点都是一个 element 对象,节点里可以嵌套节点;这里的 book.xml 例子有 <书>:每本书分別都有<书名>,<作者>,<出版年份> 和 <价格> 4个属性。

<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italiantitle>
    <author>Giada De Laurentiisauthor>
    <year>2005year>
    <price>30.00price>
  book>
  <book category="children">
    <title lang="en">Harry Pottertitle>
    <author>J K. Rowlingauthor>
    <year>2005year>
    <price>29.99price>
  book>
  <book category="web">
    <title lang="en">Learning XMLtitle>
    <author>Erik T. Rayauthor>
    <year>2003year>
    <price>39.95price>
  book>
bookstore>
Book.xml
  1. 把整个XML文件的树先取出来放在一个XML对象里,加载到内存里,它返回的是一个ElementTree 里的 ElementTree类型
    >>> from xml.etree import ElementTree as ET
    >>> xmltree = ET.parse('book.xml')
    >>> print(type(xmltree))
    <class 'xml.etree.ElementTree.ElementTree'>
    ET.parse()
  2. 如果你想取它的根目录的话,调用getroot( )函数,它返回的是一个ElementTree 里的 Element类型 
    >>> xmltree = ET.parse('book.xml')
    >>> root = xmltree.getroot()
    >>> print(root)
    'bookstore' at 0x10d065868>
    
    >>> print(type(root))
    <class 'xml.etree.ElementTree.Element'>
    getroot()例子
  3. 用的 open( )函数打开一个XML文件,它返回的是XML形式的字符串,然后通过调用XML( )函数来读取/操作出XML格式的文件,它返回的也是一个 ElementTree 里的Element类型
    >>> str_xmltree = open('book.xml','r').read() # 返回XML形式的字符串
    >>> print(type(str_xmltree))
    <class 'str'>
    
    >>> root = ET.XML(str_xmltree)
    >>> print(root)
    'bookstore' at 0x10d06eb88>
    
    >>> print(type(root)) 
    <class 'xml.etree.ElementTree.Element'>
    ET.XML()例子
  4. 取出XML文件的 tag element,一個XML文件可以有很多 tag,tag 是構成XML格式的重要元素。 比如在上面 bookstore 的例子里,tag 分別是 、<author>、<year>、<price></span>,如果想找到一個 XML 的 Tag,就可以調用 tag变量來找。<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_67e3e87b-6d25-4d67-8de0-6b84040c783d" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_67e3e87b-6d25-4d67-8de0-6b84040c783d" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_67e3e87b-6d25-4d67-8de0-6b84040c783d" class="cnblogs_code_hide"> <pre>>>> xmltree = ET.parse(<span style="color:#800000;">'</span><span style="color:#800000;">book.xml</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span>>>> root =<span style="color:#000000;"> xmltree.getroot() </span>>>> <span style="color:#0000ff;">print</span>(root.tag) <span style="color:#008000;">#</span><span style="color:#008000;"> 调用 tag 函数</span> <span style="color:#000000;">bookstore </span>>>> <span style="color:#0000ff;">for</span> child <span style="color:#0000ff;">in</span> root: <span style="color:#008000;">#</span><span style="color:#008000;">下一层也有下一层的Tag</span> ... <span style="color:#0000ff;">print</span><span style="color:#000000;">(child.tag) ... book book book </span>>>> <span style="color:#0000ff;">for</span> child <span style="color:#0000ff;">in</span> root: <span style="color:#008000;">#</span><span style="color:#008000;">下一层也有下一层的Tag</span> ... <span style="color:#0000ff;">print</span><span style="color:#000000;">(child.tag) ... </span><span style="color:#0000ff;">for</span> grandchild <span style="color:#0000ff;">in</span><span style="color:#000000;"> child: ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(child.tag, grandchild.tag) ... book book title book author book year book price book book title book author book year book price book book title book author book year book price</span></pre> </div> <span class="cnblogs_code_collapse">tag变量</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">取出XML文件Tag中的metadata/attribue,例如: <book <span style="color:#0000ff;">category="cooking"</span>>,catergory = "cooking" 就是調用以下函数可以拿到的,返回的是一个<strong><span style="color:#993300;">字典</span></strong>类型。<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_20ccd2cc-a114-40e7-b779-d5021f64234d" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_20ccd2cc-a114-40e7-b779-d5021f64234d" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_20ccd2cc-a114-40e7-b779-d5021f64234d" class="cnblogs_code_hide"> <pre>>>> xmltree = ET.parse(<span style="color:#800000;">'</span><span style="color:#800000;">book.xml</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span>>>> root =<span style="color:#000000;"> xmltree.getroot() </span>>>> <span style="color:#0000ff;">for</span> child <span style="color:#0000ff;">in</span><span style="color:#000000;"> root: ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(child.tag, child.attrib,type(child.attrib)) ... book {</span><span style="color:#800000;">'</span><span style="color:#800000;">category</span><span style="color:#800000;">'</span>: <span style="color:#800000;">'</span><span style="color:#800000;">cooking</span><span style="color:#800000;">'</span>} <<span style="color:#0000ff;">class</span> <span style="color:#800000;">'</span><span style="color:#800000;">dict</span><span style="color:#800000;">'</span>><span style="color:#000000;"> book {</span><span style="color:#800000;">'</span><span style="color:#800000;">category</span><span style="color:#800000;">'</span>: <span style="color:#800000;">'</span><span style="color:#800000;">children</span><span style="color:#800000;">'</span>} <<span style="color:#0000ff;">class</span> <span style="color:#800000;">'</span><span style="color:#800000;">dict</span><span style="color:#800000;">'</span>><span style="color:#000000;"> book {</span><span style="color:#800000;">'</span><span style="color:#800000;">category</span><span style="color:#800000;">'</span>: <span style="color:#800000;">'</span><span style="color:#800000;">web</span><span style="color:#800000;">'</span>} <<span style="color:#0000ff;">class</span> <span style="color:#800000;">'</span><span style="color:#800000;">dict</span><span style="color:#800000;">'</span>></pre> </div> <span class="cnblogs_code_collapse">attrib变量</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">想取出 Tag 裡值的話,例如: <author><span style="color:#0000ff;">Giada De Laurentiis</span></author>,可以調用 text变量<br></span></span></span> <div class="cnblogs_code"> <img id="code_img_closed_d2c2f88a-34fb-412a-902c-d3d877092361" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_d2c2f88a-34fb-412a-902c-d3d877092361" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_d2c2f88a-34fb-412a-902c-d3d877092361" class="cnblogs_code_hide"> <pre>>>> xmltree = ET.parse(<span style="color:#800000;">'</span><span style="color:#800000;">book.xml</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span>>>> root =<span style="color:#000000;"> xmltree.getroot() </span>>>> <span style="color:#0000ff;">for</span> child <span style="color:#0000ff;">in</span><span style="color:#000000;"> root: ... </span><span style="color:#0000ff;">for</span> grandchild <span style="color:#0000ff;">in</span><span style="color:#000000;"> child: ... </span><span style="color:#0000ff;">print</span>(child.tag+<span style="color:#800000;">"</span><span style="color:#800000;">-</span><span style="color:#800000;">"</span>+grandchild.tag+<span style="color:#800000;">"</span><span style="color:#800000;">:</span><span style="color:#800000;">"</span><span style="color:#000000;">, grandchild.text) ... book</span>-<span style="color:#000000;">title: Everyday Italian book</span>-<span style="color:#000000;">author: Giada De Laurentiis book</span>-year: 2005<span style="color:#000000;"> book</span>-price: 30.00<span style="color:#000000;"> book</span>-<span style="color:#000000;">title: Harry Potter book</span>-<span style="color:#000000;">author: J K. Rowling book</span>-year: 2005<span style="color:#000000;"> book</span>-price: 29.99<span style="color:#000000;"> book</span>-<span style="color:#000000;">title: Learning XML book</span>-<span style="color:#000000;">author: Erik T. Ray book</span>-year: 2003<span style="color:#000000;"> book</span>-price: 39.95</pre> </div> <span class="cnblogs_code_collapse">text变量</span> </div> </li> <li> <p><span style="font-size:12px;">如何生成新的XML文件<br></span></p> </li> <li> <p><span style="font-size:12px;">如何创建节点</span></p> </li> <li> <p><span style="font-size:12px;">如何创建XML</span></p> </li> </ol> <p> </p> <div class="cnblogs_code"> <img id="code_img_closed_5ae45fe2-eabe-435c-82e7-60401226cf79" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_5ae45fe2-eabe-435c-82e7-60401226cf79" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_5ae45fe2-eabe-435c-82e7-60401226cf79" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;"><</span><span style="color:#800000;">data </span><span style="color:#ff0000;">title</span><span style="color:#0000ff;">="'CTO"</span><span style="color:#ff0000;"> age</span><span style="color:#0000ff;">="50"</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">country </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Liechtenstein"</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">rank </span><span style="color:#ff0000;">updated</span><span style="color:#0000ff;">="yes"</span><span style="color:#0000ff;">></span>2<span style="color:#0000ff;"></</span><span style="color:#800000;">rank</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">year</span><span style="color:#0000ff;">></span>2023<span style="color:#0000ff;"></</span><span style="color:#800000;">year</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">gdppc</span><span style="color:#0000ff;">></span>141100<span style="color:#0000ff;"></</span><span style="color:#800000;">gdppc</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">neighbor </span><span style="color:#ff0000;">direction</span><span style="color:#0000ff;">="E"</span><span style="color:#ff0000;"> name</span><span style="color:#0000ff;">="Austria"</span> <span style="color:#0000ff;">/></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">neighbor </span><span style="color:#ff0000;">direction</span><span style="color:#0000ff;">="W"</span><span style="color:#ff0000;"> name</span><span style="color:#0000ff;">="Switzerland"</span> <span style="color:#0000ff;">/></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">country</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">country </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Singapore"</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">rank </span><span style="color:#ff0000;">updated</span><span style="color:#0000ff;">="yes"</span><span style="color:#0000ff;">></span>5<span style="color:#0000ff;"></</span><span style="color:#800000;">rank</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">year</span><span style="color:#0000ff;">></span>2026<span style="color:#0000ff;"></</span><span style="color:#800000;">year</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">gdppc</span><span style="color:#0000ff;">></span>59900<span style="color:#0000ff;"></</span><span style="color:#800000;">gdppc</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">neighbor </span><span style="color:#ff0000;">direction</span><span style="color:#0000ff;">="N"</span><span style="color:#ff0000;"> name</span><span style="color:#0000ff;">="Malaysia"</span> <span style="color:#0000ff;">/></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">country</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">country </span><span style="color:#ff0000;">name</span><span style="color:#0000ff;">="Panama"</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">rank </span><span style="color:#ff0000;">updated</span><span style="color:#0000ff;">="yes"</span><span style="color:#0000ff;">></span>69<span style="color:#0000ff;"></</span><span style="color:#800000;">rank</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">year</span><span style="color:#0000ff;">></span>2026<span style="color:#0000ff;"></</span><span style="color:#800000;">year</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">gdppc</span><span style="color:#0000ff;">></span>13600<span style="color:#0000ff;"></</span><span style="color:#800000;">gdppc</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">neighbor </span><span style="color:#ff0000;">direction</span><span style="color:#0000ff;">="W"</span><span style="color:#ff0000;"> name</span><span style="color:#0000ff;">="Costa Rica"</span> <span style="color:#0000ff;">/></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">neighbor </span><span style="color:#ff0000;">direction</span><span style="color:#0000ff;">="E"</span><span style="color:#ff0000;"> name</span><span style="color:#0000ff;">="Colombia"</span> <span style="color:#0000ff;">/></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">country</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">data</span><span style="color:#0000ff;">></span></pre> </div> <span class="cnblogs_code_collapse">xo.xml</span> </div> <div class="cnblogs_code"> <img id="code_img_closed_fbfd915c-8ddf-49cd-985c-ccd0d6abcb5f" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_fbfd915c-8ddf-49cd-985c-ccd0d6abcb5f" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_fbfd915c-8ddf-49cd-985c-ccd0d6abcb5f" class="cnblogs_code_hide"> <pre>>>> <span style="color:#0000ff;">from</span> xml.etree <span style="color:#0000ff;">import</span><span style="color:#000000;"> ElementTree as ET </span>>>> tree = ET.parse(<span style="color:#800000;">'</span><span style="color:#800000;">xo.xml</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span>>>> root =<span style="color:#000000;"> tree.getroot() </span>>>> <span style="color:#0000ff;">for</span> child <span style="color:#0000ff;">in</span><span style="color:#000000;"> root: ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(child.tag,child.attrib) ... </span><span style="color:#0000ff;">for</span> grandchild <span style="color:#0000ff;">in</span><span style="color:#000000;"> child: ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(grandchild.tag,grandchild.text) ... country {</span><span style="color:#800000;">'</span><span style="color:#800000;">name</span><span style="color:#800000;">'</span>: <span style="color:#800000;">'</span><span style="color:#800000;">Liechtenstein</span><span style="color:#800000;">'</span><span style="color:#000000;">} rank </span>2<span style="color:#000000;"> year </span>2023<span style="color:#000000;"> gdppc </span>141100<span style="color:#000000;"> neighbor None neighbor None country {</span><span style="color:#800000;">'</span><span style="color:#800000;">name</span><span style="color:#800000;">'</span>: <span style="color:#800000;">'</span><span style="color:#800000;">Singapore</span><span style="color:#800000;">'</span><span style="color:#000000;">} rank </span>5<span style="color:#000000;"> year </span>2026<span style="color:#000000;"> gdppc </span>59900<span style="color:#000000;"> neighbor None country {</span><span style="color:#800000;">'</span><span style="color:#800000;">name</span><span style="color:#800000;">'</span>: <span style="color:#800000;">'</span><span style="color:#800000;">Panama</span><span style="color:#800000;">'</span><span style="color:#000000;">} rank </span>69<span style="color:#000000;"> year </span>2026<span style="color:#000000;"> gdppc </span>13600<span style="color:#000000;"> neighbor None neighbor None</span></pre> </div> <span class="cnblogs_code_collapse">XML例子</span> </div> <p> </p> <h2>shutil 模块介绍和操作实战</h2> <p><span style="font-size:12px;">这个模块主要是为了处理文件、文件夹和压缩文件,它文件內容,權限,文件夾嵌套文件夾、壓縮文件的處理。</span></p> <p><span style="font-size:12px;">文件操作:打开文件以及读写文件</span></p> <ol> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">拷贝文件:從一個文件拷贝到另外一個文件,这里接受的是一个文件<span style="color:#993300;"><strong>对象</strong> </span> </span></span></span></span></span><span style="font-size:12px;"><class '_io.TextIOWrapper'></span> <div class="cnblogs_code"> <img id="code_img_closed_f8f71169-3ba2-48ae-8dbe-4233f254fce5" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_f8f71169-3ba2-48ae-8dbe-4233f254fce5" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_f8f71169-3ba2-48ae-8dbe-4233f254fce5" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil </span><span style="color:#008000;">#</span><span style="color:#008000;"> Copy a file from source to destination</span> src = open(<span style="color:#800000;">"</span><span style="color:#800000;">src.txt</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">r</span><span style="color:#800000;">"</span>) <span style="color:#008000;">#</span><span style="color:#008000;"><class '_io.TextIOWrapper'></span> dest = open(<span style="color:#800000;">"</span><span style="color:#800000;">dest.txt</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">w</span><span style="color:#800000;">"</span>) <span style="color:#008000;">#</span><span style="color:#008000;"><class '_io.TextIOWrapper'></span> <span style="color:#000000;"> shutil.copyfileobj(src,dest)</span></pre> </div> <span class="cnblogs_code_collapse">拷贝文件(方法一)</span> </div> </li> <li><span style="font-size:12px;">拷贝文件:從一個文件拷贝到另外一個文件,这里接受的是一个文件<span style="color:#993300;"><strong>字符串</strong></span></span><br> <div class="cnblogs_code"> <img id="code_img_closed_7d08de5a-7c0b-4f73-b161-bed0ecebd199" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_7d08de5a-7c0b-4f73-b161-bed0ecebd199" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_7d08de5a-7c0b-4f73-b161-bed0ecebd199" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil </span><span style="color:#008000;">#</span><span style="color:#008000;"> Copy a file from source to destination</span> shutil.copyfile(<span style="color:#800000;">"</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">"</span>)</pre> </div> <span class="cnblogs_code_collapse">拷贝文件(方法二)</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">只拷贝文件的权限<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_6d61eb6b-b8dd-436c-94e7-8883477d72aa" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_6d61eb6b-b8dd-436c-94e7-8883477d72aa" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_6d61eb6b-b8dd-436c-94e7-8883477d72aa" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil shutil.copymode(</span><span style="color:#800000;">"</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">"</span>)</pre> </div> <span class="cnblogs_code_collapse">拷贝文件权限</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">拷贝状态的信息,包括:mode bits, atime, mtime, flags</span></span> <div class="cnblogs_code"> <img id="code_img_closed_e7771532-111b-4812-9ab1-1958a7468691" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_e7771532-111b-4812-9ab1-1958a7468691" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_e7771532-111b-4812-9ab1-1958a7468691" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shut shutil.copystat(</span><span style="color:#800000;">"</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">"</span>)</pre> </div> <span class="cnblogs_code_collapse">拷贝文件</span> </div> </li> <li><span style="font-size:12px;">拷贝文件和权限</span><br> <div class="cnblogs_code"> <img id="code_img_closed_085b64f5-74bc-42f4-9681-88bc722f0dc3" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_085b64f5-74bc-42f4-9681-88bc722f0dc3" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_085b64f5-74bc-42f4-9681-88bc722f0dc3" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil shutil.copy(</span><span style="color:#800000;">'</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">'</span>, <span style="color:#800000;">'</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">'</span>)</pre> </div> <span class="cnblogs_code_collapse">拷贝文件和权限</span> </div> </li> <li><span style="font-size:12px;">拷贝文件和状态信息</span><br> <div class="cnblogs_code"> <img id="code_img_closed_1e1518a2-4ae4-4593-928b-bb0b164a71ef" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_1e1518a2-4ae4-4593-928b-bb0b164a71ef" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_1e1518a2-4ae4-4593-928b-bb0b164a71ef" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil shutil.copy2(</span><span style="color:#800000;">'</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">'</span>, <span style="color:#800000;">'</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">'</span>)</pre> </div> <span class="cnblogs_code_collapse">拷贝文件和状态信息</span> </div> </li> <li><span style="font-size:12px;">递归的去拷贝文件夹</span><br> <div class="cnblogs_code"> <img id="code_img_closed_ac2397c8-77e1-4bcc-9f11-a5e14a00feb3" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_ac2397c8-77e1-4bcc-9f11-a5e14a00feb3" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_ac2397c8-77e1-4bcc-9f11-a5e14a00feb3" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil shutil.copytree(</span><span style="color:#800000;">'</span><span style="color:#800000;">folder1</span><span style="color:#800000;">'</span>, <span style="color:#800000;">'</span><span style="color:#800000;">folder2</span><span style="color:#800000;">'</span>, ignore=shutil.ignore_patterns(<span style="color:#800000;">'</span><span style="color:#800000;">*.pyc</span><span style="color:#800000;">'</span>, <span style="color:#800000;">'</span><span style="color:#800000;">tmp*</span><span style="color:#800000;">'</span>))</pre> </div> <span class="cnblogs_code_collapse">递归的去拷贝文件夹</span> </div> </li> <li><span style="font-size:12px;">递归的去移动文件,它类似mv命令,其实就是重命名</span><br> <div class="cnblogs_code"> <img id="code_img_closed_e800b8e1-d92c-4f93-ac3c-b08ddec3dbc3" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_e800b8e1-d92c-4f93-ac3c-b08ddec3dbc3" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_e800b8e1-d92c-4f93-ac3c-b08ddec3dbc3" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> shutil shutil.move(</span><span style="color:#800000;">'</span><span style="color:#800000;">folder1</span><span style="color:#800000;">'</span>, <span style="color:#800000;">'</span><span style="color:#800000;">folder3</span><span style="color:#800000;">'</span>)</pre> </div> <span class="cnblogs_code_collapse">递归的去移动文件</span> </div> </li> <li><span style="font-size:12px;">创建压缩包并返回文件路径</span><br> <div class="cnblogs_Highlighter"> <pre><code class="language-python">shutil.make_archive(base_name, format,...)</code></pre> </div> </li> <li>xxxxx</li> </ol> <h3>Python 的压缩操作</h3> <p><span style="font-size:12px;">对于解压单个文件来说,<strong><span style="color:#993300;"><em>注意:</em><em>zipfile 模块來解压指定文件只需要输入字符串,但 tarfile <em>模块</em>必需传入一个 <class 'tarfile.TarInfo'>的对象才可以成功解压</em></span></strong>。<br></span></p> <h3><span style="font-size:12px;">zipfile</span></h3> <ol> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">压缩文件<br></span></span></span> <div class="cnblogs_code"> <img id="code_img_closed_28fdae73-5a7f-49ea-a1fa-9936c5e5626e" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_28fdae73-5a7f-49ea-a1fa-9936c5e5626e" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_28fdae73-5a7f-49ea-a1fa-9936c5e5626e" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> zipfile z </span>= zipfile.ZipFile(<span style="color:#800000;">"</span><span style="color:#800000;">examples.zip</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">w</span><span style="color:#800000;">"</span><span style="color:#000000;">) z.write(</span><span style="color:#800000;">"</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">"</span><span style="color:#000000;">) z.write(</span><span style="color:#800000;">"</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">"</span><span style="color:#000000;">) z.close()</span></pre> </div> <span class="cnblogs_code_collapse">Zip压缩</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">解压全部文件<br></span></span></span> <div class="cnblogs_code"> <img id="code_img_closed_40a5a687-206e-4d3c-ac2e-c513407c4516" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_40a5a687-206e-4d3c-ac2e-c513407c4516" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_40a5a687-206e-4d3c-ac2e-c513407c4516" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> zipfile z </span>= zipfile.ZipFile(<span style="color:#800000;">"</span><span style="color:#800000;">examples.zip</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">r</span><span style="color:#800000;">"</span><span style="color:#000000;">) z.extractall() z.close()</span></pre> </div> <span class="cnblogs_code_collapse">Zip解压全部</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">解压单个文件,调用 namelist( ) 去找一个压缩文件里有哪些文件 (返回<span style="color:#993300;"><strong>字符串</strong></span>)<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_48297d40-e1f2-4f2d-8c56-628dee23f575" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_48297d40-e1f2-4f2d-8c56-628dee23f575" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_48297d40-e1f2-4f2d-8c56-628dee23f575" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> zipfile z </span>= zipfile.ZipFile(<span style="color:#800000;">"</span><span style="color:#800000;">examples.zip</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">r</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span>(z.namelist()) <span style="color:#008000;">#</span><span style="color:#008000;">['f1.log', 'f2.log']</span> <span style="color:#0000ff;">for</span> item <span style="color:#0000ff;">in</span><span style="color:#000000;"> z.namelist(): </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(item) z.extract(</span><span style="color:#800000;">"</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">"</span><span style="color:#000000;">) z.close()</span></pre> </div> <span class="cnblogs_code_collapse">Zip解压单个文件</span> </div> </li> </ol> <h3><span style="font-size:12px;">tarfile</span></h3> <ol> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">压缩文件<br></span></span></span> <div class="cnblogs_code"> <img id="code_img_closed_7e6693a6-8575-4d50-91c9-437e5fcd95d6" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_7e6693a6-8575-4d50-91c9-437e5fcd95d6" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_7e6693a6-8575-4d50-91c9-437e5fcd95d6" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> tarfile tar </span>= tarfile.open(<span style="color:#800000;">'</span><span style="color:#800000;">example.tar</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">w</span><span style="color:#800000;">'</span><span style="color:#000000;">) tar.add(</span><span style="color:#800000;">'</span><span style="color:#800000;">f1.log</span><span style="color:#800000;">'</span>, arcname=<span style="color:#800000;">'</span><span style="color:#800000;">access.log</span><span style="color:#800000;">'</span><span style="color:#000000;">) tar.add(</span><span style="color:#800000;">'</span><span style="color:#800000;">f2.log</span><span style="color:#800000;">'</span>, arcname=<span style="color:#800000;">'</span><span style="color:#800000;">error.log</span><span style="color:#800000;">'</span><span style="color:#000000;">) tar.close()</span></pre> </div> <span class="cnblogs_code_collapse">tar压缩文件</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">解压全部文件<br></span></span></span> <div class="cnblogs_code"> <img id="code_img_closed_35e350d9-b8f2-41f1-9c8f-2a3809673216" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_35e350d9-b8f2-41f1-9c8f-2a3809673216" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_35e350d9-b8f2-41f1-9c8f-2a3809673216" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> tarfile tar </span>= tarfile.open(<span style="color:#800000;">"</span><span style="color:#800000;">example.tar</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">r</span><span style="color:#800000;">"</span><span style="color:#000000;">) tar.extractall() </span><span style="color:#008000;">#</span><span style="color:#008000;"> 可设置解压地址</span> tar.close()</pre> </div> <span class="cnblogs_code_collapse">tar解压全部文件</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">解压单个文件,tarfile 模块在解压单个文件时,只接受一个<span style="color:#993300;"><strong>对象</strong></span>而不是字符串<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_a242e491-c454-41a0-8f8b-d201960b5698" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_a242e491-c454-41a0-8f8b-d201960b5698" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_a242e491-c454-41a0-8f8b-d201960b5698" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> tarfile tar </span>= tarfile.open(<span style="color:#800000;">"</span><span style="color:#800000;">example.tar</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">r</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#008000;">#</span><span style="color:#008000;"> Return a TarInfo object <class 'tarfile.TarInfo'></span> <span style="color:#0000ff;">for</span> item <span style="color:#0000ff;">in</span><span style="color:#000000;"> tar.getmembers(): </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(item,type(item)) </span><span style="color:#008000;">#</span><span style="color:#008000;">传入字符串获取一个对象</span> obj = tar.getmember(<span style="color:#800000;">'</span><span style="color:#800000;">access.log</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span>(obj) <span style="color:#008000;">#</span><span style="color:#008000;"> <TarInfo 'access.log' at 0x1033499a8></span> <span style="color:#000000;"> tar.extract(obj) tar.close()</span></pre> </div> <span class="cnblogs_code_collapse">tar解压单个文件</span> </div> </li> </ol> <h2> </h2> <h2>subprocess 模块介绍和操作实战</h2> <p><span style="font-size:12px;">如果参数 shell=True,第一个参数接受的是数符串;如果参数 shell=False,第一个参数接受的是列表</span></p> <ol> <li><span style="font-size:12px;"><span style="font-size:12px;">返回状态码: subprocess.call( )<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_f72e22b4-5709-4958-a34c-b86d0b2c0d4c" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_f72e22b4-5709-4958-a34c-b86d0b2c0d4c" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_f72e22b4-5709-4958-a34c-b86d0b2c0d4c" class="cnblogs_code_hide"> <pre>>>> ret1 = subprocess.call([<span style="color:#800000;">"</span><span style="color:#800000;">ls</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">-l</span><span style="color:#800000;">"</span>],shell=<span style="color:#000000;">False) </span>-rw-r--r-- 1 jcchoiling staff 535 Sep 17 17:04<span style="color:#000000;"> access.log </span>>>> <span style="color:#0000ff;">print</span><span style="color:#000000;">(ret1) </span>>>> ret2 = subprocess.call([<span style="color:#800000;">"</span><span style="color:#800000;">ls -l</span><span style="color:#800000;">"</span>],shell=<span style="color:#000000;">True) </span>-rw-r--r-- 1 jcchoiling staff 535 Sep 17 17:04<span style="color:#000000;"> access.log </span>>>> <span style="color:#0000ff;">print</span>(ret2)</pre> </div> <span class="cnblogs_code_collapse">subprocess 中的 "ls -la"</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">执行命令,如果执行状态码是0,则返回0,否则抛异常: subprocess.check_call( )</span></span></li> <li><span style="font-size:12px;"><span style="font-size:12px;">执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常: subprocess.check_out( )<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_4e77565a-f8e3-4ac3-b793-1c9601475391" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_4e77565a-f8e3-4ac3-b793-1c9601475391" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_4e77565a-f8e3-4ac3-b793-1c9601475391" class="cnblogs_code_hide"> <pre>>>> <span style="color:#0000ff;">import</span><span style="color:#000000;"> subprocess </span>>>> ret3 = subprocess.check_output([<span style="color:#800000;">"</span><span style="color:#800000;">ls -l</span><span style="color:#800000;">"</span>],shell=<span style="color:#000000;">True) </span>>>><span style="color:#000000;"> ret3 b</span><span style="color:#800000;">'</span><span style="color:#800000;">total 80\n-rw-r--r-- 1 jcchoiling staff 535 Sep 17 17:04 access.log\n-rw-r--r-- 1 jcchoiling staff 1136 Sep 5 22:05 createxmlOps.py\ndrwxr-xr-x 9 jcchoiling staff 306 Sep 17 16:55 data\n-rw-r--r-- 1 jcchoiling staff 535 Sep 17 17:04 error.log\n-rw-r--r-- 1 jcchoiling staff 10240 Sep 17 17:04 example.tar\n-rw-r--r-- 1 jcchoiling staff 615 Sep 17 17:06 s1.py\n-rw-r--r-- 1 jcchoiling staff 198 Sep 17 18:03 s2.py\n-rw-r--r-- 1 jcchoiling staff 938 Sep 5 20:33 xmlOps.py\n-rw-r--r-- 1 jcchoiling staff 361 Sep 17 17:04 zipOps.py\n</span><span style="color:#800000;">'</span> >>><span style="color:#000000;"> type(ret3) </span><<span style="color:#0000ff;">class</span> <span style="color:#800000;">'</span><span style="color:#800000;">bytes</span><span style="color:#800000;">'</span>></pre> </div> <span class="cnblogs_code_collapse">返回內容</span> </div> </li> </ol> <p><span style="font-size:12px;">上面几个方法内部其实也是调用subprocess.Popen( )函数,它相当于创建一些管道,分别是数据输入管道,数据输出管道和输出错误管道</span></p> <p><span style="font-size:12px;"><a href="http://img.e-com-net.com/image/info8/1a984462031643cab098d6b6ccc26fc9.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/1a984462031643cab098d6b6ccc26fc9.jpg" alt="第七章:Python基础のXML操作和面向对象(一)_第1张图片" width="650" height="260" style="border:1px solid black;"></a></span></p> <ol> <li><span style="font-size:12px;"><span style="font-size:12px;"><span style="font-size:12px;">基本的 subprocess.Popen( )函数<br></span></span></span> <div class="cnblogs_code"> <img id="code_img_closed_e690629a-dc5a-4d8d-a4ab-1b46fff5edb8" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_e690629a-dc5a-4d8d-a4ab-1b46fff5edb8" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_e690629a-dc5a-4d8d-a4ab-1b46fff5edb8" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> subprocess ret1 </span>= subprocess.Popen([<span style="color:#800000;">"</span><span style="color:#800000;">mkdir</span><span style="color:#800000;">"</span>,<span style="color:#800000;">"</span><span style="color:#800000;">t1</span><span style="color:#800000;">"</span>]) <span style="color:#008000;">#</span><span style="color:#008000;">第一个参数可以是字符串</span> ret2 = subprocess.Popen(<span style="color:#800000;">"</span><span style="color:#800000;">mkdir t2</span><span style="color:#800000;">"</span>, shell=True) <span style="color:#008000;">#</span><span style="color:#008000;">也可以是列表</span></pre> </div> <span class="cnblogs_code_collapse">subprocess.Popen( )基本语法一</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">也可以传入参数,指定特定路径</span></span> <div class="cnblogs_code"> <img id="code_img_closed_9a14e629-6562-418c-9e34-67507d236f41" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_9a14e629-6562-418c-9e34-67507d236f41" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_9a14e629-6562-418c-9e34-67507d236f41" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> subprocess obj </span>= subprocess.Popen(<span style="color:#800000;">"</span><span style="color:#800000;">mkdir t3</span><span style="color:#800000;">"</span>, shell=True, cwd=<span style="color:#800000;">'</span><span style="color:#800000;">/home/dev</span><span style="color:#800000;">'</span>,)</pre> </div> <span class="cnblogs_code_collapse">subprocess.Popen( )基本语法二</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">以下是创建管道的完整代码,首先要创建一个子对象,然后在创建的同时传入一些参数开通这三个管道,在适当的管道里分别写入或读取数据,最后每个管道都必需关闭才是完整<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_2364d1d6-d33b-4d6b-aa3a-110ec92d8797" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_2364d1d6-d33b-4d6b-aa3a-110ec92d8797" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_2364d1d6-d33b-4d6b-aa3a-110ec92d8797" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> subprocess obj </span>= subprocess.Popen([<span style="color:#800000;">"</span><span style="color:#800000;">python</span><span style="color:#800000;">"</span>], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=<span style="color:#000000;">True) obj.stdin.write(</span><span style="color:#800000;">"</span><span style="color:#800000;">print(1)\n</span><span style="color:#800000;">"</span><span style="color:#000000;">) obj.stdin.write(</span><span style="color:#800000;">"</span><span style="color:#800000;">print(2)</span><span style="color:#800000;">"</span><span style="color:#000000;">) obj.stdin.close() cmd_out </span>=<span style="color:#000000;"> obj.stdout.read() obj.stdout.close() cmd_error </span>=<span style="color:#000000;"> obj.stderr.read() obj.stderr.close() </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(cmd_out) </span><span style="color:#0000ff;">print</span>(cmd_error)</pre> </div> <span class="cnblogs_code_collapse">subprocess中的管道通信例子一(完整遍)</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">另外可以調用 communicate( ) 函数来获取输出stdout.read( )和错误的信息。<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_5622d8fb-cff3-4d5f-80b3-02a355a73af8" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_5622d8fb-cff3-4d5f-80b3-02a355a73af8" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_5622d8fb-cff3-4d5f-80b3-02a355a73af8" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> subprocess obj </span>= subprocess.Popen([<span style="color:#800000;">"</span><span style="color:#800000;">python</span><span style="color:#800000;">"</span>], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=<span style="color:#000000;">True) obj.stdin.write(</span><span style="color:#800000;">"</span><span style="color:#800000;">print(1)\n</span><span style="color:#800000;">"</span><span style="color:#000000;">) obj.stdin.write(</span><span style="color:#800000;">"</span><span style="color:#800000;">print(2)</span><span style="color:#800000;">"</span><span style="color:#000000;">) out_error_list </span>=<span style="color:#000000;"> obj.communicate() </span><span style="color:#0000ff;">print</span>(out_error_list)</pre> </div> <span class="cnblogs_code_collapse">subprocess中的管道通信例子二(半简洁遍)</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">最简洁的方式是当创建完一条管道后,直接调用obj.communicate("需要传入的命令") 来完成整个<span style="color:#993300;"><strong>写入、</strong><strong>读取</strong></span><span style="color:#ff0000;"><span style="color:#000000;">和</span><span style="color:#993300;"><strong>关闭</strong></span></span>的过程。</span></span> <div class="cnblogs_code"> <img id="code_img_closed_37f49fe9-431e-4591-af04-be381f43acbe" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_37f49fe9-431e-4591-af04-be381f43acbe" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_37f49fe9-431e-4591-af04-be381f43acbe" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">import</span><span style="color:#000000;"> subprocess obj </span>= subprocess.Popen([<span style="color:#800000;">"</span><span style="color:#800000;">python</span><span style="color:#800000;">"</span>], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=<span style="color:#000000;">True) out_error_list </span>= obj.communicate(<span style="color:#800000;">'</span><span style="color:#800000;">print("hello")</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span>(out_error_list)</pre> </div> <span class="cnblogs_code_collapse">subprocess中的管道通信例子三(简洁遍)</span> </div> </li> </ol> <p>  </p> <h2>初探面向对象与操作实战</h2> <p><span style="font-size:12px;">类是对对象的一些描述,是关于对象的蓝图,它包含方法、属性或者叫成员。</span></p> <p><span style="font-size:12px;">面向对象的目标是代码重用,可以把属性和函数封装到一个类中,也可以把它当作工厂,在每次创建对象时可以确保该对象有相同的属性和方法。</span><span style="font-size:12px;">比如以下例子:创建了一个obj 的对象,这个对象的类型是SQLHelper类,如果基于这个类把 hhost, uusername 和pwd 的参数赋值,此时你可以调用刚才这些参数,然后通过函数打印出来</span></p> <div class="cnblogs_code"> <img id="code_img_closed_07075636-d86c-447d-9043-28965827fc8e" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_07075636-d86c-447d-9043-28965827fc8e" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_07075636-d86c-447d-9043-28965827fc8e" class="cnblogs_code_hide"> <pre>>>> <span style="color:#0000ff;">class</span><span style="color:#000000;"> SQLHelper: ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> fetch(self,sql): ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.hhost) ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.uusername) ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.pwd) ... </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(sql) ... ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> create(self,sql): ... </span><span style="color:#0000ff;">pass</span><span style="color:#000000;"> ... ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> remove(self,nid): ... </span><span style="color:#0000ff;">pass</span><span style="color:#000000;"> ... ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> modify(self,name): ... </span><span style="color:#0000ff;">pass</span><span style="color:#000000;"> ... </span>>>> obj =<span style="color:#000000;"> SQLHelper() </span>>>> obj.hhost = <span style="color:#800000;">'</span><span style="color:#800000;">cl.salt.com</span><span style="color:#800000;">'</span> >>> obj.uusername = <span style="color:#800000;">'</span><span style="color:#800000;">janice</span><span style="color:#800000;">'</span> >>> obj.pwd = <span style="color:#800000;">'</span><span style="color:#800000;">123</span><span style="color:#800000;">'</span> >>> obj.fetch(<span style="color:#800000;">"</span><span style="color:#800000;">Select * from A</span><span style="color:#800000;">"</span><span style="color:#000000;">) cl.salt.com janice </span>123<span style="color:#000000;"> Select </span>* <span style="color:#0000ff;">from</span> A</pre> </div> <span class="cnblogs_code_collapse">面向对象编程一</span> </div> <p><span style="font-size:12px;">优化以上的代码,如果 host, username 和 pwd 是一些共同的参数的话,其实不需要每次创建对象时都把它赋值,因为当创建一个对象的时候,Python 默认会找 __init__ 方法,这个方法我们称之为构造方法。此时,可以把一些通用的参数放在构造方法里,当创建对象时那些参数便会自动创建。</span></p> <div class="cnblogs_code"> <img id="code_img_closed_ffd0586f-7b12-4c1c-a098-78ee991c1bd7" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_ffd0586f-7b12-4c1c-a098-78ee991c1bd7" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_ffd0586f-7b12-4c1c-a098-78ee991c1bd7" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> SQLHelper: </span><span style="color:#008000;">#</span><span style="color:#008000;"> 自动执行</span> <span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">自动执行</span><span style="color:#800000;">"</span><span style="color:#000000;">) self.hhost </span>= <span style="color:#800000;">'</span><span style="color:#800000;">cl.salt.com</span><span style="color:#800000;">'</span><span style="color:#000000;"> self.uusername </span>= <span style="color:#800000;">'</span><span style="color:#800000;">janice</span><span style="color:#800000;">'</span><span style="color:#000000;"> self.pwd </span>= <span style="color:#800000;">'</span><span style="color:#800000;">123</span><span style="color:#800000;">'</span> <span style="color:#008000;">#</span><span style="color:#008000;"> self 是一个形式参数,是自动会给传值的参数</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> fetch(self,sql): </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.hhost) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.uusername) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.pwd) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(sql) </span><span style="color:#008000;">#</span><span style="color:#008000;"> 在类里的函数称之为方法</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> create(self,sql): </span><span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> remove(self,nid): </span><span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> modify(self,name): </span><span style="color:#0000ff;">pass</span><span style="color:#000000;"> obj1 </span>=<span style="color:#000000;"> SQLHelper() </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj1.hhost) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj1.uusername) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj1.pwd) </span><span style="color:#800000;">"""</span><span style="color:#800000;"> 自动执行 cl.salt.com janice 123 </span><span style="color:#800000;">"""</span></pre> </div> <span class="cnblogs_code_collapse">面向对象编程(优化版)无参数遍</span> </div> <p><span style="font-size:12px;">如果每个对象的 hhost, uusername 和 pwd 都不一样的话,也可以在创建对象时,通过传入参数的方式,把数值传进去</span></p> <div class="cnblogs_code"> <img id="code_img_closed_d30632b7-fa43-4176-8b31-e48db9161466" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_d30632b7-fa43-4176-8b31-e48db9161466" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_d30632b7-fa43-4176-8b31-e48db9161466" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> SQLHelper: </span><span style="color:#008000;">#</span><span style="color:#008000;"> 自动执行</span> <span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self,a1,a2,a3): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">自动执行</span><span style="color:#800000;">"</span><span style="color:#000000;">) self.hhost </span>=<span style="color:#000000;"> a1 self.uusername </span>=<span style="color:#000000;"> a2 self.pwd </span>=<span style="color:#000000;"> a3 </span><span style="color:#008000;">#</span><span style="color:#008000;"> self 是一个形式参数,是自动会给传值的参数</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> fetch(self,sql): </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.hhost) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.uusername) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(self.pwd) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(sql) </span><span style="color:#008000;">#</span><span style="color:#008000;"> 在类里的函数称之为方法</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> create(self,sql): </span><span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> remove(self,nid): </span><span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> modify(self,name): </span><span style="color:#0000ff;">pass</span><span style="color:#000000;"> obj1 </span>= SQLHelper(<span style="color:#800000;">'</span><span style="color:#800000;">cl.salt.com</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">janice</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">123</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj1.hhost) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj1.uusername) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj1.pwd) obj2 </span>= SQLHelper(<span style="color:#800000;">'</span><span style="color:#800000;">c2.salt.com</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">alex</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">999</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj2.hhost) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj2.uusername) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj2.pwd) </span><span style="color:#800000;">"""</span><span style="color:#800000;"> 自动执行 cl.salt.com janice 123 自动执行 c2.salt.com alex 999 </span><span style="color:#800000;">"""</span></pre> </div> <span class="cnblogs_code_collapse">面向对象编程(优化版)有参数遍</span> </div> <p><span style="font-size:12px;">面向对象编程的三大特性:</span></p> <ul> <li><span style="font-size:12px;">封装</span></li> <li><span style="font-size:12px;">继承</span></li> <li><span style="font-size:12px;">多态</span></li> </ul> <h3>封装</h3> <p><span style="font-size:12px;">在对象里是可以封装任意类型的数据</span></p> <div class="cnblogs_code"> <img id="code_img_closed_c3603e3f-0838-427f-8f6d-56c664672cc8" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_c3603e3f-0838-427f-8f6d-56c664672cc8" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_c3603e3f-0838-427f-8f6d-56c664672cc8" class="cnblogs_code_hide"> <pre>>>> <span style="color:#0000ff;">class</span><span style="color:#000000;"> f1(): ... </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self,name,obj): ... self.name </span>=<span style="color:#000000;"> name ... self.obj</span>=<span style="color:#000000;">obj ... </span>>>> <span style="color:#0000ff;">class</span><span style="color:#000000;"> f2(): ... </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self,name,age): ... self.name </span>=<span style="color:#000000;"> name ... self.age </span>=<span style="color:#000000;"> age ... </span>>>> f2_obj = f2(<span style="color:#800000;">"</span><span style="color:#800000;">alex</span><span style="color:#800000;">"</span>,11<span style="color:#000000;">) </span>>>> f1_obj = f1(<span style="color:#800000;">"</span><span style="color:#800000;">aa</span><span style="color:#800000;">"</span><span style="color:#000000;">,f2_obj) </span>>>> <span style="color:#0000ff;">print</span><span style="color:#000000;">(f1_obj.obj.age) </span>11 >>> <span style="color:#0000ff;">print</span><span style="color:#000000;">(f1_obj.obj.name) alex </span>>>> <span style="color:#0000ff;">print</span><span style="color:#000000;">(f1_obj.name) aa</span></pre> </div> <span class="cnblogs_code_collapse">对象里封装任意类型的数据</span>  </div> <p><span style="font-size:12px;">课堂练习</span></p> <div class="cnblogs_code"> <img id="code_img_closed_db2dda52-ac3b-4f92-a998-2618c3bd54c3" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_db2dda52-ac3b-4f92-a998-2618c3bd54c3" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_db2dda52-ac3b-4f92-a998-2618c3bd54c3" class="cnblogs_code_hide"> <pre><span style="color:#000000;">练习一:在终端输出如下信息 小明,10岁,男,上山去砍柴 小明,10岁,男,开车去东北 小明,10岁,男,最爱大保健 老李,90岁,男,上山去砍柴 老李,90岁,男,开车去东北 老李,90岁,男,最爱大保健</span></pre> </div> <span class="cnblogs_code_collapse">练习一:在终端输出如下信息</span> </div> <div class="cnblogs_code"> <img id="code_img_closed_5b30e09d-f554-4b15-a23a-273b1df2958f" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_5b30e09d-f554-4b15-a23a-273b1df2958f" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_5b30e09d-f554-4b15-a23a-273b1df2958f" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> Person: </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self,name,age,gender): self.name</span>=<span style="color:#000000;">name self.age</span>=<span style="color:#000000;">age self.gender</span>=<span style="color:#000000;">gender </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> chop_the_wood(self): </span><span style="color:#0000ff;">return</span> <span style="color:#800000;">"</span><span style="color:#800000;">上山去砍柴</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> driving(self): </span><span style="color:#0000ff;">return</span> <span style="color:#800000;">"</span><span style="color:#800000;">开车去东北</span><span style="color:#800000;">"</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> keep_fit(self): </span><span style="color:#0000ff;">return</span> <span style="color:#800000;">"</span><span style="color:#800000;">最爱大保健</span><span style="color:#800000;">"</span><span style="color:#000000;"> p1 </span>= Person(<span style="color:#800000;">'</span><span style="color:#800000;">小明</span><span style="color:#800000;">'</span>,10,<span style="color:#800000;">'</span><span style="color:#800000;">男</span><span style="color:#800000;">'</span><span style="color:#000000;">) p2 </span>= Person(<span style="color:#800000;">'</span><span style="color:#800000;">老李</span><span style="color:#800000;">'</span>,90,<span style="color:#800000;">'</span><span style="color:#800000;">男</span><span style="color:#800000;">'</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span>(p1.name + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + str(p1.age) + <span style="color:#800000;">"</span><span style="color:#800000;">岁,</span><span style="color:#800000;">"</span> + p1.gender + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> p1.chop_the_wood()) </span><span style="color:#0000ff;">print</span>(p1.name + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + str(p1.age) + <span style="color:#800000;">"</span><span style="color:#800000;">岁,</span><span style="color:#800000;">"</span> + p1.gender + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> p1.driving()) </span><span style="color:#0000ff;">print</span>(p1.name + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + str(p1.age) + <span style="color:#800000;">"</span><span style="color:#800000;">岁,</span><span style="color:#800000;">"</span> + p1.gender + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> p1.keep_fit()) </span><span style="color:#0000ff;">print</span>(p2.name + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + str(p2.age) + <span style="color:#800000;">"</span><span style="color:#800000;">岁,</span><span style="color:#800000;">"</span> + p2.gender + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> p2.chop_the_wood()) </span><span style="color:#0000ff;">print</span>(p2.name + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + str(p2.age) + <span style="color:#800000;">"</span><span style="color:#800000;">岁,</span><span style="color:#800000;">"</span> + p2.gender + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> +<span style="color:#000000;"> p2.driving()) </span><span style="color:#0000ff;">print</span>(p2.name + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + str(p2.age) + <span style="color:#800000;">"</span><span style="color:#800000;">岁,</span><span style="color:#800000;">"</span> + p2.gender + <span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span> + p2.keep_fit())</pre> </div> <span class="cnblogs_code_collapse">练习一代码</span>  </div> <div class="cnblogs_code"> <img id="code_img_closed_4ab8cf46-f550-4ff8-b62d-9d92d1b19103" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_4ab8cf46-f550-4ff8-b62d-9d92d1b19103" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_4ab8cf46-f550-4ff8-b62d-9d92d1b19103" class="cnblogs_code_hide"> <pre>1<span style="color:#000000;">、创建三个游戏人物,分别是: 苍井井,女,</span>18<span style="color:#000000;">,初始战斗力1000 东尼木木,男,</span>20<span style="color:#000000;">,初始战斗力1800 波多多,女,</span>19<span style="color:#000000;">,初始战斗力2500 </span>2<span style="color:#000000;">、游戏场景,分别: 草丛战斗,消耗200战斗力 自我修炼,增长100战斗力 多人游戏,消耗500战斗力</span></pre> </div> <span class="cnblogs_code_collapse">练习二:游戏人生程序</span> </div> <div class="cnblogs_code"> <img id="code_img_closed_121b5376-744c-47af-b6cd-416246d17761" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_121b5376-744c-47af-b6cd-416246d17761" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_121b5376-744c-47af-b6cd-416246d17761" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> Avatar: </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self,name,gender,age,initial_value): self.gender </span>=<span style="color:#000000;"> gender self.name </span>=<span style="color:#000000;"> name self.age </span>=<span style="color:#000000;"> age self.initial_value </span>=<span style="color:#000000;"> initial_value </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> 草丛战斗(self,avatar): self.initial_value </span>-= 200<span style="color:#000000;"> avatar.initial_value </span>-= 200 <span style="color:#0000ff;">def</span><span style="color:#000000;"> 自我修炼(self): self.initial_value </span>+= 100 <span style="color:#0000ff;">return</span><span style="color:#000000;"> self.initial_value </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> 多人游戏(self,avatar): self.initial_value </span>-= 500<span style="color:#000000;"> avatar.initial_value </span>-= 500<span style="color:#000000;"> avatar1 </span>= Avatar(<span style="color:#800000;">'</span><span style="color:#800000;">苍井井</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">女</span><span style="color:#800000;">'</span>,18,1000<span style="color:#000000;">) avatar2 </span>= Avatar(<span style="color:#800000;">'</span><span style="color:#800000;">东尼木木</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">男</span><span style="color:#800000;">'</span>,20,1800<span style="color:#000000;">) avatar3 </span>= Avatar(<span style="color:#800000;">'</span><span style="color:#800000;">波多多</span><span style="color:#800000;">'</span>,<span style="color:#800000;">'</span><span style="color:#800000;">女</span><span style="color:#800000;">'</span>,19,2500<span style="color:#000000;">) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">{}|{}|{}|{}</span><span style="color:#800000;">"</span><span style="color:#000000;">.format(avatar1.name,avatar1.age,avatar1.gender,avatar1.initial_value)) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">{}|{}|{}|{}</span><span style="color:#800000;">"</span><span style="color:#000000;">.format(avatar2.name,avatar2.age,avatar2.gender,avatar2.initial_value)) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">{}|{}|{}|{}</span><span style="color:#800000;">"</span><span style="color:#000000;">.format(avatar3.name,avatar3.age,avatar3.gender,avatar3.initial_value)) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">草丛战斗</span><span style="color:#800000;">"</span>.center(50,<span style="color:#800000;">"</span><span style="color:#800000;">-</span><span style="color:#800000;">"</span><span style="color:#000000;">)) avatar1.草丛战斗(avatar2) avatar1.草丛战斗(avatar2) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(avatar1.initial_value) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(avatar2.initial_value) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">自我修炼</span><span style="color:#800000;">"</span>.center(50,<span style="color:#800000;">"</span><span style="color:#800000;">-</span><span style="color:#800000;">"</span><span style="color:#000000;">)) avatar1.自我修炼() avatar1.自我修炼() </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(avatar1.initial_value) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(avatar2.initial_value) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">多人游戏</span><span style="color:#800000;">"</span>.center(50,<span style="color:#800000;">"</span><span style="color:#800000;">-</span><span style="color:#800000;">"</span><span style="color:#000000;">)) avatar1.多人游戏(avatar2) avatar1.多人游戏(avatar2) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(avatar1.initial_value) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(avatar2.initial_value) </span><span style="color:#800000;">"""</span><span style="color:#800000;"> 苍井井|18|女|1000 东尼木木|20|男|1800 波多多|19|女|2500 -----------------------草丛战斗----------------------- 600 1400 -----------------------自我修炼----------------------- 800 1400 -----------------------多人游戏----------------------- -200 400 </span><span style="color:#800000;">"""</span></pre> </div> <span class="cnblogs_code_collapse">练习二代码</span>  </div> <h3>继承</h3> <p><span style="font-size:12px;">继承在面向对象编程里是一个很重要的特性,子类可以继承着父类的所有功能,这好比现实生活中父亲与儿子,儿子会继承着父亲的基因。继承也有分单继承和多继承,以下是一个单继承的例子。</span></p> <div class="cnblogs_Highlighter"> <pre><code class="language-python">class F2(F1) # F2是儿子,F1是父亲</code></pre> </div> <div class="cnblogs_code"> <img id="code_img_closed_894c06cf-d724-4f4b-9578-40174756f741" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_894c06cf-d724-4f4b-9578-40174756f741" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_894c06cf-d724-4f4b-9578-40174756f741" class="cnblogs_code_hide"> <pre>>>> <span style="color:#0000ff;">class</span><span style="color:#000000;"> S1: ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> F1(self): ... self.F2() ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> F2(self): ... </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">S1的F2</span><span style="color:#800000;">"</span><span style="color:#000000;">) ... </span>>>> <span style="color:#0000ff;">class</span><span style="color:#000000;"> S2(S1): ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> F3(self): ... self.F1() ... </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> F2(self): ... </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">S2的F2</span><span style="color:#800000;">"</span><span style="color:#000000;">) ... </span>>>> s2 =<span style="color:#000000;"> S2() </span>>>><span style="color:#000000;"> s2.F3() S2的F2 </span>>>> s1 =<span style="color:#000000;"> S1() </span>>>><span style="color:#000000;"> s1.F1() S1的F2</span></pre> </div> <span class="cnblogs_code_collapse">单继承例子</span> </div> <p><span style="font-size:12px;">多继承意味着一个子类可以有多于一个的父类,然后父亲们的所有功能,儿子都会继承着,以下是一个多继承的例子。<br></span></p> <div class="cnblogs_Highlighter"> <pre><code class="language-python">class F3(F2,F1) # F3是儿子,F1和F2是父亲</code></pre> </div> <div class="cnblogs_code"> <img id="code_img_closed_64f4b437-7df0-4f11-be7f-6e132331f064" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_64f4b437-7df0-4f11-be7f-6e132331f064" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_64f4b437-7df0-4f11-be7f-6e132331f064" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> C0: </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f2(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">C0</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> C1(C0): </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f1(self): </span><span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> C2: </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f2(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">C2</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> C3(C1,C2): </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f3(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">f3.....</span><span style="color:#800000;">"</span><span style="color:#000000;">) c3 </span>=<span style="color:#000000;"> C3() c3.f2() </span><span style="color:#0000ff;">print</span>(C3.<span style="color:#800080;">__mro__</span><span style="color:#000000;">) </span><span style="color:#800000;">"""</span><span style="color:#800000;"> C0 (<class '__main__.C3'>, <class '__main__.C1'>, <class '__main__.C0'>, <class '__main__.C2'>, <class 'object'>) </span><span style="color:#800000;">"""</span></pre> </div> <span class="cnblogs_code_collapse">多继承例子一</span> </div> <p><span style="font-size:12px;">在这个例子中,C3继承着C1和C2,跟据Python的规定,假设已经创建了一个C3类型的对象,它的运行顺序应该是:</span></p> <ol> <li><span style="font-size:12px;">从对象本身的类里找__init__构造方法</span></li> <li><span style="font-size:12px;">然后这里调用了c3.f2( ) 这个方法,此时,Python会从左往右一直往上跟着父子的继承关系找F2( ) 这个方法。</span></li> <li><span style="font-size:12px;">在下列例子,它会先从C1类里找f2( ),C1有的话它会执行,如果没有的话,因为C1的父类是C0,此时Python会往上到C0类里找f2( )方法而不是往右边的C2类里找 f2( )。</span></li> <li><span style="font-size:12px;">如果所有链条的父类都没有f2( )方法的话,Python此时会在C3的第2个继承父类里找f2( )方法,直到找到为此。<br><a href="http://img.e-com-net.com/image/info8/a61a458ddf7648cf9de2baddf62a5ba5.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/a61a458ddf7648cf9de2baddf62a5ba5.jpg" alt="第七章:Python基础のXML操作和面向对象(一)_第2张图片" width="650" height="359" style="border:1px solid black;"></a></span></li> </ol> <p><em><span style="font-size:12px;">这个是多继承例子,然而继承终点不是指向同一对象,但如果继承的时候,最于都会指向同一个父类的话,程序的运行顺序又会是怎么样呢?</span></em></p> <div class="cnblogs_code"> <img id="code_img_closed_aa18cc43-9887-4873-bf73-5baaf6e0b7aa" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_aa18cc43-9887-4873-bf73-5baaf6e0b7aa" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_aa18cc43-9887-4873-bf73-5baaf6e0b7aa" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> C_2: </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f2(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">C-1</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> C_1(C_2): </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f2(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">C-1</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> C0(C_2): </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f1(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">C0</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> C1(C0): </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f1(self): </span><span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> C2: </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f2(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">C2</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> C3(C1,C2): </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> f3(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">f3.....</span><span style="color:#800000;">"</span><span style="color:#000000;">) c3 </span>=<span style="color:#000000;"> C3() c3.f2() </span><span style="color:#0000ff;">print</span>(C3.<span style="color:#800080;">__mro__</span><span style="color:#000000;">) </span><span style="color:#800000;">"""</span><span style="color:#800000;"> C-1 (<class '__main__.C3'>, <class '__main__.C1'>, <class '__main__.C0'>, <class '__main__.C_2'>, <class '__main__.C2'>, <class 'object'>) </span><span style="color:#800000;">"""</span></pre> </div> <span class="cnblogs_code_collapse">多继承例子二</span> </div> <p><span style="font-size:12px;">在这个例子中,C3继承着C1和C2,然后最终都指向C_2类,跟据Python的规定,假设已经创建了一个C3类型的对象,它的运行顺序应该是:</span></p> <ol> <li><span style="font-size:12px;">从对象本身的类里找__init__构造方法</span></li> <li><span style="font-size:12px;">然后这里调用了c3.f2( ) 这个方法,此时,Python会从左往右一直往上跟着父子的继承关系找f2( ) 这个方法,<strong><span style="color:#993300;">必须注意一点!!!!! 如果遇到共同类的话,它会在C3的第2个继承父类里找f2( )方法,而不是我们上一个例子看到的一直随着链条向上找f2( )</span></strong></span></li> <li><span style="font-size:12px;">在下面例子,它会先从C1类里找f2( )方法,C1有的话它就会执行,如果没有的话,因为C1的父类是C0,所以此时的Python会往上到C0类里找f2( )方法而不是直接到C2类里。</span></li> <li><span style="font-size:12px;">但如果C0都没有的话,此时的Python不会再往上到C_2类里找,反而会在C3的第2个继承父类里找f2( )方法,直到找到为此,<span style="color:#993300;"><strong>这是因为两个父类都有指向共同的曾祖父类</strong></span>。<br><a href="http://img.e-com-net.com/image/info8/06b3f35792b749f8b34ad31ce5884a39.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/06b3f35792b749f8b34ad31ce5884a39.jpg" alt="第七章:Python基础のXML操作和面向对象(一)_第3张图片" width="650" height="310" style="border:1px solid black;"></a><br></span></li> </ol> <p><span style="font-size:12px;">再来一个例子,这个继承关系的运行顺序必需很清晰,因为在往后读其他框架源码的时候,很多时候都运用了继承技巧,如果不了解的话就很容易在看源码的时候迷失了。</span><span style="font-size:12px;">下面例子假设S1类继承了S2、S3类,然后S2里有一个 process( )方法,S3里有构造方法,S3继承著S4,里面也有一個构造方法和一個 self.run( ) 方法,self.run( )再调用process( )方法。S4最后也继承著S5,S5里有一个 forever( )方法。</span></p> <div class="cnblogs_code"> <img id="code_img_closed_64cf7fa7-c918-45e8-96aa-7a677eb57bd9" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_64cf7fa7-c918-45e8-96aa-7a677eb57bd9" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_64cf7fa7-c918-45e8-96aa-7a677eb57bd9" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> s5: </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> forever(self): self.run() </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> s4(s5): </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self): self.run() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> run(self): self.process() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> process(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">s4-process</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> s3(s4): </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self): self.name</span>=<span style="color:#800000;">'</span><span style="color:#800000;">s3-name</span><span style="color:#800000;">'</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> s2: </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> process(self): </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">s2-process...</span><span style="color:#800000;">"</span><span style="color:#000000;">) </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> s1(s2,s3): </span><span style="color:#0000ff;">pass</span><span style="color:#000000;"> obj </span>=<span style="color:#000000;"> s1() </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(obj.name) obj.forever() </span><span style="color:#0000ff;">print</span>(s1.<span style="color:#800080;">__mro__</span><span style="color:#000000;">) </span><span style="color:#800000;">"""</span><span style="color:#800000;"> s3-name s2-process... (<class '__main__.s1'>, <class '__main__.s2'>, <class '__main__.s3'>, <class '__main__.s4'>, <class '__main__.s5'>, <class 'object'>) </span><span style="color:#800000;">"""</span></pre> </div> <span class="cnblogs_code_collapse">多继承例子三</span> </div> <p><span style="font-size:12px;">假设已经创建了一个C3类型的对象,它的运行顺序应该是:</span></p> <ol> <li><span style="font-size:12px;">从对象本身的类里找__init__构造方法,如果自身类没有__init__方法的话就到它的父类里找,在s2找不到,然后到s3找,找到第一个__init__方法就可以啦</span></li> <li><span style="font-size:12px;">然后执行第2行代码,obj.forever( ),Python 会首先到S2类找这个方法,没有便到s3类找,也没有会一直往链条的父类去找,直到s5类找到了forever( )方法</span></li> <li><span style="font-size:12px;">执行forever( )方法,不过这个方法是调用对象本身的self.run( )方法,此時會再從 s1类里再找一次 run( )方法:s1 --> s2 --> s3 --> s4 --> s5<br></span></li> <li><span style="font-size:12px;">找到在S4类里的run( )的方法,此时,它又调用process( )方法! (你心里会想是不是又要再重新从S1类再找一遍呀!) 没错!你猜对啦<br></span></li> <li><span style="font-size:12px;">先从S1类找 process( )方法,没有的话会到s2类里找!!!!! 终于找到啦!!!</span></li> <li><span style="font-size:12px;">所以运行后的结果是打印 s2-process 而不是 s4-process<br><a href="http://img.e-com-net.com/image/info8/e9630b94f4ed42c1bc337f2cddb371db.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/e9630b94f4ed42c1bc337f2cddb371db.jpg" alt="第七章:Python基础のXML操作和面向对象(一)_第4张图片" width="650" height="358" style="border:1px solid black;"></a><br></span></li> </ol> <h3><span style="font-size:12px;">Socket编程的例子<br></span></h3> <p><span style="font-size:12px;">刚刚说过很多时候在读源码时都必需透晰理解类之间的继承关系,这里的一个例子就是关于 socket编程:</span></p> <ul> <li><span style="font-size:12px;">创建一个 ThreadingTCPServer( )的类;</span></li> <li><span style="font-size:12px;">执行对象的 serve_forever( )方法</span></li> </ul> <div class="cnblogs_Highlighter"> <pre><code class="language-python">import socketserver obj = socketserver.ThreadingTCPServer() #命令一 obj.serve_forever() #命令二</code></pre> </div> <p><span style="font-size:12px;">下面是创建一个 ThreadingTCPServer( ) 类的图解和运行顺序:</span></p> <ol> <li><span style="font-size:12px;"><span style="font-size:12px;">首先是创建了一个 socketserver.ThreadingTCPServer 的对象叫 obj,紧接着是找他的 __init__( )方法,下面是它的类,这里是一个空值。<br><a href="http://img.e-com-net.com/image/info8/57d663b5fce24ab2b69895335701c741.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/57d663b5fce24ab2b69895335701c741.jpg" alt="第七章:Python基础のXML操作和面向对象(一)_第5张图片" width="650" height="192" style="border:1px solid black;"></a><br></span></span> <div class="cnblogs_Highlighter"> <pre><code class="language-python">class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass</code></pre> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">ThreadingTCPServer 继承者两个类,一个是 ThreadingMixIn,另外一个是 TCPServer,當在自己本身找不到 __init__( )方法,就會到它的父類去找 (ThreadingMixIn 或者 TCPServer),这里最终在 TCPServer 这个类里找到了 __init__( ) 方法,到這里命令一就完整啦<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_0f0d7e53-227a-4d2e-8655-5d85fdf49333" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_0f0d7e53-227a-4d2e-8655-5d85fdf49333" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_0f0d7e53-227a-4d2e-8655-5d85fdf49333" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span>(self, server_address, RequestHandlerClass, bind_and_activate=<span style="color:#000000;">True): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Constructor. May be extended, do not override.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> BaseServer.</span><span style="color:#800080;">__init__</span><span style="color:#000000;">(self, server_address, RequestHandlerClass) self.socket </span>=<span style="color:#000000;"> socket.socket(self.address_family, self.socket_type) </span><span style="color:#0000ff;">if</span><span style="color:#000000;"> bind_and_activate: </span><span style="color:#0000ff;">try</span><span style="color:#000000;">: self.server_bind() self.server_activate() </span><span style="color:#0000ff;">except</span><span style="color:#000000;">: self.server_close() </span><span style="color:#0000ff;">raise</span></pre> </div> <span class="cnblogs_code_collapse">TCPServer里的__init__( )方法</span> </div> </li> <li><span style="font-size:12px;"><span style="font-size:12px;">现在是执行命令二:执行 obj.serve_forever( )方法,还是会先从 ThreadingTCPServer 类里找 serve_forever( )方法,但它并不存在,然后开始到它的父类,即 ThreadingMixIn 类里找,也会找不到发现 serve_forever( )方法。<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_24aa92d1-8c58-4758-a1ab-dcbe0c8004f3" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_24aa92d1-8c58-4758-a1ab-dcbe0c8004f3" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_24aa92d1-8c58-4758-a1ab-dcbe0c8004f3" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> ThreadingMixIn: </span><span style="color:#800000;">"""</span><span style="color:#800000;">Mix-in class to handle each request in a new thread.</span><span style="color:#800000;">"""</span> <span style="color:#008000;">#</span><span style="color:#008000;"> Decides how threads will act upon termination of the</span> <span style="color:#008000;">#</span><span style="color:#008000;"> main process</span> daemon_threads =<span style="color:#000000;"> False </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> process_request_thread(self, request, client_address): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Same as in BaseServer but as a thread. In addition, exception handling is done here. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">try</span><span style="color:#000000;">: self.finish_request(request, client_address) self.shutdown_request(request) </span><span style="color:#0000ff;">except</span><span style="color:#000000;">: self.handle_error(request, client_address) self.shutdown_request(request) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> process_request(self, request, client_address): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Start a new thread to process the request.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> t </span>= threading.Thread(target =<span style="color:#000000;"> self.process_request_thread, args </span>=<span style="color:#000000;"> (request, client_address)) t.daemon </span>=<span style="color:#000000;"> self.daemon_threads t.start()</span></pre> </div> <span class="cnblogs_code_collapse">class ThreadingMixIn 源码</span> </div> </li> <li><span style="font-size:12px;">此时会到它的第2个父类里找:TCPSever 类</span><br> <div class="cnblogs_code"> <img id="code_img_closed_3945c26d-d2c1-4d2a-ad7a-4801a6d5541d" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_3945c26d-d2c1-4d2a-ad7a-4801a6d5541d" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_3945c26d-d2c1-4d2a-ad7a-4801a6d5541d" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> TCPServer(BaseServer): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Base class for various socket-based server classes. Defaults to synchronous IP stream (i.e., TCP). Methods for the caller: - __init__(server_address, RequestHandlerClass, bind_and_activate=True) - serve_forever(poll_interval=0.5) - shutdown() - handle_request() # if you don't use serve_forever() - fileno() -> int # for selector Methods that may be overridden: - server_bind() - server_activate() - get_request() -> request, client_address - handle_timeout() - verify_request(request, client_address) - process_request(request, client_address) - shutdown_request(request) - close_request(request) - handle_error() Methods for derived classes: - finish_request(request, client_address) Class variables that may be overridden by derived classes or instances: - timeout - address_family - socket_type - request_queue_size (only for stream sockets) - allow_reuse_address Instance variables: - server_address - RequestHandlerClass - socket </span><span style="color:#800000;">"""</span><span style="color:#000000;"> address_family </span>=<span style="color:#000000;"> socket.AF_INET socket_type </span>=<span style="color:#000000;"> socket.SOCK_STREAM request_queue_size </span>= 5<span style="color:#000000;"> allow_reuse_address </span>=<span style="color:#000000;"> False </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span>(self, server_address, RequestHandlerClass, bind_and_activate=<span style="color:#000000;">True): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Constructor. May be extended, do not override.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> BaseServer.</span><span style="color:#800080;">__init__</span><span style="color:#000000;">(self, server_address, RequestHandlerClass) self.socket </span>=<span style="color:#000000;"> socket.socket(self.address_family, self.socket_type) </span><span style="color:#0000ff;">if</span><span style="color:#000000;"> bind_and_activate: </span><span style="color:#0000ff;">try</span><span style="color:#000000;">: self.server_bind() self.server_activate() </span><span style="color:#0000ff;">except</span><span style="color:#000000;">: self.server_close() </span><span style="color:#0000ff;">raise</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> server_bind(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called by constructor to bind the socket. May be overridden. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">if</span><span style="color:#000000;"> self.allow_reuse_address: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, </span>1<span style="color:#000000;">) self.socket.bind(self.server_address) self.server_address </span>=<span style="color:#000000;"> self.socket.getsockname() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> server_activate(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called by constructor to activate the server. May be overridden. </span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.socket.listen(self.request_queue_size) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> server_close(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called to clean-up the server. May be overridden. </span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.socket.close() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> fileno(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Return socket file number. Interface required by selector. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">return</span><span style="color:#000000;"> self.socket.fileno() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> get_request(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Get the request and client address from the socket. May be overridden. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">return</span><span style="color:#000000;"> self.socket.accept() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> shutdown_request(self, request): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called to shutdown and close an individual request.</span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">try</span><span style="color:#000000;">: </span><span style="color:#008000;">#</span><span style="color:#008000;">explicitly shutdown. socket.close() merely releases</span> <span style="color:#008000;">#</span><span style="color:#008000;">the socket and waits for GC to perform the actual close.</span> <span style="color:#000000;"> request.shutdown(socket.SHUT_WR) </span><span style="color:#0000ff;">except</span><span style="color:#000000;"> OSError: </span><span style="color:#0000ff;">pass</span> <span style="color:#008000;">#</span><span style="color:#008000;">some platforms may raise ENOTCONN here</span> <span style="color:#000000;"> self.close_request(request) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> close_request(self, request): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called to clean up an individual request.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> request.close()</span></pre> </div> <span class="cnblogs_code_collapse">class TCPServer 源码</span> </div> </li> <li><span style="font-size:12px;">也是找不到,TCPServer 也有一个父类叫 BaseServer</span><br> <div class="cnblogs_code"> <img id="code_img_closed_3ea4ffca-1df5-4342-92cb-c824c343a636" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_3ea4ffca-1df5-4342-92cb-c824c343a636" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_3ea4ffca-1df5-4342-92cb-c824c343a636" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> BaseServer: </span><span style="color:#800000;">"""</span><span style="color:#800000;">Base class for server classes. Methods for the caller: - __init__(server_address, RequestHandlerClass) - serve_forever(poll_interval=0.5) - shutdown() - handle_request() # if you do not use serve_forever() - fileno() -> int # for selector Methods that may be overridden: - server_bind() - server_activate() - get_request() -> request, client_address - handle_timeout() - verify_request(request, client_address) - server_close() - process_request(request, client_address) - shutdown_request(request) - close_request(request) - service_actions() - handle_error() Methods for derived classes: - finish_request(request, client_address) Class variables that may be overridden by derived classes or instances: - timeout - address_family - socket_type - allow_reuse_address Instance variables: - RequestHandlerClass - socket </span><span style="color:#800000;">"""</span><span style="color:#000000;"> timeout </span>=<span style="color:#000000;"> None </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self, server_address, RequestHandlerClass): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Constructor. May be extended, do not override.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.server_address </span>=<span style="color:#000000;"> server_address self.RequestHandlerClass </span>=<span style="color:#000000;"> RequestHandlerClass self.</span><span style="color:#800080;">__is_shut_down</span> =<span style="color:#000000;"> threading.Event() self.</span><span style="color:#800080;">__shutdown_request</span> =<span style="color:#000000;"> False </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> server_activate(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called by constructor to activate the server. May be overridden. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span> serve_forever(self, poll_interval=0.5<span style="color:#000000;">): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Handle one request at a time until shutdown. Polls for shutdown every poll_interval seconds. Ignores self.timeout. If you need to do periodic tasks, do them in another thread. </span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.</span><span style="color:#800080;">__is_shut_down</span><span style="color:#000000;">.clear() </span><span style="color:#0000ff;">try</span><span style="color:#000000;">: </span><span style="color:#008000;">#</span><span style="color:#008000;"> XXX: Consider using another file descriptor or connecting to the</span> <span style="color:#008000;">#</span><span style="color:#008000;"> socket to wake this up instead of polling. Polling reduces our</span> <span style="color:#008000;">#</span><span style="color:#008000;"> responsiveness to a shutdown request and wastes cpu at all other</span> <span style="color:#008000;">#</span><span style="color:#008000;"> times.</span> <span style="color:#000000;"> with _ServerSelector() as selector: selector.register(self, selectors.EVENT_READ) </span><span style="color:#0000ff;">while</span> <span style="color:#0000ff;">not</span> self.<span style="color:#800080;">__shutdown_request</span><span style="color:#000000;">: ready </span>=<span style="color:#000000;"> selector.select(poll_interval) </span><span style="color:#0000ff;">if</span><span style="color:#000000;"> ready: self._handle_request_noblock() self.service_actions() </span><span style="color:#0000ff;">finally</span><span style="color:#000000;">: self.</span><span style="color:#800080;">__shutdown_request</span> =<span style="color:#000000;"> False self.</span><span style="color:#800080;">__is_shut_down</span><span style="color:#000000;">.set() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> shutdown(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Stops the serve_forever loop. Blocks until the loop has finished. This must be called while serve_forever() is running in another thread, or it will deadlock. </span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.</span><span style="color:#800080;">__shutdown_request</span> =<span style="color:#000000;"> True self.</span><span style="color:#800080;">__is_shut_down</span><span style="color:#000000;">.wait() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> service_actions(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called by the serve_forever() loop. May be overridden by a subclass / Mixin to implement any code that needs to be run during the loop. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">pass</span> <span style="color:#008000;">#</span><span style="color:#008000;"> The distinction between handling, getting, processing and finishing a</span> <span style="color:#008000;">#</span><span style="color:#008000;"> request is fairly arbitrary. Remember:</span> <span style="color:#008000;"># </span> <span style="color:#008000;">#</span><span style="color:#008000;"> - handle_request() is the top-level call. It calls selector.select(),</span> <span style="color:#008000;">#</span><span style="color:#008000;"> get_request(), verify_request() and process_request()</span> <span style="color:#008000;">#</span><span style="color:#008000;"> - get_request() is different for stream or datagram sockets</span> <span style="color:#008000;">#</span><span style="color:#008000;"> - process_request() is the place that may fork a new process or create a</span> <span style="color:#008000;">#</span><span style="color:#008000;"> new thread to finish the request</span> <span style="color:#008000;">#</span><span style="color:#008000;"> - finish_request() instantiates the request handler class; this</span> <span style="color:#008000;">#</span><span style="color:#008000;"> constructor will handle the request all by itself</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> handle_request(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Handle one request, possibly blocking. Respects self.timeout. </span><span style="color:#800000;">"""</span> <span style="color:#008000;">#</span><span style="color:#008000;"> Support people who used socket.settimeout() to escape</span> <span style="color:#008000;">#</span><span style="color:#008000;"> handle_request before self.timeout was available.</span> timeout =<span style="color:#000000;"> self.socket.gettimeout() </span><span style="color:#0000ff;">if</span> timeout <span style="color:#0000ff;">is</span><span style="color:#000000;"> None: timeout </span>=<span style="color:#000000;"> self.timeout </span><span style="color:#0000ff;">elif</span> self.timeout <span style="color:#0000ff;">is</span> <span style="color:#0000ff;">not</span><span style="color:#000000;"> None: timeout </span>=<span style="color:#000000;"> min(timeout, self.timeout) </span><span style="color:#0000ff;">if</span> timeout <span style="color:#0000ff;">is</span> <span style="color:#0000ff;">not</span><span style="color:#000000;"> None: deadline </span>= time() +<span style="color:#000000;"> timeout </span><span style="color:#008000;">#</span><span style="color:#008000;"> Wait until a request arrives or the timeout expires - the loop is</span> <span style="color:#008000;">#</span><span style="color:#008000;"> necessary to accommodate early wakeups due to EINTR.</span> <span style="color:#000000;"> with _ServerSelector() as selector: selector.register(self, selectors.EVENT_READ) </span><span style="color:#0000ff;">while</span><span style="color:#000000;"> True: ready </span>=<span style="color:#000000;"> selector.select(timeout) </span><span style="color:#0000ff;">if</span><span style="color:#000000;"> ready: </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> self._handle_request_noblock() </span><span style="color:#0000ff;">else</span><span style="color:#000000;">: </span><span style="color:#0000ff;">if</span> timeout <span style="color:#0000ff;">is</span> <span style="color:#0000ff;">not</span><span style="color:#000000;"> None: timeout </span>= deadline -<span style="color:#000000;"> time() </span><span style="color:#0000ff;">if</span> timeout <<span style="color:#000000;"> 0: </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> self.handle_timeout() </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> _handle_request_noblock(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Handle one request, without blocking. I assume that selector.select() has returned that the socket is readable before this function was called, so there should be no risk of blocking in get_request(). </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">try</span><span style="color:#000000;">: request, client_address </span>=<span style="color:#000000;"> self.get_request() </span><span style="color:#0000ff;">except</span><span style="color:#000000;"> OSError: </span><span style="color:#0000ff;">return</span> <span style="color:#0000ff;">if</span><span style="color:#000000;"> self.verify_request(request, client_address): </span><span style="color:#0000ff;">try</span><span style="color:#000000;">: self.process_request(request, client_address) </span><span style="color:#0000ff;">except</span><span style="color:#000000;">: self.handle_error(request, client_address) self.shutdown_request(request) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> handle_timeout(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called if no new request arrives within self.timeout. Overridden by ForkingMixIn. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> verify_request(self, request, client_address): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Verify the request. May be overridden. Return True if we should proceed with this request. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">return</span><span style="color:#000000;"> True </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> process_request(self, request, client_address): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Call finish_request. Overridden by ForkingMixIn and ThreadingMixIn. </span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.finish_request(request, client_address) self.shutdown_request(request) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> server_close(self): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called to clean-up the server. May be overridden. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> finish_request(self, request, client_address): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Finish one request by instantiating RequestHandlerClass.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.RequestHandlerClass(request, client_address, self) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> shutdown_request(self, request): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called to shutdown and close an individual request.</span><span style="color:#800000;">"""</span><span style="color:#000000;"> self.close_request(request) </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> close_request(self, request): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Called to clean up an individual request.</span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">pass</span> <span style="color:#0000ff;">def</span><span style="color:#000000;"> handle_error(self, request, client_address): </span><span style="color:#800000;">"""</span><span style="color:#800000;">Handle an error gracefully. May be overridden. The default is to print a traceback and continue. </span><span style="color:#800000;">"""</span> <span style="color:#0000ff;">print</span>(<span style="color:#800000;">'</span><span style="color:#800000;">-</span><span style="color:#800000;">'</span>*40<span style="color:#000000;">) </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">'</span><span style="color:#800000;">Exception happened during processing of request from</span><span style="color:#800000;">'</span>, end=<span style="color:#800000;">'</span> <span style="color:#800000;">'</span><span style="color:#000000;">) </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(client_address) </span><span style="color:#0000ff;">import</span><span style="color:#000000;"> traceback traceback.print_exc() </span><span style="color:#008000;">#</span><span style="color:#008000;"> XXX But this goes to stderr!</span> <span style="color:#0000ff;">print</span>(<span style="color:#800000;">'</span><span style="color:#800000;">-</span><span style="color:#800000;">'</span>*40)</pre> </div> <span class="cnblogs_code_collapse">class BaseServer 源码</span> </div> </li> <li><span style="font-size:12px;"> 在 BaseServer 里找到了 serve_forever( )方法,这个方法又调用了一个叫 self._handle request nonblock(),它又会再调用 self.process_request(request, client_address)。最於我們可以在 ThreadingMixIn 類里找到了 process_request(self, request, client_address) 的方法。 </span></li> </ol> <p><strong><span style="font-size:12px;">Socket编程例子的继承关系图</span></strong></p> <p><a href="http://img.e-com-net.com/image/info8/042f7b3ec58544e29587e083249eb487.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/042f7b3ec58544e29587e083249eb487.jpg" alt="第七章:Python基础のXML操作和面向对象(一)_第6张图片" width="650" height="572" style="border:1px solid black;"></a> </p> <p> </p> <h2>本周作业</h2> <p><span style="font-size:12px;">运用面向对象来写一个选课系统</span></p> <p><span style="font-size:12px;">背景:这是一个 pay-as-you-go 的网上选课系统,学生可以在选该学期想学习的课程,每个课程都有它相对应的课时,学费和难易度,每个老师都有他自己的姓名,爱好,姓别,专长,老师的功能是负责<strong>授课;</strong></span><span style="font-size:12px;">每个学生也有他的姓名,爱好,姓别和技能评分,学生的功能是负责<strong>上课</strong>; 当学生们选完课之后呢,因为每个课程都有学费,所以呢,当老师完整了他任务的时候,可以有收入,相反学生便有同等的支出,程序运行之后可以打印老师X 在该学期的收入和学生X 在该学期的支出。</span></p> <p><span style="font-size:12px;">管理员:</span><br><span style="font-size:12px;">创建老师:姓名、性别、年龄、资产</span><br><span style="font-size:12px;">创建课程:课程名称、上课时间、课时费、关联老师</span><br><span style="font-size:12px;">学生:用户名、密码、性别、年龄、选课列表[]、上课记录{课程1:【di,a,】}</span></p> <ol> <li><span style="font-size:12px;">管理员设置课程信息和老师信息</span></li> <li><span style="font-size:12px;">老师上课获得课时费</span></li> <li><span style="font-size:12px;">学生上课,学到“上课内容”</span></li> <li><span style="font-size:12px;">学生可自选课程</span></li> <li><span style="font-size:12px;">学生可查看已选课程和上课记录</span></li> <li><span style="font-size:12px;">学生可评价老师,差评老师要扣款</span></li> <li><span style="font-size:12px;">使用pickle</span></li> </ol> <p><span style="font-size:12px;"><em>pickle 模块将对象转化为文件保存在磁盘上,在需要的时候再读取并还原,pickle 介绍和操作可以参考这里</em></span></p> <p> </p> <p><strong><span style="font-size:12px;">思路</span></strong></p> <p><span style="font-size:12px;">管理员</span></p> <ul> <li><span style="font-size:12px;"><span style="font-size:12px;">创建老师 (名字、爱好、资产)<br></span></span> <div class="cnblogs_code"> <img id="code_img_closed_48f6df91-2b22-4b2a-92c3-3d73b65d282f" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_48f6df91-2b22-4b2a-92c3-3d73b65d282f" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_48f6df91-2b22-4b2a-92c3-3d73b65d282f" class="cnblogs_code_hide"> <pre><span style="color:#0000ff;">class</span><span style="color:#000000;"> Teacher: </span><span style="color:#0000ff;">def</span> <span style="color:#800080;">__init__</span><span style="color:#000000;">(self,favor,name,age): self.favor </span>=<span style="color:#000000;"> favour self.name </span>=<span style="color:#000000;"> name self.age </span>=<span style="color:#000000;"> age self.asset </span>=<span style="color:#000000;"> 0 </span><span style="color:#0000ff;">def</span><span style="color:#000000;"> teaching_event(self): self.asset </span>= asset - 1 <span style="color:#0000ff;">def</span><span style="color:#000000;"> gain(self,value): self.asset </span>+=<span style="color:#000000;"> value obj1 </span>= Teacher() <span style="color:#008000;">#</span><span style="color:#008000;"> Teacher类型</span> obj2 = Teacher() <span style="color:#008000;">#</span><span style="color:#008000;"> Teacher类型</span> obj3 = Teacher() <span style="color:#008000;">#</span><span style="color:#008000;"> Teacher类型</span> obj4 = Teacher() <span style="color:#008000;">#</span><span style="color:#008000;"> Teacher类型</span> <span style="color:#000000;"> lst </span>=<span style="color:#000000;"> [obj1,obj2,obj3,obj4] pickle.dump(lst)</span></pre> </div> <span class="cnblogs_code_collapse">class Teacher</span> </div> </li> <li><span style="font-size:12px;">创建</span><span style="font-size:12px;">课程</span></li> <li><span style="font-size:12px;">课時类</span> <ul> <li><span style="font-size:12px;">课時名</span></li> <li><span style="font-size:12px;">课时费</span></li> <li><span style="font-size:12px;">负责老师 = li[0]</span></li> </ul></li> <li><span style="font-size:12px;">功能</span> <ul> <li><span style="font-size:12px;">上課</span> <ul> <li><span style="font-size:12px;">返回给学生学习的内容</span></li> <li><span style="font-size:12px;"><span style="font-size:12px;">负责老师掙錢li[0].gain(</span></span><span style="font-size:12px;">课时费)</span></li> </ul></li> <li><span style="font-size:12px;">课程对象</span></li> </ul></li> </ul> <p><span style="font-size:12px;">学生</span></p> <ul> <li><span style="font-size:12px;">類</span></li> <li><span style="font-size:12px;">功能: 選課</span></li> <li><span style="font-size:12px;">上課:生物課 </span></li> <li><span style="font-size:12px;">课程对象.对象</span></li> </ul> <p><span style="font-size:12px;">[知识点:pickle 模块、类的封装]</span></p> <p> </p> <div class="cnblogs_code"> <img id="code_img_closed_5106ca8b-b9e5-4991-8c7f-8562b6cd4831" class="code_img_closed" src="http://img.e-com-net.com/image/info8/b8d97b5613f94ed2ba791cad57d0b2ed.gif" alt="" width="0" height="0"> <img id="code_img_opened_5106ca8b-b9e5-4991-8c7f-8562b6cd4831" class="code_img_opened" src="http://img.e-com-net.com/image/info8/2f88dd3f1cd145f59c0e47b51acdbd4b.gif" alt="" width="0" height="0"> <div id="cnblogs_code_open_5106ca8b-b9e5-4991-8c7f-8562b6cd4831" class="cnblogs_code_hide"> <pre>Username >><span style="color:#000000;"> janice Password </span>>><span style="color:#000000;"> janice123 Login Successfully. Welcome to the IT Academy HI JANICE [Student], PLEASE SELECT ONE ACTION [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 4 -------------YOUR COURSE INFORMATION--------------<span style="color:#000000;"> Class Name: English Date: Sat Total hours: </span>10<span style="color:#000000;"> Price: </span>1000 -------------YOUR ACCOUNT INFORMATION-------------<span style="color:#000000;"> Total Course taken: </span>1<span style="color:#000000;"> Name: Janice Gender: Female Balance: </span>1000<span style="color:#000000;"> [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 3 --------------ALL COURSE INFORMATION--------------<span style="color:#000000;"> Class Name: Programming Date: Fri Total hours: </span>8<span style="color:#000000;"> Price: </span>800<span style="color:#000000;"> Class Name: English Date: Sat Total hours: </span>10<span style="color:#000000;"> Price: </span>1000<span style="color:#000000;"> Class Name: Sales </span><span style="color:#0000ff;">and</span><span style="color:#000000;"> Marketing Date: Sat Total hours: </span>15<span style="color:#000000;"> Price: </span>1500<span style="color:#000000;"> Class Name: Arts </span><span style="color:#0000ff;">and</span><span style="color:#000000;"> Design Date: Sat Total hours: </span>20<span style="color:#000000;"> Price: </span>2000<span style="color:#000000;"> [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 1 ------------------Pick a course-------------------<span style="color:#000000;"> c1. Programming c4. English c2. Sales </span><span style="color:#0000ff;">and</span><span style="color:#000000;"> Marketing c3. Arts </span><span style="color:#0000ff;">and</span><span style="color:#000000;"> Design Which </span><span style="color:#0000ff;">class</span> do you want to <span style="color:#0000ff;">try</span><span style="color:#000000;">? Please pick one </span>>><span style="color:#000000;"> c1 You have selected c1 [c</span>=confirmed][q=<span style="color:#000000;">quit] Confirmed to take the course? </span>>><span style="color:#000000;"> c Programming A new course added to your account [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 4 -------------YOUR COURSE INFORMATION--------------<span style="color:#000000;"> Class Name: English Date: Sat Total hours: </span>10<span style="color:#000000;"> Price: </span>1000<span style="color:#000000;"> Class Name: Programming Date: Fri Total hours: </span>8<span style="color:#000000;"> Price: </span>800 -------------YOUR ACCOUNT INFORMATION-------------<span style="color:#000000;"> Total Course taken: </span>2<span style="color:#000000;"> Name: Janice Gender: Female Balance: </span>1800<span style="color:#000000;"> [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 2<span style="color:#000000;"> Attendng a </span><span style="color:#0000ff;">class</span><span style="color:#000000;">..... [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 2<span style="color:#000000;"> Attendng a </span><span style="color:#0000ff;">class</span><span style="color:#000000;">..... [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 2<span style="color:#000000;"> Attendng a </span><span style="color:#0000ff;">class</span><span style="color:#000000;">..... [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> 4 -------------YOUR COURSE INFORMATION--------------<span style="color:#000000;"> Class Name: English Date: Sat Total hours: </span>10<span style="color:#000000;"> Price: </span>1000<span style="color:#000000;"> Class Name: Programming Date: Fri Total hours: </span>8<span style="color:#000000;"> Price: </span>800 -------------YOUR ACCOUNT INFORMATION-------------<span style="color:#000000;"> Total Course taken: </span>2<span style="color:#000000;"> Name: Janice Gender: Female Balance: </span>1500<span style="color:#000000;"> [q</span>=quit] 1.选课; 2.上课; 3.查看课程信息; 4<span style="color:#000000;">.查看个人课程信息 </span>>> </pre> </div> <span class="cnblogs_code_collapse">程序运行结果</span> </div> <p> </p> <p> </p> <h2>參考資料</h2> <p><span style="font-size:12px;">银角大王:Python开发【第六篇】:模块</span></p> <p><span style="font-size:12px;">银角大王:Python 面向对象(初级篇)</span></p> <p><span style="font-size:12px;">Subprocess </span></p> <p> </p> </div> <p>转载于:https://www.cnblogs.com/jcchoiling/p/5840875.html</p> </div> </div> </div> </div> </div> <!--PC和WAP自适应版--> <div id="SOHUCS" sid="1700750537512792064"></div> <script type="text/javascript" src="/views/front/js/chanyan.js"></script> <!-- 文章页-底部 动态广告位 --> <div class="youdao-fixed-ad" id="detail_ad_bottom"></div> </div> <div class="col-md-3"> <div class="row" id="ad"> <!-- 文章页-右侧1 动态广告位 --> <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_1"> </div> </div> <!-- 文章页-右侧2 动态广告位 --> <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_2"></div> </div> <!-- 文章页-右侧3 动态广告位 --> <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_3"></div> </div> </div> </div> </div> </div> </div> <div class="container"> <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的:(第七章:Python基础のXML操作和面向对象(一))</h4> <div id="paradigm-article-related"> <div class="recommend-post mb30"> <ul class="widget-links"> <li><a href="/article/1835514462770130944.htm" title="斤斤计较的婚姻到底有多难?" target="_blank">斤斤计较的婚姻到底有多难?</a> <span class="text-muted">白心之岂必有为</span> <div>很多人私聊我会问到在哪个人群当中斤斤计较的人最多?我都会回答他,一般婚姻出现问题的斤斤计较的人士会非常多,以我多年经验,在婚姻落的一塌糊涂的人当中,斤斤计较的人数占比在20~30%以上,也就是说10个婚姻出现问题的斤斤计较的人有2-3个有多不减。在婚姻出问题当中,有大量的心理不平衡的、尖酸刻薄的怨妇。在婚姻中仅斤斤计较有两种类型:第一种是物质上的,另一种是精神上的。在物质与精神上抠门已经严重的影响</div> </li> <li><a href="/article/1835514464028422144.htm" title="情绪觉察日记第37天" target="_blank">情绪觉察日记第37天</a> <span class="text-muted">露露_e800</span> <div>今天是家庭关系规划师的第二阶最后一天,慧萍老师帮我做了个案,帮我处理了埋在心底好多年的一份恐惧,并给了我深深的力量!这几天出来学习,爸妈过来婆家帮我带小孩,妈妈出于爱帮我收拾东西,并跟我先生和婆婆产生矛盾,妈妈觉得他们没有照顾好我…。今晚回家见到妈妈,我很欣赏她并赞扬她,妈妈说今晚要跟我睡我说好,当我们俩躺在床上准备睡觉的时候,我握着妈妈的手对她说:妈妈这几天辛苦你了,你看你多利害把我们的家收拾得</div> </li> <li><a href="/article/1835514335561084928.htm" title="芦花鞋一四" target="_blank">芦花鞋一四</a> <span class="text-muted">许叶晗</span> <div>又是在一个寒冷的夏日里,青铜和葵花决定今天一起去卖芦花鞋,奶奶亲手给他们做了一碗热乎乎的粥对他们说:“就靠你们两挣生活费了这碗粥赶紧趁热喝了吧!”于是青铜和葵花喝完了奶奶给她们做的粥,就准备去镇上卖卢花鞋,这回青铜和葵花穿着新的芦花鞋来到了镇上。青铜这回看到了很多人都在卖,用手势表达对葵花说:“这回有好多人在抢我们生意呢!我们必须得吆喝起来。”葵花点了点头。可是谁知他们也大声的叫,卖芦花喽!卖芦花</div> </li> <li><a href="/article/1835514307744460800.htm" title="QQ群采集助手,精准引流必备神器" target="_blank">QQ群采集助手,精准引流必备神器</a> <span class="text-muted">2401_87347160</span> <a class="tag" taget="_blank" href="/search/%E5%85%B6%E4%BB%96/1.htm">其他</a><a class="tag" taget="_blank" href="/search/%E7%BB%8F%E9%AA%8C%E5%88%86%E4%BA%AB/1.htm">经验分享</a> <div>功能概述微信群查找与筛选工具是一款专为微信用户设计的辅助工具,它通过关键词搜索功能,帮助用户快速找到相关的微信群,并提供筛选是否需要验证的群组的功能。主要功能关键词搜索:用户可以输入关键词,工具将自动查找包含该关键词的微信群。筛选功能:工具提供筛选机制,用户可以选择是否只显示需要验证或不需要验证的群组。精准引流:通过上述功能,用户可以更精准地找到目标群组,进行有效的引流操作。3.设备需求该工具可以</div> </li> <li><a href="/article/1835514207114719232.htm" title="关于沟通这件事,项目经理不需要每次都面对面进行" target="_blank">关于沟通这件事,项目经理不需要每次都面对面进行</a> <span class="text-muted">流程大师兄</span> <div>很多项目经理都会遇到这样的问题,项目中由于事情太多,根本没有足够的时间去召开会议,那在这种情况下如何去有效地管理项目中的利益相关者?当然,不建议电子邮件也不需要开会的话,建议可以采取下面几种方式来形成有效的沟通,这几种方式可以帮助你努力的通过各种办法来保持和各方面的联系。项目经理首先要问自己几个问题,项目中哪些利益相关者是必须要进行沟通的?可以列出项目中所有的利益相关者清单,同时也整理出项目中哪些</div> </li> <li><a href="/article/1835513803861749760.htm" title="机器学习与深度学习间关系与区别" target="_blank">机器学习与深度学习间关系与区别</a> <span class="text-muted">ℒℴѵℯ心·动ꦿ໊ོ꫞</span> <a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0/1.htm">学习</a><a class="tag" taget="_blank" href="/search/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/1.htm">深度学习</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>一、机器学习概述定义机器学习(MachineLearning,ML)是一种通过数据驱动的方法,利用统计学和计算算法来训练模型,使计算机能够从数据中学习并自动进行预测或决策。机器学习通过分析大量数据样本,识别其中的模式和规律,从而对新的数据进行判断。其核心在于通过训练过程,让模型不断优化和提升其预测准确性。主要类型1.监督学习(SupervisedLearning)监督学习是指在训练数据集中包含输入</div> </li> <li><a href="/article/1835513701143244800.htm" title="铭刻于星(四十二)" target="_blank">铭刻于星(四十二)</a> <span class="text-muted">随风至</span> <div>69夜晚,绍敏同学做完功课后,看了眼房外,没听到动静才敢从书包的夹层里拿出那个心形纸团。折痕压得很深,都有些旧了,想来是已经写好很久了。绍敏同学慢慢地、轻轻地捏开折叠处,待到全部拆开后,又反复抚平纸张,然后仔细地一字字默看。只是开头的三个字是第一次看到,让她心漏跳了几拍。“亲爱的绍敏:从四年级的时候,我就喜欢你了,但是我一直不敢说,怕影响你学习。六年级的时候听说有人跟你表白,你接受了,我很难过,但</div> </li> <li><a href="/article/1835513570171908096.htm" title="底层逆袭到底有多难,不甘平凡的你准备好了吗?让吴起给你说说" target="_blank">底层逆袭到底有多难,不甘平凡的你准备好了吗?让吴起给你说说</a> <span class="text-muted">造命者说</span> <div>底层逆袭到底有多难,不甘平凡的你准备好了吗?让吴起给你说说我叫吴起,生于公元前440年的战国初期,正是群雄并起、天下纷争不断的时候。后人说我是军事家、政治家、改革家,是兵家代表人物。评价我一生历仕鲁、魏、楚三国,通晓兵家、法家、儒家三家思想,在内政军事上都有极高的成就。周安王二十一年(公元前381年),因变法得罪守旧贵族,被人乱箭射死。我出生在卫国一个“家累万金”的富有家庭,从年轻时候起就不甘平凡</div> </li> <li><a href="/article/1835513571501502464.htm" title="2020-01-25" target="_blank">2020-01-25</a> <span class="text-muted">晴岚85</span> <div>郑海燕坚持分享590天2020.1.24在生活中只存在两个问题。一个问题是:你知道想要达成的目标是什么,但却不知道如何才能达成;另一个问题是:你不知道你的目标是什么。前一个是行动的问题,后一个是结果的问题。通过制定具体的下一步行动,可以解决不知道如何开始行动的问题。而通过去想象结果,对结果做预估,可以解决找不着目标的问题。对于所有吸引我们注意力,想要完成的任务,你可以先想象一下,预期的结果究竟是什</div> </li> <li><a href="/article/1835513568917811200.htm" title="随笔 | 仙一般的灵气" target="_blank">随笔 | 仙一般的灵气</a> <span class="text-muted">海思沧海</span> <div>仙岛今天,我看了你全部,似乎已经进入你的世界我不知道,这是否是梦幻,还是你仙一般的灵气吸引了我也许每一个人都要有一份属于自己的追求,这样才能够符合人生的梦想,生活才能够充满着阳光与快乐我不知道,我为什么会这样的感叹,是在感叹自己的人生,还是感叹自己一直没有孜孜不倦的追求只感觉虚度了光阴,每天活在自己的梦中,活在一个不真实的世界是在逃避自己,还是在逃避周围的一切有时候我嘲笑自己,嘲笑自己如此的虚无,</div> </li> <li><a href="/article/1835513567663714304.htm" title="想家" target="_blank">想家</a> <span class="text-muted">爆米花机</span> <div>也许不同于大家对家乡的思念,我对家乡甚至是疯狂的不舍。还未踏出车站就感觉到幸福,我享受这里的夕阳、这里的浓烈柴火味、这里每一口家常菜。我是宅女,我贪恋家的安逸。刚刚踏出大学校门,初出茅庐,无法适应每年只能国庆和春节回家。我焦虑、失眠、无端发脾气,是无法适应工作的节奏,是无法接受我将一步步离开家乡的事实。我不想承认自己胸无大志,选择再次踏上征程。图片发自App</div> </li> <li><a href="/article/1835513551624695808.htm" title="【iOS】MVC设计模式" target="_blank">【iOS】MVC设计模式</a> <span class="text-muted">Magnetic_h</span> <a class="tag" taget="_blank" href="/search/ios/1.htm">ios</a><a class="tag" taget="_blank" href="/search/mvc/1.htm">mvc</a><a class="tag" taget="_blank" href="/search/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/1.htm">设计模式</a><a class="tag" taget="_blank" href="/search/objective-c/1.htm">objective-c</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0/1.htm">学习</a><a class="tag" taget="_blank" href="/search/ui/1.htm">ui</a> <div>MVC前言如何设计一个程序的结构,这是一门专门的学问,叫做"架构模式"(architecturalpattern),属于编程的方法论。MVC模式就是架构模式的一种。它是Apple官方推荐的App开发架构,也是一般开发者最先遇到、最经典的架构。MVC各层controller层Controller/ViewController/VC(控制器)负责协调Model和View,处理大部分逻辑它将数据从Mod</div> </li> <li><a href="/article/1835513551142350848.htm" title="OC语言多界面传值五大方式" target="_blank">OC语言多界面传值五大方式</a> <span class="text-muted">Magnetic_h</span> <a class="tag" taget="_blank" href="/search/ios/1.htm">ios</a><a class="tag" taget="_blank" href="/search/ui/1.htm">ui</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0/1.htm">学习</a><a class="tag" taget="_blank" href="/search/objective-c/1.htm">objective-c</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>前言在完成暑假仿写项目时,遇到了许多需要用到多界面传值的地方,这篇博客来总结一下比较常用的五种多界面传值的方式。属性传值属性传值一般用前一个界面向后一个界面传值,简单地说就是通过访问后一个视图控制器的属性来为它赋值,通过这个属性来做到从前一个界面向后一个界面传值。首先在后一个界面中定义属性@interfaceBViewController:UIViewController@propertyNSSt</div> </li> <li><a href="/article/1835513440525971456.htm" title="一百九十四章. 自相矛盾" target="_blank">一百九十四章. 自相矛盾</a> <span class="text-muted">巨木擎天</span> <div>唉!就这么一夜,林子感觉就像过了很多天似的,先是回了阳间家里,遇到了那么多不可思议的事情儿。特别是小伙伴们,第二次与自己见面时,僵硬的表情和恐怖的气氛,让自己如坐针毡,打从心眼里难受!还有东子,他现在还好吗?有没有被人欺负?护城河里的小鱼小虾们,还都在吗?水不会真的干枯了吧?那对相亲相爱漂亮的太平鸟儿,还好吧!春天了,到了做窝、下蛋、喂养小鸟宝宝的时候了,希望它们都能够平安啊!虽然没有看见家人,也</div> </li> <li><a href="/article/1835513424734416896.htm" title="UI学习——cell的复用和自定义cell" target="_blank">UI学习——cell的复用和自定义cell</a> <span class="text-muted">Magnetic_h</span> <a class="tag" taget="_blank" href="/search/ui/1.htm">ui</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0/1.htm">学习</a> <div>目录cell的复用手动(非注册)自动(注册)自定义cellcell的复用在iOS开发中,单元格复用是一种提高表格(UITableView)和集合视图(UICollectionView)滚动性能的技术。当一个UITableViewCell或UICollectionViewCell首次需要显示时,如果没有可复用的单元格,则视图会创建一个新的单元格。一旦这个单元格滚动出屏幕,它就不会被销毁。相反,它被添</div> </li> <li><a href="/article/1835512920797179904.htm" title="element实现动态路由+面包屑" target="_blank">element实现动态路由+面包屑</a> <span class="text-muted">软件技术NINI</span> <a class="tag" taget="_blank" href="/search/vue%E6%A1%88%E4%BE%8B/1.htm">vue案例</a><a class="tag" taget="_blank" href="/search/vue.js/1.htm">vue.js</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a> <div>el-breadcrumb是ElementUI组件库中的一个面包屑导航组件,它用于显示当前页面的路径,帮助用户快速理解和导航到应用的各个部分。在Vue.js项目中,如果你已经安装了ElementUI,就可以很方便地使用el-breadcrumb组件。以下是一个基本的使用示例:安装ElementUI(如果你还没有安装的话):你可以通过npm或yarn来安装ElementUI。bash复制代码npmi</div> </li> <li><a href="/article/1835512809883004928.htm" title="10月|愿你的青春不负梦想-读书笔记-01" target="_blank">10月|愿你的青春不负梦想-读书笔记-01</a> <span class="text-muted">Tracy的小书斋</span> <div>本书的作者是俞敏洪,大家都很熟悉他了吧。俞敏洪老师是我行业的领头羊吧,也是我事业上的偶像。本日摘录他书中第一章中的金句:『一个人如果什么目标都没有,就会浑浑噩噩,感觉生命中缺少能量。能给我们能量的,是对未来的期待。第一件事,我始终为了进步而努力。与其追寻全世界的骏马,不如种植丰美的草原,到时骏马自然会来。第二件事,我始终有阶段性的目标。什么东西能给我能量?答案是对未来的期待。』读到这里的时候,我便</div> </li> <li><a href="/article/1835512542735200256.htm" title="C语言宏函数" target="_blank">C语言宏函数</a> <span class="text-muted">南林yan</span> <a class="tag" taget="_blank" href="/search/C%E8%AF%AD%E8%A8%80/1.htm">C语言</a><a class="tag" taget="_blank" href="/search/c%E8%AF%AD%E8%A8%80/1.htm">c语言</a> <div>一、什么是宏函数?通过宏定义的函数是宏函数。如下,编译器在预处理阶段会将Add(x,y)替换为((x)*(y))#defineAdd(x,y)((x)*(y))#defineAdd(x,y)((x)*(y))intmain(){inta=10;intb=20;intd=10;intc=Add(a+d,b)*2;cout<<c<<endl;//800return0;}二、为什么要使用宏函数使用宏函数</div> </li> <li><a href="/article/1835512305320816640.htm" title="地推话术,如何应对地推过程中家长的拒绝" target="_blank">地推话术,如何应对地推过程中家长的拒绝</a> <span class="text-muted">校师学</span> <div>相信校长们在做地推的时候经常遇到这种情况:市场专员反馈家长不接单,咨询师反馈难以邀约这些家长上门,校区地推疲软,招生难。为什么?仅从地推层面分析,一方面因为家长受到的信息轰炸越来越多,对信息越来越“免疫”;而另一方面地推人员的专业能力和营销话术没有提高,无法应对家长的拒绝,对有意向的家长也不知如何跟进,眼睁睁看着家长走远;对于家长的疑问,更不知道如何有技巧地回答,机会白白流失。由于回答没技巧和专业</div> </li> <li><a href="/article/1835512178023690240.htm" title="谢谢你们,爱你们!" target="_blank">谢谢你们,爱你们!</a> <span class="text-muted">鹿游儿</span> <div>昨天家人去泡温泉,二个孩子也带着去,出发前一晚,匆匆下班,赶回家和孩子一起收拾。饭后,我拿出笔和本子(上次去澳门时做手帐的本子)写下了1\2\3\4\5\6\7\8\9,让后让小壹去思考,带什么出发去旅游呢?她在对应的数字旁边画上了,泳衣、泳圈、肖恩、内衣内裤、tapuy、拖鞋……画完后,就让她自己对着这个本子,将要带的,一一带上,没想到这次带的书还是这本《便便工厂》(晚上姑婆发照片过来,妹妹累得</div> </li> <li><a href="/article/1835511911769272320.htm" title="C语言如何定义宏函数?" target="_blank">C语言如何定义宏函数?</a> <span class="text-muted">小九格物</span> <a class="tag" taget="_blank" href="/search/c%E8%AF%AD%E8%A8%80/1.htm">c语言</a> <div>在C语言中,宏函数是通过预处理器定义的,它在编译之前替换代码中的宏调用。宏函数可以模拟函数的行为,但它们不是真正的函数,因为它们在编译时不会进行类型检查,也不会分配存储空间。宏函数的定义通常使用#define指令,后面跟着宏的名称和参数列表,以及宏展开后的代码。宏函数的定义方式:1.基本宏函数:这是最简单的宏函数形式,它直接定义一个表达式。#defineSQUARE(x)((x)*(x))2.带参</div> </li> <li><a href="/article/1835511912192897024.htm" title="微服务下功能权限与数据权限的设计与实现" target="_blank">微服务下功能权限与数据权限的设计与实现</a> <span class="text-muted">nbsaas-boot</span> <a class="tag" taget="_blank" href="/search/%E5%BE%AE%E6%9C%8D%E5%8A%A1/1.htm">微服务</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E6%9E%B6%E6%9E%84/1.htm">架构</a> <div>在微服务架构下,系统的功能权限和数据权限控制显得尤为重要。随着系统规模的扩大和微服务数量的增加,如何保证不同用户和服务之间的访问权限准确、细粒度地控制,成为设计安全策略的关键。本文将讨论如何在微服务体系中设计和实现功能权限与数据权限控制。1.功能权限与数据权限的定义功能权限:指用户或系统角色对特定功能的访问权限。通常是某个用户角色能否执行某个操作,比如查看订单、创建订单、修改用户资料等。数据权限:</div> </li> <li><a href="/article/1835511912843014144.htm" title="理解Gunicorn:Python WSGI服务器的基石" target="_blank">理解Gunicorn:Python WSGI服务器的基石</a> <span class="text-muted">范范0825</span> <a class="tag" taget="_blank" href="/search/ipython/1.htm">ipython</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a> <div>理解Gunicorn:PythonWSGI服务器的基石介绍Gunicorn,全称GreenUnicorn,是一个为PythonWSGI(WebServerGatewayInterface)应用设计的高效、轻量级HTTP服务器。作为PythonWeb应用部署的常用工具,Gunicorn以其高性能和易用性著称。本文将介绍Gunicorn的基本概念、安装和配置,帮助初学者快速上手。1.什么是Gunico</div> </li> <li><a href="/article/1835511669476913152.htm" title="小丽成长记(四十三)" target="_blank">小丽成长记(四十三)</a> <span class="text-muted">玲玲54321</span> <div>小丽发现,即使她好不容易调整好自己的心态下一秒总会有不确定的伤脑筋的事出现,一个接一个的问题,人生就没有停下的时候,小问题不断出现。不过她今天看的书,她接受了人生就是不确定的,厉害的人就是不断创造确定性,在Ta的领域比别人多的确定性就能让自己脱颖而出,显示价值从而获得的比别人多的利益。正是这样的原因,因为从前修炼自己太少,使得她现在在人生道路上打怪起来困难重重,她似乎永远摆脱不了那种无力感,有种习</div> </li> <li><a href="/article/1835511542284644352.htm" title="学点心理知识,呵护孩子健康" target="_blank">学点心理知识,呵护孩子健康</a> <span class="text-muted">静候花开_7090</span> <div>昨天听了华中师范大学教育管理学系副教授张玲老师的《哪里才是学生心理健康的最后庇护所,超越教育与技术的思考》的讲座。今天又重新学习了一遍,收获匪浅。张玲博士也注意到了当今社会上的孩子由于心理问题导致的自残、自杀及伤害他人等恶性事件。她向我们普及了一个重要的命题,她说心理健康的一些基本命题,我们与我们通常的一些教育命题是不同的,她还举了几个例子,让我们明白我们原来以为的健康并非心理学上的健康。比如如果</div> </li> <li><a href="/article/1835511163450912768.htm" title="2021年12月19日,春蕾教育集团团建活动感受——黄晓丹" target="_blank">2021年12月19日,春蕾教育集团团建活动感受——黄晓丹</a> <span class="text-muted">黄错错加油</span> <div>感受:1.从陌生到熟悉的过程。游戏环节让我们在轻松的氛围中得到了锻炼,也增长了不少知识。2.游戏过程中,我们贡献的是个人力量,展现的是团队的力量。它磨合的往往不止是工作的熟悉,更是观念上契合度的贴近。3.这和工作是一样的道理。在各自的岗位上,每个人摆正自己的位置、各司其职充分发挥才能,并团结一致劲往一处使,才能实现最大的成功。新知:1.团队精神需要不断地创新。过去,人们把创新看作是冒风险,现在人们</div> </li> <li><a href="/article/1835511036317364224.htm" title="Cell Insight | 单细胞测序技术又一新发现,可用于HIV-1和Mtb共感染个体诊断" target="_blank">Cell Insight | 单细胞测序技术又一新发现,可用于HIV-1和Mtb共感染个体诊断</a> <span class="text-muted">尐尐呅</span> <div>结核病是艾滋病合并其他疾病中导致患者死亡的主要原因。其中结核病由结核分枝杆菌(Mycobacteriumtuberculosis,Mtb)感染引起,获得性免疫缺陷综合症(艾滋病)由人免疫缺陷病毒(Humanimmunodeficiencyvirustype1,HIV-1)感染引起。国家感染性疾病临床医学研究中心/深圳市第三人民医院张国良团队携手深圳华大生命科学研究院吴靓团队,共同研究得出单细胞测序</div> </li> <li><a href="/article/1835511030260789248.htm" title="c++ 的iostream 和 c++的stdio的区别和联系" target="_blank">c++ 的iostream 和 c++的stdio的区别和联系</a> <span class="text-muted">黄卷青灯77</span> <a class="tag" taget="_blank" href="/search/c%2B%2B/1.htm">c++</a><a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95/1.htm">算法</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a><a class="tag" taget="_blank" href="/search/iostream/1.htm">iostream</a><a class="tag" taget="_blank" href="/search/stdio/1.htm">stdio</a> <div>在C++中,iostream和C语言的stdio.h都是用于处理输入输出的库,但它们在设计、用法和功能上有许多不同。以下是两者的区别和联系:区别1.编程风格iostream(C++风格):C++标准库中的输入输出流类库,支持面向对象的输入输出操作。典型用法是cin(输入)和cout(输出),使用>操作符来处理数据。更加类型安全,支持用户自定义类型的输入输出。#includeintmain(){in</div> </li> <li><a href="/article/1835510909070569472.htm" title="瑶池防线" target="_blank">瑶池防线</a> <span class="text-muted">谜影梦蝶</span> <div>冥华虽然逃过了影梦的军队,但他是一个忠臣,他选择上报战况。败给影梦后成逃兵,高层亡尔还活着,七重天失守......随便一条,即可处死冥华。冥华自然是知道以仙界高层的习性此信一发自己必死无疑,但他还选择上报实情,因为责任。同样此信送到仙宫后,知道此事的人,大多数人都认定冥华要完了,所以上到仙界高层,下到扫大街的,包括冥华自己,全都准备好迎接冥华之死。如果仙界现在还属于两方之争的话,冥华必死无疑。然而</div> </li> <li><a href="/article/1835510656011431936.htm" title="爬山后遗症" target="_blank">爬山后遗症</a> <span class="text-muted">璃绛</span> <div>爬山,攀登,一步一步走向制高点,是一种挑战。成功抵达是一种无法言语的快乐,在山顶吹吹风,看看风景,这是从未有过的体验。然而,爬山一时爽,下山腿打颤,颠簸的路,一路向下走,腿部力量不够,走起来抖到不行,停不下来了!第二天必定腿疼,浑身酸痛,坐立难安!</div> </li> <li><a href="/article/52.htm" title="解读Servlet原理篇二---GenericServlet与HttpServlet" target="_blank">解读Servlet原理篇二---GenericServlet与HttpServlet</a> <span class="text-muted">周凡杨</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/HttpServlet/1.htm">HttpServlet</a><a class="tag" taget="_blank" href="/search/%E6%BA%90%E7%90%86/1.htm">源理</a><a class="tag" taget="_blank" href="/search/GenericService/1.htm">GenericService</a><a class="tag" taget="_blank" href="/search/%E6%BA%90%E7%A0%81/1.htm">源码</a> <div>在上一篇《解读Servlet原理篇一》中提到,要实现javax.servlet.Servlet接口(即写自己的Servlet应用),你可以写一个继承自javax.servlet.GenericServletr的generic Servlet ,也可以写一个继承自java.servlet.http.HttpServlet的HTTP Servlet(这就是为什么我们自定义的Servlet通常是exte</div> </li> <li><a href="/article/179.htm" title="MySQL性能优化" target="_blank">MySQL性能优化</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a> <div>        性能优化是通过某些有效的方法来提高MySQL的运行速度,减少占用的磁盘空间。性能优化包含很多方面,例如优化查询速度,优化更新速度和优化MySQL服务器等。本文介绍方法的主要有:         a.优化查询         b.优化数据库结构   </div> </li> <li><a href="/article/306.htm" title="ThreadPool定时重试" target="_blank">ThreadPool定时重试</a> <span class="text-muted">dai_lm</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/ThreadPool/1.htm">ThreadPool</a><a class="tag" taget="_blank" href="/search/thread/1.htm">thread</a><a class="tag" taget="_blank" href="/search/timer/1.htm">timer</a><a class="tag" taget="_blank" href="/search/timertask/1.htm">timertask</a> <div>项目需要当某事件触发时,执行http请求任务,失败时需要有重试机制,并根据失败次数的增加,重试间隔也相应增加,任务可能并发。 由于是耗时任务,首先考虑的就是用线程来实现,并且为了节约资源,因而选择线程池。 为了解决不定间隔的重试,选择Timer和TimerTask来完成 package threadpool; public class ThreadPoolTest { </div> </li> <li><a href="/article/433.htm" title="Oracle 查看数据库的连接情况" target="_blank">Oracle 查看数据库的连接情况</a> <span class="text-muted">周凡杨</span> <a class="tag" taget="_blank" href="/search/sql/1.htm">sql</a><a class="tag" taget="_blank" href="/search/oracle+%E8%BF%9E%E6%8E%A5/1.htm">oracle 连接</a> <div>首先要说的是,不同版本数据库提供的系统表会有不同,你可以根据数据字典查看该版本数据库所提供的表。 select * from dict where table_name like '%SESSION%'; 就可以查出一些表,然后根据这些表就可以获得会话信息 select sid,serial#,status,username,schemaname,osuser,terminal,ma</div> </li> <li><a href="/article/560.htm" title="类的继承" target="_blank">类的继承</a> <span class="text-muted">朱辉辉33</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a> <div>类的继承可以提高代码的重用行,减少冗余代码;还能提高代码的扩展性。Java继承的关键字是extends 格式:public class 类名(子类)extends 类名(父类){ } 子类可以继承到父类所有的属性和普通方法,但不能继承构造方法。且子类可以直接使用父类的public和 protected属性,但要使用private属性仍需通过调用。 子类的方法可以重写,但必须和父类的返回值类</div> </li> <li><a href="/article/687.htm" title="android 悬浮窗特效" target="_blank">android 悬浮窗特效</a> <span class="text-muted">肆无忌惮_</span> <a class="tag" taget="_blank" href="/search/android/1.htm">android</a> <div>最近在开发项目的时候需要做一个悬浮层的动画,类似于支付宝掉钱动画。但是区别在于,需求是浮出一个窗口,之后边缩放边位移至屏幕右下角标签处。效果图如下:   一开始考虑用自定义View来做。后来发现开线程让其移动很卡,ListView+动画也没法精确定位到目标点。   后来想利用Dialog的dismiss动画来完成。   自定义一个Dialog后,在styl</div> </li> <li><a href="/article/814.htm" title="hadoop伪分布式搭建" target="_blank">hadoop伪分布式搭建</a> <span class="text-muted">林鹤霄</span> <a class="tag" taget="_blank" href="/search/hadoop/1.htm">hadoop</a> <div>要修改4个文件    1: vim hadoop-env.sh  第九行    2: vim core-site.xml            <configuration>     &n</div> </li> <li><a href="/article/941.htm" title="gdb调试命令" target="_blank">gdb调试命令</a> <span class="text-muted">aigo</span> <a class="tag" taget="_blank" href="/search/gdb/1.htm">gdb</a> <div>原文:http://blog.csdn.net/hanchaoman/article/details/5517362   一、GDB常用命令简介   r run 运行.程序还没有运行前使用 c             cuntinue </div> </li> <li><a href="/article/1068.htm" title="Socket编程的HelloWorld实例" target="_blank">Socket编程的HelloWorld实例</a> <span class="text-muted">alleni123</span> <a class="tag" taget="_blank" href="/search/socket/1.htm">socket</a> <div>public class Client { public static void main(String[] args) { Client c=new Client(); c.receiveMessage(); } public void receiveMessage(){ Socket s=null; BufferedRea</div> </li> <li><a href="/article/1195.htm" title="线程同步和异步" target="_blank">线程同步和异步</a> <span class="text-muted">百合不是茶</span> <a class="tag" taget="_blank" href="/search/%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5/1.htm">线程同步</a><a class="tag" taget="_blank" href="/search/%E5%BC%82%E6%AD%A5/1.htm">异步</a> <div>多线程和同步 : 如进程、线程同步,可理解为进程或线程A和B一块配合,A执行到一定程度时要依靠B的某个结果,于是停下来,示意B运行;B依言执行,再将结果给A;A再继续操作。 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回,同时其它线程也不能调用这个方法   多线程和异步:多线程可以做不同的事情,涉及到线程通知     &</div> </li> <li><a href="/article/1322.htm" title="JSP中文乱码分析" target="_blank">JSP中文乱码分析</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/jsp/1.htm">jsp</a><a class="tag" taget="_blank" href="/search/%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81/1.htm">中文乱码</a> <div>        在JSP的开发过程中,经常出现中文乱码的问题。         首先了解一下Java中文问题的由来:         Java的内核和class文件是基于unicode的,这使Java程序具有良好的跨平台性,但也带来了一些中文乱码问题的麻烦。原因主要有两方面,</div> </li> <li><a href="/article/1449.htm" title="js实现页面跳转重定向的几种方式" target="_blank">js实现页面跳转重定向的几种方式</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/JavaScript/1.htm">JavaScript</a><a class="tag" taget="_blank" href="/search/%E9%87%8D%E5%AE%9A%E5%90%91/1.htm">重定向</a> <div>        js实现页面跳转重定向有如下几种方式: 一.window.location.href <script language="javascript"type="text/javascript"> window.location.href="http://www.baidu.c</div> </li> <li><a href="/article/1576.htm" title="【Struts2三】Struts2 Action转发类型" target="_blank">【Struts2三】Struts2 Action转发类型</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/struts2/1.htm">struts2</a> <div> 在【Struts2一】 Struts Hello World http://bit1129.iteye.com/blog/2109365中配置了一个简单的Action,配置如下   <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configurat</div> </li> <li><a href="/article/1703.htm" title="【HBase十一】Java API操作HBase" target="_blank">【HBase十一】Java API操作HBase</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/hbase/1.htm">hbase</a> <div>Admin类的主要方法注释:   1. 创建表 /** * Creates a new table. Synchronous operation. * * @param desc table descriptor for table * @throws IllegalArgumentException if the table name is res</div> </li> <li><a href="/article/1830.htm" title="nginx gzip" target="_blank">nginx gzip</a> <span class="text-muted">ronin47</span> <a class="tag" taget="_blank" href="/search/nginx+gzip/1.htm">nginx gzip</a> <div>Nginx GZip 压缩 Nginx GZip 模块文档详见:http://wiki.nginx.org/HttpGzipModule 常用配置片段如下: gzip on; gzip_comp_level 2; # 压缩比例,比例越大,压缩时间越长。默认是1 gzip_types text/css text/javascript; # 哪些文件可以被压缩 gzip_disable &q</div> </li> <li><a href="/article/1957.htm" title="java-7.微软亚院之编程判断俩个链表是否相交 给出俩个单向链表的头指针,比如 h1 , h2 ,判断这俩个链表是否相交" target="_blank">java-7.微软亚院之编程判断俩个链表是否相交 给出俩个单向链表的头指针,比如 h1 , h2 ,判断这俩个链表是否相交</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a> <div> public class LinkListTest { /** * we deal with two main missions: * * A. * 1.we create two joined-List(both have no loop) * 2.whether list1 and list2 join * 3.print the join</div> </li> <li><a href="/article/2084.htm" title="Spring源码学习-JdbcTemplate batchUpdate批量操作" target="_blank">Spring源码学习-JdbcTemplate batchUpdate批量操作</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a> <div>Spring JdbcTemplate的batch操作最后还是利用了JDBC提供的方法,Spring只是做了一下改造和封装 JDBC的batch操作: String sql = "INSERT INTO CUSTOMER " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)"; </div> </li> <li><a href="/article/2211.htm" title="[JWFD开源工作流]大规模拓扑矩阵存储结构最新进展" target="_blank">[JWFD开源工作流]大规模拓扑矩阵存储结构最新进展</a> <span class="text-muted">comsci</span> <a class="tag" taget="_blank" href="/search/%E5%B7%A5%E4%BD%9C%E6%B5%81/1.htm">工作流</a> <div>    生成和创建类已经完成,构造一个100万个元素的矩阵模型,存储空间只有11M大,请大家参考我在博客园上面的文档"构造下一代工作流存储结构的尝试",更加相信的设计和代码将陆续推出.........     竞争对手的能力也很强.......,我相信..你们一定能够先于我们推出大规模拓扑扫描和分析系统的....</div> </li> <li><a href="/article/2338.htm" title="base64编码和url编码" target="_blank">base64编码和url编码</a> <span class="text-muted">cuityang</span> <a class="tag" taget="_blank" href="/search/base64/1.htm">base64</a><a class="tag" taget="_blank" href="/search/url/1.htm">url</a> <div>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.io.UnsupportedEncodingException; </div> </li> <li><a href="/article/2465.htm" title="web应用集群Session保持" target="_blank">web应用集群Session保持</a> <span class="text-muted">dalan_123</span> <a class="tag" taget="_blank" href="/search/session/1.htm">session</a> <div>关于使用 memcached 或redis 存储 session ,以及使用 terracotta 服务器共享。建议使用 redis,不仅仅因为它可以将缓存的内容持久化,还因为它支持的单个对象比较大,而且数据类型丰富,不只是缓存 session,还可以做其他用途,一举几得啊。1、使用 filter 方法存储这种方法比较推荐,因为它的服务器使用范围比较多,不仅限于tomcat ,而且实现的原理比较简</div> </li> <li><a href="/article/2719.htm" title="Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']" target="_blank">Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a> <div>    public function getMinLimit () {        $sql = "...";        $result = yii::app()->db->createCo</div> </li> <li><a href="/article/2846.htm" title="solr StatsComponent(聚合统计)" target="_blank">solr StatsComponent(聚合统计)</a> <span class="text-muted">eksliang</span> <a class="tag" taget="_blank" href="/search/solr%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2/1.htm">solr聚合查询</a><a class="tag" taget="_blank" href="/search/solr+stats/1.htm">solr stats</a> <div>StatsComponent 转载请出自出处:http://eksliang.iteye.com/blog/2169134 http://eksliang.iteye.com/ 一、概述        Solr可以利用StatsComponent 实现数据库的聚合统计查询,也就是min、max、avg、count、sum的功能   二、参数</div> </li> <li><a href="/article/2973.htm" title="百度一道面试题" target="_blank">百度一道面试题</a> <span class="text-muted">greemranqq</span> <a class="tag" taget="_blank" href="/search/%E4%BD%8D%E8%BF%90%E7%AE%97/1.htm">位运算</a><a class="tag" taget="_blank" href="/search/%E7%99%BE%E5%BA%A6%E9%9D%A2%E8%AF%95/1.htm">百度面试</a><a class="tag" taget="_blank" href="/search/%E5%AF%BB%E6%89%BE%E5%A5%87%E6%95%B0%E7%AE%97%E6%B3%95/1.htm">寻找奇数算法</a><a class="tag" taget="_blank" href="/search/bitmap+%E7%AE%97%E6%B3%95/1.htm">bitmap 算法</a> <div>那天看朋友提了一个百度面试的题目:怎么找出{1,1,2,3,3,4,4,4,5,5,5,5}  找出出现次数为奇数的数字.   我这里复制的是原话,当然顺序是不一定的,很多拿到题目第一反应就是用map,当然可以解决,但是效率不高。   还有人觉得应该用算法xxx,我是没想到用啥算法好...!   还有觉得应该先排序...   还有觉</div> </li> <li><a href="/article/3100.htm" title="Spring之在开发中使用SpringJDBC" target="_blank">Spring之在开发中使用SpringJDBC</a> <span class="text-muted">ihuning</span> <a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a> <div>  在实际开发中使用SpringJDBC有两种方式:   1. 在Dao中添加属性JdbcTemplate并用Spring注入;     JdbcTemplate类被设计成为线程安全的,所以可以在IOC 容器中声明它的单个实例,并将这个实例注入到所有的 DAO 实例中。JdbcTemplate也利用了Java 1.5 的特定(自动装箱,泛型,可变长度</div> </li> <li><a href="/article/3227.htm" title="JSON API 1.0 核心开发者自述 | 你所不知道的那些技术细节" target="_blank">JSON API 1.0 核心开发者自述 | 你所不知道的那些技术细节</a> <span class="text-muted">justjavac</span> <a class="tag" taget="_blank" href="/search/json/1.htm">json</a> <div>2013年5月,Yehuda Katz 完成了JSON API(英文,中文) 技术规范的初稿。事情就发生在 RailsConf 之后,在那次会议上他和 Steve Klabnik 就 JSON 雏形的技术细节相聊甚欢。在沟通单一 Rails 服务器库—— ActiveModel::Serializers 和单一 JavaScript 客户端库——&</div> </li> <li><a href="/article/3354.htm" title="网站项目建设流程概述" target="_blank">网站项目建设流程概述</a> <span class="text-muted">macroli</span> <a class="tag" taget="_blank" href="/search/%E5%B7%A5%E4%BD%9C/1.htm">工作</a> <div>一.概念 网站项目管理就是根据特定的规范、在预算范围内、按时完成的网站开发任务。 二.需求分析 项目立项   我们接到客户的业务咨询,经过双方不断的接洽和了解,并通过基本的可行性讨论够,初步达成制作协议,这时就需要将项目立项。较好的做法是成立一个专门的项目小组,小组成员包括:项目经理,网页设计,程序员,测试员,编辑/文档等必须人员。项目实行项目经理制。 客户的需求说明书   第一步是需</div> </li> <li><a href="/article/3481.htm" title="AngularJs 三目运算 表达式判断" target="_blank">AngularJs 三目运算 表达式判断</a> <span class="text-muted">qiaolevip</span> <a class="tag" taget="_blank" href="/search/%E6%AF%8F%E5%A4%A9%E8%BF%9B%E6%AD%A5%E4%B8%80%E7%82%B9%E7%82%B9/1.htm">每天进步一点点</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0%E6%B0%B8%E6%97%A0%E6%AD%A2%E5%A2%83/1.htm">学习永无止境</a><a class="tag" taget="_blank" href="/search/%E4%BC%97%E8%A7%82%E5%8D%83%E8%B1%A1/1.htm">众观千象</a><a class="tag" taget="_blank" href="/search/AngularJS/1.htm">AngularJS</a> <div>事件回顾:由于需要修改同一个模板,里面包含2个不同的内容,第一个里面使用的时间差和第二个里面名称不一样,其他过滤器,内容都大同小异。希望杜绝If这样比较傻的来判断if-show or not,继续追究其源码。 var b = "{{", a = "}}"; this.startSymbol = function(a) { </div> </li> <li><a href="/article/3608.htm" title="Spark算子:统计RDD分区中的元素及数量" target="_blank">Spark算子:统计RDD分区中的元素及数量</a> <span class="text-muted">superlxw1234</span> <a class="tag" taget="_blank" href="/search/spark/1.htm">spark</a><a class="tag" taget="_blank" href="/search/spark%E7%AE%97%E5%AD%90/1.htm">spark算子</a><a class="tag" taget="_blank" href="/search/Spark+RDD%E5%88%86%E5%8C%BA%E5%85%83%E7%B4%A0/1.htm">Spark RDD分区元素</a> <div>关键字:Spark算子、Spark RDD分区、Spark RDD分区元素数量     Spark RDD是被分区的,在生成RDD时候,一般可以指定分区的数量,如果不指定分区数量,当RDD从集合创建时候,则默认为该程序所分配到的资源的CPU核数,如果是从HDFS文件创建,默认为文件的Block数。   可以利用RDD的mapPartitionsWithInd</div> </li> <li><a href="/article/3735.htm" title="Spring 3.2.x将于2016年12月31日停止支持" target="_blank">Spring 3.2.x将于2016年12月31日停止支持</a> <span class="text-muted">wiselyman</span> <a class="tag" taget="_blank" href="/search/Spring+3/1.htm">Spring 3</a> <div>              Spring 团队公布在2016年12月31日停止对Spring Framework 3.2.x(包含tomcat 6.x)的支持。在此之前spring团队将持续发布3.2.x的维护版本。          请大家及时准备及时升级到Spring </div> </li> <li><a href="/article/3862.htm" title="fis纯前端解决方案fis-pure" target="_blank">fis纯前端解决方案fis-pure</a> <span class="text-muted">zccst</span> <a class="tag" taget="_blank" href="/search/JavaScript/1.htm">JavaScript</a> <div>作者:zccst FIS通过插件扩展可以完美的支持模块化的前端开发方案,我们通过FIS的二次封装能力,封装了一个功能完备的纯前端模块化方案pure。 1,fis-pure的安装 $ fis install -g fis-pure $ pure -v 0.1.4 2,下载demo到本地 git clone https://github.com/hefangshi/f</div> </li> </ul> </div> </div> </div> <div> <div class="container"> <div class="indexes"> <strong>按字母分类:</strong> <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a> </div> </div> </div> <footer id="footer" class="mb30 mt30"> <div class="container"> <div class="footBglm"> <a target="_blank" href="/">首页</a> - <a target="_blank" href="/custom/about.htm">关于我们</a> - <a target="_blank" href="/search/Java/1.htm">站内搜索</a> - <a target="_blank" href="/sitemap.txt">Sitemap</a> - <a target="_blank" href="/custom/delete.htm">侵权投诉</a> </div> <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 E-COM-NET.COM , All Rights Reserved. <!-- <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>--> </div> </div> </footer> <!-- 代码高亮 --> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script> <link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script> </body> </html>