import pickle


class PersonDb(object):
    def __init__(self, dbName, autoload = True):
        self.dbName = dbName
        self.db = None
        if autoload:
            self.setup()

    def setup(self):
        self.db = PersonDb.load(self.dbName)
        self.getGroups()

    def getGroups(self):
        tmp = set()
        for person in self.db:
            for group in person.group:
                tmp.append(person)
        self.groups = tmp

    def save(data, fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        with open(filen,"wb") as pickleOut:
            pickle.dump(data, pickleOut)

    def load(fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        with open(filen, "rb") as pickleIn:
            data = pickle.load(pickleIn)
        return data

    def fileExtension(fileName):
        result = fileName.strip()
        if '.json' not in result:
            result += '.json'
        return result

import pickle
import os


class PersonDb(object):
    def __init__(self, dbName, autoload = True):
        self.dbName = dbName
        self.db = None
        if autoload:
            self.setup()

    def setup(self):
        self.db = PersonDb.load(self.dbName)
        self.getGroups()

    def getGroups(self):
        tmp = set()
        for person in self.db:
            for group in person.group:
                tmp.append(person)
        self.groups = tmp

    def save(data, fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        with open(filen,"wb") as pickleOut:
            pickle.dump(data, pickleOut)

    def load(fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        if os.path.exists(filen):
            with open(filen, "rb") as pickleIn:
                data = pickle.load(pickleIn)
            return data
        else:
            return []

    def fileExtension(fileName):
        result = fileName.strip()
        if '.json' not in result:
            result += '.json'
        return result

Add handling of not present file.
Add handling of not present file.

Signed-off-by: Matej Dujava <03ce64f61b3ea1fda633fb2a103b989e3272d16b@gmail.com>

  import pickle
+ import os
  
  
  class PersonDb(object):
      def __init__(self, dbName, autoload = True):
          self.dbName = dbName
          self.db = None
          if autoload:
              self.setup()
  
      def setup(self):
          self.db = PersonDb.load(self.dbName)
          self.getGroups()
  
      def getGroups(self):
          tmp = set()
          for person in self.db:
              for group in person.group:
                  tmp.append(person)
          self.groups = tmp
  
      def save(data, fileName = 'database'):
          filen = PersonDb.fileExtension(fileName)
          with open(filen,"wb") as pickleOut:
              pickle.dump(data, pickleOut)
  
      def load(fileName = 'database'):
          filen = PersonDb.fileExtension(fileName)
+         if os.path.exists(filen):
-         with open(filen, "rb") as pickleIn:
+             with open(filen, "rb") as pickleIn:
-             data = pickle.load(pickleIn)
+                 data = pickle.load(pickleIn)
-         return data
+             return data
+         else:
+             return []
  
      def fileExtension(fileName):
          result = fileName.strip()
          if '.json' not in result:
              result += '.json'
          return result
  
Add handling of not present file.
## Code Before:
import pickle


class PersonDb(object):
    def __init__(self, dbName, autoload = True):
        self.dbName = dbName
        self.db = None
        if autoload:
            self.setup()

    def setup(self):
        self.db = PersonDb.load(self.dbName)
        self.getGroups()

    def getGroups(self):
        tmp = set()
        for person in self.db:
            for group in person.group:
                tmp.append(person)
        self.groups = tmp

    def save(data, fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        with open(filen,"wb") as pickleOut:
            pickle.dump(data, pickleOut)

    def load(fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        with open(filen, "rb") as pickleIn:
            data = pickle.load(pickleIn)
        return data

    def fileExtension(fileName):
        result = fileName.strip()
        if '.json' not in result:
            result += '.json'
        return result

## Instruction:
Add handling of not present file.
## Code After:
import pickle
import os


class PersonDb(object):
    def __init__(self, dbName, autoload = True):
        self.dbName = dbName
        self.db = None
        if autoload:
            self.setup()

    def setup(self):
        self.db = PersonDb.load(self.dbName)
        self.getGroups()

    def getGroups(self):
        tmp = set()
        for person in self.db:
            for group in person.group:
                tmp.append(person)
        self.groups = tmp

    def save(data, fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        with open(filen,"wb") as pickleOut:
            pickle.dump(data, pickleOut)

    def load(fileName = 'database'):
        filen = PersonDb.fileExtension(fileName)
        if os.path.exists(filen):
            with open(filen, "rb") as pickleIn:
                data = pickle.load(pickleIn)
            return data
        else:
            return []

    def fileExtension(fileName):
        result = fileName.strip()
        if '.json' not in result:
            result += '.json'
        return result
