Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: module1
- # Purpose:
- #
- # Notes:
- #
- # References: http://azitech.wordpress.com/2011/07/06/pythons-enumerate-function/
- #
- # Author: User
- #
- # Created: 29/07/2014
- # Copyright: (c) User 2014
- # Licence: <your licence>
- #
- # Prefixes: f_ = Normal Function, v_ = Variable, k_ = Lambda Function
- # g_ = Generator Function, o_ = Class Obj., c_ = Class
- # go_ = Gen. Obj., m_ = Method, ko_ = Lambda Obj.
- #-------------------------------------------------------------------------------
- # Imports #
- import inspect
- import locale
- from datetime import datetime
- # Configurations #
- locale.setlocale(locale.LC_ALL, '') # Set the locale for your system 'en_US.UTF-8'
- # Main #
- def main():
- # Variables
- # Global
- # Local
- # Init Generators
- # Main Code
- print(">>>To demo Python's 'enumerate' func.<<<")
- ##'Enumerate' allows for iteration using index and value from a list, tuple, etc.
- x = [1, 5, 7, 3, 8] ## Init test list
- print("x = %s") %(x) ## Print test list
- 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
- print("x[%i] = %i") %(index, item) ## Prints each index and coresponding item value from the list.
- print("Just item: %s") %(item) ## Prints just the item value.
- # Tests
- # Print line number (prl)
- ## print("Line: %i") %(f_LineNo()) # DEBUG
- # Output to file
- ## v_Path = 'C:/Users/User/Dropbox/Python/Out/out.txt'
- ## v_Mode = 'w'
- ## v_FileConn = f_OutputToFileOut(v_Path, v_Mode)
- ## sys.exit("Error message") ## Stop execution
- #-----------Basement------------
- # Functions #
- # Normal Functions
- # Generator Functions
- # Lambda Functions
- # Classes #
- # Test Functions
- # Print line number func
- def f_LineNo(): ## Returns the current line number in program
- return inspect.currentframe().f_back.f_lineno
- # Print output to file
- def f_OutputToFileOut(v_Path, v_Mode):
- import sys ## Imports 'sys' if not already done
- v_orig_stdout = sys.stdout ## Backs up 'sys.stdout' so may be restored when complete
- v_FileConn = file(v_Path, v_Mode) ## Opens a connection and assigns to a connection variable
- ## such that ('file path and name', 'mode')
- ## Modes in Windows: 'r' = read; 'w' = write; 'a' appending; 'r+' for reading and writing
- ## On Windows, 'b' appended to the mode opens the file in binary mode like 'r+b'
- ## *** 'r' is assumed if mode is omitted
- sys.stdout = v_FileConn ## Sets 'sys.stdout' to the connection variable
- ## The output going to the file
- # Main Loop #
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement