Test if include/import has been already done - julia

I have the following include/import in my "main.jl" file:
include("Global.jl")
import .Global
As importing this module takes some time and it is always the same on every execution, I would like test in advance if .Global exists so I can bypass the include/import.
The idea is that I can edit all changes in my text editor and use the interactive console to reload the whole program but waiving that step if it is already there.

import X is already basically a no-op if X has already been imported.
The problem is when you do the include you are defining a new module also called X so import tries and loads the new one.
I suggest thus converting your module into a package,
adding it to your environment, then just doing import X

You can use isdefined(Main, :ModuleName) (don't forget the colon).
if !isdefined(Main, :Global)
include("Global.jl")
import .Global
end
For the same question when you are using a package named "MyPackage":
if !isdefined(Main, :MyPackage)
using MyPackage
end

Related

How to see all the code from the Example on pub.dev/packages

When looking at the Examples in the pub.dev/packages, the code for some contained with one page, while the more advanced ones are not showing all the code. For example, in the firebase_auth package, the Example shows the main.dart file, while the most important code is probably at the other two imported files:
import './register_page.dart';
import './signin_page.dart';
My question - How do I see the two files?
Thanks, Gal.
You can use this package by call some function and use IDE to jump to definition for example command + click in VS code.
Or you can open library file in directory flutter_dir/.pub-cache/hosted/pub.dartlang.org/name_of_lib/lib

Why use `exec_statement` when writing PyInstaller hooks?

In the PyInstaller docs they demonstrate the use of eval_statement() and exec_statement() which call eval() or exec() in a new instance of Python. But they don't say why you would want to run your code in a separate instance.
For example, why couldn't their example of:
from PyInstaller.utils.hooks import exec_statement
mpl_data_dir = exec_statement(
"import matplotlib; print(matplotlib._get_data_path())"
)
datas = [ (mpl_data_dir, "") ]
not just be:
import matplotlib
datas = [(matplotlib._get_data_path(), "")]
I've tried doing this with my own library and it doesn't seem to do it any harm. So why the extra complexity? Why do all the other hooks included in PyInstaller use the 1st method?
All the hooks use the first method in case of path and environment manipulations. What happens if matplotlib changes sys.path or some other environmental manipulation that could mess with the build process? Using a separate python instance means this won't happen.

julia counterpart for "from module import some_func" in python

I find using Module in julia is equivalent to from Module import * in python. Is there any way to import a single function or variable from a module.
You can import e.g. a single function with
using Module.func
If you also want to be able to extend the function with new methods, use instead
import Module.func
If you want to import several named functions and variables from the same module, you can e.g. use one of
using Module: func1, func2
import Module: func1, func2
All of these forms allow to import definitions from Module regardless of whether they are exported there, unlike using Module, which only imports exported definitions.
Also keep in mind that it is really values that are imported, not variables, so if you try to assign to an imported variable, you will not see a change in the originating module (and you will get a warning).
If you have a module Foo with a function bar (or something else specific you wan to import) you can do:
import Foo: bar
This brings only bar into scope.

Sikuli - NameError: global name 'openApp' is not defined

I'm calling a sikuli function, inside Sikuli IDE, but I get this error "NameError: global name 'openApp' is not defined"...
If I try to do openApp('calc') in a new Sikuli blank file, it works, but if I use in another .sikuli file like:
def sample():
import myLib
# my Lib is .py file that I've created and put it on sikuli-script.jar
var = somevalue
myLib.myFunction(something)
openApp('calc')
I get the error with "openApp" and other sikuli funcions like "Key" (ex: Key.ENTER) too...
Hope I had explained that well
By default, Sikuli will insert a from sikuli import * into all main files. This error tends to occur when you import sikuli modules. If you are importing modules, you will need to add the import manually. See the documentation for more advice.
If your tests are in the same folder basically you can do,
import testName
reload(testName)
from testName import *
This will import your test and execute it's content.
testName should be name of the file without .sikuli extension
I ran into a similar problem was solved by putting from sikuli import * at the first line of any file you are importing. I hope this helps!
I only mentioned this, because with the imported files, I had the greatest overall success with this one, and it became habit to make this the first line.

How can you programmatically tell the CPython interpreter to enter interactive mode when done?

If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not been given -i? The obvious use case is in debugging by interactively inspecting the state when an exceptional condition has occurred.
You want the code module.
#!/usr/bin/env python
import code
code.interact("Enter Here")
Set the PYTHONINSPECT environment variable. This can also be done in the script itself:
import os
os.environ["PYTHONINSPECT"] = "1"
For debugging unexpected exceptions, you could also use this nice recipe http://code.activestate.com/recipes/65287/
The recipe metioned in the other answer using sys.excepthook, sounds like what you want. Otherwise, you could run code.interact on program exit:
import code
import sys
sys.exitfunc = code.interact
The best way to do this that I know of is:
from IPython import embed
embed()
which allows access to variables in the current scope and brings you the full power of IPython.

Resources