MongoDB: how to get db.stats() from API - python

I'm trying to get results of db.stats() mongo shell command in my python code (for monitoring purposes).
But unlike for example serverStatus I can't do db.command('stats'). I was not able to find any API equivalent in mongodb docs. I've also tried variations with db.$cmd but none of that worked.
So,
Small question: how can I get results of db.stats() (number of connections/objects, size of data & indexes, etc) in my python code?
Bigger question: can anyone explain why some of shell commands are easily accessible from API, while others are not? It's very annoying: some admin-related tools are accessible via db.$cmd.sys, some via db.command, some via ...? Is there some standard or explanation of this situation?
PS: mongodb 2.0.2, pymongo 2.1.0, python 2.7

The Javascript shell's stats command helper actually invokes a command named dbstats, which you can run from PyMongo using the Database.command method. The easiest way to find out what command a shell helper will run is to invoke the shell helper without parentheses -- this will print out the Javascript code it runs:
> db.stats
function (scale) {
return this.runCommand({dbstats:1, scale:scale});
}
As for why some commands have helpers and others do not, it's largely a question of preference, time, and perceived frequency of use by the driver authors. You can run any command by name with Database.command, which is just a convenience wrapper around db.$cmd.find_one. You can find a full list of commands at List of Database Commands. You can also submit a patch against PyMongo to add a helper method for commands you find that you need to invoke frequently but aren't supported by PyMongo yet.

Related

Import Golang module from Github in Python program

I have a program written in Python (.py) which requires to import and use a function written in Golang (.go) package located in Github repo.
How can I import the .go package module in python code and use the function. The function should accept args from my python code and return a value after doing some operations.
Note: I want to achieve this in Python2.7 version.
go_file.go
// Utility function to get string formed from input list of strings.
func NewFromStrings(inputs []string) string {
// do something
return "abc"
}
python_file.py
# This is just a pseudo code to make problem statement more clear.
import github.com/path_to_package/go_file
str_list = ['abc', 'def']
result = go_file.NewFromStrings(str_list)
print(result)
Thanks in advance :)
You have a few options, but it isn't possible to directly import Go code from python without a little work.
To do the inverse of the question you linked in your comment, you can create a small go cli to expose your Go function. You can then execute this Go cli from python using subprocess.run. The result will then be accessible from stdout. See https://docs.python.org/3/library/subprocess.html#subprocess.run
Alternatively, you can communicate using IPC, like sockets, which will involve creating a little server in Go.
The third option I can think of is exporting your Go function in a native library and calling it from python using c bindings. This is probably the most complicated route, but will get you closest to being able to import go code from python.
Check out this blog post with a much more thorough break down of some of your options: https://www.ardanlabs.com/blog/2020/06/python-go-grpc.html

PyCharm has no auto-completion for AWS Glue Python

I'm new to Python and AWS Glue and I have trouble to enable auto-completion for certain case.
First case:
dynamic_frame has no API list
If I force the creation of spark DataFrame, no API list is showed, it seems DataSource0 is unknown.
But glueContext has the API list shown:
Second case:
Spark DataFrame can be created and API list is showed. Since I'm new to AWS Glue, I'm not sure this is the best practice to use the object of DynamicFrame for the DF conversion, instead of using .toDF() directly and what would be the impact.
Third case:
Define the type to have API list showed. and again, I'm new to Python and I come from a background of C/JAVA/SCALA, so I've no idea if this would be weird or "non-python" code style.
Environnment:
Python 3.6 installed by Anaconda
pyspark and AWS Glue installed via pip
To solve the first case try putting a type hint in front of the variable DataSource0 : DynamicFrame.
The Linter of the IDE tries running your code in a separate background process to resolve the types and populate the auto-completion list. If you use a type hint you are helping the Linter determine the type of the attribute and so the IDE doesn't have to determine the type dynamically.
The reason auto-completion works with the library functions in the other examples is because in those cases no dynamic code needs to resolved by the Linter. The library functions by themselves are clearly determined.
Sometimes it also takes PyCharm a few seconds to resolve auto-completions, so don't force the menu, give it a few seconds for the background Linter process to complete between writing the variable and the dot (Ctrl+Space afterwards to show the auto-complete).
In your case those library functions also have several levels of depth, that makes resolving the auto-suggestions heavier for the IDE. Flat really is better than nested in this case. But this isn't a problem just an inconvenience, provided the code is correct it will execute as expected at run-time (it does make writing the code more difficult without the occasional help of auto-suggestions).

Win32com interfacing with Reflection Desktop

