for a 'good python'
jak
nospam at please.ty
Wed Apr 12 08:50:29 EDT 2023
Stefan Ram ha scritto:
> jak <nospam at please.ty> writes:
>> @property
>> def subnet(self):
>> return self.__to_str(self.__tsubnet)
>
> Maybe each of those attributes should be an object of a
> special class where your "__to_str" is "__str__"? E.g.,
>
> # code in "calcip.__init__"
> self.tsubnet = ip_address_class.from_int( subnet )
>
> where "ip_address_class" is as in:
>
> import collections
> import random
>
> class ip_address_class( collections.UserList ):
> def __init__( self, bytes_address ):
> super().__init__( bytes_address )
> @classmethod
> def from_int( cls, int_address ):
> return cls( int_address.to_bytes( 4, 'big' ))
> def __str__( self ):
> return '.'.join( str( byte_ )for byte_ in self.data )
>
> if __name__ == '__main__':
> ip_address = \
> ip_address_class.from_int( random.randint( 0, 4294967295 ))
> print( ip_address[ 0 ])
> print( ip_address )
>
> . Now the client can access each byte individually and also
> get a "nice" string representation.
>
> (You may add more "from_..." methods for all the various
> formats you create addresses from.)
>
> But you should also research the standard library to see
> if something like this doesn't already exist ready-made
> specifically for IP addresses in the standard library.
>
>
ok. thanks a lot. now i try to do that.
More information about the Python-list
mailing list