hughesadam_87 54 Junior Poster

Can you explain this question? What do you mean by "the one from random array input" ?

Aren't you just using import random ?

I'm using from RandomArray import

Is there a better/simpler import?

hughesadam_87 54 Junior Poster

Sorry double post

hughesadam_87 54 Junior Poster

Got it. Thanks for straightening out that terminonology.

I'm still a bit unsure of how to pass variables through a method. For example, if I had a random number generated in def random and wanted to pass it later to a method called def othermethod, how would I do that?


P.S. Can't wait to see the pens crush the wings tonight. My brothers, sister and dad are going to be there, got tickets for like 300 bucks a pop. I'm stuck in D.C.

hughesadam_87 54 Junior Poster

Hey all,

I have a class with various methods and I want to pass the output of one method into the next method. For example:

class RockPaperScissors:
	'''Rock paper scissors game, best of 3'''
	tries = 3

	def __init__(self, name):
		'Reads in users call'
		self.name = name
		print 'You picked %s' % self.name   #%d if define a class variable
		RockPaperScissors.tries -= 1

	def random(self):
		'Generates a random number'
		number = randint(1, 3000)       
		print number
		'Test if even or odd'
		if number <= 10:
			output = 'paper'
		elif number < 20:
			output = 'scissors'
		else:
			output = 'rock'

	def decision(self):
		'Decides if win or loss'
		print ???????

At the end of the code, I'd like to print the output from def random; however, I'm not sure how to tell the code to recognize that output was used in the above method/instance.

I see there is a lot of information on how to pass variables between classes, but not on instances. Also, if anyone could recommend a better random number generator than the one from random array input, that would be really helpful. Also, am I confusing the terminology of instance and method here?

Thanks.

hughesadam_87 54 Junior Poster

Thanks guys, I will go through, and try to understand/reproduce your suggestions tomorrow morning. jlm669, you are right that this is better suited to a function, but it's just a small snippet from a bigger code I'm working in, so I have no choice but to consider it from an object oriented perspective. It's neat actually what the code does. It takes in data about the raw human genome from the UCSC Genome Browser data base and builds a dictionary by chromosome and then outputs the data into more useable forms. The dictionary part is neat, but is still confusing me a bit.

Also, to the mod who edited my post, sorry about that. I'll be more aware of my need to proofread.

hughesadam_87 54 Junior Poster

I am trying to learn object oriented programming and am still shaky on the ins and outs of classes. I understand their purpose, but can't seem to grasp many simple operations when using them.

The dive into python section on classes is not very helpful either, so if you know any resources, please let me know.

Here is what I seek to do:

Make a class that reads in data from a file.
Strips white space and splits the data.
Prints the output to the screen.

I seek to do that using classes.

Here is an attempt:

class FormatData:
	"""
	CLass to take data file and format it to my liking
	"""

	def __init__(self, file=none):


		infile = open(file)
		for line in infile: 
			line = line.strip()
                        sline = line.split()		      
                        print sline

The code runs, but nothing prints to the screen. I don't understand how to run this code properly. Unfortunately, the project I am working on has this identical operation stored in a class, and then goes on to create a dictionary so I need to understand this before I move on. Why is it not printing to screen? Do I have to do something special since I am working in classes?

Editor's note: Please do not mix tabs and spaces in your Python code! Proper indentations are just too important with Python. Avoid tabs.

hughesadam_87 54 Junior Poster

Hey all,

I'm trying to read through a code written by a colleague last year in which he used the BioPython package. The package is no longer on my computer, and I have to wait for the systems admin to update it; thus, I'm forced to merely read the code. What I keep struggling with is this part:

for seq_record in SeqIO.parse(handle, "fasta"): #for a given entry in the new seq_record holding

seq_string = seq_record.seq.tostring();
"""Get the sequence into a string"""
seq_length = len(seq_record.seq); #why seq_record.seq not set_record
"""get the sequence length"""

Exactly why did he need to call "seq_record.seq" instead of just "seq_record". Was that extra .seq necessary? If so, why?

