I am using python 2.7 and I am pretty new to python. I was wanted to ask why lines in my code are being skipped although I don't see a reason for them to be.
My code is seen below:
def add_client:
code for adding client
def check_clients:
code for listing out client info
modes = {'add': add_client, 'check': check_clients}
while True:
while True:
action = raw_input('Input Action: \n').lower()
if action in modes or ['exit']:
break
print 'Actions available:',
for i in modes:
print i.title() + ',',
print 'Exit'
if action in modes:
modes[mode](wb)
if action == 'exit':
break
When I run the code and input an action that is not in the list of modes, it does not print out 'Actions available: add, check, exit' and just seems to skip like seen below.
Input Action:
k
Input Action:
If I change the code to what is seen below it works as intended:
modes = {'add': add_entries, 'check': check_stats}
while True:
while True:
mode = raw_input('Input Action: \n').lower()
if mode not in modes:
print 'Actions available:',
for i in modes:
print i.title() + ',',
print 'End/Exit'
if mode in modes or ['end', 'exit']:
break
if mode in modes:
modes[mode](wb)
if mode in ['end', 'exit']:
break
Output:
Input Action:
k
Actions available: Add, Check, End/Exit
From my understanding, I thought that when an if statement is false, the code within the if statement is skipped and the code following should be ran, but for some reason, this doesn't seem to be the case here. Is there a reason for this or is my understanding of the if statement incorrect?
The condition action in modes or ['exit'] evaluates to True regardless of the value of action. It read like (action in modes) or (['exit']) (so you apply or operator to operands action in modes and ['exit']). Non-empty list ['exit'] evaluates to True in boolean context, so or returns True. You are suggested to use action in modes or action == 'exit' here to achieve your goal.
Related
var a = $v('P1995_LUMBER');
if ((a = '1')) {
apex.submit({
request: "CREATE",
set: {
LUMBER: "P1995_LUMBER",
LST_NME: "P1995_LST_NME",
FST_NME: "P1995_FST_NME",
},
});
} else if (a != '1') {
apex.submit({
request: "Update",
set: {
LUMBER: "P1995_LUMBER",
LST_NME: "P1995_LST_NME",
FST_NME: "P1995_FST_NME",
},
});
} else {
alert("bang bang");
}
Couple of things:
JavaScript's equality check is either == or === (more details here). (a = '1') assign '1' to the variable.
It seems like you're not using the apex.submit process correctly. Typically, you would set the item's value
e.g.:
apex.page.submit({
request: "SAVE",
set: {
"P1_DEPTNO": 10,
"P1_EMPNO": 5433
}
} );
Although, by looking at your JavaScript code, I would say you don't even need to use JavaScript.
Whenever you submit a page, all items on it are automatically sent to the server-side. You can then reference them using bind variables. You could then simply have two process, one for the Create and one for the Update, each having the corresponding insert/update statement using the different items on your page.
Usually what you will see is a page with two buttons for Create/Edit. They will have a server-side condition so that only the correct one is displayed.
Try creating a Form type page (form with report) using the wizard, and you'll see how everything is done.
Without seeing the page and the code you're using it's hard to tell what your issue really is, more details would be required.
That code does not have any sql in it so it is impossible to diagnose why you are encountering a TOO_MANY_ROWS exception. Run the page in debug mode and check the debug data - it should show you what statement is throwing the exception. If you need more help, post a proper reproducible case, not a single snipped of code without any context.
When I run this code it says ${month} not found and ${month status} is returning True:
${month status}= Run Keyword and Return Status Element Should Be Visible xpath=//div[#data-title='Cost for Month']
Run Keyword If '${month status}' == 'True'
... ${month}= Get Text xpath=//div[#data-title='Cost for Month']
... Log Dashboard is displaying ${month} Cost Usage for month Text
... ELSE
... Fail Cost Dashboard is not displaying Cost Usage for month Text
${month status}= Run Keyword and Return Status Element Should Be Visible xpath=//div[#data-title='Cost for Month']
${month}= Run Keyword If '${month status}' == 'True' Get Text xpath=//div[#data-title='Cost for Month']
Run Keyword If '${month status}' == 'True' Log Dashboard is displaying ${month} Cost Usage for month Text
Run Keyword If '${month status}' == 'False' Fail Cost Dashboard is not displaying Cost Usage for month Text
A bit of a crude solution but we came across this situation as well, and solved it using the fix above.
I think your code was failing because ${month} was being interpreted as a keyword rather than a variable.
In my company we have coding styles, that include always using curly braces with an if-statement, so:
// instead of this...
if (condition)
doThis();
// we want this
if (condition) {
doThis();
}
Now not everyone has done this since the start and we would like to clean up and make the code more consistent. Is there a way (with PHPStorm preferably) to find all braceless if-statements?
If you have python installed, you can run following program, I wrote for you, to find all such lines in your code: (tested on python27)
run as : python bracketless.py yourfile
import sys,re
fname = sys.argv[1]
with open(fname) as f:
if_block = False
lnum = 0
for i,line in enumerate(f):
if (re.search(r'\bif\b', line)) and not '{' in line:
if_block = True
lnum = i+1
if ';' in line:
print 'Found bracketless if in line {}'.format(lnum)
if_block = False
elif if_block:
if '{' in line:
if_block = False
elif ';' in line:
print 'Found bracketless if in line {}'.format(lnum)
if_block = False
You could try to use a regular expression search as follows:
Edit > Find > Find in Path
Search for the string if\s*\(.+\)[^{\S]*\n, and be sure that the Regular expression checkbox is active.
This technique isn't perfect, as it will yield false positives for conditionals spanning multiple lines (such that their opening brace is on line 2, 3, etc...). But it can accurately distinguish between simple single-line conditionals, like the two you provided in your question.
You could also simply use PhpStorm's built in code style tools for this. If you go to File -> Settings and then to Editor -> Code Style -> PHP -> Wrapping and Braces you can set force braces for if/for/foreach/while/do...while to 'always'. Braces will then automatically be added when reformatting your code.
However, this approach obviously has the "side effect" of applying all other formatting rules as well. That may not be what you are looking for.
I am using dockyard/ember-validation to validate controller properties, but I haven;t been able to get it working as I expected it to. So I have basically toggle effect on checkboxes, and I have defined 2 validation rules and only one of them should be executed/validated when user toggles between the checkboxes.
My validation rules are defined as :
validations: {
"instructions": {
format: {
if: 'inlineSource',
'with': /^(?!\s*$).+/
}
},
"externalSourceValue": {
format: {
if: 'externalSource',
'with': /^(?!\s*$).+/
}
}
}
Here either inlineSource is true or externalSource is true, but both will never be simultaneously true. I would expect only one validation rule to be exercised, but it seems both are getting run disregarding the if condition there.
Here is the jsbin to the issue: http://jsbin.com/ODAmukOM/1/
Follow these steps :
1) click on External Website
2) set the input field value to empty
3) click to Content I Specify
4) the validation sets the controller to invalid state
Thanks,
Dee
you are adding 2 format validations,
instructions is valid if inlineSource && /^(?!\s*$).+/
really your logic needs to be more like this
"instructions": {
format: {
if: function(object, validator) {
console.log('inlineSource',object);
if(!object.get('inlineSource')){
return (object.get('instruction') || '').match(/^(?!\s*$).+/);
}
return true;
}
}
}
unfortunately validation is only run when you change properties, so you change instruction validation is run and it's invalid, you then switch (flipping inlineSource), but validation won't happen again. At this point you'll need to manually run validations to get that validation checked again. good luck
I'm trying to make a conditional statement based on whether a checkbox is checked or not. I've tried something like the following, but it always returns as true.
self.folderactive = QtGui.QCheckBox(self.folders)
self.folderactive.setGeometry(QtCore.QRect(50, 390, 71, 21))
self.folderactive.setObjectName(_fromUtf8("folderactive"))
if self.folderactive.isChecked:
folders.createDir('Desktop')
print "pass"
elif not self.folderactive.isChecked:
folders.deleteDir('Desktop')
print "nopass"
Is there a way to get a bool value of whether a checkbox is checked or not?
self.folderactive.isChecked isn't a boolean, it's a method - which, in a boolean context, will always evaluate to True. If you want the state of the checkbox, just invoke the method:
if self.folderactive.isChecked():
...
else:
...
x = self.folderactive.isChecked()
x will be True or Falseāa Boolean value.
(It's the brackets at the end that make the difference.)