Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Things past redress and now with me past care. -- William Shakespeare, "Richard II"


comp / comp.misc / Re: Why Strict YAML Refuses To Do Implicit Typing

SubjectAuthor
o Re: Why Strict YAML Refuses To Do Implicit TypingStefan Ram

1
Subject: Re: Why Strict YAML Refuses To Do Implicit Typing
From: Stefan Ram
Newsgroups: comp.misc
Organization: Stefan Ram
Date: Wed, 24 Apr 2024 12:14 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.misc
Subject: Re: Why Strict YAML Refuses To Do Implicit Typing
Date: 24 Apr 2024 12:14:22 GMT
Organization: Stefan Ram
Lines: 60
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <notation-20240424131100@ram.dialup.fu-berlin.de>
References: <slrnv2gtfc.icd.bencollver@svadhyaya.localdomain>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de q7yZSTE6IsKu9MMcyZ9NtwMaUk78j8ZDxKeDq5z2d/aoBp
Cancel-Lock: sha1:dTHE/i4zpPa0tPPcGy3AxOptG18= sha256:uy9zQlF9gUrSttdoKs8oR2vxpliiSYq+7UUrGWitZDc=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

Ben Collver <bencollver@tilde.pink> wrote or quoted:
>- GB
>- IE
>- FR
>- DE

These days JSON is ubiquitous on the web. In the programming world,
it's Python that rules. Check this out - the following notation is
valid JSON as well as valid Python, and it's so straightforward that
most people could understand it after a bit of training.

{ "countries":
[ "GB",
"IE",
"FR",
"DE" ]}

Here's a script that parses the same string (like it could
have been read from a config file) once as JSON and once
as Python - the result is the same.

Main.py

# This is a script for Python 3.9.

# Prepare that input like it's a string that got read from a file.
input = '''
{ "countries":
[ "GB",
"IE",
"FR",
"DE" ]}
'''[ 1: -1 ]
print( f"input is (without the outer apostrophes):\n'{input}'" )

print( "\nReading input as json:" )
import json
print( json.loads( input ))

print( "\nReading input as Python:" )
import ast
print( ast.literal_eval( input ))

Output

input is (without the outer apostrophes):
'{ "countries":
[ "GB",
"IE",
"FR",
"DE" ]}'

Reading input as json:
{'countries': ['GB', 'IE', 'FR', 'DE']}

Reading input as Python:
{'countries': ['GB', 'IE', 'FR', 'DE']}

So the input got converted into a Python dict, which contains
a Python list, which in turn holds Python strs (strings).

1

rocksolid light 0.9.8
clearnet tor