Skip to content

felix11h/python-pitfalls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

next() with map()

As an example, consider a function

def get_matching_data(dataset: tuple[dict,...]):
    return next(data for data in dataset if data["key"]=="match")

that gets a specific object in a tuple of a dictionaries by its key. Since we do not give a default value, we expect an item with the given to always exist, otherwise to raise an error. On its own the construction operates as expected. The code

a = ({"key": "match"}, {"key": "no match"})
b = ({"key": "no match"},)

for dataset in (a,b):
    print(get_matching_data(dataset))

returns

{'key': 'match'}
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in get_data_by_key
StopIteration

However, if next() is combined with map(), we get unexpected results. The following

a = ({"key": "match"}, {"key": "no match"})
b = ({"key": "no match"},)
c = ({"key": "match"}, {"key": "no match"})

print(tuple(map(get_matching_data, (a, b, c))))

results in

({'key': 'match'},)

We do not(!) get an error and the data returned is incomplete.

avoid dateutil.parser and parse dates explicitly

from datetime import datetime

from dateutil.parser import parse

datestr = "2020:06:28 10:06:34+02:00"

print(parse(datestr))
print(datetime.strptime(datestr, "%Y:%m:%d %H:%M:%S%z"))

returns

2021-06-25 10:06:34+02:00
2020-06-28 10:06:34+02:00

About

Collection of code snippets demonstrating common Python pitfalls.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages