odd-2% vi x.py odd-2% python x.py Traceback (most recent call last): File "x.py", line 28, in tk.mainloop() NameError: name 'tk' is not defined odd-2% !vi vi x.py odd-2% python x.py Fatal Python error: PyEval_RestoreThread: NULL tstate zsh: abort python x.py odd-2% vi x.py odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: a=[1,2,5,6,8,10] In [2]: a=[1,2,"abc",[1,2,3],{1:5,3:4}] In [3]: a Out[3]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [4]: from cPickle import * In [5]: a Out[5]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [6]: dumps(a) Out[6]: "(lp1\nI1\naI2\naS'abc'\np2\na(lp3\nI1\naI2\naI3\naa(dp4\nI1\nI5\nsI3\nI4\nsa." In [7]: b=dumps(a) In [8]: b Out[8]: "(lp1\nI1\naI2\naS'abc'\np2\na(lp3\nI1\naI2\naI3\naa(dp4\nI1\nI5\nsI3\nI4\nsa." In [9]: c=loads(b) In [10]: c Out[10]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [11]: f=file("z.txt","w") In [12]: f.write(b) In [13]: exit() Do you really want to exit ([y]/n)? y odd-2% odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: a --------------------------------------------------------------------------- NameError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () NameError: name 'a' is not defined In [2]: from cPickle import * In [3]: a=file("z.txt","r").read() In [4]: a Out[4]: "(lp1\nI1\naI2\naS'abc'\np2\na(lp3\nI1\naI2\naI3\naa(dp4\nI1\nI5\nsI3\nI4\nsa." In [5]: b=loads(a) In [6]: b Out[6]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [7]: a=file("z.txt","r") In [8]: c=load(a) In [9]: c Out[9]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [10]: a=file("zz.txt","w") In [11]: dump(a,c) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () TypeError: argument must have 'write' attribute In [12]: dump(c,a) In [13]: dump(c,a) In [14]: dump(c,a) In [15]: Do you really want to exit ([y]/n)? odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: a=file("zz.txt","r") In [2]: b=load(a) --------------------------------------------------------------------------- NameError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () NameError: name 'load' is not defined In [3]: from cP cPickle cProfile In [3]: from cPickle import * In [4]: b=load(a) In [5]: c=load(a) In [6]: d=load(a) In [7]: e=load(a) --------------------------------------------------------------------------- EOFError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () EOFError: In [8]: b Out[8]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [9]: c Out[9]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [10]: d Out[10]: [1, 2, 'abc', [1, 2, 3], {1: 5, 3: 4}] In [11]: Do you really want to exit ([y]/n)? odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: class student: ...: def __init__(): ...: self.name=None ...: self.zip=None ...: self.id=None ...: ...: In [2]: a=student() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () TypeError: __init__() takes no arguments (1 given) In [3]: class student: def __init__(self): self.name=None self.zip=None self.id=None ...: ...: In [8]: a=student() In [9]: a.name="steve" In [10]: a.zip=77584 In [11]: a.id=120110 In [12]: a Out[12]: <__main__.student instance at 0x10dcd7a28> In [13]: dir(a) Out[13]: ['__doc__', '__init__', '__module__', 'id', 'name', 'zip'] In [14]: a.name Out[14]: 'steve' In [15]: a Out[15]: <__main__.student instance at 0x10dcd7a28> In [16]: from cPickle import * In [17]: b=file("zz.txt","w") In [18]: dump(a,b) In [19]: Do you really want to exit ([y]/n)? odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: from cPickle import * In [2]: b=file("zz.txt","r") In [3]: a=load(b) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () AttributeError: 'FakeModule' object has no attribute 'student' In [4]: class student: ...: KeyboardInterrupt In [4]: class student: ...: "" ...: ...: In [6]: b.rewind() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () AttributeError: 'file' object has no attribute 'rewind' In [7]: b.seek(0) In [8]: a=load(b) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () AttributeError: 'FakeModule' object has no attribute 'student' In [9]: In [10]: import anydbm In [11]: a=[1,2,3,"abc",[1,2,4]] In [12]: In [13]: a Out[13]: [1, 2, 3, 'abc', [1, 2, 4]] In [14]: o=anydbm.open("zzz","c") In [15]: o Out[15]: In [16]: o["a"]="abcdefg" In [17]: o Out[17]: In [18]: o["a"] Out[18]: 'abcdefg' In [19]: Do you really want to exit ([y]/n)? odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: import anydbm In [2]: o=anydbm.open("zzz","c") In [3]: o["a"] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () KeyError: 'a' In [4]: a=[1,2,3,"abc",[1,2,4]] In [5]: o["a"]="abcd" In [6]: a["a"] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () TypeError: list indices must be integers, not str In [7]: o[a] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () TypeError: argument must be string or read-only buffer, not list In [8]: o["a"] Out[8]: 'abcd' In [9]: o.close() In [10]: Do you really want to exit ([y]/n)? odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: import anydbm In [2]: o=anydbm.open("zzz","c") In [3]: o["a"] Out[3]: 'abcd' In [4]: a=[1,2,3,"abc",[1,2,4]] In [5]: o["b"]=a --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () TypeError: dbm mappings have string elements only In [6]: from cPickle import * In [7]: o["b"]=dumps(a) In [8]: o["b"] Out[8]: "(lp1\nI1\naI2\naI3\naS'abc'\np2\na(lp3\nI1\naI2\naI4\naa." In [9]: loads(o["b"]) Out[9]: [1, 2, 3, 'abc', [1, 2, 4]] In [10]: import shelve In [11]: help(shelve) In [12]: p=shelve.open("zzzz.db") In [13]: p[1]=10 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/stevel/wp/lecture/2012_01_Intro_Programming/lec12/ in () /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shelve.pyc in __setitem__(self, key, value) 131 p = Pickler(f, self._protocol) 132 p.dump(value) --> 133 self.dict[key] = f.getvalue() 134 135 def __delitem__(self, key): TypeError: dbm mappings have string indices only In [14]: p["1"]=10 In [15]: p["2"]=a In [16]: p["2"] Out[16]: [1, 2, 3, 'abc', [1, 2, 4]] In [17]: p.close() In [18]: Do you really want to exit ([y]/n)? odd-2% ipython Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) Type "copyright", "credits" or "license" for more information. IPython 0.10.2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: from cP cPickle cProfile In [1]: import shelve In [2]: p=shelve.open("zzzz.db") In [3]: p["1"] Out[3]: 10 In [4]: p["2"] Out[4]: [1, 2, 3, 'abc', [1, 2, 4]] In [5]: Do you really want to exit ([y]/n)?