def search(self, key, value=None): """ search("cloudmesh.cloud.*.cm.active", True) :param key: :param value: :return: """ flat = FlatDict(self.data, sep=".") result = flat.search(key, value) return result
def test_002(self): HEADING("FlatDict") f = FlatDict(self.d) pprint(f.dict) pprint(f.__dict__) print(f['user']) print(f['extra__created']) print(f.user) print(f.extra__created) f.user = '******' assert f.user == 'GREGOR' assert f['extra__minDisk'] == 40
def edit(key, caps=True, show=False): global gui_enabled if not gui_enabled: Console.Warning( "Cloudmesh Gui not supported, can not find tkinter") return "" config = Config() entry = dict(FlatDict(config[key], sep='.')) layout = [[gui.Text(f'Cloudmesh Configuration Editor: {key}')]] length = 1 for _key, _value in entry.items(): length = max(length, len(_key)) length = length + 3 for _key, _value in entry.items(): if caps: label = _key.capitalize() else: label = _key secrets = Config.secrets() if _key in secrets and not show: field = [ gui.Text(label, size=(length, 1)), gui.InputText(key=f"{key}.{_key}", password_char="*", default_text=_value) ] else: field = [ gui.Text(label, size=(length, 1)), gui.InputText(key=f"{key}.{_key}", default_text=_value) ] layout.append(field) layout.append([gui.Submit(), gui.Cancel()]) window = gui.Window('Cloudmesh Configuration Editor', layout, background_color="white") event, values = window.Read() window.Close() if event == "Submit": for _key, _value in values.items(): config[_key] = _value if show: Console.ok(f"{_key}={_value}") config.save() else: print(event)
def __init__(self): self.data = { "user": "******", "test": { "res": "success", "time": "30s" } } self.flat = FlatDict(self.data) self.dotd = dotdict(self.data)
def sampleFlatdict(self): """ function to use FlatDict """ from cloudmesh.common.FlatDict import FlatDict data = { 'name': 'Prateek', 'address': {'city': 'Naples','state': 'FL' } } flat = FlatDict(data, sep=':') print(flat)
def flatdict(self): d = { 'name': 'shihui', 'school': 'Indiana University', 'additional': { 'class': 'cloud computing', 'semester': 'second' } } flat = FlatDict(d, sep=".") print(flat)
def demo_flatdict(self): test_dict = { "country": "UNITED STATES OF AMERICA", "attributes": { "Language": "English", "Capital": "Washington D.C.", "Currency": "USD" } } print(test_dict) flat_list = FlatDict(test_dict, sep=".") banner("Flattened list for test_dict") print(flat_list)
def doit(self): data = { 'name': 'Falconi', 'address': { 'city': 'Portland', 'state': 'OR' } } flat = FlatDict(data) print(f"My name is {flat.name}") print(f"The City I live in is {flat.address__city}") print(f"The State I live in is {flat.address__state}")
# E.Cloudmesh.Common.3 # Develop a program that demonstrate the use of FlatDict. from cloudmesh.common.FlatDict import FlatDict dict = { "name": "Zhi", "education": "Ph.D. in Public Health", "address": { "University": "Indiana University - Bloomington", "city": "Bloomington", "zip": "47404" } } flattened = FlatDict(dict) print(flattened)
# import flatdict, note F & D from cloudmesh.common.FlatDict import FlatDict # create a simple nested dict sample_dict = { "breed": "Yorkie", "Weight": 12, "color": "silver", "attribute": { "plus": "cute", "minus": "not interested in food", "favorite": { "treat": "chicken breast", "toy": "lamb chop" } } } # transform dict using FlatDict sample_dict = FlatDict(sample_dict) print(sample_dict)
# fa19-516-170 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict from cloudmesh.common.Printer import Printer student = { "name": "Yanting", "address": { "city": "Bloomington", "state": "Indiana", "country": "United States" } } flatStudent = FlatDict(student) print(flatStudent) data = [student] table = Printer.flatwrite( data, sort_keys=["name"], order=["name", "address.city", "address.state", "address.country"], header=["Name", "City", "State", "Country"], output='table') print(table)
# fa19-516-160 # E.Cloudmesh.Common.3 # Task : Develop a program that demonstrates the use of FlatDict. # Imports from cloudmesh.common.FlatDict import FlatDict # import FlatDict # Sample data data = [{ "name": "Shreyans", "course": "e516", "address": { "city": "Bloomington", "state": "IN" } }, { "name": "Ronak", "course": "e260", "address": { "city": "Indianapolis", "state": "IL" } }] # Conversion to FlatDict and Print data flat_data = FlatDict(data[0]) print(type(flat_data)) print(flat_data) #Code reference Introduction to python : Gregor von Laszewski.: section 6.4.2
print("data.ElementOne=" + data.ElementOne) print("data[\"ElementOne\"]=" + data["ElementOne"]) if __name__ == "__main__": StopWatch.start('script') CC = CloudCommon({ "ElementOne": "One", "ElementTwo": "Two", "ElementThree": { "SubElementOne": "Three", "SubElementTwo": "Two" } }) # Shows usage of Flatdict (E.Cloudmesh.Common.3) flat = FlatDict(CC.getItems(), sep='.') # each banner completes part of 1.a banner("Normal Dictionary") print(CC.getItems()) banner("Flat Dictionary") print(flat) CC.PrintItemsInDotDict() banner("Usage of VERBOSE to show info about my dict") variables = Variables() variables['debug'] = True variables['trace'] = True variables['verbose'] = 10 VERBOSE(CC.getItems()) banner("Execute a shell command for pip") res = Shell.pip() print(res)
''' E.Cloudmesh.Common.3 Develop a program that demonstrates the use of FlatDict. ''' from cloudmesh.common.FlatDict import FlatDict data = {'name': 'Xin', 'address': {'city': 'Indy', 'state': 'IN'}} flat = FlatDict(data, sep=',') print(flat)
from cloudmesh.common.FlatDict import FlatDict data = { 'name': { 'fname': 'Jonathan', 'lname': 'Beckford' }, 'address': { 'location': { 'city': 'Atlanta', 'zip': '30041' }, 'state': 'GA' }, } flat = FlatDict(data, sep='.') print(flat)
# fa19-516-163 E.Cloudmesh.Common.3 # Cloudmesh imports from cloudmesh.common.FlatDict import FlatDict from cloudmesh.common.util import banner import urllib.request import json def fetchJson(url: str) -> str: json_url = urllib.request.urlopen(url) data = json.loads(json_url.read()) return data # Fetch some JSON openapi = fetchJson("https://itpeople-api.apps.iu.edu/openapi.json") dd = FlatDict(openapi) print(dd) # Present some info about it banner("API Title") print(dd.info__title) banner("API Description") print(dd.info__description)
if __name__ == "__main__": d = { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } } banner( "Converting input dictionary to a Flat Dictionary and printing keys: ") d1 = FlatDict(d) for i in d1.keys(): print(i, " : ", d1[i])
# fa19-516-153 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict from cloudmesh.common.debug import VERBOSE data = {'hello': 'dictionary'} data['hello2'] = { 'tester': 'test' } VERBOSE(data) flat = FlatDict(data) VERBOSE(flat)
# fa19-516-172: Cloudmesh Common # E.Cloudmesh.Common.3 # Develop a program that demonstrates the use of FlatDict . from cloudmesh.common.FlatDict import FlatDict data = {"name": "Nayeem", "address": {"city": "Bloomington", "state": "IN"}} flat = FlatDict(data, sep=".") print(flat)
# -*- coding: utf-8 -*- """ E.Cloudmesh.Common.3 fa19-516-168 Example of demonstrates the use of FlatDict # FlatDict flatten out dictionaries that contain dicts within dicts """ from cloudmesh.common.FlatDict import FlatDict city = { "cityid": "DFW", "city": { "name": "Dallas", "stateID": "TX", "State": "Texas" } } flat = FlatDict(city, sep=".") print("Print city dict - \n", city, "\n") print("pring flat city dict using .sep -\n", flat)
# fa19-516-000 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict test_dict = { "username": "******", "classes": { "cloud_computing": "E516", "data_mining": "B565", "machine_learning": "B555" } } print(FlatDict(test_dict, sep="."))
def run(self): newobj = FlatDict(self.obj, sep=".") VERBOSE(f"I flattened {newobj}")
# sp20-516-250 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict store = {"name": "Amazon", "employees": {"type1": "hourly", "type2": "salary"}} flat = FlatDict(store, sep=".") print(flat)
# -*- coding: utf-8 -*- """ Created on Wed Sep 18 22:42:12 2019 @author: Siddhesh """ from cloudmesh.common.FlatDict import FlatDict data = { "name": "Siddhesh", "address": { "city": "Bloomington", "state": "Indiana" } } data = FlatDict(data) print("Using FlatDict") print(data)
# fa20-516-223 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict from cloudmesh.common.Shell import Shell from cloudmesh.common.debug import VERBOSE dist = Shell.distribution() rahul_dist = {"name": "rahul", "dist": dist} VERBOSE(rahul_dist) # Convert the dict to flat distribution. flatdist = FlatDict(rahul_dist, sep=".") print(flatdist["dist.platform"])
#sp20-516-244 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict class ECC3: def __init__(self, choice, iterable=()): self.choice = choice self.__dict__.update(iterable) def run(self): stringy = 'One.' + str(self.choice) flattened = FlatDict(self.__dict__, sep='.') return flattened[stringy] if __name__ == '__main__': print('Let\'s add or subtract one from one') choice = input('Type either \'plus\' or \'minus\' below:\n') data = {'One': {'plus': 'two', 'minus': 'zero'}} flattened = FlatDict(data, sep='-') root = ECC3(choice, data) answer = root.run() print('Now we have', answer) print('That was fun!')
def run(self): stringy = 'One.' + str(self.choice) flattened = FlatDict(self.__dict__, sep='.') return flattened[stringy]
# E.Cloudmesh.Common.3 # Demostrate use of Flatdict from cloudmesh.common.FlatDict import FlatDict flatDictData = { "name": "Pratibha", "topic": "Cloudmesh API", "assignment": { "assignmentNumber": "Common.3", "assignmentTopic": "FlatDict Demo" } } flatDictData = FlatDict(flatDictData, ".") print(flatDictData)
# E.Cloudmesh.Common.3 ## Develop a program that demonstartes the use of Flatdict # E.Cloudmesh.Common.3 ## Develop a program that demonstartes the use of FlatDict from cloudmesh.common.FlatDict import FlatDict Jagadeesh = { "Name": "Jagadeesh", "Class": { "Course": "CloudComputing", "Instructor": "Gregor" } } Jagadeesh = FlatDict(Jagadeesh) print(Jagadeesh['Class__Course'])
#!/usr/bin/env python3 # Josh Goodman sp20-516-220 E.Cloudmesh.Common.3 from cloudmesh.common.FlatDict import FlatDict from pprint import pprint if __name__ == "__main__": gene = { "symbol": "gro", "proteins": { "symbol": "gro-PA" }, "transcripts": { "symbol": "gro-RA" } } gene = FlatDict(gene, sep='.') pprint(gene)