Python Forum
Best construct? Array, class, other?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best construct? Array, class, other?
#1
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
Reply
#2
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])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the if construct not calculating correctly? egemynet 21 3,882 Mar-21-2022, 01:20 PM
Last Post: deanhystad
  Building command in a looping construct DennisT 3 1,883 Sep-08-2020, 06:32 PM
Last Post: DennisT
  class random var write to array storzo 5 2,902 Aug-02-2019, 03:26 PM
Last Post: storzo
  Multi-Dimm Array or Class? PappaBear 2 2,264 May-02-2019, 07:04 PM
Last Post: PappaBear
  shortening an elif construct Skaperen 10 5,434 Jul-24-2018, 07:06 AM
Last Post: Skaperen
  What work faster and take less memory array or class? Kamilbek 1 3,134 Apr-20-2017, 05:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020