diff --git a/_posts/2011-10-27-opencv-rect-expects-four-integers.markdown b/_posts/2011-10-27-opencv-rect-expects-four-integers.markdown index be2b3e8..9069161 100644 --- a/_posts/2011-10-27-opencv-rect-expects-four-integers.markdown +++ b/_posts/2011-10-27-opencv-rect-expects-four-integers.markdown @@ -24,7 +24,7 @@ You may encounter this error when using  cv.SetImageROI(img, roi) TypeError: CvRect argument 'rect' expects four integers -[/python] +{% endhighlight %} The answer is pretty simple, you have to set rect as a tuple and not a list: -[python] +{% highlight python %} roi = (0, 0, dims[0] / 2, dims[1] / 2 ) -[/python] +{% endhighlight %} There it is, pretty simple, isn't?! diff --git a/_posts/2011-11-28-simple-region-growing-implementation-in-python.markdown b/_posts/2011-11-28-simple-region-growing-implementation-in-python.markdown index e3335ab..9cc8aac 100644 --- a/_posts/2011-11-28-simple-region-growing-implementation-in-python.markdown +++ b/_posts/2011-11-28-simple-region-growing-implementation-in-python.markdown @@ -202,7 +202,7 @@ Due to the method itself, only grayscale images may be processe
region growing tests with a gnu
Here is the input image, the image with the seed point placed, and the final result!
Here is the region growing function implemented in Tippy: -[python] +{% highlight python %} import sys import cv import numpy @@ -289,12 +289,12 @@ def simple_region_growing(img, seed, threshold=1): del contour_val[index] return reg -[/python] +{% endhighlight %} Here is a simple test of the function, using Tippy functions. If you only want to use the function, juste remove the tippy stuff and copy the function in your source. Please note than OpenCV is needed for the function to work ;) -[python] +{% highlight python %} import cv import tippy.segmentations as se @@ -315,7 +315,7 @@ else: out_img = se.simple_region_growing(img, seed, threshold) do.display_single_image(out_img, "Region Growing result") -[/python] +{% endhighlight %} As you can see, the implementation is rather short in code. An option has been included to let user interactively choose their seed. diff --git a/_posts/2011-12-02-pythonunittest-assertraises-raises-error.markdown b/_posts/2011-12-02-pythonunittest-assertraises-raises-error.markdown index 2995ff8..fca8d6d 100644 --- a/_posts/2011-12-02-pythonunittest-assertraises-raises-error.markdown +++ b/_posts/2011-12-02-pythonunittest-assertraises-raises-error.markdown @@ -54,7 +54,7 @@ Let's say I want to create a (dum) function calculating the square value of a -[python] +{% highlight python %} def square_value(a): """ Returns the square value of a. @@ -65,11 +65,11 @@ def square_value(a): raise TypeError("Input should be a string:") return out -[/python] +{% endhighlight %} -[python] +{% highlight python %} import dum_function as df # import function module import unittest class Test(unittest.TestCase): @@ -95,11 +95,11 @@ class Test(unittest.TestCase): if __name__ == "__main__": unittest.main() -[/python] +{% endhighlight %} -[python] +{% highlight python %} ====================================================================== ERROR: test_square_value (__main__.Test) ---------------------------------------------------------------------- @@ -114,26 +114,26 @@ TypeError: Input should be a string: Ran 1 test in 0.000s FAILED (errors=1) -[/python] +{% endhighlight %} The TypeError is actullay raised, and generates a test failure. The problem is that this is exactly the behavior we wanted :s. To avoid this error, simply run the function using lambda in the test call : -[python] +{% highlight python %} self.assertRaises(TypeError, lambda: df.square_value(self.false_int)) -[/python] +{% endhighlight %} The final output : -[python] +{% highlight python %} ---------------------------------------------------------------------- Ran 1 test in 0.000s OK -[/python] +{% endhighlight %} Perfect ! diff --git a/_posts/2011-12-28-programming-tips-2.markdown b/_posts/2011-12-28-programming-tips-2.markdown index 362297b..b27ee32 100644 --- a/_posts/2011-12-28-programming-tips-2.markdown +++ b/_posts/2011-12-28-programming-tips-2.markdown @@ -457,13 +457,13 @@ echo ${FOLDER%/}

PYTHON

