Python Packages
What is a Python Package?
Python modules may contain several classes, functions,
variables, etc. whereas Python packages contain several
modules. In simpler terms, Package in Python is a folder that
contains various modules as files.
Creating Package
Let’s create a package in Python named mypckg that will contain
two modules mod1 and mod2.
To create this module follow the below steps:
Create a folder named mypckg.
Inside this folder create an empty Python file i.e. __init__.py
Then create two modules mod1 and mod2 in this folder.
The Hierarchy of our Python package looks like this:
JSON:
JavaScript Object Notation (JSON) is an easy to read and
write data interchange format.
JSON is used as an alternative to XML and is easy for
machines to parse and generate.
JSON is built on two structures - a collection of name-
value pairs (e.g. a Python dictionary) and ordered lists of
values (e.g.. A Pythonlist).
Python JSON
• JSON is a syntax for storing and exchanging
data.
• JSON is text, written with JavaScript object
notation.
JSON in Python
Python has a built-in package called json, which can be used to
work with JSON data.
Import the json module:
import json
Convert from JSON to Python
If you have a JSON string, you can parse it by
using the json.loads() method.
Example
• Convert from JSON to Python
• import json
• # some JSON:
• x = '{ "name":"John", "age":30, "city":"New York"}’
• # parse x:
• y = json.loads(x)
• # the result is a Python dictionary:
• print(y["age"])
Convert from Python to JSON
If you have a Python object, you can convert it
into a JSON string by using the json.dumps()
method.
Example
Convert from Python to JSON:
import json
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
You can convert Python objects of the following types,
into JSON strings:
dict
list
tuple
string
int
float
True
False
None
• Example
• Convert Python objects into JSON strings, and print
the values:
• import json
• print(json.dumps({"name": "John", "age": 30}))
• print(json.dumps(["apple", "bananas"]))
• print(json.dumps(("apple", "bananas")))
• print(json.dumps("hello"))
• print(json.dumps(42))
• print(json.dumps(31.76))
• print(json.dumps(True))
• print(json.dumps(False))
• print(json.dumps(None))
When you convert from Python to JSON, Python
objects are converted into the JSON (JavaScript)
equivalent:
Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null
Example
Convert a Python object containing all the legal data types:
import json
x = {
"name": "John",
"age": 30,
"married": True,
"divorced": False,
"children": ("Ann","Billy"),
"pets": None,
"cars": [
{"model": "BMW 230", "mpg": 27.5},
{"model": "Ford Edge", "mpg": 24.1}
]
}
print(json.dumps(x))
• XML: XML (Extensible Markup Language) is a
data format for structured document
interchange.
• The Python minidom library provides a
minimal implementation of the Document
Object Model interface and has an API similar
to that in other languages.
HTTPLib & URLLib: HTTPLib2 and URLLib2 are Python
libraries used in network/internet programming.
SMTPLib: Simple Mail Transfer Protocol (SMTP) is a protocol
which handles sending email and routing e-mail between mail
servers. The Python smtplib module provides an SMTP client
session object that can be used to send email.
NumPy: NumPy is a package for scientific computing in Python.
NumPy provides support for large multi-dimensional arrays
and matrices.
What is XML?
• XML is an abbreviation name of "Extensible Markup
Language".
• It is used to understand data dynamically by the XML
framework.
• It is primarily focused on creating web pages where
the data has a specific structure.
• A page is created using the XML known as the XML
document.
• XML generates a tree-like structure that is
straightforward and supports hierarchy.
The sample structure of the XML file
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
JSON,XML.pptx

JSON,XML.pptx

  • 1.
  • 2.
    What is aPython Package? Python modules may contain several classes, functions, variables, etc. whereas Python packages contain several modules. In simpler terms, Package in Python is a folder that contains various modules as files. Creating Package Let’s create a package in Python named mypckg that will contain two modules mod1 and mod2. To create this module follow the below steps: Create a folder named mypckg. Inside this folder create an empty Python file i.e. __init__.py Then create two modules mod1 and mod2 in this folder.
  • 3.
    The Hierarchy ofour Python package looks like this:
  • 4.
    JSON: JavaScript Object Notation(JSON) is an easy to read and write data interchange format. JSON is used as an alternative to XML and is easy for machines to parse and generate. JSON is built on two structures - a collection of name- value pairs (e.g. a Python dictionary) and ordered lists of values (e.g.. A Pythonlist).
  • 5.
    Python JSON • JSONis a syntax for storing and exchanging data. • JSON is text, written with JavaScript object notation. JSON in Python Python has a built-in package called json, which can be used to work with JSON data. Import the json module: import json
  • 6.
    Convert from JSONto Python If you have a JSON string, you can parse it by using the json.loads() method.
  • 7.
    Example • Convert fromJSON to Python • import json • # some JSON: • x = '{ "name":"John", "age":30, "city":"New York"}’ • # parse x: • y = json.loads(x) • # the result is a Python dictionary: • print(y["age"])
  • 8.
    Convert from Pythonto JSON If you have a Python object, you can convert it into a JSON string by using the json.dumps() method.
  • 9.
    Example Convert from Pythonto JSON: import json # a Python object (dict): x = { "name": "John", "age": 30, "city": "New York" } # convert into JSON: y = json.dumps(x) # the result is a JSON string: print(y)
  • 10.
    You can convertPython objects of the following types, into JSON strings: dict list tuple string int float True False None
  • 11.
    • Example • ConvertPython objects into JSON strings, and print the values: • import json • print(json.dumps({"name": "John", "age": 30})) • print(json.dumps(["apple", "bananas"])) • print(json.dumps(("apple", "bananas"))) • print(json.dumps("hello")) • print(json.dumps(42)) • print(json.dumps(31.76)) • print(json.dumps(True)) • print(json.dumps(False)) • print(json.dumps(None))
  • 12.
    When you convertfrom Python to JSON, Python objects are converted into the JSON (JavaScript) equivalent: Python JSON dict Object list Array tuple Array str String int Number float Number True true False false None null
  • 13.
    Example Convert a Pythonobject containing all the legal data types: import json x = { "name": "John", "age": 30, "married": True, "divorced": False, "children": ("Ann","Billy"), "pets": None, "cars": [ {"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ] } print(json.dumps(x))
  • 14.
    • XML: XML(Extensible Markup Language) is a data format for structured document interchange. • The Python minidom library provides a minimal implementation of the Document Object Model interface and has an API similar to that in other languages.
  • 15.
    HTTPLib & URLLib:HTTPLib2 and URLLib2 are Python libraries used in network/internet programming. SMTPLib: Simple Mail Transfer Protocol (SMTP) is a protocol which handles sending email and routing e-mail between mail servers. The Python smtplib module provides an SMTP client session object that can be used to send email. NumPy: NumPy is a package for scientific computing in Python. NumPy provides support for large multi-dimensional arrays and matrices.
  • 16.
    What is XML? •XML is an abbreviation name of "Extensible Markup Language". • It is used to understand data dynamically by the XML framework. • It is primarily focused on creating web pages where the data has a specific structure. • A page is created using the XML known as the XML document. • XML generates a tree-like structure that is straightforward and supports hierarchy.
  • 17.
    The sample structureof the XML file <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description>