axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change
on this file since f52f5df was
1fda364,
checked in by Ted Faber <faber@…>, 15 years ago
|
missed these?!
|
-
Property mode set to
100644
|
File size:
1.9 KB
|
Line | |
---|
1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | class ip_addr: |
---|
4 | def __init__(self, x): |
---|
5 | if isinstance(x, ip_addr): |
---|
6 | self.ival = x.ival |
---|
7 | elif isinstance(x, int): |
---|
8 | self.ival = x |
---|
9 | elif isinstance(x, str): |
---|
10 | self.ival = 0 |
---|
11 | sh = 24 |
---|
12 | bytes = x.split(".", 4) |
---|
13 | for b in bytes: |
---|
14 | self.ival |= (int(b) << sh) |
---|
15 | sh -= 8 |
---|
16 | else: |
---|
17 | self.ival = 0 |
---|
18 | |
---|
19 | def __str__(self): |
---|
20 | return "%d.%d.%d.%d" % ((self.ival & 0xff000000) >> 24, |
---|
21 | (self.ival & 0x00ff0000) >> 16, |
---|
22 | (self.ival & 0x0000ff00) >> 8, |
---|
23 | self.ival & 0x000000ff) |
---|
24 | |
---|
25 | def __add__(self, i): |
---|
26 | if isinstance(i, int): return ip_addr(self.ival + i) |
---|
27 | elif isinstance(i, ip_addr): return ip_addr(self.ival + i.ival) |
---|
28 | else: raise ValueError("IP addrs must operate on IP addrs or ints") |
---|
29 | |
---|
30 | def __sub__(self, i): |
---|
31 | if isinstance(i, int): return ip_addr(self.ival - i) |
---|
32 | elif isinstance(i, ip_addr): return ip_addr(self.ival - i.ival) |
---|
33 | else: raise ValueError("IP addrs must operate on IP addrs or ints") |
---|
34 | |
---|
35 | def __and__(self, i): |
---|
36 | if isinstance(i, int): return ip_addr(self.ival & i) |
---|
37 | elif isinstance(i, ip_addr): return ip_addr(self.ival & i.ival) |
---|
38 | else: raise ValueError("IP addrs must operate on IP addrs or ints") |
---|
39 | |
---|
40 | def __or__(self, i): |
---|
41 | if isinstance(i, int): return ip_addr(self.ival | i) |
---|
42 | elif isinstance(i, ip_addr): return ip_addr(self.ival | i.ival) |
---|
43 | else: raise ValueError("IP addrs must operate on IP addrs or ints") |
---|
44 | |
---|
45 | def __xor__(self, i): |
---|
46 | if isinstance(i, int): return ip_addr(self.ival ^ i) |
---|
47 | elif isinstance(i, ip_addr): return ip_addr(self.ival ^ i.ival) |
---|
48 | else: raise ValueError("IP addrs must operate on IP addrs or ints") |
---|
49 | |
---|
50 | def __radd__(self, i): |
---|
51 | return self.__add__(i) |
---|
52 | |
---|
53 | def __rsub__(self, i): |
---|
54 | return self.__sub__(i) |
---|
55 | |
---|
56 | def __rand__(self, i): |
---|
57 | return self.__and__(i) |
---|
58 | |
---|
59 | def __ror__(self, i): |
---|
60 | return self.__or__(i) |
---|
61 | |
---|
62 | def __rxor__(self, i): |
---|
63 | return self.__xor__(i) |
---|
64 | |
---|
65 | def __int__(self): |
---|
66 | return self.ival |
---|
67 | |
---|
68 | def __oct__(self): |
---|
69 | return oct(self.ival) |
---|
70 | |
---|
71 | def __hex__(self): |
---|
72 | return hex(self.ival) |
---|
Note: See
TracBrowser
for help on using the repository browser.