[Tutor] Scanning a file for specific text and copying it to a new file

2010-12-02 Thread Ben Ganzfried
I'm trying to build a program that reads in a file and copies specific
sections to a new file.  More specifically, every time the words
"summary on" are in the original file, I want to copy the following
text to the new file until I get to the words "summary off".

My questions are the following:
1) Once I have read in the old file, how do I copy just the parts that
I want to the new file?  What command do I give the computer to know
that "summary on" (whether capitalized or not) means start writing to
the new file-- and "summary off" means stop?  Also, I assume I'll put
all of this in a while loop based on the condition that we should read
until we are done reading the whole document?  (although, this is
somewhat inefficient because the last "summary on/off" could be way
before the end, but this is preferable to missing one that is at the
end...)

Thank you very much for any help you can provide.  Much appreciated!

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Meaning of -1 in Python

2010-12-10 Thread Ben Ganzfried
I'm currently working through the Google Python tutorial exercises and
had questions about the following function:

def not_bad(s):
  # +++your code here+++
  # LAB(begin solution)
  n = s.find('not')
  b = s.find('bad')
  if n != -1 and b != -1 and b > n:
s = s[:n] + 'good' + s[b+3:]
  return s

It's clear that n!=-1 and b!=-1 means something like : "if in the
string 's' we find the word "not" and in string 's' we find the word
"bad."

I'm wondering the following:
On a deeper computational level, what is going on here?  What exactly
does Python take -1 to mean?  Is it saying that since the string 's'
is indexed starting from 0 to len(s), and since -1 is not part of
that, that therefore something having the value of -1 will never be in
the string?  If so, then how exactly does using negative numbers to
count a string work?  I have read something about this earlier...

Also, does the part: b>n mean, in this case: "bad comes after not in
the string 's'"?

Thank you very much.

Sincerely,

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tab delimited question

2010-12-13 Thread Ben Ganzfried
I'm searching line by line for certain tags and then printing the tag
followed by the word immediately following the tag.

So for example, suppose I had the following line of text in a file:
"this   is  a   key test123 noisenoise  noise   noise   noise"

In this example, I would want to print "key test123" to a new file.
The rest of the words I would not want.

Here is my code so far:

def test(infile, outfile):
  for line in infile:
tagIndex = line.find("key")
start = tagIndex + 4
stop = line[start:].find("\t") -1
if tagIndex != -1:
print("start is: ", start)
print("stop is: ", stop)
print("spliced word is ", line[start: stop])

My question is the following: What is wrong w/ the variable 'stop'?
The index it gives me when I print out 'stop' is not even close to the
right number.  Furthermore, when I try to print out just the word
following the tag w/ the form: line[start: stop], it prints nothing
(it seems b/c my stop variable is incorrect).

I would greatly appreciate any help you have.  This is a much
simplified example from the script I'm actually writing, but I need to
figure out a way to eliminate the noise after the key and the word
immediately following it are found.

Thank you very much for any help you can provide.

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] AttributeError: 'list' object has no attribute 'find'

2010-12-21 Thread Ben Ganzfried
Hey,

I keep getting the error above and unfortunately browsing through
google and finding similar responses has not been fruitful for me.  My
code is below and I have marked off the location of the problem in my
code.  I'm wondering the following:

1) Doesn't the read() file object method return the specified
characters from the file as a string?
2) If #1 is correct, then why is my variable "source" being viewed as
a list as opposed to a string?
3) How can I change my variable "source" so that it can use the 'find'
method?  Or alternatively, is there another method besides the 'find'
method that would do the same thing that 'find' does that would work
on my variable 'source' as it currently is?

Thanks so much,

Ben

#recursively find each instance of the tag throughout the whole document
def findOneTag (tag, source, output):
print("tag is ", tag)
#print("source is now ", source)
print("output is ", output)
#base case
if source == "":
return
#recursive case
tagIndex = source.find(tag) #(*THIS IS THE LOCATION OF THE
PROBLEM**)
print("tagIndex is ", tagIndex)
start = source[tagIndex:].find("\t") + 1
print("start is ", start)
stop = source[tagIndex + start:].find("\t")
print("stop is ", stop)
if tagIndex !=-1:
output.write(tag + "\t" + line[start: stop])
print("spliced text is: ", line[start:stop])
#recursively call findOneTag(tag, String source[stop:], output)
findOneTag(tag, source[stop + 1:], output)

def main():
tag = "species"
inname = "skeletontext.txt"
outname = "skeletontext1234567.txt"
inname1 = open(inname, "r")
output = open(outname, "w")
source = inname1.readlines()
print("source is: ", source)
findOneTag(tag, source, output)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Java Virtual Machine Launcher Question

2010-12-26 Thread Ben Ganzfried
Happy holidays everyone!

My current question relates to a broader python programming question I
have and so I thought it was worth posting here.  I'm trying to read a
single MAGE-TAB file (http://www.mged.org/mage-tab/) and acquire
metadata.

I just downloaded WinRAR to open zip files, but when trying to open
the .jar file for my converter, I get the following message:

"Java Virtual Machine Launcher:

Failed to load Main-Class manifest attribute from
C:\Users\Ben\AppData\Local\Temp\Rar$DI01.718\isatools_deps.jar"

If anyone could help me better understand how to fix this, what is
happening and/or point me in the right direction, I would be very much
obliged.

Thank you very much!

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Odd result from function call

2011-01-07 Thread Ben Ganzfried
When I call one of my functions from the shell (ie compare(10, 5)) it
produces the correct output.  However, when I run the program after calling
the method later in the script, the result is bizarre.  I'm curious why the
wrong result is printed.  Here is an example:

def compare(x,y):
if x < y:
print (x, " is less than ", y)
print("x is ", x, "y is ", y)
elif x > y:
print(x, " is greater than ", y)
else:
print(x, " and ", y, " are equal.")


x = input("First x is: ")
y = input("First y is: ")
print("x is ", x)
print("y is ", y)
compare(x,y)
a = input("Second x is: ")
b = input("Second y is: ")
print("x is ", a)
print("y is ", b)
compare(a,b)
c = input("Third x is: ")
d = input("Third y is: ")
print("x is ", c)
print("y is ", d)
compare(c,d)

Sample (and incorrect) output w/ 10, 5:

First x is: 10
First y is: 5
x is  10
y is  5
10  is less than  5
x is  10 y is  5
Second x is:

When I do simply compare(10, 5) from the shell, I get the correct output (ie
10 is greater than 5).  I had thought I had narrowed the problem down to the
fact that when I run the script only the first digit is counted-- however,
it seems as if only the first digit is counted (ie anything starting w/ a 9
will be greater than anything starting with a 1 (even if the numbers are 9
and 1324234)), and THEN, the second digit is counted (such that 89 is
correctly identified at 81).

Anyway I'm wondering:
1) Why does the script run correctly when I simply call the function from
the shell but not when I try to call the function from within the script?
2) What is actually going on such that only the first digit is being
evaluated?  That is, the interpreter knows that x is 10 and y is 5-- and
yet, for some reason the 5 is being tested against the 1 and since 5 is
bigger than 1, it concludes that 5 is greater than 10.

thanks!

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Object/Class Beginner Questions

2011-01-14 Thread Ben Ganzfried
Hey guys,

I'm using a tutorial geared for a 2.x version of Python and I am currently
using Python 3.1-- so it is possible that my confusion has to do with
different notations between them.  But in any case, here is what I have:

>>> type(Time)

>>> t1 = Time()
>>> type(t1)


where:

class Time:
def __init__(self, hours = 0, minutes = 0, seconds = 0):
   self.hours = hours
   self.minutes = minutes
   self.seconds = seconds

def print_time(t1):
print(t.hours, ":", t.minutes, ":", t.seconds)

Now the book I am working with has the following example:

>>> type(Point)

>>> p = Point()
>>> type(p)


My questions are the following:
1) Why is the type for my class Time : >>> type(Time)
  
