Use data from R cell in Python cell of databricks notebook - databricks

Is this possible and if so how? As a simple example I might have an R cell like the following
%r
x <- rbinom(10, 100, .5)
How can I access x from a Python cell?

A small excerpt from the Databricks website - mix-languages
When you invoke a language magic command, the command is dispatched to
the REPL in the execution context for the notebook. Variables defined
in one language (and hence in the REPL for that language) are not
available in the REPL of another language. REPLs can share state only
through external resources such as files in DBFS or objects in object
storage.
Some workarounds that I have seen on stackoverflow
Save in file in DBFS and read it back in python cell.
Other not tested/not recommended options.
2.1. Set environmental variable in one language and then read the same in another language.
2.2. Set value as a spark - conf and then read back in another language cell.
The file option works well. The other options I have not been able to pass along reliably. Maybe it is just for me.
Please validate and see if it works for you...

Related

How to reference the most current Physical Sequential (PS) file in JCL

I wanted to create a job where I need to consider the latest file available as input file.
File format is as below: FILE1.TEST.TYYMMDD
is there any way to identify latest file based on date present in file name via JCL.
P.S. GDG versions are not created in existing process . Only PS file is created.
Thank you
I wanted to create a job where I need to consider the latest file available as input file. File [name] format is as below: FILE1.TEST.TYYMMDD is there any way to identify latest file based on date present in file name via JCL.
No.
You indicate that GDGs are not created in the existing process. GDGs would be the best way to accomplish your goal. Absent GDGs, you must write code.
You could accomplish your goal by writing (C, clist, COBOL, PL/I, Rexx) code using the LMDINIT and LMDLIST ISPF services. Then you would execute your code by running ISPF in batch. Many mainframe shops have a cataloged procedure to execute ISPF in batch.
Agree with #cschneid that there is not a platform way to handle this. However, I want to point out that GDGs are the platform way of managing PS files for access in a relative form.
Your comment
GDG versions are not created in existing process . Only PS file is
created.
That statement didn't make sense to me. GDGs are not a file type like physical sequential (PS) or partitioned (PO). It's a convention to allow relative reference to files created over time which sounds like what you want. I've only seen the use of GDGs for PS files.
Putting the date in the file name can have its uses but to z/OS its only part of the filename and not meta information that it operates on (like G0000v00's in GDGs.

How to create a logfile when calling CPLEX from python

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)
...

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

Resources