Python memo using perl-Dictionary type (case is not valid)

--Are you a python join? I will make a note of what I thought.

In perl, this is hash.

--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!

Error without key

--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 ...

Confirmation of key existence

--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

With perl ...

--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

Python memo using perl-Dictionary type (case is not valid)
Python memo using perl --format is convenient.
Python round is not strictly round
Python immutable type int memo
Python memo using perl --join
Python data type summary memo
Python Note: About comparison using is
[Personal memo] Python sequence type / mapping type
python note: when easy_install is not available
[Python] Name Error: name'urlparse' is not defined
Python learning basics ~ What is type conversion? ~
[python] [meta] Is the type of python a type?
[Python] Complete preprocessing Memo as it is
Python memo
python memo
Python memo
Python memo
Python memo
Python memo
[Personal memo] julia --Using Python library with julia using PyCall
Python Pandas is not suitable for batch processing
python memo: Treat lists as a set type
Python log is not output with docker-compose up
[Hyperas] Type Error: Resolving'function' object is not subscriptable
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
There is a pattern that the program did not stop when using Python threading