Tuesday, 1 October 2013

Inheritance & Exceptions

1. Inheritance:

 - superclass vs subclass:
  
  class DerivedClassName(BaseClassName):
  class DerivedClassName(modname.BaseClassName): 
  e.g.class IntStack(Stack): 
 
- built-in functions that work with inheritance:
  
   a)Use isinstance() to check an instance’s type: isinstance(obj, int) will be True only if   obj.__class__ is int or some class derived from int.
   b)Use issubclass() to check class inheritance: issubclass(bool, int) is True since bool is a subclass of int. However, issubclass(float, int) is False since float is not a subclass of int.



2. Exceptions:

e.g. class SpecialException(Exception):
    pass

class ExtremeException(SpecialException):
    pass

if __name__ == '__main__':
    try:
        #1/0
        #raise SpecialException('I am a SpecialException')
        #raise Exception('I am an Exception')
        raise ExtremeException('I am an ExtremeException')
        #1/0
    except SpecialException as se:
        print(se)
        print('caught as SpecialException')
        raise
    except ExtremeException as ee:
        print(ee)
        print('caught as ExtremeException')
        #raise
    except Exception as e:
        print(e)
        print('caught as Exception')


Lab: Final question was solved by not shifting every positions after pop, but add 1 index to every elements, which saves operating time :)

19.5. Glossary

exception
An error that occurs at runtime.
handle an exception
To prevent an exception from causing our program to crash, by wrapping the block of code in a try ... except construct.
raise
To create a deliberate exception by using the raise statement.

Abstract Data Types(ADTs)

 An abstract data type, or ADT, specifies a set of operations (or methods) and the semantics of the operations (what they do), but it does not not specify the implementation of the operations. 

Common ADTs:list, dictionary, stack……(from slices)

 

Reading from a paragraph about Stack(useful information)

&

Design(class) 

& 

Implementation(init, push, pop, is_empty) 

&

 Test(if __name__ == '__main__')

 e.g.

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def is_empty(self):
        return (self.items == [])

 

 

 The first lab we just be on time but lack of time check the correctness, 

 anyway, the cooperation was pleasant :)

 

25.9. Glossary

(again, for my own convenience:) )

abstract data type (ADT)
A data type (usually a collection of objects) that is defined by a set of operations but that can be implemented in a variety of ways.
client
A program (or the person who wrote it) that uses an ADT.
delimiter
A character that is used to separate tokens, such as punctuation in a natural language.
generic data structure
A kind of data structure that can contain data of any type.
implementation
Code that satisfies the syntactic and semantic requirements of an interface.
interface
The set of operations that define an ADT.
infix
A way of writing mathematical expressions with the operators between the operands.
parse
To read a string of characters or tokens and analyze its grammatical structure.
postfix
A way of writing mathematical expressions with the operators after the operands.
provider
The code (or the person who wrote it) that implements an ADT.
token
A set of characters that are treated as a unit for purposes of parsing, such as the words in a natural language.
veneer
A class definition that implements an ADT with method definitions that are invocations of other methods, sometimes with simple transformations. The veneer does no significant work, but it improves or standardizes the interface seen by the client.

Code Like a Pythonista: Idiomatic Python:

http://python.net/~goodger/projects/pycon/2007/idiomatic/presentation.html

Getting Started With 108

For the first week, mainly what we did on my view was revision of CSC108.
Rereading notes from 108 and attending rampup session do helped a lot.
( I like the sheet that received at rampup session!)


Things from CSC108 notes:
 week1
 - Python as a calculator: Arithmetic operators (/ float, // int)
 - Syntax(valid combination) vs Semantics(meaning of combination)
 - Variables (Name start with letter or _ )
 - Function (Define, Calls, Built-in)
week2
- String(Escape Sequesces '\', format{})
- Input / Output
- Function Design steps(Example, Type, Header, Description, Body, Test)
- Call Stack(assignment statement, return expression)
week3
- Bool(comparison, logical)
- Converting(e.g. str())
- Import( import math or math.())
- If, elif, else
week4
- More String(==, !=,  <,  >,  <=, >=, in, len(), cannot be modified)
- Index and Slices
- Inside Object( dir(str))
- Loop(for variables in str:)
- Accumulator(number, string)
- While

week5
- Nested Loops
- Range
- Turples(Immutable)
week6
- Nested Lists(visualizer)
- Files(open, write, readline)
week7
- Dictionary(unique key, order doesn't matter)
week8
- Files(Random Story Generation, representing, working with data)
week9
- Test Cases
- Unittest
week10
- Run Time Complexity of Functions
- Sorting Algorithms(Selection, Bubble, Insertion)
week11
- Object-Oriented Programming Class Event/Day



What's new for me is conceps are introduced.
copying concepts just for my own convenience.
15.11 Glossary
 
attribute
One of the named data items that makes up an instance.
class
A user-defined compound type. A class can also be thought of as a template for the objects that are instances of it. (The iPhone is a class. By December 2010, estimates are that 50 million instances had been sold!)
constructor
Every class has a “factory”, called by the same name as the class, for making new instances. If the class has an initializer method, this method is used to get the attributes (i.e. the state) of the new object properly set up.
initializer method
A special method in Python (called __init__) that is invoked automatically to set a newly created object’s attributes to their initial (factory-default) state.
instance
An object whose type is of some class. Instance and object are used interchangeably.
instantiate
To create an instance of a class, and to run its initializer.
method
A function that is defined inside a class definition and is invoked on instances of that class.
object
A compound data type that is often used to model a thing or concept in the real world. It bundles together the data and the operations that are relevant for that kind of data. Instance and object are used interchangeably.
object-oriented programming
A powerful style of programming in which data and the operations that manipulate it are organized into objects.
object-oriented language
A language that provides features, such as user-defined classes and inheritance, that facilitate object-oriented programming.