Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
More Jobs
Recent
Arrays, loops, strings...
Similar virtual attributes and their getters/setter
Check type is XML Serializable
Serena Collage slideshow
Shortest regular expression for matching a subdomain.
Caching Methods
Tab-Switching in jQuery
Apache log file sorting
How to find max, min of three numbers?
repeated code in rspec model test
Popular
Caching Methods
How to find max, min of three numbers?
Similar virtual attributes and their getters/setter
Shortest regular expression for matching a subdomain.
Arrays, loops, strings...
Check type is XML Serializable
Apache log file sorting
Tab-Switching in jQuery
Serena Collage slideshow
A simple factorial program.
Pastable version of
Classtemplate
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51</pre> <pre class="sunburst"><span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>! /usr/bin/python</span> <span style="color:#65B042;"><span style="color:#65B042;">"""</span> A python primer. pydoc, doctest and classes.</span> <span style="color:#65B042;"></span> <span style="color:#65B042;">Syntax:</span> <span style="color:#65B042;"> To generate man pages:</span> <span style="color:#65B042;"> > pydoc classtemplate</span> <span style="color:#65B042;"></span> <span style="color:#65B042;"> To try all the tests in doctest (and execute the script):</span> <span style="color:#65B042;"> > python classtemplate.py -v</span> <span style="color:#65B042;"><span style="color:#65B042;">"""</span></span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>Class definition</span> <span style="color:#99CF50;">class</span> <span style="text-decoration:underline;">Table</span>: <span style="color:#65B042;"><span style="color:#65B042;">"""</span>Class Table.</span> <span style="color:#65B042;"></span> <span style="color:#65B042;"> Doctest: here we insert python command lines inputs and outputs.</span> <span style="color:#65B042;"> >>> print Table.database</span> <span style="color:#65B042;"> http://access.com/db</span> <span style="color:#65B042;"> <span style="color:#65B042;">"""</span></span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>Data attributes here</span> database<span style="color:#E28964;">=</span><span style="color:#65B042;"><span style="color:#65B042;">'</span>http://access.com/db<span style="color:#65B042;">'</span></span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>Method attributes here</span> <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;"><span style="color:#DAD085;">__init__</span></span>(<span style="color:#3E87E3;">self</span>,<span style="color:#3E87E3;">id</span>,<span style="color:#3E87E3;">text</span>): <span style="color:#65B042;"><span style="color:#65B042;">"""</span>Initializes the id and textlabel data attributes.</span> <span style="color:#65B042;"></span> <span style="color:#65B042;"> Doctest: Testing more data attributes defined at construction time</span> <span style="color:#65B042;"> >>> x = Table(1,'coucou')</span> <span style="color:#65B042;"> >>> print x.id</span> <span style="color:#65B042;"> 1</span> <span style="color:#65B042;"> >>> print x.textlabel</span> <span style="color:#65B042;"> coucou</span> <span style="color:#65B042;"> <span style="color:#65B042;">"""</span></span> <span style="color:#3E87E3;">self</span>.id<span style="color:#E28964;">=</span><span style="color:#DAD085;">id</span> <span style="color:#3E87E3;">self</span>.textlabel<span style="color:#E28964;">=</span>text <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">#</span>doctest -- "Debugging sucks :( Testing rocks :)"</span> <span style="color:#99CF50;">def</span> <span style="color:#89BDFF;">_test</span>(): <span style="color:#65B042;"><span style="color:#65B042;">"""</span>Inline Doctest activated. Cool! :D</span> <span style="color:#65B042;"> This means that whenever the module is called in python</span> <span style="color:#65B042;"> </span> <span style="color:#65B042;"> > python thismodule.py -v</span> <span style="color:#65B042;"> </span> <span style="color:#65B042;"> the doctest function will try all the tests implemented in doctest.</span> <span style="color:#65B042;"> <span style="color:#65B042;">"""</span></span> <span style="color:#E28964;">import</span> doctest doctest.testmod() <span style="color:#E28964;">if</span> <span style="color:#9B859D;">__name__</span> <span style="color:#E28964;">==</span> <span style="color:#65B042;"><span style="color:#65B042;">"</span>__main__<span style="color:#65B042;">"</span></span>: _test() </pre> </div> </div> <a href="http://refactormycode.com/codes/51-classtemplate" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>