A python script that wants to use Mac startup / end time for attendance management

Recently, I have been shifting to a work style that sells off time, so I decided to manage attendance for the first time in a few years, so I wrote a script for it, so I will leave it as a memo.

Requirements

code

All you have to do is hit ** last reboot ** and ** last shutdown ** to sort

$ python attendance_output.py 7 <-Take the month you want to take as an argument

attendance_output.py


import sys                                                                                                                                     
import re
import subprocess
import dateutil.parser

def attendanceDate(month):
    output = {}
    checkMonth = [0]
    def assignDate(matchDate, func):
        parseDate = dateutil.parser.parse(matchDate)
        if checkMonth[0]:
            if checkMonth[0] is not parseDate.month: return

        if parseDate.month is month:
            dateStr = parseDate.day
            if dateStr in output:
                if job in output[dateStr]:
                    output[dateStr][job] = parseDate if func(output[dateStr][job], parseDate) else output[dateStr][job]
                else:
                    output[dateStr].update({job: parseDate})
            else:
                output[dateStr] = {job: parseDate}
            checkMonth[0] = parseDate.month

    for job, func in {'reboot': lambda x,z:x>z, 'shutdown': lambda x,z:x<z}.items():
        for checkDate in subprocess.check_output("last %s" % job, shell=True).splitlines():
            matchObj = re.findall(r'%s\s+\~\s+([a-zA-Z]{3}\s[a-zA-Z]{3}\s+\d{1,2}\s+\d{1,2}\:\d{1,2})\s' % job, checkDate.decode('utf-8'))
            if len(matchObj): assignDate(matchObj[0], func)

    return output

for key, value in sorted(attendanceDate(int(sys.argv[1])).items(), key=lambda x: x[0]):
    print('%s/%s %s, %s' % (int(sys.argv[1]), key,
                'start %s' % value['reboot'].strftime('%H:%M') if 'reboot' in value else '',
                'end %s' % value['shutdown'].strftime('%H:%M') if 'shutdown' in value else ''))

result

It is OK if the data is output as follows. It's even better to output CSV or spreadsheet later

6/1 start 14:14, end 14:42
6/2 start 11:44, 
6/5 start 11:56, end 22:30
6/6 start 12:00, end 19:07
6/7 start 11:51, end 19:03
6/8 start 12:15, end 12:22
6/9 start 10:17, 
6/13 start 12:04, end 19:19
6/14 start 12:10, 
6/16 start 12:26, end 18:59
6/18 start 17:50, end 18:18
6/19 start 12:29, end 19:04
6/23 start 12:03, 
6/27 start 12:26, 
6/28 start 15:48, end 18:16
6/29 start 12:40, end 19:11
6/30 start 18:30, end 21:57

Recommended Posts

A python script that wants to use Mac startup / end time for attendance management
How to use pip, a package management system that is indispensable for using Python
A python script for Mac that zips without garbled characters on Windows
A Python script that saves a clipboard (GTK) image to a file.
A python script that converts Oracle Database data to csv
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
I want to use a wildcard that I want to shell with Python remove
[Introduction to Python] How to use the in operator in a for statement?
A script that returns 0, 1 attached to the first Python prime number
A python script that deletes ._DS_Store and ._ * files created on Mac
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
Things to watch out for when creating a Python environment on a Mac
After buying a new Mac, use pyenv + poetry to build a Python environment.
[Python] I made a decorator that doesn't seem to have any use.
How to use hmmlearn, a Python library that realizes hidden Markov models
Tips for Python beginners to use Scikit-image examples for themselves 3 Write to a file
[Python] It was very convenient to use a Python class for a ROS program.
[Python] Use pandas to extract △△ that maximizes ○○
[Mac] Building a virtual environment for Python
[Python] Organizing how to use for statements
How to use "deque" for Python data
Use Django from a local Python script
Script to create a Mac dictionary file
How to run a Maya Python script
How to use the Slack API using Python to delete messages that have passed a certain period of time for a specific user on a specific channel
I want to create a nice Python development environment for my new Mac
Python script to get a list of input examples for the AtCoder contest
How to use MkDocs for the first time
Use logger with Python for the time being
A clever way to time processing in Python
"Python Kit" that calls a Python script from Swift
A layman wants to get started with Python
Use Tor to connect from urllib2 [Python] [Mac]
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
A python script that generates a sample dataset for checking the operation of a classification tree
A Python script that crawls RSS in Azure Status and posts it to Hipchat
A python script that imports a dated csv file into BigQuery as a time partition table
[Python] I want to use only index when looping a list with a for statement
A python script that gets the number of jobs for a specified condition from indeed.com
A program that asks for a few kilograms to reach BMI and standard weight [Python]
[Mac] I want to make a simple HTTP server that runs CGI with Python
Script to easily create a client device environment for AWS IoT (Python v2 version)
A script that transfers tweets containing specific Twitter keywords to Slack in real time
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to execute sleep command, command that takes time as a test [bash, linux, mac]
"Stop committing Japanese files to git on Mac> <" For the time being, I wrote a script to search for incompatible Japanese files on Mac and Linux.
Create a dataset of images to use for learning
[BigQuery] How to use BigQuery API for Python -Table creation-
[For beginners] How to use say command in python!
I tried Python on Mac for the first time.
I made a VM that runs OpenCV for Python
A memorandum to run a python script in a bat file
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
What's in that variable (when running a Python script)
Created a Python library DateTimeRange that handles time ranges
[Python] How to write a docstring that conforms to PEP8
Convenient to use matplotlib subplots in a for statement
[Python] A convenient library that converts kanji to hiragana
How to build a development environment for TensorFlow (1.0.0) (Mac)
Python script to create a JSON file from a CSV file
Building a Python environment for programming beginners (Mac OS)