Determining the Bazaar version number from Python without calling bzr - python

I have a django (Python) project that needs to know what version its code is on in Bazaar for deployment purposes. This is a web application, so I don't want to do this because it fires off a new subprocess and that's not going to scale.
import subprocess
subprocess.Popen(["bzr", "revno"], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
Is there a way to parse Bazaar repositories to calculate the version number? Bazaar itself is written in Python and contains this code for calculating the revno, which makes me think it isn't exactly trivial.
rh = self.revision_history()
revno = len(rh)
Edit: Final fix
from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = len(branch.revision_history())
Edit: Final fix but for real this time
from bzrlib.branch import BzrBranch
branch = BzrBranch.open_containing('.')[0]
revno = branch.last_revision_info()[0]

You can use Bazaar's bzrlib API to get information about any given Bazaar repository.
>>> from bzrlib.branch import BzrBranch
>>> branch = BzrBranch.open('.')
>>> branch.last_revision_info()
More examples are available here.

Do it once and cache the result (in a DB/file, if need be)? I doubt the version is going to change that much.

Related

How to store the last displayed value from the console?

My python script passes changing inputs to a program called "Dymola", which in turn performs a simulation to generate outputs. Those outputs are stored as numpy arrays "out1.npy".
for i in range(0,100):
#code to initiate simulation
print(startValues, 'ParameterSet:', ParameterSet,'time:', stoptime)
np.save('out1.npy', output_data)
Unfortunately, Dymola crashes very often, which makes it necessary to rerun the loop from the time displayed in the console when it has crashed (e.g.: 50) and increase the number of the output file by 1. Otherwise the data from the first set would be overwritten.
for i in range(50,100):
#code to initiate simulation
print(startValues, 'ParameterSet:', ParameterSet,'time:', stoptime)
np.save('out2.npy', output_data)
Is there any way to read out the 'stoptime' value (e.g. 50) out of the console after Dymola has crashed?
I'm assuming dymola is a third-party entity that you cannot change.
One possibility is to use the subprocess module to start dymola and read its output from your program, either line-by-line as it runs, or all after the created process exits. You also have access to dymola's exit status.
If it's a Windows-y thing that doesn't do stream output but manipulates a window GUI-style, and if it doesn't generate a useful exit status code, your best bet might be to look at what files it has created while or after it has gone. sorted( glob.glob("somepath/*.out")) may be useful?
I assume you're using the dymola interface to simulate your model. If so, why don't you use the return value of the dymola.simulate() function and check for errors.
E.g.:
crash_counter = 1
from dymola.dymola_interface import DymolaInterface
dymola = DymolaInterface()
for i in range(0,100):
res = dymola.simulate("myModel")
if not res:
crash_counter += 1
print(startValues, 'ParameterSet:', ParameterSet,'time:', stoptime)
np.save('out%d.npy'%crash_counter, output_data)
As it is sometimes difficult to install the DymolaInterface on your machine, here is a useful link.
Taken from there:
The Dymola Python Interface comes in the form of a few modules at \Dymola 2018\Modelica\Library\python_interface. The modules are bundled within the dymola.egg file.
To install:
The recommended way to use the package is to append the \Dymola 2018\Modelica\Library\python_interface\dymola.egg file to your PYTHONPATH environment variable. You can do so from the Windows command line via set PYTHONPATH=%PYTHONPATH%;D:\Program Files (x86)\Dymola 2018\Modelica\Library\python_interface\dymola.egg.
If this does not work, append the following code before instantiating the interface:
import os
import sys
sys.path.insert(0, os.path.join('PATHTODYMOLA',
'Modelica',
'Library',
'python_interface',
'dymola.egg'))

Set environment to production in Authorize.net Python SDK

Can anyone tell me how to switch my environment for a Settled Batch List Request from SANDBOX to PRODUCTION?
I've looked everywhere for documentation and the source code, and the farthest I've gotten is
settledBatchListRequest = apicontractsv1.getSettledBatchListRequest()
apicontrollersbase.APIOperationBase(settledBatchListRequest).setenvironment(constants.PRODUCTION)
But I still get an 'Invalid Credentials' Error
This is definitely confusing, I ended up looking at the source here:
https://github.com/AuthorizeNet/sdk-python/blob/master/authorizenet/constants.py
This should help:
>>> from authorizenet.constants import constants
>>> constants.PRODUCTION
'https://api2.authorize.net/xml/v1/request.api'
>>> constants.SANDBOX
'https://apitest.authorize.net/xml/v1/request.api'
Ultimately I usually end up setting these in my config files like so:
Production:
AUTH_NET_ENVIRONMENT = "https://api2.authorize.net/xml/v1/request.api"
Dev:
AUTH_NET_ENVIRONMENT = "https://apitest.authorize.net/xml/v1/request.api"
Then in your controller:
controller.setenvironment(settings.AUTH_NET_ENVIRONMENT)
Assuming you are creating the controller correctly, adding this line:
from authorizenet.constants import constants
or replacing setenvironment(constants.PRODUCTION) with:
setenvironment("https://api2.authorize.net/xml/v1/request.api")
should work. Hope this helps!

Get host of running job with DRMAA interface

Using the DRMAA interface to Son of Grid Engine I would like to get the name of a host running a particular job. I would like something like the following (using the drmaa-python interface)
>>> import drmaa
>>> s = drmaa.Session()
>>> s.initialize()
>>> jt = s.createJobTemplate()
>>> jt.remoteCommand = 'sleep'
>>> jt.args = ['100']
>>> jobid = s.runJob(jt)
>>> jobid
'1'
>>> s.jobStatus(jobid)
'running'
>>> s.the_function_I_want(jobid)
'worker-host-123'
Questions
Is this possible through the DRMAA interface? Looking at the spec it seems not, but I thought I'd ask anyway
If so, how?
Short answer: You are right, this is not possible.
Long answer:
The drmaa-python library acts as wrapper for an underlying C implementation of the DRMAAv1 API (see https://www.ogf.org/documents/GFD.130.pdf). Such a C library usually comes together with your cluster framework. DRMAAv1 does not support a standardized monitoring of jobs, and therefore both the DRMAAv1 C and Python library can't get this information.
If you wonder about that lack of functionality, please note that the original API design is from 2004.
The second version of DRMAA (https://www.ogf.org/documents/GFD.231.pdf) does support monitoring. At the time of writing, the only known implementation comes with Univa GridEngine, and there is no existing Python wrapper implementation for it.
You could try to parse the job log file by yourself to extract the execution host name. If you need to pass special options to qsub for getting such a file, you can use the nativeSpecification field in the job template.

No module named objects [bokeh]

Note from maintainers: This question is no longer relevant. The bokeh.objects module has not existed for years
I'm trying to run this script:
#Code will be significantly simplified in the 0.4 release
import time
from bokeh.objects import GlyphRenderer
renderer = [r for r in curplot().renderers if isinstance(r, GlyphRenderer)][0]
ds = renderer.data_source
while True:
df = pd.io.json.read_json(url+json_call)
ds.data["x"] = x+N*i
ds.data["y"] = df.rssi
ds._dirty = True
session().store_obj(ds)
time.sleep(1.5)
i+=1
from:
https://www.continuum.io/content/painless-streaming-plots-bokeh
but at this line:
from bokeh.objects import GlyphRenderer
I got:
No module named objects
The version I'm using is
0.11.1
On linux mint 17.1
Note from maintainers: This answer is no longer relevant. The bokeh.objects module has not existed for years
did you try installing bokeh before trying the examples? If not, just run:
pip install bokeh
and try your script again.
if it does not work, it's likely that the bokeh sources changed, so you might want to change the
from bokeh.objects import GlyphRenderer
into
from bokeh.models.renderers import GlyphRenderer
cf the source code
At the first line of your example it states:
#Code will be significantly simplified in the 0.4 release
which means that the example's code was already about to be deprecated at the time of the writing of the tutorial.
So instead of copy/pasting that code, you should try to understand how it works, and recreate it using the documentation and sources:
http://docs.bokeh.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart
https://github.com/bokeh/bokeh
have fun!
The objects module was deleted in commit 5b5d28304c5ea209e243af5943917fe494d9ef9c (v0.7.1) after being deprecated in 8bb4a2f1f43b39b869c508ef7aee69f7aabb46b8 (v0.7.0). The deprecation message reads: "use bokeh.models instead". I leave finding GlyphRenderer in the current codebase as an exercise for you.
conda update bokeh solved this for me.
Regarding streaming (since that is is the example the OP was interested in), the current and stable streaming API is demonstrated here:
https://github.com/bokeh/bokeh/tree/master/examples/app/ohlc
This simple interface has been the way to efficiently stream to data soruces since 0.11.1, and will continue to be going forward.
The basic idea is to construct a dict with all the columns of a data source, and just the new data that is to be appended:
# construct a source with x and y columns
source = ColumnDataSource(data=dict(x=[....], y=[....]))
# sends the few new data points to the client, does not re-send all the data
source.stream(dict(x=[10, 11], y=[20, 21]))
Typically you'd probably call stream from some kind of periodic callback. The OHLC app linked above is a complete example.

calling Objective C functions from Python?

Is there a way to dynamically call an Objective C function from Python?
For example, On the mac I would like to call this Objective C function
[NSSpeechSynthesizer availableVoices]
without having to precompile any special Python wrapper module.
As others have mentioned, PyObjC is the way to go. But, for completeness' sake, here's how you can do it with ctypes, in case you need it to work on versions of OS X prior to 10.5 that do not have PyObjC installed:
import ctypes
import ctypes.util
# Need to do this to load the NSSpeechSynthesizer class, which is in AppKit.framework
appkit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('AppKit'))
objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))
objc.objc_getClass.restype = ctypes.c_void_p
objc.sel_registerName.restype = ctypes.c_void_p
objc.objc_msgSend.restype = ctypes.c_void_p
objc.objc_msgSend.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
# Without this, it will still work, but it'll leak memory
NSAutoreleasePool = objc.objc_getClass('NSAutoreleasePool')
pool = objc.objc_msgSend(NSAutoreleasePool, objc.sel_registerName('alloc'))
pool = objc.objc_msgSend(pool, objc.sel_registerName('init'))
NSSpeechSynthesizer = objc.objc_getClass('NSSpeechSynthesizer')
availableVoices = objc.objc_msgSend(NSSpeechSynthesizer, objc.sel_registerName('availableVoices'))
count = objc.objc_msgSend(availableVoices, objc.sel_registerName('count'))
voiceNames = [
ctypes.string_at(
objc.objc_msgSend(
objc.objc_msgSend(availableVoices, objc.sel_registerName('objectAtIndex:'), i),
objc.sel_registerName('UTF8String')))
for i in range(count)]
print voiceNames
objc.objc_msgSend(pool, objc.sel_registerName('release'))
It ain't pretty, but it gets the job done. The final list of available names is stored in the voiceNames variable above.
2012-4-28 Update: Fixed to work in 64-bit Python builds by making sure all parameters and return types are passed as pointers instead of 32-bit integers.
Since OS X 10.5, OS X has shipped with the PyObjC bridge, a Python-Objective-C bridge. It uses the BridgeSupport framework to map Objective-C frameworks to Python. Unlike, MacRuby, PyObjC is a classical bridge--there is a proxy object on the python side for each ObjC object and visa versa. The bridge is pretty seamless, however, and its possible to write entire apps in PyObjC (Xcode has some basic PyObjC support, and you can download the app and file templates for Xcode from the PyObjC SVN at the above link). Many folks use it for utilities or for app-scripting/plugins. Apple's developer site also has an introduction to developing Cocoa applications with Python via PyObjC which is slightly out of date, but may be a good overview for you.
In your case, the following code will call [NSSpeechSynthesizer availableVoices]:
from AppKit import NSSpeechSynthesizer
NSSpeechSynthesizer.availableVoices()
which returns
(
"com.apple.speech.synthesis.voice.Agnes",
"com.apple.speech.synthesis.voice.Albert",
"com.apple.speech.synthesis.voice.Alex",
"com.apple.speech.synthesis.voice.BadNews",
"com.apple.speech.synthesis.voice.Bahh",
"com.apple.speech.synthesis.voice.Bells",
"com.apple.speech.synthesis.voice.Boing",
"com.apple.speech.synthesis.voice.Bruce",
"com.apple.speech.synthesis.voice.Bubbles",
"com.apple.speech.synthesis.voice.Cellos",
"com.apple.speech.synthesis.voice.Deranged",
"com.apple.speech.synthesis.voice.Fred",
"com.apple.speech.synthesis.voice.GoodNews",
"com.apple.speech.synthesis.voice.Hysterical",
"com.apple.speech.synthesis.voice.Junior",
"com.apple.speech.synthesis.voice.Kathy",
"com.apple.speech.synthesis.voice.Organ",
"com.apple.speech.synthesis.voice.Princess",
"com.apple.speech.synthesis.voice.Ralph",
"com.apple.speech.synthesis.voice.Trinoids",
"com.apple.speech.synthesis.voice.Vicki",
"com.apple.speech.synthesis.voice.Victoria",
"com.apple.speech.synthesis.voice.Whisper",
"com.apple.speech.synthesis.voice.Zarvox"
)
(a bridged NSCFArray) on my SL machine.
Mac OS X from 10.5 onward has shipped with Python and the objc module that will let you do what you want.
An example:
from Foundation import *
thing = NSKeyedUnarchiver.unarchiveObjectWithFile_(some_plist_file)
You can find more documentation here.
You probably want PyObjC. That said, I've never actually used it myself (I've only ever seen demos), so I'm not certain that it will do what you need.

Categories