question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing Python dictionaries within fypp

See original GitHub issue

Is it possible to assign and retrieve Python dictionary key-value pairs from within fypp without writing a dedicated Python class? If yes, what is the correct syntax to do so?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aradicommented, Nov 11, 2021

I am happy you managed. Just a comment: Variables in the global scope (chkval, tslb1, etc.) do not need a #:global assignment, they are global by default…

Otherwise, you may eventually be interested in the FyTest unit testing framework, which offers unit testing and uses the aforementioned dictionary manipulation quite heavily.

0reactions
wyphancommented, Nov 10, 2021

Never mind, I figured it out! Here’s my current mod_testing_macros.fypp:

#! -*- mode: f90; -*-
#:mute
!===============================================================================
! Testing infrastructure fypp macros
! Last edited: Nov 10, 2021 (WYP)
!===============================================================================
#:endmute

#! Initialize testing infrastructure

#! Number of tests
#:global ntst
#:set ntst = 0

#! Dictionaries to hold golden values and test labels
#:global chkval
#:set chkval = {}
#:global tstlbl
#:set tstlbl = {}

#! Macro to register golden value
#:def save_gold( lbl, val )
  $:tstlbl.update([(ntst, lbl)])
  $:chkval.update([(ntst, val)])
#:enddef save_gold

#! Macro to check scalar test result exactly against golden value
#:def check_exact( val )
  #:set lbl  = tstlbl[ntst]
  #:set gold = chkval[ntst]
  BLOCK
    USE ISO_FORTRAN_ENV, ONLY: o => output_unit, e => error_unit
    WRITE(o,'(A)') ${lbl}$
    IF( ${val}$ == ${gold}$ ) THEN
      WRITE(e,*) 'OK: result = ', ${val}$
    ELSE
      nerr = nerr + 1
      WRITE(e,*) 'Error[${_FILE_}$]: result = ', ${val}$, ', should be ${gold}$'
    END IF
  END BLOCK
  #:set ntst = ntst + 1
#:enddef check_exact

#:def test_summary()
  BLOCK
    USE ISO_FORTRAN_ENV, ONLY: o => output_unit
    WRITE(o,'("Summary for ${_FILE_}$:")')
    IF( nerr > 0 ) THEN
      WRITE(o,'("  ",I0," out of ${ntst}$ tests failed")') nerr
    ELSE
      WRITE(o,'("  All ${ntst}$ tests succeeded")')
    END IF
  END BLOCK
#:enddef test_summary
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Nested Dictionary (With Examples)
In this article, you'll learn about nested dictionary in Python. More specifically, you'll learn to create nested dictionary, access elements, modify them and ......
Read more >
Python - Accessing Items in Lists Within Dictionary
Method 1: Manually accessing the items in the list. This is a straightforward method, where the key from which the values have to...
Read more >
Python How to Access Nested Dictionary (with 5+ Examples)
This guide teaches you how to access nested dictionaries in Python. Besides, you learn how to create a nested dictionary, access the elements,...
Read more >
Python Nested Dictionary
Learn to create a Nested Dictionary in Python, access change add and remove nested dictionary items, iterate through a nested dictionary and more....
Read more >
Python - Access Dictionary Items
You can access the items of a dictionary by referring to its key name, inside ... The keys() method will return a list...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found