More Data Abstraction UW CSE 190p Summer 2012. Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Data Abstraction UW CSE 190p Summer Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these functions.
1 OBJECT-ORIENTED CONCEPTS. 2 What is an object?  An object is a software entity that mirrors the real world in some way.  A software object in OOP.
6-1 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process and relate it to Polya’s.
Chapter 6 Problem Solving and Algorithm Design. 6-2 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Chapter 2: Algorithm Discovery and Design
Run time vs. Compile time
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Guide To UNIX Using Linux Third Edition
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Abstract Data Types and Encapsulation Concepts
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Introduction SWE 619. Why Is Building Good Software Hard? Large software systems enormously complex  Millions of “moving parts” People expect software.
 Computer Science 1MD3 Introduction to Programming Michael Liut Ming Quan Fu Brandon.
Local Definitions, Scope, Functional Abstraction, and Polymorphism.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Invitation to Computer Science, Java Version, Second Edition.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Design Exercise UW CSE 140 Winter Exercise Given a problem description, design a module to solve the problem 1) Specify a set of functions – For.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Cohesion and Coupling CS 4311
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
More On Classes UW CSE 160 Spring Classes define objects What are objects we've seen? 2.
Introduction to Computing Using Python Data Storage and Processing  How many of you have taken IT 240?  Databases and Structured Query Language  Python.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Session 7 Methods Strings Constructors this Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Design Exercise UW CSE 190p Summer 2012 download examples from the calendar.
Design Exercise UW CSE 160 Spring Exercise Given a problem description, design a module to solve the problem 1) Specify a set of functions – For.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Ranking of Database Query Results Nitesh Maan, Arujn Saraswat, Nishant Kapoor.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Written by: Dr. JJ Shepherd
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CMSC 330: Organization of Programming Languages Operational Semantics.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Data Abstraction UW CSE 160 Spring What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
CS 265 Jianbo Zhao. What is interface? Interface is a detailed boundary between code that provides a service and code that uses it. An interface defines.
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
SQL – Python and Databases
CS 326 Programming Languages, Concepts and Implementation
Character (String) Data
Design Exercise UW CSE 140 Winter 2014.
Design Exercise UW CSE 160 Winter 2017.
Data Abstraction UW CSE 160 Winter 2017.
Data Abstraction UW CSE 160 Winter 2016.
List Algorithms Taken from notes by Dr. Neil Moore
Number and String Operations
CISC101 Reminders Slides have changed from those posted last night…
Design Exercise UW CSE 160 Spring 2018.
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Data Abstraction UW CSE 160 Spring 2018.
Dr. Bhargavi Dept of CS CHRIST
Introduction to Python: Day Three
CSE 303 Concepts and Tools for Software Development
Design Exercise UW CSE 160 Winter 2016.
A Level Computer Science Topic 6: Introducing OOP
Data Abstraction UW CSE 160.
Presentation transcript:

More Data Abstraction UW CSE 190p Summer 2012

Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these functions operated on the same data structure – a list of tuples of measurements – a dictionary associating words with a frequency count Both modules had a common general form – One function to create the data structure from some external source – Multiple functions to query the data structure in various ways – This kind of situation is very common

What we’ve learned so far data structure – a collection of related data – the relevant functions are provided for you – Ex: list allows append, sort, etc. – What if we want to make our own kind of “list,” with its own special operations? module – a named collection of related functions – but shared data must be passed around explicitly – What if we want to be sure that only our own special kind of list is passed to each function?

Abstraction: Emphasis on exposing a useful interface. Encapsulation: Emphasis on hiding the implementation details. Information Hiding: The process by which you achieve encapsulation. Procedural abstraction: Hiding implementation details of functions Data Abstraction: Hiding implementation details of data types Overall: – Your job is to choose which details to hide and which details to expose. – The language’s job is to provide you with machinery to get this done Terms of Art

Tools for abstraction Functions Modules – A named set of related functions – A namespace mechanism to refer to avoid conflicts What else?

Tools for abstraction: Default Values As you generalize a function, you tend to add parameters. Downsides: – A function with many parameters can be awkward to call – Existing uses need to be updated

def twittersearch(query): ""”Return the responses from the query””” url = " + query remote_file = urllib.urlopen(url) raw_response = remote_file.read() response = json.loads(raw_response) return [tweet["text"] for tweet in response["results"]] def twittersearch(query, page=1): ""”Return the responses from the query for the given page””” resource = “ qs = “?q=" + query + “&page=“ + page url = resource + qs remote_file = urllib.urlopen(url) raw_response = remote_file.read() response = json.loads(raw_response) return [tweet["text"] for tweet in response["results"]]

Data Abstraction, first attempt: Group related functions in a module

Text Analysis def read_words(filename): “””Return a dictionary mapping each word in filename to its frequency””” words = open(filename).read().split() wordcounts = {} for w in words: cnt = wordcounts.setdefault(w, 0) wordcounts[w] = cnt + 1 return wordcounts def wordcount(wordcounts, word): “””Return the count of the given word””” return wordcounts[word] def topk(wordcounts, k=10): “””Return top 10 most frequent words””” scores_with_words = [(s,w) for (w,s) in wordcounts.items()] scores_with_words.sort() return scores_with_words[0:k] def totalwords(wordcounts): “””Return the total number of words in the file””” return sum([s for (w,s) in wordcounts])

# program to compute top 10: wordcounts = read_words(filename) result = topk(wordcounts, 10) The wordcount dictionary is exposed to the user. If we want to change our implementation to use a list, we can’t be sure we won’t break their program. We want to collect all the data we need and all the functions we need and bring them together into one unit.

Data Abstraction, first attempt: Group related functions in a module Cons: Doesn’t achieve encapsulation Other ideas? my_new_datatype = {} my_new_datatype[“read_words”] = read_words my_new_datatype[“topk”] = topk my_new_datatype[“wordcounts”] = {} wordcounts = my_new_datatype[“read_words”](“somefile.txt”) We’re no better off. We have everything lumped into one place, but the different functions can’t communicate with each other. For example, the read_words function can’t pass the wordcounts dictionary directly to the topk function. So the user still has access, and we don’t trust users.

Data Abstraction, first attempt: Group related functions in a module Cons: Doesn’t achieve encapsulation Data Abstraction, second attempt: Group related functions and data into a data structure Didn’t really help, and made the syntax more difficult. Other ideas?

def read_words(filename): “””Return a dictionary mapping each word to its frequency””” words = open(filename).read().split() for w in words: cnt = wordcounts.setdefault(w, 0) wordcounts[w] = cnt + 1 def wordcount(word): “””Return the count of the given word””” return wordcounts[word] def topk(k=10): “””Return top 10 most frequent words””” scores_with_words = [(s,w) for (w,s) in wordcounts.items()] scores_with_words.sort() return scores_with_words[0:k] def totalwords(): “””Return the total number of words in the file””” return sum([s for (w,s) in wordcounts]) wordcounts = {} Global variable?

# program to compute top 10: read_words(filename) result = topk(10) We’re no longer passing wordcounts around explicitly! Problem solved?

Data Abstraction, first attempt: Group related functions in a module We have to rely on the user to pass the right values around We can’t be sure we won’t break their program if we change our implementations Data Abstraction, second attempt: Group related functions and data into a data structure Didn’t really help, and made the syntax more difficult. Data Abstraction, third attempt: Use a global variable to manage communication between functions Avoids handing off values to that untrustworthy user But pollutes the global namespace And the user still handles our dictionary Other ideas?

def read_words(filename): “””Return a dictionary mapping each word to its frequency””” ourdata.wordcounts = {} words = open(filename).read().split() for w in words: cnt = ourdata.wordcounts.setdefault(w, 0) ourdata.wordcounts[w] = cnt + 1 def wordcount(word): “””Return the count of the given word””” return ourdata.wordcounts[word] def topk(k=10): “””Return top 10 most frequent words””” scores_with_words = [(s,w) for (w,s) in ourdata.wordcounts.items()] scores_with_words.sort() return scores_with_words[0:k] def totalwords(): “””Return the total number of words in the file””” return sum([s for (w,s) in ourdata.wordcounts]) ourdata = object() Global variable, but we can change how its used.

Data Abstraction, first attempt: Group related functions in a module We have to rely on the user to pass the right values around We can’t be sure we won’t break their program if we change our implementations Data Abstraction, second attempt: Group related functions and data into a data structure Didn’t really help, and made the syntax more difficult. Data Abstraction, third attempt: Use a global variable to manage communication between functions Avoids handing off values to that untrustworthy user But pollutes the global namespace And the user still handles our dictionary Data Abstraction, fourth attempt: Use a global variable, but make it generic so the user doesn’t see that we’re using a dictionary, a list, or whatever Still pollutes the global namespace The user still might get their grubby paws on our implementation

Classes A class is like a module: it provides a namespace for a set of functions A class is like a function: it generates a local scope for variables that won’t be seen outside of the class A class can like a data structure: it generates a storage area for data that you can retrieve later.

def read_words(self, filename): “””Return a dictionary mapping each word to its frequency””” words = open(filename).read().split() self.wordcounts = {} for w in words: cnt = self.wordcounts.setdefault(w, 0) self.wordcounts[w] = cnt + 1 def wordcount(self, word): “””Return the count of the given word””” return self.wordcounts[word] def topk(self, k=10): “””Return top 10 most frequent words””” scores_with_words = [(s,w) for (w,s) in self.wordcounts.items()] scores_with_words.sort() return scores_with_words[0:k] def totalwords(self, wordcounts): “””Return the total number of words in the file””” return sum([s for (w,s) in self.wordcounts]) class WordCounts: Special syntax A reference to shared state

# program to compute top 10: wc = WordCounts() wc.read_words(filename) result = wc.topk(10)

# program to compute top 10: wc = WordCounts() wc.read_words(filename) result = wc.topk(10) result = WordCounts.topk(wc, 10) A namespace, like a module A function with two arguments The shared state

Quantitative Analysis def read_measurements(filename): “””Return a dictionary mapping column names to data. Assumes the first line of the file is column names.””” datafile = open(filename) rawcolumns = zip(*[row.split() for row in datafile]) columns = dict([(col[0], col[1:]) for col in rawcolumn return columns def STplot(measurements): “””Generate a scatter plot comparing salinity and temperature””” xs = tofloat(measurements, “salt”) ys = tofloat(measurements, “temp”) plt.plot(xs, ys) plt.show() def minimumO2(measurements): “””Return the minimum value of the oxygen measurement””” return min(tofloat(measurements, “o2”)) def tofloat(measurements, columnname): “””Convert each value in the given iterable to a float””” return [float(x) for x in measurements[columnname]]

def read_measurements(self, filename): “””Return a dictionary mapping column names to data. Assumes the first line of the file is column names.””” datafile = open(filename) rawcolumns = zip(*[row.split() for row in datafile]) self.columns = dict([(col[0], col[1:]) for col in rawcolumn def STplot(self): “””Generate a scatter plot comparing salinity and temperature””” xs = tofloat(self.columns, “salt”) ys = tofloat(self.columns, “temp”) plt.plot(xs, ys) plt.show() def minimumO2(self): “””Return the minimum value of the oxygen measurement””” return min(tofloat(self.columns, “o2”)) def tofloat(self, columnname): “””Convert each value in the given iterable to a float””” return [float(x) for x in self.columns[columnname]] class Measurements:

mm = Measurements() mm.read_measurements(filename) result = mm.Stplot()

We now come to the decisive step of mathematical abstraction: we forget about what the symbols stand for....[The mathematician] need not be idle; there are many operations which he may carry out with these symbols, without ever having to look at the things they stand for. Hermann Weyl, The Mathematical Way of Thinking Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics. Grady Booch