您的位置:首页 > 编程语言 > Python开发

python学习记录之1027

2015-10-27 17:25 225 查看
Last login: Mon Oct 26 19:50:07 on ttys000
QXdeMacBook-Pro:~ qx$ cd Desktop/workspace/
QXdeMacBook-Pro:workspace qx$ ls
array		deduplication	grep		overflow	test.txt	zhishu.cpp
convert		deduplication.c	hello.py	qx.JPG		thumb_qx.jpg
convert.c	get-pip.py	hello.pyc	test		zhishu
QXdeMacBook-Pro:workspace qx$ class Student(object):
-bash: syntax error near unexpected token `('
QXdeMacBook-Pro:workspace qx$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Student(object):
...     pass
...
>>> s=Student()
>>> s.name='Qx'
>>> print s.name
Qx
>>> def set_age(self,age):
...     self.age=age
...
>>> from types import MethodType
>>> s.set_age=MethodType(set_age,s,Student)
>>> s.set_age(22)
>>> s.age
22
>>> s2=Student()
>>> s2.set_age(11)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute 'set_age'
>>> def set_score(self,score):
...     self.score=score
...
>>> Student.set_score=MethodType(set_score, None, Student)
>>> s.set_score(100)
>>> s.score
100
>>> s2.set_score(99)
>>> s2.score
99
>>> class Student(object):
...     __slot__=('name','age')
...
>>> s=Student()
>>> s.name='Qx'
>>> s.name
'Qx'
>>> s.age=22
>>> s.age
22
>>> s.score=100
>>> s.score
100
>>> class Student(object):
...     __slots__=('name','age')
...
>>> s=Student()
>>> s.name='Qx'
>>> s.age=22
>>> s.score=100
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Student' object has no attribute 'score'
>>> class GraduateStudent(Student):
...     pass
...
>>> g=GraduateStudent
>>> g=GraduateStudent()
>>> g.score=100
>>> g.score
100
>>> class Student(object);
File "<stdin>", line 1
class Student(object);
^
SyntaxError: invalid syntax
>>> class Student(object):
...     @property
...     def score(self):
...             return self._score
...     @score.setter
...     def score(self,value):
...             if not isinstance(value,int):
...                     raise ValueError('score must be an integer!')
...             if value <0 or value>100:
...                     raise ValueError('score must be between 0~100!')
...             self._score=value
...
>>> s=Student()
>>> s.score
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in score
AttributeError: 'Student' object has no attribute '_score'
>>> s.score=60
>>> s.score
60
>>> s.score=999
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 10, in score
ValueError: score must be between 0~100!
>>> class Fib(object):
...     def __init__(self):
...             self.a,self.b=0,1
...     def __iter__(self):
...             return self
...     def next(self):
...             self.a,self.b=self.b,self.a+self.b
...             if self.a>10000:
...                     raise StopIteration()
...             return self.a
...
>>> for n in fib():
...     print n
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fib' is not defined
>>> for n in Fib():
...     print n
...
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
>>>
QXdeMacBook-Pro:~ qx$ cd Desktop/workspace/
QXdeMacBook-Pro:workspace qx$ vim dlTiebaImg.py
QXdeMacBook-Pro:workspace qx$ python dlTiebaImg.py
QXdeMacBook-Pro:workspace qx$ vim fib.py
QXdeMacBook-Pro:workspace qx$ python fib.py
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
QXdeMacBook-Pro:workspace qx$ vim fib.py
QXdeMacBook-Pro:workspace qx$ python fib.py
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
573147844013817084101
QXdeMacBook-Pro:workspace qx$ vim fib.py
QXdeMacBook-Pro:workspace qx$ python fib.py
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
[1, 2, 3, 5]
573147844013817084101
QXdeMacBook-Pro:workspace qx$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Chain(object):
...     def __init__(self,path=''):
...             self._path=path
...     def __getattr__(self,path):
...             return Chain('%s/%s' % (self._path,path))
...     def __str__(self):
...             return self._path
...
>>> Chain().status.user.timeline.list
<__main__.Chain object at 0x10a402410>
>>> Chain().status.user.timeline.list
<__main__.Chain object at 0x10a402490>
>>> Chain().status.user.timeline
<__main__.Chain object at 0x10a402450>
>>> Chain().status
<__main__.Chain object at 0x10a402490>
>>> print Chain().status.user.timeline.list
/status/user/timeline/list
>>> Chain().status.user.timeline.list._path
'/status/user/timeline/list'
>>> callable(Chain())
False
>>> callable(abs)
True
>>> callable(max)
True
>>> max([1,2,3,5])
5
>>> max(1,2,3,5)
5
>>> class Student(object):
...     def __init__(self,name):
...             self.name=name
...     def __call__(self):
...             print 'My name is %s' % self.name
...
>>> s=Student('qx')
>>> s()
My name is qx
>>> callable(Student())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 2 arguments (1 given)
>>> callable(Student('qx'))
True
>>> callable(str)
True
>>> callable('sfjjk')
False
>>> from fib import Fib
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
[1, 2, 3, 5]
573147844013817084101
>>> exit()
QXdeMacBook-Pro:workspace qx$ vim hello.py
QXdeMacBook-Pro:workspace qx$ vim helloworld.py
QXdeMacBook-Pro:workspace qx$ from helloworld import Hello
from: can't read /var/mail/helloworld
QXdeMacBook-Pro:workspace qx$ vim helloworld.py
QXdeMacBook-Pro:workspace qx$ from helloworld import Hello
from: can't read /var/mail/helloworld
QXdeMacBook-Pro:workspace qx$ h=Hello()
-bash: syntax error near unexpected token `('
QXdeMacBook-Pro:workspace qx$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from helloworld import Hello
>>> h=Hello()
>>> h.hello('Qx')
hello, Qx!
>>> print type(Hello)
<type 'type'>
>>> print (type(h))
<class 'helloworld.Hello'>
>>> type(h)
<class 'helloworld.Hello'>
>>> def fn(self,name='world'):
...     print 'hello, %s!' % name
...
>>> Hello=type('Hello',(object,),dict(hello=fn))
>>> h=Hello()
>>> h.hello()
hello, world!
>>> h=Hello('Qx')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters
>>> type(Hello)
<type 'type'>
>>> type(h)
<class '__main__.Hello'>
>>> class Student(object):
...     name='Qx'
...
>>> s=Student()
>>> print s.name
Qx
>>> print Student.name
Qx
>>> s.name='qx'
>>> print s.name
qx
>>> print Student.name
Qx
>>> del s.name
>>> pritn s.name
File "<stdin>", line 1
pritn s.name
^
SyntaxError: invalid syntax
>>> print s.name
Qx
>>> f=open('test.txt','r')
>>> f.read()
'hello world\n'
>>> f.read()
''
>>> f=open('test.txt','r')
>>> f.read()
'hello world!'
>>> f.close()
>>> with open('test.txt','r') as f:
...     print f.read()
...
hello world!
>>> f=open('test.txt','r')
>>> for line in f.readlines():
...     print line.strip()
...
hello world!
my name is Qx!
this is a test file!
>>> f.read(8)
''
>>> f.read(18)
''
>>> f=open('test.txt','r')
>>> f.read(18)
'hello world!\nmy na'
>>> f.read(18)
'me is Qx!\nthis is '
>>> f.read(18)
'a test file!'
>>> f=open('test.txt','r')
>>> f.readline()
'hello world!\n'
>>> f.readline()
'my name is Qx!\n'
>>> f.readline()
'this is a test file!'
>>> f.readlines()
[]
>>> f=open('test.txt','r')
>>> f.readlines()
['hello world!\n', 'my name is Qx!\n', 'this is a test file!']
>>> '  dfdjfhj  '.strip()
'dfdjfhj'
>>> f=open('test.txt','r')
>>> f.read()
'hello world!\nmy name is Qx!\nthis is a test file!\n\xe6\x88\x91\xe5\x8f\xab\xe7\xa7\xa6\xe5\xad\xa6'
>>> with codecs.open('test.txt','r') as f:
...     f.read()
...
'hello world!\nmy name is Qx!\nthis is a test file!\n\xe6\x88\x91\xe5\x8f\xab\xe7\xa7\xa6\xe5\xad\xa6'
>>> with open('test.txt','w') as f:
...     f.write('i like python!')
...     f.write('i like programming!')
...
>>> import os
>>> os.name
'posix'
>>> os.uname()
('Darwin', 'QXdeMacBook-Pro.local', '14.1.1', 'Darwin Kernel Version 14.1.1: Fri Feb  6 21:06:10 PST 2015; root:xnu-2782.15.4~1/RELEASE_X86_64', 'x86_64')
>>> os.environ
{'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'TERM_PROGRAM_VERSION': '343.6.2', 'LOGNAME': 'qx', 'USER': 'qx', 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin', 'HOME': '/Users/qx', 'TERM_PROGRAM': 'Apple_Terminal', 'LANG': 'zh_CN.UTF-8', 'TERM': 'xterm-256color', 'Apple_PubSub_Socket_Render': '/private/tmp/com.apple.launchd.isjXiiHcHH/Render', 'VERSIONER_PYTHON_VERSION': '2.7', 'SHLVL': '1', 'XPC_FLAGS': '0x0', 'TMPDIR': '/var/folders/tj/mjd3g45n07vchnlgbq7775j40000gn/T/', 'TERM_SESSION_ID': 'A933D085-A7C1-427C-B158-693E450E723C', 'XPC_SERVICE_NAME': '0', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.0SKuqFWguX/Listeners', 'SHELL': '/bin/bash', '_': '/usr/bin/python', 'OLDPWD': '/Users/qx', '__CF_USER_TEXT_ENCODING': '0x1F5:0x19:0x34', 'PWD': '/Users/qx/Desktop/workspace'}
>>> os.getenv('PATH')
'/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin'
>>> os.path.abspath('.')
'/Users/qx/Desktop/workspace'
>>> os.path.abspath('')
'/Users/qx/Desktop/workspace'
>>> os.path.abspath()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abspath() takes exactly 1 argument (0 given)
>>> os.path.abspath('')
'/Users/qx/Desktop/workspace'
>>> os.path.join('/Users/qx','Desktop')
'/Users/qx/Desktop'
>>> os.path.join('/Users/qx','workspace')
'/Users/qx/workspace'
>>> os.mkdir('testdir')
>>> os.mkdir('../testdir')
>>> os.rmdir('../testdir')
>>> os.path.split('testdir')
('', 'testdir')
>>> os.path.split('/User/qx/Desktop/workspace/testdir')
('/User/qx/Desktop/workspace', 'testdir')
>>> os.path.split('/User/qx/Desktop/testdir')
('/User/qx/Desktop', 'testdir')
>>> os.path.splitext('/path/to/file.txt')
('/path/to/file', '.txt')
>>> [x for x in os.listdir('.') if os.path.isdir(x)]
['array', 'grep', 'overflow', 'test', 'testdir']
>>> [x for x in os.listdir('..') if os.path.isdir(x)]
[]
>>> [x for x in os.listdir('../') if os.path.isdir(x)]
[]
>>> [x for x in os.listdir('../picture') if os.path.isdir(x)]
[]
>>> [x for x in os.listdir('/User/qx/Desktop') if os.path.isdir(x)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/User/qx/Desktop'
>>> [x for x in os.listdir('/Users/qx/Desktop') if os.path.isdir(x)]
[]
>>> [x for x in os.listdir('.') if os.path.isdir(x)]
['array', 'grep', 'overflow', 'test', 'testdir']
>>> [x for x in os.listdir('.') if os.path.isdir(x) and os.path.splitext(x)[1]=='.py']
[]
>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
['dlTiebaImg.py', 'fib.py', 'get-pip.py', 'hello.py', 'helloworld.py']
>>> ['11te','wwte','33te'].find('te')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'find'
>>> 'fffffte'.find('te')
5
>>> def search(s):
...     for x in os.listdir('.'):
...             if os.path.isfile(x) and os.path.splitext(x)[0].find(s):
...                     print x
...             if os.path.isdir(x):
...                     search(s)
File "<stdin>", line 5
if os.path.isdir(x):
^
IndentationError: expected an indented block
>>> def search(s):
...     for x in os.listdir('.'):
...             if os.path.isfile(x) and os.path.splitext(x)[0].find(s):
...                     print os.path.splitext(x)[1]+x
File "<stdin>", line 3
if os.path.isfile(x) and os.path.splitext(x)[0].find(s):
^
IndentationError: expected an indented block
>>> for x in os.listdir('.'):
...     print x
...
.DS_Store
array
convert
convert.c
deduplication
deduplication.c
dlTiebaImg.py
fib.py
fib.pyc
get-pip.py
grep
hello.py
hello.pyc
helloworld.py
helloworld.pyc
overflow
qx.JPG
test
test.txt
testdir
thumb_qx.jpg
zhishu
zhishu.cpp
>>> for x in os.listdir('.'):
...     print os.path.splitext(x)[0]
...
.DS_Store
array
convert
convert
deduplication
deduplication
dlTiebaImg
fib
fib
get-pip
grep
hello
hello
helloworld
helloworld
overflow
qx
test
test
testdir
thumb_qx
zhishu
zhishu
>>> def search(s):
...     __path=['.']
...     for x in os.listdir()
KeyboardInterrupt
>>> def search(s):
...     __file=[]
...     __path=['.']
...     for i in __path:
...             for x in os.listdir(i):
...                     if os.path.isdir(x):
...                             __path.append(x)
...                     if t in x:
...                             __file.append(os.path.join(i,x))
...     return __file
...
>>> search('ello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in search
NameError: global name 't' is not defined
>>> def search(s):
...     __file=[]
...     __path=['.']
...     for i in __path:
...             for x in os.listdir(i):
...                     if os.path.isdir(x):
...                             __path.append(x)
...                     if s in x:
...                             __file.append(os.path.join(i,x))
...     return __file
...
>>> search('ello')
['./hello.py', './hello.pyc', './helloworld.py', './helloworld.pyc']
>>> search('py')
['./dlTiebaImg.py', './fib.py', './fib.pyc', './get-pip.py', './hello.py', './hello.pyc', './helloworld.py', './helloworld.pyc']
>>> search('get')
['./get-pip.py', 'grep/getSalary', 'grep/getSalary.c']
>>> def search(s):
...     file=[]
...     path=['.']
...     for p in path:
...             for x in os.listdir(p):
...                     if os.path.isdir(x):
...                             path.append(x)
...                     if s in os.path.splitext(x)[0]:
...
File "<stdin>", line 9

^
IndentationError: expected an indented block
>>> def search(s):
...     file=[]
...     path=['.']
...     for p in path:
...             for x in os.listdir(p):
...                     if os.path.isdir(x):
...                             path.append(x)
...                     if s in os.path.splitext(x)[0]:
...                             file.append(os.path.join(p,x))
...     return file
...
>>> search('py')
[]
>>> search('ello')
['./hello.py', './hello.pyc', './helloworld.py', './helloworld.pyc']
>>> search('get')
['./get-pip.py', 'grep/getSalary', 'grep/getSalary.c']
>>> search('get')
['./get-pip.py', 'grep/getSalary', 'grep/getSalary.c']
>>> def search(s):
...     file=[]
...     path=['.']
KeyboardInterrupt
>>> os.listdir('.')
['.DS_Store', 'array', 'convert', 'convert.c', 'deduplication', 'deduplication.c', 'dlTiebaImg.py', 'fib.py', 'fib.pyc', 'get-pip.py', 'grep', 'hello.py', 'hello.pyc', 'helloworld.py', 'helloworld.pyc', 'overflow', 'qx.JPG', 'test', 'test.txt', 'testdir', 'thumb_qx.jpg', 'zhishu', 'zhishu.cpp']
>>> os.listdir('array')
['.DS_Store', 'main', 'main.c', 'main.dSYM']
>>> os.listdir('grep')
['.DS_Store', 'a', 'a.c', 'getSalary', 'getSalary.c', 'test']
>>> os.listdir('grep/test')
['getSalary.c']
>>> os.listdir('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: ''
>>>
>>> os.listdir('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: ''
>>> os.listdir('.')
['.DS_Store', 'array', 'convert', 'convert.c', 'deduplication', 'deduplication.c', 'dlTiebaImg.py', 'fib.py', 'fib.pyc', 'get-pip.py', 'grep', 'hello.py', 'hello.pyc', 'helloworld.py', 'helloworld.pyc', 'overflow', 'qx.JPG', 'test', 'test.txt', 'testdir', 'thumb_qx.jpg', 'zhishu', 'zhishu.cpp']
>>> os.listdir('./')
['.DS_Store', 'array', 'convert', 'convert.c', 'deduplication', 'deduplication.c', 'dlTiebaImg.py', 'fib.py', 'fib.pyc', 'get-pip.py', 'grep', 'hello.py', 'hello.pyc', 'helloworld.py', 'helloworld.pyc', 'overflow', 'qx.JPG', 'test', 'test.txt', 'testdir', 'thumb_qx.jpg', 'zhishu', 'zhishu.cpp']
>>> os.listdir('/')
['.DocumentRevisions-V100', '.file', '.fseventsd', '.PKInstallSandboxManager', '.Spotlight-V100', '.Trashes', '.vol', 'Applications', 'bin', 'cores', 'dev', 'etc', 'home', 'installer.failurerequests', 'Library', 'net', 'Network', 'private', 'sbin', 'System', 'tmp', 'Users', 'usr', 'var', 'Volumes']
>>> def search(s):
...     file=[]
...     path=['.']
...     for p in path:
...             for x in os.listdir(p):
...                     if os.path.isfile(x):
...                             path.append(os.path.join(p,x))
File "<stdin>", line 6
if os.path.isfile(x):
^
IndentationError: expected an indented block
>>> def search(s):
...     file=[]
...     path=['.']
...     for p in path:
...             for x in os.listdir(p):
...                     if os.path.isdir(x):
...                             path.append(os.path.join(p,x))
...                     if s in os.path.splitext(x)[0]:
...                             file.append(os.path.join(p,x))
...     return file
...
>>> search('py')
[]
>>> search('llo')
['./hello.py', './hello.pyc', './helloworld.py', './helloworld.pyc']
>>> search('get')
['./get-pip.py', './grep/getSalary', './grep/getSalary.c', './grep/test/getSalary.c']
>>> search('a')
['./array', './deduplication', './deduplication.c', './dlTiebaImg.py', './array/main', './array/main.c', './array/main.dSYM', './grep/a', './grep/a.c', './grep/getSalary', './grep/getSalary.c', './overflow/main', './overflow/main.c', './overflow/main.dSYM', './grep/test/getSalary.c']
>>> def search(s):
...     file=[]
...     path=['.']
...     for p in path:
...             for x in os.listdir(p):
...                     if os.path.isdir(x):
...                             path.append(os.path.join(p,x))
...                     else:
...                             if s in os.path.splitext(x)[0]:
...                                     file.append(os.path.join(p,x))
...     return file
...
>>> search('a')
['./deduplication', './deduplication.c', './dlTiebaImg.py', './array/main', './array/main.c', './array/main.dSYM', './grep/a', './grep/a.c', './grep/getSalary', './grep/getSalary.c', './overflow/main', './overflow/main.c', './overflow/main.dSYM', './grep/test/getSalary.c']
>>> search('a')
['./deduplication', './deduplication.c', './dlTiebaImg.py', './array/main', './array/main.c', './array/main.dSYM', './grep/a', './grep/a.c', './grep/getSalary', './grep/getSalary.c', './overflow/main', './overflow/main.c', './overflow/main.dSYM', './grep/test/getSalary.c']
>>> search('006')
['./test/006', './test/006.c']
>>> search('006')
['./test/006', './test/006.c']
>>> search('main')
['./array/main', './array/main.c', './array/main.dSYM', './overflow/main', './overflow/main.c', './overflow/main.dSYM']
>>> search('main2')
[]
>>> search('get')
['./get-pip.py', './grep/getSalary', './grep/getSalary.c', './grep/test/getSalary.c']
>>> search('fib')
['./fib.py', './fib.pyc']
>>> search('main2')
[]
>>> search('main1')
[]
>>> search('get')
['./get-pip.py', './grep/getSalary', './grep/getSalary.c', './grep/test/getSalary.c']
>>> search('get')
['./get-pip.py', './grep/getSalary', './grep/getSalary.c', './grep/test/getSalary.c']
>>> search('main1')
[]
>>> search('main2')
[]
>>> search('main3')
[]
>>> search('main')
['./array/main', './array/main.c', './array/main.dSYM', './overflow/main', './overflow/main.c', './overflow/main.dSYM']
>>> search('fib')
['./fib.py', './fib.pyc']
>>> search('get')
['./get-pip.py', './grep/getSalary', './grep/getSalary.c']
>>> search('fib')
['./fib.py', './fib.pyc']
>>> search('get')
['./get-pip.py', './grep/getSalary', './grep/getSalary.c']
>>> search('zhi')
['./zhishu', './zhishu.cpp']
>>> search('Sala')
['./grep/getSalary', './grep/getSalary.c']
>>> os.listdir(./grep/test)
File "<stdin>", line 1
os.listdir(./grep/test)
^
SyntaxError: invalid syntax
>>> os.listdir('./grep/test')
['getSalary.c']
>>> search('Sala')
['./grep/getSalary', './grep/getSalary.c']
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: