【问题标题】:Save data into two separate Parse apps将数据保存到两个单独的 Parse 应用程序中
【发布时间】:2015-06-14 21:36:17
【问题描述】:

我编写了一个小型 Python Django 程序,它使用 ParsePy 解析来自 JSON API 调用的数据并将其保存到 Parse 中。

我有一个 python 文件,它收集数据并将其保存到 Parse 应用程序数据库中。 Python 文件还将一些数据传递到不同的文件中,该文件应将传递的数据保存到不同的 Parse 应用程序中。

在伪代码中:

文件1.py

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

文件2.py

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

当我单独运行文件时,代码运行良好。但是,当我通过第一个文件执行程序时,数据全部保存到第一个 Parse 应用程序数据库中,而不是第二个。

【问题讨论】:

  • 你能在register 上展示你的register 功能吗?为什么要在file1 上省钱? second one 是什么意思。这是否意味着你有两个 Parse 数据库?
  • 能否提供更多代码细节?

标签: python django parse-platform parsepy


【解决方案1】:

我认为你可以使用这样的模式:

#!/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()

只需定义你自己的 Source 和 Destination 类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多