0

My scenarios where I need it:

  1. User is inputting True or False and it is parsed as str by default. I cannot change the parsing type as python 3+ parses as str (Using Python 3.8) So when I parse bool(input('Enter True or False')), it returns True regardless since default bool function only returns False when there is an empty string. True otherwise.

  2. I have a json for which I need it.

It has following representation:

list_of_visits ='''{"Museum": "True",
"Library": "True",
"Park": "False"}
'''

Note that I cannot have its representation without qoutes as:

list_of_visits ='''{"Museum": True,
"Library": True,
"Park": False}
'''

Cause parsing this as follows throws error:

jsonic = json.loads(list_of_visits)
jsonic

But I need to parse from int and float as well at some places and I cannot write functions for each type separately . I am trying to build a one-stop solution which might inherit functionality from bool() and does this conversion as well and I can use it everywhere i.e. solution which can not only perform traditional bool() operations but also able to parse string representations.

Currently I do following steps:

if type(value) == str and value =='True':
    reprs = True
elif type(value) == str and value =='False':
    reprs = False
else:
    reprs = bool(value)
  • Why is the JSON sending strings? You can write `true` and `false` in JSON. – Barmar Oct 15 '20 at 23:31
  • When I use above code for JSONs without qoutes, it thorws this error: `JSONDecodeError: Expecting value: line 1 column 12 (char 11)` –  Oct 15 '20 at 23:34
  • In JSON, `true` and `false` have to be lowercase. – Barmar Oct 15 '20 at 23:39
  • `'''{"Museum": true}'''` – Barmar Oct 15 '20 at 23:40
  • If you use a proper JSON encoder on the sending site, it should do that automatically. – Barmar Oct 15 '20 at 23:41
  • Hah! I added those qoutes intentionally cz couldnt parse them without them. I used str(value).title() to achieve this from original JSON –  Oct 15 '20 at 23:46
  • Well you shouldnt. JSON parsers are designed to return right format. So no need to alter this – Hamza Oct 15 '20 at 23:50
  • But @Barmar my first scenario is still valid? Or no? –  Oct 15 '20 at 23:51
  • It's valid, it's just not a boolean. – Barmar Oct 15 '20 at 23:53
  • 1
    What if you really want to have the string `"True"` in your JSON? How is the receiver supposed to know which ones should be converted to booleans and which should be left alone? JSON syntax is unambiguous, that's one of it benefits. – Barmar Oct 15 '20 at 23:54

1 Answers1

-1

You can define your own function which may work in all scenarios as:

def get_bool(key):
    return value if value := {'True':True, 'False':False}.get(key) else bool(key)
    

For a single value such as input you might parse it as:

get_bool('False')

which returns: False

For jsons like that you might do:

from toolz.dicttoolz import valmap
valmap(get_bool, jsonic)

which returns:

{'Museum': 1, 'Library': 1, 'Park': 0}
    

For 3.7 and lower:

I'm in love with walrus operator since it came out in python 3.8. Anyone looking for lower versions might need some repetitions:

def get_bool(key):
return {'True':True, 'False':False}.get(key) if  key.title() in {'True':True, 'False':False}.keys() else bool(key)

Note that though it does work for every case, your JSON representation is wrong. JSONs can have boolean values so can the string representations of JSONs. But you got to use javascript syntax as true and false instead of Python's True & False. Since JSON is a javascript notation.

Hamza
  • 5,373
  • 3
  • 28
  • 43
  • Trying this out. But I am also curious that why do python does not have this functionality in it and what is the significance of using this instead of parsing as I mentioned. Like I can think of several scenarios where it might be actually useful. –  Oct 15 '20 at 23:35
  • True! But it is more useful this way. I never thought of your scenarios until I come across this. while using bool for years – Hamza Oct 15 '20 at 23:48