-2

I created a dict source = {'livemode': False}. I thought it's possible to access the livemode value via source.livemode. But it doesn't work. Is there a way to access it that way?

As a not source['livemode'] works, but I need source.livemode as that's already used in my code and I have to handle it as an alternative to the Stripe return value charge.

I want to give a bit more context

Here I create a charge via Stripe:

def _create_charge(self, request, order_reference, order_items_dict, token):
        try:
            charge = stripe.Charge.create(
                amount=order_items_dict['total_gross'],
                application_fee=order_items_dict['application_fee'],
                currency=order_items_dict['event'].currency,
                source=token,
                stripe_account=order_items_dict['event'].organizer.stripe_account,
                expand=['balance_transaction', 'application_fee'],
            )

        except stripe.error.StripeError as e:
            body = e.json_body
            err = body.get('error', {})
            messages.error(
                request,
                err.get('message')
            )

        else:
            if charge.paid and charge.status == 'succeeded':
                return charge

I can access this with e.g. charge_or_source.livemode

def _create_order(self, request, charge_or_source, order_status):
        order_reference = request.session.get('order_reference')
        new_order = self.order_form.save(commit=False)
        print(charge_or_source.livemode, "charge_or_source.livemode")
        new_order_dict = {
            'total_gross': self.order_items_dict['total_gross'],
            'livemode': charge_or_source.livemode,
        }

Now there is a case (when the order is Free) where I have to 'skip' the _create_charge function but still, I have to send information about charge_or_source.livemode. Therefore I tried to create the above-mentioned dictionary.

currarpickt
  • 2,290
  • 4
  • 24
  • 39
Joey Coder
  • 3,199
  • 8
  • 28
  • 60
  • There is no such thing in Python. You must be confusing it with another language. – DYZ Nov 28 '18 at 07:23
  • One more thought I have is that Stripe returns a JSON object. Is it possible to access these via dict.key? – Joey Coder Nov 28 '18 at 07:24
  • 2
    Make it a class instance then and instantiate the dict keys as attributes. – Dschoni Nov 28 '18 at 07:24
  • 1
    There is no such thing as a JSON object in Python. A "JSON object" is a dictionary. – DYZ Nov 28 '18 at 07:24
  • 2
    @Dschoni 's suggestion will work, but it's about as anti-python-zen as you get. And probably more work than it's worth. And why on earth is "that's already used in my code" an excuse to do things wrong? – ShlomiF Nov 28 '18 at 07:27
  • I am a bit confused then what Stripe gives me back then. I can access charge variable via `charge.livemode`. – Joey Coder Nov 28 '18 at 07:28
  • That's a different question. – Dschoni Nov 28 '18 at 07:29
  • I wrote a bit more context to hopefully explain it better what my problem is. (see edit) – Joey Coder Nov 28 '18 at 07:32
  • Okay, I figured out now that Stripe is actually using the way @B. Morris suggested and this confused me. I now just changed it to the 'normal' way with source['livemode'] – Joey Coder Nov 28 '18 at 07:50

3 Answers3

2

I'm a beginner myself, but let me try and answer:

Say you have a dictionary:

dictionary = {"One": 1, "Two": 2, "Three": 3}

You can create a class with its keys like:

class DictKeys:
    One = 'One'
    Two = 'Two'
    Three = 'Three'

Here, One, Two and Three are class variables or attributes, which means if you create an object for this class:

key = DictKeys()

You can access all of those keys using the '.' (dot) operator.

key.One
>>'One'

Now just plug it where ever you want to access your dictionary!

dictionary[key.One]
>>1

I'm sure this isn't the best way, and class access is a tiny bit slower than dict access, but if you really want to, you can access all your keys with a dot using this method.

AbdurRehman Khan
  • 830
  • 9
  • 20
1

You can implement a custom dict wrapper (either a subclass of dict or something that contains a dict) and implement __getattr__ (or __getattribute__) to return data from the dict.

class DictObject(object):
    def __init__(self, data):
        self.mydict = data
    def __getattr__(self, attr):
        if attr in self.mydict: return self.mydict[attr]
        return super(self, DictObject).__getattr__(attr)
B. Morris
  • 630
  • 3
  • 15
0

The correct way to access a dictionary is how you proposed it:

source['livemode'] 
F. Win
  • 390
  • 5
  • 17