Life is short, I use python--when I play Python what do I play

Source: Internet
Author: User

Article background

The first computer in the home was bought in the summer of 2006, 10 years after the university basically no one used, after two years as second-hand sold to an acquaintance.

Brother Ego 10 years old, this year just on the first. He used my mother's cell phone to send text messages to me half a year before I graduated, and asked me when I would send my school desktops home to him. Although since childhood has been because of playing computer problems and parents occasionally a little awkward, but I still very much encourage my brother to contact the computer and network. Some people might say that children only play games and play games. Children's nature is not to play, in the process of entertainment is in fact can be imperceptible to learn a lot of computer daily use of knowledge, but also through the internet and have known, not yet know friends to communicate, the benefits are many.

However, everything has to be a degree. Junior high school students can not be like our adults to restrain themselves (and do not say that many adults will indulge in this virtual world), I have to write a program to limit my brother to play computer time on weekdays.

Technology selection

In fact, my first idea is to use Java, because most of the usual use of it, and in addition to C + + has not been used in other languages.

  The basic idea of the procedure

    1. Use a TXT file to record your computer's cumulative usage time in one day
    2. Automatically shuts down after more than a few hours of usage
    3. Automatic operation of program boot

  Why did I finally choose python?

think of how to write, search information when the Java is not suitable, although not impossible, but there are several problems to solve a little trouble. For a "lazy" programmer like me, it's a good reason to be "deprecated". Just to give a simple example, the end will use the system shutdown command to achieve automatic shutdown this function, with java+ run Windows command to search out a lot of things, look at the headache. After all, it is free time to play, there is no need to such a brain.

But with Python you just need to use two lines of code!

1 # shutdown Command        2 cmd="cmd.exe/k shutdown-s-T 0"; 3 4 # Execute shutdown Command 5 os.system (cmd)

Write code

After a random search on the internet, the code for the test was soon written out.

1 #!/usr/bin/env python2 3 #Coding=utf-84 5 ImportOS6 Import Time7 Importdatetime8 9 #limit time based on whether the workday is setTen ifDatetime.date.today (). Weekday () <5: OneTimeLimit = 20 A Else: -TimeLimit = 40 -  the #read the date recorded in the text -f = open ('E:\\thefile.txt','r+') -F_date =F.readline () - F.close +  - #Read the system date and compare it to the text date + #if not equal, empty the file for the same day initialization AN_date = Time.strftime ("%d/%m/%y")+"\ n" at ifF_date! =n_date: -     Print("sucessed") -f = open ('E:\\thefile.txt','r+') - f.truncate () - F.close -f = open ('E:\\thefile.txt','r+') in F.write ((n_date)) -Run_time="0" to f.write (run_time) + F.close -  the #a dead loop statement that jumps out of a loop when and only if the run time is greater than or equal to the limit time *  while2 > 1 : $f = open ('E:\\thefile.txt','r+')Panax NotoginsengF_date =F.readline () -Run_time =F.readline () theRun =Int (run_time) +     Print(Run) A     ifrun<TimeLimit: theTime.sleep (2) +Run = run + 1 - f.truncate () $ F.close $f = open ('E:\\thefile.txt','r+') - f.write (f_date) -Run_time =Str (run) the f.write (run_time) - F.closeWuyi     Else: the          Break -  Wu #shutdown Command -Cmd="cmd.exe/k shutdown-s-T 0"; About  $ Print(CMD)

 In order to facilitate understanding of the program, and then put the contents of the TXT file for reference

01/10/201420

  Several points that need to be specifically raised

First, the Python file is copied to the "Startup" folder of the system because it needs to be enabled for boot.

Second, since the. py file will have a console interface appearing after it is started, it is very important to use the effect, so the suffix of the. py file is changed to. pyw.

Third, there is no need to worry if you want to use the computer will be subject to this limit, as long as the boot six minutes to the Python process to turn off.

The final result

Busy for a while, finally the actual use of the code to finish writing.

1 #!/usr/bin/env python2 3 #Coding=utf-84 5 ImportOS6 Import Time7 Importdatetime8 9 #limit time based on whether the workday is setTen ifDatetime.date.today (). Weekday () <5: OneTimeLimit = 20 A Else: -TimeLimit = 40 -  the #read the date recorded in the text -f = open ('E:\\thefile.txt','r+') -F_date =F.readline () - F.close +  - #Read the system date and compare it to the text date + #if not equal, empty the file for the same day initialization AN_date = Time.strftime ("%d/%m/%y")+"\ n" at ifF_date! =n_date: -     Print("sucessed") -f = open ('E:\\thefile.txt','r+') - f.truncate () - F.close -f = open ('E:\\thefile.txt','r+') in F.write ((n_date)) -Run_time="0" to f.write (run_time) + F.close -  the #a dead loop statement that jumps out of a loop when and only if the run time is greater than or equal to the limit time *  while2 > 1 : $f = open ('E:\\thefile.txt','r+')Panax NotoginsengF_date =F.readline () -Run_time =F.readline () theRun =Int (run_time) +     ifrun<TimeLimit: ATime.sleep (360) theRun = run + 1 + f.truncate () - F.close $f = open ('E:\\thefile.txt','r+') $ f.write (f_date) -Run_time =Str (run) - f.write (run_time) the F.close -     Else:Wuyi          Break the  - #shutdown Command WuCmd="cmd.exe/k shutdown-s-T 0"; -  About #Execute shutdown Command $Os.system (CMD)

  From Monday to Friday, the younger brother can only use the computer for up to two hours, while on weekends, it is four hours.

  Feelings

1. Although this is just a very simple small program, it is almost impossible to say, but the role is quite large. Because I work with my brother is not at home, usually is the mother of the brother, every time to stare at him, can not let him play computer too long time, and sometimes to very loudly let him shut down to write homework. Now, as long as everyone to discuss the normal use of computer time, set parameters on the line. Mother can do their own things with peace of mind, the younger brother can also feel comfortable to play the game he wants to play. (making rules that are implemented in a convenient and non-discriminating manner, which is the lubricant that reduces friction)

2. Usually work is in Java, spare time in other languages (such as Python) play is a very interesting practice. With interest and small works as the driving, perhaps later can become your job-hopping or transformation by virtue.

3. As programmers, programming is our job, and it can be our tool to change our lives and change our thinking, even if it seems trivial to others.

PS: About programming, on the real life of the creative and demand, if you have any interesting ideas, very welcome to comment on the message to share. Also can pay attention to my public number, "Chinese laborers small Y" continue to communicate.

 

Life is short, I use python--when I play Python what do I play

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.