I run a number of python scripts and programs to aggregate and edit data in Attachmate Extra. My company was on an old version of Attachmate Extra until recently. I'm testing the new version (Reflection Desktop v. 16.2) and my scripts no longer work. I built them with the aid of the helpful advice on this link. I would like to be able to control (scrape, write, etc) screens on the new version.
Here's where I currently am. Running this code creates the a new window:
system = win32com.client.Dispatch("ReflectionIBM.Session")
system.Visible = True
...but then from there I cannot do any of the commands I previously used. Running this, for example
system.MoveCursor(11, 65)
creates a new tab in the emulator that doesn't connect to a session.
I've looked all around the Reflection documentation for an answer. This page led me to believe the old session method is no longer necessary but I'm not sure. I think I'm wrapping the correct object, and the documentation says that legacy commands still work, but I haven't figured out how to link them.
For reference, here are the lines I was using previously to connect to Attachmate:
system = win32com.client.Dispatch("EXTRA.System")
sess0 = system.ActiveSession
Screen = sess0.Screen
Any help is appreciated. I've been scouring the win32com browser for a list commands and looking through the registry to find available classes but I don't know what to look for. Thank you!
EDIT:
I previously used a couple of functions to read, write, and move the cursor around within Attachmate. Example:
def write(screen,row,col,text):
screen.row = row
screen.col = col
screen.SendKeys(text)
write(screen, 10, 65, "test")
Is there a way to get this working again in Reflection?
I still do not know why this works the way it does. In VBA the GetObject method works on "Reflection Workspace" but in python that did not produce any usable attributes that I could find. For python, to get the active session object I had to use EXTRA.System:
from win32com.client.gencache import EnsureDispatch
screen = EnsureDispatch("EXTRA.System").ActiveSession.Screen
From there the code seems generally the same as VBA with
screen.GetString(row, col, len)
screen.PutString(data, row, col)
screen.SendKeys('<PF1>')
for interacting with the host.
After more documentation reading and more trial and error I solved it. "EXTRA.System" is kept for legacy reasons so technically will still work. However, to connect to an active session of Reflection this worked:
system = win32com.client.GetObject('Reflection Workspace')
then to get the active view:
screen = system.GetObject("Frame").SelectedView.Control.Screen
or a specific numbered view:
screen = system.GetObject("Frame").view(1).Control.Screen
The code for interacting with Reflection also has changed and now looks like:
screen.GetText(row, col, len)
screen.PutText2(data, row, col)
screen.SendControlKey(ControlKeyCode)
The documentation for ControlKeyCode does not seem to provide the codes for the control keys. However, you can find the definitions in the Visual Basic Object Browser that comes with Reflection. In Reflection, on the macros tab, click Visual Basic, then press F2 and search for ControlKeyCode. A list of them should show up. For example, mine shows ControlKey_F1=10.
screen.SendKeys("N")
can still be used to send individual key strokes such as the N key but SendControlKey seems to have replaced the command keys such as Enter, Page Up, and the Function keys.
Have launched python package py_reflection in this regards,
How to start:
Install the package via pip:
py -m pip install py_reflection
Run the package (Run the commands below in terminal):
python
>>>from py_reflection import app
>>>app.run()
Api Endpoint and their description: ** All of the endpoints have a common parameter view_idx(integer, optional). Use this parameter to toggle between session in emulator.
/send_keys: Use this endpoint to press keys in the emulator. Parameters to pass: text(string), x(integer), y(integer)
/get_text: Use this endpoint to get text from a specific coordinate. Parameters to pass: x(integer), y(integer)
/press_key: Use this endpoint to press special control keys: Parameters to pass: control_key(string all in caps). Available control keys: 'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','F13','F14','F15','F16','F17','F18','F19','TAB','DELETE','LEFT','DOWN','UP','RIGHT','PAGEUP','PAGEDOWN','CLEAR','END','ENTER'
/get_text_coordinates: Use this endpoint to get coordinates of text present in emulator screen. Parameters to pass: text(string), total_row_count(integer, optional), total_column_count(integer, optional)
/check_text_present: Use this endpoint to check if given text present in emulator screen. Parameters to pass: text(string), total_row_count(integer, optional), total_column_count(integer, optional)
/move_cursor: Use this endpoint to move cursor to specified coordinate. Parameters to pass: x(integer), y(integer)
/get_view_count: Use this endpoint to get number of sessions opened in emulator.
More info at pypi.org

Python Libvirt API for domtime actions

Does anybody know if python-libvirt has any API to perform 'domtime operations'.
I'm writing a python script that runs the command manually using os module.
...
os.system("virsh domtime %s --sync" % my_domain)
...
I want to use the libvirt API for that.
Do you know if it is possible?
I'm not finding any reference into the Documentation.
As stated in the documentation:
The Python binding should be complete and are mostly automatically
generated from the formal description of the API in xml.
According to the source of domtime, the command uses the calls virDomainSetTime and virDomainGetTime, which can be accessed through Python as libvirt.virDomain.setTime and libvirt.virDomain.getTime.

Python in Filemaker Pro

I was hoping to calculate fields using some rather complicated functions, which I don't think I can realistically write in filemaker.
I would prefer to write a script to extract data into python, perform some procedures and then import it back into filemaker (so a user can see the results "live" in layouts, without having to leave filemaker).
Is this possible in Filemaker Pro?
That python module is meant to work with FileMaker server: send GET/POST requests, get back response in XML, and parse it. Technically you can use it to do a lot (add and delete records, run scripts, etc.) but in your case it won't fit.
There are some plug-ins that can execute shell commands, so this way you can call Python from a command line. Other than that you cannot do this.
But in some time (a few months) there will be a FileMaker plug-in with embedded Python :)
I have a FileMaker plug-in called bBox that executes Python code. Mac OS X only, but a free download at http://beezwax.net/bbox.
It has these Python related functions:
bBox_PythonCompile ( mode; script )
bBox_PythonExecute ( mode )
bBox_PythonFinalize
bBox_PythonGetVar ( name {; asType) }
bBox_PythonSetVar ( name; value {; asType} )
A few parts are admittedly still a bit rough. The types that the GetVar and SetVar functions can work with are limited, for example. But the code has been out for a while with only a few reported issues, all since fixed.
I've worked with a couple solutions that used pyFilemaker with good results. It isn't getting much attention these days. On the other hand, there haven't been many external changes to FileMaker's XML interface either.
You may want to checkout PyFileMaker (python object wrapper for FM.) It enables you to access/edit FileMaker server database.

Categories