APIのXMLレスポンスを構築するライブラリを構築しようとしています。私の問題を説明するのを助けるために、2つのサンプルAPIの応答があります。最初はメニューを表示し、2番目はテキストを表示します。Python APIラッパーデザインパターン
<CiscoIPPhoneMenu>
<Title>Title text goes here</Title>
<Prompt>Prompt text goes here</Prompt>
<MenuItem>
<Name>The name of each menu item</Name>
<URL>The URL associated with the menu item</URL>
</MenuItem>
<SoftKeyItem>
<Name>Name of soft key</Name>
<URL>URL or URI of soft key</URL>
<Position>Position information of the soft key</Position>
</SoftKeyItem>
</CiscoIPPhoneMenu>
...
<CiscoIPPhoneText>
<Title>Title text goes here</Title>
<Prompt>The prompt text goes here</Prompt>
<Text>The text to be displayed as the message body goes here</Text>
<SoftKeyItem>
<Name>Name of soft key</Name>
<URL>URL or URI of soft key</URL>
<Position>Position information of the soft key</Position>
<SoftKeyItem>
</CiscoIPPhoneText>
さて、私のモジュールの概要については、次のようになりますので:
class CiscoIPPhone(object):
def __init__(self, title=None, prompt=None):
self.title = title
self.prompt = prompt
class MenuItem(object):
def __init__(self, name, url):
self.name = name
self.url = url
class CiscoIPPhoneMenu(CiscoIPPhone):
def __init__(self, *args, **kwargs):
super(CiscoIPPhoneMenu, self).__init__(*args, **kwargs)
self.items = []
def add_menu(self, name, url):
self.items.append(MenuItem(name, url))
注:読みやすくするために、私はこれらのクラスを扱う検証とサニタイズを削除しました。
だから、私の質問は以下のとおりです。
- 私は、それをこの見なさ間違っていたり、悪い習慣を事実上は、これらのオブジェクトの直列化表現を出力やっているんですか?
- この種のAPIインターフェイスクラスを記述するデザインパターンはありますか?
- 同様のことをするエレガントなPythonライブラリがありますか? (私はDjangoのモデルシリアライゼーション、またはDjango-Tastypieの削除版のように思っています)。