how to run opl code(by caling opl file) in python - python-3.x

I have a data file inputpy.xlsx and I am converting this file to an opl input language(1 and 0) called outputpy.xlsx, and I have a working opl model that takes my outputpy.xlsx file and outputs to an excel file(oploutput.xlsx) from cplex ide.
However, my problem is I am currently running both of them separately.
First I am running python code to make outputpy.xlsx and than I am using cplex ide to make oploutput.xlsx.
My question is :
How can I call/run my cplex code from my python code, after I created the outputpy.xlsx in python? I don't want to run python code and then run the cplex ide separately.
what I find online is some suggest using python API or doccplex, I am not sure the difference between them. I believe that using python API I have to change the cplex code for the python language, and I also can't find the nice tutorial for this.
Do you have suggestions?
Thank you in advance.

One very simple way to do this is to use oplrun instead of the IDE. Using oplrun you can run a project and/or mod file from the command line. Then you can invoke this from Python using subprocess.popen() or just os.system() (see this related question).
The description of oplrun can be found in the reference documentation in chapter IDE and OPL > oplrun command line interface
In order to run OPL directly from Python you could have a look at the doopl package.

Related

How to display all the functions / api available in a class / file in python

I am very new to python and started using pycharm ide for development.
How to display all the functions / API available in a class/file in python.
e.g. like when we code in scala/java in ide wee get all the functions/ methods available for that class/object by just typing object_name.(dot) and it shows all the API.
not able to get that for python in pycharm.
for example, I wanted to see all the functions available in the python List. I searched dir(module-name) but that is not what I need while developing faster.
thx in advance.
dir(module-name) and dir(class-name) is exactly how you do this. Yes, it doesn't return things in a userfriendly manner and you may need to dig down further for nested modules, but the only better option would be to learn from the package/api docs.

Freeling Python API working on sample, get Import error on other code

I'm trying out Freeling's API for python. The installation and test were ok, they provide a sample.py file that works perfectly (I've played around a little bit with it and it works).
So I was trying to use it on some other python code I have, in a different folder (I'm kind of guessing this is a path issue), but whenever I import freeling (like it shows on the sample.py):
import freeling
FREELINGDIR = "/usr/local";
DATA = FREELINGDIR+"/share/freeling/";
LANG="es";
freeling.util_init_locale("default");
I get this error:
ModuleNotFoundError: No module named 'freeling'.
The sample.py is located on the ~/Freeling-4.0/APIs/Python/ folder, while my other file is located in ~/project/, I dont know if that can be an issue.
Thank you!
A simple solution is to have a copy of freeling.py in the same directory as your code, since python will look there.
A better solution is to either paste it in one of the locations where it usually checks (like the lib folder in its install directory), or to tell it that the path where your file is should be scanned for a module.
You can check out this question to see how it can be done on Windows. You are basically just setting the PYTHONPATH environment variable, and there will only be minor differences in how to do so for other OSes. This page gives instructions that should work on Linux systems.
I like this answer since it adds the path at runtime in the script itself, doesn't make persistent changes, and is largely independent of the underlying OS (apart from the fact that you need to use the appropriate module path of course).
You need to set PYTHONPATH so python can find the modules if they are not in the same folder.

Making a Stand Alone Executable from a Python Project

I have a big python program which contains a lot of python files, images, etc.
I tried to create an executable file of all the program, but it does not success.
I am using PyQt5, Tesorflow and many other classes.
I hope anyone can help to create an exe for my final univesity project.
Thank You a lot.
Here is a link with a sample. and here a nice article.
basically, you get a tool like pyinstaller:
pyinstaller --onefile <your_script_name>.py
If you have more than 1 file, as you say in your answer then you can use compile all:
compileall.compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1)
as shown here
Have a look at this Git project that uses IronPython
and this web page that has a step by step for IronPython
Good luck

Can other extensions (besides .py .pyc .pyo) be recognized as Python modules?

I have a few test files residing in test/ directory which has an __init__.py file. The test files are basically Python files but have the extension as .thpy (Python test files). Each of these tests internally use unittest.
For code coverage purposes, I'm using the trace module. coverage.py, unfortunately would have been ideal but at this time, doesn't give information on the number of line hits per line.
unittest and trace are not really compatible. unittest.py doesn't play well with trace.py - why?
For example: I have a file name cluster_ha.thpy. From the solution given for the above question, I need to mention module='cluster_ha' inside cluster_ha.thpy but because of the extension, Python doesn't look at it as a Python module.
Is there any way to get around this? Is there a hack that can make other extensions to be considered as a Python module? Or perhaps, there's another module out there which can help me get code coverage?

I'm getting an invalid syntax error in configparser.py

I'm trying to get the pymysql module working with python3 on a Macintosh. Note that I am a beginning python user who decided to switch from ruby and am trying to build a simple (sigh) database project to drive my learning python.
In a simple (I thought) test program, I am getting a syntax error in confiparser.py (which is used by the pymysql module)
def __init__(self, defaults=None, dict_type=_default_dict,
allow_no_value=False, *, delimiters=('=', ':'),
comment_prefixes=('#', ';'), inline_comment_prefixes=None,
strict=True, empty_lines_in_values=True,
default_section=DEFAULTSECT,
interpolation=_UNSET):
According to Komodo, the error is on the second line. I assume it is related to the asterix but regardless, I don't know why there would be a problem like this with a standard Python module.
Anyone seen this before?
You're most certainly running the code with a 2.x interpreter. I wonder why it even tries to import 3.x libraries, perhaps the answer lies in your installation process - but that's a different question. Anyway, this (before any other imports)
import sys
print(sys.version)
should show which Python version is actually run, as Komodo Edit may be choosing the wrong executable for whatever reason. Alternatively, leave out the parens and it simply fails if run with Python 3.
In Python 3.2 the configparser module does indeed look that way. Importing it works fine from Python 3.2, but not from Python 2.
Am I right in guessing you get the error when you try to run your module with Komodo? Then you just have configured the wrong Python executable.

Resources