SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-02-11 06:18:51

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

Pomodoro Technique in bash & python

I just finished writing a Pomodoro Technique script, first in bash and then python, to see if I could do it. (What ended up happening was I did a lot of web searching for functions! tongue ) Anyway, I wanted to submit them to the community to see if you all could help me learn by "editing" the code. Any suggestions on how to clean these suckers up would be appreciated (especially the python one smile ), because I know they are filthy right now. Thanks in advance!

They are both in the pastebin, here and here.


Punch all your friends.

Offline

Be excellent to each other!

#2 2011-02-11 07:27:32

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: Pomodoro Technique in bash & python

The first one:

I don't know much python, so... what is btog? I see the declaration and I see you changed the value, but I don't see you do anything with them. Confusing.

EDIT: Oh now I see what you did.

On both files:

This btog thingy is not very elegant. You should create a counter variable that stores the information whether you are in the first step or not and let the function/class decide which line to choose (if...else...). It's not so important in a small project like this, but anything larger than that would be confusing this way.

Question: Why did you use functions in the first and classes in the second? A reason or just playing?

Last edited by Awebb (2011-02-11 07:36:35)


I'm so meta, even this acronym

Offline

#3 2011-02-11 15:46:12

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

Re: Pomodoro Technique in bash & python

oh...that if function would make sense for the string-changing. Though in the python I abandoned it alltogether.

And as to the question, I think it's because in the python, I thought performing the actions by passing class objects to a function would be better then writing 3 functions...it's probably messier though. hm...


Punch all your friends.

Offline

#4 2011-02-11 23:16:31

pico
#! Member
Registered: 2011-01-23
Posts: 66

Re: Pomodoro Technique in bash & python

There's no need to have those class definitions. They don't serve any purpose, nor their attributes for that matter.
Simply insert the data to the notifier constructor.

I didn't got what btog is for in the python version.

Offline

#5 2011-02-12 01:41:37

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

Re: Pomodoro Technique in bash & python

btog was for making the work string "GET BACK TO WORK!" and "for another..." after the first iteration, but the way I was using it didn't work and I abandoned it. It's cleaned up now.

Thanks for looking at it pico! I have applied your advices.


Punch all your friends.

Offline

#6 2011-02-13 19:26:06

benj1
Wiki Wizard
From: Yorkshire, England
Registered: 2009-09-05
Posts: 1,084

Re: Pomodoro Technique in bash & python

First off, sleep works in seconds but options say its in minutes so you get a message to take a break every 25 seconds (even tho it says minutes).

You don't need

os.popen(0"pkill -9 -f pomodoro.py")

Python will exit automatically but if you want to explicitly exit

sys.exit(0)