hughesadam_87 54 Junior Poster

Hey guys,

Have been working on this tiny simple code for an hour and can't figure out how to fix it. The code does this:
Reads in 3-column, tab-delimited data file
Adds "1000" in the fourth column to every line
Writes out the 4-column file

def columns(infile, outfile):
	f = open(infile,'r')
	o = open(outfile,'w')

	temp = []   #Store an empty list
 
	for line in f:
		line=line.strip()
		line=line.split('\t')
		line.insert(3, "1000")
		temp.append(line)
#		print temp	 
 	
	o.write('\n'.join(temp)) 	
     	
	 

	print "See %s" % (outfile)    

	f.close()
	o.close()

And getting the error:
TypeError: sequence item 0: expected string, list found

I realize I'm trying to writeout a list and it is barking, but what can I do to fix it? I had an almost identical code run which only had one column of data to work with, but with multiple columns, it won't writeout.

hughesadam_87 54 Junior Poster

Is the data seperated using only spaces and tabs?

If so, try using string replacement (I don't know the phyton syntax) and replace every sequence of 1 or more tab or space-char with a tab-char.

A regular expression might look something like this: ([\t ]+)

Hmmm but I need to insert tabs where they are missing, you know what I mean? I need the code to recognize that text has been squished together and in those spots, separate them. Maybe something like tell the program to search for white spaces of length 2 or less. If found, delete them and replace by tab. How would one do that?

hughesadam_87 54 Junior Poster

Ok, no matter what I do, I can't get the data to appear as it does in my data file. Sorry, is the issue clear from my explanation?

hughesadam_87 54 Junior Poster

Sorry, have to put it into code format to see the error:

A                            B                               C                               D                              E
F  G                           H                                 I                               J
K                            M                               N                              O                              P
hughesadam_87 54 Junior Poster

My data set actually formatted correctly when I posted it lol let me try again:

A B C D
E F G H I
J K L M

hughesadam_87 54 Junior Poster

Hey everyone,

I have the following data set

A B C D E
F G H I J
K L M N O
P Q R S T

Where every ten lines or so (though not a regular pattern) the data breaks its format and two of the data pieces get lumped together without a tab delimiter. I don't have the code to work with, only the data itself. I'd like to write a code which essentially scans through the set, and when it finds that there is no tab space between two elements, it inserts one and continues.

My initial inkling would be to write all of the elements into a blank array and then scan the array for the absence of a tab character; however, I have no idea how to tell python to scan for a missing character and I also don't know how to tell python to look for a "tab" in general. E.G.: what is the command for a tab, '\t' or something of the like?

Thanks.

hughesadam_87 54 Junior Poster

Thanks for the help slate.

hughesadam_87 54 Junior Poster

What I don't understand is if the code was inherently flawed, why did it run a 1,000,000 line file perfectly fine.

hughesadam_87 54 Junior Poster

Hey all,

I ran a code today which digested an input file which was 304 MB, consisting of about 10 million lines with six columns a piece. I ran the code and got an indexing error. In troubleshooting, I copied only the first 1,000,000 lines into a new data file, ran it and the code ran fine. Google searches say that python arrays are only limited by the ram of the machine.

My question is how can I get a sense of the limitations of my machine? The machine I'm running the code on has several 64bit processors and 8 gb's of ram. Is there an exact way (say a built in command?) that will allow me to test if a data file will be too large without requiring that I actually run the file and wait for it to error. Secondly, what would you recommend I do to obviate such a problem in the future? Lastly, is there a smart way to append the code so that if it fails, it will let me know exactly at which line it failed so I get a sense of how far it got before crashing?

Thanks

hughesadam_87 54 Junior Poster

Cool suggestion, added.

hughesadam_87 54 Junior Poster

First let me say thanks to everyone who has been responding to my posts and providing valuable insight. I have been trying to add rep whenever possible, and appreciate your help.

My assignment was to write a python code which takes data from an infile, then has the user specify a column out of the infile, and then the program writes only the information from that column to an outfile. Thanks to help from you guys, I've also been able to include a portion of the code which will skip over blank lines instead of crashing. I'd like to optimize/change the code with your suggestions. I feel that I've probably included too many operations and am being redundant in places. Here is my code, with comments which try to convey my level of understanding. Please add suggestions and point out where my comments are fallacious.

def columns(infile, outfile):
	f = open(infile,'r')
	o = open(outfile,'w')

	col = int(raw_input('Please select a column (starting at 0) from you in file, %s:' % (infile))) 

	temp = []   #Store an empty list
 
	for line in f:
		if not line.strip():   #If empty line, strip the new line character and remaining space 
			continue
		else:
			line = line.split() #Split lines so that they can be operated on via list operations
        		temp.append(line[col])	 #Write the user-specified column into the empty list, "temp
		       
	o.write('\n'.join(temp))   
					

	print "See %s" % (outfile)  
	
	f.close()
	o.close()
hughesadam_87 54 Junior Poster

Perhaps I can clarify my concern. Take this code, which claims to skip blank lines (and works) which I found online:

infile = open("text.txt","r")

for line in infile:
        if not line.strip():
                continue
        else:
                print line

What is this code saying? Take a line in the infile, and then I don't understand what "if not line.strip()" is really doing. How would you guys put that in laymen's terms? "if not line.strip()"

hughesadam_87 54 Junior Poster

Hey all,

I have been reading some older posts on line/list manipulation, but don't quite understand exactly how to implement all of the tools that you guys are using (line.strip() for example) to get what I need. Essentially, I have a working program that iterates through data and outputs it into a file. If one of the lines in the file is blank, the program crashes. I want to know what simple snipet of code I can add to ensure that the program continues iterating. Even more awesome would be a way for the program to let me know at which points in the data blank lines appear. But I would be happy just having the program skip the blank lines.

Any suggestions?

hughesadam_87 54 Junior Poster

Thanks for the help woooee

hughesadam_87 54 Junior Poster

Hey fellas,

I have searched for this answer for about an hour on and off and cannot find anything so sorry to have to resort to posting.

I have a list [a, b, c, d]

And I want to write it to a simple output file, but I want it to be written with a new line after each character so that it appears in the file as:
a
b
c
d

There is more going on in the program but this is the only thing I really am concerned with at the moment. It's complaining when I insert a newline character into the writeout (I assume this only applies to strings).

Here is a snipet of my code if that helps:
--------------------------------------------- (Declarations separated below)
temp = [ ]

col = int(raw_input('Please select a column (starting at 0) from your infile, %s:' % (infile)))
--------------------------------------------

for line in f:
line = line.split() #Line has been split into a list of components [a,b,c...]
temp.append(line[col]) #Write the user-specified column into the empty list, "temp"

for i in range(len(temp)): #This line basically just prints one by one
o.write(temp)

hughesadam_87 54 Junior Poster

Thanks very much for your help.

hughesadam_87 54 Junior Poster

Hey guys,

I'm new to python so if this is a silly question, pardon me.

I have the following basic assignment:

Take a user-specified range of lines from some data file, call it input, and write them to an output data file. What I want to do is have the user specify the range (for example lines 10-20) that will be picked from the input file to the output file. I am trying to use readlines() and I am able to get the program to pick a certain number of lines, but it always begins as line 1. For example, if I specify lines (30-200) it will recognize that it must extract 170 lines from the input file; however, it starts at line 1 and runs to line 170 instead of starting at line 30. Here is a snipet of the code:

first = int(raw_input('Enter a starting value'))
last = int(raw_input('Enter a final value'))

def add_line_numbers(infile, outfile):
f = open(infile,'r')
o = open(outfile,'w')
i = 1


for i in range(first, last):
o.write(str(i)+'\t'+f.readline())


f.close()
o.close()


--- Parsing code follows


The code originally worked fine until I began editing it. The original code takes an entire input file and transfers it to an entire output file. So only my edits are in question.

The 'i' acts as a counter, and that part works fine. The counter will read …