13.07.2015 Views

Analysis of Structures - Baustatik-Info-Server

Analysis of Structures - Baustatik-Info-Server

Analysis of Structures - Baustatik-Info-Server

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.14. OOP WITH CLASSES Page 432.14.2 Implementation <strong>of</strong> a Time Stack ClassIn section 2.12 we have discussed a little program which implements a time checker using a stack container.We can implement this problem in a much more clearer version, if we do it in terms <strong>of</strong> the OOPapproach. The class TimeCheck, which is introduced, encapsulates the time stack list and the log filesname as attributes and the following methods.• Set reads the actual time with the datetime.now() and pushes it onto the time stack.• Get reads the actual time with the datetime.now(), pops the the last time object from the stack andcalculates the timespan. The timespan is optionally printed into the console window or into a logfile.• GetTime calculates the seconds in floats form the passed timedelta object.The main program, which is used to discuss the class follows the steps <strong>of</strong> the classical implementation<strong>of</strong> the problem discussed in section 2.12. We first create an empty tuple and an empty list. Than wecreate the TimeCheck instance 16 simple by assigning it’s name to a variable (s = TimeCheck()). Topush a time object onto the stack, we call the class’s member function Set. So, if we want to check theperformance <strong>of</strong> a code, we first call the Set method and after having executed the code, we call the Getmethod. The Get method will print the comment and will return the used seconds <strong>of</strong> execution time infloats. The simplest kind <strong>of</strong> an implementation is the implementation in the file <strong>of</strong> the main program.This you can see in the following example.A UML diagram <strong>of</strong> the class TimeCheck is given in figure 2.12.TimeCheckself. log: name <strong>of</strong> the log fileself. stack: stack listself. init (..): contructorself.Set(..): push a timeself.GetTime(..): calculate secondsself.Get(..): pop a timeThe class TimeCheck contentsall the data and functions touse a time stack and to get thetime-span information in a properfloat format. The class is able topresent a formated output <strong>of</strong> themeasured time-spans.Figure 2.12: UML-Diagram <strong>of</strong> the TimeCheck ClassOOP Implementation <strong>of</strong> the Time Checking Problem’’’Class to investigate the timespan, to measure the performanceby implementing a time stack container’’’# - module - member aliasfrom datetime import datetime as time # we need the datetime packageclass TimeCheck():# class name# constructor16 A physical created object in memory <strong>of</strong> a class is called instance <strong>of</strong> a class.2.12.2011

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!