[Tutor] Shifts and DateTime problems

Kirby Urner urnerk@qwest.net
Wed, 06 Feb 2002 22:46:32 -0800


At 01:20 AM 2/7/2002 +0100, mikalzet@libero.it wrote:

>Well, by fumbling around I've finally reached this result:
>
>
>class Shift:
>
>     def __init__(self, name="None", worker="None", begin="None", end="None"):
>         self.name = name
>         self.worker = worker
>         self.begin = begin
>         self.end = end
>
>     def __str__(self):
>         return str(self.name) + "\n" + str(self.worker)  + "\n" + 
> str(self.begin) + " - " + str(self.end)
>
>from mx.DateTime import *
>import pgdb
>db = pgdb.connect(database = 'turni')
>cursor = db.cursor()
>cursor.execute('select * from shifts;')
>shifts = cursor.fetchone()
>pythonworker = shifts[0]
>pythonname= shifts[3]
>pythonbegin = DateTimeFrom(shifts[1])
>pythonend = DateTimeFrom(shifts[2])
>turno = Shift(name = pythonname, begin=pythonbegin.hour, end = 
>pythonend.hour, worker = pythonworker)
>print turno
>
>
>Which gives me the following output:
>
>Shiftsname
>Workersname
>7 - 13
>
>So it seems most of the problems I was complaining about are solved.
>It does seem sort of fumbling though ...
>Any comments  ?

You don't have to name your input arguments as long as they
correspond, i.e. instead of

turno = Shift(name = pythonname, begin=pythonbegin.hour,
               end = pythonend.hour, worker = pythonworker)

you can just go:

shifts = cursor.fetchone()
turno  = Shift(pythonname, pythonbegin.hour,
                pythonend.hour, pythonworker)

You may also choose to skip all those interim variables,
and just go:

shifts = cursor.fetchone()
turno  = Shift(shifts[3],                     # Name
                DateTimeFrom(shifts[1]).hour,  # Start Hour
                DateTimeFrom(shifts[2]).hour,  # End Hour
                shifts[0])                     # Worker

Note where I put my comments -- that's legal.  Just
trying to give you some ideas, point out your
many freedoms.

If the only purpose of your class is really just to
print something formatted, then you don't really
need a class.  But I assume you're thinking to
expand on the above model somehow.

Finally, if DateTimeFrom is all you're using from
mx.DateTime, then I'd go:

from mx.DateTime import DateTimeFrom

but that's a small matter.

Kirby