'dumping YAML with tags as JSON

I know I can use ruamel.yaml to load a file with tags in it. But when I want to dump without them i get an error. Simplified example :-

from ruamel.yaml import YAML
from json import dumps
import sys

yaml = YAML()
data = yaml.load(
"""
!mytag
a: 1
b: 2
c: 2022-05-01
"""
)

try:
    yaml2 = YAML(typ='safe', pure=True)
    yaml.default_flow_style = True
    yaml2.dump(data, sys.stdout)
except Exception as e:
    print('exception dumping using yaml', e)
try:
    print(dumps(data))
except Exception as e:
    print('exception dumping using json', e)

exception dumping using cannot represent an object: ordereddict([('a', 1), ('b', 2), ('c', datetime.date(2022, 5, 1))])

exception dumping using json Object of type date is not JSON serializable

I cannot change the load() without getting an error on the tag. How to get output with tags stripped (YAML or JSON)?



Solution 1:[1]

I don't understand why you use alt_1 instead of alt.

And if you want to send some key then you have to use Controller()

This code works for me

from pynput.keyboard import Key, Listener, Controller

def on_press(key):
    print(f'{key} pressed')

def on_release(key):
    print(f'{key} released')

    if key == Key.alt:
        print('>>> I press ALT <<<')
        controller.press(Key.alt)

    if key == Key.esc:
        # Stop listener
        return False

#--- main ---

controller = Controller()

# Collect events until released
with Listener(on_press=on_press,on_release=on_release) as listener:
    # ... other code ...
    listener.join()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 furas