RE: Arrays, Got Me Confused



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@xxxxxxxxxx
[mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk@xxxxxxxxxx]
On Behalf Of Tim Golden
Sent: 13 April 2007 14:03
Cc: python-list@xxxxxxxxxx
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

.



Relevant Pages

  • Re: Arrays, Got Me Confused
    ... Wider fragments of code don't really exists at this moment in time:-D this ... Basically I'm trying to create a class that contains an array of MAC ... Python has two central datastructures ...
    (comp.lang.python)
  • (patch for Bash) regex(3) splitting/matching
    ... I usually do this in Python. ... 'help array' will give you more info on other options for 'array' ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Why C Is Not My Favourite Programming Language
    ... And the number of modules in Python 2.4's Global Module Index is 362. ... The PDP architecture ideals ... fflushcan't be used to flush the contents of standard ... But it's not foolish that in ksh if you refer to an array name ...
    (comp.lang.c)
  • Re: Allowing zero-dimensional subscripts
    ... The items of a tuple are arbitrary Python objects. ... items are formed by comma-separated lists of expressions. ... the grammar rule used for list subscript is different from the ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)
  • Re: Brain going crazy with recursive functions
    ... I want eventually to port this to scheme, but I know python ... I'm in linear_search for the array, I also call linear_search for each ... return linear_search_iter(0, truth_func, array, acc) ...
    (comp.lang.python)