PyYAML YAML - 如何使用自定义命名args构建字典

时间:2018-01-31 15:31:32

标签: python dictionary yaml pyyaml

我想制作一个使用config.yamlexplanation about its architecture)的自定义pdflatex构建器。我应该处理的是this one

我当前的代码(完整代码here)将尝试使用我想稍后使用的args构建一个字典:

# a useful way to have all the useful mappings in config
def build_dictionary(document):
    # default mapping
    # key : 'quadri' , value : (1-8)
    # key : 'name' , value : ['advSecu', 'advalgo',..]
    # key : 'option' , value : ['FSAB', 'AUCE',..]
    # key : 'code' , value : [1031, 1101, 1102,...] (not useful for me)
    # key : 'type' , value : ['exercises', 'errata', 'formulaire', 'mcq', 'notes', 'summary']
    # not useful for me : 'code', 'sol', 'num'
    default_argument_mapping = document['clients'][0]['arguments']
    temp_mapping = document['clients'][0]['output']['parameters'][0]  # temp var to simplify my life ^^

    # merged type ; to have all the types declared in this config yml
    custom_type = dict()
    custom_type.update(document['clients'][0]['output']['parameters'][1]['mapping'])
    custom_type.update(document['clients'][1]['output']['parameters'][1]['mapping'])

    # make a more simpler dictionary because I don't hard yml stuff
    mapping_dictionary = dict(
        [
            (temp_mapping['parameters'][0]['key']['arg'], temp_mapping['parameters'][0]['mapping']),  # quadri
            (temp_mapping['parameters'][1]['key']['arg'], temp_mapping['parameters'][1]['mapping']),  # option
            (temp_mapping['parameters'][4]['arg'], default_argument_mapping[temp_mapping['parameters'][4]['arg']]),
            # code
            (temp_mapping['parameters'][5]['key']['arg'], temp_mapping['parameters'][5]['mapping']),  # name
            (document['clients'][0]['output']['parameters'][1]['key']['arg'],  # type
             custom_type),
            ('year', document['clients'][1]['arguments']['year']),  # year
            ('month', document['clients'][1]['arguments']['month']),  # month
            ('minmaj', document['clients'][1]['arguments']['minmaj']),  # minmaj
            ('sol', default_argument_mapping['sol'])  # sol
        ]
    )
    return mapping_dictionary, default_argument_mapping

返回的字典是:

{
  'quadri': {
    1: 'BACHELIER',
    2: 'BACHELIER',
    3: 'BACHELIER',
    4: 'BACHELIER',
    5: 'BACHELIER',
    6: 'BACHELIER',
    7: 'MASTER',
    8: 'MASTER'
  },
  'code': [1031, 1101, 1102, 1103, 1104, 1105, 1106, 1113, 1120, 1121, 1122, 1123, 1131, 1152, 1170, 1171, 1201, 1202, 1203, 1210, 1223, 1225, 1230, 1252, 1301, 1302, 1304, 1310, 1315, 1317, 1321, 1330, 1341, 1350, 1370, 1401, 1402, 1503, 1510, 1530, 1702, 1731, 1802, 1803, 1901, 1930, 2004, 2132, 2142, 2143, 2144, 2145, 2146, 2170, 2172, 2224, 2241, 2251, 2252, 2261, 2263, 2266, 2335, 2345, 2347, 2348, 2365, 2380, 2440, 2450, 2460, 2470, 2471, 2760, 2870],
  'sol': ['', '-Sol'],
  'name': {
    'advalgo': 'Advanced Algorithms for Optimization',
    'concurrent': 'Concurrent Systems',
    'autolin': 'Automatique Linéaire',
    'mmc': 'Mécanique des Milieux Continus',
    'paradigms': 'Programming paradigms',
    'secu': 'Computer System Security',
    'embedded': 'Software and operating systems for embedded systems',
    'algo': 'Algorithmique et structures de données',
    'gameth': 'Game Theory',
    'chimieorga': 'Chimie Organique',
    'hydrau': None,
    'conv': 'Convert. électrom',
    'ia': 'Artificial Intelligence',
    'oz': 'Computer language concepts',
    'strat': "Stratégies d'entreprises",
    'matstruct': 'Matériaux structuraux',
    'circmes': 'Circuits et Mesures',
    'infoth': 'Information theory and coding',
    'gpo': 'Gestion de production et des opérations',
    'edo': 'Equations différentielles ordinaires',
    'grh': 'Gestion des ressources humaines',
    'info': 'Informatique',
    'elec': 'Physique',
    'sigsys': 'Signaux & Systèmes',
    'mana': 'Management humain',
    'modstoch': 'Stochastic Modelling',
    'maintenance': 'Software Maintenance and Evolution',
    'numth': 'Number Theory',
    'database': 'Databases',
    'matrix': 'Matrix Theory',
    'physique': 'Physique',
    'dam': 'DAM',
    'calcu': 'Calculabilité',
    'philo': 'Philosophie',
    'coo': 'Conception orientée objet et gestion de données',
    'dispo': 'Dispositifs electroniques',
    'telecom': 'Télécommunications',
    'tdo': 'Théorie des organisations',
    'ca': "Complément d'analyse",
    'logique': 'Logique et structure discrètes',
    'ml': 'Machine Learning',
    'os': 'Systèmes informatiques',
    'progmeth': 'Programming methods',
    'cl': 'Computational Linguistics',
    'mcp': 'Méthodes de conception de programmes',
    'opticombi': 'Integer Programming and Combinatorial Optimisation',
    'network': 'Réseaux informatiques',
    'nlp': 'Nonlinear programming',
    'ananum': 'Analyse numérique',
    'math': 'Mathématiques',
    'devmeth': 'Software Engineering: Development Methods',
    'stat': 'Probability and statistics',
    'projet': 'Projet',
    'geomat': 'Géomatériaux',
    'prostoch': 'Stochastic Processes',
    'circ': 'Circuits électroniques',
    'distributed': 'Languages and algorithms for distributed applications',
    'archi': 'Architecture and performance of computer systems',
    'crypto': 'Cryptography',
    'chimie': 'Chimie',
    'cloud': 'Cloud Computing',
    'advSecu': 'Secured systems engineering',
    'opti': "Modèles et méthodes d'optimisation",
    'constraint': 'Constraint Programming',
    'meca': 'Physique',
    'numGeo': 'Numerical Geometry',
    'translators': 'Languages and Translators',
    'magn': 'Electromagnetisme',
    'eco': "Économie de l'entreprise",
    'methodnum': 'Méthodes Numériques'
  },
  'minmaj': ['All', 'Mineure', 'Majeure'],
  'month': ['Janvier', 'Mars', 'Juin', 'Août', 'Novembre'],
  'option': {
    'ECGE': 'INFO',
    'MECA': 'MECA',
    'LSMG': 'INFO',
    'AUCE': 'GCE',
    'MAPR': 'FYKI',
    'FSAB': 'Tronc commun',
    'INMA': 'MAP',
    'INGI': 'INFO',
    'BIR': 'INFO',
    'SINF': 'INFO',
    'MAT': 'MAP',
    'ELEC': 'ELEC'
  },
  'type': {
    'test': 'Interros',
    'errata': 'Books',
    'notes': 'CM',
    'mcq': 'APE',
    'exam': 'Examens',
    'formulaire': 'Formulaires',
    'exercises': 'APE',
    'summary': 'Synthèses'
  },
  'year': [2006, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]
}

你有什么建议我用这种丑陋的方式来创建这个字典(它有效,因为我知道我需要的数据在哪里,但如果发生一些重映射可能不好)?

0 个答案:

没有答案