[PYTHON] I tried to make a dictionary function that does not distinguish between cases

dictex01.py


class PcInformation:
    def __init__(self):
        self.pc_dict = {}

    def __getitem__(self, key):
        return self.pc_dict.get(key.upper(), '---Not set---')  #Keys are unified in uppercase

    def __setitem__(self, key, value):
        self.pc_dict[key.upper()] = value  #Keys are unified in uppercase

    def __delitem__(self, key):
        del self.pc_dict[key.upper()]  #Keys are unified in uppercase

    def __len__(self):
        return len(self.pc_dict)


pc_inf = PcInformation()
pc_inf['wpc001'] = '192.168.1.33'
pc_inf['Wpc001'] = '192.168.1.39'  #I'm not case-sensitive, so I can update it
pc_inf['WPC010'] = '192.168.1.11'
pc_inf['wpc022'] = '192.168.1.22'
pc_inf['WPC_010'] = '192.168.1.100'
print(pc_inf['wpc010'])  #You can get it because it is not case-sensitive
print(pc_inf['wpc999'])  #wpc999 is not in the dictionary
del pc_inf['WpC_010']  #Delete

print('Number of terminals:{}'.format(len(pc_inf)))
print('List of terminals:')
for i, item in enumerate(pc_inf.pc_dict.items()):
    print('{:>5})  PC Name: {:<12}    IP: {}'.format(str(i + 1), item[0], item[1]))

Execution result: 192.168.1.11 --- Not set --- Number of terminals: 3 List of terminals:     1) PC Name: WPC010 IP: 192.168.1.11     2) PC Name: WPC022 IP: 192.168.1.22     3) PC Name: WPC001 IP: 192.168.1.39

Recommended Posts

I tried to make a dictionary function that does not distinguish between cases
I tried a neural network Π-Net that does not require an activation function
I tried to make a system that fetches only deleted tweets
I tried to make a Web API
I tried to make a skill that Alexa will return as cold
I tried to make a ○ ✕ game using TensorFlow
I tried to make a translation BOT that works on Discord using googletrans
I tried to make a "fucking big literary converter"
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
I tried to make a memo app that can be pomodoro, but a reflection record
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
I tried to make a generator that generates a C # container class from CSV with Python
How to make a recursive function
[5th] I tried to make a certain authenticator-like tool with python
I tried to create a server environment that runs on Windows 10
I tried to make an activity that collectively sets location information
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
A special Python codec that seems to know but does not know
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a strange quote for Jojo with LSTM
I tried to make an image similarity function with Python + OpenCV
I tried to make a mechanism of exclusive control with Go
I tried to make a site that makes it easy to see the update information of Azure
I implemented a method to calculate the evaluation index (specificity, NPV) that scikit-learn does not have
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Python] I tried to make a simple program that works on the command line using argparse.
I tried to create a linebot (implementation)
Python: I tried to make a flat / flat_map just right with a generator
I tried to make a face diagnosis AI for a female professional golfer ①
How to fix a bug that jupyter notebook does not start automatically
I tried to make a face diagnosis AI for a female professional golfer ②
I tried to make a calculator with Tkinter so I will write it
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to make a serial communication single function module that controls the servo motor on the Petit Robo board in C language
I tried to make a url shortening service serverless with AWS CDK
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
A story that sometimes does not work if pip is up to date
When I tried to make a VPC with AWS CDK but couldn't make it
[Patent analysis] I tried to make a patent map with Python without spending money
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
[Python] Smasher tried to make the video loading process a function using a generator
Load a photo and make a handwritten sketch. With zoom function. Tried to make it.
[Python] I tried to get the type name as a string from the type function
I tried my best to make an optimization function, but it didn't work.
I tried to make a suspicious person MAP quickly using Geolonia address data
I tried to make a simple image recognition API with Fast API and Tensorflow
I tried installing a driver for a NIC that is not recognized by Linux
I tried to build a super-resolution method / ESPCN
I tried to build a super-resolution method / SRCNN ①
I added a function to CPython (ternary operator)