Advertisement
KChrisC

Python Enumerate Example

Jul 29th, 2014
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name:        module1
  3. # Purpose:
  4. #
  5. # Notes:
  6. #
  7. # References:  http://azitech.wordpress.com/2011/07/06/pythons-enumerate-function/
  8. #
  9. # Author:      User
  10. #
  11. # Created:     29/07/2014
  12. # Copyright:   (c) User 2014
  13. # Licence:     <your licence>
  14. #
  15. # Prefixes:    f_ = Normal Function, v_ = Variable, k_ = Lambda Function
  16. #              g_ = Generator Function, o_ = Class Obj., c_ = Class
  17. #              go_ = Gen. Obj., m_ = Method, ko_ = Lambda Obj.
  18. #-------------------------------------------------------------------------------
  19.     # Imports #
  20. import inspect
  21. import locale
  22. from datetime import datetime
  23.  
  24. # Configurations #
  25. locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'
  26.  
  27.  
  28.     # Main #
  29. def main():
  30.         # Variables
  31.             # Global
  32.  
  33.             # Local
  34.  
  35.             # Init Generators
  36.  
  37.         # Main Code
  38.     print(">>>To demo Python's 'enumerate' func.<<<")
  39.     ##'Enumerate' allows for iteration using index and value from a list, tuple, etc.
  40.  
  41.     x = [1, 5, 7, 3, 8] ## Init test list
  42.     print("x = %s") %(x) ## Print test list
  43.  
  44.     for index, item in enumerate(x): ## Assigns the list's index value and the item value at that index to the two iteration variables in turn
  45.         print("x[%i] = %i") %(index, item) ## Prints each index and coresponding item value from the list.
  46.         print("Just item: %s") %(item) ## Prints just the item value.
  47.  
  48.  
  49.         # Tests
  50.             # Print line number (prl)
  51.     ## print("Line: %i") %(f_LineNo()) # DEBUG
  52.             # Output to file
  53.     ## v_Path = 'C:/Users/User/Dropbox/Python/Out/out.txt'
  54.     ## v_Mode = 'w'
  55.     ## v_FileConn = f_OutputToFileOut(v_Path, v_Mode)
  56.  
  57.     ## sys.exit("Error message") ## Stop execution
  58.  
  59. #-----------Basement------------
  60.  
  61.     # Functions #
  62.         # Normal Functions
  63.  
  64.         # Generator Functions
  65.  
  66.         # Lambda Functions
  67.  
  68.     # Classes #
  69.  
  70.         # Test Functions
  71.             # Print line number func
  72. def f_LineNo(): ## Returns the current line number in  program
  73.     return inspect.currentframe().f_back.f_lineno
  74.  
  75.             # Print output to file
  76. def f_OutputToFileOut(v_Path, v_Mode):
  77.     import sys ## Imports 'sys' if not already done
  78.     v_orig_stdout = sys.stdout ## Backs up 'sys.stdout' so may be restored when complete
  79.     v_FileConn = file(v_Path, v_Mode) ## Opens a connection and assigns to a connection variable
  80.         ## such that ('file path and name', 'mode')
  81.         ## Modes in Windows: 'r' = read; 'w' = write; 'a' appending; 'r+' for reading and writing
  82.         ## On Windows, 'b' appended to the mode opens the file in binary mode like 'r+b'
  83.         ## *** 'r' is assumed if mode is omitted
  84.     sys.stdout = v_FileConn ## Sets 'sys.stdout' to the connection variable
  85.         ## The output going to the file
  86.  
  87.     # Main Loop #
  88. if __name__ == '__main__':
  89.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement