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.
A class that dumps MIDI (SMF) files. The basic part could be ported in about 4 hours without minor corrections. Python is amazing.
--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.
--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