Access dict via dict.key

2024/10/5 15:08:00

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_bodyerr = 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.

Answer

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.

https://en.xdnf.cn/q/120275.html

Related Q&A

Function not returning anything

My viewdef login(request):c = {}c.update(csrf(request))return render_to_response(request, login.html, c)def auth_view(request):username = request.POST.get (username, )password = request.POST.get (passw…

My entry box always returns PY_VAR1 value!!though Im using the .get function

please take a look at my code, its really simple I need to take the value from the entry box and use it in my program and when pressing the add button I print it ,it keeps giving me this value PY_VAR1…

Arthimatic Quiz Not Accepting Correct Answers

I am attempting to make an arithmetic quiz, but have run into this issue: Even if I input the correct answer, it seems to ignore the correct answer code and go straight to the incorrect answer code. Ba…

Linux - Check if python script is running in screen and run if not [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

Making Phonebook in python : i want to get this screen by fixing my current code

I made my code like below....But as i input the data such as spam & number, previous data is deleted.So id like to make multiple value in one key... (i think using list is kinda good method)For exa…

Adding enemies to a pygame platformer

Im new to pygame and trying to make a platformer game thats based on this tutorial: http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.pyI cant quite figure out how to …

Tips for cleaning up a challenges answer? Weighted Sum of Digits [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Get a variable as filename from python script and use it in a batch script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 3.x AttributeError: NoneType object has no attribute groupdict

Being a beginner in python I might be missing out on some kind of basics. But I was going through one of the codes from a project and happened to face this :AttributeError: NoneType object has no attri…

importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this …