Re: Fumbling with emacs + elpy + flake8

2018-09-14 Thread Toni Sissala
I'm on Ubuntu 16.04. I found out that flake8 did not play well with 
emacs if installed with --user option, nor when installed in a virtual 
environment. Didn't research any further, since I got it working with 
plain pip3 install flake8


Do you see any error messages?


Toni


On 13.9.2018 21:59, Martin Schöön wrote:

I am trying to set up emacs for Python coding on my secondary computer.
I follow these instructions but fail to make flake8 play with elpy:

https://realpython.com/emacs-the-best-python-editor/#elpy-python-development

I have done this some time in the past on my main computer and there it
works just fine. I have compared the set-up of this on the two computers
and fail to figure out why it works on one and not the other.

There are a couple of things that are not the same on the computers:

1) The secondary computer has a later version of emacs installed.

2) I used pip3 install --user flake8 on the secondary computer but on
the primary computer I think I left out the --user flag (not knowing
about it at the time) but I have added the path to .local/bin and
M-x elpy-config finds flake8 on both computers. Yet it does not
work on my secondary computer...

Any leads are greatly appreciated.

/Martin

PS Debian on both computers.



--
https://mail.python.org/mailman/listinfo/python-list


Re: mocking for get method in requests

2019-03-26 Thread Toni Sissala

On 18.1.2019 19:16, Shakti Kumar wrote:

Hello people,
I noticed something weird (weird as per my current knowledge, though I know
its subjective) today.


Hi Kumar,



mock_req.get('').return_value = 'Hello'


Here you are calling mock_req -MagicMocks get-method with parameter '' 
and assigning 'Hello' to its return value's return_value attribute. By 
default MagicMock instance methods always return a new MagicMock 
instance, which will create attributes on demand. You are not actually 
setting the return value of the patched requests, but rather assigning a 
return_value attribute to a local instance of a MagicMock.




mock_req.get.return_value = 'Hello'
Here you are assigning 'hello' to mock_req instance's get -methods 
return_value. This is probably what you are after. Notice that you are 
not actually calling any of its methods, only assigning.


Mocks are powerful but take some time to get use to.


>>> from unittest import mock
>>> a = mock.MagicMock()
>>> a.get.return_value = 'asd'
>>> a.get()
'asd'
>>> a.get.assert_called_once_with()
>>> b = mock.MagicMock()
>>> new_mock = b.get()
>>> new_mock.return_value = 'qwe'
>>> new_mock()
'qwe'
>>> b.get.assert_called_once_with()
>>> new_mock.assert_called_once_with()
>>> b.get()

>>> b.get.assert_called_once_with()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.5/unittest/mock.py", line 802, in 
assert_called_once_with

    raise AssertionError(msg)
AssertionError: Expected 'get' to be called once. Called 2 times.



Hope this helps,

Toni




sample.py file

--

import requests
def random_testing():
 out = requests.get('www.cisco.com')
 a = out.json()
 return a


testing.py file

--

@patch(*’*sample.requests')
def test_random_testing(self, mock_req):
 mock_req.get('').return_value = 'Hello'
 out = api.random_testing()


Patching the sample.requests in this way does not lead the output of
requests.get() function in sample.py file to be ‘Hello’ as indicated
in
mock_req.get('').return_value = 'Hello'
Instead, it creates a new field called return_value in ‘out', and
hence out.return_value is ‘Hello’ instead of just ‘out’.

But if I patch it as,

@patch(*’*sample.requests')
def test_random_testing(self, mock_req):
 mock_req.get.return_value = 'Hello'
 out = api.random_testing()

It does give the value of ‘out’ as ‘Hello’ in sample.py file.
I know I am missing something, which is where I need some help :)

Thanks.



--
https://mail.python.org/mailman/listinfo/python-list