TypeError: Need a valid target to patch. You supplied: ‘SomeClass’
Problem:
I needed to mock out a method in a class that I was clearly importing. But when I tried to patch it gave me the following error where “SomeClass” is the name of the class I was trying to mock out.
TypeError: Need a valid target to patch. You supplied: ‘SomeClass’
Solution:
A very clear explanation of what I was doing wrong was on SO
http://stackoverflow.com/questions/16060724/patch-why-wont-the-relative-patch-target-name-work
Unittest using assertRaises
Problem:
I needed to test that a function returns an exception.
Solution:
Sample code
# sample.py def sum(a, b): total = None try: total = a + b except TypeError: raise TypeError("Integers only") return total
and
# test_assert.py import unittest from sample import sum class TestSum(unittest.TestCase): def test_total(self): self.assertEquals(sum("3", 2), 4) if __name__ == "__main__": unittest.main()
Running the test to check that an assert is raised
$ python test_assert.py E ====================================================================== ERROR: test_total (__main__.TestSum) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_assert.py", line 7, in test_total self.assertEquals(sum("3", 2), 4) File "/Users/rodnee/sample.py", line 6, in sum raise TypeError("Integers only") TypeError: Integers only ---------------------------------------------------------------------- Ran 1 test in 0.000s
Changing the unit test to catch the raised exception
# test_assert.py import unittest from sample import sum class TestSum(unittest.TestCase): def test_total(self): self.assertEquals(sum(2, 2), 4) # First argument is the Exception to be caught ie TypeError # Second argument is the method under test ie sum # Third to x arguments are the values to pass onto sum() self.assertRaises(TypeError, sum, "3", 2) if __name__ == "__main__": unittest.main()
Running the updated unit test produces
$ python test_assert.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
Source:
http://www.lengrand.fr/2011/12/pythonunittest-assertraises-raises-error/
https://pymotw.com/2/exceptions/
http://eu.wiley.com/WileyCDA/WileyTitle/productCd-1118901223.html
Code Combat – Learn to code by playing a game
NATS
NATS is an open-source, high-performance, lightweight cloud native messaging system.
NATS was created by Derek Collison, Founder/CEO of Apcera who has spent 20+ years designing, building, and using publish-subscribe messaging systems. Unlike traditional enterprise messaging systems, NATS has an always-on dial tone that does whatever it takes to remain available. This forms a great base for building modern, reliable, and scalable cloud and distributed systems.
Show current git branch on your bash prompt
I started learning to use git and the examples I saw showed the branches on the command prompt which I think is very cool and helpful. And of course, I wanted that too.
In 3 simple steps.
1. Download
curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL
2. Add the following to .bashrc
# Add Git configurations source git-completion.bash export PS1='\[\033[1;34m\]\!\[\033[0m\] \[\033[1;35m\]\u\[\033[0m\]:\[\033[1;35m\]\W\[\033[0m\] \[\033[1;92m\]$(__git_ps1 "(%s)")\[\033[0m\]\$ ' export GIT_PS1_SHOWDIRTYSTATE=1
3. Restart your terminal so that the changes take place or source your bashrc YMMV.
This is just the tip of the iceberg. You can get much much more at the resources listed below
Source:
http://blog.taylormcgann.com/2012/06/19/show-current-git-branch-your-bash-prompt/#comment-12515
http://thepugautomatic.com/2008/12/git-dirty-prompt/
Interesting – Using Selenium
Interesting articles below on
Running Selenium with Firefox (headless)
https://azevedorafaela.wordpress.com/2014/10/30/running-selenium-headless-firefox-in-ubuntu/
What is a headless browser
https://azevedorafaela.wordpress.com/2014/06/09/what-is-headless-browser/
Eight Terminal Utilities Every OS X Command Line User Should Know
I need to learn at least 3 of these.
Javascript – True or False?
Interesting article on Javascript.
I haven’t had the chance to work with Javascript so I don’t know if this is true or not. Is he just ranting or does he have a valid point. Here is some of what he says
…
It’s much more about the problem you are trying to solve, the community you interact with and the quality of the libraries you are going to rely on to build something useful. These things can and do vary wildly. Python is a great general-purpose language with a friendly community, Ruby is probably a bit more web-focused but mostly the same. PHP is great for making web applications if you’re 12. C# is great for windows desktop applications. J is used by about 20 people in the whole world but is great for numeric applications. Java is not really good for writing software.The JavaScript community though largely of well-meaning and enthusiastic programmers (not usually “software engineers”) who are fresh out of their Coder Bootcamp and ready to disrupt the stodgy old world of UNIX weenies who waste their time doing silly things like memory management and aren’t up on the latest frontend frameworks. With the notable exception of transpiling C to JavaScript, running JavaScript on an operating system instead of a web browser requires a whole lot of library support for things like networking, threading, files and more. So much to rewrite and make asynchronous!
…
Read the full article at http://spiegelmock.com/post/127971602352/programming-with-the-lowest-common-denominator