| |||||||||||
| |||||||||||
Moving detail into classes
Object Orientation: Design Techniques example from a Well House Consultants training course
More on Object Orientation: Design Techniques [link]
Source code: bobclass Module: Q907
# Moving detail into classes
data = """ # Data for use during training courses # - are any celebreties overweight? # - Who is tallet and shortest. # - Sort names by Surname # - And lots of other challenges! # As reported at http://www.celeb-height-weight.psyphil.com # Not sure on date or accuracy of this # Fields are space separated. You CANNOT assume 2 names though # (Pam Anderson is a problem as are Ciara and Catherine Zeta Jones) # 1 lb = 0.453592 kgs # Write your code to ignore comments and blank lines Adriana Lima 5 feet 10 inches / 177.80 cms 110.23-121.25 lbs Alicia Keys 5 feet 6 inches / 167.64 cms 114.64-125.66 lbs Al Pacino 5 feet 7 inches / 170.18 cms 165.35-176.37 lbs Amber Valletta 5 feet 8.5 inches / 173.99 cms 99.21-110.23 lbs Angelina Jolie 5 feet 8 inches / 172.72 cms 116.84-127.87 lbs Anna Kournikova 5 feet 8 inches / 172.72 cms 114.64-125.66 lbs Ashley Judd 5 feet 7 inches / 170.18 cms 119.05-130.07 lbs Avril Lavigne 5 feet 2 inches / 157.48 cms 108.03-119.05 lbs Bar Rafaeli 5 feet 8.5 inches / 174 cms 123.46-134.48 lbs Ben Stiller 5 feet 6.5 inches / 168.91 cms 165.35-176.37 lbs Beyonce Knowles 5 feet 6 inches / 167.64 cms 125.66-136.69 lbs Blake Lively 5 feet 8 inches / 172.72 cms 119.05-130.07 lbs Brad Pitt 5 feet 11 inches / 180.34 cms 194.01-205.03 lbs Britney Spears 5 feet 4 inches / 162.56 cms 103.62-114.64 lbs Bruce Willis 5 feet 11.5 inches / 181.61 cms 202.83-213.85 lbs Calista Flockhart 5 feet 6 inches / 167.64 cms 92.59-103.62 lbs Cameron Diaz 5 feet 8.5 inches / 173.99 cms 114.64-125.66 lbs Carmen Kass 5 feet 10.5 inches / 179.07 cms 108.03-119.05 lbs Carmen Electra 5 feet 3 inches / 160.02 cms 108.03-119.05 lbs Carrie Underwood 5 feet 3 inches / 160.02 cms 108.03-119.05 lbs Cate Blanchett 5 feet 8.5 inches / 173.99 cms 114.64-125.66 lbs Catherine Zeta Jones 5 feet 8 inches / 172.72 cms 123.46-134.48 lbs Charlize Theron 5 feet 9.5 inches / 176.53 cms 125.66-136.69 lbs Christian Bale 6 feet 0 inch/ 182.88 cms 185.19-196.21 lbs Christina Aguilera 5 feet 1.5 inches / 156.21 cms 99.21-110.23 lbs Christy Turlington 5 feet 10 inches / 177.80 cms 110.23-121.25 lbs Ciara 5 feet 8 inches / 172.72 cms 127.87-138.89 lbs Cindy Crawford 5 feet 10 inches / 177.80 cms 114.64-125.66 lbs Cindy Margolis 5 feet 7 inches / 170.18 cms 114.64-125.66 lbs Claire Danes 5 feet 6 inches / 167.64 cms 105.82-116.84 lbs Courtney Love 5 feet 9 inches / 175.26 cms 123.46-134.48 lbs Daniel Radcliffe 5 feet 5.5 inches / 166.37 cms 114.64-125.66 lbs Daniel Craig 5 feet 10.5 inches / 179.07 cms 178.57-189.60 lbs Denise Richards 5 feet 6 inches / 167.64 cms 114.64-125.66 lbs Drew Barrymore 5 feet 4 inches / 162.56 cms 116.84-127.87 lbs Elisha cuthbert 5 feet 2.5 inches / 158.75 cms 105.82-116.84 lbs Elle MacPherson 6 feet 0 inch / 182.88 cms 123.46-134.48 lbs Emily Osment 5 feet 2.5 inches / 158.75 cms 103.62-114.64 lbs Emma Bunton 5 feet 2 inches / 157.48 cms 130.07-141.10 lbs Eva Longoria 5 feet 2 inches / 157.48 cms 99.21-110.23 lbs Faith Hill 5 feet 9 inches / 175.26 cms 116.84-127.87 lbs Fergie / Stacy Ferguson 5 feet 2.5 inches / 158.75 cms 103.62-114.64 lbs Fiona Apple 5 feet 3 inches / 160.02 cms 99.21-110.23 lbs George Clooney 5 feet 11 inches / 180.34 cms 185.19-196.21 lbs Geri Halliwell 5 feet 2 inches / 157.48 cms 101.41-112.44 lbs Gisele Bundchen 5 feet 11 inches / 180.34 cms 114.64-125.66 lbs Gwen Stefani 5 feet 6 inches / 167.64 cms 110.23-121.25 lbs Gwyneth Paltrow 5 feet 9 inches / 175.26 cms 110.23-121.25 lbs Halle Berry 5 feet 5.5 inches / 166.37 cms 110.23-121.25 lbs Heather Locklear 5 feet 5 inches / 165.10 cms 105.82-116.84 lbs Heidi Klum 5 feet 9 inches / 175.26 cms 114.64-125.66 lbs Hilary Duff 5 feet 1 inch / 154.94 cms 121.25-132.28 lbs Hulk Hogan 6 feet 4 inches / 193.04 cms 295.42-306.44 lbs Jason Statham 5 feet 9 inches / 175.26 cms 165.35-176.37 lbs Jenna Jameson 5 feet 4 inches / 163 cms 103.62-114.64 lbs Jessica Alba 5 feet 6 inches / 167.64 cms 114.64-125.66 lbs Jessica Biel 5 feet 7.5 inches / 171.45 cms 105.82-116.84 lbs Jessica Simpson 5 feet 3 inches / 160.02 cms 116.84-127.87 lbs Jennifer Aniston 5 feet 4.5 inches / 163.83 cms 108.03-119.05 lbs Jennifer Garner 5 feet 8 inches / 172.72 cms 110.23-121.25 lbs Jennifer Love Hewitt 5 feet 2.5 inches / 158.75 cms 114.64-125.66 lbs Jennifer Lopez 5 feet 5.5 inches / 166.37 cms 119.05-130.07 lbs Jenny McCarthy 5 feet 7 inches / 170.18 cms 116.84-127.87 lbs Jewel Kilcher 5 feet 6 inches / 167.64 cms 112.44-123.46 lbs Jodie Kidd 6 feet 2 inches / 187.96 cms 132.28-143.30 lbs Johnny Depp 5 feet 10 inches / 177.80 cms 152.12-163.14 lbs Julia Roberts 5 feet 8 inches / 172.72 cms 116.84-127.87 lbs Julia Stiles 5 feet 7 inches / 170.18 cms 112.44-123.46 lbs Justin Timberlake 6 feet 1 inch / 185.42 cms 156.53-167.55 lbs Kate Beckinsale 5 feet 8 inches / 172.72 cms 114.64-125.66 lbs Kate Hudson 5 feet 6 inches / 167.64 cms 112.44-123.46 lbs Katherine Heigl 5 feet 9 inches / 175.26 cms 116.84-127.87 lbs Katie Holmes 5 feet 9 inches / 175.26 cms 114.64-125.66 lbs Kate Moss 5 feet 6 inches / 167.64 cms 101.41-112.44 lbs Keira Knightley 5 feet 7 inches / 170.18 cms 99.21-110.23 lbs Keri Russell 5 feet 4 inches / 162.56 cms 112.44-123.46 lbs Kirsten Dunst 5 feet 6 inches / 167.64 cms 103.62-114.64 lbs Kristen Stewart 5 feet 4 inches / 163 cms 103.62-108.03 lbs Lacey Chabert 5 feet 2 inches / 157.48 cms 99.21-110.23 lbs Laetitia Casta 5 feet 7 inches / 170.18 cms 119.05-130.07 lbs Leann Rimes 5 feet 5 inches / 165.10 cms 110.23-121.25 lbs Leonardo Dicaprio 5 feet 11.5 inches / 181.61 cms 163.14-174.17 lbs Lil Wayne 5.5 inches / 165.10 cms 134.48-145.51 lbs Lindsay Lohan 5 feet 5 inches / 165.10 cms 103.62-114.64 lbs Lisa Kudrow 5 feet 8 inches / 172.72 cms 116.84-127.87 lbs Lucy Lawless 5 feet 10 inches / 177.80 cms 123.46-134.48 lbs Madonna 5 feet 4 inches / 162.56 cms 99.21-110.23 lbs Mandy Moore 5 feet 10 inches / 177.80 cms 123.46-134.48 lbs Mariah Carey 5 feet 9 inches / 175.26 cms 121.25-132.28 lbs Mark Wahlberg 5 feet 8.5 inches / 173.99 cms 163.14-174.17 lbs Matt Damon 5 feet 10 inches / 177.80 cms 154.32-165.35 lbs Megan Fox 5 feet 4 inches / 163 cms 108.03-112.44 lbs Miley Cyrus 5 feet 4.5 inches / 164 cms 103.62-108.03 lbs Milla Jovovich 5 feet 8 inches / 172.72 cms 116.84-127.87 lbs Naomi Campbell 5 feet 10 inches / 177.80 cms 108.03-119.05 lbs Natalie Portman 5 feet 4 inches / 162.56 cms 105.82-116.84 lbs Nicole Kidman 5 feet 10.5 inches / 179.07 cms 116.84-127.87 lbs Nicole Richie 5 feet 1 inch / 154.94 cms 92.59-103.62 lbs Orlando Bloom 5 feet 10.5 inches / 179.07 cms 147.71-152.12 lbs Pam / Pamela Anderson 5 feet 7 inches / 170.18 cms 99.21-110.23 lbs Paris Hilton 5 feet 8 inches / 172.72 cms 110.23-121.25 lbs Patrick Swayze 5 feet 10 inches / 178 cms 105.82-110.23 lbs Penelope Cruz 5 feet 4 inches / 172.72 cms 103.62-114.64 lbs Rachelle Lefevre 5 feet 6 inches / 168 cms 108.03-119.05 lbs Rachael Leigh Cook 5 feet 2 inches / 157.48 cms 97-108.03 lbs Rachel Miner 5 feet 2 inches / 157 cms 103.62-108.03 lbs Raven Symone 5 feet 11 inches / 180.34 cms 163.14-174.17 lbs Rebecca Gayheart 5 feet 8 inches / 172.72 cms 108.03-119.05 lbs Rebecca Romijn-Stamos 5 feet 11 inches / 180.34 cms 123.46-134.48 lbs Reese Witherspoon 5 feet 2 inches / 157.48 cms 110.23-121.25 lbs Rihanna 5 feet 8 inches / 172.72 cms 125.66-136.69 lbs Robert Pattinson 6 feet 1 inches / 185 cms 171.96-176.37 lbs Rodrigo Santoro 6 feet 2 inches / 187.96 cms 176.37-180.78 lbs Rose McGowan 5 feet 3 inches / 160.02 cms 97-108.03 lbs Rupert Grint 5 feet 10 inches / 177.80 cms 132.28-143.30 lbs Ryan Seacrest 5 feet 8 inches / 172.72 cms 149.91-154.32 lbs Sandra Bullock 5 feet 7 inches / 170.18 cms 110.23-121.25 lbs Salma Hayek 5 feet 2 inches / 157.48 cms 112.44-123.46 lbs Sarah Michelle Gellar 5 feet 3 inches / 160.02 cms 97-108.03 lbs Scarlett Johansson 5 feet 3.5 inches / 162 cms 110.23-114.64 lbs Shakira 5 feet 2 inches / 157.48 cms 123.46-134.48 lbs Shannon Elizabeth 5 feet 9 inches / 175.26 cms 103.62-114.64 lbs Shania Twain 5 feet 4 inches / 162.56 cms 103.62-114.64 lbs Sylvester Stallone 5 feet 10 inches / 177.80 cms 216.05-220.46 lbs Tyra Banks 5 feet 10 inches / 177.80 cms 116.84-127.87 lbs Tara Reid 5 feet 5 inches / 165.10 cms 105.82-116.84 lbs Teri Hatcher 5 feet 6 inches / 167.64 cms 101.41-112.44 lbs Taylor Swift 5 feet 11 inches / 180.34 cms 110.23-121.25 lbs Victoria Beckham 5 feet 6 inches / 167.64 cms 103.62-114.64 lbs Vin Diesel 6 feet 0 inch / 182.88 cms 216.05-220.46 lbs Zooey Deschanel 5 feet 6 inches / 167.64 cms 114.64-125.66 lbs """ import re live = re.compile('[^#]*') summat = re.compile('\S') class person(object): def __init__(this,lyne): this.valid = True nocomment = (live.findall(lyne))[0] realinfo = summat.findall(nocomment) if not realinfo: this.valid = False if this.valid: parts = lyne.split(" ") this.height = float(parts[-4]) l,h = parts[-2].split('-') this.lower = float(l) this.higher = float(h) try: this.forename = parts[0] this.surname = parts[-10] except: this.valid = False def getBMI(this): return round(this.lower * 0.453592 / (this.height * this.height * 0.01 * 0.01),2) def isValid(this): return this.valid def __str__(this): return this.forename + " " + this.surname + " is " + str(this.height) + "cms tall" # ------------ expert above, user below for lyne in data.split('\n'): johnny = person(lyne) if johnny.isValid() : print johnny,johnny.getBMI() else: pass # print "REJECTED DATA LINE: ",lyne # Sample Output # # -bash-4.1$ python bobclass # Adriana Lima is 177.8cms tall 15.82 # Alicia Keys is 167.64cms tall 18.5 # Al Pacino is 170.18cms tall 25.9 # Amber Valletta is 173.99cms tall 14.87 # Angelina Jolie is 172.72cms tall 17.77 # Anna Kournikova is 172.72cms tall 17.43 # Ashley Judd is 170.18cms tall 18.65 # Avril Lavigne is 157.48cms tall 19.76 # Bar Rafaeli is 174.0cms tall 18.5 # Ben Stiller is 168.91cms tall 26.29 # Beyonce Knowles is 167.64cms tall 20.28 # Blake Lively is 172.72cms tall 18.1 # Brad Pitt is 180.34cms tall 27.06 # Britney Spears is 162.56cms tall 17.79 # Bruce Willis is 181.61cms tall 27.89 # Calista Flockhart is 167.64cms tall 14.94 # Cameron Diaz is 173.99cms tall 17.18 # Carmen Kass is 179.07cms tall 15.28 # Carmen Electra is 160.02cms tall 19.14 # Carrie Underwood is 160.02cms tall 19.14 # Cate Blanchett is 173.99cms tall 17.18 # Catherine Jones is 172.72cms tall 18.77 # Charlize Theron is 176.53cms tall 18.29 # Christian Christian is 182.88cms tall 25.12 # Christina Aguilera is 156.21cms tall 18.44 # Christy Turlington is 177.8cms tall 15.82 # Ciara Ciara is 172.72cms tall 19.44 # Cindy Crawford is 177.8cms tall 16.45 # Cindy Margolis is 170.18cms tall 17.95 # Claire Danes is 167.64cms tall 17.08 # Courtney Love is 175.26cms tall 18.23 # Daniel Radcliffe is 166.37cms tall 18.79 # Daniel Craig is 179.07cms tall 25.26 # Denise Richards is 167.64cms tall 18.5 # Drew Barrymore is 162.56cms tall 20.06 # Elisha cuthbert is 158.75cms tall 19.05 # Elle MacPherson is 182.88cms tall 16.74 # Emily Osment is 158.75cms tall 18.65 # Emma Bunton is 157.48cms tall 23.79 # Eva Longoria is 157.48cms tall 18.15 # Faith Hill is 175.26cms tall 17.25 # Fergie Ferguson is 158.75cms tall 18.65 # Fiona Apple is 160.02cms tall 15.07 # George Clooney is 180.34cms tall 25.83 # Geri Halliwell is 157.48cms tall 18.55 # Gisele Bundchen is 180.34cms tall 15.99 # Gwen Stefani is 167.64cms tall 17.79 # Gwyneth Paltrow is 175.26cms tall 16.28 # Halle Berry is 166.37cms tall 18.06 # Heather Locklear is 165.1cms tall 17.61 # Heidi Klum is 175.26cms tall 16.93 # Hilary Duff is 154.94cms tall 22.91 # Hulk Hogan is 193.04cms tall 35.96 # Jason Statham is 175.26cms tall 24.42 # Jenna Jameson is 163.0cms tall 17.69 # Jessica Alba is 167.64cms tall 18.5 # Jessica Biel is 171.45cms tall 16.33 # Jessica Simpson is 160.02cms tall 20.7 # Jennifer Aniston is 163.83cms tall 18.26 # Jennifer Garner is 172.72cms tall 16.76 # Jennifer Hewitt is 158.75cms tall 20.63 # Jennifer Lopez is 166.37cms tall 19.51 # Jenny McCarthy is 170.18cms tall 18.3 # Jewel Kilcher is 167.64cms tall 18.15 # Jodie Kidd is 187.96cms tall 16.98 # Johnny Depp is 177.8cms tall 21.83 # Julia Roberts is 172.72cms tall 17.77 # Julia Stiles is 170.18cms tall 17.61 # Justin Timberlake is 185.42cms tall 20.65 # Kate Beckinsale is 172.72cms tall 17.43 # Kate Hudson is 167.64cms tall 18.15 # Katherine Heigl is 175.26cms tall 17.25 # Katie Holmes is 175.26cms tall 16.93 # Kate Moss is 167.64cms tall 16.37 # Keira Knightley is 170.18cms tall 15.54 # Keri Russell is 162.56cms tall 19.3 # Kirsten Dunst is 167.64cms tall 16.72 # Kristen Stewart is 163.0cms tall 17.69 # Lacey Chabert is 157.48cms tall 18.15 # Laetitia Casta is 170.18cms tall 18.65 # Leann Rimes is 165.1cms tall 18.34 # Leonardo Dicaprio is 181.61cms tall 22.44 # Lindsay Lohan is 165.1cms tall 17.24 # Lisa Kudrow is 172.72cms tall 17.77 # Lucy Lawless is 177.8cms tall 17.71 # Madonna Madonna is 162.56cms tall 17.03 # Mandy Moore is 177.8cms tall 17.71 # Mariah Carey is 175.26cms tall 17.91 # Mark Wahlberg is 173.99cms tall 24.44 # Matt Damon is 177.8cms tall 22.14 # Megan Fox is 163.0cms tall 18.44 # Miley Cyrus is 164.0cms tall 17.48 # Milla Jovovich is 172.72cms tall 17.77 # Naomi Campbell is 177.8cms tall 15.5 # Natalie Portman is 162.56cms tall 18.16 # Nicole Kidman is 179.07cms tall 16.53 # Nicole Richie is 154.94cms tall 17.49 # Orlando Bloom is 179.07cms tall 20.89 # Pam Anderson is 170.18cms tall 15.54 # Paris Hilton is 172.72cms tall 16.76 # Patrick Swayze is 178.0cms tall 15.15 # Penelope Cruz is 172.72cms tall 15.76 # Rachelle Lefevre is 168.0cms tall 17.36 # Rachael Cook is 157.48cms tall 17.74 # Rachel Miner is 157.0cms tall 19.07 # Raven Symone is 180.34cms tall 22.75 # Rebecca Gayheart is 172.72cms tall 16.43 # Rebecca Romijn-Stamos is 180.34cms tall 17.22 # Reese Witherspoon is 157.48cms tall 20.16 # Rihanna Rihanna is 172.72cms tall 19.11 # Robert Pattinson is 185.0cms tall 22.79 # Rodrigo Santoro is 187.96cms tall 22.64 # Rose McGowan is 160.02cms tall 17.18 # Rupert Grint is 177.8cms tall 18.98 # Ryan Seacrest is 172.72cms tall 22.79 # Sandra Bullock is 170.18cms tall 17.26 # Salma Hayek is 157.48cms tall 20.57 # Sarah Gellar is 160.02cms tall 17.18 # Scarlett Johansson is 162.0cms tall 19.05 # Shakira Shakira is 157.48cms tall 22.58 # Shannon Elizabeth is 175.26cms tall 15.3 # Shania Twain is 162.56cms tall 17.79 # Sylvester Stallone is 177.8cms tall 31.0 # Tyra Banks is 177.8cms tall 16.76 # Tara Reid is 165.1cms tall 17.61 # Teri Hatcher is 167.64cms tall 16.37 # Taylor Swift is 180.34cms tall 15.37 # Victoria Beckham is 167.64cms tall 16.72 # Vin Diesel is 182.88cms tall 29.3 # Zooey Deschanel is 167.64cms tall 18.5 # -bash-4.1$ # Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Python * Learning to Program in Java * Learning to program in Ruby * Learning to program in C and C++ * Object Oriented Programming in PHP * Python Programming * Perl for Larger Projects * C++ for C Programmers * C and C++ Programming * Java Bootcamp * Ruby Programming * Java Programming for the Web * Deploying Java Applications on Linux / Unix * Intermediate Python * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering all the subjects we teach are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Object Orientation: Design Techniques" training module. You'll find a description of the topic and some
other closely related examples on the "Object Orientation: Design Techniques" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |