Posts

Showing posts with the label python

Digital Marketing

Send email by Python Example

from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import COMMASPACE , formatdate def send_mail ( send_from , send_to , subject , text = " " , files = None , server = " smtp.office365.com " ): assert isinstance (send_to, list ) msg = MIMEMultipart() msg[ ' From ' ] = send_from msg[ ' To ' ] = COMMASPACE .join(send_to) msg[ ' Date ' ] = formatdate( localtime = True ) msg[ ' Subject ' ] = subject msg.attach(MIMEText(text)) for f in files or []: with open (f, " rb " ) as fil: part = MIMEApplication( fil.read(), Name = basename(f) ) # After the file is closed part[ ' Content-Disposition ' ] = ' attachment; filename=" %s " ' % basename(f) msg.attach(part) smtp = smtplib.SMTP(server, 587 ) smt...

Example of reading email from Gmail / Office 365 by IMAP using Python

import sys import imaplib import getpass import email import email.header import datetime EMAIL_ACCOUNT = " noreply@ goyun.info " EMAIL_FOLDER = " INBOX " def process_mailbox ( M ): """ Do something with emails messages in the folder. For the sake of this example, print some headers. """ rv, data = M.search( None , " ALL " ) if rv != ' OK ' : print " No messages found! " return for num in data[ 0 ].split(): rv, data = M.fetch(num, ' (RFC822) ' ) if rv != ' OK ' : print " ERROR getting message " , num return msg = email.message_from_string(data[ 0 ][ 1 ]) decode = email.header.decode_header(msg[ ' Subject ' ])[ 0 ] subject = unicode (decode[ 0 ], ' utf-8 ' ) print ' Message %s : %s ' % (num, subject) print ...

A simple python script to keep an eye on mysql process

The following codes are the same, they are just shown on different formats. Choose the one you like. ''' @author: it.goyun.info ''' import MySQLdb import time db = MySQLdb.connect(host= "192.168.1.19" , user= "i88ca" , passwd= " goyun.info " , db= " goyun.info " ) #create a cursor for the select cur = db.cursor() while ( True ): cur.execute( "show processlist;" ) for row in cur.fetchall() : # print row[0], row[1] print row[ 7 ] time.sleep( 8 ); # close the cursor cur.close() # close the connection db.close ()

Configuring Vim with a Vimrc file for Python

What is PEP 8?

PEP 8 is Python's style guide.  It's a set of rules for how to format your Python code to maximize its readability. Writing code to a specification helps to make large code bases, with lots of writers, more uniform and predictable, too.  PEP is actually an acronym that stands for Python Enhancement Proposal.

In Python, lambda expressions are utilized to construct anonymous functions.

To do so, you will use the lambda keyword (just as you use def to define normal functions). Every anonymous function you define in Python will have 3 essential parts: The lambda keyword. The parameters (or bound variables), and The function body. Example: adder = lambda x, y: x + y print (adder (8, 88)) $python3 main.py 96

What are generators in Python?

Generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.

Running psf/black to format Python file

There are other code formatters (autopep8, yapf) but the black style is now the recommendation of the Python Core Team. To use it, pip3 install black  # only required the first time black .

Python 3 - Number log() Method

Example to append to file in Python

import time with open ( "test.txt" , "a" ) as myfile: myfile. write (str(time.time() )+ '\n' )

How to solve the problem of "ImportError: No module named MySQLdb" in Python

You need to install mysql-python: yum install MySQL-python (Linux Fedora, CentOS, RedHat) apt-get install python-mysqldb (Linux Ubuntu) cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD) See also How to install mysql-python on Linux

How to solve the problem of "ImportError: No module named MySQLdb" in Python

You need to install mysql-python: yum install MySQL-python (Linux Fedora, CentOS, RedHat) apt-get install python-mysqldb (Linux Ubuntu) cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD) See also How to install mysql-python on Linux

How to install pip on CentOS

You can install python-pip by yum python-pip.noarch : A tool for installing and managing Python packages $ sudo yum -y install python-pip Or you can install python-setuptools python-setuptools.noarch : Easily build and distribute Python packages $ sudo yum -y install python-setuptools

How to fix: TypeError: not all arguments converted during string formatting in Python MySQLdb

If you encounter something similar to the following: Traceback (most recent call last):   File "scotia.failed.email.py", line 13, in <module>     cursor.execute (' select * from fail f where f.emailid= (%s) ', (emailid))   File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 187, in execute     query = query % tuple([db.literal(item) for item in args]) TypeError: not all arguments converted during string formatting: # Instead of: cursor . execute ( ' select * from fail f where f.emailid= (%s) ' , (emailid)) # Use: cursor . execute ( ' select * from fail f where f.emailid= (%s) ' , [emailid]) # Another example: count = cursor . execute ( ' select * from fail f join contacts c on f.contactid=c.contactid where f.emailid= (%s) and c.email = (%s) ' , [emailid,email]) print count

How to solve Python : name 'math' is not defined Error

import math

How to install crcmod

CentOS, RHEL, and Fedora   ------------------------   To compile and install crcmod:     sudo yum install gcc python-devel python-setuptools     sudo easy_install -U pip     sudo pip uninstall crcmod     sudo pip install -U crcmod   Debian and Ubuntu   -----------------   To compile and install crcmod:     sudo apt-get install gcc python-dev python-setuptools     sudo easy_install -U pip     sudo pip uninstall crcmod     sudo pip install -U crcmod   Mac OS X   --------   gsutil distributes a pre-compiled version of crcmod for OS X, so you shouldn't   need to compile and install it yourself. If for some reason the pre-compiled   version is not being detected, please let the Google Cloud Storage team know   (see "gsutil help support").   To compile manually on OS X, you will first need to install   `XCode <https://developer.apple.com/xcode/...

How to install MySQLdb on Linux

MySQLdb is an interface to the popular MySQL database server for Python. To install MySQLdb, just install mysql-python on Linux

How to install MySQLdb on Linux

MySQLdb is an interface to the popular MySQL database server for Python. To install MySQLdb, just install mysql-python on Linux

Python CSV example

# !/usr/bin/python # -*- coding: utf-8 -*- import csv with open ( ' data.csv ' , ' rb ' ) as f: reader = csv.reader(f) for row in reader: print row # writing to csv file with open ( ' data.csv ' , ' wb ' ) as f: writer = csv.writer(f) writer.writerow([ ' name ' , ' address ' , ' age ' ]) # single line data = [( ' xiaoming ' , ' china ' , ' 10 ' ), ( ' Lily ' , ' USA ' , ' 12 ' )] writer.writerows(data) # multiple lines

Vert.x - a tool-kit for building reactive applications on the JVM

Eclipse Vert.x is a tool-kit for building reactive applications on the JVM. Vert.x  is a lightweight, high performance application platform for the JVM that's designed for modern mobile, web, and enterprise applications Polyglot Write your application components in Java, JavaScript, CoffeeScript, Ruby, Python or Groovy... ... or mix and match several programming languages in a single app. Simplicity ...without being simplistic. Simple, powerful, APIs enable you to write non-blocking network enabled applications with ease. No complex configuration or boilerplate required. Scalability Scales using messaging passing to efficiently utilise your server cores. Uses non blocking I/O to serve many connections with minimal threads. Concurrency Simple actor-like concurrency model frees you from the pitfalls of traditional multi-threaded programming.