{ "info": { "author": "Nicolas Laurance", "author_email": "nlaurance at zindep dot com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: GNU General Public License (GPL)", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Text Processing :: Filters", "Topic :: Text Processing :: Markup :: XML", "Topic :: Utilities" ], "description": ".. contents::\n\nDetailed Documentation\n**********************\n\nXML and CSV comparisons\n=======================\n\nTwo scripts are provided xml_cmp and csv_cmp\nThey both compares 2 files and outputs delta as file_suppr,\nfile_addon and file_changes\n\nthe extension is forced to xml or csv respectively\n\nList comparison\n===============\n\nlistcomparator provides a Comparator object that allows to find the differences\nbetween two lists **provided the elements of the lists appear in the same order**\n\n>>> old = [1, 2, 3, 4, 5, 6]\n>>> new = [1, 3, 4, 7, 6]\n\n>>> from listcomparator.comparator import Comparator\n\nLet's create a Comparator object\n\n>>> comp = Comparator(old,new)\n\nThe check method gives values to additions and deletions attributes\n\n>>> comp.check()\n>>> comp.additions\n[7]\n>>> comp.deletions\n[2, 5]\n\nWe can also use lists of lists\n\n>>> old_list = [['62145', 'azerty'], ['1234', 'qwerty'], ['9876', 'ipsum']]\n>>> new_list = [['62145', 'azerty'], ['1234', 'qwertw'], ['4865', 'lorem']]\n>>> comp = Comparator(old_list, new_list)\n>>> comp.check()\n>>> comp.additions\n[['1234', 'qwertw'], ['4865', 'lorem']]\n>>> comp.deletions\n[['1234', 'qwerty'], ['9876', 'ipsum']]\n\nWe can have an issue when a modification, in our case \"qwerty\" became \"qwertz\",\nappears in both outputs, comp.additions and comp.deletions.\nYou might want to consider this a change.\nComparator can handle this and filter out such cases if you provide a function\nthat tells Comparator how to recognize such cases\nIn our example, we consider 2 elements to be the same if the first element of the\nlist is the same, a kind of id.\n\n>>> def my_key(x):\n... return x[0]\n...\n\nThe getChanges methods then provides a new attribute : changes\n\n>>> comp.getChanges(my_key)\n>>> comp.changes\n[['1234', 'qwertw']]\n\nof course, additions and deletions stay unchanged\n\n>>> comp.additions\n[['1234', 'qwertw'], ['4865', 'lorem']]\n>>> comp.deletions\n[['1234', 'qwerty'], ['9876', 'ipsum']]\n\nYou might want to consider only 'pure' additions and deletions\ngetChanges allows for a keyword argument 'purge' that does just that\n\n>>> comp.getChanges(my_key, purge=True)\n>>> comp.changes\n[['1234', 'qwertw']]\n>>> comp.additions\n[['4865', 'lorem']]\n>>> comp.deletions\n[['9876', 'ipsum']]\n\nThe old and new attributes store the lists to be compared\nyou might want to reset those, Comparator provides a purgeOldNew method\nto clear up memory\n\n>>> comp.old\n[['62145', 'azerty'], ['1234', 'qwerty'], ['9876', 'ipsum']]\n>>> comp.new\n[['62145', 'azerty'], ['1234', 'qwertw'], ['4865', 'lorem']]\n>>> comp.purgeOldNew()\n>>> comp.old\n>>> comp.new\n\n\ncompare XML files\n=================\n\nComparator can be used to compare xml files\nlet's make two xml files describing books\n\n>>> old='''\n... \n... White pages 1995\n... \n... La Poste\n... \n... Paris\n... ABEL Antoine 82 23 44 12\n... ABEL Pierre 82 67 23 12\n... \n... \n... Yellow pages 2007\n... \n... La Poste\n... \n... Bretagne\n... Zindep 82 23 44 12\n... ZYM 82 67 23 12\n... \n... \n... Dark pages 2007\n... \n... La Poste\n... \n... Greves\n... SNCF 82 23 44 12\n... \n... \n... \n... '''\n\n>>> new='''\n... \n... White pages 1995\n... \n... La Poste\n... \n... Paris\n... ABIL Antoine 82 23 44 12\n... ABEL Pierre 82 67 23 12\n... \n... \n... Yellow pages 2007\n... \n... La Poste\n... \n... Bretagne\n... Zindep 82 23 44 12\n... ZYM 82 67 23 12\n... \n... \n... Blue pages 2007\n... \n... La Poste\n... \n... Bretagne\n... Mer 82 23 44 12\n... Ciel 82 67 23 12\n... \n... \n... \n... '''\n\nelementtree is required to parse xml\n\n>>> from elementtree import ElementTree as ET\n\nfor this test we'll use cStringIO rather than a file\n\n>>> import cStringIO\n>>> ex_old = cStringIO.StringIO(old)\n>>> ex_new = cStringIO.StringIO(new)\n\nwe parse contents\n\n>>> root_old = ET.parse(ex_old).getroot()\n>>> root_new = ET.parse(ex_new).getroot()\n\nthe \"book\" tag identifies objects we want\n>>> objects_old = root_old.findall('book')\n>>> objects_new = root_new.findall('book')\n\nas we can't compare 2 objects, we stringify them\n\n>>> objects_old = [ET.tostring(o) for o in objects_old]\n>>> objects_new = [ET.tostring(o) for o in objects_new]\n\nfrom there, Comparator is usefull\n\n>>> my_comp = Comparator(objects_old, objects_new)\n>>> my_comp.check()\n\n>>> for e in my_comp.additions:\n... print e\n...\nWhite pages 1995\n\nLa Poste\n\nParis\nABIL Antoine 82 23 44 12\nABEL Pierre 82 67 23 12\n\n\n\nBlue pages 2007\n\nLa Poste\n\nBretagne\nMer 82 23 44 12\nCiel 82 67 23 12\n\n\n\n\n>>> for e in my_comp.deletions:\n... print e\n...\nWhite pages 1995\n\nLa Poste\n\nParis\nABEL Antoine 82 23 44 12\nABEL Pierre 82 67 23 12\n\n\n\nDark pages 2007\n\nLa Poste\n\nGreves\nSNCF 82 23 44 12\n\n\n\n\nwe need to know wich tag is used to uniquely define an object\nhere we choose to use the \"title\" tag\n\n>>> def item_signature(xml_element):\n... title = xml_element.find('title')\n... return title.text\n...\n\nwe build our custom function for use by the Comparator\n\n>>> def my_key(str):\n... file_like = cStringIO.StringIO(str)\n... root = ET.parse(file_like)\n... return item_signature(root)\n...\n\nthen the getChanges method of the Comparator becomes available\n\n>>> my_comp.getChanges(my_key, purge=True)\n\nWhat books have been exclusively added ?\n\n>>> for e in my_comp.additions:\n... print e\n...\nBlue pages 2007\n\nLa Poste\n\nBretagne\nMer 82 23 44 12\nCiel 82 67 23 12\n\n\n\n\nwhat books have been exclusively removed ?\n\n>>> for e in my_comp.deletions:\n... print e\n...\nDark pages 2007\n\nLa Poste\n\nGreves\nSNCF 82 23 44 12\n\n\n\n\nwhat books have changed ? that is have same title, but different other values\n\n>>> for e in my_comp.changes:\n... print e\n...\nWhite pages 1995\n\nLa Poste\n\nParis\nABIL Antoine 82 23 44 12\nABEL Pierre 82 67 23 12\n\n\n\n\n\nthen we can put those results back in xml file\n\n* This code conforms to PEP8\n* It is fully tested, 100% coverage\n* A Buildbot runs tests at each commit\n\nContributors\n************\n\n\nMain developpers\n================\n\n* Nicolas Laurance \n\nwith contributions of\n---------------------\n\n* Yves Mahe \n\n\nChange history\n**************\n\nNew in 0.1\n==========\n\nFirst Release", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://code.google.com/p/listcomparator/", "keywords": "xml csv list compare diff difference", "license": "GPL", "maintainer": "", "maintainer_email": "", "name": "ListComparator", "package_url": "https://pypi.org/project/ListComparator/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/ListComparator/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://code.google.com/p/listcomparator/" }, "release_url": "https://pypi.org/project/ListComparator/0.1/", "requires_dist": null, "requires_python": null, "summary": "Compares ordered lists, xml and csv application", "version": "0.1" }, "last_serial": 784630, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "e8b3b57781101ab6eeeda7e9fb807e27", "sha256": "1b17dad959d0963261a8e8e08c06018329d413461d0f0facd77fb206b66074fb" }, "downloads": -1, "filename": "ListComparator-0.1.tar.gz", "has_sig": false, "md5_digest": "e8b3b57781101ab6eeeda7e9fb807e27", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10897, "upload_time": "2009-12-13T19:26:59", "url": "https://files.pythonhosted.org/packages/6e/c6/29c3bbc181c6b24dcfb8b46a76d66bf2c83f3802f727de727bbb41841688/ListComparator-0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e8b3b57781101ab6eeeda7e9fb807e27", "sha256": "1b17dad959d0963261a8e8e08c06018329d413461d0f0facd77fb206b66074fb" }, "downloads": -1, "filename": "ListComparator-0.1.tar.gz", "has_sig": false, "md5_digest": "e8b3b57781101ab6eeeda7e9fb807e27", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10897, "upload_time": "2009-12-13T19:26:59", "url": "https://files.pythonhosted.org/packages/6e/c6/29c3bbc181c6b24dcfb8b46a76d66bf2c83f3802f727de727bbb41841688/ListComparator-0.1.tar.gz" } ] }