How to create a logfile when calling CPLEX from python - python-3.x

In the website of https://www.ibm.com/support/knowledgecenter/SSSA5P_12.6.2/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/tutorials/InteractiveOptimizer/solnOptions.html
I knew that when CPLEX solved a problem,it will create a logfile named as "cplex.log", but when I use CPLEX to solve a problem in python, this file wasn't created. And I'm so confused about that if this problem have some matters with the difference of languages. I mean, when using CPLEX to solve a problem in MATLAB, Java or C++, logfile will be created, but not created in python.
I'm expecting for your help.Thanks so much.

The cplex.log file is specific to the CPLEX interactive. It is not created automatically when using the other APIs (e.g., Python, Java, etc.). However, you can create it yourself doing the following (e.g., with the Python API):
cpx = cplex.Cplex()
cplexlog = "cplex.log"
cpx.set_results_stream(cplexlog)
cpx.set_warning_stream(cplexlog)
cpx.set_error_stream(cplexlog)
cpx.set_log_stream(cplexlog)
The argument to the set_*_stream methods can be a path (as above) or a file-like object, so you can do pretty much anything you want (e.g., implement a file-like object to display output on stdout but also write it to a log file, etc.). See the documentation for set_results_stream for more details.
NOTE: There is some output that gets displayed from the interactive that is not available in the other APIs. However, you should be able to recreate it easily as all of the information is available through the programmatic APIs.
EDIT:
With CPLEX 12.10 using a filename with the set_results_stream, set_warning_stream, set_error_stream, and set_log_stream methods has been removed (see announcement here). Instead, a file-like object should be passed in, like so:
with cplex.Cplex() as cpx, \
open("cplex.log") as cplexlog:
cpx.set_results_stream(cplexlog)
cpx.set_warning_stream(cplexlog)
cpx.set_error_stream(cplexlog)
cpx.set_log_stream(cplexlog)
...

Related

How to share a variable between 2 pyRevit scripts?

I am using the latest version of pyRevit, v45.
I'm writing some info in temporary files with
myTempFile = script.get_instance_data_file("id")
This creates a file named pyRevit_2018_xxxx_id.tmp in which I store useful info. If I'm not mistaken, the "xxxx" part is changing every time I reload Revit. Now, I need to get access to this information from another pyRevit script.
How can I retrieve the name of the temp file I need to read? In other words, how do I access "myTempFile" from within the second script, which has no idea of the name of "myTempFile"?
I guess I can share somehow that variable between my script, but what's the proper way to do this? I know this must be a very basic programming question, but I'm indeed not a programmer ;)
Thanks a lot,
Arnaud.
Ok, I realise now that my variables in the 1st script cease to exist after its execution.
So for now I wrote the file name in another file, of which I know the name.. That works.
But if there's a cleaner way to do this, I'd be glad to learn ;)
Arnaud
pyrevit.script module provides 4 different methods for creating temporary files based on their use case:
get_instance_data_file:
for data files marked with Revit instance pid. This means that scripts running on another instance will not see this temp file.
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_instance_data_file
get_universal_data_file:
for temp files accessible to all Revit instances and versions
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_universal_data_file
get_data_file:
Base method to get a standard temp file for current revit version
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_data_file
get_document_data_file:
temp file marked with active document (so scripts working on another document will not see this)
http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_document_data_file
Each method uses a pattern to create the temp file name. So as long as the call to the method is the same of different scripts, the method generates the same file name.
Example:
Script 1:
from pyrevit import script
tfile = script.get_data_file('mydata')
Script 2:
from pyrevit import script
tempfile = script.get_data_file('mydata')
In this example tempfile = tfile since the file id is the same.
There is documentation on each so make sure you take a look at those and pick the flavor that serves your purpose.

Python application menu

I'm writing an application menu in Python for a linux Desktop Environment I'm creating, and I'm wondering if there is a module that will allow me to read .desktop files, and create a Dict/Object from the results.
If there isn't, I would like some tips on how to go about writing this myself.
The files you are referring to (.desktop files) are structured similarly to .ini files for many applications, as seen in this example on the Arch Linux Wiki:
[Desktop Entry]
Type=Application # Indicates the type as listed above
Version=1.0 # The version of the desktop entry specification to which this file complies
Name=jMemorize # The name of the application
Comment=Flash card based learning tool # A comment which can/will be used as a tooltip
Exec=jmemorize # The executable of the application.
Icon=jmemorize # The name of the icon that will be used to display this entry
Terminal=false # Describes whether this application needs to be run in a terminal or not
Categories=Education;Languages;Java; # Describes the categories in which this entry should be shown
Sections are headed by [Bracketed Text], and everything else is key-value pairs separated by = (I have also seen :).
Python has the built-in ConfigParser module to handle these. That link is the technical documentation but the PythonWiki has a page with simpler examples. This library does precisely what you asked for: it reads files formatted like .desktop files into objects and dicts.
The first example on the wiki shows this clearly:
>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("c:\\tomorrow.ini")
['c:\\tomorrow.ini']
>>> Config.sections()
['Others', 'SectionThree', 'SectionOne', 'SectionTwo']
We import ConfigParser (renamed to configparser in Python3), instantiate it, and tell it to read the file. Config.sections() is now a list containing all the section headers [Bracketed Text].
There are various flavors of ConfigParser objects depending on your needs. The basic one has methods like getint, getboolean, etc., which take a section and an option and try to return it coerced into the specified Python object. There are also methods like items and defaults to return the items and defaults respectively from a section given as an argument.
I don't know enough personally about .desktop files to know what sorts of things you might encounter or how exactly you need to configure the parser, but this should get you started.
Use the built in open() function with parameters set to read. If you have trouble using the function consider having a look at the python reference Python Library Reference, open() function. There you'll find a comprehensive explanation of how to use the function and it's parameters. Youtube and Google will also give you enough tuts on how to read and write to files in python.
Hope this solves your question.

