#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW #log# opts = Struct({'__allownew': True, 'logfile': 'ipython_log.py'}) #log# args = [] #log# It is safe to make manual edits below here. #log#----------------------------------------------------------------------- import matplotlib _ip.magic("logstart ") _ip.system("ls -F *txt") _ip.system("less sample.txt") _ip.system("less file.") _ip.system("less file.txt") import random x = [random.randint() for i in range(10)] #?random.random x = [random.random() for i in range(10)] x x x = (random.random() for i in range(10)) x class bird(object): def __init__(self, color): self.color = color crow = bird("black") class bird(object): def __init__(self, color): self.color = color def print_color(self): print self.color crow = bird("black") crow.print_color() bluebird = bird("blue") bluebird.print_color() bluebird.color crow.black crow.color id(crow) id(bluebird) class bird(object): def __init__(self, color, wingspan): self.color = color; self.ws = wingspan def print_color(self): print self.color class bird(object): def __init__(self, color): self.canfly = True self.color = color crow = bird("black") crow.color crow.__class__ crow.__doc__ crow.__dict__ locals() crow.__dict__ crow.__dict__['color'] crow.__dict__['color'] = 'red' crow.color crow.color = 'red' bird bird.__dict__ bird.__dict__['variable'] = 1 class bird(object): counter = 0 def __init__(self, color): self.color = color bird.count += 1 class bird(object): counter = 0 def __init__(self, color): self.color = color bird.counter += 1 crow = bird("black") raven = bird("black") bird.counter bird.__dict__ bird.__dict__['counter'] crow.__dict__ bird.counter crow.counter class utility(object): def foo(x,y): return x * y utility.foo(2,4) class utility(object): @staticmethod def foo(x,y): return x * y utility.foo(34,7) class utility(object): def foo(cls, x, y): return cls.adjustment + x * y f class utility(object): def foo(cls, x, y): return cls.adjustment + x * y utility.foo(utility, 34,6) class utility(object): adjustment = 10 @staticmethod def foo(cls, x, y): return cls.adjustment + x * y utility.foo(utility, x, y) utility.foo(utility, 4,5) class utility(object): z = 10 @classmethod def foo(kls, x): return kls.z + x utility.foo(4) u = utility() u.foo(4) u.z u.z = 11 utility.z u.foo(4) u.__dict__ utility.z u.foo(1) utility.foo(1) class utlity(object): @staticmethod def foo(1): class utlity(object): @staticmethod def foo(l): print l utlity.foo(1) u = utlity() u.foo(1) class u(object): def foo(): print 1 obj = u() obj.foo() u.__dict__ obj.__dict__ class cancall(object): def __call__(self, x): print x c = cancall() c(4) class foo(object): def __init__(self, x): self.x = x a = foo(1) b = foo(3) c = foo(2) x = [a,b,c] sorted(x) sorted(x)[0].x x [obj.x for obj in x] sorted(x, key=lambda i: i.x) [obj.x for obj in sorted(x, key=lambda i: i.x)] x = sorted(x, key=lambda i: i.x) x sorted(x) #?cmp class foo(object): def __init__(self, x): self.x = x def __lt__(self, other): return self.x < other.x _ip.magic("hist ") _ip.magic("hist -n") a = foo(1) b = foo(3) c = foo(2) x = [a,b,c] [obj.i for obj in x] [obj.x for obj in x] [obj.x for obj in sorted(x)] x[0] a < b a.x b.x b < a a == b x class foo(object): def __init__(self, x): self.x = x def __lt__(self, other): return self.x < other.x def __repr__(self): return self.x a = foo(1) #?sorted a class foo(object): def __init__(self, x): self.x = x def __lt__(self, other): return self.x < other.x def __repr__(self): return str(self.x) a = foo(1) z a a = foo(1) b = foo(5) c = foo(2) x = [a,b,c] x sorted(x) a type(a) class foo(object): pass bird class bird(object): counter = 0 def __init__(self, color): self.color = color bird.counter += 1 class crow(bird): def eats_carrion(self): returns True class crow(bird): def eats_carrion(self): return True b = bird() b = bird("white") c = crow("black") c.color class bird(object): def flies(): return True class emu(bird): pass class bird(object): def flies(self): return True class emu(object): def flies(self): return "Badly" b = bird() b.flies() e = emu() e.flies() class emu(bird): def flies(self): return "Badly" e = emu() e.flies e.flies() class Instrument(object): def __init__(self, name): self.name = name class Guitar(object): def __init__(self, f) class Guitar(Instrument): def __init__(self, name, strings=6): self.string = strings super(Guitar, self).__init__(name) i = Instruments("Trombone") i = Instrument("Trombone") i i.name g = Guitar("CF Martin", 12) g.name _ip.magic("hist ") g.name class Folk(Instrument): def __init__(name, year): self.year = year super(Folk, self).__init__(name) class Bazouki(Folk, Guitar): def __init__(self, ethnicity="greek"): self.ethnicity = ethnicity print(super(Bazouki, self)) b = Bazouki() Bazouki.__mro__ g g.name class Guitar(Instrument): def __getattr__(self, name): print "When does this get called?" print name if name in self.__dict__: print self.__dict__[name] g = Guitar("Martin") g.name g.strings class Guitar(Instrument): def __getattribute__(self, name): print "When does this get called?" print name if name in self.__dict__: print self.__dict__[name] g = Guitar("martin") g.name class Cur(object): def __init__(self, v): self.v = v # And check for negative numbers etc def __get__(self, instance, cls): return instance.value def __set__(self, instance, value): intance.value = value + 1 # Check for negative numbers etc class Instrument(object): val = Cur(0) i = Instrument() i.val class Instrument(object): val = Cur(0) def __init__(self): self.value = 0 i = Instrument() i.val i.val = 10 class Cur(object): def __init__(self, v): self.v = v # And check for negative numbers etc def __get__(self, instance, cls): return instance.value def __set__(self, instance, value): instance.value = value + 1 # Check for negative numbers etc class Instrument(object): val = Cur(0) def __init__(self): self.value = 0 i = Instrument() i.val i.val = 10 i.value i.val class Instrument(object): def __init__(self, name): self.name = name i = Instrument("M") i.name class Instrument(object): def __init__(self, name): self._name = name @property def name(self): return self._name + "Modified" i = Instrument("M") i.name i.name() i._name i.name class Instrument(object): def __init__(self, name): self._name = name @property def name(self): return self._name + "Modified" @name.setter def name(self, value): self._name = value + "SET" i = Instrument("M") i.name i.name = "Martin" i._name i.__dict__ #?i help(Instrument) _ip.magic("hist ") help(Instrument) type(i.name) #?i #?Instrument help(Instrument) class Instrument(object): def __init__(self, name): self._name = name @property def name(self): return self._name + "Modified" @name.setter def name(self, value): """Does this get nuked?"""f class Instrument(object): def __init__(self, name): self._name = name @property def name(self): return self._name + "Modified" @name.setter def name(self, value): """Does this get nuked?""" self._name = value help(Instrument) x = {"first": [1,2,3], "second": [4,5,6]} x = \ 3 x = 4 + 6 + 10 + 23 x = (4 + 6 + 20) reload datetime import datetime reload(datetime) x = ['1', '4', ' '] hex hex("10") #?hex int("0x10") int("0x10", 0) #?int int("0x10", 16) int("10", 16) int("10", 16) a = b = c = 0 c x1 = x2 = [] x1.append('col1') x2.append('col2') x x2 x = 0x10 x x = 7x10 x = 010 x x = 010 f = object() f f.__dict__ class foo(object): pass f = foo() f.__dict__ f.__dict__['x'] = 1 f.x import unittest #?unittest.findTestCases x = unittest.findTestCases() x = unittest.findTestCases(datetime) x x.run x.run() #?x.run x.run(res) res = '' x.run(res) res #?exec x = unittest.findTestCases(datetime) round #?round round(1/7) 1/7.0 round(1/7.0) round(1/7.0, 4) round(1/7.0, 2) "%.2f" % 1/7.0 "%.2f" % (1/7.0)