import yaml
import sys
from tinydb.storages import Storage

class YAMLStorage(Storage):
    def __init__(self, filename):  # (1)
        self.filename = filename

    def read(self):
        with open(self.filename) as handle:
            try:
                data = yaml.safe_load(handle.read())  # (2)
                return data
            except yaml.YAMLError:
                return None  # (3)

    def write(self, data):
        with open(self.filename, 'w') as handle:
            yaml.dump(yaml.safe_load(str(data)), handle)

    def close(self):  # (4)
        pass

import yaml
import sys
from tinydb.database import Document
from tinydb.storages import Storage, touch

def represent_doc(dumper, data):
    # Represent `Document` objects as their dict's string representation
    # which PyYAML understands
    return dumper.represent_data(dict(data))

yaml.add_representer(Document, represent_doc)

class YAMLStorage(Storage):
    def __init__(self, filename):
        self.filename = filename
        touch(filename, False)

    def read(self):
        with open(self.filename) as handle:
            data = yaml.safe_load(handle.read())
            return data

    def write(self, data):
        with open(self.filename, 'w') as handle:
            yaml.dump(data, handle)

    def close(self):
        pass

Fix YAMLStorage as per TinyDB doc changes
Fix YAMLStorage as per TinyDB doc changes

  import yaml
  import sys
+ from tinydb.database import Document
- from tinydb.storages import Storage
+ from tinydb.storages import Storage, touch
+ 
+ def represent_doc(dumper, data):
+     # Represent `Document` objects as their dict's string representation
+     # which PyYAML understands
+     return dumper.represent_data(dict(data))
+ 
+ yaml.add_representer(Document, represent_doc)
  
  class YAMLStorage(Storage):
-     def __init__(self, filename):  # (1)
+     def __init__(self, filename):
          self.filename = filename
+         touch(filename, False)
  
      def read(self):
          with open(self.filename) as handle:
-             try:
-                 data = yaml.safe_load(handle.read())  # (2)
+             data = yaml.safe_load(handle.read())
-                 return data
+             return data
-             except yaml.YAMLError:
-                 return None  # (3)
  
      def write(self, data):
          with open(self.filename, 'w') as handle:
-             yaml.dump(yaml.safe_load(str(data)), handle)
+             yaml.dump(data, handle)
  
-     def close(self):  # (4)
+     def close(self):
          pass
  
Fix YAMLStorage as per TinyDB doc changes
## Code Before:
import yaml
import sys
from tinydb.storages import Storage

class YAMLStorage(Storage):
    def __init__(self, filename):  # (1)
        self.filename = filename

    def read(self):
        with open(self.filename) as handle:
            try:
                data = yaml.safe_load(handle.read())  # (2)
                return data
            except yaml.YAMLError:
                return None  # (3)

    def write(self, data):
        with open(self.filename, 'w') as handle:
            yaml.dump(yaml.safe_load(str(data)), handle)

    def close(self):  # (4)
        pass

## Instruction:
Fix YAMLStorage as per TinyDB doc changes
## Code After:
import yaml
import sys
from tinydb.database import Document
from tinydb.storages import Storage, touch

def represent_doc(dumper, data):
    # Represent `Document` objects as their dict's string representation
    # which PyYAML understands
    return dumper.represent_data(dict(data))

yaml.add_representer(Document, represent_doc)

class YAMLStorage(Storage):
    def __init__(self, filename):
        self.filename = filename
        touch(filename, False)

    def read(self):
        with open(self.filename) as handle:
            data = yaml.safe_load(handle.read())
            return data

    def write(self, data):
        with open(self.filename, 'w') as handle:
            yaml.dump(data, handle)

    def close(self):
        pass
