When I run a python program it's as simple as writing into terminal python myprogram.py. Now, I'm trying to run that program on a google vm instance and shut it off with as few steps as possible. I'm trying to get it all written down in a python program, but I was told that my method of using the subprocess module is not the right way to do it. I was told that the best way to do it is to use the googleapiclient module. So the current I use to create an instance is:
def create_instance(name='', machine_type=''):
name = 'kfoley76'
machine_type = 'n1-standard-1'
subprocess.run(['gcloud', 'compute', 'instances', 'create',
name, f'--machine-type={machine_type}',
'--zone=us-west2-a', '--boot-disk-auto-delete'])
How would I rewrite that using googleapiclient module. I assume the answer would be located here
https://cloud.google.com/compute/docs/reference/rest/v1/instances/start
but that documentation is utterly incomprehensible.
Please see the tutorial I mentioned in my answer to your other question. I'm confident it will get you exactly what you need.
Related
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
I have a problem using Airtable module for python
While I'm using my IDE in my local computer the documentation works fine but when I use the same code on any site like repl.it or host it on pythonanywhere it gives me errors
some attributes I found it's replacement such as insert will be create, get_all will be get...etc, but I couldn't find a replacement for the argument 'sort' which is used in get_all attribute, and that makes my code unable to work as I want it to work
Make sure you're using the same version of Airtable in all of the places where you're trying to run it.
Let's say that I wanted to create my own library to be used in Python. How would I code a key press? How do I establish that without referencing the Python library of "keyboard"? I would love to be able to view the "keyboard" library that I import, in order to see how that library was created.
Thanks!
You can check out the inspect module.
Also possibly duplicate of this: How can I get the source code of a Python function?
I have defined a simple SOAP-service in Spyne.
When I run my server like using the run module functionality of python like this:
python -m my_module.service
then everything's fine.
But when I run it from a wrapper script like this:
#!/usr/bin/env python
import my_module.service
sys.exit(my_module.service.main())
then suddenly in the generated WSDL, there will exist a namespace import xmlns:s0="my_module.service" on the<wsdl:definitions …>-tag.
Why is that? Where is that coming from? How can I set this myself in the main() method or prevent it from getting inserted in the first place? I looked throught he code of spyne but couldnt find the relevant lines.
This thread at GitHub explains how and why:
https://github.com/arskom/spyne/issues/233
I am trying to learn how Python reloads modules, but have hit a roadblock.
Let's say I have:
dir1\file1.py:
from dir2.file2 import ClassOne
myObject = ClassOne()
dir1\dir2\file2.py:
class ClassOne():
def reload_module():
reload(file2)
The reload call fails to find module "file2".
My question is, how do I do this properly, without having to keep everything in one file?
A related question: When the reload does work, will myObject use the new code?
thank you
def reload_module():
import file2
reload(file2)
However, this will not per se change the type of objects you've instantiated from classes held in the previous version of file2. The Python Cookbook 2nd edition has a recipe on how to accomplish such feats, and it's far too long and complex in both code and discussion to reproduce here (I believe you can read it on google book search, or failing that the original "raw" version [before all the enhancements we did to it], at least, should still be on the activestate cookbook online site).