I have noticed a particular feature in Visual Studio and I am wondering if this feature is also available in Eclipse + PyDev.
In Visual Studio, if one were to type a function call and that particular function does not already exist, VS would show a code error and give an option to generate a new empty function matching the signature provided in the function call.
In other words, same I am working in a particular Python function or class and I realize I need a new function to process some string. In my current function I type processString(myString), which returns an error because the processString function does not currently exist. Is there some way to then click on the processString function call and create a new block in my module:
def processString(myString):
pass
Thanks in advance for your help.
Thank you #Eric Wilson.
If I type the function call processString(myString) then hit 'CTRL+1' the code completion/template window appears offering me the option to create a new class, method, assign to a field, or assign to a variable.
This was exactly what I was looking for.
Related
The documentation states:
Mode: It is one of “array”, “read”, “write”, “unset”, or a list or tuple of such strings. but I'm not sure I understand what it means.
I'm using trace_add to call a function whenever my object tk.StringVar changes value and I'm using the mode write without really understanding why.
EDIT: what I find online is only about the deprecated method trace; nothing about the method trace_add.
Using the value "write" means you want the callback to be called whenever the variable is written (ie: changed; when the set method is called).
Using the value "read" means you want the callback to be called whenever the variable is read (ie: the get method is called).
Using the value "unset" means you want the callback to be called whenever the variable is unset. In the case of tkinter, this happens when the variable is deleted.
The canonical tcl documentation for "array" says: "Invoke commandPrefix whenever the variable is accessed or modified via the array command". Tkinter doesn't directly give access to tcl's array command, so this one isn't very useful in tkinter.
So I'm trying to write a python program to integrate into a greater in-house developed python application. This program I'm writing needs to generate an xml document and populate the fields with data stored in variables from another function in a different module.
After realizing I can't have both programs import each other (main program needs to call xmlgen.py to generate the xml doc, while xmlgen.py needs to utilize variables in the main program to generate that doc), I'm a little bit at a loss as to what to do here.
In the example shown below, xmlgen.py needs to use variables from the function sendFax in Faxer.py. Faxer.py needs to call xmlgen.py to generate the document.
snippet from xmlgen.py:
from lxml import etree
from Faxer import coverPage, ourOrg, ourPhonenum, ourFaxnum, emailAddr, sendReceipt, webAddr, comments
from Faxer import sendFax
def generateXml():
#xml file structure
root = etree.Element('schedule_fax')
...
~ A bunch of irrelevant xml stuff
...
grandchild_recipient_name = etree.Element('name')
grandchild_recipient_name.text = cliName
child_recipient.append(grandchild_recipient_name)
Now the piece of the main program I need to utilize the "cliName" variable from...
def sendFax(destOrg, destFax, cliName, casenum, attachments, errEAddr, comment, destName):
creds=requests.auth.HTTPBasicAuth(user,password)
allData=''
allData+='<schedule_fax>\n'
allData+='<cover_page>\n'
allData+='<url>'+prepXMLString(coverPage)+'</url>\n'
allData+='<enabled>true</enabled>\n'
allData+='<subject>'+prepXMLString(cliName)+' - case # '+str(casenum)+'</subject>\n'
Now when I try to import sendFax function from Faxer.py, I'm unable to call any of the variables from the function like,
grandchild_recipient_name.text = sendFax.cliName
does not work. What am i doing wrong here?? I'm not a python guru and am in fact quite new to all of this, so I'm hoping it's something simple. Should I just dump everything into a new function in the main program?
As pointed out above, you are trying to reference cliName as if it is an attribute of the function. This would be closer to being correct if sendFax was a class, but that's another subject. The snippet you have provided is simply a function definition. It doesn't guarantee that this function is ever actually used or give you any idea what cliName actually is, cliName is just the name used by the function internallt to describe the 3rd value supplied as input.
What you need to do is find where sendFax is actually used, rather than where it is defined. Then look at what the variables are called which are passed into it. There are two ways to pass variables into a function: by position and by name. If the variables are being passed by pposition you will find something like:
sendFax(some_name,some_other_name,yet_another_name,...
The third one of these will be the variable which becomes cliName inside the function.
If being passed by name you will see something like
sendFax(cliName=yet_another_name,...
Where once again yet_another_name is the thing which becomes cliName.
Depending on how the programme is structured you may be able to refer to yet_another_name from your program and get the value you need.
from Faxer import yet_another_name
But this will only work if Faxer runs and finishes with the one and only value of yet_another_name assigned. If Faxer iterates through lots of values of yet_another_name, or simply doesn't run sensibly when called as an import you'll need a more sophisticated approach.
I'm running Odoo 12 and I'd like to set up an automated action after a new Opportunity is created.
This automated action would be some custom Python code which calls a system parameter, gets its value and stores it in a field in the new opportunity's record.
Afterwards, that system parameter should be increased by 1 so another new opportunity doesn't have the same number.
How do you get/set a system parameter from python code?
I've created a system parameter with key "customParameter"and value 10122.
The field where this parameter should be put is called "x_studio_deal_quotation_id", within a new opportunity.
I've got a little experience in Python, so I've no idea how to call upon this parameter. Can someone help me out? Or is there an easier way to achieve the same?
A big thanks in advance.
I managed to find what I'm looking for on my own, this is the code I used:
record[("x_studio_deal_quotation_id")] = record.env['ir.config_parameter'].get_param('customParameter')
var = record.env['ir.config_parameter'].get_param('customParameter')
var = int(var)
var += 1
record.env['ir.config_parameter'].set_param('customParameter', var)
In this function I called my system parameter by using record.env['ir.config_parameter'].get_param('customParameter')
I had to convert it to int because I got some error that the type wasn't right.
Finally, I add 1 to my variable value and write the same system parameter with the new value by using:record.env['ir.config_parameter'].set_param('customParameter', var)
I am looking for a way in python to stop certain parts of the code inside a function but only when the output of the function is assigned to a variable. If the the function is run without any assignment then it should run all the inside of it.
Something like this:
def function():
print('a')
return ('a')
function()
A=function()
The first time that I call function() it should display a on the screen, while the second time nothing should print and only store value returned into A.
I have not tried anything since I am kind of new to Python, but I was imagining it would be something like the if __name__=='__main__': way of checking if a script is being used as a module or run directly.
I don't think such a behavior could be achieved in python, because within the scope of the function call, there is no indication what your will do with the returned value.
You will have to give an argument to the function that tells it to skip/stop with a default value to ease the call.
def call_and_skip(skip_instructions=False):
if not skip_instructions:
call_stuff_or_not()
call_everytime()
call_and_skip()
# will not skip inside instruction
a_variable = call_and_skip(skip_instructions=True)
# will skip inside instructions
As already mentionned in comments, what you're asking for is not technically possible - a function has (and cannot have) any knowledge of what the calling code will do with the return value.
For a simple case like your example snippet, the obvious solution is to just remove the print call from within the function and leave it out to the caller, ie:
def fun():
return 'a'
print(fun())
Now I assume your real code is a bit more complex than this so such a simple solution would not work. If that's the case, the solution is to split the original function into many distinct one and let the caller choose which part it wants to call. If you have a complex state (local variables) that need to be shared between the different parts, you can wrap the whole thing into a class, turning the sub functions into methods and storing those variables as instance attributes.
I am trying to figure out what the required string argument for the nuke.ChannelMask_Knob() function corresponds to. On some other knob constructors the first argument seems to be the name/label, but that does not seem to be the case for the ChannelMask_Knob...
I have looked at the Nuke Python API, but I am unsure how to follow it back to the appropriate function definition to answer my question. My line of thinking is that this has to do with the init function which is overridden by the ChannelMask_Knob class, but the parameter list according to the API is just "..." which I believe means it has to do with a builtin function. Since I can't see the body of the init function, I have no idea what that argument is used for, thus my problem...
Here is an example of my issue:
test_knob = nuke.ChannelMask_Knob("required_argument")
node.addKnob(test_knob)
This works just fine, but I would like to know what the "required_argument" is used for since it is apparently not the name or label for the knob.
You must intentionally leave a blank space for parameter in ChannelMask_Knob('') method. It doesn't work. But for assigning a name and a label you need to use .setName('name') and .setLabel('label') methods respectively. It works fine.
Here is a code:
import nuke
noop = nuke.nodes.NoOp()
### maskKnob = nuke.ChannelMask_Knob('name', 'label', False)
maskKnob = nuke.ChannelMask_Knob('')
maskKnob.setName('name')
maskKnob.setLabel('label')
noop.addKnob(maskKnob)
For selection of channel in this dropdown menu it's better to use the following method:
nuke.selectedNode().knob('name').setValue('alpha')
nuke.selectedNode().knob('name').setValue('disparity')