--Are you a python join? I will make a note of what I thought.
--It seems that the hash key can be either uppercase or lowercase. It's convenient. ――It's a module, not a plain python. It's called CaseInsensitiveDict. --Please install the module with ``` pip install requests` `` in advance.
>>> from requests.structures import CaseInsensitiveDict
>>> cid = CaseInsensitiveDict({'Apple': 'osx', 'NeXT': 'STEP'})
>>> cid
{'Apple': 'osx', 'NeXT': 'STEP'}
>>> cid['apple']
'osx'
>>> cid['APPLE']
'osx'
>>> cid['next']
'STEP'
>>> cid['nEXT']
'STEP'
--Um convenient!
--If you use perl, "" will be returned if you don't have hash, but if you use python, you will get an error. (Troublesome ...)
>>> cid['microsoft']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "〜〜/python3.5/site-packages/requests/structures.py", line 54, in __getitem__
return self._store[key.lower()][1]
KeyError: 'microsoft'
--I was angry ...
--It seems that you can use the in operator and has_key method to check the existence of the key ... --CaseInsensitiveDict seems to have restrictions. --has_key was not prepared.
has_key
>>> cid.has_key('microsoft')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'CaseInsensitiveDict' object has no attribute 'has_key'
――If it is in, it's okay.
in
>>> 'microsoft' in cid
False
>>> 'apple' in cid
True
>>> 'Apple' in cid
True
--Use a module called Hash :: Case :: Preserve. ――It seems that the behavior when there is no key is the same as the plain hash.
cid.pl
use Hash::Case::Preserve;
tie my(%cid),'Hash::Case::Preserve';
$cid{'NeXT'} = 'STEP';
$cid{'Apple'} = 'osx';
print $cid{'next'};
print $cid{'APPLE'};
print $cid{'microsoft'};
print keys %cid
sh-3.2$ perl -l cid.pl
STEP
osx
AppleNeXT
Recommended Posts