LoginSignup
0
3

More than 5 years have passed since last update.

Python > datetime > 実行時引数 > すぐに処理を始める

Last updated at Posted at 2017-08-25
動作環境
Raspberry Pi 2 Model B (以下RPi)
Raspbian Jessie
Python 2.7.9

Python > datetime > 開始時分秒まで待つにて待機処理を実装した。

一方で、ある条件においては「すぐに始めたい」場合もあるだろう。

実行時引数をチェックする処理を実装した。

import用

util_arg_no_wait_170825.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse

# PEP8


def hasNoWaitArgument():
    parser = argparse.ArgumentParser(description="wait mode")

    parser.add_argument(
        '-n',
        '--noWaitStart',
        dest='noWaitStart',
        action='store_true',
        help='no wait start')

    cmd_args = parser.parse_args()
    return cmd_args.noWaitStart


if __name__ == '__main__':
    if not hasNoWaitArgument():
        print('waiting')
        # do some wait
    print('started')

help

$ python util_arg_no_wait_170825.py -h
usage: util_arg_no_wait_170825.py [-h] [-n]

wait mode

optional arguments:
  -h, --help         show this help message and exit
  -n, --noWaitStart  no wait start

waitあり実行例

$ python util_arg_no_wait_170825.py 
waiting
started

waitなし実行例

$ python util_arg_no_wait_170825.py -n
started

使用例

http://qiita.com/7of9/items/0ec6c6b830409dfc0441
のutil_wait_170825.py (v0.2)も使用した場合。

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import serial
import datetime
import util_wait_170825 as wai
import util_arg_no_wait_170825 as arg_nw

#=== { Configuration
#start time
ST_HH, ST_MM, ST_SS = 16, 4, 0
#=== }


if not arg_nw.hasNoWaitArgument():
    print('waiting')
    wai.wait_until(ST_HH, ST_MM, ST_SS)

# 何かの処理...

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3