{"action":"closed","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/7694","repository_url":"https://api.github.com/repos/numpy/numpy","labels_url":"https://api.github.com/repos/numpy/numpy/issues/7694/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/7694/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/7694/events","html_url":"https://github.com/numpy/numpy/issues/7694","id":157540177,"number":7694,"title":"Support `with` statement for `seterr`","user":{"login":"tillahoffmann","id":966348,"avatar_url":"https://avatars.githubusercontent.com/u/966348?v=3","gravatar_id":"","url":"https://api.github.com/users/tillahoffmann","html_url":"https://github.com/tillahoffmann","followers_url":"https://api.github.com/users/tillahoffmann/followers","following_url":"https://api.github.com/users/tillahoffmann/following{/other_user}","gists_url":"https://api.github.com/users/tillahoffmann/gists{/gist_id}","starred_url":"https://api.github.com/users/tillahoffmann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tillahoffmann/subscriptions","organizations_url":"https://api.github.com/users/tillahoffmann/orgs","repos_url":"https://api.github.com/users/tillahoffmann/repos","events_url":"https://api.github.com/users/tillahoffmann/events{/privacy}","received_events_url":"https://api.github.com/users/tillahoffmann/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-05-30T18:02:43Z","updated_at":"2016-05-30T18:19:02Z","closed_at":"2016-05-30T18:19:02Z","body":"I often find myself saving the current error reporting configuration, modifying the configuration for a particular operation, and subsequently restoring the original configuration. For example,\r\n\r\n```python\r\nimport numpy as np\r\n\r\n\r\ndef compute_entropy(probabilities):\r\n    err = np.geterr()\r\n    np.seterr(divide='ignore')\r\n    surprisal = - np.log(probabilities)\r\n    np.seterr(**err)\r\n    return np.sum(probabilities * surprisal)\r\n```\r\n\r\nI have handled such situations using an `ErrorContext` class that stores the current configuration in its `__enter__` method and restores it in `__exit__` such that the above code becomes.\r\n\r\n```python\r\nimport numpy as np\r\n\r\n\r\ndef compute_entropy(probabilities):\r\n    with ErrorContext(divide='ignore'):\r\n        surprisal = - np.log(probabilities)\r\n    return np.sum(probabilities * surprisal)\r\n```\r\n\r\nWould such functionality be desired as a contribution or is it too specific to the particular use case? If desired, I will submit a PR."}}
{"actor":{"display_login":"tillahoffmann"}}