Defining our own types?

M

Michael Yanowitz

Hello:

I know this will probably turn about to be another dumb question
and I know I have asked a few really dumb ones lately on this list,
but I am hoping otherwise this time:

suppose I type:
ip = 123.45.67.89
(no quotes)
- is it possible (maybe by catching an exception), to have this
automatically converted to an ip address and maybe have the exception
convert this into:
ip = make_ip_address (123, 45, 67, 89)

Or even better, if possible. Can I define my own type
IP = BYTE + '.' + BYTE + '.' + BYTE + '.' + BYTE
BYTE = in range(256)
and have Python over-ride its rules so that if I type in a number
followed by a dot followed by number followed by a dot followed by a
number followed by a dot and another number, it can call
make_ip_address() on the value?

Thanks in advance:
Michael Yanowitz
 
K

Kay Schluehr

Michael said:
Hello:

I know this will probably turn about to be another dumb question
and I know I have asked a few really dumb ones lately on this list,
but I am hoping otherwise this time:

suppose I type:
ip = 123.45.67.89
(no quotes)
- is it possible (maybe by catching an exception), to have this
automatically converted to an ip address and maybe have the exception
convert this into:
ip = make_ip_address (123, 45, 67, 89)

This will fail already on the parser level. The tokenizer recogizes
'123.45' as a number as well as '.67' and '.89' and can't fit them
together to something meaningfull. So you must adapt the tokenizer to
accept '123.45.67.89' as a number and handle the corresponding NUMBER
token by visiting the syntax tree later on i.e. returning either a
NUMBER or a function call.

I've written a framework that lets you do this [1] but be aware that
you actually create a new language. Personally I don't think it's worth
the effort and would define an IP object instead:

ip = IP (123, 45, 67, 89)

and/or

ip = IP ("123.45.67.89")

Regards,
Kay

[1] http://www.fiber-space.de/EasyExtend/doc/EE.html
 
T

tobiah

suppose I type:
ip = 123.45.67.89

This is probably far from what you want,
but I would do something like this:

********************************************
class ip(list):

def __init__(self, ip):

bytes = ip.split('.')
for x in range(4):
self.append(bytes[x])

def __repr__(self):

return '.'.join(self)


localhost = ip('192.168.1.1')

print localhost
print localhost[2]
*******************************************
192.168.1.1
1

That way at least you can get at the value
in both of the meaningful ways, and you can
probably think of more methods, like applying
netmasks, etc...
 
T

tobiah

suppose I type:
ip = 123.45.67.89

This is probably far from what you want,
but I would do something like this:

********************************************
class ip(list):

def __init__(self, ip):

bytes = ip.split('.')
for x in range(4):
self.append(bytes[x])

def __repr__(self):

return '.'.join(self)


localhost = ip('192.168.1.1')

print localhost
print localhost[2]
*******************************************
192.168.1.1
1

That way at least you can get at the value
in both of the meaningful ways, and you can
probably think of more methods, like applying
netmasks, etc...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top