User Tools

Site Tools


python_cookbook

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
python_cookbook [2016/08/22 13:21]
mantis [Automatically set parameters of initializer as member variables]
python_cookbook [2019/03/05 09:50]
mantis
Line 327: Line 327:
 </​code>​ </​code>​
  
-==== Headline ​====+==== Taking care of boilerplate code ====
  
-[[https://​glyph.twistedmatrix.com/​2016/​08/​attrs.html|attrs]] helps with+[[https://​glyph.twistedmatrix.com/​2016/​08/​attrs.html|attrs]] helps with boilerplate code a lot, e.g. initialisation,​ string representation,​ comparison. 
 + 
 +<code python>​ 
 +import attr 
 +@attr.s 
 +class Point3D(object):​ 
 +    x = attr.ib() 
 +    y = attr.ib() 
 +    z = attr.ib() 
 +</​code>​
 ====== CGI Scripting ====== ====== CGI Scripting ======
 ===== Content Type ===== ===== Content Type =====
Line 1136: Line 1145:
  
   * [[http://​docs.python.org/​lib/​module-logging.html|Logger Documentation]]   * [[http://​docs.python.org/​lib/​module-logging.html|Logger Documentation]]
-  * [[http://​docs.python.org/​release/​2.5/​lib/​node422.html|Output ​Formatting]]+  * [[http://​docs.python.org/​release/​2.5/​lib/​node422.html|Python 2 output ​Formatting]] 
 +  * [[https://​docs.python.org/​3/​howto/​logging-cookbook.html|Python 3 logging cookbook]]
  
 ====== Mathematics ====== ====== Mathematics ======
Line 1769: Line 1779:
 y = numpy.array([41.621207814814809,​ 42.328298238095236,​ 45.881729878787887,​ 43.800834224999996]) y = numpy.array([41.621207814814809,​ 42.328298238095236,​ 45.881729878787887,​ 43.800834224999996])
 y_smoothed = scipy.signal.cspline1d(y) y_smoothed = scipy.signal.cspline1d(y)
 +</​code>​
 +
 +===== t-test =====
 +
 +<code python>
 +from scipy import stats
 +import numpy
 +import statistics
 +
 +# http://​www.biostathandbook.com/​onesamplettest.html
 +data = [120.6, 116.4,​117.2,​118.1,​114.1,​116.9,​113.3,​121.1,​116.9,​117.0]
 +
 +m = sum(data)/​len(data)
 +
 +null_hypothesis = 120
 +
 +t_value, p_value = stats.ttest_1samp(data,​ null_hypothesis)
 +
 +print(statistics.stdev(data))
 +print(numpy.std(data,​ ddof=1))
 +
 +print(t_value,​ p_value)
 </​code>​ </​code>​
 ====== Statistical Functions ====== ====== Statistical Functions ======
Line 1822: Line 1854:
 (found in comments [[http://​programmingpraxis.com/​2012/​05/​29/​streaming-median/​|here]] - damn I hate paywalls. I'd love to read the piglet tracking paper!) (found in comments [[http://​programmingpraxis.com/​2012/​05/​29/​streaming-median/​|here]] - damn I hate paywalls. I'd love to read the piglet tracking paper!)
  
-====== ​Unit Tests ======+====== ​Testing ​====== 
 + 
 +===== unittest ===== 
   * Python 2 https://​docs.python.org/​2.7/​library/​unittest.html   * Python 2 https://​docs.python.org/​2.7/​library/​unittest.html
   * python 3 https://​docs.python.org/​3.4/​library/​unittest.html   * python 3 https://​docs.python.org/​3.4/​library/​unittest.html
   * http://​agiletesting.blogspot.com/​2005/​01/​python-unit-testing-part-1-unittest.html the nutshell   * http://​agiletesting.blogspot.com/​2005/​01/​python-unit-testing-part-1-unittest.html the nutshell
  
-===== Basics ​=====+==== Basics ====
 <code python> <code python>
 import unittest import unittest
Line 1844: Line 1879:
 </​code>​ </​code>​
  
-===== classwide Setup and teardown ​=====+==== classwide Setup and teardown ====
  
 There are two class methods that are called before/​after tests in an individual class run.  There are two class methods that are called before/​after tests in an individual class run. 
Line 1872: Line 1907:
 </​code> ​     ​ </​code> ​     ​
  
-===== Test for exceptions ​=====+==== Test for exceptions ====
  
 Since [[http://​docs.python.org/​2/​library/​unittest.html#​unittest.TestCase.assertRaises|python 2.7]] this is best done by using the context manager returned by unittest.assertRaises() Since [[http://​docs.python.org/​2/​library/​unittest.html#​unittest.TestCase.assertRaises|python 2.7]] this is best done by using the context manager returned by unittest.assertRaises()
python_cookbook.txt · Last modified: 2019/03/05 09:51 by mantis