I have a function which could get a String formatted like this:
"true"^^<http://www.w3.org/2001/XMLSchema#boolean>
"100"^^<http://www.w3.org/2001/XMLSchema#int>
Now i want to split the String on the ^^ Characters and convert the first part of the string based on the second part. I also want to remove the " first before converting.
This is my code which i use for this:
def getValue(tObject):
toReturn = tObject.split("^^")
if len(toReturn) == 2:
if toReturn[1] == "<http://www.w3.org/2001/XMLSchema#boolean>":
return bool(toReturn[0].replace('"', ""))
elif toReturn[1] == "<http://www.w3.org/2001/XMLSchema#int>":
return int(toReturn[0].replace('"', ""))
return None
But i'm not so happy with it. Is there maybe a more elegant (pythonic) way to archive this?
You can use a regex, to
check if the given value is valid
retrieve the value to cast, and the way to cast
PATTERN = re.compile(r'"(.*)"\^\^<http:.*#(\w+)>')
types = {"boolean": bool, "int": int}
def getValue(value):
m = PATTERN.fullmatch(value)
return types[m.group(2)](m.group(1)) if m else None
Instead of if len(...) you could just try to unpack the result and except a ValueError. Then you can use a dict for the types and str.strip instead of str.replace:
types = {'boolean': bool, 'int': int}
try:
value, type_hint = tObject.split('^^')
except ValueError:
return None
else:
return types[type_hint.rstrip('>').rsplit('#', 1)[1]](value.strip('"'))
Firstly, you could remove return None, since the function returns None by default.
Secondly, you could use toReturn[1].endswith("boolean>") to match the end of the string, instead of matching the whole string with toReturn[1] == "<http://www.w3.org/2001/XMLSchema#boolean>". Same with the int string as well.
Thirdly, you could store the return value in one variable before the if..elif, then you don't have to calculate it twice for each condition.
Code:
def getValue(tObject):
toReturn = tObject.split("^^")
if len(toReturn) == 2:
return_value = toReturn[0].replace('"', "")
if toReturn[1].endswith("boolean>"):
return bool(return_value)
elif toReturn[1].endswith("int>"):
return int(return_value)
This might not be much of a logic improvement, but the code does look less cluttered now. If you wan't more terse, "pythonic" ways of doing this problem, the other answers might be more suitable.
I'm try to do something like that in Python:
return None if a is None
Instead of having:
if a is None:
return None
But I get an invalid syntax error.
I tried to use pass:
return None if a is None else pass
But it doesn't work as well. Is there a pythonian way to do it?
return None if ...
is unconditional return of a conditional value.
If you want conditional return, all what you can change is put it on single line, like that:
if a is None: return
The syntax:
c = a if condition else b
is a proxy for:
if condition:
c = a
else:
c = b
And MUST have both branches of the condition (as detailed in the grammar description from the official documentation and further discussed in the corresponding PEP), which is missing in your code. More discussion on the if/else ternary operator in Python here.
The same holds true for return:
return a if condition else b
is equivalent to:
if condition:
return a
else:
return b
In your code, if you do not have instructions after that condition, you could use:
return None if a is None else None
which turns out to be a pretty useless statement, as can be replaced with:
return None
or even just omitted altogether.
Finally:
if a is None:
return None
is already the Pythonic version of the code.
Acceptable alternatives include:
if a is None:
return
if a is None: return None
if a in None: return
PEP8 Style Guide for Python Code suggests avoiding putting multiple statements on the same line:
Compound statements (multiple statements on the same line) are generally discouraged.
however, is open to small-body one-liners:
sometimes it's okay to put an if/for/while with a small body on the same line
Am new to python and I had to create an object only with certain attributes that are not None. Example:
if self.obj_one is None and self.obj_two is None:
return MyObj(name=name)
elif self.obj_one is not None and self.obj_two is None:
return MyObj(name=name, obj_one=self.obj_one)
elif self.obj_one is None and self.obj_two is not None:
return MyObj(name=name, obj_two=self.obj_two)
else:
return MyObj(name=name, obj_one=self.obj_one, obj_two=self.obj_two)
Coming from a Java land I know python is full of short hand so wanted to know if there is a cleaner way for writing the above? Of course my actual object has plenty more attributes. I tried searching but couldn't find anything helpful so am in doubt if its possible or not cause this doesn't scale if there are more than 2 variable attributes.
One way could be using the double-star operator, like this:
kwargs = {'name': name}
if self.obj_one is not None:
kwargs['obj_one'] = self.obj_one
if self.obj_two is not None:
kwargs['obj_two'] = self.obj_two
return MyObj(**kwargs)
In plain words: you construct a dictionary with your keyword arguments, and then pass that dictionary preceded by ** to the callable.
However, None is often (not always) used as a default value for optional arguments. Probably this will work too:
return MyObj(name=name, obj_one=self.obj_one, obj_two=self.obj_two)
without any if or that sort of stuff.
for the following code
a =func()
if a != None:
b.append(a)
a can be assigned to None, is there a way to avoid the if statement and only use one line of code?
original problem is the following
import xml.etree.ElementTree as etree
r = etree.parse(f).getroot()
b = etree.Element('register',{})
a = r.find('tag_name') # a may get None if did not find it
if a != None:
b.append(a)
ok, I used all the answers and got this, personally I think it's the most complex python I have ever wrote so far, lol
NS_MAP = {
'spirit' : 'http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4',
'app' : 'http://www.app.com/SPIRIT-app'
}
mp=etree.Element('MemoryProperty', {'version':'alpha'})
mpt=etree.ElementTree(mp)
def copy_tags(tp, op, p, tn, ns='spirit'):
c = p.find('{%s}%s'%(NS_MAP[ns],tn))
if c is not None:
(op == '<-') and tp.append(c)
return c
for reg in regs:
te = etree.Element('register',{})
copy_tags(te,'<-',reg,'name')
copy_tags(te,'<-',reg,'addressOffset')
copy_tags(te,'<-',reg,'access')
(lambda e, t: copy_tags(te,'<-',t,'usageConstraints',ns='app') if t is not None else None)(te, copy_tags(te,'|',reg,'vendorExtensions'))
mp.append(te)
mpt.write('map_gen.xml')
If you can call func() beforehand, and you want to combine the test and assignment statements into a single statement, then you can do this, with an if-else expression:
b += [a] if a is not None else []
If a is not None, then this will add [a] to b -- essentially the same operation as b.append(a)
If a is None, then this will add [] to b, which will leave b unchanged.
This won't work unless b is a list, or at least supports "+=" in-place addition. If it doesn't -- perhaps it's some custom object, then you should be able to do this:
(b.append(a) if a is not None else None)
This is an expression, evaluated for its side effects, and then thrown away. If a is None, then the b.append(a) call will never be executed. In either case, the value of the expression is None, but we don't care about it, so it gets ignored.
Now, if you want to combine the func() call with this, then you'll have to do something different in order to avoid calling func twice. If you can use the "+=" syntax, then you can do it like this:
b += filter(None, [func()])
filter(None, <list>) returns the list with all false elements (None included, but also 0 and []) removed. This statement, then, will add either [func()] or [] to b.
[Edited]
Finally, for the worst case scenario: If you can't call func() more than once, and you can't use b += <list>, and you need to accept 0, "", [], etc, and only exclude None, and you need it all on one line, here's the ugliest line of code yet:
(lambda l, a: l.append(a) if a is not None else None)(b, func())
This is essentially #ekhumoro's solution, compressed into one line. It defines an anonymous function, calls it, discards the value, and then discards the function, all for the sake of the side effect.
Now, this is a single line, but it's certainly not easier to read or understand than the original code. If I were you, I'd stick with the original, or go with #ekhumoro's idea of just defining a helper function and using that.
python 3.8 walrus operator
if a := func(): b.append(a)
You asked the wrong question here. The clue is in your reply to one of the comments where you say "I have 10+ tags, if I can get 3 line to 1 line, I will save 20+ lines".
So your problem actually is not that you have 3 lines of code but that you are needlessly repeating 3 lines of code over and over. You could use a function to extract the repeated lines, but it sounds like in this case you may actually want a loop:
THE_TAGS = ('tag1', 'tag2', 'and so on')
for tag in THE_TAGS:
a = r.find(tag) # a may get None if did not find it
if a != None:
b.append(a)
Or if you need to append to different lists:
def extract_tag(r, tag_name, to):
a = r.find(tag_name) # a may get None if did not find it
if a != None:
to.append(a)
extract_tag(r, 'tag1', b)
extract_tag(r, 'tag2', c)
Short answer: Not really.
Longer answer: If you really wanted to avoid this (perhaps because you want to implement this behavior --- appending only non-None values) from several different blocks of code) then you could create a class as a proxy around the underlying b object and hide the details in its append method.
class NonNoneAppender:
def __init__(self, obj):
if not hasattr(obj, 'append') or not callable(obj.append):
raise ValueError, "Object must have append method"
self.__obj = obj
def append(self, item):
if item is not None:
return self.__obj.append(item)
def __getattr__(self, attr):
return getattr( self.__obj, attr)
... and then you could do something like:
b = NonNoneAppender(b)
However, I'm not sure this would make any sense at all for your code.
Attacking your real problem, and doing it in two lines for clarity:
temp = [r.find(tag) for tag in list_of_tags]
b.extend(x for x in temp if x is not None)
Note: Element.extend is new in Python 2.7/3.2
Presumably you're not trying to remove just a single if statement from your code...
So the obvious answer is to use a function:
import xml.etree.ElementTree as etree
def append(parent, child):
if child is not None:
parent.append(child)
r = etree.parse(f).getroot()
b = etree.Element('register',{})
append(b, r.find('tag_name'))
You can just add everything and remove Nones at the end with b = [a for a in b if b is not None]. Or, in your particular use case, you can do b.extend(r.findall('tag_name')[:1]). This may be a bit slower, however, as it will go through the whole tree, rather than stopping at the first instance.
b+=list(set([r.find('tag_name')])-set([None]))
But it's very ugly. A little cleaner, but also a line longer:
b.append(r.find('tag_name'))
b.remove(None)
Still not very neat though. If I were you I'd just keep that if statement.
We have
matches = re.findall(r'somewhat', 'somewhere')
Can we simplify this
if len(matches) > index:
return matches[index]
else:
return 'default'
or
return matches[index] if len(mathes) > index else 'default'
to something similar to JS's
return matches[index] || 'default'
that we can simply use
return 'somewhere'.match(/somewhat/)[index] || 'default'
Something like this might help:
>>> reg = re.compile('-\d+-')
>>> reg.findall('a-23-b-12-c') or ['default']
['-23-', '-12-']
>>> reg.findall('a-b-c') or ['default']
['default']
Edit
Ugly one-liner
(reg.findall('a-b-c')[index:] or ['default'])[0]
I'd be tempted to use a try except block. You'd be required to consider when index was negative though. Is this an error or an acceptable input?
But the following would work:
try:
return re.findall(r'somewhat', 'somewhere')[index]
except IndexError:
return 'default'
This is meant to be the preferred way if you're concerned about efficiency as it avoids checking the bounds of the array twice (once when you manually do it and and second when python does its internal check as well).
edit: I've never particularly liked this way as it hides any IndexErrors thrown by sub calls and would return the default (which I doubt would be the desired behaviour and a possible source of bugs).
Not really, because running mathes[x] with an invalid index will throw an IndexError, which does not return False.