tags:

views:

318

answers:

4

Ok, here are some easy points. PyBinding came with this script:

def IsNotNull(value):
    return value is not None

It is close, but what I want is this.

bool IsNotNullOrEmpty(string value) {
    return (value != null) && (value.Length > 0 );
}

EDIT WTF?

Someone gives the right answer "return value is not None and len(value) > 0", then deletes his post before I can say thank you. And what's left is a three people spouting off nonsense that doesn't even come close to giving the right result.

+1  A: 

To check if a string is empty you would use len. Try this:

def IsNotNull(value):
    return value is not None and len(value) > 0
Brian McKenna
Wrong again. Did you even bother trying this code?
Jonathan Allen
Why have I been downvoted? `IsNotNull('test') is True` `IsNotNull('') is False` `IsNotNull(None) is False` - all the functionality is there!
Brian McKenna
1. Because stack overflow sucks and I can't change my vote now that I see that you wote "not not" instead of "not". 2. I'm pissed off that the nice answer was deleted and replaced with ugly abuses of implicit conversions. Especially since, aside from yours, they are all wrong.
Jonathan Allen
There you go, I've rolledback to my original answer. I deleted my post and then replaced it with what I believed to be a "Pythonic" solution, which involves the `not not`.
Brian McKenna
-1 This is not necessary in Python as None and '' are False in Python: http://docs.python.org/library/stdtypes.html#truth-value-testing
Lukas Cenovsky
@Lukas, view the history of this answer. Jonathon didn't like that solution and it doesn't always work for IronPython classes.
Brian McKenna
@Brian. Well, this is not Pythonic but it is fine hack to make PyBindig work if my last comment at @Ignacio's answer is true.
Lukas Cenovsky
A: 

i think,

if IsNotNull(value) {

is equivalent to

if not value:

for strings. so i think the function is not necessary in python.

Dyno Fu
A: 
def IsNotNullString(s):
    return bool(s)

Rules of Python boolean conversion.

Max Shawabkeh
Didn't work. Empty strings still came back as True.
Jonathan Allen
+3  A: 

You should not be doing this in a function. Instead you should just use:

if someStringOrNone:
Ignacio Vazquez-Abrams
Didn't work. Empty strings still came back as True.
Jonathan Allen
This is the preferred Pythonic version and it does work perfectly for Python's strings. The only reason it might not work for you is if you are passing some .NET type that is not compatible with Python strings to the function.
Max Shawabkeh
Works for me:`IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.1433``Type "help", "copyright", "credits" or "license" for more information.``>>> s=''``>>> if s:``... print "Yes"``...` `>>> s='*'``>>> if s:``... print "Yes"``... ``Yes``>>> `
Ignacio Vazquez-Abrams
@Jonathan I don't think you are passing empty string to the test. As Ignacio showed you, it works for empty strings. Can you show us code where empty string evalutes as True? I think that would be a bug then.
Lukas Cenovsky
The empty string was being stored in a WPF control and routed to an IronPython function via PyBinding. I have no idea what a "Python string" is or whether or not it is compatible with a System.String.
Jonathan Allen
I don't know PyBinding but this looks to me like PyBinding handles values as richer objects. So then its empty string is not '' but some instance of "PyBindingValue" class which is definitely not False.
Lukas Cenovsky
Print the `repr()` of the value so we can take a look.
Ignacio Vazquez-Abrams