[Tutor] subprocess output
I'm using Python 2.6.5 and I've got a challenge with the subprocess module. I'd like the output to be stored in a variable, and not sent to the stdout. The relevant lines as they are now: #!/usr/bin/env python import subprocess import sys dom = sys.argv[1] switch = sys.argv [2] answer = subprocess.call("whois " + dom, shell=True) Execute the above (whatever.py -example.com -a1) prints the entire WHOIS output, but I just want it saved to the variable 'answer', nothing more. Changing 'shell=True' to 'shell=False' raises an exception and removing the 'shell=' altogether is troublesome as well. Thoughts on how I can accomplish this? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Making a python script to feed files into another python script
I am trying to create a script that will get files from a directory that the user specifies, then feeds the relevant input and output (which are input and output paths) into another python script whose path is also given by the user. I'm pretty lazy after all and I would rather spend my time making tools than doing the busy-work myself. So far I can get it to read the directory fine but it ends up just opening the script I want the files to be fed into instead of giving it the input and output to run and running it. Any ideas? Here is what I have so far: import sys, os indir = raw_input('input directory path ') # input directory script1 = os.system(raw_input('python script path ')) for j in os.listdir(indir): input = os.path.join(indir,j) output = os.path.join(indir,j.split('rovctd')[0]+'.txt') script1(input,output) If this is going to be too complicated, I would at least be happy with how to tack on the list directory stuff to then of the script1- it would not be a stand alone script but at least it would work. I tired to do that too but it only processed the first file in the directory instead of all of them. Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Making a python script to feed files into another pythonscript
Unfortunately I was confused by both responses I received. I have only been working with Python for a week, so everything is new. Thanks for your patience. John- I was not sure what you meant by "command line". I took what you said quite literally and created this: indir = raw_input('input directory path ') # asks for input directory script1 = raw_input('python script path ') # asks for path to desired script for j in os.listdir(indir): input = os.path.join(indir,j) output = os.path.join(indir,j.split('rovctd')[0]+'.txt') os.system('python %s %s %s' % (script1, input, output) which does not run and I was confused by all the % symbols. I think I completely misinterpreted your comment. Alan-I am confused about how to used the communicate() module. Could you please give me an example? the format should be(?): communicate(input=None) where input is the file path I want to give my child process (script1) (what if I have two files paths I need to give it?- one for the input file path and one for the output? and I want to put PIPE and not None so that it will deliver that input? What I was trying to say before at the end of my post: > I would at least be happy with how to tack on the list > directory stuff to THE END of the script1- it would not be a > stand alone script but at least it would work. In other words, instead of having a stand alone script that feeds files into script1 (the program I want the files to be fed into-it reformats files so it requires a file path input and output), could I have script1 ask me for a directory instead of a specific input and output file path as it does now. Then it would theoretically go through and reformat each file in that directory, rename them and put the formatted files back in that directory. I hope that is clearer. Thanks again! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Newline
Hello everybody, I'm new to the mailing list so I'm pretty sure I'll have lots of questions:) It's a very basic question I have and everybody might look at this question and say, "Wow, she reallly doesn't get it?" But oh well. Here's my question: I'm in Chapter 2 of my Python Programming Third Editition book. I've gotten to the section of the chapter where it talks about a "newline character. Exactly what is a newline character? I keep reading the same section of the book and I'm just not really understanding. Here's the code that the book uses to explain the newline character: print("Here", end=" ") print("it is...") The book says that I can specify that a space be the final character printed instead of a newline. In the first print() statement I specify that a space be the final string printed. So the text "Here" appears but no newline character. The next print() statement prints the text "it is"..." right after the space following the final "e" in the "Here" text. You do it by passing a space to the end parameter of the print() function with the code end" ". My Operating System is Windows 7 and the Python version is 3.1.3. That's it. Thanks to anybody who responds to my question. -- *Ava Durand* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] nested loops
I am trying to write a function...(it's kind of like a long way to do BLAST but only comparing 2 sequences) I have 3 loops nested...Within those loops, I obtain a "best fit score" so to speak. I can get the program to run...but the loops run all the way to the end. I can't figure out how to print the best score found...and the alignment that went with it. (therefore, my results will only be correct if my last alignment is the highest scoring one--which it usually isn't) To try to clear this up... The part of my pseudocode that I'm having trouble putting into actual code in python is: "if that alignment has the best score seen so far save the score and that alignment" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] function help
ok...here's the function I've written so far. def padWithGaps(seq): for letter in seq: letter="-" line=len(seq) dashline=line*letter return dashline def replace(someString,position,letter): first=someString[0:position] second=letter third=someString[position+1:len(someString)] newString=first+second+third return newString ##findScore("MPFVS","MS-V-") would return a score of 2 def findScore(first,second): count=0 for position in range(0,len(first)): if first[position]==second[position]: count=count+1 position=position+1 return count #shorter is a 3 amino acid sequence ##longer is any length sequence greater than 3 ###i=first amino acid; j=second amino acid; k=third amino acid def findAlignment(shorter,longer): for i in range(0,len(longer)-2): for j in range(1,len(longer)-1): for k in range(2,len(longer)): dashline=padWithGaps(longer) nextLine=replace(dashline,i,shorter[0]) nextNext=replace(nextLine,j,shorter[1]) alignment=replace(nextNext,k,shorter[2]) score=findScore(longer,alignment) don't know what to do here print longer print alignment print "Score = " + str(score) I don't know what to do at the end of my loop but what I'm trying to do in pseudocode is: "if that alignment has the best score seen so far save the score and the alignment print the best score and the best alignment" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question
I am trying to complete an assignment and I am stuck at the if-else statements area. Could someone please help me? the instructions and what I have so far are below... Instructions: Your "main" function should do the following: (1) create an empty list; (2) ask the user if he/she wants to perform a list operation. if "yes": (a) prompt the user for the operation: "test", "peek", "add", or "remove"; (b) perform the operation: (i) if "test" then print whether the list is empty or not; (ii) if "peek" then print the number at the beginning of the list (with a label) but don't remove it from the list; (iii) if "add", then prompt the user for a number and add it to the end of the list; (iv) if "remove", then delete the number at the beginning of the list and print it (with a label); (c) print the entire list from beginning to end; repeat step (2) until the user enters "no". What I have so far.. def main(): l = list() x = eval(input('Enter a number: ')) while x >= 0: l.append(x) x = eval(input('Enter a number: ')) ask = input (" Do you want to perform a list operation?") if "yes": input (" Do you want to test, peek, add, or remove?") if "test": if not l: print("The list is not empty") else: print("The list is empty") elif "peek": print(l[0]) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question
From: tutor-bounces+afowler2=broncos.uncfsu@python.org [tutor-bounces+afowler2=broncos.uncfsu@python.org] on behalf of Alan Gauld [alan.ga...@btinternet.com] Sent: Thursday, August 23, 2012 5:59 PM To: tutor@python.org Subject: Re: [Tutor] Question On 23/08/12 18:02, Ashley Fowler wrote: > def main(): > l = list() > x = eval(input('Enter a number: ')) Don;t use eval() its bad practicecand fort advanced use only. Instead explicitly convert to int() or float() > while x >= 0: > l.append(x) > x = eval(input('Enter a number: ')) Same here. > ask = input (" Do you want to perform a list operation?") > if "yes": You need to test if ask is equal to 'yes' Testing 'yes' directly will always be true because 'yes' always exists. > input (" Do you want to test, peek, add, or remove?") you need to store the reurn value from input as you did above > if "test": and use that stored value in the test. > if not l: > print("The list is not empty") > else: > print("The list is empty") HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ __ REPLY: Thank you I finally fixed it. Can anyone break down how to do the next step which is: "You also need to write a function "printList" of one parameter that takes a list as its input and neatly prints the entire contents of the list in a column." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing a list as a column
Does anyone know how to print a list in a form of a column instead of a row? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing list in a Column
Can anyone help me edit this code below to return the list in the form of a column instead of a row? def printList(): list1 = input("Insert a list") list = [list1] print (list) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Scheme
This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the "pi" constant. Be sure to use "cube" from problem (1) to find the cube of r (r^3). Tests: (sphere 2) ==> 33.51 ... (sphere 5.3) ==> 623.61 ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
Understood. Disregard this problem. From: tutor-bounces+afowler2=broncos.uncfsu@python.org [tutor-bounces+afowler2=broncos.uncfsu@python.org] on behalf of Kal Sze [swordan...@gmail.com] Sent: Friday, August 31, 2012 1:32 AM To: tutor@python.org Subject: Re: [Tutor] Scheme And this looks like a homework problem, too. It is against etiquette to just ask for the solution to homework on ANY forum, message board, or mailing list. Since it's been given to you as homework, you're supposed to give it enough thoughts, and (hopefully) come up with your solution. Even when you go to the Lisp or Scheme mailing list, you should at least show what you have tried, paste your own code, and tell them where you are stuck. On 31 August 2012 07:32, Ashley Fowler mailto:afowl...@broncos.uncfsu.edu>> wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the "pi" constant. Be sure to use "cube" from problem (1) to find the cube of r (r^3). Tests: (sphere 2) ==> 33.51 ... (sphere 5.3) ==> 623.61 ... ___ Tutor maillist - Tutor@python.org<mailto:Tutor@python.org> To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scheme
yes From: tutor-bounces+afowler2=broncos.uncfsu@python.org [tutor-bounces+afowler2=broncos.uncfsu@python.org] on behalf of Mark Lawrence [breamore...@yahoo.co.uk] Sent: Friday, August 31, 2012 1:32 AM To: tutor@python.org Subject: Re: [Tutor] Scheme On 31/08/2012 00:32, Ashley Fowler wrote: > This is a problem using the Scheme programming...Can anybody help me with > this problem? > > > 2. Write a procedure (sphere r) that takes the radius of a sphere > as the value of its input parameter and returns the volume of that > sphere given by the formula: (4/3)π(r^3). Use (require scheme/math) > or (require racket/math) to load the math library containing the > "pi" constant. > Be sure to use "cube" from problem (1) to find the cube of r (r^3). > > Tests: > (sphere 2) ==> 33.51 ... > (sphere 5.3) ==> 623.61 ... > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Have I got this right? You're asking a question about the Scheme programming language on a Python mailing list, yes? -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Student Class
I need help creating a student class. I am suppose to create a student class with there first name, last name, credits and gpa. Can anybody help me get started? Below is what needs to be included. For number one, is the variables suppose to be parameters? for instance, class Student: def __init__(self, name, hours, gpa): self.name = name 1. four instance variables: firstName (a string), lastName (a string), numCredits (an integer), gpa (a float); 2. an accessor method for each instance variable; 3. a mutator method for each instance variable; 4. an __init__ method that sets all the instance variables to values provided by the user; 5. a __str__ method that returns a string that contains all the data of the object in the order: firstName, lastName, numCredits, gpa. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Student Class
so far i have: class Student: def __init__(self, first_name, last_name, numCredits, gpa): self.first_name = first_name self.last_name = last_name self.numCredits = numCredits self.gpa = gpa def getFirstname(self): return self.first_name def getLastname(self): return self.last_name def getNumCredits(self): return self.numCredits def getGpa(self): return self.gpa def setFirstname(self, first_name): self.first_name = first def setLastname(self, last_name): self.last_name = last def setNumcredits(self, numCredits): self.NumCredits = credit def setGpa(self, gpa): self.gpa = gpa def __str__(self): return (self.first_name, self.last_name, self.numCredits, self.gpa) From: Tutor [tutor-bounces+afowler2=broncos.uncfsu@python.org] on behalf of Alan Gauld [alan.ga...@btinternet.com] Sent: Wednesday, September 05, 2012 11:39 PM To: tutor@python.org Subject: Re: [Tutor] Student Class On 05/09/12 23:10, Ashley Fowler wrote: > I need help creating a student class. We don't do your homework for you we will only offer hints. So you need to show us what you are trying and we will try to steer you to something better. > class with there first name, last name, credits > and gpa. Can anybody help me get started? Below is what needs to be > included. For number one, is the variables > suppose to be parameters? Normally yes, you'd make those variables the input parameters to the __init__() method. > 1. four instance variables: firstName (a string), lastName (a string), > numCredits (an integer), gpa (a float); These would normally be defined inside __init__() > 2. an accessor method for each instance variable; > 3. a mutator method for each instance variable; This is not good Python practice. It smells like Java... In python it's customary to access attributes directly. If you really want to use access methods then you should create a 'property' > 4. an __init__ method that sets all the instance variables to values > provided by the user; > 5. a __str__ method that returns a string that contains all the data > of the object in the order: firstName, lastName, numCredits, gpa. These are fairly standard. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question
I have a question. In a assignment it asks for me to do the following below... if "peek" then print the Student object at the beginning of the list (using __str__) but don't remove it from the list; Could you explain what it means? This is what I have so far, elif ask == "peek": print("First Name In List =", names[0]) How would you make it a string? I know have a example of it but I'm not sure how to apply it to my code...The example is below. def __str__(self): return self.sides + "\t" + self.value Also below is part of the assignment and my whole code is in the attachment. A "main" function which does the following: (1) create an empty list; (2) ask the user if he/she wants to perform a list operation. if "yes": (a) prompt the user for the operation: "test", "peek", "add", or "remove"; (b) perform the operation: (i) if "test" then print whether the list is empty or not; (ii) if "peek" then print the Student object at the beginning of the list (using __str__) but don't remove it from the list; (iii) if "add", then prompt the user for student info, make a Student object containing that info, and add that Student object to the end of the list; (iv) if "remove", then delete the Student object at the beginning of the list and print it using __str__; repeat step (2) until the user enters "no"; (3) neatly print the entire list from beginning to end. StudentTester.py Description: StudentTester.py ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Constructing a object
How do you construct a object using variables? For instance I have to construct a student object, by first prompting the user to enter variables for the Student class (their first and last names, credits and gpa) then construct a Student object using those variables. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Print List
I am trying to complete the following below: You also need to write a function "printList" of one parameter that takes a list as its input and neatly prints the entire contents of the list in a column. Each student in the list should be printed using __str__. So far i have: def printList(lists): print("First Name\tLast Name\tCredits\tGPA") for i in lists: print (i) Any Suggestions or Corrections? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Str Method
Hello I am trying to add a str method to a Set ADT implementation to allow a user to print the contents of a set. However the resulting string should look like that of a list. except I am suppose to use curly brackets to surround the elements. For an example... >>> set1 = Set() >>> print(set1) {} Question is how do you implement the "curly brackets" in my str method? This is what I have so far... def __init__( self, *initElements ): self._theElements = list() def __str__(self): return self._theElements ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Drawing simple graphics objects
Hi, could you help me with python 3.5 coding for graphics? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Str method
I'm trying to initialize a list to several elements using the string method. I have some idea that you use a for loop I suppose... Could anybody help me out? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str method
I have code for a Set ADT. The Set ADT is a container that stores a collection of values or elements. Below is some of my code...I have run into two problems so far. First I have to modify __init__ method so that new sets can be initialized to a list of elements. This modification adds a starred parameter, *initElements. But somehow I still need to initialize a list to several elements and I have no clue what to do. The other problem is I had to add the str method to allow a user to print the contents of the set... meaning I must construct a string from the elements of the set one by one, and with curly braces at the beginning and at the end. class Set : def __init__( self, *initElements ): self._theElements = list() def __len__( self ): return len( self._theElements ) def __contains__( self, element ): return element in self._theElements def add( self, element ): if element not in self : self._theElements.append( element ) def remove( self, element ): assert element in self, "The element must be in the set." self._theElements.remove( item ) def __iter__( self ): return _SetIterator( self._theElements ) def __str__(self): for element in str(self._theElements): return element From: Oscar Benjamin [oscar.j.benja...@gmail.com] Sent: Sunday, November 04, 2012 8:52 PM To: Ashley Fowler Cc: tutor@python.org Subject: Re: [Tutor] Str method On 5 November 2012 01:45, Ashley Fowler wrote: > I'm trying to initialize a list to several elements using the string > method. I have some idea that you use a for loop I suppose... > > Could anybody help me out? I'm afraid not. Your question is too vague for anyone receiving your message to know what it is you are trying to do (I have no idea what "the string method" is). Can you please: 1) Post the code that you tried 2) Explain what output you were hoping for 3) Show the exact output that you got (if it is an error message then please post the entire exact error message). Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] I need help on this program please
This is what I have to do : On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion: A, B, AND C=2 D, E, AND F=3 G, H, AND I=4 J, K, AND L=5 M, N, AND O=6 P, Q, R, AND S=7 T, U, AND V=8 W, X, Y AND Z=9 Write a program that asks the user to enter a 10-character telephone numbers in the format XXX-XXX-. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the application should display 555-438-3663. def main(): digits = ["2", "3", "4", "5", "6", "7", "8", "9"] numeric_phone=" " phone_number = input('Enter 10-Digit phone number: ') ch= digits[index] for ch in phone_number: if ch.isalpha(): if ch == 'A' or ch == 'B' or ch == 'C': index = 0 if ch == 'D' or ch == 'E' or ch == 'F': index = 1 if ch == 'G' or ch == 'H' or ch == 'I': index = 2 if ch == 'J' or ch == 'K' or ch == 'L': index = 3 if ch == 'M' or ch == 'N' or ch == 'O': index = 4 if ch == 'P' or ch == 'Q' or ch == 'R' or ch == 'S': index = 5 if ch == 'T' or ch == 'U' or ch == 'V': index = 6 if ch == 'W' or ch == 'X' or ch == 'Y' or ch == 'Z': index = 7 numeric_phone= numberic_phone+ch print(numeric_phone) This is what I have so far. Can anyone make suggestions or tell me what I need to correct?___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor