Convert Webpay Entity type to Dict type (recursively in Python)

Webpay, I don't think it's very popular, but I'm trying to use it for a while.

The documentation looks easy to understand, but it takes a while to understand after all because it's barely possible.

Python bindings are provided (installable with pip) and are relatively easy to request.

import webpay

client = webpay.WebPay(settings.WEBPAY_SECRET)
response = client.customer.create(card=token)

Like this.

This response variable contains the webpay ʻEntity type (webpay.data_types.Entity`), which is similar in content to a dictionary, but each value is expanded into a class variable. I will. It's like a JavaScript object.

It's nested, so trying to log it can be very annoying. For the time being, __str__ is implemented, and a somewhat structured character string similar to yaml is returned, but it is a little nervous because it includes line breaks and so on. Unless you also write a parser, reusability seems low.

Therefore. If you recursively convert it to a dictionary type, it will be easier to handle.

from webpay.data_types import Entity


def webpay_entity_to_dict(webpay_entity):
    """
Recursively convert webpay Entity type to Dict
    """
    if isinstance(webpay_entity, list):
        return [webpay_entity_to_dict(e) for e in webpay_entity]
    if not isinstance(webpay_entity, Entity):
        return webpay_entity
    return {key: webpay_entity_to_dict(value) for key, value
            in webpay_entity.__dict__.items()}

If you pass this, you can make it a dictionary type, so it will be easier to serialize it, such as json.dumps () and then logging it.

Recommended Posts

Convert Webpay Entity type to Dict type (recursively in Python)
Convert markdown to PDF in Python
Convert psd file to png in Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Convert PDFs to images in bulk with Python
Type Python scripts to run in QGIS Processing
Convert exponential notation float to str in Python
How to handle datetime type in python sqlite3
Convert cubic mesh code to WKT in Python
Convert dict to array
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
How to convert Python # type for Python super beginners: str
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert CIDR notation netmask to dotted decimal notation in Python
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
[Python] How to sort dict in list and instance in list
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
To flush stdout in Python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Login to website in Python
Use list-keyed dict in Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert CIDR notation in Python
Speech to speech in python [text to speech]
Convert python 3.x code to python 2.x
Post to Slack in Python
How to convert Python # type for Python super beginners: int, float
Convert images passed to Jason Statham-like in Python to ASCII art
Convert Excel file to text in Python for diff purposes
Determine the date and time format in Python and convert to Unixtime
[Python] How to do PCA in Python
Practice! !! Introduction to Python (Type Hints)
How to collect images in Python
Convert files written in python etc. to pdf with syntax highlighting
How to use SQLite in Python
Workflow to convert formula (image) to python
Type in Python to implement algebraic extension (3) ~ Polynomial ring / Polynomial quotient ring / Field extension ~
Convert list to DataFrame with python
Read big endian binary in Python and convert it to ndarray
Try to calculate Trace in Python
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
Convert the result of python optparse to dict and utilize it
How to handle Japanese in Python
Python> tuple> Convert double tuple to single tuple
An alternative to `pause` in Python
What to do when the value type is ambiguous in Python?
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.