Beginner: Data type conversion question

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Jan 16 08:43:26 CET 2009


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 the conversion here 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



More information about the Python-list mailing list