Thursday, June 7, 2018

What list operations are possible in Python 3.7?

I am using Python 3.7 on a Windows 10 machine.

You can define a list as shown.


ListMethods_0

You can reverse a list

ListMethods_1

Index of a list element. It is zero based.

ListMethods_2

You can add element to a list using append()


ListMethods_3.png

You can sort the list using sort()


ListMethods_4.png

You can find the argument limits of list index as shown here. List can have very large  number of elements indeed.



ListMethods_5.png

You can remove element from list


ListMethods_6.png

Beware of case sensitiveness.


How do you connect to SQL Server using pyodbc?

pyodbc is an open source Python module that simplifies accessing ODBC databases. It implements the DB API 2.0 specs and has been filled with Python related items.

Connecting to SQL Server (SQL Server 2016 Sp2) is easy as long as you get the needed items correctly. Review the foolowing link if you landed on this page:

http://hodentekhelp.blogspot.com/2018/06/how-do-you-install-pyodbc.html
I assume you have installed:


  • pyodbc
  • Microsoft ODBC Driver for SQL Server


as described in the above link.

Now you need to create a DSN using the Microsoft ODBC which you installed.

Launch Microsoft ODBC Data Source Adminsitrator (x64) from search charm:



MicrosoftAdmin.png
We will create a User DSN. In the User DSN tabbed page shown click Add....






odbcAdmin1.png


In the Create New Data Source page choose ODBC Driver 17 for SQL Server.


odbcAdmin2.png

Click Finish.

In the Create a New Data Souce to SQL Server fill in the orginally empty boxes as shown (Of course you have to use the server details you have. For server the drop-down may or may not dsiplay your servers in which case you need to fill it in.


odbcAdmin3.png

Click Next> after filling up the page as shown above.
In the page that gets displayed as shown agree with the defaults.


odbcAdmin4.png


Click Next>.

Place check mark for Chnage the default database to: and using the drop-down pick a database (herein Northwind) on your server instance as shown.


odbcAdmin5.png

Accept other defaults and click Next>





odbcAdmin6.png
Click Finish.

The ODBC Microsoft SQL Server Setup page gets dispalyed ssummarizing the choices you made. Not that Mars is not supported.



odbcAdmin7.png

Click Test Data Source...

SQL Server ODBC Data Source Test
results page is displayed.


odbcAdmin8.png

Connecting to Northwind database on the server instnace Hodentek9\OHANA using pyodbc

Launch Python Shell and type in the following statements that connects to SQL Server. You create a cursor and fetch using SQL query in cursor.execute().


pyodbcConnect

Tuesday, June 5, 2018

How do you install pyodbc?

pyodbc is an open source Python module that simplifies accessing ODBC databases. It implements the DB API 2.0 specs and has been filled with Python related items.

It can be installed with pip, with this simple statement:

pip install pyodbc

The following link provides the steps to install and configure pyodbc on Windows. The versions I have used are not the same ones used in the above link as I faced difficulties following the items.

I used Python 3.7 which was described in my previous post:
https://hodentekmsss.blogspot.com/2018/03/you-need-python-to-install-command-line.html

I followed this with installing Microsoft ODBC Driver from here:
https://docs.microsoft.com/en-us/sql/connect/sql-connection-libraries?view=sql-server-2017#anchor-20-drivers-relational-access

You must download the ODBC for working with Python in the above page.

Python drivers.png

Download the ones you want, herein the (x64)


MSODBC17.png

Double clicking the downloaded msi you can install Microsoft ODBC driver as shown here:


MSODBC17_1


Make sure you highlight the ODBC Driver for SQL Server before you click Next> on the screen.
I
t is successfully installed and gets enterer into the Drives' list in ODBC Data Source Administrator (x64) as shown.


MSODBC17_2


Now you install pyodbc using pip. I found the pip used for this post here in the Scripts folder:

C:\Users\Owner\AppData\Local\Programs\Python\Python37\Scripts

Open up a command screen in Windows and used the command as shown here:


installpyodbc.png

pyodbc gets installed here:

C:\Users\Owner\AppData\Local\Programs\Python\Python35\Lib\site-packages\









Sunday, June 3, 2018

Did Microsoft buy GitHub?

Two days ago it was a rumor but according to a post on Mashable (https://mashable.com/2018/06/03/microsoft-github-acquistion/#7oGpfyPu4Oqt) it seems to have acquired GitHub to be announced on Monday. It is supposed to be valued at $2B but GitHub thinks it's true value exceeds the offer.

It was also on Bloomberg TV Channel.
https://www.bloomberg.com/news/articles/2018-06-03/microsoft-is-said-to-have-agreed-to-acquire-coding-site-github

How do you create an undirected graph in Python?

An undirected graph in discrete mathematical formulation is a structure in which some pairs of objects are in some sense related (Wikipedia). A line with two end points is a graph element with two nodes connected by an edge stretching between the two points.

Here are some examples of graphs:

 The above from Wikipedia. Graph with  6 nodes and 7 edges.
This one with 6 nodes and 8 edges.

In Python you can construct a dictionary that associates nodes with edges as shown here in this schematic.


In this graph 5 edges are associated with 6 edges and it can be represented by this dictionary object,

A is connected to B and C while A,B,D and E connects to C.

Review this post after the above:
http://hodentekhelp.blogspot.com/2018/06/how-do-you-construct-python-dictionary.html




Saturday, June 2, 2018

How do you connect to SQL Server from Python?

Well, like other software you need a Python SQL Driver. These drivers can be downloaded from here:

https://docs.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server?view=sql-server-2017

There are two drivers and Microsoft recommends using pyodbc.

pyodbc
pymssql


The one thing that bothers me is that the drivers are Python version dependent and there are so many of them. This is only for Windows that includes both x32bit and x64bit.

Go here to get the proper one:
https://www.python.org/downloads/windows/

I am on Windows 10 Pro Version 1803 (OS Build 17134.48). I also have several versions of SQL Servers. I will try out the web installer for x32 and x64 versions of Python3.7.0b5(64).

Friday, June 1, 2018

How do you construct a Python dictionary?

Python dictionary has features common to JSON values.


Python dictionary has keys and associated with keys there are lists.
{key:[values],key[values]..}

Here is an example:

graph={"A":["C","B"],
       "B":["A","C"],
       "C":["A","B","D","E"],
       "D":["C","E"],
       "E":["C","D"]
      }


Defining a dictionary in Python.


Dict_0


In Python you can do the following to access keys and associated lists.

You can find the keys as show here:


Dict_1

You can gather all of the items in dictionary using the following:

Dict_2

You can list out the values only as shown here:


Dict_3

You can also access the lists by looping as in the following:


Dict_4


More in my next...