Posts

Showing posts with the label python

Connect Oracle from Django 1.7 (Linux)

Hi, today I'm gonna create new Oracle's related Project with Django 1.7.  My setup is  Kubuntu 14.10 First thing I will have to have installed oracle-client. You should download these files below from oracle website oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-jdbc-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-odbc-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-tools-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm Then we would use 'alien', which is linux application that can convert rpm to deb. By doing this >>> sudo alien *.rpm Then use dpkg install them all >>> sudo dpkg -i *.deb Next , edit your bashrc file, by add export ORACLE_HOME=/usr/lib/oracle/12.1/client64 PATH=$PATH:$ORACLE_HOME/bin export LD_LIBRARY_PATH=$ORACLE_HOME/lib Create virtuaenv or pip to your env >>...

Setup environment for python developer in windows :)

Hi my friend :)     Sometime I may have a change to develop something in window, this is a guide to get python development up! Actually I just found powershell, interesting!!! http://www.tylerbutler.com/2012/05/how-to-install-python-pip-and-virtualenv-on-windows-with-powershell/ https://pip.pypa.io/en/latest/installing.html

โหลดไฟล์ด้วย urllib2

urllib2 module เป็นตัวที่ผมชอบและใช้อยู่เป็นประจำ นี่เป็น code ที่ผมเขียนเก็บไว้เพื่อใช้กับ code อื่นๆในการเขียน script ที่ใช้โหลดไฟล โดยมาจะใช้โหลดเว็ปการตูนต่างๆ import urllib2 def download(filename, img_url):     p = urllib2.urlopen(img_url)     with open(filename, 'w') as f:         f.write(p.read()) จิงๆแล้วบางเว็ปอาจจะห้าม user-agent ที่ไม่รู้จักโหลดไฟล ด้วยความสามารถของ urllib2 เราก็จะทำแบบนี้ได้ import urllib2 def download(filename, img_url):     req = urllib2.Request(img_url, headers={'User-Agent':'Mozilla/5.0'})     p = urllib2.urlopen(req)     with open(filename, 'w') as f:         f.write(p.read()) จะเห็นได้ว่าเราสามารถกำหนด header ได้เองและ img_url ก็แกะออกมาได้จาก beautifulsoup module นั่นเอง หึหึหึ

การใช้งาน beautifulsoup สำหรับ python

ใช้ beautifulsoup ทำอะไรได้     เราสามารถใช้โมดูลตัวนี้แกะ dom เพิ่มหรือลบ element ออกจาก dom ได้ ติดตั้ง beautifulsoup >> pip install beautifulsoup4 ลองเล่นกัน >> from bs4 import Beautifulsoup ผมจะลองโหลดหน้าเว็ปจาก google มาดู >> import urllib2 >> p = urllib2.urlopen('http://google.com') >> soup = Beautifulsoup(p) ลอง print ดู ก็จะได้เห้น content เต็มๆ >> print soup ถ้าทำแบบนี้ >> soup.find_all('table') เราก็จะได้ list ของ table ทั้งหมด ใน page นั้น ตัว soup นี้จะมี fn find_*** เยอะมาก อย่างเช่นถ้าเราทำแบบนี้ >> soup.find_all('table')[0] ผลที่ได้ก็จะเป็น element obj ที่เป็นของ bs นั่นเอง ซึ่งเราสามารถเอา attr ออกมาได้โดย >> t0 = soup.find_all('table')[0] >> print t0.attrs >> {'cellpadding': '0', 'cellspacing': '0'} โดยส่วนตัวผมใช้เจ้า bs (Beautifulsoup) ในการแกะเอา link ต่างเพื่อมาใช้โหลดอีกที เพราะว่าผลที่ออกมาจาก code ด้านบ...

Python module search order

The search order is important! 1.Program's working directory 2.$PYTHONPATH directories 3.Python standard library directories 4.(and any .pth path files) ref  http://www.curiousvenn.com/wp-content/uploads/2013/07/modules_101.pdf

Write a better python with Guide

I found this during i sit down in Lib of Burapha U. http://docs.python-guide.org/en/latest/ , it look like interesting guide and worth to read. :) thanks, hitchhiker

Recursive rename in python script

#!/usr/bin/env python import fnmatch import os root = os.getcwd() pattern = "*.JPG" for root, dirs, files in os.walk(root):     for filename in fnmatch.filter(files, pattern):         #print (os.path.join(root, filename))         full_src = os.path.join(root, filename)         full_dst = full_src.replace('.JPG','.jpg')         os.rename(full_src, full_dst)         print "Renaming {0} to {1}".format(full_src, full_dst)

Deal with mysql_config not found while installing mysql-python

sudo apt-get install mysql sudo apt-get install libmysqlclient-dev then pip install mysql-python ref http://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface

Python re module's stuff # 1

When you do something with aws, then you need only ip-address from something like this $ ec2-1-2-3-4.ap-southeast-1.compute.amazonaws.com I used to deal with it like split something then concat with something. Now I just found something cool in python's re module. >>> import re >>> ec2_ip = re.search(r'ec2-(\d+)-(\d+)-(\d+)-(\d+), ).groups() so I get >>> print ec2_ip '1.2.3.4' yaha, love this.

Python Advanced Ref

(thank  Gaël Varoquaux)  http://gael-varoquaux.info/computers/python_advanced/index.html

Remove PYC function in buildbot package

I play with buildbot around 4-5 days. Then I just found '''  buildbot.steps.python_twisted.RemovePYCs '''. There has content in the method is ''' find . -name "*.pyc" | xargs rm ''' which is interesting. :)

Buildbot master.cfg : issue#1

- Don't use WithProperties within SetProperty 

Using python-linkedin

After follow instruction from http://code.google.com/p/python-linkedin/ Then you have use the ACCESS_TOKEN that NOT the REQUEST_TOKEN

Purge queues with amqplib

Use amqplib from amqplib import client_0_8 as amqp # Create connection conn = amqp.Connection(     host="localhost:5672",     userid="xxxxx",     password="xxxxxxxxx"     virtual_host="/",     insist=False ) chan = conn.channel() # Get list of queues (list type) # # ex. list_queues = ['queue-1','queue2',....] # for queue in list_queues:     chan.queue_purge(queue) Preparation Data ( thk juacompe )         sudo rabbitmqctl list_queues name > queues.py    inside vim let's do        $s/^/"/  # for insert " infront of queue name       $s/$/",/ # for insert ", infront of queue name       fill queue=["xxxx for the first line       then you will get something like this       ----------------------------       queues_list = ["xxxxx",       "xxxxx", ...

Prepare for python development on Mac

Here's instruction 1. Get the dmg file of python from www.python.org, then install it. 2. Get the dmg file of setuptool from pypi, then install it. 3. Follow step from http://blog.praveengollakota.com/47430655 (very thank to praveen)

Quote from The Kirit

" Python not just load module from import but do execute that module so becareful about import order " ... The Kirit

Setup Python/DJango Environment with Virtualenv

for Linux environment.( general purpose ) - sudo apt-get install python-setuptools - sudo easy_install pip - pip install virtualenv (additional pip install virtualenvwrapper) - virtualenv --no-site-packages - source /bin/activate - pip install yolk - pip install ipython ( additional for other module please do - sudo apt-get install python-dev build-essential )

Way to GO

Today, I create new inspiration with my gf. That we should do something to make people realize what we can do. So I decide to - get my code when I implemented data mining related code for my friend. - improve that code to make it more efficient. - write blog about how to matplotlib and scientific tools.

Secret of Vim #1

In visual mode. If you use V (visual mode) to select multiline, then press : prompt will appear >>>:' So if you need to comment or insert something at first place please do >>>:' s/^/#/ for example above , I need to comment selected multiline. If you want some change with // , then we have to change delimeter, so >>>:' s!^!//! Hahaha, Cool , ha?

New Python Expression (for me) :P

1. >>> if 2 2. >>> var1 = var2 if condition true else var 3 It means > IF condition true: var1 = var2 ELSE: var1 = var3 oh man , just like ruby syntax or ruby like this :P