How to use TideSDK openFolderChooseDialog - python

I'm trying to use TideSDK and python tp get the user to select a folder from the hard drive. Everything works, but I have no idea how obtain which folder the user selected.
I can't seem to find documentation on what Ti.UI.UserWindow.openFolderChooseDialog returns and what kind of object the callback function uses. I just get errors that "window" in "onopen" in my code below is a None Type object when I try to print it out.
Is there any documentation on the proper use of the openFolderChooseDialog, what signature the callback needs to be and how to get the Folder/directory from the dialog?
My code:
def onopen(window):
Ti.App.stdout("------------------ Opening Dialog")
Ti.App.stdout(window)
def burndir():
try:
dir = Ti.UI.getCurrentWindow().openFolderChooserDialog(onopen)
Ti.App.stdout(dir)
except:
Ti.App.stderr("------ There was an error: ")
Ti.App.stderr(sys.exc_info()[0])
Ti.App.stderr(sys.exc_info()[1])
Ti.App.stderr(sys.exc_info()[2])
Any help is much appreciated

I found the answer in a Javascript Code example here:
https://github.com/appcelerator/titanium_developer/blob/master/Resources/perspectives/projects/js/projects.js#L1338
It appears that openFolderChooserDialog return nothing (a None object in Python). The callback function passes one argument which is a StaticBoundList (a Tuple object in Python) that contains all of the selected folders (in case of allowing multiple selections)
Here is the updated code:
def onopen(window):
if (len(window) > 0):
Ti.App.stdout("------------------ Opening Dialog")
Ti.App.stdout(window[0])
else:
Ti.App.stdout("------------------ Nothing Selected")
def burndir():
try:
Ti.UI.getCurrentWindow().openFolderChooserDialog(onopen)
except:
Ti.App.stderr("------ There was an error: ")
Ti.App.stderr(sys.exc_info()[0])
Ti.App.stderr(sys.exc_info()[1])
Ti.App.stderr(sys.exc_info()[2])
I hope this helps someone struggling to find the same documentation!

Related

PyWinAuto with out using "child_window"

I have NO return on child_window when the program is in its state i expect to work in.
I need a way to edit the text field but literally all examples and google searches i have done show no examples of implementation EXCEPT when using child_window
this should put Test into the edit field
from pywinauto.application import Application
app = Application(backend="uia").connect(title="DaVinci Resolve Studio - Template")
#app.DaVinciResolveStudioTemplate.print_control_identifiers()
Title = app.DaVinciResolveStudioTemplate.['TitleEdit', 'Edit8'].wrapper_object()
Title.type_keys("Test")
it returns a syntax error
I have read teh documentation and i HONESTLY have no idea how to initiate the return with out a child window. Googing "no child_window" with multiple iterations has yielded me hours wasted and NOT ONE solution.
if text is entered the child_window appears in returns, but that isnt how the program start's
This is the native return
please, explain it for me how im supposed to seach/grab/interact with out child window? with a example please because this has me at a loss
Syntax error is in line Title = app.DaVinciResolveStudioTemplate.['TitleEdit', 'Edit8'].wrapper_object().
This line should be written as either Title = app.DaVinciResolveStudioTemplate['TitleEdit', 'Edit8'].wrapper_object() or Title = app.DaVinciResolveStudioTemplate.Edit8.wrapper_object().

How can I use multiple things to check in an else statement?

I am trying to create a machine learning program. So far, I have stored each of the 'learned' meanings in a text file in a list of all things that are attribute to that meaning. The text file is read by a python file that generates class objects that attribute the data in the text file to them. Then, in a master python file, the main prompt is where I am having trouble.
else:
try:
for obj in gc.get_objects():
try:
if isinstance(obj, LearnedClasses.learned):
if str(user.lower()) in obj.keys:
exec(obj.exstring)
chat()
break
except:
raise Exception
except Exception:
user = user.split()
for x in user:
learnlist.append(x)
learnch = random.choice(learnlist)
learnp = input("What does '{}' mean?".format(learnch))
learn(learnch, learnp)
chat()
This code is what follows the basic 'built-in' responses that I made. This is what happens after it can not find any keywords in the built-in section. I am using GC to collect all of the class objects that were generated from the text file. Then, if the prompt matches any keywords with any of its 'learned' keywords, I intend for it to respond with the responses set for that class. However, I can not get it to move on to the if ALL ELSE fails part, which starts at except Exception. How could I arrange this so that it can do what I described? Thank you.
It's a bit hard to follow this code; a couple of suggestions for improvement:
raise Exception has a typo: it should be raise Exception()
There's not much point having an except block which just raises an exception with no additional information; omit it?
The garbage collector is not a good place to store information that you will, in fact, need later; can you change the code so that the learned classes are stored in a list in a variable, which is returned or passed around somehow?
As a general rule, you should never touch the garbage collector unless you're running out of memory, and then only to figure out how to help it discard things.
As others have suggested in the comments, rather than try/except, use something like a flag variable to keep track of whether you've already answered, or the for/break/else construct:
for lc in learned_class_list:
if str(user.lower()) in lc.keys:
exec(lc.exstring)
chat()
break
else:
user = user.split()
for x in user:
learnlist.append(x)
learnch = random.choice(learnlist)
learnp = input("What does '{}' mean?".format(learnch))
learn(learnch, learnp)

Maya RuntimeError: OpenMaya.py (kInvalidParameter): Index not in valid Range

I am writing a python tool that will store attributes of the stereoscopic camera in a scene into a JSON file.
I use this code to get the selected plane with the attributes I need to store
import maya.OpenMaya as om
selected = om.MSelectionList()
om.MGlobal.getActiveSelectionList(selected)
obj = om.MObject()
selected.getDependNode(0,obj)
sel = om.MFnDependencyNode(obj).name()
if sel != "npl_Near_ZP":
cmds.confirmDialog(title= 'Error:', message = 'Error: Please Select the correct plane', button =['OK'])
else:
jsonCreate(sel)
It was working perfectly before but now when I try to the script, I get the following runtime error and I 've sort of hit a wall here cause I don't understand what the problem is
Error: RuntimeError: file S:\Maya_2018_DI\build\Release\runTime\Python\Lib\site-packages\maya\OpenMaya.py line 18296: (kInvalidParameter): Index not in valid range #
I m still new to Maya so any help will be greatly appreciated
update I restarted my computer and the script is now running as it should. Can anyone explain this behaviour in case it happens again

Python GTK get selected value from the treeview

I am working on a mini GUI project , I am currently struggling to figure out how to get selected value from the list and then return that value to the main function so that I can use that value in somewhere else . Can someone help me please !!!!
####
self.device_list_store = gtk.ListStore(str,str,str,str,str)
for device in self.get_dev_list():
self.device_list_store.append(list(device))
device_list_treeview = gtk.TreeView(self.device_list_store)
selected_row = device_list_treeview.get_selection()
selected_row.connect("changed",self.item_selected)
####
def item_selected(self,selection):
model,row = selection.get_selected()
if row is not None:
selected_device = model[row][0]
at the moment ,the item_selected function is not returning anything , I want to return selected_device back to the main function so I can use it in other functions as well .
EDIT: I've edited code above to remove formatting errors #jcoppens
As you can see in the documentation, the item_selected function is called with one parameter, tree_selection. But if you define the function inside a class, it requires the self parameter too, which is normally added automatically. In your (confusing) example, there is no class defined, so I suspect the problem is your program which is incomplete.
Also, I suspect you don't want device_list_treeview = gtk.T... in the for loop:
for device in self.get_dev_list():
self.device_list_store.append(list(device))
device_list_treeview = gtk.TreeView(self.device_list_store)
And I suspect you want selected_device = mod... indented below the if:
if row is not None:
selected_device = model[row][0]
Please convert your example in a complete program, and formatted correctly.
BTW: item_selected is not a good name for the signal handler. It is also called if the item is unselected (which is why the signal is called 'changed')!
And important: Even though you should first read the basic Python tutorials and Gtk tutorials, you should then consider using lazka's excellent reference for all the Python APIs. There's a link on the page to download it completely and have it at hand in your computer.

pyvmomi:error when calling RelocateVM

Hi I am using pyvmomi API, to perform vmotions against a cluster when DRS is set to manual mode. I am going through a vcenter and querying a cluster and getting recommendation and using that to perform the Vmotions. The code is something like this.
content=getVCContent(thisHost, {'user':username,'pwd':decoded_password},logger)
allClusterObj = content.viewManager.CreateContainerView(content.rootFolder, [pyVmomi.vim.ClusterComputeResource], True)
allCluster = allClusterObj.view
for thisDrsRecommendation in thisCluster.drsRecommendation:
print thisDrsRecommendation.reason
for thisMigration in thisDrsRecommendation.migrationList:
print ' vm:', thisMigration.vm.name
while True:
relocate_vm_to_host(thisMigration.vm.name,thisMigration.destination.name, allClusterObj.view)
#FUNCTION definition
def relocate_vm_to_host(vm, host , allCluster):
for thisCluster in allCluster:
for thisHost in thisCluster.host:
if thisHost.name == host:
for thisVm in thisHost.vm:
print 'Relocating vm:%s to host:%s on cluster:%s' %(thisVm.name,thisHost.name,thisCluster.name)
task = thisVm.RelocateVM(priority='defaultpriority')
I am getting an error saying the attribute doesn't exist.
AttributeError: 'vim.VirtualMachine' object has no attribute 'RelocateVM'
But the pyvmomi documentaion here https://github.com/vmware/pyvmomi/blob/master/docs/vim/VirtualMachine.rst
has a detailed explanation for the method
RelocateVM(spec, priority):
Anyone know what's the reason the method is missing? I also tried checking the available methods of the object ,that has RelocateVM_Task ,instead of RelocateVM(for which I couldn't find documentation) When I used that I get this error
TypeError: For "spec" expected type vim.vm.RelocateSpec, but got str
I checked the documentation for vim.vm.RelocateSpec, I am calling it in a function , but still throws an error.
def relocate_vm(VmToRelocate,destination_host,content):
allvmObj = content.viewManager.CreateContainerView(content.rootFolder, [pyVmomi.vim.VirtualMachine], True)
allvms = allvmObj.view
for vm in allvms:
if vm.name == VmToRelocate:
print 'vm:%s to relocate %s' %(vm.name , VmToRelocate)
task = vm.RelocateVM_Task(spec = destination_host)
Any help is appreciated.
Thanks
Looks like a mistake in the documentation. The method is called Relocate (and not RelocateVM).
Note, BTW, that in your first sample you're not passing the destination host to the call to Relocate so something is definitely missing there.
You can see some samples at https://gist.github.com/rgerganov/12fdd2ded8d80f36230f or at https://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py.
Lastly, one way to realize you're using the wrong name is by calling Python's dir method on a VirtualMachine object. This will list all properties of the object so you can see which methods it has:
>>> vm = vim.VirtualMachine('vm-1234', None)
>>> dir(vm)
['AcquireMksTicket', [...] 'Relocate', 'RelocateVM_Task', [...] ]
(abbreviated output)

Categories

Resources