Categories
Python Answers

How to use list comprehension with if statement with Python?

Sometimes, we want to use list comprehension with if statement with Python.

In this article, we’ll look at how to use list comprehension with if statement with Python.

How to use list comprehension with if statement with Python?

To use list comprehension with if statement with Python, we put the if expression at the end of the list comprehension expression.

For instance, we write

[y for y in a if y not in b]

to get the values of y in list a that’s not in list b.

Conclusion

To use list comprehension with if statement with Python, we put the if expression at the end of the list comprehension expression.

Categories
Python Answers

How to ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module?

Sometimes, we want to ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module.

In this article, we’ll look at how to ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module.

How to ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module?

To ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module, we can strip the namespace from the DOM object with the rpartition method.

For instance, we write

from io import StringIO 
import xml.etree.ElementTree as ET

# ...

it = ET.iterparse(StringIO(xml))
for _, el in it:
    prefix, has_namespace, postfix = el.tag.partition('}')
    if has_namespace:
        el.tag = postfix
root = it.root

to call ET.iterparse to parse rhe xml file.

Then we loop through the nodes with a for loop.

In it, we call el.tag.rpartition with '}' to get the tag value from postfix.

Then we check if has_namespace is True.

If it is, then we assign el.tag to postfix.

Conclusion

To ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module, we can strip the namespace from the DOM object with the rpartition method.

Categories
Python Answers

How to share a result queue among several processes with Python?

Sometimes, we want to share a result queue among several processes with Python.

In this article, we’ll look at how to share a result queue among several processes with Python.

How to share a result queue among several processes with Python?

To share a result queue among several processes with Python, we use the Queue class.

For instance, we write

import multiprocessing
def worker(name, que):
    que.put("%d is done" % name)

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=3)
    m = multiprocessing.Manager()
    q = m.Queue()
    workers = pool.apply_async(worker, (33, q))

to create a queue from the Manager object with m.Queue.

Then we call pool.apply_async with the worker function and (33, q) to call worker with 33 and queue q as the arguments.

In worker, we call que.put to return a result after worker is done running.

Conclusion

To share a result queue among several processes with Python, we use the Queue class.

Categories
Python Answers

How to find common substring between two strings with Python?

Sometimes, we want to find common substring between two strings with Python.

In this article, we’ll look at how to find common substring between two strings with Python.

How to find common substring between two strings with Python?

To find common substring between two strings with Python, we can use the os.path.commonprefix method.

For instance, we write

import os
common = os.path.commonprefix(['apple pie available', 'apple pies'])

to call os.path.commonpredix with a list of strings to find the common substring between the 2 strings.

Then common should be 'apple pie'.

Conclusion

To find common substring between two strings with Python, we can use the os.path.commonprefix method.

Categories
Python Answers

How to create a copy of an object in Python?

Sometimes, we want to create a copy of an object in Python.

In this article, we’ll look at how to create a copy of an object in Python.

How to create a copy of an object in Python?

To create a copy of an object in Python, we can use the copy.copy method to create a shallow copy of an object.

We can use copy.deepcopy to create a deep copy of an object.

For instance, we write

import copy

class C():
    def __init__(self):
        self.x = [1]
        self.y = [2]

c = C()
d = copy.copy(c)

to create a shallow copy of object c and assign it to d.

Likewise, we write

d = copy.deepcopy(c)

to create a deep copy of object c and assign it to d.

Conclusion

To create a copy of an object in Python, we can use the copy.copy method to create a shallow copy of an object.

We can use copy.deepcopy to create a deep copy of an object.