Can I alter Python source code while executing?

What I mean by this is:
I have a program. The end user is currently using it. I submit a new piece of source code and expect it to run as if it were always there?
I can't find an answer that specifically answers the point.
I'd like to be able to say, "extend" or add new features (rather than fix something that's already there on the fly) to the program without requiring a termination of the program (eg. Restart or exit).
Yes, you can definitely do that in python.
Although, it opens a security hole, so be very careful.
You can easily do this by setting up a "loader" class that can collect the source code you want it to use and then call the exec builtin function, just pass some python source code in and it will be evaluated.
Check the package
http://opensourcehacker.com/2011/11/08/sauna-reload-the-most-awesomely-named-python-package-ever/ . It allows to overcome certain raw edges of plain exec. Also it may be worth to check Dynamically reload a class definition in Python

Labelling Neo4j database using Neo4django

This question is related to the github issue of Neo4django. I want to create multiple graphs using Neo4j graph DB from Django web framework. I'm using Django 1.4.5, neo4j 1.9.2 and neo4django 0.1.8.
As of now Neo4django doesn't support labeling but the above is my core purpose and I want to be able to create labels from Neo4django. So I went into the source code and tried to tweak it a little to see if I can make this addition. In my understanding, the file 'db/models/properties.py' has class BoundProperty(AttrRouter) which calls gremlin script through function save(instance, node, node_is_new). The script is as follows:
script = '''
node=g.v(nodeId);
results = Neo4Django.updateNodeProperties(node, propMap);
'''
The script calls the update function from library.groovy and all the function looks intuitive and nice. I'm trying to add on this function to support labeling but I have no experience of groovy. Does anyone have any suggestions on how to proceed? Any help would be appreciated. If it works it would be a big addition to neo4django :)
Thank you
A little background:
The Groovy code you've highlighted is executed using the Neo4j Gremlin plugin. First it supports the Gremlin graph DSL (eg node=g.v(nodeId)), which is implemented atop the Groovy language. Groovy itself is a dynamic superset of Java, so most valid Java code will work with scripts sent via connection.gremlin(...). Each script sent should define a results variable that will be returned to neo4django, even if it's just null.
Anyway, accessing Neo4j this way is handy (though will be deprecated I've heard :( ) because you can use the full Neo4j embeddeded Java API. Try something like this to add a label to a node
from neo4django.db import connection
connection.gremlin("""
node = g.v(nodeId)
label = DynamicLabel.label('Label_Name')
node.rawVertex.addLabel(label)
""", nodeId=node_id)
You might also need to add an import for DynamicLabel- I haven't run this code so I'm not sure. Debugging code written this way is a little tough, so make liberal use of the Gremlin tab in the Neo4j admin.
If you come up with a working solution, I'd love to see it (or an explanatory blog post!)- I'm sure it could be helpful to other users.
HTH!
NB - Labels will be properly supported shortly after Neo4j 2.0's release- they'll replace the current in-graph type structure.

Getting echofunc.vim to work

I came across echofunc.vim today (from a link in SO). Since I'm rubbish at remembering the order of function parameters, it looked like a very useful tool for me.
But the documentation is a bit lean on installation! And I've not been able to find any supplementary resources on the internet.
I'm trying to get it running on a RHEL box. I've copied the script into ~/.vim/plugin/echofunc.vim however no prompt when I type in a function name followed by '('. I've tried adding
let g:EchoFuncLangsUsed = ["php","java","cpp"]
to my .vimrc - still no prompting.
I'm guessing it needs to read from a dictionary somewhere - although there is a file in /usr/share/vim/vim70/ftplugin/php.vim, this is the RH default and does not include an explicit function list.
I'm not too bothered about getting hints on the functions/methods I've defined - just trying to get hints for the built-in functions. I can see there is a dictionary file available here which appears to provide the resources required for echofunc.vim, I can't see how I set this up.
TIA,
It expects a tags file, the last line of the description describes exactly how to generate it:
ctags -R --fields=+lS .
It works here with PHP but not with JS. Your mileage may vary.
I didn't know about this plugin, thanks for the info.
You should try phpcomplete.vim, it shows a prototype of the current function in a scratchpad. It is PHP only, though.

Resources