Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.2 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Programming > Libraries

    dictmapper 0.1

    Download button

    No screenshots available
    Downloads: 101  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Reshef Mann | More programs
    MIT/X Consortium Lic... / FREE
    March 21st, 2012, 04:57 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    dictmapper description

    Maps hierarchical dictionaries to flat structures

    dictmapper is a Python module created to help transforming a sequence of Python dictionaries to tabular format and specifically to transform JSON documents to be exported in CSV fromat.

    Usage


    Create a mapper:

    from dictmapper import Mapper, Mapping

    class UserMapper(Mapper):

     user_id = Mapping('user_id')
     email = Mapping('email')
     Name = Mapping(lambda u: '%s %s' % (u['first_name'], u['last_name']), name='User Name')
     nickname = Mapping('nickname', default='N/A')
     street = Mapping('address/street')
     city = Mapping('address/city')
     joined_at = Mapping('joined_at', transform=lambda d: d.strftime('%Y-%m-%d'))


    Sample input:

    users_docs = [
     {
     'user_id': '1000001',
     'first_name': 'Test',
     'last_name': 'User',
     'email': 'user@test.com',
     'address': {
     'street': 'Example Road',
     'city': 'Emerald City',
     },

     'joined_at': datetime.now(),
     },
     {
     'user_id': '1000002',
     'first_name': 'Example',
     'last_name': 'Member',
     'nickname': 'exampy',
     'email': 'example@member.com',
     'address': {
     'street': 'Sample Road',
     'city': 'Emerald City',
     },

     'joined_at': datetime.now(),
     }
    ]


    Output:

    >>> mapper = UserMapper()
    >>> mapper.headers()
    ['User id', 'Email', 'Name', 'Nickname', 'Street', 'City', 'Joined at']
    >>> res = mapper.map(users_docs)
    >>> res
    [['1000001',
     'user@test.com',
     'Test User',
     'N/A',
     'Example Road',
     'Emerald City',
     '2012-03-18'],
     ['1000002',
     'example@member.com',
     'Example Member',
     'exampy',
     'Sample Road',
     'Emerald City',
     '2012-03-18']]


    Export to csv


    I recommend using the excellent tablib:

    import tablib
    data = tablib.Dataset(*res, headers=mapper.headers())
    data.csv



    Product's homepage

    Requirements:

    · Python

      


    TAGS:

    hierarchical dictionaries | Python library | flat structures | Python | map | hierarchical

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM