Re: [Tutor] optimize a plot
On Wed, Nov 16, 2011 at 1:51 AM, Alan Gauld wrote: > On 15/11/11 14:34, lina wrote: >> >> Sorry for some not mature questions asked before, fixed now by: >> >> def PlotPathway(list1): >> >> for i in range(len(list1)): >> for j in range(len(list1[i])-1): >> if list1[i][j] != list1[i][j+1]: >> if ((int(list1[i][j]))< 43 and >> (int(list1[i][j-1]))< 43): >> g.add_edge(list1[i][j], list1[i][j+1]) >> for i in range(43,84): >> if g.has_node(i): >> g.delete_node(i) >> g.draw('graph4.png', prog="dot") >> >> >> just still don't get why the "if" does not work as expected. > > > Sorry, I haven't been following you earlier thread. My mistake, I attached a figure in earlier posting, but the message was held for approval due to it's too big. I just noticed those three notifications, so I canceled posting, not sure waiting for approval will get approved or not. > Which 'if'? There are 3 to choose from. > And how did you expect it to behave, and what is it doing that you didn't > expect? During those process, I met lots of problems, some are fixed, and some are forgot or switch to another problem. One thing is that I did not know how to get node value, and for the node value to change the attirbute of this certain node. This way is wrong: def PlotPathway(list1): for i in range(len(list1)): for j in range(len(list1[i])-1): if list1[i][j] != list1[i][j+1]: g.add_edge(list1[i][j], list1[i][j+1]) if list1[i][j]<=42: g.node_attr.update(color='deepskyblue',style='filled') if list1[i][j] > 42: g.node_attr.update(color='green',style='filled') it will keep on updating and mess things up, but this way can get the value. Below is a better way: def colorNodes(listofnodes): node_list=[] node_list_A=[] node_list_B=[] for node in g: node_list.append(node) if node.value < 42: ### I don't know how to get the value of the node. g.node_attr.update(color='deepskyblue',style='filled') else: g.node_attr.update(color='green',style='filled') > > -- > 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
Re: [Tutor] IndentationError:
The indentation is indeed off: Original code: def PlotPathway(list1): for i in range(len(list1)): for j in range(len(list1[i])-1): if list1[i][j] != list1[i][j+1]: g.add_edge(list1[i][j], list1[i][j+1]) if list1[i][j]<=42: g.node_attr.update(color='deepskyblue',style='filled') if list1[i][j] > 42: g.node_attr.update(color='green',style='filled') What I think you meant: def PlotPathway(list1): for i in range(len(list1)): for j in range(len(list1[i])-1): if list1[i][j] != list1[i][j+1]: g.add_edge(list1[i][j], list1[i][j+1]) if list1[i][j]<=42: g.node_attr.update(color='deepskyblue',style='filled') if list1[i][j] > 42: g.node_attr.update(color='green',style='filled') Notice that I *consistently* use 4 spaces, and *only spaces, not tabs,* for each indentation level. In your code (assuming the copy paste I did was correct) I could see a mixture in the number of spaces for each indentation level. The error was the python interpreted the second and third if statements as being not properly indented, becuase of the lack of consitency: 1. they did not align with the first if statement in side the for loop 2. the did not align with the for-loop either, so there could not be intrepreted as being on the same level as the for loop. Hope this makes sense and helps! Best regards, /dario On Wed, Nov 16, 2011 at 8:52 AM, lina wrote: > Why it keeps on complaining: > > $ python plot-pathway.py > File "plot-pathway.py", line 35 >if list1[i][j]<=42: > ^ > IndentationError: unindent does not match any outer indentation level > > > def PlotPathway(list1): >for i in range(len(list1)): >for j in range(len(list1[i])-1): >if list1[i][j] != list1[i][j+1]: >g.add_edge(list1[i][j], list1[i][j+1]) > >if list1[i][j]<=42: >g.node_attr.update(color='deepskyblue',style='filled') >if list1[i][j] > 42: >g.node_attr.update(color='green',style='filled') > > I checked the indentation very carefully, seems no problems. > > really no clue, > > Thanks with best regards, > ___ > 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
Re: [Tutor] IndentationError:
On Wed, Nov 16, 2011 at 4:06 PM, Dario Lopez-Kästen wrote: > The indentation is indeed off: > Original code: > def PlotPathway(list1): > for i in range(len(list1)): > for j in range(len(list1[i])-1): > if list1[i][j] != list1[i][j+1]: > g.add_edge(list1[i][j], list1[i][j+1]) > > if list1[i][j]<=42: > g.node_attr.update(color='deepskyblue',style='filled') > if list1[i][j] > 42: > g.node_attr.update(color='green',style='filled') > > What I think you meant: > def PlotPathway(list1): > for i in range(len(list1)): > for j in range(len(list1[i])-1): > if list1[i][j] != list1[i][j+1]: > g.add_edge(list1[i][j], list1[i][j+1]) > > if list1[i][j]<=42: > g.node_attr.update(color='deepskyblue',style='filled') > if list1[i][j] > 42: > g.node_attr.update(color='green',style='filled') > > > Notice that I consistently use 4 spaces, and only spaces, not tabs, for each > indentation level. In your code (assuming the copy paste I did was correct) > I could see a mixture in the number of spaces for each indentation level. > The error was the python interpreted the second and third if statements as > being not properly indented, becuase of the lack of consitency: > > they did not align with the first if statement in side the for loop > the did not align with the for-loop either, so there could not be > intrepreted as being on the same level as the for loop. Yes, it's lack of consistency, sorry, I thought I posted something like: if list1[i][j] != list1[i][j+1]: g.add_edge(list1[i][j], list1[i][j+1]) if list1[i][j]<=42: g.node_attr.update(color='deepskyblue',style='filled') if list1[i][j] > 42: g.node_attr.update(color='green',style='filled') The second and third if are inside the first if, it still complaining. I set the gedit, use space not tab, Now I even typed space by space, avoid using tab, it still has the same problem. How can I fixed it? I put the two files in below links: https://docs.google.com/open?id=0B93SVRfpVVg3NzlkMzVmOWYtNDk4MS00Yzk1LWEwMWQtYzIzMWU0Y2M2NmUz https://docs.google.com/open?id=0B93SVRfpVVg3ZDhiZjM1ZGItZDU0Ny00MDhhLThjZDQtYmRjMWJkMmVkNTk5 > > Hope this makes sense and helps! > Best regards, > /dario > On Wed, Nov 16, 2011 at 8:52 AM, lina wrote: >> >> Why it keeps on complaining: >> >> $ python plot-pathway.py >> File "plot-pathway.py", line 35 >> if list1[i][j]<=42: >> ^ >> IndentationError: unindent does not match any outer indentation level >> >> >> def PlotPathway(list1): >> for i in range(len(list1)): >> for j in range(len(list1[i])-1): >> if list1[i][j] != list1[i][j+1]: >> g.add_edge(list1[i][j], list1[i][j+1]) >> >> if list1[i][j]<=42: >> g.node_attr.update(color='deepskyblue',style='filled') >> if list1[i][j] > 42: >> g.node_attr.update(color='green',style='filled') >> >> I checked the indentation very carefully, seems no problems. >> >> really no clue, >> >> Thanks with best regards, Thanks again, >> ___ >> 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
Re: [Tutor] IndentationError:
On Wed, Nov 16, 2011 at 9:21 AM, lina wrote: > it still complaining. I set the gedit, use space not tab, > Now I even typed space by space, avoid using tab, it still has the same > problem. > > How can I fixed it? > The line if list1[i][j] != list1[i][j+1]: still contains a tab, thus it is now indented negative 3 spaces plus a tab plus 3 spaces compared to the encompassing loop, which is problematic both because there is a negative number in there and because it differs from the rest of the lines in the same loop (6 spaces) -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] binary file query
If we are opening a binary file in python then do we have to use "Import struct" and do "struct.unpack" to work on the data from that binary file? Will we be able to process the file as text file without using struct? Thanks, Dhanashri ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Bulk Import Script Help Please
> Hi, > > > > I am new to python, and I need to bulk import a csv data dump into our > content management system, Equella. > > > > I need to bulk import the CSV file dump into Equella to create a Taxonomy > using a Python script. > > > > I would really appreciate any help someone can provide me, or get me started > with the script. > > > > I have the csv file, the API details for bulk import and Admin access to the > Equella Taxonomy console; > > > > If you require more specific or further info, please don't hesitate to > contact me. > > > > Regards > > Amir > > > > Amir Rana > > Mob: 07843193398 > > E-mail: gags...@yahoo.com > > Please Consider the Environment before Printing > > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndentationError:
On Wed, Nov 16, 2011 at 4:31 PM, Andre Engels wrote: > On Wed, Nov 16, 2011 at 9:21 AM, lina wrote: >> >> it still complaining. I set the gedit, use space not tab, >> Now I even typed space by space, avoid using tab, it still has the same >> problem. >> >> How can I fixed it? > > The line > > if list1[i][j] != list1[i][j+1]: > > still contains a tab, thus it is now indented negative 3 spaces plus a tab > plus 3 spaces compared to the encompassing loop, which is problematic both > because there is a negative number in there and because it differs from the > rest of the lines in the same loop (6 spaces) > Thanks, fixed, seems my gedit has problem? the preference setting using space, not Tab, really headache about it, sometimes. Thanks again, > -- > André Engels, andreeng...@gmail.com > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Running windows media player
Hello friends, I need to run mp3/ogg files with Windows Media Player from a python script. I have the following code which only can start Windows Media Player. But I dont know how to run a mp3/ogg file with this instance of Windows Media Player: from pywinauto import application app = application.Application() try: wmp = app.start_( # connect_(path = ur"C:\Program Files\Windows Media Player\wmplayer.exe") except application.ProcessNotFoundError: print "Error Message " Can someone help me? Br Mahbub -- View this message in context: http://old.nabble.com/Running-windows-media-player-tp32845242p32845242.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running windows media player
Op 15-11-11 04:29, MD.Mahbubur Rahman schreef: Hello friends, I need to run mp3/ogg files with Windows Media Player from a python script. I have the following code which only can start Windows Media Player. But I dont know how to run a mp3/ogg file with this instance of Windows Media Player: I don't have Windows, so can't test, but according to this Microsoft page [1], you can give some comandline parameters to WMP. Cheers, Timo [1] http://support.microsoft.com/kb/241422/en-us from pywinauto import application app = application.Application() try: wmp = app.start_( # connect_(path = ur"C:\Program Files\Windows Media Player\wmplayer.exe") except application.ProcessNotFoundError: print "Error Message " Can someone help me? Br Mahbub ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndentationError:
Op 16-11-11 09:46, lina schreef: On Wed, Nov 16, 2011 at 4:31 PM, Andre Engels wrote: On Wed, Nov 16, 2011 at 9:21 AM, lina wrote: it still complaining. I set the gedit, use space not tab, Now I even typed space by space, avoid using tab, it still has the same problem. How can I fixed it? The line if list1[i][j] != list1[i][j+1]: still contains a tab, thus it is now indented negative 3 spaces plus a tab plus 3 spaces compared to the encompassing loop, which is problematic both because there is a negative number in there and because it differs from the rest of the lines in the same loop (6 spaces) Thanks, fixed, seems my gedit has problem? the preference setting using space, not Tab, really headache about it, sometimes. Gedit won't alter existing tabs when adjusting that setting. So that tab was probably already there, or you copy-pasted it from some other source. Cheers, Timo Thanks again, -- André Engels, andreeng...@gmail.com ___ 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
Re: [Tutor] binary file query
On 14/11/11 05:34, Shirkhedkar, Dhanashri wrote: If we are opening a binary file in python then do we have to use “Import struct” and do “struct.unpack” to work on the data from that binary file? That depends but in general the answer is no. But it all depends on what is in the file. If it is a known format (like Pickle say) there may well be a higher level module that will interopret the file for you. But fundamentally, opening a file in binary mode means you are reading the raw bytes from the file and the interpretation of those bytes is entirely your responsibility. Will we be able to process the file as text file without using struct? Only if the bytes represent text. You can certainly treat the bytes as if they were text and interesting things may happen. Whether the text interpretation bears any resemblance to the original intent of the files author is another matter all together. The key thing in dealing with binary files is that you must know what the bytes you are reading represent (or be willing to reverse engineer it!). If an existing interpreter (eg pickle, PIL, gzip etc) exists that will make life easier. If not you can either read the bytes into a list and process them manually. Or, if you know what they represent, you can use struct to convert them into appropriate Python data objects. The choice is yours and depends on many factors. -- 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
Re: [Tutor] Bulk Import Script Help Please
On 10/11/11 17:10, Amir wrote: I am new to python, and I need to bulk import a csv data dump into our content management system, Equella. OK, we can definitely help with the Python, the Equella bit will likely need a more specialised forum. What is your background? Can you already program in any other language? If so, which? What OS are you using? What Python version do you have? What tutorial are you following, if any? All of these answers can help us focus our responses. I need to bulk import the CSV file dump into Equella to create a Taxonomy using a Python script. OK, That needs more explanation too. What do you mean by a csv file dump? One large file or a folder full of files or a zip/tar file full of csvb files? And what kind of taxonomy do you mean? Is it one containing the data from the csv files or is it a taxonomy of csv files? And what is the taxonomy structure based on - is it part of Equilla? Or are you using a 3rd party tool, if so which? I have the csv file, the API details for bulk import and Admin access to the Equella Taxonomy console; OK, this suggests a single csv file, an API into Equilla and that the taxonomy is part of Equllla? Is that correct? If so the first thing to do is learn how to open and read the csv file. You should be able to do that easily using the csv module in the standard Python library. Is that enough of a starter? If not what extra information do you need? The more specific you make your questions, the more specific we can make our answers. -- 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] Thank you!
Thank you to all for your help on my SQL DDL parsing script, and thank you Alan for having this wonderful list. I got the program to work and verified the DDL is being parsed correctly. I've been working as a database administrator for many years, but before that I programmed on the mainframe. I'm used to COBOL, SQL, DB2, and latley XML. I've done some Unix scripting and VB script, taught myself some basic C and C++, with some odd Visual Basic along the way, but Python is enitirely new to me. That's why I got thrown off with the output not lining up in Notepad - I've never written anything before that could be displayed in a mode that used proportional fonts. The things I like about Python: - the forced indentation makes for really readable code - it's free! - it can be used on any platform - it can be used for scripting or for builing a stand alone program - it can work in any programming style (O-O, sturctured, etc.) I'm going to be doing a presentation on Python programming for database administrators. This script will help a lot to explain some of the basic concepts. Thanks again very much for your help. Python rocks! -- Frank L. "Cranky Frankie" Palmeri Risible Riding Raconteur & Writer “How you do anything is how you do everything.” - from Alabama Crimson Tide training room ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list of objects?
Please always reply-all so a copy goes to the list. Am forwarding this on your behalf. I will reply this afternoon. On 11/16/2011 7:18 AM, Elwin Estle wrote: Part of the reason I am writing this program is to learn OOP, hence the use of classes. Most of what little programming background I have comes from working with various flavors of BASIC on old Atari 8-bit computers back in the day, and more recently a little Tcl/Tk, so my first instinct is to do everything from a procedural standpoint. I'm trying to get out of my comfort zone. As for the game, I am doing the 'easy' version of spider that uses 8 suits of spades. I was planing to just re-draw the display after each move. Input-wise, I was just going to use raw_input. My idea was to have a move consist of a three character coordinate, The 10 dealt piles would be named by letter, A-J, and the cards in each pile would be by number, i.e. 1-nn. A move would consist of the starting pile, the number of the card in that pile, and the letter of the destination pile, so 'B5H' would move everything from the 5th card down in pile B and put it on pile H. *From:* bob gailer *To:* Elwin Estle *Cc:* "tutor@python.org" *Sent:* Tuesday, November 15, 2011 9:18 PM *Subject:* Re: [Tutor] list of objects? On 11/15/2011 8:40 AM, Elwin Estle wrote: I am attempting to write a text based spider solitaire game. What are the rules of your version of Spider? The only spiders I know have 10 dealt piles and 1 draw pile. I think you have greatly complicated things by using classes. Consider: deck = random.shuffle(range(13)*8) # list of 108 card "indexes" in random order. values = "A23456789JQK" # display values corresponding to indexes piles = [deck[x:x+10] for x in range(0,108,10)] Anytime you want to display the value of a card use values[cardIndex] for example to display the last card in each pile: for pile in piles: print values[pile[-1]], What will your actual display look llike? Will you completely reprint it after each move, or alter it in place? How will you get the moves from the player? -- Bob Gailer 919-636-4239 Chapel Hill NC -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
Hi! It is no homework, in fact am I working ahead of class. I have now, after five minutes thinking, solved my problem, but a new problem has risen. But to begin with, here is how I solved the problem: from tkinter import* import time the_time='' class Window(Frame): def __init__(self,master): super(Window,self).__init__(master) self.grid() self.create_widgets() def create_widgets(self): #Create a hello button: hej=self.hellobttn=Button(self,text="Hey") self.hellobttn.grid(row=0, column=0) #Create a label that displays time: self.display_time=Label(self, text=the_time) self.display_time.grid(row=0, column=1) def change_value_the_time(): global the_time newtime = time.strftime('%H:%M:%S') if newtime != the_time: the_time= newtime self.display_time.config(text=the_time, font="40") self.display_time.after(20, change_value_the_time) change_value_the_time() root=Tk() root.title("Test") root.geometry("200x200") app=Window(root) root.mainloop() I found some help on the internet about making a clock, although I had to modify a lot of the code to fit my own code and window. Now to my next question. Say that I want a text “Hi, how are you?” to be printed when the time passes 15:00:00 each day. How do I do that? At first I thought that I could just write an if statement. Like: if the_time>15:00:00: print (“Hi, how are you?”) But it is obviously not working. Thank you for your help! Another question, am I supposed to add tutor@python.orgtu...@python.org; as copy? You did that, right? Regards Mic From: Wayne Werner Sent: Tuesday, November 15, 2011 10:14 PM To: Mic Cc: tutor@python.org Subject: Re: [Tutor] Clock in tkinter? On Tue, Nov 15, 2011 at 2:00 PM, Mic wrote: Hi! I am new to programming and I hop this question isn’t stupid. Welcome! I am making a small GUI program. It is supposed to have a button and a clock in it that displays the same time as it is according to the computer. So as far as I am concerned both the clock and the are supposed to be widgets? Is this a homework assignment, or just something that you're doing for fun? It seems homework-ish. We don't mind pointing you in the right direction on homework assignments, but we definitely won't do it for you. So how do I add this clock as a widget placed next to the button? Tkinter doesn't have a native clock widget, so if you want to make a clock you need to roll your own. Here is the code I have written so far, that only displays a GUI window, and a button: from tkinter import * import time class Window(Frame): def __init__(self,master): super(Window,self).__init__(master) self.grid() self.create_widgets() def create_widgets(self): self.test_button=Button(self, text="Hi") self.test_button.grid(row=0,column=0) root=Tk() root.title("Test") root.geometry("200x200") app=Window(root) root.mainloop() Giving us code (especially such a small amount) is exactly the right thing to do when you ask a question - it shows that you've tried something, and if it's broken it usually shows why it's broken. You can add text to the Label widget - and you can change the text on that widget. You're already importing the time module - for more information about what it contains, you can run the interactive interpreter and do this: >>> import time >>> help(time) Or you can look online for the commands that might help you. If you get stuck, let us know what you're doing and where you're stuck at. HTH, Wayne___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
Thank you Yes, wise statement. Mic From: Asokan Pichai Sent: Wednesday, November 16, 2011 5:17 AM To: Mic Cc: tutor@python.org Subject: Re: [Tutor] Clock in tkinter? > On Wed, Nov 16, 2011 at 1:30 AM, Mic wrote: > Hi! > I am new to programming WELCOME! >and I hop this question isn’t stupid. A teacher of mine said, "The only stupid question is the one you thought of asking but did not ask!" Happy learning Asokan Pichai <>___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] modulus
Please can anyone tell me how i can print this without all the brackets and commas, i know i need the modulus symbol but i dont know how it works. any advice would be appreciated regards adrian def arguments(): name=raw_input ("Please enter your firstname: ") surname=raw_input ("Enter your surname: ") address1=raw_input ("Enter Address line 1: ") address2=raw_input ("Enter Address line 2: ") address3=raw_input ("Enter Address line 3: ") return name,surname,address1,address2,address3 address=arguments() print "%s %s" % address ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
On Wed, Nov 16, 2011 at 9:46 AM, ADRIAN KELLY wrote: > Please can anyone tell me how i can print this without all the brackets > and commas, i know i need the modulus symbol but i dont know how it works. > any advice would be appreciated > > regards > adrian > > def arguments(): > name=raw_input ("Please enter your firstname: ") > surname=raw_input ("Enter your surname: ") > address1=raw_input ("Enter Address line 1: ") > address2=raw_input ("Enter Address line 2: ") > address3=raw_input ("Enter Address line 3: ") > return name,surname,address1,address2,address3 > address=arguments() > print "%s %s" % address > In this case it's not actually modulus, it's just the syntax for string formatting. I'm not sure *what* the reasoning behind the % was, but that's the way it is. There are two ways to do string formatting, the new (.format) and old (%). In new style formatting you use the .format method of the string: "{0} {1} {2}".format("One", 2, "Five") You don't usually have to worry about the type, though you can specify it along with some other useful modifiers for precision, spacing, and alignment. In old style formatting, you use a string with format specifiers (%s, %d, etc.) followed by a tuple of arguments. Here, the lengths have to match exactly - if you have one specifier then you must have a 1-element tuple. In your case, you're returning a 5 element tuple, so you want 5 format specifiers: print "%s %s %s %s %s" % address However, if you just want to print the data out like that you can do it a little easier like this: print ' '.join(address) Or if you are in 3.x or use `from __future__ import print_function` then you can do this: print(*address) HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
ADRIAN KELLY wrote: Please can anyone tell me how i can print this without all the brackets and commas, i know i need the modulus symbol but i dont know how it works. any advice would be appreciated regards adrian def arguments(): name=raw_input ("Please enter your firstname: ") surname=raw_input ("Enter your surname: ") address1=raw_input ("Enter Address line 1: ") address2=raw_input ("Enter Address line 2: ") address3=raw_input ("Enter Address line 3: ") return name,surname,address1,address2,address3 address=arguments() This line is misleading. arguments() does not return an address. It returns a name, a surname, and three address lines. print "%s %s" % address Try one of these instead: # Version 1 name,surname,address1,address2,address3 = arguments() print name print surname print address1 print address2 print address3 # Version 2 items = arguments() for item in items: print item # Version 3 items = arguments() print "%s %s %s %s %s" % items -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
Wayne Werner wrote: In this case it's not actually modulus, it's just the syntax for string formatting. I'm not sure *what* the reasoning behind the % was, but that's the way it is. I believe the designers of the C programming language are to blame. [...] In old style formatting, you use a string with format specifiers (%s, %d, etc.) followed by a tuple of arguments. Here, the lengths have to match exactly - if you have one specifier then you must have a 1-element tuple. That's actually wrong. If you have one specifier, you must have one object of any sort *except* a tuple. >>> "%s" % 42 # One object not a tuple. '42' But if you have a tuple, the % formatting will try to use each element in the tuple separately: >>> "%s" % (23, 42) # One object which is a tuple Traceback (most recent call last): File "", line 1, in TypeError: not all arguments converted during string formatting So if you actually want to use a tuple as the object, you need to wrap it in a single item tuple: >>> "%s" % ((23, 42),) # Tuple inside a tuple. '(23, 42)' -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
really appreciate that answer thanks very much.. Adrian Kelly 1 Bramble Close Baylough Athlone County Westmeath 0879495663 From: waynejwer...@gmail.com Date: Wed, 16 Nov 2011 09:59:50 -0600 Subject: Re: [Tutor] modulus To: kellyadr...@hotmail.com CC: tutor@python.org On Wed, Nov 16, 2011 at 9:46 AM, ADRIAN KELLY wrote: Please can anyone tell me how i can print this without all the brackets and commas, i know i need the modulus symbol but i dont know how it works. any advice would be appreciated regards adrian def arguments(): name=raw_input ("Please enter your firstname: ") surname=raw_input ("Enter your surname: ") address1=raw_input ("Enter Address line 1: ") address2=raw_input ("Enter Address line 2: ") address3=raw_input ("Enter Address line 3: ") return name,surname,address1,address2,address3 address=arguments() print "%s %s" % address In this case it's not actually modulus, it's just the syntax for string formatting. I'm not sure *what* the reasoning behind the % was, but that's the way it is. There are two ways to do string formatting, the new (.format) and old (%). In new style formatting you use the .format method of the string: "{0} {1} {2}".format("One", 2, "Five") You don't usually have to worry about the type, though you can specify it along with some other useful modifiers for precision, spacing, and alignment. In old style formatting, you use a string with format specifiers (%s, %d, etc.) followed by a tuple of arguments. Here, the lengths have to match exactly - if you have one specifier then you must have a 1-element tuple. In your case, you're returning a 5 element tuple, so you want 5 format specifiers: print "%s %s %s %s %s" % address However, if you just want to print the data out like that you can do it a little easier like this: print ' '.join(address) Or if you are in 3.x or use `from __future__ import print_function` then you can do this: print(*address) HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
On Wed, Nov 16, 2011 at 9:28 AM, Mic wrote: > Hi! > It is no homework, in fact am I working ahead of class. I have now, after > five minutes thinking, solved my problem, but a new problem has risen. > But to begin with, here is how I solved the problem: > > > > from tkinter import* > import time > > the_time='' > > > > class Window(Frame): > def __init__(self,master): > super(Window,self).__init__(master) > self.grid() > self.create_widgets() > > > def create_widgets(self): > > #Create a hello button: > hej=self.hellobttn=Button(self,text="Hey") > self.hellobttn.grid(row=0, column=0) > > #Create a label that displays time: > self.display_time=Label(self, text=the_time) > self.display_time.grid(row=0, column=1) > > def change_value_the_time(): > global the_time > newtime = time.strftime('%H:%M:%S') > if newtime != the_time: > the_time= newtime > self.display_time.config(text=the_time, font="40") > self.display_time.after(20, change_value_the_time) > If you're going to put a function inside your class (since you're using self in there, I'm sure that's what you meant to do), you should change it to: def change_value_the_time(self): and call it with self.display_time.after(20, self.change_value_the_time) But your program also has unnecessary code. First, since you have a class then instead of using a global, you should simply declare `self.the_time = ''` in your __init__ for your class. Personally, I would have written the function more like this: def update_time(self): self.display_time.config(text=time.strftime('%H:%M:%S'), font='40') self.after(20, self.update_time) Then at the end of my __init__ function I would call self.update_time() I'm not sure how that will work with your current setup, though. I found some help on the internet about making a clock, although I had to > modify a lot of the code to fit my own code and window. > Now to my next question. Say that I want a text “Hi, how are you?” to be > printed when the time passes 15:00:00 each day. How do I do that? > > At first I thought that I could just write an if statement. Like: > > if the_time>15:00:00: > print (“Hi, how are you?”) > > But it is obviously not working. > You can write one very similar to that. Take a look at the time.localtime method. > > Thank you for your help! Another question, am I supposed to add > tutor@python.org; as copy? You did that, right? > Yes - if you click "reply to all" in your email client, that will automatically send it to tutor@python.org HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
On Wed, Nov 16, 2011 at 10:09 AM, Steven D'Aprano wrote: > Wayne Werner wrote: > > >> In old style formatting, you use a string with format specifiers (%s, %d, >> etc.) followed by a tuple of arguments. Here, the lengths have to match >> exactly - if you have one specifier then you must have a 1-element tuple. >> > > That's actually wrong. If you have one specifier, you must have one object > of any sort *except* a tuple. I think you misunderstood - I said a 1-element tuple - e.g. (3,) > >>> "%s" % 42 # One object not a tuple. > '42' > > But if you have a tuple, the % formatting will try to use each element in > the tuple separately: > > >>> "%s" % (23, 42) # One object which is a tuple > As above, that's a two-element tuple. It was explained to me once that in this case: "%s" % 42 That since python expects to see a single-element tuple it treats it as or converts 42 to a single element tuple. I suppose I could have called it a tuple of length 1 instead. Sorry for the mistunderstanding, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
Thanks for your quick answer. Hmm, I never thought that didn’t have to use a global variable. When you mentioned time.localtime() method, did you mean that I should convert the time to numbers and then write an if statement? Like, say that I make 1 hour equivalent to the integear clock= 1. Then I write an if statement that says that the computer should print “Hi” if clock>1: print (“hi”) Did I understand that correctly? Thanks for your answers! Mic From: Wayne Werner Sent: Wednesday, November 16, 2011 5:17 PM To: Mic Cc: tutor@python.org Subject: Re: [Tutor] Clock in tkinter? On Wed, Nov 16, 2011 at 9:28 AM, Mic wrote: Hi! It is no homework, in fact am I working ahead of class. I have now, after five minutes thinking, solved my problem, but a new problem has risen. But to begin with, here is how I solved the problem: from tkinter import* import time the_time='' class Window(Frame): def __init__(self,master): super(Window,self).__init__(master) self.grid() self.create_widgets() def create_widgets(self): #Create a hello button: hej=self.hellobttn=Button(self,text="Hey") self.hellobttn.grid(row=0, column=0) #Create a label that displays time: self.display_time=Label(self, text=the_time) self.display_time.grid(row=0, column=1) def change_value_the_time(): global the_time newtime = time.strftime('%H:%M:%S') if newtime != the_time: the_time= newtime self.display_time.config(text=the_time, font="40") self.display_time.after(20, change_value_the_time) If you're going to put a function inside your class (since you're using self in there, I'm sure that's what you meant to do), you should change it to: def change_value_the_time(self): and call it with self.display_time.after(20, self.change_value_the_time) But your program also has unnecessary code. First, since you have a class then instead of using a global, you should simply declare `self.the_time = ''` in your __init__ for your class. Personally, I would have written the function more like this: def update_time(self): self.display_time.config(text=time.strftime('%H:%M:%S'), font='40') self.after(20, self.update_time) Then at the end of my __init__ function I would call self.update_time() I'm not sure how that will work with your current setup, though. I found some help on the internet about making a clock, although I had to modify a lot of the code to fit my own code and window. Now to my next question. Say that I want a text “Hi, how are you?” to be printed when the time passes 15:00:00 each day. How do I do that? At first I thought that I could just write an if statement. Like: if the_time>15:00:00: print (“Hi, how are you?”) But it is obviously not working. You can write one very similar to that. Take a look at the time.localtime method. Thank you for your help! Another question, am I supposed to add tutor@python.org; as copy? You did that, right? Yes - if you click "reply to all" in your email client, that will automatically send it to tutor@python.org HTH, Wayne___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] binary file query
>If we are opening a binary file in python then do we have to use "Import >struct" and do "struct.unpack" to work on the data from that binary file? >Will we be able to process the file as text file without using struct? You should open the file with the 'rb' parameter and then read from it as you need to. with open( 'filename.ext', 'rb' ) as f: # python 2.6+ syntax f.read( [size] ) # OR f.readline( [size] ) http://docs.python.org/tutorial/inputoutput.html Start from section 7.2 Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
On Wed, Nov 16, 2011 at 10:59 AM, Mic wrote: > Thanks for your quick answer. Hmm, I never thought that didn’t have to > use a global variable. > That's often the point of using classes - you want to collect related data and functions in one place. In this case, you're creating a clock widget, so you want all of your clock data and methods inside your class. > When you mentioned time.localtime() method, did you mean that I should > convert the time to numbers and then write an if statement? > Like, say that I make 1 hour equivalent to the integear clock= 1. Then I > write an if statement that says that the computer should print “Hi” > if clock>1: > print (“hi”) > Take a look at what's available from the localtime: >>> help(time.localtime) and >>> help(time.localtime()) You should be able to figure out a way to make it work pretty much like what your initial thought was. As an aside, you've been top-posting, which can cause a lot of problems for people who want to follow the thread of the conversation - all they see is what you've written with no context. The appropriate thing to do is reply inline, removing any irrelevant material and (usually) indicating that you've done so with a or [...] or something similiar. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
Okay, that is good to know. Yes, I here is how I solved my problem: def print_hi_15_00(): time1=int(time.strftime('%H')) if time1>15: print("hi") That worked fine. However, if the time is say 15, I want it print 16 instead. And if the time is 16, I want it to print 17 instead. Here is how I thought to solve it. time2=time1 + 1 print(time2) ...And apparently it worked now, when I tested it. It didn’t earlier... I wonder if you have any suggestions regarding how to place widgets in my window. Even though I try to arrange them as orderly as possible they get placed at all different kind of places in the window. I use the grid method and place them in the window by using row=something and column=something. It just doesn’t look good. It looks okay I suppose, but not as good as I want it to look. I must admit though that I did not understand what you wrote earlier, refering to the fact that I don’t need global variables. I tried your suggestion of the function but didn’t get it to work. So if I got it right. Inside the constructor (__init__) I should declare: self.the_time=’’ Then, if I understood it right, I should also place the function call there? self.update_time() as you refered to? Oh, by top-posting you mean that I include everything I have written before in my post, and that it is no good to do? So I just post what I wrote, and nothing from earlier on in this conversation? Mic ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clock in tkinter?
Top-posting is what I'm doing here. Putting my remarks BEFORE the part I'm quoting. On 11/16/2011 12:44 PM, Mic wrote: Oh, by top-posting you mean that I include everything I have written before in my post, and that it is no good to do? So I just post what I wrote, and nothing from earlier on in this conversation? This is where the new remarks should go. Just after the part you're responding to. And since I'm not commenting on anything else, I snipped it out. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple threads
(You're top-posting. Put your remarks AFTER what you're quoting) On 11/16/2011 12:52 PM, Jack Keegan wrote: Ok, I thought that processes would do the same job as threads. So would the general rule be some thing like so: If I want another piece of work to run (theoretically) along side my main script, and I want to share data between them, I should use a thread and share data with the thread-safe queue. If the work I want done can function and complete on its own, go for a process. Would that be about right? Yes, with all the caveats I mentioned before. With some language implementations, and with some operating systems, and on some CPU-systems, the guidelines could be different. They all trade off in ways too complex to describe here. For example, if a thread is mostly doing I/O, it may be just as efficient as a separate process, even if sharing data isn't an issue. And in some languages, sharing data between processes isn't all that tough, either. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Thank you!
On 16/11/11 13:50, Cranky Frankie wrote: Thank you to all for your help on my SQL DDL parsing script, and thank you Alan for having this wonderful list. Not much to do with me I'm just one the moderators who happens to flush the admin queue occasionally! :-) before that I programmed on the mainframe. I'm used to COBOL, SQL, DB2, and latley XML. I've done some Unix scripting and VB script, taught myself some basic C and C++, with some odd Visual Basic along the way, but Python is enitirely new to me. That's why I got thrown off with the output not lining up in Notepad - I've never written anything before that could be displayed in a mode that used proportional fonts. I don't believe you! :-) What you probably mean is that you haven't had a display tool that could use proportional fonts (or at least never used them!) You will hit the same problem using some Winsdows VT200 emulators, or even 3270 emulators for the mainframe if they support proportional fonts. (But never, ever try working a 3270 with proportional fonts!) You could take some plain text produced by one of your programs 20 years ago and Notepad, with the wrong font, would still cause misalignment. It's nothing to do with the output but all to do with the display tool. And its by no means a new problem, its been around since the first graphical, bitmapped, displays appeared in the 1980's... -- 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
Re: [Tutor] Clock in tkinter?
On Wed, Nov 16, 2011 at 11:44 AM, Mic wrote: > I wonder if you have any suggestions regarding how to place widgets in > my window. > Even though I try to arrange them as orderly as possible they get placed > at all different kind of places in the window. > I use the grid method and place them in the window by using row=something > and column=something. > > It just doesn’t look good. It looks okay I suppose, but not as good as I > want it to look. > http://twitpic.com/2t4n9q/full http://twitpic.com/2t4n9p/full Those images might help you understand a little bit more about the grid layout. > So if I got it right. Inside the constructor (__init__) I should declare: > self.the_time=’’ > Then, if I understood it right, I should also place the function call > there? > self.update_time() as you refered to? > Your constructor should look something like this: def __init__(self): #Your call to super self.the_time = '' # your other code self.update_time() I don't know for sure that it will work, but I expect that it will. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tutor Ilde running problems
Hi all, Just installed python 2.7.2 on my windows 7 32-bit laptop. However when I attempt to run idle it wont open (nothing happens) Wondered if any of you have had this problem and have any tips? Cheers> ___ > 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
Re: [Tutor] Tutor Ilde running problems
On Wed, Nov 16, 2011 at 1:41 PM, Nidian Job-Smith wrote: > Hi all, > > Just installed python 2.7.2 on my windows 7 32-bit laptop. However > when I attempt to run idle it wont open (nothing happens) > > Wondered if any of you have had this problem and have any tips? > This is more of a Windows issue, but I'll give it a go: I would try opening the command prompt (type cmd in the start menu) and typing the following at the prompt: c:\Python27\python.exe c:\Python27\lib\idlelib\idle.pyw If you installed Python somewhere else, replace Python27 with the directory you installed it to. Copy and paste the output of that command - you might need to click on the icon in the top left of the window and there's an edit option that should help you out. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] format integer to currency
print ("i own {0:.2f} {1}".format(1.1,"million")) can anyone help me with code to change the format of this to currency €1.10 million thanks for your help ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] format integer to currency
ADRIAN KELLY wrote: print ("i own {0:.2f} {1}".format(1.1,"million")) can anyone help me with code to change the format of this to currency €1.10 million thanks for your help # Python 2.6 or 2.7 print (u"I own €{0:.2f} {1}".format(1.1, "million")) # Python 3 print ("I own €{0:.2f} {1}".format(1.1, "million")) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
Wayne Werner wrote: On Wed, Nov 16, 2011 at 10:09 AM, Steven D'Aprano wrote: Wayne Werner wrote: In old style formatting, you use a string with format specifiers (%s, %d, etc.) followed by a tuple of arguments. Here, the lengths have to match exactly - if you have one specifier then you must have a 1-element tuple. That's actually wrong. If you have one specifier, you must have one object of any sort *except* a tuple. I think you misunderstood - I said a 1-element tuple - e.g. (3,) Actually, I didn't misunderstand... I just worded my reply badly. Sorry for the confusion. What I should have said was, you *can* use a 1-element tuple, but you don't *need* to use a 1-element tuple UNLESS that element is itself a tuple. For any other object, just use the object itself. There's no need to wrap it in a tuple first. As above, that's a two-element tuple. It was explained to me once that in this case: "%s" % 42 That since python expects to see a single-element tuple it treats it as or converts 42 to a single element tuple. "Treats as" may be true; "converts to" not so much. What it actually does is this: py> import dis py> dis.dis(compile('"%s" % x', '', 'single')) 1 0 LOAD_CONST 0 ('%s') 3 LOAD_NAME0 (x) 6 BINARY_MODULO 7 PRINT_EXPR 8 LOAD_CONST 1 (None) 11 RETURN_VALUE Notice that the call to BINARY_MODULO (the % operator) takes two arguments, the string "%s" and the object x, whatever it happens to be. Python can't convert x to a tuple at this point, because it doesn't know what x is, and it may not know how many format specifiers are in the string either. Once the string and the object hit BINARY_MODULO, all bets are off. It will do whatever it likes, because that's purely internal implementation. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulus
On Wed, Nov 16, 2011 at 4:04 PM, Steven D'Aprano wrote: > Wayne Werner wrote: > >> It was explained to me once that in > > this case: >> >> "%s" % 42 >> >> That since python expects to see a single-element tuple it treats it as or >> converts 42 to a single element tuple. >> > > "Treats as" may be true; "converts to" not so much. What it actually does > is this: > > py> import dis > py> dis.dis(compile('"%s" % x', '', 'single')) > 1 0 LOAD_CONST 0 ('%s') > 3 LOAD_NAME0 (x) > 6 BINARY_MODULO > 7 PRINT_EXPR > 8 LOAD_CONST 1 (None) > 11 RETURN_VALUE > > > Notice that the call to BINARY_MODULO (the % operator) takes two > arguments, the string "%s" and the object x, whatever it happens to be. > Python can't convert x to a tuple at this point, because it doesn't know > what x is, and it may not know how many format specifiers are in the string > either. > > Once the string and the object hit BINARY_MODULO, all bets are off. It > will do whatever it likes, because that's purely internal implementation. Ah, very cool. Just because I was interested, I did the same thing, only using (x,) and there was only one difference (line? 6): >>> dis.dis(compile('"%s" % (x, )', '', 'single')) 1 0 LOAD_CONST 0 ('%s') 3 LOAD_NAME0 (x) 6 BUILD_TUPLE 1 9 BINARY_MODULO 10 PRINT_EXPR 11 LOAD_CONST 1 (None) 14 RETURN_VALUE The more you know! Thanks, -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] change values in an array
I am trying to do something really simple. I have a numpy array and if any values in the array are 255 I want to change them to 1. but I can't seem to get anything to work! If I use: for i, value in enumerate(mask_arr): if value==255: mask_arr[i]=1 I get this error: Traceback (most recent call last): File "d:/timeseries_mask.py", line 51, in if value==255: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() and if I try to add a.any() to my code: for i, value in enumerate(mask_arr): if value.any()==255: mask_arr[i]=1 my array does not change at all. Any feedback will be greatly appreciated ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor