datetime format in Python / JavaScript

Python

I recommend dateutil.

import datetime
import dateutil

>>> dateutil.parser.parse('2014-12-24T16:15:16')
datetime.datetime(2014, 12, 24, 16, 15, 16)

>>> datetime_obj = datetime.datetime.strptime('2014-12-24T16:15:16', '%Y-%m-%dT%H:%M:%S')
datetime.datetime(2014, 12, 24, 16, 15, 16)

>>> datetime_obj = datetime.datetime.strptime('201408282300', '%Y%m%d%H%M')
datetime.datetime(2014, 8, 28, 23, 0)

>>> datetime_obj.strftime('%Y-%m-%d %H:%M')

strftime >> datetime -> str
strptime >> str --> datetime

ref:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Django Template

class DriverInfoForm(forms.ModelForm):
    service_time_start = forms.TimeField(
        widget=forms.TimeInput(format='%H:%M'),
        input_formats=['%H:%M', ]
    )
日期:{{ withdraw.presented_at|date:"%Y 年 %n 月" }}
聯絡時間:{{ driver.service_time_start|date:"H:i" }} - {{ driver.service_time_end|date:"H:i" }}

要注意的是
Django 似乎不能 parse AM / PM
所以儘量用 24 小時制

ref:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date

JavaScript

moment("201408292300", "YYYYMMDDHHmm");

Write to files in Python

因為寫入資料到 disk 很慢
比較好的方式是累積一些資料後再一次寫入(所謂的 buffer)
Python 的 open('/path/to/file/') 預設就有 buffer 了(第三個參數就是 buffering)
無論是 read, write 或 append

The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.

In [1]: import io
In [2]: io.DEFAULT_BUFFER_SIZE
Out[2]: 8192
from django.core.management.base import NoArgsCommand

from music.models import Song


class Command(NoArgsCommand):

    def handle_noargs(self, **options):
        start = time.time()

        total_lines = 0
        with open('dump_song_data.txt', 'w') as f:
            # header_line = 'user id, song id\n'
            # f.write(header_line)

            songs = Song.objects.filter(play_count__gte=10000)
            for song in songs.iterator():
                line = '%s\n' % (song.id)
                f.write(line)

                total_lines += 1

        end = time.time()

        print(total_lines)
        print(end - start)

ref:
https://docs.python.org/2/library/functions.html#open
http://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file
http://stackoverflow.com/questions/16669428/process-very-large-20gb-text-file-line-by-line
http://stackoverflow.com/questions/1896674/python-how-to-read-huge-text-file-into-memory