Because of the C # Development Foundation, it should be easy to learn python, mainly some grammar, plus now the Internet is so developed.
Feel still want to have a train of thought, otherwise learn what can only be what. Words do not say much, simply send the results of these days of learning it;
The first time to write a blog, we do not laughed at Ah simple grammar is not much to say, random search can be obtained. ()
Unit tests come: To do two Tests:
# used to judge prime numbers
Import Math
def isprime (n):
Print ("Verify number" +STR (n) + "prime start")
Print ("Open square:" +str (Round (MATH.SQRT (n)))
if (n<=1):
return False;
For I in range (2,round (MATH.SQRT (n))):
#只需要判断到数的开平方数字即可
if (n% i==0): print (n/i)
return False;
return True;
# used to judge a 12-digit number, the last one is the first 11 divided by 7 comodule
def isbarcode (n): Print ("Verify that the number" +n+ "matches the rule start")
if (Len (n)!=12):
return False;
Ncount=int (N[0:11]) #利用了正则表达式取前11位
Endnumber=int (N[-1]) #利用了正则表达式取最后1位
Print (ncount)
Print (Endnumber)
if (endnumber==ncount% 7):
return True;
Else
Print (ncount% 7)
return False;
#这个文件保存为count. Py. (the name is random)
#下面开始验证 #保存为test. py
From count import Isbarcode
From count import IsPrime
Import UnitTest
Class Test (UnitTest. TestCase):
Print ("--test--")
def setUp (self):
Print ("--start--")
#先来两个正确的号码
def test_case (self):
Self.asserttrue (Isbarcode ("126112611262"), "It is not Barcode")
Self.asserttrue (IsPrime (157), "It is Prime")
def tearDown (self):
Print ("--end--")
Print ("Test to see where it appears")
If __name__== "__main__":
Unittest.main ()
In the code we see a code like this:
If __name__== "__main__":
Unittest.main () Very curious feeling, online search what this means,
See http://www.cnblogs.com/xuxm2007/archive/2010/08/04/1792463.html, thank this man read several times, finally read understand, really strong! The plain is to write some code, for yourself, similar to private is not? It also serves as an entrance to the module (personal understanding).
Let's see the results of the operation:
>>> ================================ RESTART ================================
>>>--unnitest:main Module
--test--
Test it and see where it's shown.
--start--
Verify that the number 126112611262 conforms to the rule start
12611261126
2 Verifying the number 157 whether prime numbers start
Open Square: 13
--end--.
----------------------------------------------------------------------
Ran 1 Test in 0.045s
Ok
>>> #哈, the verification has passed! But I don't know if we found one more string "
--unnitest:main module ", this is my own added, open-source things are good, casual play;
A line of code is added to the main.py below C:\Python33\Lib\unittest:
From. Import loader, runner
From. Signals Import Installhandler
__unittest = True
Print ("--unnitest:main module") #这是新增加的
# #好了, let's make a faulty unit test, 157 change to 156
The result is this:
>>> ================================ RESTART ================================
>>>--unnitest:main Module
--test--
Test it and see where it's shown.
--start--
Verify that the number 126112611262 conforms to the rule start
12611261126
2
Verify that the number 156 prime is starting
Open Square: 12
78.0
--end--
F
======================================================================
Fail:test_case (__main__. Test)
----------------------------------------------------------------------
Traceback (most recent): File ' C:\Python33\test.py ', line one, in Test_case self.asserttrue (IsPrime (156), "It's n OT prime ") Assertionerror:false are not true:it are not prime numbers
----------------------------------------------------------------------
Ran 1 Test in 0.052s FAILED (Failures=1)
>>> #出错了!!!!!!!! 156 is not a prime number, one more time: This time change 126112611262 to 126112611261,156 to remain unchanged
>>> ================================ RESTART ================================
>>>
--unnitest:main Module
--test--
Test it and see where it's shown.
--start--
Verify that the number 126112611261 conforms to the rule start
12611261126
1
2
--end--
F
======================================================================
Fail:test_case (__main__. Test)
----------------------------------------------------------------------
Traceback (most recent): File "C:\Python33\test.py", line ten, in Test_case self.asserttrue (Isbarcode ("126112611 261 ")," It is not Barcode ") Assertionerror:false is not true:it are not Barcode
# #结束, ah ah, the first hint after the second one does not execute?! ( What's going on?) , can you do 2 units of test? Solution )
----------------------------------------------------------------------
Ran 1 Test in 0.033s
FAILED (Failures=1)
>>> #我用的IDLE (Python GUI), pop-up hints are slow, lots of code is still hand-knocked, but I feel it's the charm.
#感谢虫师, it is the reference to your book to imitate this code (about selenium), I learn Python is also to do automated testing (hehe)
#很多东西还得进一步摸索, some principles are not very understanding, but also to refuel!
#写在C罗的葡萄牙进4强的当天, it's Belgium vs Wales right now, and hopefully Bale will have a fight with Cristiano, haha.
Learn the first lesson of Python (Simple unit test)