So question.
I have a TCL program that is calling a .py program and working as expected. But the problem is when the full TCL code is run (normal run takes about 3-4 hours), it takes 12-15 hours to run with python (and it is doing nothing difficult or special). I noticed that when TCL calls the python script, it does not create a .pyc file. However, when I call the same python script that TCL calls from another python script, I get better performance and a .pyc refresh.
Just curious if TCL-to-Python is limited in this way. If I need to extend the functionality of my TCL, am I better off using a compiled language such as Java or C?
Edit:
I moved to a team that uses off the shelf product written in TCL. The software technically works, but it is a maintenance nightmare. Tens of thousands of lines of code in a single file. All global variables, no parameters. Almost no documentation. All tests, even syntax adjustments, take at least 3 hours. One errant keystroke can cost you have a workday. And to top it off, the consultant that wrote it all has left.
What I was attempting to do is extend the software in strategic places while I attempt to make it more maintainable. Python and TCL seem to work together, which is why I made this attempt. But this has made me wonder if something like C or Java would be a better try.
I was attempting to write code that I can run and test independently (utilizing MongoDB as the data source) and then plug it in. Like I said before, it functionally works. The loop I was testing looped about 230,000 times, so that is why the 10 hour increase I'm sure.
I just come out of a 4 year stint in C#, so that is where my most recent experience lies. I'm a noob in Python. There is very little out there regarding TCL-to-C# plug-ins, besides something known as Eclipse that someone wrote years ago.
Related
Before I buy my first setup. I'll launch my deep-learning-pipline on sth like vast.ai.
I never did it before, but How can I protect my script from being "stolen"?
This should be a serious launch and take around 7 days to finish training.
google colab doesn allow enough memory & ram for what i need ( need around 64GB ram)
is there a way to run a python script encrypted? (note: it makes use of libaries)
It is hard to run python encrypted. However, you could try to store the code into encrypted disk space.
There are some ways, from fully obfuscating your script to converting your script into equivalent cython or creating an executable out of it using the likes of Nuitka.
You may also implement some important logic in C/C++ (as extensions) and then call them in your script.
You may also set up a server where you feel is OK and send the bits that needs to be executed, basically create a distributed system.
As you can see there are many ways, and the deeper you go the more complex it gets.
Also you might want to have a look here as well.
How can I run c/c++ code within python in the form:
def run_c_code(code):
#Do something to run the code
code = """
Arbitrary code
"""
run_c_code(code)
It would be great if someone could provide an easy solution which does not involve installing packages. I know that C is not a scripting language but it would be great if it could do a 'mini'-compile that is able to run the code into the console. The code should run as it would compiled normally but this needs to be able to work on the fly as the rest of the code runs it and if possible, run as fast as normal and be able to create and edit variables so that python can use it. If necessary, the code can be pre-compiled into the code = """something""".
Sorry for all the requirements but if you can make the c code run in python then that would be great. Thanks in advance for all the answers..
As somebody else already pointed out, to run C/C++ code from "within" Python, you'd have to write said C/C++ code into an own file, compile it correctly, and then execute that program from your Python code.
You can't just type one command, compile it, and execute it. You always have to have the whole "framework" set up. You can't compile a program when you haven't yet written the } that ends the class/function/statement 20 lines later on. At this point you'd already have to write the whole C/C++ program for it to work. It's simply not meant to be interpreted on the run, line by line. You can do that with python, bash/dash/batch, and a few others. But C/C++ definitely isn't one of them.
With those come several issues. Firstly, the C/C++ part probably needs data from the Python part. I don't know of any way of doing it in RAM alone (maybe there is one, but I don't know), so the Python part would have to write it into a file, the C/C++ part would read and process it, then put the processed data into another file, and then the Python part would have to read that and continue.
Which brings another point up. Here we're already getting into multi-threading territory, because the moment you execute that C/C++ program you're dealing with a second thread. So, somehow, you'd have to coordinate those programs so that the Python part only continues once the C/C++ part is done. Shouldn't be a huge problem to get running, but it can be a nightmare to performance and RAM if done wrongly.
Without knowing to what extent you use that program, I also like to add that C/C++ isn't platform-independent like Python. You'll have to compile that program for every single different OS that you run it on. That may come with minor changes to the code and in general just a lot of work because you have to debug and test it for every single system.
To sum up, I think it may be better to find another solution. I don't know why you'd want to run this specific part in C/C++, but I'd recommend trying to get it done in one language. If there's absolutely no way you can get it done in Python (which I doubt, there's libraries for almost everything), you should get your Python to C/C++ instead.
If you want to run C/C++ code - you'll need either a C/C++ compiler, or a C/C++ interpreter.
The former is quite easy to arrange (though probably not suitable for an end user product) and you can just compile the code and run as required.
The latter requires that you attempt to process the code yourself and generate python code that you can then import. I'm not sure this one is worth the effort at all given that even websites that offer compilation tools wrap gcc/g++ rather than implement it in javascript.
I suspect that this is an XY problem; you may wish to take a couple of steps back and try to explain why you want to run c++ code from within a python script.
I have an assignment for school that provided some code in c and a make file. We could chose any language to code in for the part we were assigned, and i chose python.
is it possible to execute the python code file in the make file along with the provided code files(which are in c)?
You can do anything in a makefile - you can run any commands that you could type yourself and hit Enter. A makefile is just a convenient way to do that - it has 3 advantages over typing commands yourself:
You don't have to type them every time, you can just type make and hit Enter, that's much faster.
Any of the commands will not be run, if it was run before and "nothing important" changed in the meantime. This will make things faster again, but probably not by much for a small project. But you have to specify in the makefile, what is "important" for each command. The amount of time you will need to do that, will probably be more than what you saved, for a small project. Also, if you sc**w up, some commands may not run when they should, and you won't even know about it. This will mess you up BIG LEAGUE (and by the way people, this is what our Dear Leader says, not "bigly"). But if you want to learn makefiles, for later big projects, then yes go for it!
The commands can run in parallel as much as possible, which will speed things up, provided you correctly specify in the makefile, what can and cannot be run in parallel, and provided you have a multiple-processor computer. Exactly like in 2 above, for a small project it is not worth it, unless you want to learn makefiles.
I am relatively new to programming, well I have been programming in Python for about 6 months now due to taking Computing at A-Level. I believe that I have caused a disaster that influentially may cause me to drop marks and overall get a very bad mark in Computing.
One of the specification points that I must've not read was to create a GUI that is simple and easy to use (for the children using the program). I created and designed the program which took me months in IDLE getting ready to hand my coursework in before April the 11th, my deadline.
So my questions stands, is there a way that I can broadly 'add' a GUI to my script, or add one within a few days. I do not know how to use Tkinter and my program is 300 lines long.
Help please :-(
GUI programs are very different from command-line programs. A command-line program generally executes in a lineair fashion. A GUI program is event-driven. Event handler functions are called when e.g. a button is pressed or a menu is chosen.
Try separating your program into two files; a module that does the real work and a front-end that does the interaction with the user. Once you have made this separation, it should be relatively easy to add a GUI front-end.
If you want your program to basically work everywhere, stick to TkInter.
So I am working on a Matlab application that has to do some communication with a Python script. The script that is called is a simple client software. As a side note, if it would be possible to have a Matlab client and a Python server communicating this would solve this issue completely but I haven't found a way to work that out.
Anyhow, after searching the web I have found two ways to call Python scripts, either by the system() command or editing the perl.m file to call Python scripts instead. Both ways are too slow though (tic tocing them to > 20ms and must run faster than 6ms) as this call will be in a loop that is very time sensitive.
As a solution I figured I could instead save a file at a certain location and have my Python script continuously check for this file and when finding it executing the command I want it to. Now after timing each of these steps and summing them up I found it to be incredibly much faster (almost 100x so for sure fast enough) and I cant really believe that, or rather I cant understand why calling python scripts is so slow (not that I have more than a superficial knowledge of the subject). I also found this solution to be really messy and ugly so just wanted to check that, first, is it a good idea and second, is there a better one?
Finally, I realize that the Python time.time() and Matlab tic, toc might not be precise enough to measure time correctly on that scale so also a reason why I ask.
Spinning up new instances of the Python interpreter takes a while. If you spin up the interpreter once, and reuse it, this cost is paid only once, rather than for every run.
This is normal (expected) behaviour, since startup includes large numbers of allocations and imports. For example, on my machine, the startup time is:
$ time python -c 'import sys'
real 0m0.034s
user 0m0.022s
sys 0m0.011s