Ctrl-F11 runs current file. How do I run the "main" file? - pydev

I have several python files in my workspace. One is the file containing the main function. I.e main.py contains:
if __name__ == '__main__':
DoImportantStuff()
The other files are classes that are used by the DoImportantStuff() function. One class per file. What's the suggested way to run the main file when I'm editing the other classes? Ctrl-F11 only runs the currently open file and since I'm often editing the other classes that don't have a main function nothing will happen. If I switch over to the main file I can run the program. But do I really have to switch to the main file first?

Please refer to this question, in a nutshell you should override ctrl+F11 behavior

Related

In which directory are functions executed when using the Matlab Compiler?

I have a batch program written in Matlab, which will be deployed to a production system by packaging it using the Compiler Toolbox.
When starting the program, multiple config and data files will be loaded, where the first config file contains paths and filenames for the other files to be loaded. I have a logger in the form of a hidden global object, and its static functions allow to add to the log. Every line will be immediately written to file (Performance does not matter, but reliable logging does!).
I want to dynamically specify the location of the logfile in a text file, but I don't know where I should put that file. In the classic interpreter mode, the wd of the constructor within the loggers' classdef seems to be directory where the file is saved, but I don't really understand why.
What is a reliable way of finding a textfile in the same directory as my classdef in a Compiler-packaged program using relative paths?
Thanks!

How to import .txt file in Scala

Hi im new to programming, how do i import a .txt file? My code cant find the file, is there any specific directory it has to be put into?
My code:
object Zettel01 extends App {
import scala.io.Source
object Suchtest {
val gesch = Source.fromFile("DieUnendlicheGeschichte.txt").getLines()
for (w <- gesch) println(w)
}
}
I have tried different code but the problem is always the same, i cant find the .txt file...
Thanks in advance for any help
Flurry1337
Every Scala program that you run on your computer is ultimately a java process. This process will have a "working directory", just as every process on your computer does. By default, the working directory is the working directory of the process that started it, that is, the current directory of the shell or command-line interpreter at the time when you started your program.
Now, that means it is important to know how exactly you start your program. If you are using a command line and start your program in the fashion of java MyCoolProgram, then the current directory of the shell will become the working directory of the program. If you use an IDE like Eclipse or IntelliJ IDEA, those typically use the project folder of your IDE project as the working directory of the process that they start.
There is a simple way to find it out quickly: You can always print out the result of new java.io.File(".").getAbsolutePath(). This will print the full path to the working directory. For example, you can write a little Scala program like this:
object PrintWorkingDirectory extends App {
println(new java.io.File(".").getAbsolutePath())
}
and start it. On the console, you should find the full path of the program's working directory. If you put a file named "DieUnendlicheGeschichte.txt" in this directory, your program will find that file under exactly that file name.
Of course, you don't have to dump all your files into that one directory. You can make subdirectories in order to organize your files better. For example, you might put your file in a path like "resources/text/DieUnendlicheGeschichte.txt".
Finally, I would like to point out that there is also a different way to associate resource files with your program, and to load them. The idea is that you put the code (class files) as well as resources like texts, images, CSV files, XML files and the like into one big file. This would be a JAR file. You can then use ClassLoader to access resources inside the JAR file by a URL.
Explaining that process in detail is out of scope for this question; this is just dropping a couple of buzzwords that you (or other readers) can search for in case they want to look up a more elaborated process.
System.getProperty("user.dir") also tells you the working directory.

Custom batch file creation for building code generated from Simulink Models

How can I create a custom batch file for my code generated from Simulink model ?
I can see, if I edit and change my template make file from Configuration Parameter Dialog box, I can get the desired make file.
But I want a custom .bat file too, that calls this make file along with other commands.
I have some environment variable to set and run couple of scripts in .bat file, before compilation begins. Based on these outputs from script the code is to be compiled and linked.
Using Matlab Version: 2012b
Create a STF_wrap_make_cmd_hook that generates your desired modelname.bat file as shown in the example code
here (mathworks login necessary).
You will probably also need to write your own make_yourtarget.m file and edit the make command field shown in your screenshot to use that one instead of make_rtw.
Other hooks into the build process are described here, perhaps the 'before_make' will also be useful.

How can I add a custom package to the startup path in Dymola/Modelica?

I have a custom package that I find myself reusing repeatedly in Dymola models, and I'd like to put this package in a common directory that is automatically loaded whenever I start Dymola. My current strategy is to load the custom package when a model I'm working on is loaded and then save total. This is not elegant because the contents of the custom package end up saved in multiple locations across my hard drive, and if I change one of them, the changes are not reflected everywhere. I would like a more robust way of distributing this custom package to all of my models. Is there a way to tell Dymola to automatically load my custom packaged every time?
The trick is to add the following lines to settings.mos in c:/Users/USERNAME/AppData/Roaming/Dynasim:
Utilities.setenv("MODELICAPATH", "C:\Users\USERNAME\Documents\Dymola");
openModel("c:\Users\USERNAME\Documents\Dymola\UserDefined\package.mo")
The first line adds the directory to the path that Dymola uses to search for packages that have not been loaded prior to the first run of a model, and the second line loads the specified package. These two commands may be somewhat redundant, but I am doing both because I want to make sure my custom packages are on the path in addition to loading the UserDefined package.
Two suggestions. First, you need to add your package to the MODELICAPATH. You'll have to consult the Dymola documentation to figure out exactly what you need to do. But normally, what this means is that you have to set an environment variable that gives a list of directories (; separated) to be searched for your package. Now that will put it in your path so it can find it automatically, but it won't load it until it needs it.
If you want it to always appear in the package browser, you'll probably need to set up a .mos file (script) to load it. Dymola has that capability, but you'll have to read the manual to figure out what that script has to be called and where Dymola expects to find it.
I hope that helps.
In the instalation folder of Dymola 2018 -> insert -> dymola.mos
I've added the lines:
Utilities.setenv("MODELICAPATH", "C:\Users\XXXX\Documents\Dymola");
openModel("C:\Users\XXXX\Documents\Dymola\DCOL\package.mo");
openModel(“C:\Users\XXXX\Documents\Dymola\Annex60 1.0.0\package.mo”);
Now I don't get the utilities sentence, as the DCOL package loads fine without it and the added 'utilities' package in the package menu is useless.
But it does not open the Annex60 package.
I've tried a lot of different combinations and can't get multiple packages to load. I doubt that "cd" and "Advanced.ParallelizeCode", which are also added in the text work.
The accepted answer does not work since Dymola 2017 FD01, as the file settings.mos is not used anymore. User settings are stored in the setup.dymx file instead, located in
C:\Users\USERNAME\AppData\Roaming\DassaultSystemes\Dymola
In contrast to the setup.mos file you can not include custom lines with modelica script in setup.dymx.
The answer using dymola.mos still works, but you need admin privileges to modify this file.
Here is a simple solution which works with all Dyomola versions:
You can pass a .mos-script as first parameter to the dymola.exe.
This can e.g. be done like this:
Create a .mos script somewhere with commands like openModel(), etc.
Create a desktop shortcut to Dymola.exe
Open the properties of the shortcut and add the path to the .mos script in the Target text field. It will then look something like this:
"C:\Program Files\Dymola 2018 FD01\bin64\Dymola.exe" "C:\<some-path>\startup.mos"
Start Dymola with the desktop shortcut. The script will be executed and eventual errors or messages are displayed in the Commands window
Another suggestion where you don't need to hardcode your package into an environment variable of your operating system (and maybe more safe for inexperienced programmers):
Go to the folder where Dymola is installed (e.g. C:\Program Files\Dymola 2020).
Search for the Dymola.mos file in the insert-folder. 'insert' folder
Open the script (e.g., in notepad++)
Add the link(s) to your Dymola-library-package.mo file(s) here with the openModel statement
e.g., openModel("C:/IDEAS/package.mo"); Dymola.mos script
Save the script. Now, every time you open Dymola, your libraries will be loaded automatically.

Does matlab have a matlabrc file?

Today I stumbled upon this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560
The question is basically how to make Matlab read your startup.m file regardless of where
you start your matlab session.
One of the solutions offered was:
One solution would be to ask the system administrator to add a few lines
to "matlabrc.m" that adds some pre-determined folder in the user's home
directory to the MATLAB path (say, ~/.matlabstart). Then each user can
have their own "startup.m" file inside this folder.
What I ended up doing in my system (OS X) was to add a startup.m file in:
/Applications/MATLAB_R2011a.app/toolbox/local/
In this startup.m file I added:
if exist([getenv('HOME') '/.matlabrc/startup.m'])
run([getenv('HOME') '/.matlabrc/startup.m']);
end
That way users have the option of creating the hidden folder ~/.matlabrc and inside it they can put the file startup.m. In this startup file they can tell matlab what to execute whenever they start Matlab regardless of the directory where they started it. An example of what I added to my own personal startup.m file is
addpath(genpath('/Users/jmlopez/matlabcode/'))
Now I can add as many folders inside that directory and all of them will be added
to the path every time I start Matlab automatically without having to modify the path.
The question is: Did Matlab already provided a special file like the one I created or did I just go through all this trouble to accomplish what I wanted? If the answer is the second option I gave, then, why doesn't Matlab provide this? It is such a pain in the ass to add directories to the Matlab path whenever you do not have admin permissions and I do not want to carry my startup.m file to every directory I go to. Can someone shed some light into this please?
You can save the pathdef file (which stores all the paths you add) to a custom directory. The problem however is that when matlab starts, it doesn't automatically know which custom directory you used in the previous session.
But that's where the MATLABPATH environment variable comes in. Because this allows to set the matlab starting path yourself. In linux this is simply done by setting this environment variable MATLABPATH before starting matlab (from a terminal / in your .bashrc / ...)
export MATLABPATH=$HOME/.matlab
This way you can let all users have their own pathdef file, which solves the problem of having to add them manually at startup.
EDIT
I tested out if adding startup.m to that MATLABPATH directory worked, ie: does matlab run that startup file? ... and it does. I think it doesn't work for you, because there is another startup.m file in some other (higher priority) directory (probably matlabroot), so that gets precedence. My only startup file is in MATLABPATH, so there is only one choice.
EDIT2
Nope, I added startup to matlabroot directory, and still my own startup file in .matlab gets run. Are you sure you set the MATLABPATH correctly before you started matlab?