How can I make these functions work following OOP Principles?

I have several functions/methods in a class that are kind of connected. I am building a class that mimics terminal commands and links. However, someone told me this is not proper OOP. How can I separate these methods to work independently. Methods shouldn't call other methods. Correct?

class directory:
  #FILES, LINKS AND DIRECTORIES
  current_path = []
  hold_files = [
    'test1.txt', 'test2.py',
    {'/desktop': ['computer.txt','tfile.doc',
      {'/peace':{
        '/pictures': [
        'hello.gif',
        'smile.gif',
        'run.gif'
        ]},
    '/work':[
    'file1.txt',
    'file2.txt'
    ]
      }]
    }
  ]

#recursively delete folder (if dot in)
  def delete(itself):
    #if dictionary, call self, else delete
    del itself
    return


      
  ## HELPER METHODS

  # Join list together to produce new link, basically return the added folder to the link
  def concatenate(self):
      new_link ="".join(current_path)
      return new_link


#strip slashes and place in list 
  def adjust_link(self, paths):
      new_string = ""

      # shorten link, if someone uses cd .., basically go back to previous folder
      if paths == "cd ..":
          current_path.pop()

      #extend link, if someone is cding into another folder, remove /'s and append to separate list 
      elif "cd " in paths:
          paths = paths[3:]
          for slash in paths:
              if slash == "/":
                  current.append(new_string)
                  new_string = ""
              else:
                  new_string+=slash
      # This shouldn't be here as OOP must be separated but this calls the other function to concatenate a new link
      stripped = concatenate()
      return stripped


#returns link
  def link(self, paths):
    address_location = adjust_link(paths)
    return address_location



directory.link("cd desktop/peace")
directory.link("cd pictures")
directory.link("cd ..")
directory.delete()

Thank you.

*Also, this is not a refactoring question. I already asked on stack exchange code review and they told me to come here. Code does not work.

Edit 2: why won't "directory.link()" work?

1 answer

  • answered 2020-08-11 01:05 RufusVS

    "I have a program here and I want to convert it to OOP" is not usually how it's done: "I have a problem I want to solve using OOP" is usually the approach. It looks like you are creating something that will traverse an internal directory structure. So a skeleton might look like:

    class DirectoryTraverser:
    
         def __init__(self, directory_tree):
             self.hold_files = directory_tree
             self.current_path = []
    
         def... #  all your other functions
    
    # then to use it might look like:
    
    # create a directory traversal object with directory tree
    
    dt = DirectoryTraverser(hold_files)  
    
    dt.link("cd desktop/peace")
    dt.link("cd pictures")
    dt.link("cd ..")
    dt.delete()