# Beginner: Data type conversion question

flagg ianand0204 at gmail.com
Fri Jan 16 18:14:17 CET 2009

On Jan 15, 11:43 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Thu, 15 Jan 2009 21:09:43 -0800, flagg wrote:
> > def checkSerial():
> >     """
> >     Checks the current 'date' portion of the serial number and checks
> >     the current 'counter'(the two digit number at the end of the serial
> >     number), then returns a complete new serial """
> >     currentDate =  time.strftime("%Y""%m""%d", time.localtime())
>
> The format string can be written as *one* string literal instead of
> three: "%Y%m%d".
>
> >     for (name, ttl, rdata) in zone.iterate_rdatas(SOA):
> >         date = str(rdata.serial)[0:8]
> >         inc = str(rdata.serial)[8:10]
>
> Here you are converting `rdata.serial` twice.
>
>           tmp = str(rdata.serial)
>           date = tmp[0:8]
>           inc = int(tmp[8:10])
>
> As `inc` is conceptually a number, you should do theconversionhere and
> treat it as number from now on.
>
> >     if date == currentDate:
> >         int(inc) + 1
> >         print inc
> >         newInc = str(inc).zfill(2)
> >         serial = date + newInc
> >         print "date is the same"
> >         return serial
> >     elif date < currentDate:
> >         newInc = "01".zfill(2)
> >         serial = currentDate + newInc
> >         print "date is different"
> >         return serial
>
> Both branches do almost the same.  You should try to avoid such code
> duplication.
>
>       if date == currentDate:
>           inc += 1
>       elif date < currentDate:
>           inc = 1
>       else:
>           assert False   # Should never happen.
>
>       return "%s%02d" % (date, inc)
>
> That's it.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Ah

return "%s%02d" % (date, inc)

This piece is what I was missing in my original design.  I couldn't
figure out hot to take and integer and force it to my double digit.
i.e. 01 instead of 1.   Which is why I was using strings to make that
happen. (obviously incorrectly).  Thanks

Could you explain what "assert" does in the if statement.  I am
assuming if the first two conditions are not met, it will hit "assert
false" and exit the program?