pyvmomi specific folder name - python-3.x

Struggling with grasping OOP and pyvmomi in general. Have a question about calling a specific folder by name - it seems that the object names can be folder or VM, and calling hasattr is the best way to differentiate the two?
Also, when I want to call the contents of a specific folder, I've been setting a variable with the name of the folder and looping through each object checking for a match, isn't there a better way, like calling specific object and folder name? I can't see to get it work. Here is what I have right now:
vmfolder = datacenter.vmFolder
vmfolderList = vmfolder.childEntity
folder = "FooFolder"
for i in vmfolderList:
try:
folderName = i.name
if folderName == folder:
vmfolder = i
print(f"Working in folder: {folderName}")
except:
pass
What I have right now doesn't feel very pythonic. I used to write code years ago but it was entirely procedureal and the whole object/attribute thing is a struggle - if anyone recommends a good tutorial or resource as well, it would be appreciated. I feel that once I get over that hurdle I should be able to pick up any SDK and run with it.

Instead of testing using hasattr I would expect folder and VM objects to be of a different type. Checking for a type is entirely pythonic.

Related

How to create same path to directory on different platforms in Python?

In the piece of code I have, there are many instances where I have the following line
'/home/myname/directory'
For example, I have the following lines of code
filepath = os.listdir('/home/myname/directory')
for content in filepath
# do something
In the next part of the project, I have to share the code with some one else. I know this person runs openSUSE. If I want code to create that specific directory with the same path to the directory as mine, what do I need to include?
I know its going to involve the OS module but i am not sure which functions and methods to use.
Your code can check the existence of the directory and create it if not found:
if not os.path.exists("/home/myname/directory"):
os.makedirs("/home/myname/directory")
# do something

How can I set an universal working directory?

I'm setting up a new algorithm and I want to share it on another computer. How can I set paths with only the folder name, for example.
I've tried to use the function os.chdir(path), but I don't achieve to reach my folder when I'm not using a 'root path' like C:/../../folder.
os.chdir(../folder_name)
By doing os.chdir(../folder_name), you are referring to the parent dir of the current directory.
You probably are looking for the os.chdir(./folder_name), with one dot? This is the current directory, where your script stands.
More here: https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats
Update: After clarification in the comments, you want to refer to folder_name from one of its subfolder (/folder_name/library). Then it is indeed .. that you should use, but .. only:
os.chdir(..)

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.

Base folder in directory structure

So I'm fairly new to UNIX (This might be basic but I couldn't find a good answer) and I'm trying to run some code I got from the web. In the README it says:
"If you put these 3rd-party packages in a pathUtils folder in the same base
folder as the shadowDetection, they should be picked up automatically by
setPath. "
Does this mean I need to create a pathUtils folder in the same directory as shadowDetection? So it would look like:
/path/shadowDetection
/path/pathUtils
or would it look like
/path/shadowDetection/pathUtils
Your help and understanding is greatly appreciated.
This ReadMe you're referencing (from https://github.com/jflalonde/shadowDetection) is unclear, since it's not clear whether "shadowDetection" refers to the shadowDetection folder, or the shadowDetection software. If it's the former, then your first example (pathUtils alongside shadowDetection folder) would make sense; if it's the latter, then your second example (pathUtils inside shadowDetection folder) would apply.
My guess is that the author means parallels so I'd try it your first way first, but since it's ambiguously worded, try it the other way if it doesn't work. Once you figure it out, email the author and suggest he clarify his ReadMe.

How to get path to UITestActionLog.html from code

Each test case saves results to a separate UITestActionLog.html file. But in the end of each test case I'd like to move that .html to a different folder and rename it.
Is it possible to do so in, say, [TestCleanup()]? If yes, then how can I programmatically get .html report location?
The TestContext class contains several fields with "directory" in their names. These can be used to access the various directories associated with running the tests.
As well as managing the files as asked by your question the TestContext class has an AddResultFile method. The Microsoft documentation on this mehod is not clear, but it seems that the files are saved for failing tests and discarded for passing tests.
To get the directory in which the UITestActionLog file will be located, use the TestContext.TestResultsDirectory Property. You can use below code to get the full path:
string fullPath = TestContext.TestResultsDirectory +"\" +"UITestActionLog.html";

Resources