Finished writing_python_scripts
authorNishanth <nishanth@fossee.in>
Tue, 12 Oct 2010 11:44:33 +0530
changeset 335 d5248a15274c
parent 334 4b1e81da1c80
child 336 c77f7f5850dd
Finished writing_python_scripts
writing_python_scripts/questions.rst
writing_python_scripts/quickref.tex
writing_python_scripts/script.rst
--- a/writing_python_scripts/questions.rst	Tue Oct 12 11:26:09 2010 +0530
+++ b/writing_python_scripts/questions.rst	Tue Oct 12 11:44:33 2010 +0530
@@ -1,90 +1,120 @@
 Objective Questions
 -------------------
 
- 1. If ``a = [1, 1, 2, 3, 3, 5, 5, 8]``. What is set(a)
+ 1. Which of the following variables contains the locations to search for
+    python modules
 
-   a. set([1, 1, 2, 3, 3, 5, 5, 8])
-   #. set([1, 2, 3, 5, 8])
-   #. set([1, 2, 3, 3, 5, 5])
-   #. Error
+   a. sys.pythonpath
+   #. sys.path
+   #. os.pythonpath
+   #. os.path
+
+   Answer: sys.path
 
-   Answer: set([1, 2, 3, 5, 8])
-
- 2. ``a = set([1, 3, 5])``. How do you find the length of a?
+ 2. What is the type of ``sys.path``
 
-   Answer: len(a)
+   a. list of strings
+   #. list of int
+   #. string
+   #. tuple of strings
 
- 3. ``a = set([1, 3, 5])``. What does a[2] produce?
+   Answer: list of strings
+
+ 3. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+    the following code:
 
-   a. 1
-   #. 3
-   #. 5
-   #. Error
+      def show(x):
+          print x
+
+      show("Hello World")
 
-   Answer: Error
+      if __name__ == "__main__":
 
- 4. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
-    is the value of ``odd | squares``?
+          show("Hello Test")
+
+    How do you import the file.
 
-   Answer: set([1, 3, 4, 5, 7, 9, 16])
+   a. import utils
+   #. import utils.py
+   #. import /home/user/utils
+   #. import /home/user/utils.py
 
- 5. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
-    is the value of ``odd - squares``?
+   Answer: import utils
 
-   Answer: set([3, 5, 7])
+ 4. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+    the following code:
 
- 6. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
-    is the value of ``odd ^ squares``?
+      def show(x):
+          print x
+
+      show("Hello World")
 
-   Answer: set([3, 4, 5, 7, 16])
+      if __name__ == "__main__":
+
+          show("Hello Test")
 
- 7. ``odd = set([1, 3, 5, 7, 9])`` and ``squares = set([1, 4, 9, 16])``. What
-    does ``odd * squares`` give?
+    How do you use the ``show`` function after doing ``import utils``
+
+   a. utils.show("hey")
+   #. show("hey")
+   #. utils.py.show("hey")
+   #. utils.py.show.py("hey")
 
-   a. set([1, 12, 45, 112, 9])
-   #. set([1, 3, 4, 5, 7, 9, 16])
-   #. set([])
-   #. Error
+   Answer: utils.show("hey")
+
+ 5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+    the following code:
 
-   Answer: Error
+      def show(x):
+          print x
 
- 8. ``a = set([1, 2, 3, 4])`` and ``b = set([5, 6, 7, 8])``. What is ``a + b``
+      show("Hello World")
+
+      if __name__ == "__main__":
 
-   a. set([1, 2, 3, 4, 5, 6, 7, 8])
-   #. set([6, 8, 10, 12])
-   #. set([5, 12, 21, 32])
-   #. Error
+          show("Hello Test")
+
+    How do you use the ``show`` function after doing ``from utils import show``
 
- 9. ``a`` is a set. how do you check if if a varaible ``b`` exists in ``a``?
+   a. utils.show("hey")
+   #. show("hey")
+   #. utils.py.show("hey")
+   #. utils.py.show.py("hey")
 
-   Answer: b in a
-
- 10. ``a`` and ``b`` are two sets. What is ``a ^ b == (a - b) | (b - a)``?
+   Answer: show("hey")
 
-   a. True
-   #. False
+ 5. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+    the following code:
 
-   Answer: False
-
+      def show(x):
+          print x
 
-Larger Questions
-----------------
+      show("Hello World")
+
+      if __name__ == "__main__":
 
- 1. Given that mat_marks is a list of maths marks of a class. Find out the
-    no.of duplicates marks in the list.
+          show("Hello Test")
+
+    What is printed when you do ``import utils``
 
    Answer::
+      Hello World
 
-     unique_marks = set(mat_marks)
-     no_of_duplicates = len(mat_marks) - len(unique_marks)
+ 6. The script ``utils.py`` is in one of locations of PYTHONPATH and contains
+    the following code:
+
+      def show(x):
+          print x
 
- 2. Given that mat_marks is a list of maths marks of a class. Find how many
-    duplicates of each mark exist.
+      show("Hello World")
+
+      if __name__ == "__main__":
+
+          show("Hello Test")
+
+    What is printed when the script is executed.
 
    Answer::
+      Hello World
+      Hello Test
 
-     marks_set = set(mat_marks)
-     for mark in marks_set:
-         occurences = mat_marks.count(mark)
-         print occurences - 1, "duplicates of", mark, "exist"
-
--- a/writing_python_scripts/quickref.tex	Tue Oct 12 11:26:09 2010 +0530
+++ b/writing_python_scripts/quickref.tex	Tue Oct 12 11:44:33 2010 +0530
@@ -1,11 +1,9 @@
-Creating a tuple:\\
-{\ex \lstinline|    t = (1, "hello", 2.5)|}
-
-Accessing elements of tuples:\\
-{\ex \lstinline|    t[index] Ex: t[2]|}
+See where python searches for modules:\\
+{\ex \lstinline|    sys.path|}
 
-Accessing slices of tuples:\\
-{\ex \lstinline|    t[start:stop:step]|}
+Include our own path in PYTHONPATH:\\
+{\ex \lstinline|    sys.path.append(our_path)|}
 
-Swapping values:\\
-{\ex \lstinline|    a, b = b, a|}
+Run certian code only if executed and not if imported:\\
+{\ex \lstinline|    if __name__=="__main__": #do something|}
+
--- a/writing_python_scripts/script.rst	Tue Oct 12 11:26:09 2010 +0530
+++ b/writing_python_scripts/script.rst	Tue Oct 12 11:44:33 2010 +0530
@@ -1,8 +1,15 @@
 .. Objectives
 .. ----------
 
+.. By the end of this tutorial, you will be able to 
+
+..  * Understand what is importing
+..  * Write your own Python modules
+..  * Understand the ``__name__=="__main__"`` idiom
+
 .. Prerequisites
 .. -------------
+.. 1. Using Python modules
      
 .. Author              : Nishanth Amuluru
    Internal Reviewer   :