Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. d = {}
  2. d['fruits'] = {}
  3. d['fruits']['orange'] = {}
  4. d['fruits']['orange']['price'] = 5.75
  5.  
  6. my_keystring = "fruits.orange.price"
  7.  
  8. def get_value(keystring, dictionary):
  9. amountkeys = keystring.count('.')+1
  10. lastfoundindex = 0
  11. counter = 0
  12.  
  13. while counter < amountkeys:
  14. if counter == 0:
  15. value = dictionary[keystring[lastfoundindex:keystring.find('.')]]
  16.  
  17. elif counter == amountkeys - 1:
  18. value = value[keystring[lastfoundindex:]]
  19. break
  20. else:
  21. value = value[keystring[lastfoundindex:keystring.find('.',lastfoundindex)]]
  22.  
  23. lastfoundindex = keystring.find('.',lastfoundindex)+1
  24. counter += 1
  25.  
  26. return value
  27.  
  28. print(F"Demo get_value(): {get_value(my_keystring, d)}nThis is the PRICE of ORANGE in FRUITS.")
  29.  
  30. def set_value(keystring, dictionary, new_value):
  31. amountkeys = keystring.count('.')+1
  32. lastfoundindex = 0
  33. counter = 0
  34.  
  35. while counter < amountkeys:
  36. if counter == 0:
  37. value = dictionary[keystring[lastfoundindex:keystring.find('.')]]
  38.  
  39. elif counter == amountkeys - 1:
  40. value[keystring[lastfoundindex:]] = new_value
  41. break
  42. else:
  43. value = value[keystring[lastfoundindex:keystring.find('.',lastfoundindex)]]
  44.  
  45. lastfoundindex = keystring.find('.',lastfoundindex)+1
  46. counter += 1
  47.  
  48. value = new_value
  49. return value
  50.  
  51. print(F"Demo set_value(): {set_value(my_keystring, d, 1.25)}nThis is the NEW PRICE of ORANGE in FRUITS!")
  52.  
  53. def del_entry(keystring, dictionary):
  54. amountkeys = keystring.count('.')+1
  55. lastfoundindex = 0
  56. counter = 0
  57.  
  58. while counter < amountkeys:
  59. if counter == 0:
  60. value = dictionary[keystring[lastfoundindex:keystring.find('.')]]
  61.  
  62. elif counter == amountkeys - 1:
  63. del value[keystring[lastfoundindex:]]
  64. break
  65. else:
  66. value = value[keystring[lastfoundindex:keystring.find('.',lastfoundindex)]]
  67.  
  68. lastfoundindex = keystring.find('.',lastfoundindex)+1
  69. counter += 1
  70.  
  71. del_entry('fruits.orange.price',d)
  72. print(F"Demo del_entry(): fruits.orange.price is now {get_value('fruits.orange', d)}!")
  73.  
  74.  
  75. def add_entry(keystring, dictionary, entry_name, entry_value = None):
  76. amountkeys = keystring.count('.')+1
  77. lastfoundindex = 0
  78. counter = 0
  79.  
  80. while counter < amountkeys:
  81. if counter == 0:
  82. value = dictionary[keystring[lastfoundindex:keystring.find('.')]]
  83.  
  84. elif counter == amountkeys - 1:
  85. value[keystring[lastfoundindex:]][entry_name] = entry_value
  86. break
  87. else:
  88. value = value[keystring[lastfoundindex:keystring.find('.',lastfoundindex)]]
  89.  
  90. lastfoundindex = keystring.find('.',lastfoundindex)+1
  91. counter += 1
  92.  
  93. add_entry('fruits.orange', d, 'in_stock', True)
  94. print(F"Demo add_entry()! Added a new entry called in_stock to fruits.orange, it's value is: {get_value('fruits.orange.in_stock',d)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement