Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. class Namespaces:
  2.     def __init__(self):
  3.         self.namespace_set = set()
  4.         self.general_dictionary = {'global': ('None', {}, set())}  
  5.  
  6.     def create_namespase_into_parent(self, dictionary_name, namespace_name, parent_namespace_name):
  7.         for key, value in dictionary_name.items():
  8.             if key == parent_namespace_name:
  9.                 dictionary_of_namespases = value[1]
  10.                 namespace_set.add(namespace_name)
  11.                 dictionary_of_namespases[namespace_name]=(parent_namespace_name,{},set())
  12.             else:
  13.                 dictionary_of_namespases = value[1]
  14.                 self.create_namespase_into_parent(dictionary_of_namespases, namespace_name, parent_namespace_name)
  15.  
  16.     def add_to_namespace_new_variable(self, dictionary_name, namespace_name, variable_name):
  17.         for key, value in dictionary_name.items():
  18.             if key == namespace_name:
  19.                 set_of_variables = value[2]
  20.                 set_of_variables.add(variable_name)
  21.             else:
  22.                 dictionary_of_namespases = value[1]
  23.                 self.add_to_namespace_new_variable(dictionary_of_namespases, namespace_name, variable_name)
  24.            
  25.     def get_variable_from_namespace(self, dictionary, namespace_name, variable_name):
  26.         for key, value in dictionary.items():
  27.             if key != namespace_name:
  28.                 dictionary_of_namespases = value[1]
  29.                 self.get_variable_from_namespace(dictionary_of_namespases, namespace_name, variable_name)
  30.             else:
  31.                 set_of_variables = value[2]
  32.                 if variable_name in set_of_variables:
  33.                     print(namespace_name)
  34.                 else:
  35.                     name_of_parent = value[0]
  36.                     if (name_of_parent == 'None'):
  37.                         print(None)
  38.                     else:
  39.                         self.get_variable_from_namespace(self.general_dictionary, name_of_parent, variable_name)
  40.        
  41. x = Namespaces()  
  42. x.namespace_set.add('global')                  
  43. number_of_requests = int(input())
  44. for item in range(number_of_requests):
  45.     command, namespace, argument = input().split()
  46.  
  47.     if command == 'create':
  48.         namespace_set = x.namespace_set
  49.         if namespace not in namespace_set:
  50.             x.create_namespase_into_parent(x.general_dictionary, namespace, argument)
  51.            
  52.     if command == 'add':
  53.         x.add_to_namespace_new_variable(x.general_dictionary, namespace, argument)
  54.                
  55.     if command == 'get':
  56.         if namespace in x.namespace_set:
  57.             x.get_variable_from_namespace(x.general_dictionary, namespace, argument)
  58.         else:
  59.             print(None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement