8000 This is a repository for python. · suskycode/pythonrepository@ff21335 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff21335

Browse files
committed
This is a repository for python.
0 parents  commit ff21335

19 files changed

+347
-0
lines changed

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>PythonKnowledge</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

.pydevproject

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?>
3+
4+
<pydev_project>
5+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
6+
<path>/PythonKnowledge</path>
7+
</pydev_pathproperty>
8+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
9+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
10+
</pydev_project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
eclipse.preferences.version=1
2+
encoding//src/python/TestStr.py=utf-8
3+
encoding//src/python/demo/Encode.py=utf-8
4+
encoding//src/python/demo/ListComposite.py=utf-8
5+
encoding//src/python/demo/TestCPickle.py=utf-8
6+
encoding//src/python/demo/TestClass.py=utf-8
7+
encoding//src/python/demo/TestControl.py=utf-8
8+
encoding//src/python/demo/TestDataStruct.py=utf-8
9+
encoding//src/python/demo/TestExtends.py=utf-8
10+
encoding//src/python/demo/TestLambda.py=utf-8
11+
encoding//src/python/demo/TestModule.py=utf-8
12+
encoding//src/python/demo/TestTryCatchFinal.py=utf-8
13+
encoding/<project>=UTF-8

src/__init__.py

Whitespace-only changes.

src/python/TestStr.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-5
6+
# Desc : str 方法
7+
#
8+
string = "sushiting"
9+
strArrays = ["s","h","i","t","i","n","g"]
10+
print string.capitalize()
11+
print string.join("-")
12+
print "-".join(strArrays)
13+
print string.split()

src/python/__init__.py

Whitespace-only changes.

src/python/demo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Encode.pyc

src/python/demo/Encode.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# 用于解决gvim控制台输出乱码
3+
import sys
4+
reload(sys)
5+
class UnicodeStreamFilter:
6+
def __init__(self, target):
7+
self.target = target
8+
self.encoding = 'utf-8'
9+
self.errors = 'replace'
10+
self.encode_to = 'cp936'
11+
def write(self, s):
12+
if type(s) == str:
13+
s = s.decode("utf-8")
14+
s = s.encode(self.encode_to, self.errors).decode(self.encode_to)
15+
self.target.write(s)
16+
sys.setdefaultencoding('cp936') # @UndefinedVariable
17+
sys.stdout = UnicodeStreamFilter(sys.stdout)
18+
if __name__ == "__main__":
19+
print '苏'
20+
21+

src/python/demo/ListComposite.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-9
6+
# Desc : 列表综合
7+
#
8+
intArr = [1,2,3,4,5]
9+
newIntArr = [3*i for i in intArr if i%2==0]
10+
print newIntArr
11+
12+
#在函数中接收元组和列表
13+
#当要使函数接收元组或字典形式的参数的时候,有一种特殊的方法,它分别使用*和**前缀。
14+
#这种方法在函数需要获取可变数量的参数的时候特别有用。
15+
def powsersum(power,*args):
16+
total = 0
17+
for i in args:
18+
total += pow(power, i)
19+
return total
20+
print powsersum(2,1,3,4),powsersum(1,1,2,3,4,5)
21+
def printmaps(args):
22+
print args
23+
for i in args:
24+
print args[i]
25+
printmaps({"name":"sushiting"})
26+
27+
#以下较为特殊 **
28+
def testdict(**args):
29+
print args
30+
testdict(a=3,b=3)

src/python/demo/TestCPickle.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-8
6+
# Desc : 类似于java的序列化,反序列化
7+
#
8+
# Python提供一个标准的模块,称为pickle。使用它你可以在一个文件中储存任何Python对象,
9+
# 之后你又可以把它完整无缺地取出来。这被称为 持久地 储存对象。
10+
# 还有另一个模块称为cPickle,它的功能和pickle模块完全相同,只不过它是用C语言编写的,因此要快得多(比pickle快1000倍)。你可以使用它们中的任一个,而我们在这里将使用cPickle模块。记住,我们把这两个模块都简称为pickle模块。
11+
import cPickle as p
12+
shoplist = ['apple','orange','banana']
13+
shopdata = file("shop.data","w")
14+
p.dump(shoplist, shopdata)
15+
shopdata.close()
16+
shopdata = file("shop.data")
17+
shoplist2 = p.load(shopdata)
18+
print shoplist2
19+

src/python/demo/TestClass.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-8
6+
# Desc :
7+
#
8+
class TestClass:
9+
#name类似java的静态变量
10+
#self 相当于类的一个实例 (属性的定义一般放在self中)
11+
name = 'sushiting'
12+
age = 23
13+
def __init__(self):
14+
print 'this is a class'
15+
def say(self):
16+
print 'hello'
17+
def setName(self,name):
18+
self.name = name
19+
def addAge(self,num):
20+
self.age = self.age + num
21+
def getName(self):
22+
print self.name
23+
test = TestClass()
24+
test.say()
25+
test.setName("jack")
26+
test.getName()
27+
print "age:%s" %TestClass.age
28+
test.addAge(3)
29+
print "age:%s" %test.age
30+
test2 = TestClass()
31+
print "age:%s" %TestClass.age

src/python/demo/TestControl.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-5
6+
# Desc : 控制流程IF/ELSE,WHILE,FOR
7+
#
8+
name = 'sushiting'
9+
if name == 'sushiting':
10+
print "it's me"
11+
else:
12+
print "it's not me"
13+
# for n in range(1,100):
14+
# if n%3==0:
15+
# print n;
16+
# 输出九九乘法表
17+
i=1
18+
while i<10:
19+
for j in range(1,i+1):
20+
print '%d*%d=%d'%(i,j,i*j),
21+
if j==i:
22+
print
23+
i=i+1;

