Python Forum

Full Version: Best construct? Array, class, other?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So as I recently modified my script to now take input from Vendor-A (asa) to Vendor-C (palo alto), I'm seeing that I may be better off using a CLASS.

The issue is that any _one security policy_ could have
   1) multiple protocols (IP/TCP/UDP/other)
   2) one or multiple source addresses (or a group of addresses)
   3) one or multiple destination addresses (or a group of addresses)
   4) one or multiple service ports
   5) a bunch of binary options (log/no-log, active/inactive, etc)

I've been RTFMing (https://python-forum.io/Thread-Class-Basics for example) but what's not clear is how do I handle the multiples?  "Class Examples" often use a person, who does not have multiple attributes...(sure, maiden name, but that's typically a one-off and it's not last 4 maiden names).

So is it an array inside a class?  I'm not sure what's the best method when (for example) the firewall service port could be one port (www) or 12 ports (80, 443, 3389, 3306, etc).

Thoughts, pointers, links

many thanks!
PappaBear
A class is just a container.  Like a dict that can have methods.  Anything you can do with a class, you can do without one.

It sounds like lists are what you're looking for.

import enum

class Protocol(enum.Enum):
   TCP = enum.auto()
   UDP = enum.auto()

class Policy:
   def __init__(self, protocols, sources, destinations, ports, use_log=False, is_active=False):
       self.protocols  = protocols
       self.sources = sources
       self.destinations = destinations
       self.ports = ports
       self.use_log = use_log
       self.is_active = is_active
   
policy = Policy([Protocol.TCP, Protocol.UDP], ["localhost"], ["some_other_ip"], [80, 443])