13 #+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} |
13 #+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} |
14 |
14 |
15 #+LaTeX_HEADER: \usepackage{listings} |
15 #+LaTeX_HEADER: \usepackage{listings} |
16 |
16 |
17 #+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, |
17 #+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, |
18 #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, |
18 #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{red}, |
19 #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} |
19 #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} |
20 |
20 |
21 #+TITLE: Accessing parts of arrays |
21 #+TITLE: Testing and debugging |
22 #+AUTHOR: FOSSEE |
22 #+AUTHOR: FOSSEE |
23 #+EMAIL: |
23 #+EMAIL: |
24 #+DATE: |
24 #+DATE: |
25 |
25 |
26 #+DESCRIPTION: |
26 #+DESCRIPTION: |
27 #+KEYWORDS: |
27 #+KEYWORDS: |
28 #+LANGUAGE: en |
28 #+LANGUAGE: en |
29 #+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t |
29 #+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t |
30 #+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc |
30 #+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc |
31 |
31 |
32 * Outline |
32 * Outline |
33 - Manipulating one and multi dimensional arrays |
33 - What software Testing is? |
34 - Access and change individual elements |
34 - Learn to test simple functions for their functionality. |
35 - Access and change rows and columns |
35 - Learn how to automate tests. |
36 - Slicing and striding on arrays to access chunks |
36 - Need for coding style and some of the standards followed by the Python Community. |
37 - Read images into arrays and manipulations |
37 - Handling Errors and Exceptions. |
38 * Sample Arrays |
|
39 #+begin_src python |
|
40 In []: A = array([12, 23, 34, 45, 56]) |
|
41 |
|
42 In []: C = array([[11, 12, 13, 14, 15], |
|
43 [21, 22, 23, 24, 25], |
|
44 [31, 32, 33, 34, 35], |
|
45 [41, 42, 43, 44, 45], |
|
46 [51, 52, 53, 54, 55]]) |
|
47 |
|
48 #+end_src |
|
49 * Question 1 |
|
50 Change the last column of ~C~ to zeroes. |
|
51 * Solution 1 |
|
52 #+begin_src python |
|
53 In []: C[:, -1] = 0 |
|
54 #+end_src |
|
55 * Question 2 |
|
56 Change ~A~ to ~[11, 12, 13, 14, 15]~. |
|
57 * Solution 2 |
|
58 #+begin_src python |
|
59 In []: A[:] = [11, 12, 13, 14, 15] |
|
60 #+end_src |
|
61 * squares.png |
|
62 #+begin_latex |
|
63 \begin{center} |
|
64 \includegraphics[scale=0.6]{squares} |
|
65 \end{center} |
|
66 #+end_latex |
|
67 * Question 3 |
|
68 - obtain ~[22, 23]~ from ~C~. |
|
69 - obtain ~[11, 21, 31, 41]~ from ~C~. |
|
70 - obtain ~[21, 31, 41, 0]~. |
|
71 * Solution 3 |
|
72 #+begin_src python |
|
73 In []: C[1, 1:3] |
|
74 In []: C[0:4, 0] |
|
75 In []: C[1:5, 0] |
|
76 #+end_src |
|
77 * Question 4 |
|
78 Obtain ~[[23, 24], [33, -34]]~ from ~C~ |
|
79 * Solution 4 |
|
80 #+begin_src python |
|
81 In []: C[1:3, 2:4] |
|
82 #+end_src |
|
83 * Question 5 |
|
84 Obtain the square in the center of the image |
|
85 * Solution 5 |
|
86 #+begin_src python |
|
87 In []: imshow(I[75:225, 75:225]) |
|
88 #+end_src |
|
89 * Question 6 |
|
90 Obtain the following |
|
91 #+begin_src python |
|
92 [[12, 0], [42, 0]] |
|
93 [[12, 13, 14], [0, 0, 0]] |
|
94 #+end_src |
|
95 |
|
96 * Solution 6 |
|
97 #+begin_src python |
|
98 In []: C[::3, 1::3] |
|
99 In []: C[::4, 1:4] |
|
100 #+end_src |
|
101 * Summary |
|
102 You should now be able to -- |
|
103 - Manipulate 1D \& Multi dimensional arrays |
|
104 - Access and change individual elements |
|
105 - Access and change rows and columns |
|
106 - Slice and stride on arrays |
|
107 - Read images into arrays and manipulate them. |
|
108 * Thank you! |
|
109 #+begin_latex |
|
110 \begin{block}{} |
|
111 \begin{center} |
|
112 This spoken tutorial has been produced by the |
|
113 \textcolor{blue}{FOSSEE} team, which is funded by the |
|
114 \end{center} |
|
115 \begin{center} |
|
116 \textcolor{blue}{National Mission on Education through \\ |
|
117 Information \& Communication Technology \\ |
|
118 MHRD, Govt. of India}. |
|
119 \end{center} |
|
120 \end{block} |
|
121 #+end_latex |
|
122 |
38 |
123 |
39 |
|
40 * gcd function |
|
41 - Create gcd.py file with: |
|
42 #+begin_LaTeX |
|
43 \begin{lstlisting}[language=python] |
|
44 def gcd(a, b): |
|
45 if a % b == 0: |
|
46 return b |
|
47 return gcd(b, a%b) |
|
48 \end{lstlisting} |
|
49 #+end_LaTeX |
|
50 |
|
51 * Test for gcd.py |
|
52 - Edit gcd.py file |
|
53 #+begin_LaTeX |
|
54 \begin{lstlisting}[language=python] |
|
55 |
|
56 def gcd(a, b): |
|
57 if b == 0: |
|
58 return a |
|
59 return gcd(b, a%b) |
|
60 |
|
61 if __name__=='__main__': |
|
62 result = gcd(48, 64) |
|
63 if result != 16: |
|
64 print "Test failed" |
|
65 print "Test Passed" |
|
66 \end{lstlisting} |
|
67 #+end_LaTeX |
|
68 |
|
69 * Automating tests |
|
70 #+begin_LaTeX |
|
71 \begin{lstlisting}[language=python] |
|
72 |
|
73 if __name=__='__main__': |
|
74 for line in open('numbers.txt'): |
|
75 numbers = line.split() |
|
76 x = int(numbers[0]) |
|
77 y = int(numbers[1]) |
|
78 result = int(numbers[2]) |
|
79 if gcd(x, y) != result: |
|
80 print "Failed gcd test |
|
81 for", x, y |
|
82 \end{lstlisting} |
|
83 #+end_LaTeX |
|
84 |
|
85 |
|
86 |