Python

By default, Python source files are treated as encoded in UTF-8.


To declare an encoding other than the default one, a special comment line should be added as the first line of the file. The syntax is as follows:
# -*- coding: encoding -*-



Sort a tuple

Tuples are arrays you cannot modify and don’t have any sort function.

But, there is a way. You can however use the sorted() function which returns a list. This list can be converted to a new tuple.

#!/usr/bin/env python

person = ('Alison','Victoria','Brenda','Rachel','Trevor')
person = tuple(sorted(person))
print(person)

Keep in mind a tuple cannot be modified, we simple create a new tuple that happens to be sorted.


Dictionary get() to fetch value: 

 This method returns the value for the given key, if present in the dictionary. If not, then it will return None (if get() is


dic = {"A":1, "B":2} print(dic.get("A")) print(dic.get("C")) print(dic.get("C","Not Found ! "))


-----------------------------------------------------------------------
Oracle connection using cx_oracle
----------------------------------------------------------------------- 1. download cx_oracle and install using pip command.
pip install cx_oracle

2. add location in PYTHONPATH variable ( sys.path)

Database query 1:

import cx_Oracle

ip = 'localhost'
port = 1521
SID = 'orcl'

dsn_tns = cx_Oracle.makedsn(ip, port, SID)
connection = cx_Oracle.connect('sys', 'sys', dsn_tns,cx_Oracle.SYSDBA)
sql = """SELECT * FROM mongo.products"""

curs = connection.cursor()
curs.execute(sql)

for row in curs:

    print(row)



Database query 2 :

import pandas as pd
import cx_Oracle

ip = 'localhost'
port = 1521
SID = 'orcl'

dsn_tns = cx_Oracle.makedsn(ip, port, SID)
print(dsn_tns)
connection = cx_Oracle.connect('sys', 'sys', dsn_tns,cx_Oracle.SYSDBA)
print(connection)
query = """SELECT * FROM mongo.products"""

df = pd.read_sql(query, con=connection)

print(df)


-----------------------------------------------------------------------
MongoDB connection using pymongo
-----------------------------------------------------------------------



-----------------------------------------------------------------------
generator vs iterator
-----------------------------------------------------------------------
  1. In creating a python generator, we use a function. But in creating an iterator in python, we use the iter() and next() functions.
  2. A generator in python makes use of the ‘yield’ keyword. A python iterator doesn’t.
  3. Python generator saves the states of the local variables every time ‘yield’ pauses the loop in python. An iterator does not make use of local variables, all it needs is iterable to iterate on.
  4. A generator may have any number of ‘yield’ statements.
  5. You can implement your own iterator using a python class; a generator does not need a class in python.
  6. To write a python generator, you can either use a Python function or a comprehension. But for an iterator, you must use the iter() and next() functions.
  7. Generator in python let us write fast and compact code. This is an advantage over Python iterators. They are also simpler to code than do custom iterator.
  8. Python iterator is more memory-efficient. 
-----------------------------------------------------------------------
For loop, map() and list comprehension
-----------------------------------------------------------------------
List comprehension is more flexible than map/for loop option.

new_list = [expression for member in iterable]

-----------------------------------------------------------------------
How Python Search module
-----------------------------------------------------------------------
1. Search standard library
2. Search present working directory.
3. search sys.path ( Set using PYTHONPATH env variable)


-----------------------------------------------------------------------
Important facts :

-----------------------------------------------------------------------

1. File Handling : If we open file with 'with' clause , file will be closed automatically.
2.  9//4   = 2    ( "// " floor division discards the fractional part)
3.  Set is collection of unique records and immutable. It doesn't have index so , cant be used for slicing operation.




Comments

Popular posts from this blog