when the type for their class Point is: 
Also, what is the difference between "class" and "classobj" in this case?
2) Why does my t1 object give the following as its type: 
And in their p object example the type is: ?
3) What is happening such that when I try to call my print_time(t1) function
I get the following error:
>>> t1 = Time()
>>> t1.hours = 3
>>> t1.minutes = 30
>>> t1.seconds = 45
>>> print_time(t1)
Traceback (most recent call last):
  File "", line 1, in 
print_time(t1)
NameError: name 'print_time' is not defined


Thank you very much.

Sincerely,

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object/Class Beginner Questions

2011-01-14 Thread Ben Ganzfried
I actually just figured it out (since the tutorial talks about the
difference in indentation between a method and a function in a later
chapter).  Basically, a method is within a class and therefore cannot be
called from the command prompt whereas a function that stands by itself in a
script can be called from the command prompt.

Although if this isn't quite right or there's more to it, I would still
definitely appreciate any advice you have.

Thanks again,

Ben

On Fri, Jan 14, 2011 at 1:53 PM, Ben Ganzfried wrote:

> Hey guys,
>
> I'm using a tutorial geared for a 2.x version of Python and I am currently
> using Python 3.1-- so it is possible that my confusion has to do with
> different notations between them.  But in any case, here is what I have:
>
> >>> type(Time)
> 
> >>> t1 = Time()
> >>> type(t1)
> 
>
> where:
>
> class Time:
> def __init__(self, hours = 0, minutes = 0, seconds = 0):
>self.hours = hours
>self.minutes = minutes
>self.seconds = seconds
>
> def print_time(t1):
> print(t.hours, ":", t.minutes, ":", t.seconds)
>
> Now the book I am working with has the following example:
>
> >>> type(Point)
> 
> >>> p = Point()
> >>> type(p)
> 
>
> My questions are the following:
> 1) Why is the type for my class Time : >>> type(Time)
>'type'>
> when the type for their class Point is: 
> Also, what is the difference between "class" and "classobj" in this case?
> 2) Why does my t1 object give the following as its type:  '__main__.Time'>
> And in their p object example the type is: ?
> 3) What is happening such that when I try to call my print_time(t1)
> function I get the following error:
> >>> t1 = Time()
> >>> t1.hours = 3
> >>> t1.minutes = 30
> >>> t1.seconds = 45
> >>> print_time(t1)
> Traceback (most recent call last):
>   File "", line 1, in 
> print_time(t1)
> NameError: name 'print_time' is not defined
>
>
> Thank you very much.
>
> Sincerely,
>
> Ben
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object/Class Beginner Questions

2011-01-14 Thread Ben Ganzfried
* I meant that*: A method actually can be called from the command prompt,
but the syntax is quite different than that used to call a function from the
command prompt.

On Fri, Jan 14, 2011 at 2:37 PM, Ben Ganzfried wrote:

> I actually just figured it out (since the tutorial talks about the
> difference in indentation between a method and a function in a later
> chapter).  Basically, a method is within a class and therefore cannot be
> called from the command prompt whereas a function that stands by itself in a
> script can be called from the command prompt.
>
> Although if this isn't quite right or there's more to it, I would still
> definitely appreciate any advice you have.
>
> Thanks again,
>
> Ben
>
>
> On Fri, Jan 14, 2011 at 1:53 PM, Ben Ganzfried wrote:
>
>> Hey guys,
>>
>> I'm using a tutorial geared for a 2.x version of Python and I am currently
>> using Python 3.1-- so it is possible that my confusion has to do with
>> different notations between them.  But in any case, here is what I have:
>>
>> >>> type(Time)
>> 
>> >>> t1 = Time()
>> >>> type(t1)
>> 
>>
>> where:
>>
>> class Time:
>> def __init__(self, hours = 0, minutes = 0, seconds = 0):
>>self.hours = hours
>>self.minutes = minutes
>>self.seconds = seconds
>>
>> def print_time(t1):
>> print(t.hours, ":", t.minutes, ":", t.seconds)
>>
>> Now the book I am working with has the following example:
>>
>> >>> type(Point)
>> 
>> >>> p = Point()
>>
>> >>> type(p)
>> 
>>
>> My questions are the following:
>> 1) Why is the type for my class Time : >>> type(Time)
>>   > 'type'>
>> when the type for their class Point is: 
>> Also, what is the difference between "class" and "classobj" in this case?
>> 2) Why does my t1 object give the following as its type: > '__main__.Time'>
>> And in their p object example the type is: ?
>> 3) What is happening such that when I try to call my print_time(t1)
>> function I get the following error:
>> >>> t1 = Time()
>> >>> t1.hours = 3
>> >>> t1.minutes = 30
>> >>> t1.seconds = 45
>> >>> print_time(t1)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> print_time(t1)
>> NameError: name 'print_time' is not defined
>>
>>
>> Thank you very much.
>>
>> Sincerely,
>>
>> Ben
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] OOP question

