97日目:Pythonプログラミング
Pythonプログラミングの講義31日目です。
今日の講義は、標準モジュールについて。内職は課題のclass化。
【講義内容】
- 標準モジュール
【ワンポイント】
shelve
import
import shelve
create
test = shelve.open('test.data') test['x'] = ['a', 'b', 'c'] test['x'].append('d') # don't append print(test['x']) test.close()
read
test = shelve.open('test.data') print(test['x']) test.close()
append
test = shelve.open('test.data') tmp = test['x'] tmp.append('d') test['x'] = tmp print(test['x']) test.close()
delete
test = shelve.open('test.data') test['z'] = ['A', 'B'] print(f"test['z']: {test['z']}") test['z'].remove('B') # don't remove [print(f'key: {k} , values: {test[k]}') for k in test.keys()] tmp = test['z'] tmp.remove('B') test['z'] = tmp [print(f'key: {k} , values: {test[k]}') for k in test.keys()] test.close()
in memory
test = shelve.open('test.data', writeback=True) test['x'] = ['a', 'b', 'c'] print(test['x']) test['x'].append('d') [print(f'key: {k} , values: {test[k]}') for k in test.keys()] test.sync() test.close()
出力
['a', 'b', 'c'] ***************************************** ['a', 'b', 'c'] ***************************************** ['a', 'b', 'c', 'd'] ***************************************** test['z']: ['A', 'B'] key: x , values: ['a', 'b', 'c', 'd'] key: z , values: ['A', 'B'] key: x , values: ['a', 'b', 'c', 'd'] key: z , values: ['A'] ***************************************** ['a', 'b', 'c'] key: x , values: ['a', 'b', 'c', 'd'] key: z , values: ['A']
【今日の積み上げ】
- Pythonの基礎