Cannot figure out line of code, also not understanding error

A

ApathyBear

I have two questions that come along with the following code:

----------------------------------------------------------------------

from __future__ import print_function

def sanitize(time):
if '-' in time:
splitter = '-'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
elif ':' in time:
splitter = ':'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
else:
return time


#start class
class Athlete:
def __init__(self, a_name, a_dob=None, a_times=[]):
self.name = a_name
self.dob= a_dob
self.times = a_times

def top3(self):
return(sorted(set([sanitize(t) for t in self.times]))[0:3])
#end class

def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)

except IOError:
print ('File error')
return (None)

james = get_coach_data('james2.txt')

print (james.name + "'s fastest times are: " + str(james.top3()))


----------------------------------------------------------------------
This is the original text file data I am working with called james2.txt located on my desktop:

James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16




----------------------------------------------------------------------





1. What does this line of code mean:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)

Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.




2. Why am I getting this error when I try to run the file?
PS C:\Users\N\desktop> python gg.py
File "gg.py", line 34
except IOError:
^
SyntaxError: invalid syntax
PS C:\Users\N\desktop>

What syntax is invalid here?
 
V

Vincent Vande Vyvre

Le 20/02/2014 08:32, ApathyBear a écrit :
I have two questions that come along with the following code:

----------------------------------------------------------------------

from __future__ import print_function

def sanitize(time):
if '-' in time:
splitter = '-'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
elif ':' in time:
splitter = ':'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
else:
return time


#start class
class Athlete:
def __init__(self, a_name, a_dob=None, a_times=[]):
self.name = a_name
self.dob= a_dob
self.times = a_times

def top3(self):
return(sorted(set([sanitize(t) for t in self.times]))[0:3])
#end class

def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)

except IOError:
print ('File error')
return (None)

james = get_coach_data('james2.txt')

print (james.name + "'s fastest times are: " + str(james.top3()))


----------------------------------------------------------------------
This is the original text file data I am working with called james2.txt located on my desktop:

James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16




----------------------------------------------------------------------





1. What does this line of code mean:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)

Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.




2. Why am I getting this error when I try to run the file?
PS C:\Users\N\desktop> python gg.py
File "gg.py", line 34
except IOError:
^
SyntaxError: invalid syntax
PS C:\Users\N\desktop>

What syntax is invalid here?
One parenthesis missing here:

return(Athlete(temp1.pop(0),temp1.pop(0), temp1))
 
C

Chris Angelico

1. What does this line of code mean:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)

Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.

It's supposed to be calling Athlete() with some arguments. However,
due to the extra open parenthesis between the keyword "return" and the
rest, it...
2. Why am I getting this error when I try to run the file?
PS C:\Users\N\desktop> python gg.py
File "gg.py", line 34
except IOError:
^
SyntaxError: invalid syntax
PS C:\Users\N\desktop>

.... causes this error, which is detected at the point where a keyword
comes in that makes no sense inside the return expression.

The solution is to delete the first open parenthesis:

return Athlete(temp1.pop(0),temp1.pop(0), temp1)

Then you have properly matched parens, and it should carry on happily.

Tip: Computers report errors where they find them, which isn't always
where the error actually is. But with most modern programming
languages, the file is read from top to bottom and left to right, so
when a problem is reported, its cause is always *before* it in the
file. Never after it. You could make a horrible mess of the file after
that "except" and you wouldn't change the error. (Apart from things
like text encoding, which are done in a separate pass.) You'll get
used to scanning up a line or two from the error to find the real
cause.

ChrisA
 
A

ApathyBear

Thanks for pointing out the missing parenthesis, it makes sense now why there was an error.

I suppose my question now is (and forgive my ignorance about classes, this is my first time learning them) why is it calling Athlete with some arguments? In order to make a class object, don't you need to create an instance by assigning a class to something?

like:
x = Athlete(temp1.pop(0),temp1.pop(0),temp1)

can a similar line look like this?:
temp1.pop(0) = Athlete(temp1.pop(0),temp1)
 
C

Chris Angelico

Thanks for pointing out the missing parenthesis, it makes sense now why there was an error.

I suppose my question now is (and forgive my ignorance about classes, this is my first time learning them) why is it calling Athlete with some arguments? In order to make a class object, don't you need to create an instanceby assigning a class to something?