Get variable name as a string: -[python] +{% highlight python %} blah = 1 blah_name = [ k for k , v in locals (). iteritems () if v is blah ][ 0 ] -[/python] +{% endhighlight %}  WARNING : Not very clever however, as variables in Python may have more that one name ! Use with caution! @@ -471,7 +471,7 @@ blah_name = [ k for k , v in locals (). iteritems () if v is blah ][ 0 ] Replace a part of a table [Numpy]: -[python] +{% highlight python %} small_BW=where((L[temp[ptr-1]]==ptr),1,0) @@ -479,7 +479,7 @@ a=BW[posi_0:posi_1,posi_2:posi_3] a[:,:]=small_BW.copy() -[/python] +{% endhighlight %}
@@ -487,31 +487,31 @@ a[:,:]=small_BW.copy() Put -1 as second argument, the info will automatically be processed. -[python] +{% highlight python %} array.reshape(3,-1) -[/python] +{% endhighlight %}
Concatenate 2 tables [Numpy]:  -[python] +{% highlight python %} c=numpy.concatenate((a,b),axis=0) -[/python] +{% endhighlight %}
Cast data in table to int: -[python] +{% highlight python %} Ndarray.astype(int) -[/python] +{% endhighlight %}
@@ -519,39 +519,39 @@ Ndarray.astype(int) Use add mode : -[python] +{% highlight python %} file.open(file_name, 'a') -[/python] +{% endhighlight %}
Test if variable exists: -[python] +{% highlight python %} try: print variable except NameError: print "Error!" -[/python] +{% endhighlight %}
Check if variable is one of several choices: -[python] +{% highlight python %} if a in [b, c,d]: blabla -[/python] +{% endhighlight %}
Multi instanciation in Python: -[python] +{% highlight python %} a = b = c = 1+1 -[/python] +{% endhighlight %} WARNING: Those three variables are the same object! @@ -559,7 +559,7 @@ a = b = c = 1+1 Print function name : -[python] +{% highlight python %} import sys def tutut(): @@ -570,7 +570,7 @@ def tutut(): if __name__ == '__main__': tutut() -[/python] +{% endhighlight %} And here is the result diff --git a/_posts/2011-12-28-simply-print-current-function-name.markdown b/_posts/2011-12-28-simply-print-current-function-name.markdown index 0b4751f..0ae80cd 100644 --- a/_posts/2011-12-28-simply-print-current-function-name.markdown +++ b/_posts/2011-12-28-simply-print-current-function-name.markdown @@ -26,7 +26,7 @@ Hopefully, Python offers a simple (but curious) way to perform this. Here is how to print your function name as a string in Python : -[python] +{% highlight python %} import sys def tutut(): @@ -37,7 +37,7 @@ def tutut(): if __name__ == '__main__': tutut() -[/python] +{% endhighlight %} And here is the result diff --git a/_posts/2011-12-29-multiprocessing-using-python.markdown b/_posts/2011-12-29-multiprocessing-using-python.markdown index fec55a7..f563603 100644 --- a/_posts/2011-12-29-multiprocessing-using-python.markdown +++ b/_posts/2011-12-29-multiprocessing-using-python.markdown @@ -73,7 +73,7 @@ Basically, the following example will be perfect for applications in whichPython documentation: -[python] +{% highlight python %} from multiprocessing import Process @@ -85,7 +85,7 @@ if __name__ == '__main__': p.start() p.join() -[/python] +{% endhighlight %} This example creates a process, performing the f function. The first line indicates the function to be run and its arguments. The second line (start) starts the processus. @@ -93,7 +93,7 @@ Finally, the join keyword waits for the function to finish prop Creating two processes now appears to be pretty simple : -[python] +{% highlight python %} from multiprocessing import Process @@ -110,17 +110,17 @@ if __name__ == '__main__': print('Exiting!) -[/python] +{% endhighlight %} In  this second example, the print will be performed only when p1 and p2 have been killed. In order to enhance you productivity, a simple idea is to create as much processes as the number of cores in your computer. Once again, Python has already made the work for you. -[python] +{% highlight python %} nb_processes = multiprocessing.cpu_count() -[/python] +{% endhighlight %} Here it is, Your application should now run slightly faster ! diff --git a/_posts/2012-02-29-profiling-a-python-script.markdown b/_posts/2012-02-29-profiling-a-python-script.markdown index 2a7b502..30d3a78 100644 --- a/_posts/2012-02-29-profiling-a-python-script.markdown +++ b/_posts/2012-02-29-profiling-a-python-script.markdown @@ -27,9 +27,9 @@ The main common option would be to use the timeit module available. -[python] +{% highlight python %} import timeit t1 = timeit.Timer("function()", "from __main__ import function") print t1.timeit(1) -[/python] +{% endhighlight %} I use this option on Eclipse because I didn't want to install the profile module on Windows. diff --git a/_posts/2012-06-17-my-very-first-python-script-docfinder.markdown b/_posts/2012-06-17-my-very-first-python-script-docfinder.markdown index a73e296..f5d9f95 100644 --- a/_posts/2012-06-17-my-very-first-python-script-docfinder.markdown +++ b/_posts/2012-06-17-my-very-first-python-script-docfinder.markdown @@ -41,7 +41,7 @@ $ docfinder folder extension So here is the code : -[python] +{% highlight python %} #!/usr/bin/python #_*_ coding: ISO-8859-15 _*_ import os @@ -56,7 +56,7 @@ def scan_rep(repertoir, extension): print '%s '%\(nom_complet) if __name__=='__main__': scan_rep(sys.argv[1],sys.argv[2]) -[/python] +{% endhighlight %} Pretty lame, isn't it ? :)