2011-01-18 Thread Ben Ganzfried
Hey guys,

I'm trying to get a version of Roulette working and I had a quick question.
Here is my code:

class Outcome:

def __init__(self, name, odds):
self.name = name
self.odds = odds

def winAmount(self, amount):
return odds*amount

def __eq__(self, other):
if (self == other):
return True
else:
return False

def __ne__(self, other):
if (self != other):
return True
else:
return False

def __str__(self):
return "%s (%d:1)" % ( self.name, self.odds )

Now whenever I try to call the winAmount method, I get the following error:
NameError: global name 'odds' is not defined

I'm wondering the following: I had thought that by initializing "odds" in
the first initialization method that "odds" should then be accessible to all
the other methods in the class.  This does not seem to be the case.  So what
is the ideal fix?  On the one hand I still need to initialize "odds",
right?  But if "odds" is within the initialization method, how else can it
be accessed by the other methods?

Thanks so much!

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Project Idea

2011-01-28 Thread Ben Ganzfried
Hey guys,

Would it be feasible for a beginner to write a script that could connect
with:
https://chrome.google.com/extensions/detail/laankejkbhbdhmipfmgcngdelahlfoji
?  More specifically, I am interested in writing a script such that the user
would have to enter a short password (say 6 random letters) and only then
could they access the Internet.  (Also, it looks like stayfocusd only works
for google chrome...i'd love for my script to apply to all other internet
browsers that may be installed on a computer as well).

If so, I would love to hear any tips or advice for getting started that you
could suggest.

Thanks a bunch,

Ben
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Roulette Unit Test Questions

2011-02-05 Thread Ben Ganzfried
Hey,

I'm having a lot of confusion getting the unit test working for one of my
classes for the Roulette bot I'm working on and would greatly appreciate any
advice or help.

Here is the description of what I am trying to do:
http://homepage.mac.com/s_lott/books/oodesign/build-python/html/roulette/bin.html
.  Basically, I have my Bin class working and I am trying to get my BinTest
class to do the following: Perform a unit test of the
Binclass.
The unit test should create several instances of
Outcome,
two instances of
Binand
establish that
Bins
can be constructed from the
Outcome
s.

While I would greatly appreciate any pointers in fixing my mistakes, I would
also greatly appreciate any pointers telling me what exactly it is that I do
not yet understand.

Thanks a bunch, my code is below.

Here is my Bin class:

from Outcome import *
class Bin:

def __init__(self, *outcomes):
self.outcomes = outcomes

def add(self, outcome):
self.outcomes += outcome
return self.outcomes
def __str__(self):
return (', '.join( map(str,self.outcomes)))



Here is the code for my BinTest class:

import Outcome
import Bin

class BinTest:
def __init__(self):
pass

def create_Outcome(outcome):
o1 = Outcome(outcome)

#creates two instances of Bin
def create_Bin():
b1 = Bin(("Red", 5), ("Black", 17),("Red", 5))
b2 = Bin(("00-0-1-2-3"), ("00"))
#establishes that Bin can be constructed from Outcome
def construct_Bin_from_Outcome():
b3 = Bin(o2)
b4 = Bin(o4)
b5 = Bin(o1)

def main():
bin_test1 = BinTest()
bin_test1.create_Outcome()
#create_Outcome("Red", 5)
#create_Outcome("Black", 17)
#create_Outcome("Red", 5)
#create_Outcome("00-0-1-2-3")
#create_Outcome("00")

