Last active
January 29, 2026 08:10
-
-
Save timendum/7c9041de8652523cd0391ed738e0fea5 to your computer and use it in GitHub Desktop.
How to edit Python VCR fixtures cassette
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import gzip | |
| import json | |
| import yaml | |
| from yaml import Dumper, Loader | |
| FILENAME = "./test.yml" | |
| with open(FILENAME) as fo: | |
| tyaml = fo.read() | |
| oyaml = yaml.load(tyaml, Loader=Loader) | |
| # check for compression library in Content-Encoding | |
| tjson = gzip.decompress(oyaml['interactions'][0]['response']['body']['string']).decode('utf-8') | |
| # optional json parse | |
| data = json.loads(tjson) | |
| # Do your edit here | |
| # same steps, backwards | |
| tjson = json.dumps(data) | |
| oyaml['interactions'][0]['response']['body']['string'] = gzip.compress(tjson.encode('utf-8')) | |
| with open(".".join(FILENAME.split('.')[:-1]) + '_new.yml', 'w') as fo: | |
| yaml.dump(oyaml, fo, Dumper=Dumper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment