Note de programmation Python

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 $@ 

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

Note de programmation Python
Remarque: Python
Note de Python
Note d'apprentissage Python_002
Remarque: décorateur Python
[Python] Note d'apprentissage 1
Note d'apprentissage Python_004
Programmation avec Python
Note d'apprentissage Python_003
[Remarque] openCV + python
Note du débutant Python
Note récapitulative sur la programmation d'apprentissage automatique Python (Jupyter)
[Note] Début de la programmation
[Note] future déclaration ~ Python ~
[Note] Lecture de fichier ~ Python ~
3. 3. Programmation IA avec Python
Programmation Python avec Atom
Programmation compétitive avec python
Remarque pour faire de python un démon
Remarque: python Skeleton Nya
Programmation Python avec Excel
LEGO Mindstorms 51515 Programmation Python
Note d'introduction Python Tkinter
Programmation avec Python Flask
Atelier de programmation Python - Super introduction Vol.3
Note
Concours de programmation Atcoder Acing Python
Python
Note d'entrée Python dans AtCoder
[Note] Faites fonctionner MongoDB avec Python
Note
Résumé de l'article sur la programmation Web Python
Paiza Python Primer 1 Apprendre la programmation
Note
[WIP] Notes d'étude Fluent Python
Programmation Python Machine Learning> Mots-clés
Atelier de programmation Python - Super introduction Vol.4
Notes depuis 3 mois depuis que j'ai commencé Python
[AtCoder] Note personnelle ABC165C [Python]
Une introduction à la programmation Python
Programmation réseau avec Python Scapy
Notez qu'il prend en charge Python 3
Une note sur [python] __debug__
[Swift / Ruby / Python / Java] Programmation orientée objet
Entrée standard Python3 pour une programmation compétitive
Programmation GUI en Python avec Appjar
Programmation fonctionnelle dans Python Project Euler 1
Remarque Python: à propos de la comparaison en utilisant is
[Note] Projet Euler en Python (problème 1-22)
[Introduction à Python3 Jour 1] Programmation et Python