issue with required parameter - python-3.x

Create a new function named defaults with two parameters:
my_optional which has a default value of True
my_required which is a required param and has no default value
Return the following logical comparison:
my_optional is my_required
Expected Output
defaults(True)
True
defaults(True, False)
False
defaults(False, False)
True
def defaults(my_optional, my_required)
my_optional = True
return my_optional is my_required
i received the error *defaults is missing positional argument: my_required

Related

How to go to first line of a function if a condition is not satisfied in python?

I want to got First line of the function goToSignUpActivit() if isAllInputValid == False
from CustomUtil import *
from Register import *
def goToSingUpActivity():
fName = input("Enter your First Name:")
lName = input("Enter your last name:")
mobileNum = input("input your mobile number")
role = input("Select your role s: for staff p: for patient:")
isFNameValid = validateInput(fName)
isLNameValid = validateInput(lName) // this function in CustomUtil package
isRoleValid = False
if (role in ["s","p","S","P"]):
isRoleValid = True
isMobileNumValid = validateMobilNum(mobileNum)// this function in CustomUtil package
isAllInputValid = False
if (isFNameValid and isLNameValid and isRoleValid and isMobileNumValid) :
isAllInputValid = True
if (isAllInputValid) :
isAllInputValid = True
if role in ["s","S"]:
registerAsStaff(fName,lName,mobileNum)// this function in Register package
return
elif role in ["p","P"]:
registerAsPatient(fName,lName,mobileNum)// this function in Register package
return
while(not isAllInputValid):
if(input("you want to exit y or n:") in ["y","Y"]):
return
goToSingUpActivity()
Above code put goToSingUpActivity() into the stack while isAllInputValid become true.
After I enter all input correctly stack pop goToSingUpActivity() one bye one.
What I need here is after isAllInputValid becomes true, clear all goToSingUpActivity() from stack.
If above solution not possible, is there any other way to modifie the code?
Edit: I got a solution by making isAllInputValid global and put the while loop outside the function after calling goToSingUpActivity().
You can use a while statement (WARNING to don't create a while(true), give to the user an opportunity to cancel his action !).
def goToSingUpActivity():
 isAllValid = False
 while(!isAllValid):
#your inputs stuff
if(isFNameValid and isLNameValid and isRoleValid and isMobileNumValid):
   isAllValid = True
 #rest of the function

Python enum set as parameter in a function

I am looking at an python API with the following function example:
bpy.ops.object.bake(type='COMBINED', pass_filter={"DIFFUSE", "DIRECT"})
while the pass_filter parameter accepts one or more of any of the following:
pass_filter (enum set in {
'NONE', 'AO', 'EMIT',
'DIRECT', 'INDIRECT',
'COLOR', 'DIFFUSE', 'GLOSSY',
'TRANSMISSION', 'SUBSURFACE',
})
on the other hand I have the following to determine whether or not the parameters should be added to pass_filter:
is_NONE = False
is_AO = True
is_EMIT = False
is_DIRECT = True
#..etc.
How do I insert these to the function, like a list or array to the parameter?
The key to this is knowing {"DIFFUSE", "DIRECT"} represents a set:
#!/usr/bin/python3
is_NONE = False
is_AO = True
is_EMIT = False
is_DIRECT = True
pass_filter = set()
if is_AO:
pass_filter.add('AO')
if is_DIRECT:
pass_filter.add('DIRECT')
print(pass_filter)
See it here
Feel free to add your extra if statements!

How do I escape true/false in terraform?

I need to pass the word true or false to a data template file in terraform. However, if I try to provide the value, it comes out 0 or 1 due to interpolation syntax. I tried doing \\true\\ as recommended in https://www.terraform.io/docs/configuration/interpolation.html, however that results in \true\, which obviously isn't right. Same with \\false\\ = \false\
To complicate matters, I also have a scenario where I need to pass it the value of a variable, which can either equal true or false.
Any ideas?
# control whether to enable REST API and set other port defaults
data "template_file" "master_spark_defaults" {
template = "${file("${path.module}/templates/spark/spark- defaults.conf")}"
vars = {
spark_server_port = "${var.application_port}"
spark_driver_port = "${var.spark_driver_port}"
rest_port = "${var.spark_master_rest_port}"
history_server_port = "${var.history_server_port}"
enable_rest = "${var.spark_master_enable_rest}"
}
}
var.spark_master_enable_rest can be either true or false. I tried setting the variable as "\\${var.spark_master_enable_rest}\\" but again this resulted in either \true\ or \false\
Edit 1:
Here is the relevant portion of conf file in question:
spark.ui.port ${spark_server_port}
# set to default worker random number.
spark.driver.port ${spark_driver_port}
spark.history.fs.logDirectory /var/log/spark
spark.history.ui.port ${history_server_port}
spark.worker.cleanup.enabled true
spark.worker.cleanup.appDataTtl 86400
spark.master.rest.enabled ${enable_rest}
spark.master.rest.port ${rest_port}
I think you must be overthinking,
if i set my var value as
spark_master_enable_rest="true"
Then i get :
spark.worker.cleanup.enabled true
spark.worker.cleanup.appDataTtl 86400
spark.master.rest.enabled true
in my result when i apply.
I ended up creating a cloud-config script to find/replace the 0/1 in the file:
part {
content_type = "text/x-shellscript"
content = <<SCRIPT
#!/bin/sh
sed -i.bak -e '/spark.master.rest.enabled/s/0/false/' -e '/spark.master.rest.enabled/s/1/true/' /opt/spark/conf/spark-defaults.conf
SCRIPT
}

Retrieving value using xpath in SoapUI Groovy step

I need that the output of an operation GET ISM is transferred to the input of another operation SET ESM.
I want to recuperate talon=603090100042390 in this tag (this is response of GET ISM):
<privateUserId>603090100042390#xxxxxxxxxxxxxx</privateUserId>
I use this script:
groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
holder = groovyUtils.getXmlHolder("GET ISM#Response")
privateUserId = holder.getNodeValue( "//privateUserId" )
assert privateUserId != null
assert privateUserId.length() > 0
latlonNode = groovyUtils.getXmlHolder(privateUserId)
latlon = latlonNode.getNodeValue("//privateUserId")
log.info(latlon)
assert latlon != null
context["latlon"] = latlon
talon is input for SET ESM.
I have this Error :
Assertion failed: assert privateUserId != null | | null false Assertion failed: assert privateUserId != null | | null false error at line: 4
but i don't know why the problem in line 4.
I want to fixed this problem. Thank's
From your question it is not clear if there any namespaces in the response. Possibly that could be one of the reason to get null.
You could use Script Assertion for the first step.
assert context.response, 'Response is empty or null'
def pId = new XmlSlurper().parseText(context.response).'**'.find{it.name() == 'privateUserId'}.text()
log.info "privateUserId value : $pId"
assert pId, "Value of privateUserId is empty or null"
def userId = pId?.substring(0, pId?.indexOf('#'))
context.testCase.setPropertyValue('USERID', userId)
In the next step, wherever extracted user id is needed, use ${#TestCase#USERID}

cx_Oracle gives OCI-22062 invalid input string when calling function with bool parameter

I have a strange problem. When I try to call an oracle stored function using cursor.call_func method I receive an OCI-22062 exception. I'm using Python 3.2 and cx_Oracle 5.1.2. It generally looks like this:
This is the function header for package pkg_planista:
FUNCTION zatwierdz_plan (p_pl_id IN dz_plany.ID%TYPE,
p_czy_nowy_algorytm IN BOOLEAN DEFAULT FALSE,
p_czy_prz_grup IN BOOLEAN DEFAULT FALSE
) RETURN NUMBER;
This is what I do:
print(proc_name, retType, params, keyword_params)
res = self.cursor.callfunc(proc_name, retType, params, keyword_params)
This print above prints:
pkg_planista.zatwierdz_plan <class 'int'> [83, False] {}
And when the callfunc is executed, the OCI-22062 error is raised: cx_Oracle.DatabaseError: OCI-22062: invalid input string [False]
How should I pass the boolean parameter to make it work?
I had to find a workaround for this problem, because I couldn't find a way to make the callfunc work as it should.
In this workaround I use one temporary table that was created for some other purpose, but it's just what I need.
I changed callfunc to execute and made it like this:
self.cursor.execute("""
declare
result number;
begin
result := pkg_planista.zatwierdz_plan(:pid, :alg = 1);
INSERT INTO tmp_dz_v_planista_pracownicy values (result);
end;
""", {'pid': pl_id, 'alg': int(new_algorithm)})
ret = self.cursor.execute("SELECT * FROM tmp_dz_v_planista_pracownicy")
This way I can call the function and receive it's return value. The tmp_dz_v_planista_pracownicy table is just a temporary table with one column of type NUMBER.

Resources