src/python/demo/TestDataStruct.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-5
6+
# Desc : 数据结构 python常用数据结构有如下:(列表、元组、字典 )===>序列
7+
#
8+
9+
#列表 数据可以伸缩
10+
import Encode # @UnusedImport
11+
shoplist = ["apple","banana","orange"]
12+
shoplist.append("mango")
13+
shoplist.remove("orange")
14+
def cmpa(a,b):
15+
return -1;
16+
shoplist.sort(cmp=cmpa, key=None, reverse=False)
17+
print len(shoplist),
18+
for fruit in shoplist:
19+
print fruit,
20+
#元组 数据固定
21+
zoo = ('woft','elephant','tiger')
22+
new_zoo = ('bird','drogan',zoo)
23+
print len(zoo),
24+
print '\nold zoo:'
25+
for animal in zoo:
26+
print animal,
27+
print '\nnew zoo:'
28+
for naimal in new_zoo:
29+
print naimal,
30+
print new_zoo[2][1]
31+
print '========='
32+
33+
#切片
34+
#shoplist[:-1]会返回除了最后一个项目外包含所有项目的序列切片。
35+
for temp in zoo[1:3]:
36+
print temp
37+
#字典 (键值对,相当于Map
38+
data = {"name":"sst","age":24}
39+
print data["name"]
40+
print data.get("age")
41+
42+
43+
#对象复制(以下并没有实现真正的复制,真正的复制通过切片来实现)
44+
zoo = ['woft','elephant','tiger']
45+
print "=======对象复制======="
46+
myzoo = zoo #切片实现 eg: myzoo=zoo[:]
47+
print "zoo is %s,myzoo is %s"%(zoo,myzoo)
48+
del zoo[1]
49+
zoo.remove('woft')
50+
print "zoo is %s,myzoo is %s"%(zoo,myzoo)
51+
52+
53+
54+
55+
56+

src/python/demo/TestExtends.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-8
6+
# Desc :
7+
#
8+
class SchoolMember(object):
9+
"""Docstring for MySchoolMemberass """
10+
11+
def __init__(self,name,sex):
12+
"""@todo: to be defined1 """
13+
self.name = name
14+
self.sex = sex
15+
def tell(self):
16+
print 'Name:"%s"'%(self.name)
17+
class Teacher(SchoolMember):
18+
"""Docstring for Teacher """
19+
def __init__(self,name,sex,salary):
20+
"""@todo: to be defined1
21+
:name: @todo
22+
:sex: @todo
23+
"""
24+
SchoolMember.__init__(self,name,sex)
25+
self._name = name
26+
self._sex = sex
27+
self.level = 'teacher'
28+
self.sex = 'women'
29+
self.salary = salary
30+
def tell(self):
31+
print 'Name:%s,Salary:%s'%(self.name,self.salary)
32+
class Student(SchoolMember):
33+
"""Docstring for Studnet """
34+
35+
def __init__(self,name,age,mark):
36+
"""@todo: to be defined1 """
37+
SchoolMember.__init__(self,name,age)
38+
self.mark = mark
39+
def tell(self):
40+
print "Name:%s,Mark:%s"%(self.name,self.mark)
41+
teacher = Teacher('Mrs Zhang','men','3500/mon')
42+
student = Student('sushiting','men',100)
43+
member = [teacher,student]
44+
for m in member:
45+
m.tell()
46+
47+
print member
48+
print student.tell()

src/python/demo/TestLambda.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-9
6+
# Desc : 强大的匿名函数 lambda args:expression 其中args为参数,expression为表达式
7+
#
8+
def make_repeater(n):
9+
return lambda args:args*n
10+
twice = make_repeater(2)
11+
print twice(2)
12+
print twice("sushiting")
13+
14+
#Python中,如果函数体是一个单独的return expression语句,
15+
#开发者可以选择使用特殊的lambda表达式形式替换该函数:lambda parameters: expression
16+
aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
17+
low = 3
18+
high = 7
19+
filter(lambda x, l=low, h=high: h>x>l, aList)
20+
nalist = [i for i in aList if 3<i and i<7]
21+
print nalist
22+
# returns: [4, 5, 6]
23+
24+
fun = lambda x:pow(3,x)
25+
print fun(3)

src/python/demo/TestModule.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-9
6+
# Desc : 学习Python一些常用模块
7+
#
8+
import Encode # @UnusedImport
9+
import os
10+
print os.name
11+
print os.getcwd()
12+
print os.listdir("d:")
13+
os.system("dir")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding:utf-8 -*-
2+
#
3+
# Author : sushiting
4+
# E-mail : sushiting@163.com
5+
# Date : 2013-7-8
6+
# Desc :
7+
#
8+
try:
9+
s = raw_input("enter something:")
10+
s = int(s)
11+
print s/1
12+
except EOFError:
13+
print "EOFerror"
14+
except:
15+
print "something is error"
16+
else:
17+
print "it don't rais exception"
18+
finally:
19+
print "this is final block"

src/python/demo/__init__.py

Whitespace-only changes.

src/python/demo/shop.data

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(lp1
2+
S'apple'
3+
p2
4+
aS'orange'
5+
p3
6+
aS'banana'
7+
p4
8+
a.

0 commit comments

Comments
 (0)
0