You are not logged in.

#1 2006-07-14 16:27:23

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

Python newbie needs the help of Dr. OOP

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

#2 2006-07-14 16:56:13

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Python newbie needs the help of Dr. OOP

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

#3 2006-07-14 17:18:29

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

Re: Python newbie needs the help of Dr. OOP

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

#4 2006-07-14 18:22:02

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

Re: Python newbie needs the help of Dr. OOP

out of curiosity what's the problem?

Offline

#5 2006-07-14 19:22:54

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

Re: Python newbie needs the help of Dr. OOP

Not a problem as such more of a "Is this the right way to do it knida thing"


Mr Green

Offline

#6 2006-07-14 19:26:47

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

Re: Python newbie needs the help of Dr. OOP

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

#7 2006-07-14 19:44:35

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

Re: Python newbie needs the help of Dr. OOP

emmmm nice wrap it up wonderd if I should add a def print_track_n_title as well?

import mpcinfo ?

Thanks  wink


Mr Green

Offline

#8 2006-07-14 21:32:52

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:
 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

#9 2006-07-14 22:06:06

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

Re: Python newbie needs the help of Dr. OOP

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

#10 2006-07-15 03:36:23

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

Re: Python newbie needs the help of Dr. OOP

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 smile

Offline

#11 2006-07-15 05:03:23

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Python newbie needs the help of Dr. OOP

deficite wrote:

I'm sitting here trying to imagine what cactusman pages look like smile

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

#12 2006-07-15 05:18:06

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

Re: Python newbie needs the help of Dr. OOP

deficite wrote:

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 smile

I thought Cactusman was part of base system, without it nothing works  wink


Mr Green

Offline

#13 2006-07-15 09:57:27

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:

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

#14 2006-07-15 10:07:53

tmadhavan
Member
From: Wales :D
Registered: 2004-03-26
Posts: 441

Re: Python newbie needs the help of Dr. OOP

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

#15 2006-07-15 10:17:47

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

Re: Python newbie needs the help of Dr. OOP

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

#16 2006-07-15 10:34:33

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

Re: Python newbie needs the help of Dr. OOP

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.

http://greenteapress.com/thinkpython/

Offline

#17 2006-07-15 10:47:17

tmadhavan
Member
From: Wales :D
Registered: 2004-03-26
Posts: 441

Re: Python newbie needs the help of Dr. OOP

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

#18 2006-07-15 10:56:08

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

Re: Python newbie needs the help of Dr. OOP

tmadhavan wrote:

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 smile

EDIT: PS, I've not tested anything above and so excuse me for my small typos/mistakes

Offline

#19 2006-07-15 15:38:48

tmadhavan
Member
From: Wales :D
Registered: 2004-03-26
Posts: 441

Re: Python newbie needs the help of Dr. OOP

Thanks a lot!

Really appreciate that, cheers.

T

Offline

#20 2006-07-15 18:29:31

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Python newbie needs the help of Dr. OOP


"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

#21 2006-07-15 18:32:09

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

Re: Python newbie needs the help of Dr. OOP

tmadhavan wrote:

...

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?
...

cactus wrote:

I think this one has been covered. Although, I guess it stresses that it should be *read* and not left lying around! tongue

Offline

#22 2006-07-15 18:32:57

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Python newbie needs the help of Dr. OOP

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

#23 2006-07-15 18:54:55

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

Re: Python newbie needs the help of Dr. OOP

'sokay. Even the boffins can make (tiny) mistakes. smile

Offline

#24 2006-07-15 20:19:05

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

Re: Python newbie needs the help of Dr. OOP

err like I totally lost it on the first page lol  8)


Mr Green

Offline

#25 2006-07-15 20:23:59

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:

err like I totally lost it on the first page lol  8)

Pray tell, young man. Exactly where did you get lost?

Offline

Board footer

Powered by FluxBB