EuroPython 2016
Whats the point of Object Orientation? Iwan Vosloo EuroPython 2016 - - PowerPoint PPT Presentation
Whats the point of Object Orientation? Iwan Vosloo EuroPython 2016 - - PowerPoint PPT Presentation
Whats the point of Object Orientation? Iwan Vosloo EuroPython 2016 Introduction About ValidationConstraint Layout Button Action post.save() https://goo.gl/3DMHFH EuroPython 2016 Introduction About data_table = DataTable(view,
EuroPython 2016
Introduction
About
ValidationConstraint Action Layout Button post.save()
https://goo.gl/3DMHFH
EuroPython 2016
Introduction
About
https://goo.gl/3DMHFH
data_table = DataTable(view, ….. )
EuroPython 2016
Introduction
About / 2
EuroPython 2016
Introduction
OOP is not OO
Supporting mechanisms for OO Conceptual model
- f a
problem domain Programming language map to Code
EuroPython 2016
Introduction
Cause and effect
EuroPython 2016
Introduction
Understand a program?
EuroPython 2016
Introduction
Scaling up
100 lines
- f code?
Courtesy: inkwellideas.com
EuroPython 2016
Introduction
How about...
180 000 lines
- f code?
EuroPython 2016
Introduction
Understanding
- pportunity
u s e l e s s i n f
- r
m a t i
- n
d a n g e r
EuroPython 2016
Introduction
A scary thought
EuroPython 2016
Concepts
A conceptual model
Chair
EuroPython 2016
Concepts
Concepts as sets
Chair Desk
EuroPython 2016
Concepts
Refinement via subsets
Chair Office Chair
EuroPython 2016
Relations
Connecting objects
Chair Desk
EuroPython 2016
Relations
Relation as a concept
Assignment
EuroPython 2016
Relations
Multiplicity
Chair Desk
EuroPython 2016
Notation
It gets complicated...
Chair Desk Assignment Office Chair
EuroPython 2016
Notation
UML
Desk Chair OfficeChair assignment
1 *
EuroPython 2016
More on concepts
Intangible concepts
Portfolio Investment Instrument UnitTrust
EuroPython 2016
More on concepts
Overlapping concepts
Person Investor
EuroPython 2016
More on concepts
Changing classification
Person Employee Investor
EuroPython 2016
More on concepts
Concept labels
Concept Synonym Type Concept Label Chair
EuroPython 2016
Affecting objects
Operations
Price PriceFile UnitTrust Fund
* load_prices( )
EuroPython 2016
Summary
Operations vs methods
Operation Object 1..*
- perates on
Method 1..* 1 is implemented by
EuroPython 2016
Summary
OO concepts
Object Type classifies 0..* subtype 0..* Relation links 2..* Operation 1..*
- perates on
Method implemented by 1..*
EuroPython 2016
How can this help?
Programs are programs
Courtesy: inkwellideas.com
Concept Concept
EuroPython 2016
How can this help?
Understanding-structured
Courtesy: inkwellideas.com
parse_file: Operation parse_xml: Method parse_csv: Method
EuroPython 2016
How can this help?
Focus on one thing
FileFormat PriceFile
EuroPython 2016
How can this help?
Zoom in
FileFormat Excel CSV
EuroPython 2016
How can this help?
Zoom in more
def parse_file(price_file): for row in price_file: yield row.split(‘,’)
EuroPython 2016
Implementing OO
OOP and Python
Object Oriented Programming Classical OOP Prototype Based
OOP implementation
EuroPython 2016
Classical OOP & Python
Types as classes
an instance an instance an instance
EuroPython 2016
Classical OOP & Python
Python cookies
>>> class InvestmentInstrument: ... pass >>> fund = InvestmentInstrument() >>> fund <InvestmentInstrument object at 0x7f6815559fd0> >>> isinstance(fund, InvestmentInstrument) True >>> type(fund) <class ‘InvestmentInstrument’>
EuroPython 2016
Classical OOP & Python
Relationships as attributes
Portfolio Person Me You
- wner
- wner
EuroPython 2016
Classical OOP & Python
Attributes
>>> portfolio = Portfolio() >>> someone = Person() >>> portfolio.owner = someone >>> portfolio.owner <Person instance at 0x7fd6d88100e0>
EuroPython 2016
Classical OOP & Python
Methods as (Python)methods
Investment Instrument def activate(self): self.is_active = True indexed by class Operation
EuroPython 2016
Classical OOP & Python
A Python method
class InvestmentInstrument: def activate(self): self.is_active = True >>> fund = InvestmentInstrument() >>> InvestmentInstrument.activate(fund) >>> fund.is_active True >>> fund.activate()
EuroPython 2016
Classical OOP & Python
Initialising instances
class InvestmentInstrument: def __init__(self): self.is_active = False def activate(self): self.is_active = True >>> fund = InvestmentInstrument() >>> fund.is_active False >>> fund.activate()
EuroPython 2016
Classical OOP & Python
Subtyping as inheritance
class InvestmentInstrument: def __init__(self): self.is_active = False def activate(self): self.is_active = True class UnitTrustFund(InvestmentInstrument): def value_of(self, units): return units * self.unit_price
EuroPython 2016
Classical OOP & Python
Subtyping as inheritance
>>> fund = UnitTrustFund() >>> isinstance(fund, InvestmentInstrument) True >>> fund.activate() >>> fund.is_active True
EuroPython 2016
Classical OOP & Python
The ghost of operations
FileFormat Excel CSV
parse( )
EuroPython 2016
Classical OOP & Python
The ghost of operations
class CSVFileFormat(FileFormat): def parse(self, price_file): for row in price_file: yield row.split(‘,’) class ExcelFileFormat(FileFormat): def parse(self, price_file): # totally different stuff
EuroPython 2016
Classical OOP & Python
The ghost of operations
FileFormat PriceFile for line in price_file.format.parse(price_file): # do stuff with line
EuroPython 2016
Classical OOP & Python
The ghost of operations
if price_file.format.is_csv: lines = parse_csv(price_file) elif price_file.format.is_excel: lines = parse_excel(price_file)
else:
raise Exception(‘not supposed to get here’) for line in lines: # do stuff with line
EuroPython 2016
Design
What’s design?
Person Employee Investor Person Role Employee Investor Company
EuroPython 2016
Design
Worse design
CommissionCalc Bundles QuoteBasis QuoteFees Commissions AbstractCommission Bundle * QuoteBenefit Benefit Summary Calc * Benefit Grouping * * QuoteBenefit Bundles Benefit Calc * Commission Bundle Altered CommissionBundle * Lookup CommissionScale LookupCommission ScaleFactory * Calculation Constants
EuroPython 2016
Design
Better design
QuoteScenario Benefit Category * * * * CommisionBundle
includedInAPI? negotiatedMax (R or %LOA)
Commission Scale * Product
VAT
BenefitGrouping * Commission Band
EuroPython 2016
Design
Inheritance vs subtyping
ObjectTable ObjectTable WithFilters ObjectTable Filter
0..*
Report ObjectTable Filter
*
ObjectTable ByDate
EuroPython 2016
Thanks
On groups.google.com:
- reahl-discuss