String is null or empty
Asked Answered
C

6

9

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 );
}
Charin answered 25/1, 2010 at 1:35 Comment(1)
Well, .NET includes String.IsNullOrEmpty. Would that work?Irick
R
19

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
Redolent answered 25/1, 2010 at 1:41 Comment(4)
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.Redolent
-1 This is not necessary in Python as None and '' are False in Python: docs.python.org/library/stdtypes.html#truth-value-testingFeatherhead
@Lukas, view the history of this answer. Jonathon didn't like that solution and it doesn't always work for IronPython classes.Redolent
@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.Featherhead
S
5

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

if someStringOrNone:
Systematic answered 25/1, 2010 at 1:48 Comment(5)
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.Openeyed
@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.Featherhead
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.Charin
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.Featherhead
Print the repr() of the value so we can take a look.Systematic
M
3

If it's IronPython, then why not use the default implementation of IsNullOrEmpty from System.String?

import clr
clr.AddReference('System')
import System
System.String.IsNullOrEmpty('') # returns True
System.String.IsNullOrEmpty(None) # returns True
System.String.IsNullOrEmpty('something') # returns False
Madian answered 14/12, 2010 at 11:40 Comment(0)
O
1
def IsNotNullString(s):
    return bool(s)

Rules of Python boolean conversion.

Openeyed answered 25/1, 2010 at 1:48 Comment(1)
@JonathanAllen I just ran this in the python interpreter and it worked for me. In which version of python did you get a True? I'm using 2.7.3Stereophonic
T
0

i think,

if IsNotNull(value) {

is equivalent to

if not value:

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

Truditrudie answered 25/1, 2010 at 1:46 Comment(0)
V
0
if not value or len(value)==0:
    return True 
else:
    return False

Try this one. I recommend you to read this book:https://play.google.com/store/apps/details?id=com.gavin.gbook

Vinegarish answered 24/8, 2014 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.