You are not logged in.

#26 2006-07-15 20:40:16

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

I dived in & banged me head lol

/me reads up cool stuff ;-)


Mr Green

Offline

#27 2006-07-16 20:46:58

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

the re.search thing is buggin' me atm

not getting the results I want using code above.....

/me reads more...


Mr Green

Offline

#28 2006-07-16 21:38:08

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Python newbie needs the help of Dr. OOP

Mr Green wrote:

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

#29 2006-07-16 23:23:29

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python newbie needs the help of Dr. OOP

Mr Green wrote:

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

#30 2006-07-17 06:02:11

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

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

#31 2006-07-17 10:25:56

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Python newbie needs the help of Dr. OOP

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

#32 2006-07-17 12:00:14

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

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

#33 2006-07-17 12:36:01

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python newbie needs the help of Dr. OOP

Mr Green wrote:

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

#34 2006-07-17 13:13:39

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: Python newbie needs the help of Dr. OOP

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

#35 2006-07-17 13:21:59

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

@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

#36 2006-07-18 18:23:46

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

mpc.rsplit("/", 1)[-1].rsplit(".", 1)[0]

Thanks xersex2 ;-)


Mr Green

Offline

#37 2006-07-18 20:26:39

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: Python newbie needs the help of Dr. OOP

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

#38 2006-07-18 20:28:36

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python newbie needs the help of Dr. OOP

cmp wrote:

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

#39 2006-07-18 20:31:13

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

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

#40 2006-07-20 11:44:15

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

#!/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

#41 2006-07-20 13:52:13

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python newbie needs the help of Dr. OOP

if you delete the import string and import re then you'll remove 2 lines without a hitch! tongue

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

#42 2006-07-20 14:12:23

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

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

#43 2006-07-20 22:35:36

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Python newbie needs the help of Dr. OOP

arooaroo wrote:

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

#44 2006-07-21 06:05:04

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Python newbie needs the help of Dr. OOP

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

#45 2006-07-21 17:42:19

deficite
Member
From: Augusta, GA
Registered: 2005-06-02
Posts: 693

Re: Python newbie needs the help of Dr. OOP

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

Board footer

Powered by FluxBB