Python programing note

# urllib (python3)
## ignore redirect
Via class

```python
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

```python
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.

```python
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...

```python
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.

```python
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
```python
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
```pythoh:
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

```bash:
#!/bin/bash
/opt/venv/bin/python hogefuga.py $@ 
```
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
```python
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