Please send questions to st10@humboldt.edu .

CIS 480 - Python
Random "whiteboard" projections, Week 7 Lecture, 10-4-05

next topic:
focus on typing in Python:
4.6: The Dynamic Typing Interlude, pp. 68-73
5.5: General Type Categories, pp. 95-96
7.3 - 7.9: Type Categories Revisited -
           Built-in Type Gotchas, pp. 117-127

then: REGULAR EXPRESSIONS - reading assignment coming!

today: mostly files, plus a little more...

iteritems method: on a dict --- one of several ways to
   grab keys and values from a dict, one "pair" at a time:

knights = {'galahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.iteritems():
    print str(type(k)) + str(type(v))
    print k + v

BUT --- there's more than one way to grab key-value pairs...

for example: the items method returns them as tuples:

type(knights.items())
for t in knights.items():
    print str(type(t))
    print t
    print t[0] + t[1]

* these two are NOT the same, performance-wise, if dictionary is LARGE ---
  building a giant list all at once vs. "on-the-fly"

  (iteritems() is doing it on the fly...)

knights['galahad']
knights.get('galahad')
knights.get('galahad', 'no nickname')

* difference? error w: knights['galahad'] if 'galahad' not a key in knights;
              None  w: knights.get('galahad') if 'galahad' not a key in knights
	      'no nickname' w: knights.get('galahad', 'no nickname') if
                 'galahad' not a key in knights

* a few last tuple comments...
--------------------------------

(40) - the expression 40 in parentheses
(40,)- the tuples with the single element 40

don't forget convenient converter functions...
list - returns a new list corresponding to some collection
tuple - returns a new tuple corresponding to some collection

------------------
FILES
-----------------

* you use the open function to open an external file for some purpose;

    * the open function returns a file object
      (an object of type file)

      which is not unlike a file handle/file stream in some other languages.

    * open function expects 2 strings as arguments:
        * a string representing the file name

            * this string CAN be relative or absolute,
              this string CAN contain path information;

            * if no path is given, the file is assumed to exist
              in the current working directory --- where the script
              is RUNNING

        * a string representing what you want to DO to the file...
	  "processing mode"

	  'r' - open for input (default!)
          'w' - create and open for output
          'a' - open for appending at the end
	  (and others we're not going into right now...)

    * once you have a file object, here are some of the things you can do with it:
          * (these are methods of file objects)

          * read() - reads the entire file into a single string

          * read(N) - read the next N bytes from the file (N >= 1)

	  * readline() - reads the next line (up to and including ther end-line marker)

	  * readlines() - read entire file into a list of line strings

              * xreadlines() is supposed to (acc. to p. 165 of "Learning Python"):
                "load lines on demand, to avoid filling memory for large files"

	  * write(S) - write the string S into the file

	  * writelines(L) - writes all the line strings in the list L into the file

	  * close() - manual close of the file 

	      * CURRENT implementations of Python do close the file when it is
                garbage-collected, if it isn't closed already ---

		BUT --- that's NOT part of Python's official language definition.

	      * SO --- that's just one of the reasons that we'll have the class
                style standard:

		"You are expected to explicitly, manually close file objects when you
		are done with them."

INTERLUDE: interactive input

* raw_input function can be used for relatively simple interactive input

raw_input(S) - print the str S to the screen,
               and return as a string whatever the user then types up to
                   the next newline