4

I wrote a small Python Django program that parses data from a JSON API call and saves it into Parse, using ParsePy.

I have a python file that collects the data and saves it into a Parse app DB. The Python file also passes some data into a different file that should save the passed data into a different Parse app.

In pseudocode:

File1.py

register('key1', 'restKey1')
file2.class1(passedData)
file1.saveData

File2.py

register('key2','restKey2')
file2.saveData

When I run the files individually, the code works perfectly. However, when I execute the program through the first file, the data is all getting saved into the first Parse app database instead of the second one.

Prajoth
  • 900
  • 3
  • 12
  • 26
  • 1
    Can you show your `register` function and on your `File1.py`, why you save on `file1`? And what do you mean by `second one`. Does it mean you have two Parse database? – Edwin Lunando Jun 23 '15 at 02:21
  • 1
    Could you please provide more detail of your code? – wherby Jun 23 '15 at 06:08

1 Answers1

1

I think you can use pattern like this:

#!/usr/bin/python

class SourceInterface(object):

    def get_data(self):
        raise NotImplementedError("Subclasses should implement this!")


class DestinationInterface(object):

    def put_data(self, data):
        raise NotImplementedError("Subclasses should implement this!")


class FileSource(SourceInterface):

    def __init__(self, filename):
        self.filename = filename

    def get_data(self):
        lines = None
        with open(self.filename, 'r') as f:
            lines = f.readlines()
        if lines:
            with open(self.filename, 'w') as f:
                if lines[1:]:
                    f.writelines(lines[1:])
            return lines[0]


class FileDestination(DestinationInterface):

    def __init__(self, filename):
        self.filename = filename

    def put_data(self, data):
        print 'put data', data
        with open(self.filename, 'a+') as f:
            f.write(data)


class DataProcessor(object):
    sources_list = []
    destinitions_list = []

    def register_source(self, source):
        self.sources_list.append(source)

    def register_destinition(self, destinition):
        self.destinitions_list.append(destinition)

    def process(self):
        for source in self.sources_list:
            data = source.get_data()
            if data:
                for destinition in self.destinitions_list:
                    destinition.put_data(data)

if __name__ == '__main__':
    processor = DataProcessor()
    processor.register_source(FileSource('/tmp/source1.txt'))
    processor.register_source(FileSource('/tmp/source2.txt'))
    processor.register_destinition(FileDestination('/tmp/destinition1.txt'))
    processor.register_destinition(FileDestination('/tmp/destinition2.txt'))
    processor.process()

Just define you own Source and Destination classes

Yevgeniy Shchemelev
  • 3,601
  • 2
  • 32
  • 39