like:
x = Athlete(temp1.pop(0),temp1.pop(0),temp1)

Calling a class will create a new instance of it. [1] What you do with
it afterwards is separate.

The return statement takes any value and, well, returns it. You can
return anything - an Athlete instance, the integer 13423523452, the
floating point value NaN, anything at all.
can a similar line look like this?:
temp1.pop(0) = Athlete(temp1.pop(0),temp1)

You can't assign to a function call, so no, you can't do that.

I recommend you start with the Python tutorial:

http://docs.python.org/3/tutorial/index.html

ChrisA


[1] The class might choose to do something different (when you call
bool with some argument, it'll return a pre-existing True or False),
but conceptually, you still get back an instance of that class.
 
G

Gary Herron

Thanks for pointing out the missing parenthesis, it makes sense now why there was an error.

I suppose my question now is (and forgive my ignorance about classes, this is my first time learning them) why is it calling Athlete with some arguments? In order to make a class object, don't you need to create an instance by assigning a class to something?

like:
x = Athlete(temp1.pop(0),temp1.pop(0),temp1)

can a similar line look like this?:
temp1.pop(0) = Athlete(temp1.pop(0),temp1)

First some notation: You are not creating a class, but rather in
instance of a class. The code
class Athlete:
...
created the class, and now you are ready to create (many?) instances
of that class.

A call like
Athlete(...)
will create an instance of that class with whatever parameters you
supply. What you do with that instance after it is created is your
choice. Assignment is one possibility, but many other operation are
also possible:

x = Athlete(...)
print( Athlete(...) )
Athlete(...)+Athlete(...) # If addition made any sense and was
implemented in the class
return Athlete(...)
...


Gary Herron
 
A

ApathyBear

Calling a class will create a new instance of it. [1] What you do with
it afterwards is separate.

Okay. So what you are saying is that return(Athlete(temp1.pop(0),temp1.pop(0), temp1)) IS in fact creating an instance of Athlete. My problem with this is that there really is no declaration of 'self' for this instance.


Usually when I do something like this.
x = Athlete("Henry", "11-15-90", [1,2,3])
I can refer to things of this instance by executing x.name or whatever other attributes the class defined.

If I create an instance with no 'self' how does this make any sense? How would I get an attribute for the our instance above?
 
C

Chris Angelico

Calling a class will create a new instance of it. [1] What you do with
it afterwards is separate.

Okay. So what you are saying is that return(Athlete(temp1.pop(0),temp1.pop(0), temp1)) IS in fact creating an instance of Athlete. My problem with this is that there really is no declaration of 'self' for this instance.


Usually when I do something like this.
x = Athlete("Henry", "11-15-90", [1,2,3])
I can refer to things of this instance by executing x.name or whatever other attributes the class defined.

If I create an instance with no 'self' how does this make any sense? How would I get an attribute for the our instance above?

Inside a method, including __init__, self refers to the object. Even
if you never assign it to anything, that instance exists somewhere,
and self can refer to it. It has an identity from the moment it begins
to exist - which is before it ever gets the name 'x' pointing to it.

You definitely should work through the tutorial I linked you to; it
explains Python's object model quite well.

ChrisA
 
A

ApathyBear

Thank you Chris. And thank you to everyone else. This has tremendously helped me. This google group is definitely the best place for python questions/discussion.

PS: Chris, I will be looking at that tutorial now.
 
D

Dave Angel

ApathyBear said:
Calling a class will create a new instance of it. [1] What you do with
it afterwards is separate.

Okay. So what you are saying is that return(Athlete(temp1.pop(0),temp1.pop(0), temp1)) IS in fact creating an instance of Athlete. My problem with this is that there really is no declaration of 'self' for this instance.


Usually when I do something like this.
x = Athlete("Henry", "11-15-90", [1,2,3])
I can refer to things of this instance by executing x.name or whatever other attributes the class defined.

If I create an instance with no 'self' how does this make any sense? How would I get an attribute for the our instance above?

The code you're describing is inside a function:


def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return Athlete(temp1.pop(0),temp1.pop(0), temp1)

So the caller might be doing something like

x = get_coach_data ("myfile.txt")

The return statement you're asking about returns a new instance of
Athlete, which gets assigned to x. Then you may try x.data or
equivalent if you like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top