Python assert equal except one attribute -


updated

thank suggestions, i'm having new issue here.

since i'm comparing 2 datetime.datetime objects , didn't realize doesn't have attribute items or keys iterate from, valid answers provided below no longer work. i'm refactoring dummy data here better reflect use cases,

# 2 datetime want assert equal long equal 'second' = datetime.datetime(2015, 7, 22, 11, 36, 49, 811000) = datetime.datetime(2015, 7, 22, 11, 36, 49, 811099)  assert == # sure return false 

as can see example, every attribute 'microsecond' equal. anyway loop through attributes , compare?

thanks guys!

edit: modified question, can use:

assert now.replace(microsecond=0) == then.replace(microsecond=0) 

or

assert now.strftime("%y-%m-%d %h:%m:%s") == then.strftime("%y-%m-%d %h:%m:%s") 

to explicitly exclude attr3 in particular (and ensure value isn't same in each dictionary):

assert all([foo[k] == bar[k] k in bar.keys() if k != 'attr3']) , foo['attr3'] != bar['attr3'] 

perhaps following help:

assert sum([foo[k]!=v (k,v) in bar.items()]) == 1 

(but you'd need sure keys same in both dictionaries).


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -