资源简介
是《python3程序开发指南》中实例与课后习题的源代码。
代码片段和文件信息
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation either version 3 of the License or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
“““
>>> u = Stack()
>>> u.push(1); u.push(2); u.push(4)
>>> str(u)
‘[1 2 4]‘
>>> u.can_undo
True
>>> while u.can_undo:
... u.undo()
>>> str(u)
‘[]‘
>>> for x in list(range(-5 0)) + list(range(5)):
... u.push(x)
>>> str(u)
‘[-5 -4 -3 -2 -1 0 1 2 3 4]‘
>>> u.top()
4
>>> total = 0
>>> for x in range(5):
... total += u.pop()
>>> str(u) total
(‘[-5 -4 -3 -2 -1]‘ 10)
>>> while u.can_undo:
... u.undo()
>>> str(u)
‘[]‘
>>> import os
>>> import tempfile
>>> filename = os.path.join(tempfile.gettempdir() “fs.pkl“)
>>> fs = FileStack(filename)
>>> for x in list(range(-5 0)) + list(range(5)):
... fs.push(x)
>>> str(fs)
‘[-5 -4 -3 -2 -1 0 1 2 3 4]‘
>>> fs.top()
4
>>> total = 0
>>> for x in range(5):
... total += fs.pop()
>>> str(fs) total
(‘[-5 -4 -3 -2 -1]‘ 10)
>>> fs.push(909)
>>> str(fs)
‘[-5 -4 -3 -2 -1 909]‘
>>> os.path.basename(fs.filename)
‘fs.pkl‘
>>> fs.save()
>>> fs2 = FileStack(filename)
>>> str(fs2)
‘[]‘
>>> fs2.push(-32)
>>> fs2.can_undo
True
>>> fs2.load()
>>> fs2.can_undo
False
>>> str(fs2)
‘[-5 -4 -3 -2 -1 909]‘
>>> fs == fs2
True
“““
import abc
import pickle
class Undo(metaclass=abc.ABCmeta):
@abc.abstractmethod
def __init__(self):
self.__undos = []
@abc.abstractproperty
def can_undo(self):
return bool(self.__undos)
@abc.abstractmethod
def undo(self):
assert self.__undos “nothing left to undo“
self.__undos.pop()(self)
def add_undo(self undo):
self.__undos.append(undo)
def clear(self): # In class Undo
self.__undos = []
class Stack(Undo):
def __init__(self):
super().__init__()
self.__stack = []
@property
def can_undo(self):
return super().can_undo
def undo(self):
super().undo()
def push(self item):
self.__stack.append(item)
self.add_undo(lambda self: self.__stack.pop())
def pop(self):
item = self.__stack.pop()
self.add_undo(lambda self: self.__stack.append(item))
return item
def top(self):
assert self.__stack “Stack is empty“
return self.__stack[-1]
def __str__(self):
return str(self.__stack)
cla
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-01-08 01:18 py30eg\
文件 4814 2011-09-01 11:18 py30eg\Abstract.py
文件 5545 2011-09-01 11:18 py30eg\Account.py
文件 2078 2011-09-01 11:18 py30eg\Appliance.py
文件 1722 2011-09-01 11:18 py30eg\Ascii.py
文件 5296 2011-09-01 11:18 py30eg\Atomic.py
文件 5466 2011-09-01 11:18 py30eg\BibTeX.py
文件 10247 2011-09-01 11:18 py30eg\BikeStock.py
文件 10396 2011-09-01 11:18 py30eg\BikeStock_ans.py
文件 9501 2011-09-01 11:18 py30eg\BinaryRecordFile.py
文件 5420 2011-09-01 11:18 py30eg\BinaryRecordFile_ans.py
文件 1679 2011-09-01 11:18 py30eg\Block.py
文件 5087 2011-09-01 11:18 py30eg\BlockOutput.py
文件 9560 2011-09-01 11:18 py30eg\CharGrid.py
文件 1788 2011-09-01 11:18 py30eg\Circle.py
文件 5224 2011-09-01 11:18 py30eg\Console.py
文件 1824 2011-09-01 11:18 py30eg\Const.py
文件 4694 2011-09-01 11:18 py30eg\ExternalStorage.py
文件 4912 2011-09-01 11:18 py30eg\FuzzyBool.py
文件 5894 2011-09-01 11:18 py30eg\FuzzyBoolAlt.py
目录 0 2018-01-08 01:18 py30eg\Graphics\
文件 351 2011-09-01 11:18 py30eg\Graphics\Bmp.py
文件 357 2011-09-01 11:18 py30eg\Graphics\Jpeg.py
文件 368 2011-09-01 11:18 py30eg\Graphics\Png.py
文件 357 2011-09-01 11:18 py30eg\Graphics\Tiff.py
目录 0 2018-01-08 01:18 py30eg\Graphics\Vector\
文件 407 2011-09-01 11:18 py30eg\Graphics\Vector\Eps.py
文件 401 2011-09-01 11:18 py30eg\Graphics\Vector\Svg.py
文件 26 2011-09-01 11:18 py30eg\Graphics\Vector\__init__.py
文件 351 2011-09-01 11:18 py30eg\Graphics\Xpm.py
文件 49 2011-09-01 11:18 py30eg\Graphics\__init__.py
............此处省略130个文件信息
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论