# IPython log file get_ipython().magic(u'logstart') lst = range(10, 89, 7) lst [n for n in lst if n % 2 == 0] newlst = [] for item in lst: newlst.append(item + 1) (n for n in lst if n % 2 == 0) g = (n for n in lst if n % 2 == 0) len(g) range(4) xrange(4) for i in range(10): print "hello" for i in xrange(10): print "hello" def foo(): while True: return 1 foo() def foo(): while True: yield 1 foo() g = foo() g.next() g.next() g.next() g.next() def foo(): for i in range(10): yield i for num in foo(): print num [x for x in foo()] class Foo(object): x = 1 def foo(self): print id(self) Foo.foo() f = Foo() f.foo() f2 = Foo() f2.foo() f = Foo() class CamelCase(object): pass dir(Foo) Foo.y = 1 Foo.y Foo.__dict__ dir(Foo) Foo.__doc__ class Foo(object): """ Just testing here""" pass Foo.__doc__ help(Foo) Foo.__class__ f.__class__ f.__class__ is Foo Foo.__class__ class Foo(object): def __init__(self, x, y): self.x = x self.y = y f = Foo(3, 4) f.x class Foo(object): def __init__(self, x, y): self._x = x self._y = y f = Foo(4,5) f._x f._x= 3 class Foo(object): def __init__(self, x): self.__x = x f = Foo() f = Foo(3) f.__x class Foo(object): def __init__(self, x): self.__x = x def out(self): return self.__x f = Foo(3) f.out() f.__x f.__dict__ f._Foo__x f._Foo__x = 3 f._Foo__x = 2 f.out() print f str(f) def Foo(object): def __init__(self, x): self.x = x def __str__(self): return "%s: %s" % (self, x) def Foo(object): def __init__(self, x): self.x = x def __str__(self): return "%s: %s" % (self, self.x) f = Foo(3) print f f.__str__() def Foo(object): def __init__(self, x): self.x = x def __str__(self): s ="%s: %s" % (self, self.x) return s f = Foo(43) print f f.__str__(self) f.__str__() def Foo(object): def __init__(self, x): self.x = x def __str__(self): "This is an object" f = Foo() f = Foo(4) print f def Foo(object): def __init__(self, x): self.x = x def __str__(self): return "This is an object" f = Foo() f = Foo(34) print f class Foo(object): def __init__(self, x): self.x = x def __str__(self): return "%s: %s" % (self, self.x) f = Foo() f = Foo(34) print f Foo class Foo(object): def __init__(self, x): self.x = x def __str__(self): return "%s: %s" % (id(self), self.x) f = Foo(4) print f f2 = Foo(45) f + f2 help(str) class Foo(object): def __init__(self, x): self.x = x def __add__(self, other): return self.x + other.x f = Foo(4) f2 = Foo(5) f + f2 f() f[0] f < f2 f > f2 lst lst.length len(lst) class Foo(object): def __init__(self): self.lst = range(10) def __iter__(self): return self.lst f = Foo() for i in f: print i class Foo(object): def __init__(self): self.lst = range(10) def __iter__(self): return (item for item in self.lst) f = Foo() for item in f: print item class Foo(object): def foo(self): return id(self) f = Foo() f.foo f.foo() Foo.foo Foo.foo(f) class Foo(object): def foo(self): return id(self) def add_1(s): return s + "1" Foo.add_1("asdf") class Foo(object): def foo(self): return id(self) @staticmethod def add_1(s): return s + "1" Foo.add_1('asdf') f = Foo() f.add_1('asdf') class Foo(object): x = 1 def universal_update(x): Foo.x = x Foo.universal_update(3) class Foo(object): @staticmethod def univ(x): Foo.x = x class Foo(object): x = 1 @classmethod def update(cls): cls.x = x Foo.update(14) class Foo(object): x = 1 @classmethod def update(cls, x): cls.x = x Foo.update(14) Foo.x class Parent(object): def foo(self): print 1 class Child1(Parent): pass p = Parent() p.foo() c = Child1() c.foo() c.__class__.mro() class Child1(Parent): def foo(self, 2): print 2 class Child1(Parent): def foo(self: print 2 class Child1(Parent): def foo(self): print 2 p = Parent() p.foo() c = Child1() c.foo() get_ipython().magic(u'pinfo super') class Child1(Parent): def foo(self): super(Child1, self).foo() print 2 c = Child1() c.foo() import abc class VirtualParent(object): __metaclass__ = abc.ABCMeta class VirtualParent(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def foo(self): print "Base class implementation" get_ipython().magic(u'hist ') obj = VirtualParent() class Child(VirtualParent): pass obj = Child() class Child(object): def foo(self): super(Child, self).foo() class Child(VirtualParent): def foo(self): super(Child, self).foo() c = Child() c.foo() "%s" % 1 "%s %s" % (1, 2) "%(x)s %(y)s" % {'x': 2, 'y': 4} f f.__dict__ import classmates10 classmates10.__dict__ import classmates10 classmates10.__dict__ f f.y f.__dict__ class Foo(object): def __init__(self, x): self.x = 1 class Foo(object): def __init__(self, x): self.x = x f = Foo() f = Foo(5) f.x f.x = 10 f.x class Foo(object): def __init__(self, x): self._x = x @property def x(self): return self._x + 1 f = Foo(4) f.x f.__dict__ class Foo(object): def __init__(self, x): self._x = x @property def x(self): return self._x + 1 @x.setter def x(self, val): self._x = val - 1 get_ipython().magic(u'hist ') f = Foo(4) f.x f.x = 10 f.__dict__ get_ipython().magic(u'pinfo property') get_ipython().magic(u'pinfo2 property') del(f.x) import this get_ipython().magic(u'pinfo abc.abstractproperty') def outer(f): def inner(*args, **kwargs): print "Before the function" return f(*args, **kwargs) return inner @outer def sub(x, y): return x - y sub(4,5) class Decorator(object): pass @Decorator def sub(x, y): return x - y class Decorator(object): def __init__(self, f): self.f = f @Decorator def sub(x, y): return x - y sub sub() class Decorator(object): def __init__(self, f): self.f = f def __call__(self, *args, **kwargs): print "before" r = self.f(*args, **kwargs) print "after" return r @Decorator def sub(x, y): return x - y sub(4,5) import itertools get_ipython().magic(u'pinfo itertools.cycle') itertools.cycle(['blue', 'white']) g = itertools.cycle(['blue', 'white']) g.next() g.next() g.next() g.next() g.next() g.next() g.next() g.next() g.next() g.next() g.next() g.next() g.next() get_ipython().magic(u'pinfo itertools.starmap') get_ipython().magic(u'pinfo itertools.groupby') import functools import contextlib get_ipython().magic(u'pinfo contextlib') get_ipython().magic(u'pinfo contextlib.contextmanager') import this reload(this) import glob get_ipython().magic(u'pinfo glob.glob') glob.glob("/tmp/*txt") glob.glob("*txt") import os get_ipython().magic(u'pinfo os.mkdir') os.getcwd() os.path os.path.isfile("/tmp") os.path.isdir("/tmp") get_ipython().magic(u'pinfo os.path.walk') get_ipython().magic(u'pinfo os.walk') os.walk(".") list(os.walk(".")) import unittest from __future__ import print_function def verbose_print(*args): if verbose: print *args verbose = False verbose_print("asdf", [1,2,2]) verbose_print("asdf", [1,2]) verbose = True verbose_print("asdf", [1,2,2]) def verbose_print(*args): if verbose: print(*args) verbose_print("asdf", [1,2,2]) verbose = Flase verbose = False verbose_print("asdf", [1,2,2]) import sys get_ipython().system(u'ls -F ') get_ipython().system(u'ls') pattern = "*.py" get_ipython().system(u'ls $pattern') dir(1) 1 dir(1) 1.numerator() 1.numerator "".join() dir(1) class(1) type(1) 1.foo