Next Generation Network Engineers
  • Network Transformation and Essential Skills for Next Generation Network Engineers
    www.ciscolive.com/online/connect/sessionDetail.ww?SESSION_ID=92606

    Python Programming for Network Engineers
    www.youtube.com/playlist?list=PLhfrWIlLOoKPn7T9FtvbOWX8GxgsFFNwn

    packetpushers.net/next-generation-network-engineers


    Method of Procedure (MOP) Is Largely Manual
    • Pre-maintenance Checks
      + Manually verify system state and readiness for MOP deployment
      + No way to track/predict an impact on the network: Will I violate my SLA?

    • Maintenance
      + Node Cost out
      + Apply Software updates
      + Apply Configuration changes
      + Verity system state to ensure updates correctly deployed
      + Node Cost in

    • Post Maintenance
      + Topology verification
      + Network state consistency check
    Cause:
    • Time Consuming
    • Expensive
    • Error Prone

    Network Change Automation

    • Time Effective
    • Low Cost
    • Error Free

    Job Roles Are Shifting for Network Engineers:

    Do Less of...
    • Device configurations
    • Continuous operations
    • Problem resolution
    • Hardware deployments
    • Monitoring and reporting
    • CLI entries and scripting
    • Hands-on repairs/deployments

    Do More of...

    • Service innovation
    • Architectural (end-to-end) design
    • Network analytics & optimization
    • Programming - APIs versus CLI
    • Software administration
    • Comprehensive policy management
    • Systems integration/validation

    Is the CCIE Dead? Programming not Configuring Future IT
    www.ciscolive.com/online/connect/sessionDetail.ww?SESSION_ID=94452

    Why Python For Network Engineering?

    Is Python considered Easy? HUH?

    MANY OF THE CONCEPTS ARE THE SAME BUT THE SYNTAX IS EASIER IN PYTHON

    C++:
    • #include stdout

      int main()
      {
        std::count << "Hello, world!\n";
      }

    PYTHON:

    • print("Hello, world!")

    Notepad ++
    notepad-plus-plus.org/download

    Python
    www.python.org/downloads

    GNS3
    www.gns3.com

    Virtual Box
    www.virtualbox.org/wiki/Downloads

    C:\Python27>python
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> "hello world"
    'hello world'
    >>> exit()

    1. intro.py

    • print "hello world 2"

    C:\Python27>python intro.py
    hello world 2

    Integer & Float:

    Assigning Values to Variables:

    • counter = 100  # An integer assignment (10, 100, -786)
    • miles = 1000.0  # A floating point (0.0, 15.20, -21.9)
    • name = "John"  # A string

    Single value to several variables:

    • a = b = c = 1

    Multiple objects to multiple variables:

    • a, b, c = 1, 2, "john"

    Delete reference to a number object:

    • del var
    • del var_a, var_b

    C:\Python27>python
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> counter = 100
    >>> counter
    100
    >>> anything=100
    >>> anything
    100
    >>> miles = 1000.5
    >>> miles
    1000.5
    >>> name = "John"
    >>> name
    'John'
    >>> name = 'John155'
    >>> name
    'John155'

    >>> a=b=c =1
    >>> a
    1
    >>> b
    1
    >>> c
    1
    >>> del a
    >>> a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'a' is not defined
    >>> del b
    >>> b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'b' is not defined
    >>> del c
    >>> c
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'c' is not defined
    >>> a,b,c = 1,2.8,'Jhon'
    >>> a
    1
    >>> b
    2.8
    >>> c
    'Jhon'
    >>> exit()

    2. Ex.py

    • counter = 500
      miles = 2000.15
      name = 'Andrew'

      print counter
      print miles
      print name

      print counter, miles, name

      print 'The counter value is: ', counter

    C:\Python27>python Ex.py
    500
    2000.15
    Andrew
    500 2000.15 Andrew
    The counter value is:  500

    String:

    • str = 'Hello World!"

    • print str  # Prints a complete string
    • print str[0]  # Prints first character of the string
    • print str[2:5]  # Prints characters starting from 3rd to 5th
    • print str[2:]  # Prints string starting from 3rd character
    • print str * 2  # Prints string two times
    • print str + "TEST"  # Prints concatenated string

    C:\Python27>python
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'a' is not defined
    >>> a = 'Some text'
    >>> a = 'Chevy 427'
    >>> a
    'Chevy 427'
    >>> a = 'Hello'
    >>> b = 'World'
    >>> a
    'Hello'
    >>> b
    'World'
    >>> c = a + b
    >>> c
    'HelloWorld'
    >>> d = b + a
    >>> d
    'WorldHello'
    >>> 50 + 30
    80
    >>> '50'+'30'
    '5030'
    >>> str = "Hello World"
    >>> str
    'Hello World'
    >>> str[0]
    'H'
    >>> str[10]
    'd'
    >>> str[2:5]
    'llo'
    >>> str[2:8]
    'llo Wo'
    >>> str[:8]
    'Hello Wo'
    >>> str[2:]
    'llo World'
    >>> str[:]
    'Hello World'
    >>> str
    'Hello World'
    >>> new_string = "test "
    >>> new_string
    'test '
    >>> new_string * 4
    'test test test test '
    >>> new_string + new_string
    'test test '
    >>> str= new_string * 4
    >>> str
    'test test test test '
    >>> str= str + str
    >>> str
    'test test test test test test test test '

    3. Ex2.py

    • str = 'Hello World'
      print str

      first = str[:5]*4
      next = str[6:]*3

      print 'The first word is: ', first
      print 'The next word is: ', next

    C:\Python27>python Ex2.py
    Hello World
    The first word is:  HelloHelloHelloHello
    The next word is:  WorldWorldWorld

    Lists:

    • list1 = ['physics', 'chemistry', 1997, 2000];
    • list2 = [1, 2, 3, 4, 5, 6, 7 ];
    • list3 = ["a", "b", "c", "d"];

    • print "list1[0]: ", list1[0]
    • print "list2[1:5]: ", list2[1:5]

    • print "Value available at index 2 : "
    • print list1[2]
    • list1[2] = 2001;
    • print "New value available at index 2 : "
    • print list1[2]

    • del list1[2];
    • print "After deleting value at index 2 : "
    • print list1

    len([1, 2, 3]) = 3 < Length

    [1, 2, 3] + [4, 5, 6] = [1, 2, 3, 4, 5, 6] < Concatenation

    ['Hi!'] * 4 = ['Hi!', 'Hi!', 'Hi!', 'Hi!'] < Repetition

    B-)

  • 6 Comments sorted by
  • C:\Python27>python
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> list1 = ['physics','chemistry',1997,2000]
    >>> list1
    ['physics', 'chemistry', 1997, 2000]
    >>> list1[1]
    'chemistry'
    >>> list1[1]='org chemistry'
    >>> list1
    ['physics', 'org chemistry', 1997, 2000]
    >>> list2 = [1,2,3,4,5]
    >>> list2
    [1, 2, 3, 4, 5]
    >>> list2*3
    [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
    >>> list3 = ['w','e','h']
    >>> list3[2]
    'h'
    >>> newstr=list1[0]
    >>> newstr
    'physics'
    >>> newstr[:4]
    'phys'
    >>> list1[3]=2001
    >>> list1
    ['physics', 'org chemistry', 1997, 2001]

    4. Ex3.py
    • list1 =['phy','chem',1997,'2001']
      print 'list before update: ',list1
      list1[2] = 2000
      list1[3] = 2020
      print 'list after update: ',list1

    C:\Python27>python Ex3.py
    list before update:  ['phy', 'chem', 1997, '2001']
    list after update:  ['phy', 'chem', 2000, 2020]

    Tuple:
    The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

    • tup1 = ('physics', 'chemistry', 1997, 2000);
    • tup2 = (1, 2, 3, 4, 5, 6, 7);
    • tup3 = "a", "b", "c", "d";

    • print "tup1[0]: ", tup1[0]
    • print "tup2[1:5]: ", tup2[1:5]

    The empty tuple:
    • tup1 = ();

    A tuple containing a single value:
    • tup1 = (50,);

    Delete Tuple Elements:
    • del tuple;
    >>> a = 1
    >>> type(a)
    <type 'int'>
    >>> list1=['phys','chem',1997,2001]
    >>> list1
    ['phys', 'chem', 1997, 2001]
    >>> type(list1)
    <type 'list'>
    >>> tup1=('phys','chem',1997,2001)
    >>> tup1
    ('phys', 'chem', 1997, 2001)
    >>> type(tup1)
    <type 'tuple'>
    >>> del list1
    >>> list1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'list1' is not defined
    >>> list1=('phys','chem',1997,2001)
    >>> type(list1)
    <type 'tuple'>
    >>> tup1[3]
    2001
    >>> tup1[3]=2020
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    >>> del tup1[3]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object doesn't support item deletion
    >>> list1=['phys','chem',1997,2001]
    >>> type(list1)
    <type 'list'>
    >>> list1[3]
    2001
    >>> list1[3]=2020
    >>> list1
    ['phys', 'chem', 1997, 2020]
    >>> del tup1
    >>> tup1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'tup1' is not defined

    Dictionary:
    • dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

    • print "dict['Name']: ", dict['Name']

    Updating Dictionary

    • dict['Age'] = 8; # update existing entry
    • dict['School'] = "DPS School"; # Add new entry

    >>> dict = {'Name' : 'Zara','Age' : 7, 'Class' : 'first'}
    >>> type(dict)
    <type 'dict'>
    >>> list1 = {'Name' : 'Zara','Age' : 7, 'Class' : 'first'}
    >>> type(list1)
    <type 'dict'>
    >>> dict
    {'Age': 7, 'Name': 'Zara', 'Class': 'first'}
    >>> dict['Age']
    7
    >>> dict['age']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'age'
    >>> dict['Age']=10
    >>> dict
    {'Age': 10, 'Name': 'Zara', 'Class': 'first'}
    >>> dict['Class'] = 'Fifth'
    >>> dict
    {'Age': 10, 'Name': 'Zara', 'Class': 'Fifth'}
    >>> del dict['Class']
    >>> dict
    {'Age': 10, 'Name': 'Zara'}

    • Suppose list1 is [3, 5, 25, 1, 3], min(list1) is 1.
      min returns the minimum element in the list.

    • Use function random.shuffle(list1) to shuffle the list(say list1).

    • Suppose list1 is [1, 5, 9, sum(list1) is 15.
      Sum returns the sum of all elements in the list.

    • Class is a user defined datatype, not a core datatype.

    • L = [1, 23, 'hello', 1], datatype is List.
      List datatype can store any values within it.

    Loop:
    • for iterating_var in sequence:
    •   statement(s)

    • for letter in 'Python':
    •   print 'Current Letter :', letter

    • fruits = ['banana', 'apple', 'mango']
    • for fruit in fruits:
    •   print 'Current fruit :', fruit

    5. forloops.py

    • fruits = ['banana', 'apple', 'mango', 'orange', 'berry']

      for i in fruits:
          print 'Current the fruit is ', i
          print 'Goodbye'
      print 'type = ', type(i)

      for letter in 'Python':
          print 'Current Letter = ', letter
      print 'Goodbye'

    C:\Python27>python forloops.py
    Current the fruit is  banana
    Goodbye
    Current the fruit is  apple
    Goodbye
    Current the fruit is  mango
    Goodbye
    Current the fruit is  orange
    Goodbye
    Current the fruit is  berry
    Goodbye
    type =  <type 'str'>
    Current Letter =  P
    Current Letter =  y
    Current Letter =  t
    Current Letter =  h
    Current Letter =  o
    Current Letter =  n
    Goodbye

    • while expression:
    •   statement(s)

    • count = 0
    • while (count < 9):
    •   print 'The count is:', count
    •   count = count + 1

    • print "Goodbye!"

    6. Whileloops.py

    • count = 0
      while (count < 9):
          print 'The count is:', count
          count = count + 1
      print "Loop has done!"

    C:\Python27>python Whileloops.py
    The count is: 0
    The count is: 1
    The count is: 2
    The count is: 3
    The count is: 4
    The count is: 5
    The count is: 6
    The count is: 7
    The count is: 8
    Loop has done!

    • for iterating_var in sequence:
    •   for iterating_var in sequence:
    •     statements(s)
    •   statements(s)

    • while expression:
    •   while expression:
    •     statement(s)
    •   statement(s)

    7. nest.py

    • for i in range(1,5):
          for j in range(1,3):
              print "j=",j
          print " *i=",i
      print "loops done"

    C:\Python27>python nest.py
    j= 1
    j= 2
     *i= 1
    j= 1
    j= 2
     *i= 2
    j= 1
    j= 2
     *i= 3
    j= 1
    j= 2
     *i= 4
    loops done

    • x = ['ab', 'cd']
      for i in x:
        i.upper()
      print(x)
      Output: ['ab','cd']
      The function upper() does not modify a string in place, it returns a new string which isn't being stored anywhere.

    • i=1
      while True:
        if i%007 == 0:
          break
        print(i)
        i += 1
      Output: 1 2 3 4 5 6
    B-)
  • Control:

    • if expression:
    •   statement(s)

    • var1 = 100
    • if var1:
    •   print "1 - Got a true expression value"
    •   print var1

    • var2 = 0
    • if var2:
    •   print "2 - Got a true expression value"
    •   print var2
    • print "Good bye!"

    8. if.py
    • var1 = 'area'
      if var1 == 'area':
        print 'Area = length * width'

      var1 = 'volume'
      if var1 == 'volume':
        print 'Volume = length & width * height'

      var1 = 100
      if var1:
          print "1 - true"
          print var1

      var2 = 0
      if var2:
          print '2 - true'
          print var2

      var3 = 'a'
      if var3:
          print "3 - true"
          print var3

      var4 = ''
      if var4:
          print "4 - true"
          print var4

    C:\Python27>python if.py
    Area = length * width
    Volume = length & width * height
    1 - true
    100
    3 - true
    a

    • if expression:
    •   statement(s)
    • else
    •   statement(s)

    9. ifelse.py

    • var1= 100
      if var1:
          print ' 1 - true'
          print var1
      else:
          print ' 1 - false'

      var2 = 0
      if var2:
          print '2 - got true'
          print var2
      else:
          print '2 - got false'
          print var2

      print 'goodbye'

    C:\Python27>python ifelse.py
     1 - true
    100
    2 - got false
    0
    goodbye

    • if expression1:
    •   statement(s)
    • elif expression2:
    •   statement(s)
    • elif expression3:
    •   statement(s)
    • else:
    •   statement(s)

    10. ifelif.py

    • var = 100
      if var == 100:
          print '1 got true'
          print var

      elif var == 150:
          print '2 got true'
          print var

      elif var == 100:
          print '3 got true'
          print var

      else:
          print '4 got false'
          print var

      print 'goodbye'

    C:\Python27>python ifelif.py
    1 got true
    100
    goodbye

    Operators:

    >>> a=10
    >>> b=20
    >>> a+b
    30
    >>> a-b
    -10
    >>> a*b
    200
    >>> b/a
    2
    >>> b%a => Modulus: / but returns remainder
    0
    >>> b=23
    >>> b%a
    3
    >>> a=4
    >>> b=3
    >>> a**b => a^b
    64
    >>> 9//2 => / but the digits after the decimal point are removed
    4
    >>> 8//2
    4
    >>> 8.5//2
    4.0
    >>> 8.5/2
    4.25

    11. operators.py

    • a = 100
      b = 100
      if (a == b):
          print 'True'
          print 'a =', a, '== b =', b
          print ''

      a = 10
      if (a != b):
          print 'True'
          print 'a =', a, '!= b =', b
          print ''

      a = 100
      if (a <> b):
          print 'True'
          print 'a =', a, 'b =', b
      else:
          print 'False - <>'
          print 'a =', a, 'b =', b
          print ''

      b = 10
      if (a > b):
          print 'True'
          print 'a =', a, '> b =', b
          print ''

      if (a < b):
          print 'True'
          print 'a =', a, 'b =', b
      else:
          print 'False - <'
          print 'a =', a, 'b =', b
          print ''

      if (a <= b):
          print 'True'
          print 'a =', a, 'b =', b
      else:
          print 'False - <='
          print 'a =', a, 'b =', b
          print ''

      if (a >= b):
          print 'True'
          print 'a =', a, '>= b =', b
          print ''

      b = 100
      if (a <= b):
          print 'True'
          print 'a =', a, '<= b =', b

      print 'end'

    C:\Python27>python operators.py
    True
    a = 100 == b = 100

    True
    a = 10 != b = 100

    False - <>
    a = 100 b = 100

    True
    a = 100 > b = 10

    False - <
    a = 100 b = 10

    False - <=
    a = 100 b = 10

    True
    a = 100 >= b = 10

    True
    a = 100 <= b = 100
    end

    12. andor.py

    • a = 1
      b = 1000

      if (a and b):
          print 'True'
          print 'a =', a, 'and b =', b
          print ''

      a = 0
      if (a or b):
          print 'True'
          print 'a =', a, 'or b =', b
          print ''

      if not(a and b):
          print 'True'
          print 'not(a =', a, 'and b =', b,')'
      print 'end'

    C:\Python27>python andor.py
    True
    a = 1 and b = 1000

    True
    a = 0 or b = 1000

    True
    not(a = 0 and b = 1000 )
    end

    Function:

    • def functionname( parameters ):
    •   "function_docstring"
    •   function_suite
    •   return [expression]

    13. funexamp.py

    • # Function definition is here
      def print_me( str ):
          "This prints a passed string into this function"
          print str
          return;

      # Now you can call print_me function
      print_me("I'm the first call to user defined function!")

      a="Again second call to the same function"
      print_me(a)

      print 'stop'

    C:\Python27>python funexamp.py
    I'm the first call to user defined function!
    Again second call to the same function
    stop

    14. funexamp2.py

    • # Function definition is here
      def change_me( my_list ):
          "This changes a passed list into this function"
          my_list.append([1,2,3,4]);
          return;

      # Now you can call change_me function
      my_list = [10,20,30];
      change_me( my_list);
      print "Values the function: ", my_list

    C:\Python27>python funexamp2.py
    Values the function:  [10, 20, 30, [1, 2, 3, 4]]

    Project 1: Change Program

    You are creating software to be sold by your company to vending machine manufacturers to count and return change. You are part of a Team, more code may be required to complete the obligations to the customer but you have been given the following assignment

    • Items in the vending machine have a cost value between .01 - .99
    • You will use a Randomly generated value from .01 to .99 to simulate the cost of possible vending items.
    • The customer chooses an item and enters some coins, the machine's electronics determine the coins to be either (Customer enters coins)

    image
    15. proj1.py
    Input coins:
    • quarter=25
      dime = 10
      nickel = 5
      penny = 1
      item_cost = 54

      print 'This item costs ', item_cost
      print ('**Enter coins in form 1,5,10,25,..')

      coin_value = input('enter coin values')

      print coin_value

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 1,5,10,25,1,1,10
    (1, 5, 10, 25, 1, 1, 10)

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 1,5,10,25,1,1,10,12,17
    (1, 5, 10, 25, 1, 1, 10, 12, 17)

    B-)
  • Invalid Coins:

    • num_coins = len(coin_value)
      print coin_value, num_coins
      count = 0

      while count < num_coins: # step each look for invalid
          current_value = coin_value[count]
          coin_amt = int(current_value)
          print 'count = ', count, 'Coin Amount = ', coin_amt
          count += 1
          if coin_amt not in (quarter, dime, nickel, penny):
              print 'An Invalid coin was detected'
              count = 0
              coin_value = 0
              num_coins = 0
              coin_amt = 0
              coin_value = input('enter coin values')
              num_coins = len(coin_value)

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 5,5,1,1,12,10
    (5, 5, 1, 1, 12, 10) 6
    count =  0 Coin Amount =  5
    count =  1 Coin Amount =  5
    count =  2 Coin Amount =  1
    count =  3 Coin Amount =  1
    count =  4 Coin Amount =  12
    An Invalid coin was detected
    enter coin values 5,5,1,1,10
    count =  0 Coin Amount =  5
    count =  1 Coin Amount =  5
    count =  2 Coin Amount =  1
    count =  3 Coin Amount =  1
    count =  4 Coin Amount =  10

    Coin Count:

    • total_quarters = 0    #This is setting coin count to zero only
      total_dimes = 0
      total_nickels = 0
      total_pennies = 0

      print coin_value, num_coins

      while count < num_coins:    # step each look for invalid
          current_value = coin_value[count]
          coin_amt = int(current_value)
          print 'count = ', count, 'Coin Amount = ', coin_amt
          count += 1

          if coin_amt not in (quarter, dime, nickel, penny):
              print 'An Invalid coin was detected'
              count = 0
              coin_value = 0
              num_coins = 0
              coin_amt = 0

              total_quarters = 0    #This is setting coin count to zero only
              total_dimes = 0    #need to deduct the coins returned
              total_nickels = 0    #need to fix this.
              total_pennies = 0

              coin_value = input('enter coin values ')
              num_coins = len(coin_value)

          elif coin_amt == 25:
              total_quarters += 1
          elif coin_amt == 10:
              total_dimes += 1
          elif coin_amt == 5:
              total_nickels += 1
          elif coin_amt == 1:
              total_pennies += 1

      coin_count = (total_quarters, total_dimes, total_nickels, total_pennies)
      total_value = total_quarters * 25 + total_dimes * 10 + total_nickels * 5 + total_pennies

      print 'Coin count = q,d,n,p ' ,coin_count
      print 'Total value =',total_value

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 5,10,5,10
    (5, 10, 5, 10) 4
    count =  0 Coin Amount =  5
    count =  1 Coin Amount =  10
    count =  2 Coin Amount =  5
    count =  3 Coin Amount =  10
    Coin count = q,d,n,p  (0, 2, 2, 0)
    Total value = 30

    Make a decision:

    • done = 0

      while done == 0:
          while count < num_coins:    # step each look for invalid
              current_value = coin_value[count]
              coin_amt = int(current_value)
              print 'count = ', count, 'Coin Amount = ', coin_amt
              count += 1
              if coin_amt not in (quarter, dime, nickel, penny):
          ...
          print 'Total value =',total_value

          # 3 conditions

          if item_cost == total_value:
              # done
              print 'Thank you please take your item'
              done = 1
          elif item_cost < total_value:
              # return change
              return_change = total_value - item_cost
              # *update coin count*
              print 'Please take your change of ',return_change
              print 'Still need to update coin count'
              done = 1
          elif item_cost > total_value:
              # input more coins
              item_cost = item_cost - total_value
              # *Do everything over until done*
              print 'Not enough money please enter ',item_cost
              count = 0
              coin_value = 0
              num_coins = 0
              coin_amt = 0
              total_quarters = 0 # fix this total as well
              total_dimes = 0
              total_nickels = 0
              total_pennies = 0
              coin_value = input('enter coin values')
              num_coins = len(coin_value)

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 25,25,1,1,1,1
    (25, 25, 1, 1, 1, 1) 6
    count =  0 Coin Amount =  25
    count =  1 Coin Amount =  25
    count =  2 Coin Amount =  1
    count =  3 Coin Amount =  1
    count =  4 Coin Amount =  1
    count =  5 Coin Amount =  1
    Coin count = q,d,n,p  (2, 0, 0, 4)
    Total value = 54
    Thank you please take your item

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 25,25,10
    (25, 25, 10) 3
    count =  0 Coin Amount =  25
    count =  1 Coin Amount =  25
    count =  2 Coin Amount =  10
    Coin count = q,d,n,p  (2, 1, 0, 0)
    Total value = 60
    Please take your change of  6
    Still need to update coin count

    C:\Python27>python proj1.py
    This item costs  54
    **Enter coins in form 1,5,10,25,..
    enter coin values 25,25
    (25, 25) 2
    count =  0 Coin Amount =  25
    count =  1 Coin Amount =  25
    Coin count = q,d,n,p  (2, 0, 0, 0)
    Total value = 50
    Not enough money please enter  4
    enter coin values 1,1,1,1
    count =  0 Coin Amount =  1
    count =  1 Coin Amount =  1
    count =  2 Coin Amount =  1
    count =  3 Coin Amount =  1
    Coin count = q,d,n,p  (0, 0, 0, 4)
    Total value = 4
    Thank you please take your item

    B-)
  • 16. proj1_g2.py

    • upload.i4th.in.th:8080/th/download.php?id=593CE4151

    Case: 1st, Condition: Exact Change, Change in: 2 quarters, Total coins: (2,0,0,0), Total Value: 50, Action: Take Item

    C:\Python27>python proj1_g2.py

    *************New Purchase****************
     This item costs  50
    **Enter coins in form 1,5,10,25,..
    25,25
    (25, 25) 2
    Is machine Being Serviced?
    0 for no or 1 for yes
    0
    Checking for invalid coins
    Checking for invalid coins
    Thank you please take your item
    Coin count= q,d,n,p  (2, 0, 0, 0)
    Total value= 50
    ***********End of Purchase****************

    Case: 2nd, Condition: Exact Change, Change in: 5 dimes, Total coins: (2,5,0,0), Total Value: 100, Action: Take Item

    *************New Purchase****************
     This item costs  50
    **Enter coins in form 1,5,10,25,..
    10,10,10,10,10
    (10, 10, 10, 10, 10) 5
    Is machine Being Serviced?
    0 for no or 1 for yes
    0
    Checking for invalid coins
    Checking for invalid coins
    Checking for invalid coins
    Checking for invalid coins
    Checking for invalid coins
    Thank you please take your item
    Coin count= q,d,n,p  (2, 5, 0, 0)
    Total value= 100
    ***********End of Purchase****************

    Case: 3rd, Condition: Too Much, Change in: 2 quarters 2 dimes, Total coins: (4,5,0,0), Total Value: 150, Action: Return change

    *************New Purchase****************
     This item costs  50
    **Enter coins in form 1,5,10,25,..
    25,25,10,10
    (25, 25, 10, 10) 4
    Is machine Being Serviced?
    0 for no or 1 for yes
    0
    Checking for invalid coins
    Checking for invalid coins
    Checking for invalid coins
    Checking for invalid coins
    Return Change (0, 2, 0, 0)
    updated coin totals = q,d,n,p  (4, 5, 0, 0)
    Thank you please take your item
    Coin count= q,d,n,p  (4, 5, 0, 0)
    Total value= 150
    ***********End of Purchase****************

    Case: 4th, Condition: Not enough, Change in: 1 quarters 1 nickle, Total coins: (5,5,1,0), Total Value: 180, Action: Enter difference

    *************New Purchase****************
     This item costs  50
    **Enter coins in form 1,5,10,25,..
    25,5
    (25, 5) 2
    Is machine Being Serviced?
    0 for no or 1 for yes
    0
    Checking for invalid coins
    Checking for invalid coins
    Not enough money please enter  20
    Coin count= q,d,n,p  (5, 5, 1, 0)
    Total value= 180
    enter coin values 10,5,5


    Checking for invalid coins
    Checking for invalid coins
    Checking for invalid coins
    Thank you please take your item
    Coin count= q,d,n,p  (5, 6, 3, 0)
    Total value= 200
    ***********End of Purchase****************

    Case: 5th, Condition: Invalid Coin, Change in: 10,40, Total coins: (5,6,3,0), Total Value: 200, Action: return coin

    *************New Purchase****************
     This item costs  50
    **Enter coins in form 1,5,10,25,..
    10,40
    (10, 40) 2
    Is machine Being Serviced?
    0 for no or 1 for yes
    0
    Checking for invalid coins
    Checking for invalid coins
    An Invalid coin was detected please take change
    Coin count is  (5, 6, 3, 0)
    enter coin values 25,25
    Checking for invalid coins
    Checking for invalid coins
    Thank you please take your item
    Coin count= q,d,n,p  (7, 6, 3, 0)
    Total value= 250
    ***********End of Purchase****************

    gns3.com

    17. func.py
    • #Procedure 1
      def main():
          try:
              # Get a number to manipulate
              num = float(input("Please enter a number to manipulate.\n"))
              # Store the result of the value, after it has been manipulated
              # by Procedure 2
              addednum = addfive(num)
              # Store the result of the value, after it has been manipulated
              # by Procedure 3
              multipliednum = multiply(addednum)
              # Send the value to Procedure 4
              display(multipliednum)
          # Deal with exceptions from non-numeric user entry
          except ValueError:
              print("You must enter a valid number.\n")
              # Reset the value of num, to clear non-numeric data.
              num = 0
              # Call main, again.
              main()
             
      # Procedure 2
      def addfive(num):
          return num + 5
         
      # Procedure 3
      def multiply(addednum):
          return addednum * 2
         
      # Procedure 4
      def display(multi):
          # Display the final value
          print("The final value is ",multi)
         
      # Call Procedure 1
      main()

    C:\Python27>python func.py
    Please enter a number to manipulate.
    'k'
    You must enter a valid number.

    Please enter a number to manipulate.
    2
    ('The final value is ', 14.0)

    18. class1.py

    • class Numchange:

          def _int_(self):
              self._number = 0
          def addfive(self,num):
              self._number = num
              return self._number + 5
             
          def multiply(self,added):
              self._added = added
              return self._added * 2

    19. op1.py

    • import class1

      maths = class1.Numchange()

      def main():

          num = float(input("Please enter a number.\n"))
         
          added = maths.addfive(num)
         
          multip = maths.multiply(added)
         
          print("The manipulated value is ", multip)
         
      main()

    C:\Python27>python op1.py
    Please enter a number.
    2
    ('The manipulated value is ', 14.0)

    • Functions are reusable pieces of the program. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times.

    • Def is a keyword used for function.

    • def sayHello():
        print('Hello World!')
      sayHello()
      sayHello()

      Output: Hello World! Hello World!

      Functions are defined using the def keyword. After this keyword comes an identifier name for the function, followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the line. Next follows the block of statements that are part of this function.
    B-)
  • 20. TN3.py
    • import telnetlib
      import time

      def telnet_gns3(ip):
          wait = .2
         
          connection = telnetlib.Telnet(ip, 23, 5)
         
          output = connection.read_until("Password:", 5)
          connection.write('cisco' + "\n")
          connection.write('ena' + "\n")
         
          output = connection.read_until("Password:", 5)
          connection.write('cisco' + "\n")
          time.sleep(wait)
         
          connection.write("conf term" + "\n")
          time.sleep(wait)
          connection.write("int f0/0" + "\n")
          time.sleep(wait)
          connection.write("ip add 6.5.5.5 255.0.0.0" + "\n")
          time.sleep(wait)
          connection.write("end" + "\n")
         
          time.sleep(wait)
          connection.write("sho ip int brief" + "\n")
          time.sleep(wait)
         
          output = connection.read_very_eager()
          print output
         
          connection.close()
         
      #Call gns3
      telnet_gns3('192.168.56.101')

    C:\Python27>python TN3.py

    ESW1#conf term
    Enter configuration commands, one per line.  End with CNTL/Z.
    ESW1(config)#int f0/0
    ESW1(config-if)#ip add 6.5.5.5 255.0.0.0
    ESW1(config-if)#end
    ESW1#sho ip int brief
    Interface                  IP-Address      OK? Method Status                Protocol
    FastEthernet0/0            6.5.5.5         YES manual administratively down down
    FastEthernet0/1            192.168.56.101  YES manual up                    up
    FastEthernet1/0            unassigned      YES unset  up                    down
    FastEthernet1/1            unassigned      YES unset  up                    down
    FastEthernet1/2            unassigned      YES unset  up                    down
    FastEthernet1/3            unassigned      YES unset  up                    down
    FastEthernet1/4            unassigned      YES unset  up                    down
    FastEthernet1/5            unassigned      YES unset  up                    down
    FastEthernet1/6            unassigned      YES unset  up                    down
    FastEthernet1/7            unassigned      YES unset  up                    down
    FastEthernet1/8            unassigned      YES unset  up                    down
     --More--

    21. TN4.py
    • ...
          output = connection.read_very_eager()
          # - Write output to a file -
          ESW1 = open("ESW1", "w")
          ESW1.write(output)
          ESW1.close
          print output
      ...

    upload.i4th.in.th/th/download.php?id=59493B941

    22. TN7.py

    • import telnetlib
      import time

      def telnet_gns3(ip):
          wait = 5
          # -Sign in-
          connection = telnetlib.Telnet(ip, 23, 5)
          connection.read_until("Password:", 5)
          connection.write('cisco' + "\n")
          connection.write('ena' + "\n")
          connection.read_until("Password:", 5)
          connection.write('cisco' + "\n")
          # -Sign in-
         
          # -Command loop-
          cmd_file = raw_input('Enter command file name and extension: ')
          selected_cmd_file = open(cmd_file, 'r')
          selected_cmd_file.seek(0)
          for each_line in selected_cmd_file.readlines():
              time.sleep(wait)
              connection.write(each_line)
              connection.write("\n")
          # -Command loop-
         
          # -Write output to a file-
          time.sleep(wait)
          output = connection.read_very_eager()
          ESW1 = open("ESW1", "w")
          ESW1.write(output)
          ESW1.close
          # -Write output to a file-
          print output
         
          connection.close()
         
      #Call gns3
      telnet_gns3('192.168.56.101')

    testcmds.txt

    • conf term
      int f0/0
      ip add 5.5.5.5 255.0.0.0
      end
      sho ip int brief
      sho run

    C:\Python27>python TN7.py
    Enter command file name and extension: testcmds.txt

    ESW1#conf term
    Enter configuration commands, one per line.  End with CNTL/Z.
    ESW1(config)#
    ESW1(config)#int f0/0
    ESW1(config-if)#
    ESW1(config-if)#ip add 5.5.5.5 255.0.0.0
    ESW1(config-if)#
    ESW1(config-if)#end
    ESW1#
    ESW1#sho ip int brief
    Interface                  IP-Address      OK? Method Status                Protocol
    FastEthernet0/0            5.5.5.5         YES manual administratively down down
    FastEthernet0/1            192.168.56.101  YES manual up                    up
    ...
    upload.i4th.in.th/th/download.php?id=594E30291

    B-)
  • Is the CCIE Dead? The Automated Future of IT

    Drive For Show And Putt For Dough - ลูกไดรฟ์มีไว้อวด ลูกพัตต์มีไว้เอาเงิน

    GUI For Show And API For Dough

    Why API?
    • Automation
    • Integration
    • Innovation

    What about networking skills?

    • "A fool with a tool is still a fool"
    B-)