How do I correctly reference other file from start.py? - jupyter

Here is what my current start.py looks like:
# import init
exec(open("init.py").read())
%matplotlib widget
set_max_notebook_memory(6*1024)
init.py contains some functions I have introduced to make my life easier.
When I open notebook, init.py gets imported, because I can use the functions I declared in it. However, neither %matplotlib widget nor set_max_notebook_memory(6*1024) do get executed.
What should I change for this to work?

Related

Is it a bad practice to export all different widgets inside a single widget then import these widgets by importing this single file wherever we need?

If I create a file like widgets.dart and put the exportation of all my widgets inside that like below:
export 'custom_app_bar.dart';
export 'content_header.dart';
export 'vertical_icon_button.dart';
Then import this file wherever I needed one of these widget inside another file like below:
import './widgets/widgets.dart';
Is it a bad practice and would have overhead process/memory issues?
I am also thinking about put more codes in the same file, for example define 3 different MobileSizeHomeScreen() , TabletSizeHomeScreen(), DesktopSizeHomeScreen() classes/widgets inside one file like home_screen.dart instead of creating 3 different mobile, tablet, desktop files, but don't know doing such a things would cause performance or maintenance issues while the project gets larger and more complexed or not?
As far as I know, that isn't a bad practice. However, Dart presents us with part and part of to do that kind of stuff. You can read more about Dart effective usage here.
Suppose all of those four files are within the same directory.
In the above's case, ./widget/widgets.dart would have this line (above the class declaration):
part 'custom_app_bar.dart';
part 'content_header.dart';
part 'vertical_icon_button.dart';
and in each of those three, they will have this line that indicates, they are part of the ./widget/widgets.dart:
part of 'widgets.dart';
Then, when you need one of the widgets, you only need to import widgets.dart
I see some of the other Dart developers also use the export stuff, so it is a matter of preference. Personally, I would prefer what Dart recommends, which is using part and part of.

matlab: cannot import package

Probably a basic mistake, but the cause is eluding me. I am trying to import a package, but I get an error saying it cannot be found or imported.
First I set the current directory to the parent directory of the package, and this does not work.
Second, the docs say that the parent folder of the package must be added to the matlab path. I tried this, and still no luck.
It is not due to using plot as the package name as I get the same error when trying to import analysis.
What I can do is to import using: import plot.* or import analyse.* and then go on to use the functions in the packages, but I want to use the namespaces (i.e. not use .*).
Edit
I'm having this problem on both versions I have installed: 2015b and 2016a.
The answer is that, somewhat counterintuitively, you don't need to call import at all. The docs state that
The parent of the top-level package folder must be on the MATLAB path.
Which is what your addpath(pwd) does and then state that (emphasis is mine):
All references to packages, functions, and classes in the package must
use the package name prefix, unless you import the package.
Meaning at this stage you should be able to call
analyse.testFunc
If you were to import analyse.testFunc you would then be able to call testFunc without prefacing it with the namespace but since you want to retain the namespace the answer is to not call import at all.

Calling functions in a nested package

I'm trying out organising my matlab code into packages, but having to use fully qualified names in nested package functions is killing me.
Say I have a package called +myPack that looks like this:
+myPack
bar.m
baz.m
The bar function might look like
function bar()
myPack.baz()
end
This is all fine and logical. However, +myPack is a componant that will be reused in multiple other packages. Lets say one looks like this:
+mySuperPack
foo.m
+myPack
bar.m
baz.m
This time, foo calls bar, which in turn calls baz. However, the original code for bar will fail because I have not used the fully qualified name
mySuperPack.myPack.baz()
Obviously +myPack doesn't know which super pack it is in, so I can't do that.
This also stops you from being able to use static methods in classes that are in packages; the class has to know which package it is in to call its own static methods, which seems crazy.
Is there any way to use nested packages like this, or am I doing packages totally wrong?
Instead of writing
mySuperPack.myPack.baz()
you can write
import mySuperPack.myPack.*
baz()
You only need to write the import statement once. Unfortunately (and this is one of the few things that really bugs me with MATLAB) import only imports into the current workspace, so you need to write it once per function/method.
I really wish you could just write it once at the top of a file, and have it import into all functions in the file, or at the top of a class and have it import into all methods of the class, but there you go. At least it's better than always needing fully qualified names everywhere.
PS on the issue of static methods: although you might typically call them as ClassName.staticMethodName, or pkgName.ClassName.staticMethodName, if you have an object obj of that class, you can also call it using obj.staticMethodName. Obviously that's not always relevant, but if you're calling a static method from within a normal method it can be convenient, as you don't need to mention (or import) the package.

catalyst require lib across whole application

So I have a .lib file with some of my own subroutines in it that I would like to make available to the entire Catalyst app. Right now I require the file in lib/myapp.pm and I have no issues. However, whenever I try to call that subroutine in a controller, I get this error:
"Undefined subroutine &myapp::Controller::Root::my_sub called at
/home/user/myapp/lib/myapp/Controller/Root.pm line 35, <DATA> line 1003."
If I require the file I want to require in the controller, that gives me no issues. However, I would prefer to have to only load it in one place for the whole application if this was possible. Also, if I require the file within the controller, does that mean that this file is being loaded every time a request is made? (I'm using mod_perl if that makes any difference). I'd like to make this as efficient in terms of the file being loaded once for the whole app and any requests, but also loaded only in one place just for the sake of clean code. Thanks!
use myapp;
is basically
BEGIN {
require myapp;
import myapp;
}
require myapp; executes myapp.pm if it hasn't already been executed. In other words, no matter how many times you do use myapp; in a process, the file will only be executed ("loaded") once.
import myapp; calls myapp::import() if it exists in order to export stuff. Assuming myapp exports my_sub, this is why your code isn't working.
You have two options.
Call the mysub in the myapp package: myapp::my_sub(...).
Use use myapp; to create a local name for my_sub in every package you call my_sub so you can call it using just my_sub(...). (This assusmes myapp exports my_sub.)
The command use myapp; will only load your myapp.pm file once, even when called multiple times. But each time it will call the import routine makes my_sub() available (Assuming you export it using Exporter or something else) without having to write myapp::my_sub().

Is there a way to import packages from your current project into a worksheet

I like worksheets as an alternative to the REPL, but I keep implementing functions in the worksheet and then copying them back into the actual project. How do I import a package from the current project so that I can call those functions in the worksheet?
There is nothing special to do. Just do regular import of the packages you need in the worksheet. For instance:
import com.acme.myproject._
The worksheet is a totally regular source file, that simply gets evaluated in a fancy manner on save.