Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json load problem
#4
Here's an example that creates two json files, and can read it back for a test
This requires python 3.2 or later

it will create two json files in a sub-directory named data

EnigmaPaths.py
from pathlib import Path

class EnigmaPaths:
    def __init__(self):
        self.homepath = Path('.')
        self.datapath = self.homepath / 'data'
        self.imagepath = self.homepath / 'image'
        self.enigma_info = self.datapath / 'enigma_info.json'
        self.color_info = self.datapath / 'color_info.json'
        self.patchboard_image = self.imagepath / 'patchboard.ppm'
InitializationData.py
import EnigmaPaths
import json


class InitializationData:
    def __init__(self):
        self.epath = EnigmaPaths.EnigmaPaths()
        self.epath.datapath.mkdir(exist_ok=True)
        self.init_data = {
            'rotor_info': {
                'rotor1_info': {
                    'name': 'rotor1',
                    'cipher': 'EKMFLGDQVZNTOWYHXUSPAIBRCJ',
                    'notches': ['R'],
                },
                'rotor2_info': {
                    'name': 'rotor2',
                    'cipher': 'AJDKSIRUXBLHWTMCQGZNPYFVOE',
                    'notches': ['F']
                },
                'rotor3_info': {
                    'name': 'rotor3',
                    'cipher': 'BDFHJLCPRTXVZNYEIWGAKMUSQO',
                    'notches': ['W']
                },
                'rotor4_info': {
                    'name': 'rotor4',
                    'cipher': 'ESOVPZJAYQUIRHXLNFTGKDCMWB',
                    'notches': ['K']
                },
                'rotor5_info': {
                    'name': 'rotor5',
                    'cipher': 'VZBRGITYUPSDNHLXAWMJQOFECK',
                    'notches': ['A']
                },
                # Rotor 6 - 8 available on Kriegsmarine M3 and M4 only
                'rotor6_info': {
                    'name': 'rotor6',
                    'cipher': 'JPGVOUMFYQBENHZRDKASXLICTW',
                    'notches': ['A', 'N']
                },
                'rotor7_info': {
                    'name': 'rotor7',
                    'cipher': 'NZJHGRCXMYSWBOUFAIVLPEKQDT',
                    'notches': ['A', 'N']
                },
                'rotor8_info': {
                    'name': 'rotor8',
                    'cipher': 'FKQHTLXOCBJSPDZRAMEWNIUYGV',
                    'notches': ['A', 'N']
                },
            },
            'reflector_B': 'YRUHQSLDPXNGOKMIEBFZCWVJAT',
            'reflector_C': 'FVPJIAOYEDRZXWGCTKUQSBNMHL',
            'unencoded': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        }
        with self.epath.enigma_info.open('w') as f:
            json.dump(self.init_data, f)


class Colors:
    '''
        Colors from the the w3schools themes website:
        main: https://www.w3schools.com/w3css/w3css_colors.asp
        themes: https://www.w3schools.com/w3css/w3css_color_themes.asp
        and specifically portions of the w3-theme-black theme css for this
        theme can be found here:  https://www.w3schools.com/lib/w3-theme-black.css
    '''
    def __init__(self):
        self.epath = EnigmaPaths.EnigmaPaths()
        self.color_theme_black = {
            '.w3_theme_l5': {
                'color': '#000',
                'background-color': '#f0f0f0'
            },
            '.w3-theme-l4': {
                'color': '#000',
                'background-color': '#cccccc'
            },
            '.w3-theme-l3': {
                'color': '#fff',
                'background-color': '#999999'
            },
            '.w3-theme-l2': {
            'color': '#fff',
                'background-color': '#666666'
            },
            '.w3-theme-l1': {
                'color': '#fff',
                'background-color': '#333333'
            },
            '.w3-theme-d1': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme-d2': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme-d3': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme-d4': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme-d5': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme-light': {
                'color': '#000',
                'background-color': '#f0f0f0'
            },
            '.w3-theme-dark': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme-action': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-theme': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-text-theme': {
                'color': '#000000'
            },
            '.w3-border-theme': {
                'border-color': '#000000'
            },
            '.w3-hover-theme:hover': {
                'color': '#fff',
                'background-color': '#000000'
            },
            '.w3-hover-text-theme': {
                'color': '#000000'
            },
            '.w3-hover-border-theme': {
                'border-color': '#000000'
            }
        }

        with self.epath.color_info.open('w') as f:
            json.dump(self.color_theme_black, f)

    def test_read(self):
        with self.epath.color_info.open() as f:
            self.init_data = json.load(f)

if __name__ == '__main__':
    InitializationData()
    Colors()
Reply


Messages In This Thread
json load problem - by mepyyeti - Dec-27-2017, 06:10 AM
RE: json load problem - by buran - Dec-27-2017, 07:32 AM
RE: json load problem - by mepyyeti - Dec-27-2017, 07:13 PM
RE: json load problem - by Larz60+ - Dec-27-2017, 08:32 PM
RE: json load problem - by mepyyeti - Dec-27-2017, 09:44 PM
RE: json load problem - by Larz60+ - Dec-28-2017, 02:05 AM
RE: json load problem - by Larz60+ - Dec-28-2017, 02:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  JSON Dump and JSON Load foxholenoob 8 1,143 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  Problem with nested JSON Kalet 7 2,816 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Problem With Database Calls and Load Timbo03 1 2,136 Nov-21-2021, 10:48 AM
Last Post: Timbo03
  Problem with Json Array kwekey 2 1,695 Aug-02-2021, 05:11 PM
Last Post: kwekey
  Problem to parse a json enigma619 3 2,394 Dec-04-2020, 08:16 AM
Last Post: enigma619
  Python - help with getting JSON from one DB and load to another DB qIekm 4 3,301 Apr-16-2020, 07:07 AM
Last Post: qIekm
  json problem enigma619 9 3,743 Dec-19-2019, 08:29 AM
Last Post: enigma619
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,917 Dec-11-2019, 07:50 AM
Last Post: parsley
  Output to a json file problem Netcode 3 3,752 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Load JSON file data into mongodb using pymongo klllmmm 1 11,885 Jun-28-2019, 12:47 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020