You are not logged in.
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!
) 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
), because I know they are filthy right now. Thanks in advance!
Punch all your friends.
Offline
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
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
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
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
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 
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 
- - - - - - - - Wiki Pages - - - - - - -
#! install guide *autostart programs, modify the menu & keybindings
configuring Conky *installing scripts
Offline
@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 
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 
Punch all your friends.
Offline
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
^ 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
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
Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.