from IPython.core.magic import register_cell_magic
import bohrium

@bohrium.replace_numpy
def execute(__code):
    exec(__code, globals(), locals())
    __excludes = set(["__excludes", "__code", "np", "bohrium"])

    try:
        # Python 2.x
        for key, value in locals().iteritems():
            if key not in __excludes:
                globals()[key] = value
    except:
        # Python 3.x
        for key, value in locals().items():
            if key not in __excludes:
                globals()[key] = value


@register_cell_magic
def bohrium(line, cell):
    # Code must end with \n
    code = cell if cell.endswith("\n") else cell + "\n"
    execute(code)
    return

from IPython.core.magic import register_cell_magic

try:
    import bohrium

    have_bohrium = True

    @bohrium.replace_numpy
    def execute(__code):
        exec(__code, globals(), locals())
        __excludes = set(["__excludes", "__code", "np", "bohrium"])

        try:
            # Python 2.x
            for key, value in locals().iteritems():
                if key not in __excludes:
                    globals()[key] = value
        except:
            # Python 3.x
            for key, value in locals().items():
                if key not in __excludes:
                    globals()[key] = value
except ImportError:
    warning_shown = False # Warning about missin bohrium has been shown

    def execute(__code):
        global warning_shown

        if not warning_shown:
            print("WARNING: Module bohrium could not be imported.\n"
                  "         The magic command '%%bohrium' will have no effect.")
            warning_shown = True

        exec(__code, globals())


@register_cell_magic
def bohrium(line, cell):
    # Code must end with \n
    code = cell if cell.endswith("\n") else cell + "\n"
    execute(code)
    return

Disable the effect of %%bohrium if bohrium cannot be imported.
Disable the effect of %%bohrium if bohrium cannot be imported.

The first time the user attempts to use %%bohrium a warning will be
shown. From this point onwards all %%bohrium statements will have no
effect silently.

  from IPython.core.magic import register_cell_magic
- import bohrium
  
+ try:
+     import bohrium
- @bohrium.replace_numpy
- def execute(__code):
-     exec(__code, globals(), locals())
-     __excludes = set(["__excludes", "__code", "np", "bohrium"])
  
+     have_bohrium = True
+ 
+     @bohrium.replace_numpy
+     def execute(__code):
+         exec(__code, globals(), locals())
+         __excludes = set(["__excludes", "__code", "np", "bohrium"])
+ 
-     try:
+         try:
-         # Python 2.x
+             # Python 2.x
-         for key, value in locals().iteritems():
+             for key, value in locals().iteritems():
-             if key not in __excludes:
+                 if key not in __excludes:
-                 globals()[key] = value
+                     globals()[key] = value
-     except:
+         except:
-         # Python 3.x
+             # Python 3.x
-         for key, value in locals().items():
+             for key, value in locals().items():
-             if key not in __excludes:
+                 if key not in __excludes:
-                 globals()[key] = value
+                     globals()[key] = value
+ except ImportError:
+     warning_shown = False # Warning about missin bohrium has been shown
+ 
+     def execute(__code):
+         global warning_shown
+ 
+         if not warning_shown:
+             print("WARNING: Module bohrium could not be imported.\n"
+                   "         The magic command '%%bohrium' will have no effect.")
+             warning_shown = True
+ 
+         exec(__code, globals())
  
  
  @register_cell_magic
  def bohrium(line, cell):
      # Code must end with \n
      code = cell if cell.endswith("\n") else cell + "\n"
      execute(code)
      return
  
Disable the effect of %%bohrium if bohrium cannot be imported.
## Code Before:
from IPython.core.magic import register_cell_magic
import bohrium

@bohrium.replace_numpy
def execute(__code):
    exec(__code, globals(), locals())
    __excludes = set(["__excludes", "__code", "np", "bohrium"])

    try:
        # Python 2.x
        for key, value in locals().iteritems():
            if key not in __excludes:
                globals()[key] = value
    except:
        # Python 3.x
        for key, value in locals().items():
            if key not in __excludes:
                globals()[key] = value


@register_cell_magic
def bohrium(line, cell):
    # Code must end with \n
    code = cell if cell.endswith("\n") else cell + "\n"
    execute(code)
    return

## Instruction:
Disable the effect of %%bohrium if bohrium cannot be imported.
## Code After:
from IPython.core.magic import register_cell_magic

try:
    import bohrium

    have_bohrium = True

    @bohrium.replace_numpy
    def execute(__code):
        exec(__code, globals(), locals())
        __excludes = set(["__excludes", "__code", "np", "bohrium"])

        try:
            # Python 2.x
            for key, value in locals().iteritems():
                if key not in __excludes:
                    globals()[key] = value
        except:
            # Python 3.x
            for key, value in locals().items():
                if key not in __excludes:
                    globals()[key] = value
except ImportError:
    warning_shown = False # Warning about missin bohrium has been shown

    def execute(__code):
        global warning_shown

        if not warning_shown:
            print("WARNING: Module bohrium could not be imported.\n"
                  "         The magic command '%%bohrium' will have no effect.")
            warning_shown = True

        exec(__code, globals())


@register_cell_magic
def bohrium(line, cell):
    # Code must end with \n
    code = cell if cell.endswith("\n") else cell + "\n"
    execute(code)
    return
