Skip to content

Instantly share code, notes, and snippets.

@MishraKhushbu
Last active April 19, 2018 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MishraKhushbu/4d6de83ad8413a30cacdc59a15041b6d to your computer and use it in GitHub Desktop.
Save MishraKhushbu/4d6de83ad8413a30cacdc59a15041b6d to your computer and use it in GitHub Desktop.
Python_Modules_19_April_2018
JSON - Python has a JSON module that will help converting the datastructures to JSON strings.
Use the import function to import the JSON module.
import json
student = {"101":{"class":'V', "Name":'Rohit', "Roll_no":7},
"102":{"class":'V', "Name":'David', "Roll_no":8},
"103":{"class":'V', "Name":'Samiya', "Roll_no":12}}
print(json.dumps(student));
Output:
{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12},
"102": {"class": "V", "Name": "David", "Roll_no": 8},
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}
JSON strings to Python Dictionaries
import json
json_data = '{"103": {"class": "V", "Name": "Samiya", "Roll_n": 12}, "102": {"class": "V", "Name": "David", "Roll_no": 8}, "101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}';
print(json.loads(json_data));
Output:
{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12},
"102": {"class": "V", "Name": "David", "Roll_no": 8},
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}
------------------------------------------------------------------------
Logging:provides tracking for events that occur while software runs. You can add logging calls to your code to indicate what events have happened.
The logging module keeps a record of the events that occur within a program, making it possible to see output related to any of the events that occur throughout the runtime of a piece of software.
Levels :Debug,Info,Warning,Error,Critical
---------------------------------------------------------------------------------
OS: The main purpose of the OS module is to interact with your operating system.
The primary use I find for it is to create folders, remove folders, move folders, and sometimes change the working directory.
You can also access the names of files within a file path by doing listdir().
e.g os.mkdir('newDir'),os.rename('newDir','newDir2')
--------------------------------------------------------------------------------------
SYS:This module provides a number of functions and variables that can be used to manipulate different parts of the Python runtime environment.
--------------------------------------------
SubProcess:
The subprocess module allows us to:
1.spawn new processes
2.connect to their input/output/error pipes
3.obtain their return codes
To run UNIX commands we need to create a subprocess that runs the command.
------------------------------------------------------------------------------------
Time:
There is a popular time module available in Python which provides functions for working with times and for converting between representations.
DateTime:
The datetime module provides a number of types to deal with dates, times, and time intervals.
This module replaces the integer/tuple-based time mechanisms in the time module with a more object-oriented interface.
------------------------------------------------------------------------------------------
getpass – Prompt the user for a password without echoing
The getopt module helps us parse the options and operands that are provided to our program on the command line. This module has one very important function, also named getopt.
getopt.getopt( args , options , 〈 long_options 〉) → ( options, operands )
Decode the given sequence of arguments, args , using the given set of options and long_options . Returns a tuple with a sequence of normalized (option,value) pairs plus a sequence of the program's operand values.
The args value should not include sys.argv[0], the program name. Therefore, the argument value for args is almost always sys.argv[1:].
The options value is a string of the one-letter options. Any options which require argument values are followed by a :. For example, "ab:c" means that the program will accept -a, -c, -ac, -b value as options.
The long_options value is optional, if present it is a list of the long options. If a long option requires a parameter value, it's name must end in =. For example, ("silent","debug","log=") means that options like --silent, --debug, and --log=myfile.log are accepted as options.
There are two results of getopt: the options and the operands. The options is a list of ( name , value ) pairs. The operands is the list of names which follow the last option. In most cases, this list is a list of file names to be used as input.
--------------------------------------------------------------------------------------
TempFile:
Many programs need to create files to write intermediate data. Creating files with unique names securely, so they cannot be guessed by someone wanting to break the application, is challenging. The tempfile module provides several functions for creating filesystem resources securely. TemporaryFile() opens and returns an un-named file, NamedTemporaryFile() opens and returns a named file, and mkdtemp()
creates a temporary directory and returns its name.
------------------------------------------------------------------------------------------
Collections:
The collections module includes container data types beyond the built-in types list, dict, and tuple.
Counter
Initializing
Accessing Counts
Arithmetic
defaultdict
Deque
Populating
Consuming
Rotating
namedtuple
Defining
Invalid Field Names
OrderedDict
Equality
---------------------------------------------------------------------------------------------------------
Itertools:
Python provides a great module for creating your own iterators. The module I am referring to is **itertools**. The tools provided by itertools are fast and memory efficient.
You will be able to take these building blocks to create your own specialized iterators that can be used for efficient looping.
---------------------------------------------------------------
Module errno
This module makes available standard errno system symbols.
The value of each symbol is the corresponding integer value, e.g., on most systems, errno.ENOENT equals the integer 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment