Searching for some kind of data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giampaolo Rodola'

    Searching for some kind of data type

    Hi,
    for an FTP server I wrote I'd need to group the FTP commands in one
    table that defines the command itself, the syntax string, required
    permission, whether it requires authorization, whether it takes
    argument and whether there's a need to validate the path from the
    argument.
    The more obvious way I found to do that is something like this:

    class CommandProperty :
    def __init__(self, perm, auth_needed, arg_needed, check_path,
    syntax):
    self.perm = perm
    self.auth_neede d = auth_needed
    self.arg_needed = arg_needed
    self.check_path = check_path
    self.syntax = syntax

    ftp_cmds = {
    "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
    arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
    "APPE" : CommandProperty (perm='a', auth_needed=Tru e,
    arg_needed=True , check_path=True , syntax="APPE <SPfile-name
    (append data to an existent file)."),
    "CDUP" : CommandProperty (perm='e',
    auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP (go
    to parentdirectory )."),
    ...
    ...
    ...
    }

    ....but I find it somewhat redundant and... "ugly".
    I was wondering if there was some kind of data type which could better
    fit such purpose or if someone could suggest me some other approach to
    do this same thing. Maybe using a dictionary is not the better choice
    here.


    Thanks in advance


    --- Giampaolo

  • Larry Bates

    #2
    Re: Searching for some kind of data type

    Giampaolo Rodola' wrote:
    Hi,
    for an FTP server I wrote I'd need to group the FTP commands in one
    table that defines the command itself, the syntax string, required
    permission, whether it requires authorization, whether it takes
    argument and whether there's a need to validate the path from the
    argument.
    The more obvious way I found to do that is something like this:
    >
    class CommandProperty :
    def __init__(self, perm, auth_needed, arg_needed, check_path,
    syntax):
    self.perm = perm
    self.auth_neede d = auth_needed
    self.arg_needed = arg_needed
    self.check_path = check_path
    self.syntax = syntax
    >
    ftp_cmds = {
    "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
    arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
    "APPE" : CommandProperty (perm='a', auth_needed=Tru e,
    arg_needed=True , check_path=True , syntax="APPE <SPfile-name
    (append data to an existent file)."),
    "CDUP" : CommandProperty (perm='e',
    auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP (go
    to parentdirectory )."),
    ...
    ...
    ...
    }
    >
    ...but I find it somewhat redundant and... "ugly".
    I was wondering if there was some kind of data type which could better
    fit such purpose or if someone could suggest me some other approach to
    do this same thing. Maybe using a dictionary is not the better choice
    here.
    >
    >
    Thanks in advance
    >
    >
    --- Giampaolo
    http://code.google.com/p/pyftpdlib/

    Seems completely reasonable to me. You might just consider using keyword
    arguments in the __init__ method and eliminate the dictionary altogether.

    Not tested, but you will get the idea:

    class CommandProperty :
    def __init__(self, perm = perm, auth_needed = True, arg_needed = True,
    check_path = False, syntax = syntax):

    self.perm = perm
    self.auth_neede d = auth_needed
    self.arg_needed = arg_needed
    self.check_path = check_path
    self.syntax = syntax

    ftpCommands = dict(
    ABOR = CommandProperty (perm = None, arg_needed = False,
    syntax="ABOR (abort transfer)."),
    APPE = CommandProperty (perm = 'a', check_path=True ,
    syntax = "APPE <SPfile-name (append data to" \
    "an existent file)."),
    CDUP = CommandProperty (perm= 'e', arg_needed = False,
    syntax="CDUP (goto parentdirectory )."),
    ....
    ....
    ....
    )


    IMHO this is a "little" easier to manage because you can take advantage of the
    default values for keyword arguments to eliminate some of the arguments.

    Hope this helps,
    Larry

    Comment

    • Gary Herron

      #3
      Re: Searching for some kind of data type

      Larry Bates wrote:
      Giampaolo Rodola' wrote:
      >Hi,
      >for an FTP server I wrote I'd need to group the FTP commands in one
      >table that defines the command itself, the syntax string, required
      >permission, whether it requires authorization, whether it takes
      >argument and whether there's a need to validate the path from the
      >argument.
      >The more obvious way I found to do that is something like this:
      >>
      >class CommandProperty :
      > def __init__(self, perm, auth_needed, arg_needed, check_path,
      >syntax):
      > self.perm = perm
      > self.auth_neede d = auth_needed
      > self.arg_needed = arg_needed
      > self.check_path = check_path
      > self.syntax = syntax
      >>
      >ftp_cmds = {
      > "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
      >arg_needed=Fal se, check_path=Fals e, syntax="ABOR (abort transfer)."),
      > "APPE" : CommandProperty (perm='a', auth_needed=Tru e,
      >arg_needed=Tru e, check_path=True , syntax="APPE <SPfile-name
      >(append data to an existent file)."),
      > "CDUP" : CommandProperty (perm='e',
      >auth_needed=Tr ue,arg_needed=F alse, check_path=Fals e, syntax="CDUP (go
      >to parentdirectory )."),
      > ...
      > ...
      > ...
      > }
      >>
      >...but I find it somewhat redundant and... "ugly".
      >I was wondering if there was some kind of data type which could better
      >fit such purpose or if someone could suggest me some other approach to
      >do this same thing. Maybe using a dictionary is not the better choice
      >here.
      >>
      >>
      >Thanks in advance
      >>
      >>
      >--- Giampaolo
      >http://code.google.com/p/pyftpdlib/
      >
      >
      Seems completely reasonable to me. You might just consider using
      keyword arguments in the __init__ method and eliminate the dictionary
      altogether.
      >
      Not tested, but you will get the idea:
      >
      class CommandProperty :
      def __init__(self, perm = perm, auth_needed = True, arg_needed =
      True,
      check_path = False, syntax = syntax):
      >
      self.perm = perm
      self.auth_neede d = auth_needed
      self.arg_needed = arg_needed
      self.check_path = check_path
      self.syntax = syntax
      >
      ftpCommands = dict(
      ABOR = CommandProperty (perm = None, arg_needed = False,
      syntax="ABOR (abort transfer)."),
      APPE = CommandProperty (perm = 'a', check_path=True ,
      syntax = "APPE <SPfile-name (append data to" \
      "an existent file)."),
      CDUP = CommandProperty (perm= 'e', arg_needed = False,
      syntax="CDUP (goto parentdirectory )."),
      ...
      ...
      ...
      )
      How does this strike you? With care, the comment and the table could
      be kept aligned and nicely readable

      cmd_data = dict(
      # cmd = (perm, auth, arg, path, syntax),
      ABOR = (None, False, False, False, "ABOR (abort transfer)."),
      APPE = (None, False, False, True, "APPE <SPfile-name (append data to"),
      ...
      ]

      ftpCommands = {}
      for cmd,args in cmd_data.iterit ems():
      ftpCommands[cmd] = CommandProperty (*args)

      Gary Herron


      >
      >
      IMHO this is a "little" easier to manage because you can take
      advantage of the default values for keyword arguments to eliminate
      some of the arguments.
      >
      Hope this helps,
      Larry
      --
      http://mail.python.org/mailman/listinfo/python-list

      Comment

      • Giampaolo Rodola'

        #4
        Re: Searching for some kind of data type

        On 2 Ago, 18:18, Gary Herron <gher...@island training.comwro te:
        Larry Bates wrote:
        Giampaolo Rodola' wrote:
        Hi,
        for an FTP server I wrote I'd need to group the FTP commands in one
        table that defines the command itself, the syntax string, required
        permission, whether it requires authorization, whether it takes
        argument and whether there's a need to validate the path from the
        argument.
        The more obvious way I found to do that is something like this:
        >
        class CommandProperty :
            def __init__(self, perm, auth_needed, arg_needed, check_path,
        syntax):
                self.perm = perm
                self.auth_neede d = auth_needed
                self.arg_needed = arg_needed
                self.check_path = check_path
                self.syntax = syntax
        >
        ftp_cmds = {
            "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
        arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
            "APPE" : CommandProperty (perm='a',  auth_needed=Tru e,
        arg_needed=True ,  check_path=True ,  syntax="APPE <SPfile-name
        (append data to an existent file)."),
            "CDUP" : CommandProperty (perm='e',
        auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP (go
        to parentdirectory )."),
            ...
            ...
            ...
            }
        >
        ...but I find it somewhat redundant and... "ugly".
        I was wondering if there was some kind of data type which could better
        fit such purpose or if someone could suggest me some other approach to
        do this same thing. Maybe using a dictionary is not the better choice
        here.
        >
        Thanks in advance
        >>
        Seems completely reasonable to me.  You might just consider using
        keyword arguments in the __init__ method and eliminate the dictionary
        altogether.
        >
        Not tested, but you will get the idea:
        >
        class CommandProperty :
            def __init__(self, perm = perm, auth_needed = True, arg_needed =
        True,
                         check_path = False, syntax = syntax):
        >
                self.perm = perm
                self.auth_neede d = auth_needed
                self.arg_needed = arg_needed
                self.check_path = check_path
                self.syntax = syntax
        >
        ftpCommands = dict(
            ABOR = CommandProperty (perm = None, arg_needed = False,
                     syntax="ABOR (abort transfer)."),
            APPE = CommandProperty (perm = 'a',  check_path=True ,
                     syntax = "APPE <SPfile-name (append datato" \
                              "an existent file)."),
            CDUP = CommandProperty (perm= 'e', arg_needed = False,
                     syntax="CDUP (goto parentdirectory )."),
        ...
        ...
        ...
            )
        >
        How does this strike you?    With care, the comment and the table could
        be kept aligned and nicely readable
        >
        cmd_data = dict(
         # cmd = (perm, auth,   arg, path,   syntax),
         ABOR = (None, False, False, False, "ABOR (abort transfer)."),
         APPE  = (None, False, False, True,  "APPE <SPfile-name (appenddata to"),
         ...
        ]
        >
        ftpCommands = {}
        for cmd,args in cmd_data.iterit ems():
            ftpCommands[cmd] = CommandProperty (*args)
        >
        Gary Herron
        >
        >
        >
        >
        >
        IMHO this is a "little" easier to manage because you can take
        advantage of the default values for keyword arguments to eliminate
        some of the arguments.
        >
        Hope this helps,
        Larry
        --
        http://mail.python.org/mailman/listinfo/python-list- Nascondi testo citato
        >
        - Mostra testo citato -- Nascondi testo citato
        >
        - Mostra testo citato -
        Thanks, I didnt' know dict() could be used with =.
        I think I'm going to use this solution.


        --- Giampaolo

        Comment

        • Paul Hankin

          #5
          Re: Searching for some kind of data type

          On Aug 2, 1:12 pm, "Giampaolo Rodola'" <gne...@gmail.c omwrote:
          Hi,
          for an FTP server I wrote I'd need to group the FTP commands in one
          table that defines the command itself, the syntax string, required
          permission, whether it requires authorization, whether it takes
          argument and whether there's a need to validate the path from the
          argument.
          The more obvious way I found to do that is something like this:
          >
          class CommandProperty :
              def __init__(self, perm, auth_needed, arg_needed, check_path,
          syntax):
                  self.perm = perm
                  self.auth_neede d = auth_needed
                  self.arg_needed = arg_needed
                  self.check_path = check_path
                  self.syntax = syntax
          >
          ftp_cmds = {
              "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
          arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
              "APPE" : CommandProperty (perm='a',  auth_needed=Tru e,
          arg_needed=True ,  check_path=True ,  syntax="APPE <SPfile-name
          (append data to an existent file)."),
              "CDUP" : CommandProperty (perm='e',
          auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP(go
          to parentdirectory )."),
              ...
              ...
              ...
              }
          >
          ...but I find it somewhat redundant and... "ugly".
          I was wondering if there was some kind of data type which could better
          fit such purpose or if someone could suggest me some other approach to
          do this same thing. Maybe using a dictionary is not the better choice
          here.
          How about something like this? The idea is to summarize the command
          table, then construct the ftp command dictionary programatically from
          it.

          cmd_table = dict(
          ABOR='-AR- (abort transfer)',
          APPE='aARP <SPfile-name (append data to a file)',
          CDUP='eA-- (go to parent directory)',
          ...
          )

          The first block of four characters give the options: permission,
          whether authentication is needed, whether the argument is required,
          and whether the path needs checking. Permission is the permission
          letter or - (for none), A or - for authentication, R or - for argument
          required, and - or P for path required. Then the help text follows
          (with the command name omitted since it's redundant).

          Turning these into CommandProperty 's is straightforward (untested
          code):

          ftp_commands = {}
          for cmd, descriptor in cmd_table.iteri tems():
          options, help = descriptor.spli t(' ', 1)
          options = [None if opt == '-' else opt for opt in options]
          perm, auth, arg, path = options
          assert perm is None or perm in 'ae...'
          assert auth is None or auth == 'A'
          assert arg is None or arg == 'R'
          assert path is None or path == 'P'
          ftp_commands[cmd] = CommandProperty (perm=perm, auth_required=a uth,
          arg_required=ar g, check_path=path , syntax = '%s %s' % (cmd, help))

          --
          Paul Hankin

          Comment

          Working...