#๐Ÿ”’ Is this OK?

40 messages ยท Page 1 of 1 (latest)

fallen moat
#

Firstly I am absolutely amazed that my comprehension has gotten to this point, but is this the best way to iterate?

It works currently, and this is an example of ['params']['global-state'] structure:

[{'key': 'TklLT21vbkNvdW50ZXI=', 'value': {'bytes': 'AAAAAAAAAAA=', 'type': 1, 'uint': 0}}, {'key': 'TklLT21vblVJRENvdW50ZXI=', 'value': {'bytes': 'AAAAAAAAAAA=', 'type': 1, 'uint': 0}}]

graceful sandalBOT
#

@fallen moat

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

unkempt burrow
#

wth is that if thats one line than its awful for readability

undone summit
#

I mean, it's not too bad

unkempt burrow
#

i would at least try to add enters and indentation if i must have a list copm like that

fallen moat
#

I should indent then?

#
current_counter = [x['value']['uint'] 
                   for x in current_counter 
                   if b64decode(x['key']) == b'Counter'][0] + 1```
#

Something like this?

undone summit
#

You could do something like

g_state = algod_client.application_info(app_id)['params']['global-state']
current_counter = [x['value']['uint'] for x in g_state if b64decode(x['key']) == b'Counter'][0]
#

oh the last [0] + 1 threw me off lol

#

can you convert that whole struct to a dict?

#

like

g_state = {x['key']: x['value'] for x in algod_client.application_info(app_id)['params']['global-state']

And then you can just access the counter directly

print(g_state['Counter']['uint'])
fallen moat
#

Oh im sorry so I redacted the b'Counter' for some reason its actually b'NIKOmonCounter'

#

I originally was going to share in a different discord where I didn't want to disclose that

#
g_state = algod_client.application_info(app_id)['params']['global-state']
current_counter = [x['value']['uint'] for x in g_state
                   if b64decode(x['key']) == b'NIKOmonCounter'][0] + 1```
#

So like this?

#

Looks OK?

undone summit
#

yeah seems ok. I would consider mapping the whole list of key value pairs to a dict too though

fallen moat
#

Becomes

{'TklLT21vbkNvdW50ZXI=': {'bytes': 'AAAAAAAAAAA=', 'type': 1, 'uint': 0}, 'TklLT21vblVJRENvdW50ZXI=': {'bytes': 'AAAAAAAAAAA=', 'type': 1, 'uint': 0}}

Methods comparison:

g_state = algod_client.application_info(app_id)['params']['global-state']
current_counter = [x['value']['uint'] for x in g_state
                   if b64decode(x['key']) == b'NIKOmonCounter'][0] + 1

g_state_2 = {x['key']: x['value'] for x in algod_client.application_info(app_id)['params']['global-state']}
current_counter_2 = [y['uint'] for x, y in g_state_2.items() 
                     if b64decode(x) == b'NIKOmonCounter'][0] + 1```
#

I feel like the original looks a little cleaner without mapping, but perhaps I'm doing it wrong

undone summit
fallen moat
#
g_state_2 = {b64decode(x['key']): x['value'] for x in algod_client.application_info(app_id)['params']['global-state']}
current_counter_2 = [y['uint'] for x, y in g_state_2.items() 
                     if x == b'NIKOmonCounter'][0] + 1```
undone summit
#
g_state = {b64decode(x['key']): x['value'] for x in algod_client.application_info(app_id)['params']['global-state']}
print(g_state[b'NIKOmonCounter'])
undone summit
fallen moat
#
# Method 1
g_state = algod_client.application_info(app_id)['params']['global-state']
current_counter = [x['value']['uint'] for x in g_state
                   if b64decode(x['key']) == b'NIKOmonCounter'][0] + 1

# Method 2
g_state_2 = algod_client.application_info(app_id)['params']['global-state']
mapped_g_state_2 = {b64decode(x['key']): x['value'] for x in g_state}
current_counter_2 = [y['uint'] for x, y in mapped_g_state.items() 
                     if x == b'NIKOmonCounter'][0] + 1
fallen moat
#

Could you explain please?

#

Method 1 loops through the array twice?

undone summit
#

you can access by key now

#

๐Ÿคท i don't know what the rest of your code looks like; i.e. if you even use this global-state thing anywhere else. But if you do, then converting to a dict first will really help

thick summit
#

Or at least split it up so the [0] + 1 is happening separately.

undone summit
#

Yeah, i really don't like indexing a list that was created on the same line

#

That's what led me to the "convert the whole thing to a dict first" idea

thick summit
#

Also, if all you care about is the first element produced, this comprehension is wasteful since it may produce many elements that will get thrown away. An easy way to do the same operation lazily is to combine next with a generator expression:

first = next(x['value']['uint'] for x in algod_client.application_info(app_id)['params']['global-state'] if b64decode(x['key']) == b'Counter')
added = first + 1
undone summit
#

Yeah. This is looking very golfy still

thick summit
#

Ya. I still don't like the look of it. A normal for loop would make it trivial to do an early exit as well.

graceful sandalBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.