python goto 包

http://entrian.com/goto/

# Example 1: Breaking out from a deeply nested loop:
from goto import goto, label
for i in range(1, 10):
    for j in range(1, 20):
        for k in range(1, 30):
            print i, j, k
            if k == 3:
                goto .end
label .end
print "Finished\n"

# Example 2: Restarting a loop:
from goto import goto, label
label .start
for i in range(1, 4):
    print i
    if i == 2:
        try:
            output = message
        except NameError:
            print "Oops - forgot to define ‘message‘!  Start again."
            message = "Hello world"
            goto .start
print output, "\n"

# Example 3: Cleaning up after something fails:
from goto import goto, label

# Imagine that these are real worker functions.
def setUp(): print "setUp"
def doFirstTask(): print 1; return True
def doSecondTask(): print 2; return True
def doThirdTask(): print 3; return False  # This one pretends to fail.
def doFourthTask(): print 4; return True
def cleanUp(): print "cleanUp"

# This prints "setUp, 1, 2, 3, cleanUp" - no "4" because doThirdTask fails.
def bigFunction1():
    setUp()
    if not doFirstTask():
        goto .cleanup
    if not doSecondTask():
        goto .cleanup
    if not doThirdTask():
        goto .cleanup
    if not doFourthTask():
        goto .cleanup

    label .cleanup
    cleanUp()

bigFunction1()
print "bigFunction1 done\n"

# Example 4: Using comefrom to let the cleanup code take control itself.
from goto import comefrom, label
def bigFunction2():
    setUp()
    if not doFirstTask():
        label .failed
    if not doSecondTask():
        label .failed
    if not doThirdTask():
        label .failed
    if not doFourthTask():
        label .failed

    comefrom .failed
    cleanUp()

bigFunction2()
print "bigFunction2 done\n"

# Example 5: Using a computed goto:
from goto import goto, label

label .getinput
i = raw_input("Enter either ‘a‘, ‘b‘ or ‘c‘, or any other letter to quit: ")
if i in (‘a‘, ‘b‘, ‘c‘):
    goto *i
else:
    goto .quit

label .a
print "You typed ‘a‘"
goto .getinput

label .b
print "You typed ‘b‘"
goto .getinput

label .c
print "You typed ‘c‘"
goto .getinput

label .quit
print "Finished\n"

# Example 6: What happens when a label is missing:
from goto import goto, label
label .real
goto .unreal      # Raises a MissingLabelError exception.
时间: 2024-08-25 15:27:29

python goto 包的相关文章

使用Python Yaml包处理Json数据

在做网络爬虫的时候会遇到json数据格式的数据包,如果返回的是一个json格式的文件,可以使用 Python Yaml包处理数据,不需要再使用正则表达式匹配了,使用实例如 https://maps-api-ssl.google.com/maps/suggest?q=hello  这个地址,我们需要query对应的数据项. 相关代码如下: # -*- coding: utf-8 -*- import yaml import urllib2 address = 'https://maps-api-s

CentOS中Python pip包管理工具的安装和使用

Python有很多第三方的模块或包,要管理这些包,有很多方法.python的pip包管理工具,类似与CentOS中yum,使用该工具安装Python包非常方便. 从Python内部来说,pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品.distribute是setuptools的取代(Setuptools包后期不再维护了),pip是easy_install的取代. pip的安装前需要setuptools 或者 distribute,如果你使用的是P

python找包的路径(找不到自定义包的问题解决)

问题:工程下自定义的包,python在执行时经常找不到包 python找包的路径:python安装路径下的lib包和PYTHONPATH下的包 可以使用[sys.path]打印出python找包的路径 import sys syspath=sys.path 但是在jenkins运行时经常提示找不到包,所以就需要手动添加PYTHONPATH,通过追加sys.path列表来实现 例如下面的方式: import os import sys rootpath=str("工程所在目录") sys

python基础:python扩展包的安装方式

python扩展包有三种安装方式: 1. pip安装方式.python3默认自带pip,无需另外安装:在python2.7版本上默认为easy_install安装工作进行安装,如果需要使用pip安装,需要自行下载安装(可以从http://www.pip-installer.org网站下载). 2. 系统自带的包安装管理工具. 3. 从源代码安装. 使用pip安装:在command命令行中输入:pip install 包名,例如:pip install flask 使用pip安装指定版本:在com

Python第三方包的安装

不得不说我又变懒了,过个年都懒得学习了 之前不太明白包的管理,都是用一个搜一下 今天总结一下 python第三方包安装的两种方式 下载源码然后setup.py安装 用pip进行安装 第一种方法 在网上找到需要的包,下载解压,找到setup.py cmd切换到包的目录,输入python setup.py install 第二种方法 cmd切换到Python27\Scripts目录 敲入pip install xxx(包名)

Python的包管理工具Pip (zz )

Python的包管理工具Pip 接触了Ruby,发现它有个包管理工具RubyGem很好用,并且有很完备的文档系统http://rdoc.info 发现Python下也有同样的工具,包括easy_install和Pip.不过,我没有细看easy_install的方法,这就简单的介绍一下Pip的安装与使用: 准备: $ curl -O http://python-distribute.org/distribute_setup.py $ python distribute_setup.py 安装: $

Python 引入包的两种方式区别

1.import XXX仅仅是告诉我们需要使用这个包,但是你真正使用的时候,需要完整的导入这个包的全路径 比如: import wechat.views 在使用其中的hello函数的时候,需要 wechat.views.hello - 这个路径不能简略 2.from ... import ...就不需要指定父的路径了 比如: from wechat.views import *    #你可以使用views内部的变量 你就可以直接使用其中的hello hello 但是如果: from wecha

Python机器学习包

常用Python机器学习包 Numpy:用于科学计算的包 Pandas:提供高性能,易于使用的数据结构和数据分析工具 Scipy:用于数学,科学工程的软件 StatsModels:用于探索数据.估计统计模型.统计检验 Scikit-learn:提供经典的机器学习算法用于数据挖掘和数据分析 matplotlib:2D绘图库,可绘制高质量的图片 Windows安装Python机器学习包网址:http://dblab.xmu.edu.cn/blog/python-machine-learning-pa

关于python的包

参考文献:http://blog.sina.com.cn/s/blog_615c388d01017b5o.html 注:本文大多对上连接的整理,仅用于学习,望博主见谅.转载请附上上述链接. 为什么要包? python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的.py文件所在的目录,然后import.这样的做法,对于少数文件是可行的,但如果程序数目很 多,