-1

Updated

Thank you all for the suggestions, but I'm having a new issue here.

Since I'm comparing two datetime.datetime objects and I didn't realize that it doesn't have attribute items or keys to iterate from, some valid answers provided below no longer work. I'm refactoring my dummy data here to better reflect my use cases,

# Two datetime that I want to assert equal as long as they are equal to the 'second'
now = datetime.datetime(2015, 7, 22, 11, 36, 49, 811000)
then = datetime.datetime(2015, 7, 22, 11, 36, 49, 811099)

assert now == then # this for sure will return false

As you can see from the example, every attribute but 'microsecond' is equal. Anyway to loop through the attributes and compare?

Thanks guys!

benjaminz
  • 3,118
  • 3
  • 35
  • 47
  • Do you need all except for any one? Or all except for a specific one? – Morgan Thrapp Jul 22 '15 at 16:03
  • All except for a specific one, this case `attr3` – benjaminz Jul 22 '15 at 16:04
  • 1
    Have you looked at this: http://stackoverflow.com/questions/7999935/python-datetime-to-string-without-microsecond-component -- try `assert now.strftime("%Y-%m-%d %H:%M:%S") == then.strftime("%Y-%m-%d %H:%M:%S")` – xnx Jul 22 '15 at 16:28
  • @xnx It worked! I just need to assert the `then.strftime("%Y-%m-%d %H:%M:%S") == now.strftime("%Y-%m-%d %H:%M:%S")`! And this could also be useful if someone wants to assert equal to day, hour, minute, etc. Please edit your answer below and I will take it as the correct answer. Thank you! – benjaminz Jul 22 '15 at 16:32
  • Have edited my answer with another suggestion as well. – xnx Jul 22 '15 at 16:53
  • @benjaminz This is why you need to have a [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Jul 22 '15 at 17:10

3 Answers3

2

EDIT: for your modified question, you 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 that its value isn't the same in each dictionary):

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

Perhaps the following will help:

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

(but you'd need to be sure that the keys are the same in both dictionaries).

xnx
  • 24,509
  • 11
  • 70
  • 109
  • 1
    I'm assuming this is to get the sum of all assert failures? What if there is a case where `foo[attr2] != bar[attr2]` but `foo[attr3] == bar[attr3]`? sum here is the same but it is not what I wanted. – benjaminz Jul 22 '15 at 16:02
2

Just loop through the attributes you want to compare, and assert for each.

compared_keys = foo.pop('attr3').keys()
for k in compared_keys:
    assert(foo[k] == bar[k])
maniexx
  • 625
  • 1
  • 12
  • 28
0

This should work for you.

foo = {
  'attr1': 1,
  'attr2': 2,
  'attr3': 3
}

bar = {
  'attr1': 1,
  'attr2': 2,
  'attr3': 3
}

assert (sum([foo[k] == v for k, v in bar.items() if k != 'attr3'])) == 2
print('Works')

foo = {
  'attr1': 1,
  'attr2': 2,
  'attr3': 3
}

bar = {
  'attr1': 1,
  'attr2': 2,
  'attr3': 300
}

assert (sum([foo[k] == v for k, v in bar.items() if k != 'attr3'])) == 2
print('Works')
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67