Stupid method of learning Python Lesson 46-49

Source: Internet
Author: User

Exercises 46

Code

setup.py

Try:from setuptools Import setupexcept importerror:from distutils.core Import setupconfig = {' description ': ' M Y Project ', ' Author ': ' Jer ', ' url ': ' URL to get it in. ', ' download_url ': ' Where to download it. ', ' Author_emai    L ': ' [email protected] ', ' Version ': ' 0.1 ', ' install_requires ': [' nose '], ' packages ': [' Jer '], ' scripts ': [], ' Name ': ' ProjectName '}setup (**config)

test.py

From nose.tools import *import jerdef setup (): print "setup!"    Def teardown (): print "TEAR down!" Def test_basic (): print "I ran!"

Notes

① This section is mainly about the basics of project building

Exercise 47

game.py

Class (object): Def __init__ (self, Name, description): Self.name = name self.description = Descri            Ption self.paths = {} def go (self, direction): Return Self.paths.get (direction, None) def add_paths (self, Paths): Self.paths.update (Paths)

blah_tests.py

From nose.tools import *from ex47.game import roomdef test_room ():     gold = room ("Goldroom",                  "" "this room has gold in it you  Can grab. there ' s a                 door to the north. "" ")     assert_equal (gold.name,  "Goldroom")     assert_equal ( gold.paths, {})     def test_room_paths ():     center =  room ("Center",  "Test room in the center")     north =  room ("North",  "Test room in the north.")     south = room ("South",  "Test room in the south.")          center.add_paths ({' North ': north,  ' South ':  south})      assert_equal (Center.go (' North '),  north)     assert_equal (The Center.go (' South ') ),  south)     def test_map ():     start = room (" Start ", " You can go west and down a hole. ")     west = room ("Trees",  "there are trees here, you  can go east. ")     down = room ("Dungeon",  "It" s dark down here, you  can go up. ")         start.add_paths ({' West ': west,  ' Down ':  down})     west.add_paths ({' East ':  start})     down.add_paths ({' Up '):  start})         assert_equal (Start.go (' West '),  west)  &Nbsp;  assert_equal (Start.go (' West '). Go (' East '),  start)     assert_equal ( Start.go (' Down '). "Go (' Up '),  start)

Test output:

①nosetests testing, automated testing

Exercise 48

My "scanner"/lexicon.py

Class lexicon (object):         def scan (Self, stuff):         self.stuff = stuff         words = stuff.split ()         sentence  = []        for i in words:             try:                 sentence.append (' Number ',  int (i))              except ValueError:                 if i in [' North ',  ' South ',  ' East ',  ' West ',  ' down ',  ' up ',  ' left ',  ' right ',  ' back ']:         &Nbsp;           sentence.append ((' direction ',  i))                 elif i  in [' Go ',  ' Stop ',  ' kill ',  ' eat ']:                     sentence.append ((' verb ',  i))                 elif i  in [' the ',  ' in ',  ' of ',  ' from ',  ' at ',  ' in ']:                     sentence.append (' Stop ',  i))                  elif i in [' door ',  ' Bear ',  ' princess ',  ' cabinet ']:          &nbSp;          sentence.append ((' noun ',  i))                  else:                      Sentence.append (' Error ',  i)         return sentence        lexicon = lexicon ()

Test Code lexicon_tests.py

From nose.tools import *from ex48.lexicon import lexicondef test_directions ():     assert_equal (Lexicon.scan ("North"),  [(' direction ',  ' North ')])      result = lexicon.scan ("North south east")     assert_equal ( result, [(' direction ',  ' North '),                            (' direction ',  ' South '),                            (' direction ',  ' East ')]                             def test_verbs ():     assert_equal (Lexicon.scan ("Go"),  [( ' Verb ',  ' go ')])     result = lexicon.scan ("Go kill eat")      Assert_equal (result, [(' verb ',  ' go '),                            (' verb ',  ' Kill '),                            (' verb ',  ' eat ')]                                                        def test_stops ():     assert_equal (Lexicon.scan ("the"),  [(' Stop ',  ' the ')]     result =  lexicon.scan ("The in&nbSp;of ")     assert_equal (result, [(' Stop ',  ' the '),                             (' Stop ',  ' in '),                            (' Stop ',  ' of ')])                            def test_nouns ():     assert_equal ( Lexicon.scan ("Bear"),  [(' noun ',  ' bear ')]     result = lexicon.scan (" Bear princess ")     assert_equal (result, [(' noun ',  ' bear '),                           &nBSP; (' noun ',  ' princess ')])                            def test_numbers ():     assert_equal (Lexicon.scan ("1234"),  [(' number ',  1234)])      Result = lexicon.scan ("3 91234")     assert_equal (result, [(' number '),  3),                            (' number ',  91234)])                              def test_errors ():     assert_equal (Lexicon.scan (" Asdfasdfasdf "),  [(' Error ',  ' ASDFASDFASDF ')])     result = lexicon.scan ( "Bear ias princess") &nbsP;   assert_equal (result, [(' noun ',  ' bear '),                             (' Error ',  ' IAS '),                            (' noun ',  ' princess ')] )

Output

Notes:

① tried for a long time, always hint

Unbound method must is called with instance as first argument

This error, finally added an instantiation statement in the lexicon.py, finally resolved

② Add points Exercise 3, identify the case with lower () all converted to lowercase and then judged

③ Add to exercise 4, another way is to manipulate the string in the try, except in the operation of the numbers.

Try:i.lower () passexcept Syntaxerror:pass

Exercise 49









Stupid method of learning Python Lesson 46-49

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.