You are not logged in.
#!/usr/bin/env python
#
# mpc.py
#
# Hacked by MrGreen
#
import os
import string
import re
def mpc():
f= os.popen("mpc")
mpc = f.readline()
f.close()
mpc = mpc[string.find(mpc, '/') +1:string.rfind(mpc, ".")]
state = re.search('volume:', mpc)
if state:
return "[Stopped]"
else:
return "[ %s ]" % mpc
print mpc()
:cry:
Mr Green
Offline
Maybe try changing the name of the function? You could be running into a namespace collision with mpc as the function name, and as variable names inside the function...
What is the actual error you are seeing?
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
dude I never get errors when I code rofl
Name change good call ... just used mpc while I was testing it
I wondered if I could clean it up more ?
/me prays Dr.OOP does not show up ;-)
Mr Green
Offline
out of curiosity what's the problem?
Offline
Not a problem as such more of a "Is this the right way to do it knida thing"
Mr Green
Offline
well i would wrap the whole thing as a class. where everything is stored as a variable. I'd maybe use a static method for the creation of the info object:
class MPCInfo:
pass # details by you
info = MPCInfo.get()
print info.artist, info.title
Offline
emmmm nice wrap it up wonderd if I should add a def print_track_n_title as well?
import mpcinfo ?
Thanks
Mr Green
Offline
mpc = mpc[string.find(mpc, '/') +1:string.rfind(mpc, ".")]
Wowsers! Just use a regular expression. It's easier to read for starters, and also it's easier to detect problems, should, for some reason, 'mpc' string doesn't contain the expected characters.
Offline
forgive me ... I was using output of mpc ... then pulling out artist - track
would re give me the same result ?
the '/' some of my tracks have a directory heading ...
Mr Green
Offline
I'm not aware of "cactusman". Is it new? I can't find it in pacman repos.
I'm sitting here trying to imagine what cactusman pages look like
Offline
I'm sitting here trying to imagine what cactusman pages look like
CACTUS(1) CACTUS(1)
NAME
cactus - A desert plant known for standing around in the sun.
SYNOPSIS
cactus [OPTION]
DESCRIPTION
Do something kick ass on standard output.
-S, --stand-duration
How long to stand around.
-D, --drink-water
Absorb some quantity of water.
-h, --help
This help. Duh.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
I'm not aware of "cactusman". Is it new? I can't find it in pacman repos.
I'm sitting here trying to imagine what cactusman pages look like
I thought Cactusman was part of base system, without it nothing works
Mr Green
Offline
forgive me ... I was using output of mpc ... then pulling out artist - track
would re give me the same result ?
the '/' some of my tracks have a directory heading ...
mpc = re.search(r'/(.+).', mpc).group(1)
(Off the top of my head)
This re will return all the text between the first forward-slash and the last fullstop. (not inclusive, of course). The parentheses denotes a "group". It's handy for isolating a particular part within the larger context. So the .group(1) method returns the contents within the first group. .group() returns the full re match, btw.
I've found Pyreb (http://pyreb.nongnu.org/) handy in testing regular expressions too.
Offline
Seeing as there are a few pythonists lurking in here, any advice on where to start with Python?
I'm hoping to use it for a third year project in uni, just to force myself to learn something new as much as anything else. However, I have real difficulty just reading book after book, without doing any 'beginner' exercises.
I'm proficient with Java/C++ and the general OO way of doing things (paradigm, if you're into all that). I was wondering if anyone had any directions to exercises or articles/books they used.
I've searched a lot, reading Byte Of Python, and I also have Dive Into Python lying around. All well and good, but what about practicals? I realise this question is asked everywhere, I'm just wondering if you guys have any specific experience.
Cheers,
t
Offline
I just started with the library reference and the language reference. If you allready programm in another language this should be sufficent.
I would also recommend to use ipython (got tab completion - very nice) and inside ipython help() (interactive documentation).
Offline
The Python cookbook is also pretty handy, I've found:
http://aspn.activestate.com/ASPN/Python/Cookbook/
There's also a book called How to Think Like a Computer Scientist that's under a GNU license. It's very popular within universities and is in fact a guide of how to do lots of cool stuff in Python (although it's now been ported to other languages) for beginners.
Offline
Thanks very much for that guys. Forgot about that Thinking In Python. I did start to read it a while back, but never finished it.
I'll take a look at ipython, but one thing I don't understand with interactive shells is how to get them to just act like editors (which is totally pointless, I know). To write code, rather than be interactive. That's probably something I can just go and find out for myself.
Cheers again.
Any examples of the type of things you use Python for? I want to believe it's good for a lot more than scripting, which it does appear to be. Is there anywhere in general day to day coding (I don't know how that's defined) that Python falls down? I hear most (all?) of the system functions etc are coded in C deep down. Being weaned on Java, I like the appeal of Python's simplicity and clean code. Is it a substitute?
Offline
Seeing as there are a few pythonists lurking in here, any advice on where to start with Python?
I'm hoping to use it for a third year project in uni, just to force myself to learn something new as much as anything else. However, I have real difficulty just reading book after book, without doing any 'beginner' exercises.
...
t
Basically, to get anywhere in python, you need to know four things:
1. How to read a file
for line in open('myfile.txt'):
print line
2. Things you can do with strings:
http://docs.python.org/lib/typesseq.html
NB Remember that strings are immutable (that is, can't be changed in place) They need to be reassigned.
mystring = "TEST"
mystring.lower() # convert upper to lower
print mystring # still prints "TEST"!!!
mystring = mystring.lower() # reassign
print mystring # prints "test" woohoo!
3. Things you can do with lists
It's a big topic. But:
boffins = [] # empty list
boffins.append('cactus')
boffins.append('phracture')
boffins.append('arooaroo')
# or
boffins = ['cactus', 'phracture', 'arooaroo']
# iterate
for boffin in boffins:
if boffin == "arooaroo":
print "arooaroo is not a boffin"
else:
print boffin
# slicing lists
realboffins = boffins[0:2]
#or
realboffins = boffins[0:-1]
#etc
4. How to use regular expressions:
Again, it's a big topic. You can do lots with REs and once you get beyond the basic ones, they look bloody crytic to say the least.
I find Python's RE approach a tad too verbose when compared to Perl (I can't compare to Ruby though).
http://docs.python.org/lib/module-re.html
import re # need to import the re module first
mystring "<boffin>cactus</boffin><boffin>dusty</boffin>"
print re.search(r'<boffin>(.+?)</boffin>', mystring).group(1) # prints 'cactus'
boffinMatches = re.finditer(r'<boffin>(.+?)</boffin>', mystring)
for boffinMatch in boffinMatches:
print boffinMatch.group(1)
# prints:
# cactus
# dusty
print re.sub(r'<boffin>cactus</boffin>', '<boffin>phracture</boffin>', mystring)
# prints "<boffin>phracture</boffin><boffin>dusty</boffin>"
And that's enough to get you started
EDIT: PS, I've not tested anything above and so excuse me for my small typos/mistakes
Offline
Thanks a lot!
Really appreciate that, cheers.
T
Offline
A great starter:
http://diveintopython.org/toc/index.html
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
...
I've searched a lot, reading Byte Of Python, and I also have Dive Into Python lying around. All well and good, but what about practicals?
...
A great starter:
http://diveintopython.org/toc/index.html
I think this one has been covered. Although, I guess it stresses that it should be *read* and not left lying around!
Offline
doh!
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
'sokay. Even the boffins can make (tiny) mistakes.
Offline
err like I totally lost it on the first page lol 8)
Mr Green
Offline
err like I totally lost it on the first page lol 8)
Pray tell, young man. Exactly where did you get lost?
Offline