473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Defining our own types?

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
Aug 16 '06 #1
3 1315
Michael Yanowitz wrote:
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

Aug 16 '06 #2
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...
--
Posted via a free Usenet account from http://www.teranews.com

Aug 17 '06 #3
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...
Aug 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to...
7
by: Senthilraja | last post by:
I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions. #include <iostream>...
12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
8
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
10
by: Joe Laughlin | last post by:
I'm sure there's a fairly easy answer for this... but how can I define a new type with range checking? Example: I want to define a new type that's like a double, except that you can only give...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
2
by: lcaamano | last post by:
We have a tracing decorator that automatically logs enter/exits to/from functions and methods and it also figures out by itself the function call arguments values and the class or module the...
2
by: Martin Z | last post by:
Hi, I'm trying to support a 3rd-party XML format for which there is no schema. As such, I've been making my objects that map to their format using the XMLSerializer... but there's one object...
8
by: lubomir dobsik | last post by:
hi, i have seen an interesting thing: #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) typedef unsigned int size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long) typedef...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.