Python programming note

urllib (python3) ignore redirect Via class

import urllib.request

class HTTPNoRedirectHandler(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        return None
opener = urllib.request.build_opener(HTTPNoRedirectHandler)
opener.open('http://google.com')

HTTPError: HTTP Error 301: Moved Permanently

Via function overload

HTTPNoRedirectHandler=urllib.request.HTTPRedirectHandler()
HTTPNoRedirectHandler.redirect_request=lambda *args: None
opener = urllib.request.build_opener(HTTPNoRedirectHandler)
opener.open('http://google.com')

Global scope Why CAN NOT modify gloval vals ? @ python3 Read OK but Write NG.

GLOBAL = "HELLO"

def main():
    # GLOBAL = GLOBAL + " WORLD"
    print(GLOBAL)
    pass

if __name__ == '__main__':
    main()

# -------
# python a.py
HELLO

We can read global val on function-socpes! but...

GLOBAL = "HELLO"

def main():
    GLOBAL = GLOBAL + " WORLD"
    print(GLOBAL)
    pass

if __name__ == '__main__':
    main()

# ----
# python a.py
Traceback (most recent call last):
  File "a.py", line 11, in <module>
    main()
  File "a.py", line 6, in main
    GLOBAL = GLOBAL + " WORLD"
UnboundLocalError: local variable 'GLOBAL' referenced before assignment

local variable???

You must set global keyward in user-function before modify it.

GLOBAL = "HELLO"

def main():
    global GLOBAL
    GLOBAL = GLOBAL + " WORLD"
    print(GLOBAL)
    pass

if __name__ == '__main__':
    main()

gzip stream Compress

python


from gzip import GzipFile
from StringIO import StringIO
io = StringIO()
with GzipFile(fileobj=io, mode='wb') as f:
    f.write('hogefuga')

s = io.getvalue()

decode b64 + ungzip

from gzip import GzipFile
from StringIO import StringIO
import base64

def undec64gz(s):
    io = StringIO()
    io.write(
        base64.b64decode(s))
    io.seek(0)
    with GzipFile(fileobj=io, mode='rb') as f:
        return f.read()

Control DATE-TIME now() - 1min

python


import pytz
import datetime

datetime.datetime.now(pytz.utc) - datetime.timedelta(minutes=1)

set (seconds|microseconds) to 0

python


datetime.datetime.now(pytz.utc).replace(second=0, microsecond=0)

format (ISO8601 | RFC3339)

python


datetime.datetime.now(pytz.utc).isoformat()

# '2016-12-03T02:02:58.073285+00:00'

get_localtz & convert to UTC tzlocal module pip install tzlocal

python


from datetime import datetime
import pytz
from tzlocal import get_localzone
tz = get_localzone()

d = datetime.strptime("2017-05-01T14:00:00", "%Y-%m-%dT%H:%M:%S")
# add tzinfo only
jd =tz.localize(d)
# convert to UTC
zd = jd.astimezone(pytz.timezone('UTC'))
zd.isoformat()

oauth2client failed by [Permission denied], but root can run it. why? http://stackoverflow.com/questions/27870024/google-gmail-api-installed-app-shows-ioerror-13-from-module-ssl-py-w-o-sudo

The httplib2 installer sets incorrect permissions for its httplib2/cacerts.txt file. One solution is to simply make its files readable by anyone by running

Shell wrapper for virualenv (for cron or any non activate env) Just exec via use whith python under virtualenv-session

python


#!/bin/bash
/opt/venv/bin/python hogefuga.py [email protected] 

Return-code passthrough too.

urllib ... Recomend Requests, but..

python


import urllib
import urllib2

url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name': 'Michael Foord',
          'location': 'Northampton',
          'language': 'Python' }
headers = {'User-Agent': user_agent}

data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

list comprehension In Japanese, naihou-hyouki.

http://www.secnetix.de/olli/Python/list_comprehensions.hawk

No []ed json array to json

python


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


def main():
    isFirst = True
    print("[")
    for line in sys.stdin:
        if line == "":
            continue
        if not isFirst:
            print(",")
        print(line.rstrip(), end="")
        isFirst = False

    print("]")

if __name__ == '__main__':
    main()

print() for python 2.x

python


from __future__ import print_function

strip list

python


l = ['hogehoge']
(v,) = l
v
# hogehoge

l = ['hoge', 'fuga']
(v1, v2) = l
v1
# hoge
v2
# fuga

jmespath quote by `

python


import jmespath

machines = {
  "machines": [
    {"name": "a", "state": "running"},
    {"name": "b", "state": "stopped"},
    {"name": "b", "state": "running"}
  ]
}

jmespath.search('machines[?state==`running`].name', machines)
['a', 'b']

set

python


a = {'k1': {"hoge": 1}, 'k2': {"fuga": 2}, 'k5' : 'fura'}
b = {'k1': {"hoge": 1}, 'k3': {"fuga": 2}, 'k4': 'age'}
df = set(a.keys()) - set(b.keys())
# {'k2', 'k5'}
for d in df:
    print a[d]

Like tr command

import string
a = "hoge.co.jp"
new_a  = a.translate(string.maketrans(".","_"))
new_a  <== OK

a = u"hoge.co.jp"
new_a  = a.translate(string.maketrans(".","_"))  <== Error!
TypeError: character mapping must return integer, None or unicode

new_a  = a.translate({ord(".") : ord("_")})
new_a  <=== OK

Echo non ascii chars STDOUT ok, but redirect fail.

import sys
import codecs
if sys.version_info[0] == 2:
    sys.stdout = codecs.getwriter('utf8')(sys.stdout)

http://stackoverflow.com/questions/492483/setting-the-correct-encoding-when-piping-stdout-in-python For Python > 3 do not set this.

STDIN

python


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys


def main():
    for line in sys.stdin:
        print line,

or raw_input(). python3 input() = python2 raw_input(). raw_input() chomp newlines by auto.

print with no newlines , comma!

python


print "this should be",
print "on the same line"

Dictionary, sort by value Have many answers... http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value

python


    sorted_dic = sorted(dic, key=dic.get, reverse=True)
    for k in sorted_dic:
        print '%d\t%s' % (dic[k], k)

Format string with slice NO.... DO NOT support

Recommended Posts

Python programming note
Note: Python
Python note
Python study note_002
Note: Python Decorator
[Python] Learning Note 1
Python study note_004
Programming in python
Python study note_003
[Note] openCV + python
Python beginner's note
"Python Machine Learning Programming" Summary Note (Jupyter)
[Note] Beginning of programming
[Note] future sentence ~ Python ~
[Note] File reading ~ Python ~
3. 3. AI programming with Python
Competitive programming diary python 20201213
Python programming with Atom
Competitive programming diary python 20201220
Competitive programming with python
Note to daemonize python
Note: python Skeleton Nya
Python basic grammar note (4)
Python basic grammar note (3)
Python programming in Excel
LEGO Mindstorms 51515 Python Programming
[Python] Dynamic programming ABC015D
Python Tkinter Primer Note
Competitive programming diary python
Programming with Python Flask
Python Programming Workshop-Super Introductory Vol.3
Note
Python3 programming functions personal summary
Atcoder Acing Programming Contest Python
Python
[Python] Dynamic programming knapsack problem
[Python] Dynamic programming TDPC D
Python Input Note in AtCoder
[Note] Operate MongoDB with Python
Note
Python web programming article summary
Paiza Python Primer 1 Learn Programming
Note
Python Competitive Programming Site Summary
[WIP] Fluent Python Study Note
Python Machine Learning Programming> Keywords
Python Programming Workshop-Super Introductory Vol.4
3 months note for starting Python
[AtCoder] ABC165C Personal Note [Python]
An introduction to Python Programming
[Python] Dynamic programming TDPC A
Network programming with Python Scapy
Note that it supports Python 3
A note about [python] __debug__
[Swift / Ruby / Python / Java] Object-oriented programming
Python3 standard input for competitive programming
GUI programming in Python using Appjar
Functional programming in Python Project Euler 1
Python Note: About comparison using is
[Note] Project Euler in Python (Problem 1-22)
[Introduction to Python3 Day 1] Programming and Python