Python for Bioinformatics

This Jupyter notebook is intented to be used alongside the book Python for Bioinformatics

Note: The code in this chapter requires a database servers to run (a MySQL and MongoDB), so you should provide one and then change the appropiate parameters in the connect string. The example with sqlite can run in this Jupyter Notebook.


In [1]:
!curl https://raw.githubusercontent.com/Serulab/Py4Bio/master/samples/samples.tar.bz2 -o samples.tar.bz2
!mkdir samples
!tar xvfj samples.tar.bz2 -C samples


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16.5M  100 16.5M    0     0  21.4M      0 --:--:-- --:--:-- --:--:-- 21.4M
._.
./
./vectorssmall.fasta
./prot.fas
./conglycinin.phy
./input4align.dnd
./Q5R5X8.fas
./Q9JJE1.xml
./primers.txt
./NC_006581.gb
./PythonU.db
./hsc1.fasta
./B1.csv
./sampleXblast.xml
./sampledata.xlsx
./NC2033.txt
./conglycinin.dnd
./BLAST_output.xml
./UniVec_Core.nhr
./seqA.fas
./fishdata.csv
./template
./pdbaa
./bioinfo/
./fishbacteria.csv
./B1IXL9.txt
./._GSM188012.CEL
./GSM188012.CEL
./example.aln
./data.csv
./3seqs.fas
./BcrA.gp
./uniprotrecord.xml
./contig1.ace
./other.xml
./UniVec_Core.nsq
./test3.csv
./conglycinin.multiple.phy
./fasta22.fas
./pMOSBlue.txt
./readme.txt
./sampleX.fas
./UniVec_Core.nin
./pdb1apk.ent.gz
./cas9align.fasta
./a19.gp
./phd1
./t3beta.fasta
./conglycinin.fasta
./UniVec_Core
./BLAST_output.html
./spfile.txt
./input4align.fasta
./t3.fasta
./TAIR7_Transcripts_by_map_position.gz
./bioinfo/seqs/
./bioinfo/seqs/513710.fasta
./bioinfo/seqs/6598312.fasta
./bioinfo/seqs/4586830.fasta
./bioinfo/seqs/15721870.fasta
./bioinfo/seqs/2623545.fasta
./bioinfo/seqs/218744616.fasta
./bioinfo/seqs/7415878.fasta
./bioinfo/seqs/63108399.fasta
./bioinfo/seqs/513717.fasta
./bioinfo/seqs/7638455.fasta
./bioinfo/seqs/513719.fasta
./bioinfo/seqs/513419.fasta
./bioinfo/seqs/513718.fasta

In [2]:
!wget https://raw.githubusercontent.com/Serulab/Py4Bio/master/code/ch12/PythonU.sql
!apt-get -y install mysql-server
!/etc/init.d/mysql start
!mysql -e 'create database PythonU;'
!mysql PythonU < PythonU.sql
!mysql -e "UPDATE mysql.user SET authentication_string=password('mypassword'),host='%',plugin='mysql_native_password' WHERE user='root';flush privileges;"


--2020-06-04 14:15:41--  https://raw.githubusercontent.com/Serulab/Py4Bio/master/code/ch12/PythonU.sql
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3276 (3.2K) [text/plain]
Saving to: ‘PythonU.sql’

PythonU.sql         100%[===================>]   3.20K  --.-KB/s    in 0s      

2020-06-04 14:15:41 (47.1 MB/s) - ‘PythonU.sql’ saved [3276/3276]

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.1-6
  libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
  libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-server-5.7 mysql-server-core-5.7 psmisc
Suggested packages:
  libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
  libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.1-6
  libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
  libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-server mysql-server-5.7 mysql-server-core-5.7
  psmisc
