2011.11.1好日子,今天博客访问量超过1000了。 2012.01.29,访问量突破2000了. 2012.02.01,访问量突破3000了.继续进步

对比测试脚本语言Tcl和Python的语法以及处理(基础篇)

上一篇 / 下一篇  2011-11-14 18:20:20 / 个人分类:自动化脚本

在编写测试自动化脚本中,由于项目变更需要使用Tclpython语言,所以在学习中对比两个学习笔记,先统计一些常用的基础对比.

期间因为个人学习深入度不足,所以未录入Tclinfo,跟踪,历史相关指令.也未录入python相关class的知识.同时由于时间不足,没列举ui和制作工具方面的对比.在后续发布深入对比时会以测试的实例作对比.同时本文是以个人学习和工作的经验编写,所以有错误的地方希望指出,我作出更新.(同时部分对比我未寻找到,标为缺省中)

 

Tcl的花括号中,所有特殊字符都将成为普通字符,失去其特殊意义,TCL解释器不会对其作特殊处理。

打印:

Tcl:puts "hello world"

Python:print 'hello world'

 

数字和表达式:

Tcl:expr 2+2

Python:2+2

 

获取用户输入:

Tcl:缺省中

Python:input("please input:")

 

赋值:

Tcl:set a 0set a "abc"打印puts $a,

set y [expr 2+100],使用unset删除变量

Python:

a =0a = 'abc'打印print a或者直接a ,

y = 2+100,使用del删除变量

 

数组:

Tcl:

set a(0) 1,set a(1) 2,.......

array get a

array name a 

array size a

array exists a

 

Pyhton:

Pyhton中有元组的概念,虽然个人学习的确在列表和数组间做出很清晰的解释,暂时以元组来作对比

a =(1,2,3,4,5,6),a[0],a[1]...

 

加到一个变量和整数:

tcl:

append X AAA;append X BBB

set a 1;incr a 1

 

Pyhton:

a = [1,2,3,4,5];a.append(5)

a = ['a','b'];a.append('c')

 

列表:

tcl:

set alist [list 1 2 3 4 5 6 7 8]

合成list :blist [concat $alist {3 4 5 6 7}]

返回索引:lindex $blist 0

返回长度:llength $blist

插入数据:set blist [linsert $blist 0 {a b}]

替换数据:set blist [lreplace $blist 0 0 c d]

返回串:lrange $blist 0 end

拆分和连接指令: set blist {1,2,3,4,5,6}

                                                                          set blist[split $blist ,]

                                                                          set  blist[join $blist ,]

python:

alist = [1,2,3,4,'a',(1,2,'b')]

blist = alist + [1,2,3,4,5]

blist[0]

len[blist]

blist[1:1] = ['a','b']

blist[1]='a'

print blist[1:4]

注意:python中拆分函数支持字符串的拆分,所以数字需以下解决:

                                                                          blist = ['a','2']

                                                                          clist ='_'.join(blist)

                                                                          clist.split('_')

 

控制流:

tcl:

控制语句中记住大括号需要空格,包括ifwhileforforeachswitchbreakcontinue等命令

set a 0

if {$a != 0} {

                           puts "fail"

                           }       else {

                           puts $a

                           }

 

for {set i 0} {$i < 10} {incr i 1} {

                           puts $i

                  }

                  

switch $x {

                           a  -

                           b  {incr  t1}

                           c  {incr   t2}

                           default {incr t3}

                  }

                  

python:

Python中的缩进是用来表示语法块的,控制语句有ifwhileforcaseloopuntilyield.以空行表示循环结束

if x < 0:

...    x = 0

...    print 'Negative changed to zero'

... elif x == 0:

...    print 'Zero'

... eles:

... print 'Zero'

...

相关的函数有range()函数.还有pass关键字是不做任何操作.

迭代索引列表  

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']  

>>> for i in range(len(a)):  

...    print i, a[i]  

类似于java中的for循环效果:  

for(int i=0;i<a.length;i++){  

   System.out.println(i+""+a[i]);  

}

 

 

执行命令eval:

tcl:

 set cmd "puts /"This is a tcltk example/""

 eval $cmd

 

python:

此函数也是支持字符串的命令:

 eval('3+4')

 

索引文件:

tcl:

        source e:/tcl&c/hello.tcl

        

python:

        import test.py

        from sys import argv,path

        

过程(procedure),全局变量:

        个人觉得在脚本语言中,其实在运用过程中,函数和过程这2个东西其实很少真正意义实现,主要还是封装成易读的模块.

        注意:过程使用了全局变量后,修改会影响到全局上.

tcl:

        set c 4

        proc test {a {d 2}} {

                  global c

                  set $f [expr $a + $c +$d]

                  return $f

                  }

                  

pyhton:

注意:过程也是要缩进的.

 

b = 2

def add1(x):

        global b

        x = b + 1

        return x

 

引用:

Tcl:

proc temp { arg } {

                  upvar $arg b

                  set b  [expr $b+2]

                  }

proc myexp  { var } {

                  set a 4

                  temp a

                  return [expr $var+$a]

                   }

pyhton:暂时缺省中

 

格式化输出:

tcl:

set msg  [format "%s is %d years old" wyj 27]

scan命令可以认为是format命令的逆

scan "some 26 34" "some %d %d" a b

python:

print 'www.%s.%s'%('qq'.'com')

 

正则表达式:

tcl:

regexp {^((0x)?[0-9a-fA-F]+|[0-9]+)$} ab

regsub there "They live there lives " their x

 

python:

import re

re.findall(r’\bf[a-z]*’, ’which foot or hand fell fastest’)

tea for too’.replace(’too’, ’two’)

 

字符串处理:

Tcl:

索引字符串:string index "0123456" 6


TAG:

 

评分:0

我来说两句

acbennn

acbennn

站在云端看浮云,晕. CSDN的博客:http://blog.csdn.net/bullswu/article/details/6798437

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 60201
  • 日志数: 44
  • 建立时间: 2011-09-18
  • 更新时间: 2013-09-22

RSS订阅

Open Toolbar