A memo to generate a dynamic variable of class from dictionary data (dict) that has only standard type data in Python3

For example, I want to write a configuration file in JSON, read it with dict, and make it a variable of Class. String lookups can be buggy when typoing.

For the time being, if it is setting data (e.g. numerical value, character string), you can go by updating __dict__.

class Conf:
  def __init__(self):
    pass

d = { 'a': 3 }

c = Conf()
c.__dict__.update(d)

print(c.a)

I want the variable to be a dict

Conversely, you can also create a dict from a variable by iterating __dict__.

TODO

Recommended Posts

A memo to generate a dynamic variable of class from dictionary data (dict) that has only standard type data in Python3
How to create an instance of a particular class from dict using __new__ () in python
Generate a class from a string in Python
Receive dictionary data from a Python program in AppleScript
How to write a list / dictionary type of Python3
Get the value of a specific key in a list from the dictionary type in the list with Python
A memo that reads data from dashDB with Python & Spark
How to develop in a virtual environment of Python [Memo]
Create an instance of a predefined class from a string in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to send a visualization image of data created in Python to Typetalk
Code reading of faker, a library that generates test data in Python
Generate a first class collection in Python
I tried to create a class that can easily serialize Json in Python
Use PIL in Python to extract only the data you want from Exif
A memo that I wrote a quicksort in Python
How to embed a variable in a python string
How to generate a Python object from JSON
Generate a MeCab dictionary from Nico Nico Pedia data
How to get dictionary type elements of Python 2.7
A well-prepared record of data analysis in Python
How to store Python function in Value of dictionary (dict) and call function according to Key
A memo that implements the job of loading a GCS file into BigQuery in Python
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
A mechanism to call a Ruby method from Python that can be done in 200 lines
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
I wrote a class that makes it easier to divide by specifying part of speech when using Mecab in python
I want to embed a variable in a Python string
A memo connected to HiveServer2 of EMR with python
Summary of tools needed to analyze data in Python
Generate a vertical image of a novel from text data
What's in that variable (when running a Python script)
Convert Webpay Entity type to Dict type (recursively in Python)
Python Note: The mystery of assigning a variable to a variable
Generate Word Cloud from case law data in python3
[Python] Creating a GUI tool that automatically processes CSV of temperature rise data in Excel
Get the value of a specific key up to the specified index in the dictionary list in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
I tried to make a generator that generates a C # container class from CSV with Python
What seems to be a template of the standard input part of the competition pro in python3
How to plot the distribution of bacterial composition from Qiime2 analysis data in a box plot
A Python script that stores 15 years of MLB game data in MySQL in 10 minutes (Baseball Hack!)
A Python script that allows you to check the status of the server from your browser
Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query
How to slice a block multiple array from a multiple array in Python
Published a library that hides character data in Python images
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
Processing of python3 that seems to be usable in paiza
Different from the import type of python. from A import B meaning
I want to start a lot of processes from python
How to generate exponential pulse time series data in python
A standard way to develop and distribute packages in Python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
How to get a list of built-in exceptions in python
Python: Create a dictionary from a list of keys and values
Hit REST in Python to get data from New Relic
A memo of writing a basic function in Python using recursion