Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dictmapper

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dictmapper

Maps hierarchical dictionaries to flat structures

  • 0.1
  • PyPI
  • Socket score

Maintainers
1

dictmapper

dictmapper was 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

.. _tablib : http://github.com/kennethreitz/tablib

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc