This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@singleton | |
class Counter: | |
def __init__(self): | |
self.count = 0 | |
def inc(self): | |
self.count += 1 | |
print type(Counter) # <type 'function'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OneOnly { | |
private static final OneOnly the_one = new OneOnly(); | |
private OneOnly() { | |
} | |
public static get() { | |
return the_one; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> print Counter() is Counter() | |
True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def singleton(cls): | |
instances = {} # Line 2 | |
def getinstance(): | |
if cls not in instances: | |
instances[cls] = cls() # Line 5 | |
return instances[cls] | |
return getinstance | |
class Counter: | |
def __init__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> print waztile.istitle.__doc__ | |
S.istitle() -> bool | |
Return True if S is a titlecased string and there is at least one | |
character in S, i.e. uppercase characters may only follow uncased | |
characters and lowercase characters only cased ones. Return False | |
otherwise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> waztile = "not poison" | |
>>> type(waztile) | |
<type 'str'> | |
>>> print waztile | |
not poison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> dir(waztile) | |
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', | |
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', | |
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', | |
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', | |
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', | |
'__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', | |
'_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', | |
'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', | |
'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# unittest.py | |
class TestProgram: | |
def __init__(...): | |
... | |
self.runTests() | |
main = TestProgram |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
class TestSomething(unittest.TestCase): | |
def test1(self): | |
self.assert_(True) | |
if __name__ == '__main__': | |
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
attr_accessor :role | |
has_many :user_roles, :dependent => :destroy | |
has_many :roles, :through => :user_roles | |
after_save :set_role | |
def set_role | |
if @role # NOTE (A): @role is nil. I want it to be params[:user][:role]. | |
# NOTE (B): do stuff here, mostly: | |
self.roles << @role |