EuroPython 2016
Introduction
About
ValidationConstraint
Action
Layout
Button
post.save()
https://goo.gl/3DMHFH
EuroPython 2016
Introduction
OOP is not OO
Supporting mechanisms
for OO
Conceptual modelof a
problem domain
Programminglanguage
map to
Code
EuroPython 2016
Summary
Operations vs methods
Operation Object1..*operates on
Method1..*
1
is implemented by
EuroPython 2016
Summary
OO conceptsObject
Type classifies
0..*
subtype0..*
Relation
links
2..*
Operation
1..*
operates on
Method
implemented by1..*
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?
Zoom in more
def parse_file(price_file): for row in price_file: yield row.split(‘,’)
EuroPython 2016
Implementing OO
OOP and Python
ObjectOriented
Programming
ClassicalOOP
PrototypeBased
OOP implementation
EuroPython 2016
Classical OOP & PythonPython cookies
>>> class InvestmentInstrument:... pass
>>> fund = InvestmentInstrument()>>> fund<InvestmentInstrument object at 0x7f6815559fd0>
>>> isinstance(fund, InvestmentInstrument)True
>>> type(fund)<class ‘InvestmentInstrument’>
EuroPython 2016
Classical OOP & PythonRelationships as attributes
Portfolio Person
Me
You
owner
owner
EuroPython 2016
Classical OOP & PythonAttributes
>>> portfolio = Portfolio()>>> someone = Person()>>> portfolio.owner = someone
>>> portfolio.owner<Person instance at 0x7fd6d88100e0>
EuroPython 2016
Classical OOP & PythonMethods as (Python)methods
InvestmentInstrument
def activate(self): self.is_active = True
indexed by class
Operation
EuroPython 2016
Classical OOP & PythonA Python method
class InvestmentInstrument:
def activate(self): self.is_active = True
>>> fund = InvestmentInstrument()>>> InvestmentInstrument.activate(fund)
>>> fund.is_activeTrue >>> fund.activate()
EuroPython 2016
Classical OOP & PythonInitialising instances
class InvestmentInstrument: def __init__(self): self.is_active = False
def activate(self): self.is_active = True
>>> fund = InvestmentInstrument()>>> fund.is_activeFalse>>> fund.activate()
EuroPython 2016
Classical OOP & PythonSubtyping 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 & PythonSubtyping as inheritance
>>> fund = UnitTrustFund()>>> isinstance(fund, InvestmentInstrument)True
>>> fund.activate()>>> fund.is_activeTrue
EuroPython 2016
Classical OOP & PythonThe 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 & PythonThe ghost of operations
FileFormatPriceFile
for line in price_file.format.parse(price_file):# do stuff with line
EuroPython 2016
Classical OOP & PythonThe 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
Worse designCommissionCalc
Bundles QuoteBasis
QuoteFeesCommissions
AbstractCommissionBundle
*
QuoteBenefit BenefitSummary
Calc
* BenefitGrouping
*
*QuoteBenefit
BundlesBenefit
Calc
*
CommissionBundle
AlteredCommissionBundle
*
LookupCommissionScale
LookupCommissionScaleFactory
*
CalculationConstants
EuroPython 2016
Design
Better designQuoteScenario
Benefit Category*
*
*
*
CommisionBundle
includedInAPI?negotiatedMax (R or %LOA)
CommissionScale
*Product
VAT
BenefitGrouping*
CommissionBand
EuroPython 2016
Design
Inheritance vs subtyping
ObjectTable
ObjectTableWithFilters
ObjectTable Filter0..*
Report ObjectTable
Filter*
ObjectTableByDate
EuroPython 2016
Thanks
On groups.google.com:● reahl-discuss
www.reahl.org
James Martin & James Odell:Object Oriented Methods: A Foundation
Martin Fowler: Refactoring
Slides: https://goo.gl/NPLnCf