The article background

The family’s first computer, bought in the summer of 2006, was largely unused after 10 years of college and sold to an acquaintance as a second-hand computer within two years.

My brother is 10 years younger than me and has just started grade one this year. He had been texting me from my mom’s phone all day, six months before I graduated, asking when I was going to send him my desktop from school. Although I have been a little uncomfortable with my parents because of playing computer since childhood, I still encourage my brother to get in touch with computer and the Internet. Some might say that all kids do is play games. What’s wrong with playing games? Children’s nature is not to play, in the process of entertainment is in fact can imperceptibly learn a lot of knowledge about the daily use of computers, but also through the Internet with already known, have not yet known friends to communicate, the benefits are many.

However, there must be a limit to everything. Junior high school students can’t control themselves as much as we adults (not to mention that many adults are addicted to this virtual world), so I came up with the idea of writing a program to limit my brother’s daily computer time.

Technology selection

Actually, my first thought was to use Java, because it’s the one I use most and I haven’t used any other language except C++.

The basic idea of the program

1. Use a TXT file to record the cumulative use time of the computer in a day

2. It will automatically shut down if the accumulated use time exceeds several hours

3. The program starts automatically

Why did I end up with Python

Thinking about how to write, search data found that Java is not suitable, although it is not impossible to achieve, but there are several problems are a little trouble to solve. For a “lazy” programmer like me, this is a good reason to “discard.” Take a simple example, finally will use the system shutdown command to achieve automatic shutdown this function, with Java+ running Windows command to find a lot of things, looking at the headache. After all, it’s your free time. There’s no need to think like this.

But with Python you only need two lines of code!

1 # Shutdown command

2 cmd=”cmd.exe /k shutdown -s -t 0″;

3

4. Run the shutdown command

5 os.system(cmd)

Code by hand

After a random search on the Internet, I soon wrote out the code for the test.

1 #! /usr/bin/env python

2

3 #coding=utf-8

4

5 import os

6 import time

7 import datetime

8

9 # Set limits depending on whether you are working days or not

10 if datetime.date.today().weekday()<5:

11 timeLimit = 20

12 else:

13 timeLimit = 40

14

Read the date recorded in the text

16 f = open(‘E:\\thefile.txt’,’r+’)

17 f_date = f.readline()

18 f.close

19

Read the system date and compare it with the text date

21 # If not, clear the file and initialize it for the day

22 n_date = time.strftime(“%d/%m/%Y”)+”\n”

23 if f_date ! = n_date:

24 print(“sucessed”)

25 f = open(‘E:\\thefile.txt’,’r+’)

26 f.truncate()

27 f.close

28 f = open(‘E:\\thefile.txt’,’r+’)

29 f.write((n_date))

30 run_time=”0″

31 f.write(run_time)

32 f.close

33

34 # infinite loop statement that breaks out of a loop if and only if the running time is greater than or equal to the limit

35 while 2 > 1 :

36 f = open(‘E:\\thefile.txt’,’r+’)

37 f_date = f.readline()

38 run_time = f.readline()

39 run = int(run_time)

40 print(run)

41 time.sleep(2)

42 if run<timeLimit:

43 run = run + 1

44 f.truncate()

45 f.close

46 f = open(‘E:\\thefile.txt’,’r+’)

47 f.write(f_date)

48 run_time = str(run)

49 f.write(run_time)

50 f.close

51 else:

52 break

53

54 # Shutdown command

55 cmd=”cmd.exe /k shutdown -s -t 0″;

56

57 print(cmd)

In order to facilitate everyone to understand the program, and then the content of the TXT file posted for reference

01/10/2014

20

A few points in particular need to be made

First, you need to copy the Python files to the Startup folder on your system, since you need to boot automatically.

Second, since the console interface will appear after the.py file is launched, it is very difficult to use the file, so change the.py file suffix to.pyw.

Third, you don’t need to worry about this limitation if you want to use your computer. Just shut down python within six minutes of starting up.

Final product

After a while, I finally finished writing the actual code.

1 #! /usr/bin/env python

2

3 #coding=utf-8

4

5 import os

6 import time

7 import datetime

8

9 # Set limits depending on whether you are working days or not

10 if datetime.date.today().weekday()<5:

11 timeLimit = 20

12 else:

13 timeLimit = 40

14

Read the date recorded in the text

16 f = open(‘E:\\thefile.txt’,’r+’)

17 f_date = f.readline()

18 f.close

19

Read the system date and compare it with the text date

21 # If not, clear the file and initialize it for the day

22 n_date = time.strftime(“%d/%m/%Y”)+”\n”

23 if f_date ! = n_date:

24 print(“sucessed”)

25 f = open(‘E:\\thefile.txt’,’r+’)

26 f.truncate()

27 f.close

28 f = open(‘E:\\thefile.txt’,’r+’)

29 f.write((n_date))

30 run_time=”0″

31 f.write(run_time)

32 f.close

33

34 # infinite loop statement that breaks out of a loop if and only if the running time is greater than or equal to the limit

35 while 2 > 1 :

36 f = open(‘E:\\thefile.txt’,’r+’)

37 f_date = f.readline()

38 run_time = f.readline()

39 run = int(run_time)

40 time.sleep(360)

41 if run<timeLimit:

42 run = run + 1

43 f.truncate()

44 f.close

45 f = open(‘E:\\thefile.txt’,’r+’)

46 f.write(f_date)

47 run_time = str(run)

48 f.write(run_time)

49 f.close

50 else:

51 break

52

53 # Shutdown command

54 cmd=”cmd.exe /k shutdown -s -t 0″;

55

56 # Execute shutdown command

57 os.system(cmd)

Monday to Friday, the younger brother’s access to the computer was limited to two hours; On weekends, it’s four hours.

feeling

1. Although this is a very simple little program, almost no difficulty at all, but the effect is quite large. Because my brother and I work are not at home, usually is the mother tube brother, every time to stare at him, can not let him play computer too long, sometimes to loudly let him quickly shut down to write homework. Now, want everybody to discuss the time that uses computer at ordinary times only, set good parameter to go. Mom can do her own thing in peace, and brother can play the game he wants to play in peace. (Make the rules, enforce them in a convenient and indistinguishable way, and this way and means are friction-reducing lubricants.)

2. Working in Java, it’s fun to play with other languages (such as Python) in your spare time. Be driven by hobbies and small projects that can help you change jobs or make changes later.

3. As programmers, programming is our bread and butter. It can also be a tool to change our lives and our thinking, even if the impact is insignificant to others.

PS: If you have any interesting ideas about programming, creativity and needs in real life, please share them in the comments. You can also add group 705673780, we exchange learning ah ~