Thursday, February 14, 2013

Testing Python for C# programmers

'''
Created on Feb 13, 2013

@author: Android.Y.You
'''
 

def main():
    print "you are in main"


#* operator of String
print '*'*20

 
import random

#format output
print "here is one random number %i " % ( random.randint(5,10))

#using type to get type information
print type(20)
print type(main)


#import everything
#before

import math
print math.pi
#after
from math import *
print pi

#comment,doc
def SayHello(msg):
    """SayeHello Function
    enter the msg as the input, typically a string
    """
    print "Hello " + msg

SayHello("ss")

#PrimeNumbers, else with for means no match.
def prime(n=100):
    for i in range(2,n):
        for j in range(2,i):
            if (i % j==0):
                break
        else:
            print i
           
prime(10)


#comprehensive list
s=range(1,20)
t=[x for x in s if x % 3==0 and x!=3 ]
print t

#lambda expression
def addx(n):
    return lambda x:x+n
add3=addx(3) # add 3
print add3(10) # 13

#keyboard input
#get input,using int as the converter, raw_input to get console in
id=int( raw_input("enter a number"))
print id>10

#list

list=range(1,3)
print type(list)
for l in list:
    print l
   
#list a mutable
list[0]=100
print list

#tuples, tuples are immutable
tu=(1,2,3)
print tu

#dictionary

d={"one":"1","two":"2"}
for k,v in d.items():
    print k,v
   
   
#fiel write
fout=open("out.txt","w") #a+ for append
fout.writelines("helloworld")
fout.close()

#write a object
class Person(object):
    Name="yourname"
    Age=20
    def __init__(self,name,age):
        self.Name=name
        self.Age=age
    def __str__(self, *args, **kwargs): #overwrite the java like tostirng
        return "name:%s,Age:%i" % (self.Name,self.Age)
p=Person("android",25)
print p

import pickle
print pickle.dumps(p) #object to strings

#file path
import os
print os.getcwd()

#walk for files
def walk(dirname):
    for file in os.listdir(dirname):
        s= os.path.join(dirname,file)
        if(os.path.isfile(s)):
            print s
        else:
            walk(s)
   
walk(r"C:/temp/")


#gui, tkinter.
from Tkinter import *
root=Tk()
w=Label(root,text="Helloworld")
w.pack()
root.mainloop()

 

#AOP, using the decroator
def logaop(func):
    print "binded"
    def ins(*a,**b):
        print "---before call the method"
        func(*a,**b)
        print "---after call the method"
     
    return ins

@logaop
def GetInfomrationFromDB(informationkey):
    print "You get the result for " + informationkey
  

 

GetInfomrationFromDB("test")


output

********************
here is one random number 9
<type 'int'>
<type 'function'>
3.14159265359
3.14159265359
Hello ss
2
3
5
7
[6, 9, 12, 15, 18]
13
enter a number10
False
<type 'list'>
1
2
[100, 2]
(1, 2, 3)
two 2
one 1
name:android,Age:25
ccopy_reg
_reconstructor
p0
(c__main__
Person
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'Age'
p6
I25
sS'Name'
p7
S'android'
p8
sb.
C:\Project\python\wp\FirstHello\src
C:/temp/a\New Text Document.txt
C:/temp/test.html

No comments:

 
Locations of visitors to this page