0 upgraded, 20 newly installed, 0 to remove and 32 not upgraded.
Need to get 19.7 MB of archives.
After this operation, 157 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 mysql-client-core-5.7 amd64 5.7.30-0ubuntu0.18.04.1 [6,649 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 mysql-client-5.7 amd64 5.7.30-0ubuntu0.18.04.1 [1,945 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 mysql-server-core-5.7 amd64 5.7.30-0ubuntu0.18.04.1 [7,437 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 psmisc amd64 23.1-1ubuntu0.1 [52.5 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/main amd64 libevent-core-2.1-6 amd64 2.1.8-stable-4build1 [85.9 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 mysql-server-5.7 amd64 5.7.30-0ubuntu0.18.04.1 [2,929 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 libhtml-tagset-perl all 3.20-3 [12.1 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/main amd64 liburi-perl all 1.73-1 [77.2 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 libhtml-parser-perl amd64 3.72-3build1 [85.9 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 libcgi-pm-perl all 4.38-1 [185 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/main amd64 libfcgi-perl amd64 0.78-2build1 [32.8 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 libcgi-fast-perl all 1:2.13-1 [9,940 B]
Get:13 http://archive.ubuntu.com/ubuntu bionic/main amd64 libencode-locale-perl all 1.05-1 [12.3 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic/main amd64 libhtml-template-perl all 2.97-1 [59.0 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic/main amd64 libtimedate-perl all 2.3000-2 [37.5 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic/main amd64 libhttp-date-perl all 6.02-1 [10.4 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic/main amd64 libio-html-perl all 1.001-1 [14.9 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic/main amd64 liblwp-mediatypes-perl all 6.02-1 [21.7 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic/main amd64 libhttp-message-perl all 6.14-1 [72.1 kB]
Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 mysql-server all 5.7.30-0ubuntu0.18.04.1 [9,948 B]
Fetched 19.7 MB in 4s (4,831 kB/s)
Preconfiguring packages ...
Selecting previously unselected package mysql-client-core-5.7.
(Reading database ... 144467 files and directories currently installed.)
Preparing to unpack .../00-mysql-client-core-5.7_5.7.30-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-client-core-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Selecting previously unselected package mysql-client-5.7.
Preparing to unpack .../01-mysql-client-5.7_5.7.30-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-client-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Selecting previously unselected package mysql-server-core-5.7.
Preparing to unpack .../02-mysql-server-core-5.7_5.7.30-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-server-core-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Selecting previously unselected package psmisc.
Preparing to unpack .../03-psmisc_23.1-1ubuntu0.1_amd64.deb ...
Unpacking psmisc (23.1-1ubuntu0.1) ...
Selecting previously unselected package libevent-core-2.1-6:amd64.
Preparing to unpack .../04-libevent-core-2.1-6_2.1.8-stable-4build1_amd64.deb ...
Unpacking libevent-core-2.1-6:amd64 (2.1.8-stable-4build1) ...
Selecting previously unselected package mysql-server-5.7.
Preparing to unpack .../05-mysql-server-5.7_5.7.30-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-server-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Selecting previously unselected package libhtml-tagset-perl.
Preparing to unpack .../06-libhtml-tagset-perl_3.20-3_all.deb ...
Unpacking libhtml-tagset-perl (3.20-3) ...
Selecting previously unselected package liburi-perl.
Preparing to unpack .../07-liburi-perl_1.73-1_all.deb ...
Unpacking liburi-perl (1.73-1) ...
Selecting previously unselected package libhtml-parser-perl.
Preparing to unpack .../08-libhtml-parser-perl_3.72-3build1_amd64.deb ...
Unpacking libhtml-parser-perl (3.72-3build1) ...
Selecting previously unselected package libcgi-pm-perl.
Preparing to unpack .../09-libcgi-pm-perl_4.38-1_all.deb ...
Unpacking libcgi-pm-perl (4.38-1) ...
Selecting previously unselected package libfcgi-perl.
Preparing to unpack .../10-libfcgi-perl_0.78-2build1_amd64.deb ...
Unpacking libfcgi-perl (0.78-2build1) ...
Selecting previously unselected package libcgi-fast-perl.
Preparing to unpack .../11-libcgi-fast-perl_1%3a2.13-1_all.deb ...
Unpacking libcgi-fast-perl (1:2.13-1) ...
Selecting previously unselected package libencode-locale-perl.
Preparing to unpack .../12-libencode-locale-perl_1.05-1_all.deb ...
Unpacking libencode-locale-perl (1.05-1) ...
Selecting previously unselected package libhtml-template-perl.
Preparing to unpack .../13-libhtml-template-perl_2.97-1_all.deb ...
Unpacking libhtml-template-perl (2.97-1) ...
Selecting previously unselected package libtimedate-perl.
Preparing to unpack .../14-libtimedate-perl_2.3000-2_all.deb ...
Unpacking libtimedate-perl (2.3000-2) ...
Selecting previously unselected package libhttp-date-perl.
Preparing to unpack .../15-libhttp-date-perl_6.02-1_all.deb ...
Unpacking libhttp-date-perl (6.02-1) ...
Selecting previously unselected package libio-html-perl.
Preparing to unpack .../16-libio-html-perl_1.001-1_all.deb ...
Unpacking libio-html-perl (1.001-1) ...
Selecting previously unselected package liblwp-mediatypes-perl.
Preparing to unpack .../17-liblwp-mediatypes-perl_6.02-1_all.deb ...
Unpacking liblwp-mediatypes-perl (6.02-1) ...
Selecting previously unselected package libhttp-message-perl.
Preparing to unpack .../18-libhttp-message-perl_6.14-1_all.deb ...
Unpacking libhttp-message-perl (6.14-1) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../19-mysql-server_5.7.30-0ubuntu0.18.04.1_all.deb ...
Unpacking mysql-server (5.7.30-0ubuntu0.18.04.1) ...
Setting up libhtml-tagset-perl (3.20-3) ...
Setting up libevent-core-2.1-6:amd64 (2.1.8-stable-4build1) ...
Setting up psmisc (23.1-1ubuntu0.1) ...
Setting up libencode-locale-perl (1.05-1) ...
Setting up mysql-server-core-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Setting up libtimedate-perl (2.3000-2) ...
Setting up libio-html-perl (1.001-1) ...
Setting up liblwp-mediatypes-perl (6.02-1) ...
Setting up liburi-perl (1.73-1) ...
Setting up libhtml-parser-perl (3.72-3build1) ...
Setting up libcgi-pm-perl (4.38-1) ...
Setting up mysql-client-core-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Setting up libfcgi-perl (0.78-2build1) ...
Setting up libhttp-date-perl (6.02-1) ...
Setting up libhtml-template-perl (2.97-1) ...
Setting up libcgi-fast-perl (1:2.13-1) ...
Setting up libhttp-message-perl (6.14-1) ...
Setting up mysql-client-5.7 (5.7.30-0ubuntu0.18.04.1) ...
Setting up mysql-server-5.7 (5.7.30-0ubuntu0.18.04.1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of stop.
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up mysql-server (5.7.30-0ubuntu0.18.04.1) ...
Processing triggers for systemd (237-3ubuntu10.41) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link

 * Starting MySQL database server mysqld
No directory, logging in with HOME=/
   ...done.

Chapter 12: Python and Databases


In [3]:
!pip install PyMySQL


Collecting PyMySQL
  Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
     |████████████████████████████████| 51kB 2.4MB/s 
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.9.3

In [0]:
import pymysql
db = pymysql.connect(host="localhost", user="root", passwd="mypassword", db="PythonU")

In [0]:
cursor = db.cursor()

In [6]:
cursor.execute("SELECT * FROM Students")


Out[6]:
5

In [7]:
cursor.fetchone()


Out[7]:
(1, 'Harry', 'Wilkinson', datetime.date(2016, 2, 10), 0)

In [8]:
cursor.fetchone()


Out[8]:
(2, 'Jonathan', 'Hunt', datetime.date(2014, 2, 16), 0)

In [9]:
cursor.fetchone()


Out[9]:
(3, 'Harry', 'Hughes', datetime.date(2015, 3, 20), 0)

In [10]:
cursor.fetchall()


Out[10]:
((4, 'Kayla', 'Allen', datetime.date(2016, 3, 15), 1),
 (5, 'Virginia', 'Gonzalez', datetime.date(2017, 4, 2), 0))

Listing 12.1: pymysql1.py: Reading results once at a time


In [11]:
!/etc/init.d/mysql stop


 * Stopping MySQL database server mysqld
   ...done.

In [0]:
get_ipython().system_raw('mysqld_safe --skip-grant-tables &')

In [0]:
!mysql -e "UPDATE mysql.user SET authentication_string=password('secret'),host='%',plugin='mysql_native_password' WHERE user='root';flush privileges;"

In [14]:
import pymysql
db = pymysql.connect(host='localhost',
    user='root', passwd='secret', db='PythonU')
cursor = db.cursor()
recs = cursor.execute('SELECT * FROM Students')
for x in range(recs):
    print(cursor.fetchone())


(1, 'Harry', 'Wilkinson', datetime.date(2016, 2, 10), 0)
(2, 'Jonathan', 'Hunt', datetime.date(2014, 2, 16), 0)
(3, 'Harry', 'Hughes', datetime.date(2015, 3, 20), 0)
(4, 'Kayla', 'Allen', datetime.date(2016, 3, 15), 1)
(5, 'Virginia', 'Gonzalez', datetime.date(2017, 4, 2), 0)

Listing 12.2: pymysql2.py: Iterating directly over the DB cursor


In [15]:
import pymysql
db = pymysql.connect(host='localhost',
    user='root', passwd='secret', db='PythonU')
cursor = db.cursor()
cursor.execute('SELECT * FROM Students')
for row in cursor:
    print(row)


(1, 'Harry', 'Wilkinson', datetime.date(2016, 2, 10), 0)
(2, 'Jonathan', 'Hunt', datetime.date(2014, 2, 16), 0)
(3, 'Harry', 'Hughes', datetime.date(2015, 3, 20), 0)
(4, 'Kayla', 'Allen', datetime.date(2016, 3, 15), 1)
(5, 'Virginia', 'Gonzalez', datetime.date(2017, 4, 2), 0)

Listing 12.3: sqlite1.py: Same script as 12.2, but with SQLite


In [16]:
import sqlite3
db = sqlite3.connect('samples/PythonU.db')
cursor = db.cursor()
cursor.execute('Select * from Students')
for row in cursor:
    print(row)


(1, 'Harry', 'Wilkinson', '2016-02-10', 0)
(2, 'Jonathan', 'Hunt', '2014-02-16', 0)
(3, 'Harry', 'Hughes', '2015-03-20', 0)
(4, 'Kayla', 'Allen', '2016-03-15', 1)
(5, 'Virginia', 'Gonzalez', '2003-04-02', 0)

In [17]:
!apt install mongodb


Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpcap0.8 libstemmer0d libyaml-cpp0.5v5 mongo-tools mongodb-clients
  mongodb-server mongodb-server-core
The following NEW packages will be installed:
  libpcap0.8 libstemmer0d libyaml-cpp0.5v5 mongo-tools mongodb mongodb-clients
  mongodb-server mongodb-server-core
0 upgraded, 8 newly installed, 0 to remove and 32 not upgraded.
Need to get 53.1 MB of archives.
After this operation, 215 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpcap0.8 amd64 1.8.1-6ubuntu1.18.04.1 [118 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 libstemmer0d amd64 0+svn585-1build1 [62.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libyaml-cpp0.5v5 amd64 0.5.2-4ubuntu1 [150 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/universe amd64 mongo-tools amd64 3.6.3-0ubuntu1 [12.3 MB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mongodb-clients amd64 1:3.6.3-0ubuntu1.1 [20.2 MB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mongodb-server-core amd64 1:3.6.3-0ubuntu1.1 [20.3 MB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mongodb-server all 1:3.6.3-0ubuntu1.1 [12.6 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mongodb amd64 1:3.6.3-0ubuntu1.1 [9,968 B]
Fetched 53.1 MB in 4s (13.3 MB/s)
Selecting previously unselected package libpcap0.8:amd64.
(Reading database ... 144996 files and directories currently installed.)
Preparing to unpack .../0-libpcap0.8_1.8.1-6ubuntu1.18.04.1_amd64.deb ...
Unpacking libpcap0.8:amd64 (1.8.1-6ubuntu1.18.04.1) ...
Selecting previously unselected package libstemmer0d:amd64.
Preparing to unpack .../1-libstemmer0d_0+svn585-1build1_amd64.deb ...
Unpacking libstemmer0d:amd64 (0+svn585-1build1) ...
Selecting previously unselected package libyaml-cpp0.5v5:amd64.
Preparing to unpack .../2-libyaml-cpp0.5v5_0.5.2-4ubuntu1_amd64.deb ...
Unpacking libyaml-cpp0.5v5:amd64 (0.5.2-4ubuntu1) ...
Selecting previously unselected package mongo-tools.
Preparing to unpack .../3-mongo-tools_3.6.3-0ubuntu1_amd64.deb ...
Unpacking mongo-tools (3.6.3-0ubuntu1) ...
Selecting previously unselected package mongodb-clients.
Preparing to unpack .../4-mongodb-clients_1%3a3.6.3-0ubuntu1.1_amd64.deb ...
Unpacking mongodb-clients (1:3.6.3-0ubuntu1.1) ...
Selecting previously unselected package mongodb-server-core.
Preparing to unpack .../5-mongodb-server-core_1%3a3.6.3-0ubuntu1.1_amd64.deb ...
Unpacking mongodb-server-core (1:3.6.3-0ubuntu1.1) ...
Selecting previously unselected package mongodb-server.
Preparing to unpack .../6-mongodb-server_1%3a3.6.3-0ubuntu1.1_all.deb ...
Unpacking mongodb-server (1:3.6.3-0ubuntu1.1) ...
Selecting previously unselected package mongodb.
Preparing to unpack .../7-mongodb_1%3a3.6.3-0ubuntu1.1_amd64.deb ...
Unpacking mongodb (1:3.6.3-0ubuntu1.1) ...
Setting up libstemmer0d:amd64 (0+svn585-1build1) ...
Setting up libyaml-cpp0.5v5:amd64 (0.5.2-4ubuntu1) ...
Setting up mongodb-server-core (1:3.6.3-0ubuntu1.1) ...
Setting up libpcap0.8:amd64 (1.8.1-6ubuntu1.18.04.1) ...
Setting up mongodb-clients (1:3.6.3-0ubuntu1.1) ...
Setting up mongodb-server (1:3.6.3-0ubuntu1.1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Created symlink /etc/systemd/system/multi-user.target.wants/mongodb.service → /lib/systemd/system/mongodb.service.
Setting up mongo-tools (3.6.3-0ubuntu1) ...
Setting up mongodb (1:3.6.3-0ubuntu1.1) ...
Processing triggers for systemd (237-3ubuntu10.41) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link


In [18]:
!/etc/init.d/mongodb start


 * Starting database mongodb
   ...done.

In [0]:
from pymongo import MongoClient

In [0]:
from pymongo import MongoClient
client = MongoClient('localhost:27017')

In [21]:
client.list_database_names()


Out[21]:
['admin', 'local']

In [0]:
db = client.PythonU

In [23]:
client.list_database_names()


Out[23]:
['admin', 'local']

In [0]:
client.drop_database('Employee')

In [0]:
students = db.Students

In [0]:
student_1 = {'Name':'Harry', 'LastName':'Wilkinson',
'DateJoined':'2016-02-10', 'OutstandingBalance':False,
'Courses':[('Python 101', 7, '2016/1'), ('Mathematics for CS',
8, '2016/1')]}

In [0]:
student_2 = {'Name':'Jonathan', 'LastName':'Hunt',
'DateJoined':'2014-02-16', 'OutstandingBalance':False,
'Courses':[('Python 101', 6, '2016/1'), ('Mathematics for CS',
9, '2015/2')]}

In [28]:
students.count()


/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: count is deprecated. Use estimated_document_count or count_documents instead. Please note that $where must be replaced by $expr, $near must be replaced by $geoWithin with $center, and $nearSphere must be replaced by $geoWithin with $centerSphere
  """Entry point for launching an IPython kernel.
Out[28]:
0

In [29]:
students.insert(student_1)


/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
  """Entry point for launching an IPython kernel.
Out[29]:
ObjectId('5ed902d980378228f849a40d')

In [30]:
students.insert(student_2)


/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead.
  """Entry point for launching an IPython kernel.
Out[30]:
ObjectId('5ed902db80378228f849a40e')

In [31]:
students.count()


/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: count is deprecated. Use estimated_document_count or count_documents instead. Please note that $where must be replaced by $expr, $near must be replaced by $geoWithin with $center, and $nearSphere must be replaced by $geoWithin with $centerSphere
  """Entry point for launching an IPython kernel.
Out[31]:
2

In [32]:
from bson.objectid import ObjectId
search_id = {'_id':ObjectId('5ed902d980378228f849a40d')}
my_student = students.find_one(search_id)
my_student['LastName']


Out[32]:
'Wilkinson'

In [33]:
my_student['_id'].generation_time


Out[33]:
datetime.datetime(2020, 6, 4, 14, 19, 5, tzinfo=<bson.tz_util.FixedOffset object at 0x7f338406fe80>)

In [34]:
for student in students.find():
    print(student['Name'], student['LastName'])


Harry Wilkinson
Jonathan Hunt

In [35]:
list(students.find())


Out[35]:
[{'Courses': [['Python 101', 7, '2016/1'],
   ['Mathematics for CS', 8, '2016/1']],
  'DateJoined': '2016-02-10',
  'LastName': 'Wilkinson',
  'Name': 'Harry',
  'OutstandingBalance': False,
  '_id': ObjectId('5ed902d980378228f849a40d')},
 {'Courses': [['Python 101', 6, '2016/1'],
   ['Mathematics for CS', 9, '2015/2']],
  'DateJoined': '2014-02-16',
  'LastName': 'Hunt',
  'Name': 'Jonathan',
  'OutstandingBalance': False,
  '_id': ObjectId('5ed902db80378228f849a40e')}]

In [0]: