How to use initialize_globals method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...12This is how a typical user interface initializes the globals::13 sage: ui_globals = globals() # or wherever the user interface stores its globals14 sage: from sage import all_cmdline # or all_notebook15 sage: from sage.repl.user_globals import initialize_globals16 sage: _ = initialize_globals(all_cmdline, ui_globals)17Now everything which was imported in ``all_cmdline`` is available as a18global::19 sage: from sage.repl.user_globals import get_global, set_global20 sage: get_global("Matrix")21 <sage.matrix.constructor.MatrixFactory object at ...>22This is exactly the same::23 sage: ui_globals["Matrix"]24 <sage.matrix.constructor.MatrixFactory object at ...>25We inject a global::26 sage: set_global("myvar", "Hello World!")27 sage: get_global("myvar")28 'Hello World!'29If we set up things correctly, this new variable is now actually30available as global::31 sage: myvar32 'Hello World!'33AUTHORS:34- Jeroen Demeyer (2015-03-30): initial version (:trac:`12446`)35"""36#*****************************************************************************37# Copyright (C) 2015 Jeroen Demeyer <>38#39# This program is free software: you can redistribute it and/or modify40# it under the terms of the GNU General Public License as published by41# the Free Software Foundation, either version 2 of the License, or42# (at your option) any later version.43#*****************************************************************************45user_globals = None46def _check():47 """48 Raise ``RuntimeError`` if ``user_globals`` has not been initialized.49 EXAMPLES::50 sage: import sage.repl.user_globals51 sage: sage.repl.user_globals._check()52 sage: sage.repl.user_globals.user_globals = None53 sage: sage.repl.user_globals._check()54 Traceback (most recent call last):55 ...56 RuntimeError: the user-space globals dictionary has not been initialized...57 """58 if user_globals is None:59 raise RuntimeError(60 "the user-space globals dictionary has not been initialized. "61 "Use initialize_globals() or set_globals() or use a different "62 "function which doesn't need these globals")63def get_globals():64 """65 Return the dictionary of all user globals.66 EXAMPLES::67 sage: from sage.repl.user_globals import get_globals, initialize_globals68 sage: initialize_globals(sage.all)69 sage: get_globals()["Matrix"]70 <sage.matrix.constructor.MatrixFactory object at ...>71 """72 _check()73 return user_globals74def set_globals(g):75 """76 Set the dictionary of all user globals to ``g``.77 INPUT:78 - ``g`` -- a dictionary. Typically, this will be some dictionary79 given by the user interface or just ``globals()``.80 EXAMPLES::81 sage: from sage.repl.user_globals import get_globals, set_globals82 sage: my_dict = dict()83 sage: set_globals(my_dict)84 sage: my_dict is get_globals()85 True86 """87 global user_globals88 user_globals = g89def initialize_globals(all, g=None):90 """91 Set the user globals dictionary to ``g`` and assign everything92 which was imported in module ``all`` as global.93 INPUT:94 - ``all`` -- a module whose globals will be injected95 - ``g`` -- a dictionary, see :func:`set_globals`. If this is96 ``None``, keep the current globals dictionary.97 EXAMPLES::98 sage: my_globs = {"foo": "bar"}99 sage: from sage.repl.user_globals import initialize_globals100 sage: initialize_globals(sage.all, my_globs)101 sage: my_globs["foo"]102 'bar'103 sage: my_globs["Matrix"]104 <sage.matrix.constructor.MatrixFactory object at ...>105 Remove ``Matrix`` from the globals and initialize again without106 changing the dictionary::107 sage: del my_globs["Matrix"]108 sage: initialize_globals(sage.all)109 sage: my_globs["Matrix"]110 <sage.matrix.constructor.MatrixFactory object at ...>111 """112 if g is not None:113 set_globals(g)114 for key in dir(all):115 if key[0] != '_':116 user_globals[key] = getattr(all, key)117def get_global(name):118 """119 Return the value of global variable ``name``. Raise ``NameError``120 if there is no such global variable.121 INPUT:122 - ``name`` -- a string representing a variable name...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?