Cheating from PHP to Python

I've written a lot of code in PHP's pear, but since I moved to composer I didn't really understand it, so I got fooled by Python. It's a good idea, so I will spell out what I felt while I was still feeling fresh.

study

A class that dumps MIDI (SMF) files. The basic part could be ported in about 4 hours without minor corrections. Python is amazing.

Porting tool

What I learned

--Unpack is different --The format string is different from PHP. (Is there a school?) --Use from the first return value. (From the second in PHP) --Do not use new. OK with a = Klass () --It is troublesome for print to add a line break by default and end = "" as the second argument. --sys.stdout.write does not have line breaks, and it is convenient to switch the sys.stdout part with fp. --Can be curried with print_ = partial (print, end = "") --It is troublesome and troublesome to receive self as the first argument of the class method. --It seems that starting a class method with _ makes it private. --The exception is raise Exception ("error wording"). You may inherit Exception. - http://docs.python.jp/2/library/exceptions.html#module-exceptions --PHP list ($ a, $ b) = $ arrayVal is convenient, but Python is easier with a, b = arrayVal --is_null There is no, but you can compare with == None, and there is has_key instead of isset (but of course be careful about handling null). -(Addition 2014/11/6) == You should use is None instead of None. -(Addition 2014/11/6) Let's use in instead of has_key --An error occurs when using pipes or redirects in Japanese conversion --Since the Python2 stdout is None (= ascii), specify it explicitly with the PYTHONIOENCODING environment variable.

 UnicodeEncodeError: 'ascii' codec can't encode character u'\u3048' in position 0: ordinal not in range(128)

--Use str.format instead of% for the string format. --str.format (a, b) instead of str% (a, b) --Normal associative arrays do not have an order unlike PHP, but you can use collections.OrderedDict to do so. --Note that only the order at the time of initialization is not maintained. It's okay to add a [key] = value later.

What I felt

--While (n--) isn't applied, so it's going to be a wasteful ghost. In the first place, n-- is useless. --for (..; ..; ..) cannot be done, so I feel that the loop condition becomes difficult to understand. ――Calling enumerate or items to turn an array or associative array with for seems to want syntactic sugar. ――You don't need; at the end of the line, but it's difficult to suppress because you enter it by hand. I'm used to not adding $. ――I don't really care about indentation. However, emacs's php-mode seems to raise else up one character, and it is troublesome to fix it at the time of porting. ――I can't use the switch statement, but I think it's better to use elif than to forget the break and get terrible. However, it is a little troublesome to port the place where the tech that controls break is not intentionally written.

By the way, I don't feel any discomfort anymore.

Recommended Posts

Cheating from PHP to Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Post from Python to Slack
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Call Matlab from Python to optimize
Create folders from '01' to '12' with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
How to access wikipedia from python
Python to switch from another language
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
Updated to Python 2.7.9
How to update Google Sheets from Python
Send a message from Python to Slack
Private Python handbook (updated from time to time)
I want to use jar from python
Convert from katakana to vowel kana [python]
Push notification from Python server to Android
Sum from 1 to 10
sql from python
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
MeCab from Python
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
"Backport" to python 2
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
How to open a web browser from python
Study from Python Hour7: How to use classes
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
From file to graph drawing in Python. Elementary elementary
I want to perform SageMaker inference from PHP
[Python] How to read data from CIFAR-10 and CIFAR-100
[python] Create table from pandas DataFrame to postgres
[Bash] Use here-documents to get python power from bash
How to generate a Python object from JSON
How to handle Linux commands well from Python
[Python] Flow from web scraping to data analysis
PHP and Python integration from scratch on Laravel
I want to use ceres solver from python
What I did when updating from Python 2.6 to 2.7
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
progate Python learning memo (updated from time to time)
From Python environment construction to virtual environment construction with anaconda
From easy git installation to docker startup python