Arrays, Got Me Confused

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Fri Apr 13 15:14:06 CEST 2007


Thanks for that Tim and Steve, greatly appreciated.

I know that the class method is just a wrapper and it seems a little silly,
but it is partly for the exercise. I'm pretty well savvy with the OOP stuff
but its something my business partner is yet to venture into, so by working
like this it helps him understand the concepts. I also feel more at home if
i can wrap this stuff up in nice user friendly methods that I can access
universally throughout the application.

I will have plenty of other class's and methods throughout the application
that will call upon this firewall stuff, and being able to universally
reference it as Firewall.addDevice() makes things more logical in my mind, I
find trying to go back to a more procedural method pretty tricky now.

Thanks again guys,

Rob

-----Original Message-----
From: python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org
[mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org]
On Behalf Of Tim Golden
Sent: 13 April 2007 14:03
Cc: python-list at python.org
Subject: Re: Arrays, Got Me Confused

Robert Rawlins - Think Blue wrote:
> Hello Guys,
> 
> Wider fragments of code don't really exists at this moment in time :-D
this
> is just a bit of a 'tester' class for me to get used to the methods.
> Basically I'm trying to create a class that contains an array of MAC
> address, these look something like this 'FD:E4:55:00:FG:A9. I want the
class
> to have the following methods for easy manipulation of the array.
> 
> addDevice(Address) - Pass in the MAC address and have it add it to the
> array.
> 
> removeDevice(Address) - Finds a device with that address in the array and
> removes it.
> 
> isFirewalled(Address) - looks for that address in the array and returns
> true/false dependant on whether it finds it.
> 
> Clear() - empty the array of all its contents.

OK, it's easy enough to do this, but you might find
that you don't actually need to do too much of it
yourself. Python has two central datastructures
built in: lists and dictionaries. There are loads
of tutorials around (and if you join the python-tutor
list, the welcome message points you towards several)
so I won't go into the details, but it looks like
you want a dict here.

You could wrap it in a class if you wanted, but
the great thing about Python is that you don't
have to. *Very* rough example code:

<code>
macs = {} # create an empty dict

incoming_mac = 'FD:E4:55:00:FG:A9'
macs[incoming_mac] = True

example_mac = 'a:b:c:d'
if example_mac in macs:
   mac_is_firewalled = macs[example_mac]

</code>

If you really wanted to go with a class (for the exercise
or because of wider requirements) you could do something
like this:

<code>
class Firewalled:
   def __init__ (self):
     self.macs = {}

   def addDevice (address):
     self.macs[address] = True

   def removeDevice (address):
     del self.macs[address]

   def isFirewalled (address):
     return address in self.macs
     # or return self.macs.get (address, False)
</code>

but as you can see, in this form you're just wrapping
Python with Python.

> Sorry for the sloppy code so far, I'm really new to Python so it's a steep
> learning curve for me, I'm by no means a programming numpty and allot of
the
> principles are the same, but the languages I'm used to are more robust and
> vague so I don't have to define what type of data i'm storing the array
and
> things.

Ummm... in Python you don't have to define what type of
data you're storing. Welcome to the language in any case:
you'll find that people around here are generally quite
friendly but if you want, you might try the tutor list
where people are very used to dealing with newcomers:

http://mail.python.org/mailman/listinfo/tutor

TJG
-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list