You are not logged in.
I dived in & banged me head lol
/me reads up cool stuff ;-)
Mr Green
Offline
the re.search thing is buggin' me atm
not getting the results I want using code above.....
/me reads more...
Mr Green
Offline
the re.search thing is buggin' me atm
not getting the results I want using code above.....
/me reads more...
Just some tips:
you don't need to import string, thats done automatically now. 'string'.find('bla') should work. readline() will only read the first line, if you want them all use readlines() and read() does the same thing as readlines except its given to you as a string versus a list. Also, you shouldn't need the re module too much unless you need to utilize special characters, you can easily test for keywords by using an if statement. if 'string' in list: or if 'characters' in string.
Obviously you are trying to parse a file but what are you trying to get out of it and what are the file contents? Post it here and I'll try to help.
Offline
the re.search thing is buggin' me atm
not getting the results I want using code above.....
/me reads more...
Are you referring to a specific re described in this thread? If so, let us know. If it's one of mine then I may have made a typo or something.
Offline
Ok....
Death_Metal/Aborted - Generic Murder Concept.mp3
I am trying to get artist & title from above (output of mpc)
readline yes I used it to pull first line of output to get the above now I need to bit between '/' & '.' (not all tracks show up / soo!)
the re.search did not work for me ... ok I can get basic matches ok ie
re.search('def', 'abcdef') but group does not always give me what I want (like in life lol)
Reading Dive into Python things are more clear but re is such a massive things(most of perl!) thats its going to take an old dog like me a little longer to figure it out
did find this
Not giving up yet ;-)
I know its basic stuff but .. the mpdclientmodule thing was giving me '-' and nothing else all I wanted was Artist & title
Mr Green
Offline
sounds like you want split for everything between '/' and '.':
>>> line = 'Death_Metal/Aborted - Generic Murder Concept.mp3'
>>> artist_title = line.split('/')[1]
>>> artist_title = artist_title.split('.')[0]
>>> print artist_title
Aborted - Generic Murder Concept
EDIT:
or re.sub
>>> artist_title = re.sub('^.*/', '', line)
>>> artist_title = re.sub('..*$', '', artist_title)
>>> print artist_title
Aborted - Generic Murder Concept
but I prefer the former.
Offline
lol the first makes more sense now.... thanks
but if '/' is not there it drops out ... guess I could catch that
if line.split('/'):
artist_title = line.split('/')[1]
could even just chop off last 4 chars?
/me crushes it to one line ;-)
Cannot handle reading online manuals grabbed a copy of this
http://www.oreilly.com/catalog/lpython2/
for some bedtime reading
Linux chicks dig python 8)
Mr Green
Offline
re.search('def', 'abcdef') but group does not always give me what I want (like in life lol)
But in this regular expression, you haven't specified any groups, so you can't specify a particular group number:
re.search('def', 'abcdef').group() # returns 'def'
re.search('def', 'abcdef').group(1) # returns a NoneType exception IIRC
re.search('d(ef)', 'abcdef').group(1) # returns 'ef'
Do you see the difference?
Offline
since 'Death_Metal/Aborted - Generic Murder Concept.mp3' seems to be a path, I'd use os.path:
import os.path
import re
line = 'Death_Metal/Aborted - Generic Murder Concept.mp3'
basename = os.path.basename(line)
artist_title_re = re.compile('([^-]*)-([^.]*).([^.]*)')
match = artist_title.re.search(basename)
artist = match.group(1).strip()
title = match.group(2).strip()
extension = match.group(3).strip()
Offline
@cmp Woooaaah! thats radical man
I grab output from mpc
[ ~ ] > mpc
Death_Metal/Aborted - Generic Murder Concept.mp3
[playing] #8/735 0:30 (13%)
volume:100% repeat: on random: off
I thought it would be easy to figure this out,as always there is more way to deal with the problem
/me may just strip off last 4 chars and be done with it lol
Mr Green
Offline
mpc.rsplit("/", 1)[-1].rsplit(".", 1)[0]
Thanks xersex2 ;-)
Mr Green
Offline
I just want to say that in some strange operating system the directory delimiters could be '' then your code won't work, while the os.path module would resolve such problems - at least if the operating system running mpd and the script are the same, if not I don't know.
Offline
I just want to say that in some strange operating system the directory delimiters could be '' then your code won't work, while the os.path module would resolve such problems - at least if the operating system running mpd and the script are the same, if not I don't know.
Good point.
And to be honest, the rsplit stuff isn't the most readable in the world, one could argue.
Offline
true there are so many of doing it ...
maybe when I have read up re then I will have a go at making a spell ;-)
for now I am learning so don't be too hard on me
8)
Mr Green
Offline
#!/usr/bin/env python
#
#ob_ mpc.py
#
# Hacked by MrGreen
#
import os
#import string
#import re
import ob3pipe
p= ob3pipe.Pipes()
def mpc():
f= os.popen("mpc")
mpc = f.readline()[:-5]
f.close()
if mpc.startswith("vol"):
return "[Not playing]"
else:
return "[ %s ]" % mpc.rsplit("/", 1)[-1].rsplit(".mp3", 1)[0]
print mpc()
Its getting better any way I can reduce lines of code?
Mr Green
Offline
if you delete the import string and import re then you'll remove 2 lines without a hitch!
The other way is the omit the final else
if something:
return X
return Y
is the same as
if something:
return X
else:
return Y
Be careful though. There's no need to go over the top in reducing code size. You can do a lot of things in one line nowadays in high level languages, but that one line can often be as cryptic as hell. You code is small enough AND it's understandable to other users, and perhaps more importantly, yourself when you next open it in a couple of months.
Offline
yeah I will remove the dead imports...
emmm return line if that works which it should thats ok...
no more...
I added '.mp3' because if a track had a '.' in the title you lose half a the string the Taginfo module I was reading about in dive into python looks interesting ;-)
But for what I need this is fine
now I got hack it into Pypanel
Mr Green
Offline
Be careful though. There's no need to go over the top in reducing code size. You can do a lot of things in one line nowadays in high level languages, but that one line can often be as cryptic as hell. You code is small enough AND it's understandable to other users, and perhaps more importantly, yourself when you next open it in a couple of months.
I'll second that. I've fallen victim to this several times in the past, having to rewrite blocks all over again because I couldn't remember what the hell I was thinking at the time. Clear code is much more important in the long-run as its easier to maintain. I think cactus said it best: "clear code is better than clever code"
Offline
Ok ...I see comments are useful not only for me but others that may want to know what is going on
I know (sorry read !) about __doc__ strings (triple quotes) seen comments in def(s) like "This is cool" on it own ... errrr
def Cactusiscool():
"Well thats what he told me!"
<rest>
The real reason for crushing code was that I tend to write 20 lines when 5 will do....& when I showed Dr.OOP my code he laughed and said 'Python is' he never finished his sentences (kinda weird!) then he wrote down a couple of lines of what looked to me like nothing ... but of course it did the same thing :oops:
No while I am learning I will comment my way through it thanks guys ;-)
Mr Green
Offline
The good thing is that python is what a lot of people call self-commenting code. Usually python code is so clear as to what it's doing that it doesn't even need comments (still good practice of course, but it's not NEARLY as bad as C/C++). This is assuming you're coding in a pythonic style of course. I usually get by with just doc strings. I've shared some of my code with people and they can understand it right away.
Offline