|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +''' |
| 4 | +© 2012-2013 eBay Software Foundation |
| 5 | +Authored by: Tim Keefer |
| 6 | +Licensed under CDDL 1.0 |
| 7 | +''' |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import ujson |
| 11 | +import json |
| 12 | + |
| 13 | +sys.path.insert(0, '%s/../' % os.path.dirname(__file__)) |
| 14 | + |
| 15 | +from ebaysdk.utils import dict2xml |
| 16 | +from ebaysdk.response import Response, ResponseDataObject |
| 17 | + |
| 18 | +def tojson(): |
| 19 | + sample_dict = { |
| 20 | + 'searchFilter': {'categoryId': {'#text': 222, '@attrs': {'site': 'US'}}}, |
| 21 | + 'paginationInput': { |
| 22 | + 'pageNumber': '1', |
| 23 | + 'pageSize': '25' |
| 24 | + }, |
| 25 | + 'itemFilter': [ |
| 26 | + {'name': 'Condition', |
| 27 | + 'value': 'Used'}, |
| 28 | + {'name': 'LocatedIn', |
| 29 | + 'value': 'GB'}, |
| 30 | + ], |
| 31 | + 'sortOrder': 'StartTimeNewest' |
| 32 | + } |
| 33 | + |
| 34 | + json.dumps(sample_dict) |
| 35 | + |
| 36 | +def toujson(): |
| 37 | + sample_dict = { |
| 38 | + 'searchFilter': {'categoryId': {'#text': 222, '@attrs': {'site': 'US'}}}, |
| 39 | + 'paginationInput': { |
| 40 | + 'pageNumber': '1', |
| 41 | + 'pageSize': '25' |
| 42 | + }, |
| 43 | + 'itemFilter': [ |
| 44 | + {'name': 'Condition', |
| 45 | + 'value': 'Used'}, |
| 46 | + {'name': 'LocatedIn', |
| 47 | + 'value': 'GB'}, |
| 48 | + ], |
| 49 | + 'sortOrder': 'StartTimeNewest' |
| 50 | + } |
| 51 | + |
| 52 | + ujson.dumps(sample_dict) |
| 53 | + |
| 54 | +def response(): |
| 55 | + |
| 56 | + xml = b'<?xml version="1.0" encoding="UTF-8"?><findItemsByProductResponse xmlns="http://www.ebay.com/marketplace/search/v1/services"><ack>Success</ack><version>1.12.0</version><timestamp>2014-02-07T23:31:13.941Z</timestamp><searchResult count="1"><item><name>Item Two</name></item></searchResult><paginationOutput><pageNumber>1</pageNumber><entriesPerPage>1</entriesPerPage><totalPages>90</totalPages><totalEntries>179</totalEntries></paginationOutput><itemSearchURL>http://www.ebay.com/ctg/53039031?_ddo=1&_ipg=2&_pgn=1</itemSearchURL></findItemsByProductResponse>' |
| 57 | + o = ResponseDataObject({'content': xml}, []) |
| 58 | + r = Response(o, verb='findItemsByProduct', list_nodes=['finditemsbyproductresponse.searchresult.item', 'finditemsbyproductresponse.paginationoutput.pagenumber']) |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + sample_dict = { |
| 63 | + 'searchFilter': {'categoryId': {'#text': 222, '@attrs': {'site': 'US'}}}, |
| 64 | + 'paginationInput': { |
| 65 | + 'pageNumber': '1', |
| 66 | + 'pageSize': '25' |
| 67 | + }, |
| 68 | + 'itemFilter': [ |
| 69 | + {'name': 'Condition', |
| 70 | + 'value': 'Used'}, |
| 71 | + {'name': 'LocatedIn', |
| 72 | + 'value': 'GB'}, |
| 73 | + ], |
| 74 | + 'sortOrder': 'StartTimeNewest' |
| 75 | + } |
| 76 | + |
| 77 | + xml = dict2xml(sample_dict) |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + |
| 81 | + import timeit |
| 82 | + |
| 83 | + print("To JSON %s" % \ |
| 84 | + timeit.repeat("tojson()", number=1000, repeat=9, |
| 85 | + setup="from __main__ import tojson")) |
| 86 | + |
| 87 | + print("To uJSON %s" % \ |
| 88 | + timeit.repeat("toujson()", number=1000, repeat=9, |
| 89 | + setup="from __main__ import toujson")) |
| 90 | + |
| 91 | + print("dict2xml() %s" % \ |
| 92 | + timeit.repeat("main()", number=1000, repeat=9, |
| 93 | + setup="from __main__ import main")) |
| 94 | + |
| 95 | + print("Response Class %s" % \ |
| 96 | + timeit.repeat("response()", number=1000, repeat=9, |
| 97 | + setup="from __main__ import response")) |
| 98 | + |
0 commit comments