print("o2 is ", o2)
print("o3 is ", o3)
print("o4 is ", o4)
print("o5 is ", o5)
print("b1 is ", b1)
print("b2 is ", b2)
print("b3 is ", b3)
print("b4 is ", b4)
print("b5 is ", b5)

if __name__ == "__main__":
main()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tic-Tac-Toe

2011-02-19 Thread Ben Ganzfried
Hey,

I *think* that everything is working except my function gameWon.  I keep
getting the following error: " line 67, in gameWon
if (cell % 3 == 0) and (self.__mycells[cell] ==  self.__mycells[cell +
1]) and (self.__mycells[cell + 1]== self.__mycells[cell + 2]):
TypeError: unsupported operand type(s) for %: 'instance' and 'int'
>>> "

My thinking was the following: I wanted to check the verticals, horizontals,
and diagonals to see if they were the same.  If so, then the game is won.
I'm not sure why I'm not actually comparing the value inside the appropriate
cells, but clearly the error means that what I'm trying to do is not what is
actually happening.  My full code is below and I would greatly appreciate
any help you can provide.

Thanks,

Ben

_-

#Ben Ganzfried
#2/18/11
#Tic-Tac-Toe

class Player:


def __init__(self, name, type):
self.__name = name
self.__type = type

def move(self):
cell_index = input("%s's (%s) move: " & (self.__name, self.__type))
cell_index = int(cell_index)
while cell_index > 8 and cell_index < 0:
print("Please retry...")
cell_index = input("%s's (%s) move: " & (self.__name,
self.__type))
return cell_index

def getName(self):
return self.__name

def getType(self):
return self.__type

class Board:

def __init__(self):
self.__mycells = []
for i in range(0, 9):
current_cell = Cell()
self.__mycells.append(current_cell)

def print1(self):
counter = 0
for cell in self.__mycells:
cell.print1()
counter +=1
if counter % 3 == 0:
print("\n")

def setCellToX(self, cell_number):
cell = self.__mycells[cell_number]
if not cell.isOccupied():
cell.setToX()
return True
else:
return False

def setCelltoO(self, cell_number):
cell = self.__mycells[cell_number]
if not cell.isOccupied():
cell.setToO()
return True
else:
return False

def isTied(self):
for cell in self.__mycells:
if not cell.isOccupied():
return False
return True

def gameWon(self):
for cell in self.__mycells:
#horizontals
if (cell % 3 == 0) and (self.__mycells[cell] ==
self.__mycells[cell + 1]) and (self.__mycells[cell + 1]==
self.__mycells[cell + 2]):
return True
#verticals
elif (cell < 3) and (self._mycells[cell] == self._mycells[cell +
3]) and (self._mycells[cell] == self.__mycells[cell + 6]):
return True
#diagonals
elif (cell == 0) and (self.__mycells[cell] ==
self.__mycells[cell + 4]) and (self.__mycells[cell] == self.__mycells[cell +
8]):
return True
elif (cell == 2) and (self.__mycells[cell] ==
self.__mycells[cell + 2]) and (self.__mycells[cell] == self.mycells[cell +
4]):
return True
return False


class Cell:

def __init__(self):
self.__value = " "

def setToX(self):
self.__value = "X"

def setToO(self):
self.__value = "O"

def print1(self):
print(self.__value,)
#add a predicate (start in terms of question, not action)
def isOccupied(self):
return not self.__value == " "

def main():
#initialize game
#draw board
#create two players
#enter a loop-- prompt each player for a loop...stops when a player wins
or a tie
board = Board()
board.print1()
pX = Player("Ben", "X")
pO = Player("Sam", "O")
while not board.gameWon() and not board.isTied():
c1 = pX.move()
success = board.setCellToX(c1)
while not success:
c1 = pX.move()
success = board.setCellToX(c1)
board.print1()
c1 = pO.move()
success = board.setCellToO(c1)
while not success:
c1 = pO.move()
success = board.setCellToO(c1)
board.print1()
print("Game Over")



if __name__ == "__main__":
main()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor