Error when trying to perform IDEAScript extraction in Python

10 posts / 0 new
Last post
Bert_B
Offline
Joined: 11/04/2020 - 06:46
Error when trying to perform IDEAScript extraction in Python

I have an IDEA file from which I want to perform a simple extraction (column1 equals 6) and save this as a new file. My IDEAScript is as follows (see also attached file):
 
Sub Main
         Call DirectExtraction()   
End Sub
 
Function DirectExtraction
         Set db = Client.OpenDatabase("myfile.IMD")
         Set task = db.Extraction
         task.IncludeAllFields
         dbName = "outputfile.IMD"
         task.AddExtraction dbName, "", column1 == ""6"""
         task.PerformTask 1, db.Count
         Set task = Nothing
         Set db = Nothing
         Client.OpenDatabase (dbName)
End Function
 
 
This works.
 
However, I now want to do this from Python using win32com.client. So I tried (the os.path.join works and the file exists):
 
import win32com.client as win3ComClient
try:
    idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
    db = idea.OpenDatabase(os.path.join(wd, os.path.basename(prefix)+'_myfile.IMD'))
    task = db.Extraction
    task.IncludeAllFields
    dbName = os.path.join(wd, os.path.basename(prefix)+'_outputfile.IMD')
    task.AddExtraction(dbName, "", "Column1 == ""6""")
    task.PerformTask(1, db.Count)
    task = Nothing
    db = Nothing
 
finally:
    idea = None
 
However, this gives me the error AttributeError: 'function' object has no attribute 'IncludeAllFields' and it marks the line with task.IncludeAllFields. So where is the mistake? So it seems like I did not "translate" the IDEAScript VB properly to Python style. The function db.Extraction does not have the attribute IncludeAllFields. But in IDEAScript it does. How can I get this running? I also tried to write it in one line, so db.Extraction.IncludeAllFields but still I get an error. So I don't know how to configure the task in general using Python. This seems to be the source of the error.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

That is one of the quirks of IDEASCript to python, you often have to add the () to the functions.

In your case you need to add () to task.IncludeAllFields so it becomes task.IncludeAllFields().  Also you will probalby have to add () on the db.Extraction line.

Bert_B
Offline
Joined: 11/04/2020 - 06:46

Hi Brian,
 
first of all a big thanks for the fast answer and this hint! Good to know. I know changed the two lines to:
 
    task = db.Extraction()
    task.IncludeAllFields()
 
It throws no error. However, I not get an error which marks the line
task.PerformTask(1, db.Count) and says that the equation I gave is not correct. However, it is correct, I took it from the IDEAScript. I also tried to play around with the quotation marks, so I tried "Column1 == 6", but still it doesn't work. Where is the mistake, why does it not work? I also tried to add () to the db.Count, so task.PerformTask(1, db.Count()), but I get an error TypeError: 'float' object is not callable.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

When the perform task gives an error that doesn't mean that there is aproblem on that line but it could be any.  Your guess is correct that it is probably the criteria.  Python doesn't allow the two double quotes when writing the equation so you have two options.  The first is to surround it with a single quote, so your criteria becomes:

'Column1 == "6"'

or you can use slash to indicate a special character

"Column1 == /"6/""

I think it is the forward slash but it might be the backward one, I can't remember at the moment. 

I am assuming the Column1 is a character field.  Also if it is a field name it should be COLUMN1, sometimes when changing it to uppercase it might have changes that you aren't expecting.  So make sure your field names match.

Bert_B
Offline
Joined: 11/04/2020 - 06:46

Thanks Brian for the help. The code itself now seems to work, at least it starts and IDEA applies the criteria and starts the extraction. But after the criteria is applied and the extraction is done I get the error message that "the new information cannot be written to the output file". I don' know what the reason is. I thought maybe I don't have enough disk space, but this is not the problem.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Is this an IDEA error or a python error?

You can try putting print("this is line 1") into your code to try and see where the error might be happening if it is unclear from the error message.

Bert_B
Offline
Joined: 11/04/2020 - 06:46

The error is an IDEA error. I can see IDEA applying the criteria and performing the extraction. And even the file gets created, however at the end IDEA somehow tries to add additional information and this fails. When I create the file just with IDEA manually I get the same number of rows, but the size is minimal smaller.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

I just noticed and this might be the prolbem, you have 

task = Nothing
db = Nothing

Nothing doesn't mean anything in Python, it should be

task = None

db = None

You are trying to make the db equal to Nothing and that might be causing the problem.

Bert_B
Offline
Joined: 11/04/2020 - 06:46

Sorry, the mistake was on my side. It now works (with both Nothing and None, but I will take None). I somehow "distrupted" the file before with simple copying it. I did get a warning from IDEA that files of the same ID exists. I removed one, but somehow there must have been a mistake here.
When I now check it with a new folder it works.
 
So big thanks for your ongoing support!

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Glad to help.  If you need to copy or move files you are generally better to use the internal move and copy functionality in IDEA as it will take care of those type of problems.

Good luck with your project.