Archive for category Python

PyCharm – The Python IDE crossing multiple platforms

Here is the declaration on the PyCharm site about it.

PyCharm is a dedicated Python and Django IDE providing a wide range of essential tools for Python developers, tightly integrated together to create a convenient environment for productive Python development and Web development.

It is available for Windows, macOS, and Linux. It can be downloaded from http://www.jetbrains.com/pycharm/download/.

I just start using it. I read some good comments about it from the internet.

Share

Tags: , ,

Conversion of list of dictionary and dataframe in Python

In Python there is a dataframe package: pandas. The dataframe package simplifies a lot of things and it is comparable to a dataframe in R. Sometime you need convert a list of dictionary to a dataframe and vice versa.

To use dataframe you need import pandas.

To convert a list of dictionary to a dataframe, use the following:

pandas.DataFrame(a_list_dict)

To convert a dataframe to a list of dictionary, use the following:

list(df.T.to_dict().values())

Share

Tags: , , ,

Use MySQL database in Python

PyMySQL its a pure python client library, you can install using pip install PyMySQL (find documentation at https://pymysql.readthedocs.io/en/latest/).

The following is a simple example to demonstrate how to use the library.

#!/usr/bin/env python
from __future__ import print_function

import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
print(cur.description)
print()
for row in cur:
    print(row)

cur.close()
conn.close()
Share

Tags: , ,

Use python to create PowerPoint slides automatically

I have a large number of images. I want to organize them in powerpoint files by automatically insert each picture to a new powerpoint slide and control it’s position and size. I came across a nice Python library – python-pptx at https://python-pptx.readthedocs.io/en/latest/. It provides functions to automate generating PowerPoint slides. Below are some information from the website. I keep the information for future use.

python-pptx is a Python library for creating and updating PowerPoint (.pptx) files.

A typical use would be generating a customized PowerPoint presentation from database content, downloadable by clicking a link in a web application. Several developers have used it to automate production of presentation-ready engineering status reports based on information held in their work management system. It could also be used for making bulk updates to a library of presentations or simply to automate the production of a slide or two that would be tedious to get right by hand.

Chris Moffitt wrote a nice article to explain how to use the above library at http://pbpython.com/creating-powerpoint.html. He specifically talked about how to use PowerPoint templates to generate new slides.

 

Share

Tags: , , , ,

Python development environment

I productive python development environment is important when you want to use Python to develop applications. Download the right version of Python from https://www.python.org/ and install it.

For example, I downloaded macOS version Python 3.6 and installed it to the machine. I found the software was installed at /Library/Frameworks/Python.framework/Versions/3.6. Relevant commands can be found in the bin folder. The most useful commands are pip3 and python3.

One Pyhton version 3.6 is installed, it can be used in any folder. Just type python3 in command line terminal. You can start interactively execute python statements. If necessary, you can use pip3 to install packages.

Share

Tags: , , , ,