Hi all,
I created a class which creates a relative unique id string, now my
program just works fine and as expected but somehow I get the feeling
that I misused the __repr__ since I guess people expect to 'execute' a
function in an instance instead of using it's representation string of
the instance itself, could you elaborate whether you find this bad
practice and if yes what would have been a better way to do it?
TIA
----- script -----[color=blue]
> import string
> import time
>
> class IDGenerator(obj ect):
> """(serverID,su bversion_length )
> Create an ID from the server name, datetimestamp and version number.
> Calling the instance returns the ID using the __repr__ class function
>
> Example usage:[color=green][color=darkred]
> >>> id = idgen('01',4)
> >>> id[/color][/color]
> 01_20060424_151 903_1[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 905_2[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 905_3[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 906_4[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 907_1[color=green][color=darkred]
> >>>
> >>> id = idgen(04,100)
> >>> id[/color][/color]
> 4_20060424_1520 43_001
>
> """
>
> def __init__(self,s erverID,subvers ion_length):
> self.ID = str(serverID)
> self.length = int(subversion_ length)
> fill_length = len(str(self.le ngth))
>
> def fill(number):
> return(string.z fill(number,fil l_length))
> self.fill = fill
>
>
> def __repr__(self):
> # If the subversion length has been reached or the generator has not
> # been defined, (re)define it, otherwise return the next value of the
> # subversion.
> try:
> return_value = self.range_gen. next()
>
> except:
> self.range_gen = ( number for number in range(1,self.le ngth+1) )
> return_value = self.range_gen. next()
>
> # Create the version stamp.
> return_value = self.ID +\
> time.strftime(" _%Y%m%d_%H%M%S_ ",time.gmtime() )+\
> self.fill(retur n_value)
>
> # And return it.
> return(return_v alue)[/color]
----- script -----
--
mph
I created a class which creates a relative unique id string, now my
program just works fine and as expected but somehow I get the feeling
that I misused the __repr__ since I guess people expect to 'execute' a
function in an instance instead of using it's representation string of
the instance itself, could you elaborate whether you find this bad
practice and if yes what would have been a better way to do it?
TIA
----- script -----[color=blue]
> import string
> import time
>
> class IDGenerator(obj ect):
> """(serverID,su bversion_length )
> Create an ID from the server name, datetimestamp and version number.
> Calling the instance returns the ID using the __repr__ class function
>
> Example usage:[color=green][color=darkred]
> >>> id = idgen('01',4)
> >>> id[/color][/color]
> 01_20060424_151 903_1[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 905_2[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 905_3[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 906_4[color=green][color=darkred]
> >>> id[/color][/color]
> 01_20060424_151 907_1[color=green][color=darkred]
> >>>
> >>> id = idgen(04,100)
> >>> id[/color][/color]
> 4_20060424_1520 43_001
>
> """
>
> def __init__(self,s erverID,subvers ion_length):
> self.ID = str(serverID)
> self.length = int(subversion_ length)
> fill_length = len(str(self.le ngth))
>
> def fill(number):
> return(string.z fill(number,fil l_length))
> self.fill = fill
>
>
> def __repr__(self):
> # If the subversion length has been reached or the generator has not
> # been defined, (re)define it, otherwise return the next value of the
> # subversion.
> try:
> return_value = self.range_gen. next()
>
> except:
> self.range_gen = ( number for number in range(1,self.le ngth+1) )
> return_value = self.range_gen. next()
>
> # Create the version stamp.
> return_value = self.ID +\
> time.strftime(" _%Y%m%d_%H%M%S_ ",time.gmtime() )+\
> self.fill(retur n_value)
>
> # And return it.
> return(return_v alue)[/color]
----- script -----
--
mph
Comment