is preferred, (0 being the exit code, 0 means successful execution, other codes indicate an error.

Were you trying to do a status icon? (i noticed the png path)
if you were, this should get you started

def Statusicon(tttext):
    def quit_si(widget, data = None):
        if data: data.set_visible(False)
        gtk.main_quit()
    
    statusicon = gtk.StatusIcon()
    statusicon.set_from_file(image)
    statusicon.set_tooltip("there are %s minutes until your next break" % tttext)
    statusicon.connect('activate', quit_si)
    statusicon.set_visible(True)
    gtk.main()

Bear in mind tho you will probably have to rewrite your app to get it to work, and gtk programming isn't the easiest so i might be pointing you in the direction of a big can of worms roll

Finally I'm not sure what you intended the --stop flag to be used for but at the moment it misses out the loop and goes straight to the 'job done' message.

Anyway this is a good introduction to python http://openbookproject.net/thinkCSpy/

As far as the bash script goes, again you don't want the 'killall' and you might want to look into making use of cron, and just having a script to generate cronjob entries, not that theres anything wrong with the way you've done it.

For bash scripting http://tldp.org/LDP/abs/html/ (don't worry its not that advanced).

anyway good first scripts, enjoy smile


- - - - - - - - Wiki Pages - - - - - - -
#! install guide           *autostart programs, modify the menu & keybindings
configuring Conky       *installing scripts

Offline

#7 2011-02-13 21:02:35

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

Re: Pomodoro Technique in bash & python

@benj1: wow, thanks for the detailed input! now, for a point-à-point:

the second thing is due to lines 30-32. I forgot to uncomment the "* 60" parts of the lines! oops tongue

also, the '--stop' option is meant to be so I can start the timer with "pomodor.py" and stop it with
"pomodoro.py --stop". Does your solution allow for this?

and the icon was just for an icon in the notification bubble--it's actually the main reason I did it in python (:sheepish:). However, IF I had a notification area icon with a menu including stage (work, play, break), time left, and "quit" I wouldn't need to have the stop function or the os.popen! I will try to implement that soon thanks.

Thanks for the links too, man! I appreciate it smile


Punch all your friends.

Offline

#8 2011-02-13 23:39:52

benj1
Wiki Wizard
From: Yorkshire, England
Registered: 2009-09-05
Posts: 1,084

Re: Pomodoro Technique in bash & python

mahatman2 wrote:

also, the '--stop' option is meant to be so I can start the timer with "pomodor.py" and stop it with
"pomodoro.py --stop". Does your solution allow for this?

Ah I see, clever, no sys.exit wouldn't kill the other process in this case, I didn't realise what you were trying to do.

If this is a learning exercise you might want to look into daemonising it and communicating using signals, daemonising could be a bit heavy weight although instructive from a learning how linux works point of view, there's what seems like a good example here, im sure google will provide more. http://www.jejik.com/articles/2007/02/a … in_python/

Using signals would definately be beneficial tho, something as simple as this at the start of the script

open('/tmp/foo','w').write(str(os.getpid()))

getpid() gets the unique process id of the current process and writes it to file /tmp/foo

Then you can use

pid = int(open('/tmp/foo','r').read())
os.killpg(pid ,signal)

('kill -l' will give you a list of signals), sigkill (9), will do basically what you were doing before but slightly safer, sigstop (19) will stop a process and sigcont (18) will allow you to start the process again.
so

os.killpg(12345,9)

will kill the process with pid 12345, and is a drop in replacement for your popen call, assuming you sort the reading and writing of the pid.


- - - - - - - - Wiki Pages - - - - - - -
#! install guide           *autostart programs, modify the menu & keybindings
configuring Conky       *installing scripts

Offline

#9 2011-02-14 03:34:58

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

Re: Pomodoro Technique in bash & python

^ Oh nice thanks! I think I lost you a little bit with the killing...which does the os.killpg() function do?


Punch all your friends.

Offline

#10 2011-02-14 19:30:21

benj1
Wiki Wizard
From: Yorkshire, England
Registered: 2009-09-05
Posts: 1,084

Re: Pomodoro Technique in bash & python

it sends a signal to a process, I don't really know why its called killpg, yes you can use it to kill a process but os.kill() does that too, killpg allows you to send any posix signal to an process.

Anyway you can use these signal to control currently running jobs (processes), you probably  already use them to a certain extent ctrl-c sends a sigkill to a process (infact your pkill example explicitly calls sigkill thats what the -9 does (changing the flag to -19 should stop the process using sigstop)), ctrl-z sends a sigstop signal, 'fg' will start it again with a sigcont.

the other option is the process, which it identifies using the pid, which is a unique number assigned to every process, os.getpid() should do the same as pgrep, except its safer in this case because youre guanteed to get only the current process, not other pomodoro.py processes.

The other 2 lines of code in my previous post just write and read the pid so you know which process to send signals to.

wikipedia probably explains it better http://en.wikipedia.org/wiki/Signal_(computing)


- - - - - - - - Wiki Pages - - - - - - -
#! install guide           *autostart programs, modify the menu & keybindings
configuring Conky       *installing scripts

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo