Re: [Tutor] gtk.TreeView displays all right on Windows, but can't see the values on Ubuntu
Op 25-04-12 06:41, Lion Chen schreef: Hi, All, Hi, please fix your formatting, the indentation is a big mess and makes it impossible to read. You should also ask your question on the PyGTK mailing list, not here. first thanks the people who gave helps to me, :) now i encountered another question... my first program LionCalculator, works ok on Windows, but there is a problem on Ubuntu. LionCalculator has a function that can store the previous calculation results to the gtk.ListStore((gobject.TYPE_STRING, gobject.TYPE_BOOLEAN, gobject.TYPE_STRING)) for i in xrange(12): self.results_store.append(["F%d" % (i+1), None, None]) when pressed F1--F12 or click the gtk.CellRendererRadio, the gtk.Entry will get the corresbonding values stored in ListStore. when i run the program on Windows, no problem, i can see everything that should display on the screen. but when i run it on Ubuntu, i can't see the "F1" -- "F12" in the ListStore, can't see the calculation results that should also display on ListStore, only the gtk.CellRendererRadio showed. i don't understand... and when i add "print ...get_store()[0][0]" and run it again, it print "F1"! strange... and when i press F1 or F2..., the result will copy to the entry, although it does not display in the TreeView, strange... the following are codes: #in class ResultsView.. def make_view(self): # make ListStore for storing results self.results_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_BOOLEAN, gobject.TYPE_STRING) #to store 12 calculation results, the results_store[0][0] is "F1--F12" for i in xrange(12): self.results_store.append(["F%d" % (i+1), None, None]) self.results_view = gtk.TreeView(self.results_store) #f_renderer is for "F1" -- "F12" f_renderer = gtk.CellRendererText() f_renderer.set_property( 'editable', False ) f_renderer.set_property("size", 5) f_renderer.set_property("cell-background", "cyan") #when clicked bt_renderer, it will copy the corresbonding values to gtk.entry bt_renderer = gtk.CellRendererToggle() bt_renderer.set_property('activatable', True) bt_renderer.set_property("radio", True) bt_renderer.set_property("cell-background", "grey") bt_renderer.connect("toggled", self.ready_cp, self.results_store) #txt_renderer is for storing calculation results txt_renderer = gtk.CellRendererText() txt_renderer.set_property( 'editable', False ) txt_renderer.set_property("size", 5) txt_renderer.set_property("cell-background", "green") #i guess the problem is in the following, but i don't know where it exactly is, ok in Windows, can't show in Ubuntu... bt_column = gtk.TreeViewColumn("F1--F12") bt_column.pack_start(f_renderer, True) bt_column.pack_start(bt_renderer, False) bt_column.set_attributes(f_renderer, text=0) #set active to be clickable. and the bt_columen is #corresbonding to results_store columne 1 bt_column.add_attribute(bt_renderer, "active", 1) #and txt_column is corresbonding to the store column 2 txt_column = gtk.TreeViewColumn("Calculation Results ", txt_renderer, text=2) self.results_view.append_column(bt_column) self.results_view.append_column(txt_column) self.results_view.show() return self.results_view #in class LionCalc def __init__(self): .. self.results_view = ResultsView() right_vbox.pack_start(self.results_view.make_view(), True, True, 0) win.show_all() could anybody give me help? thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] passing results between functions
Hi I wrote two functions which does different manipulations on text files. To start out with I passed the filename as a parameter and each function opened the file and saved it. I then realized I would need to do that twice if I wanted to use both my functions on the same file. I the modified the functions to take the input as follows: myfunction(open(sys.argv[1]),'ro')) It still wasn't good enough so I modified the function to return data as follows: def myfunction returndata = [] # some code ... return ''.join(returndata) so now I can do myfunction(mysecondfunction(sys.argv[1],'ro')) or mysecondfunction(myfunction(sys.argv[1],'ro')) so my question is philosophical. Is that the pythonian way or is there a better/easier/more efficient way to pass data? To be honest I am still a bit stuck in how I did things when I programmed in Delphi years ago and trying to make the paradigm shift and understanding the data structures. Regards -- Gerhardus Geldenhuis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] passing results between functions
On 04/25/2012 05:36 AM, Gerhardus Geldenhuis wrote: > Hi > I wrote two functions which does different manipulations on text files. > > To start out with I passed the filename as a parameter and each function > opened the file and saved it. > > I then realized I would need to do that twice Do what twice? > if I wanted to use both my > functions on the same file. I the modified the functions to take the input > as follows: > myfunction(open(sys.argv[1]),'ro')) This is a syntax error. When posting code, please use copy/paste so we don't have to guess which part is just from your retyping. > It still wasn't good enough so I modified the function to return data as > follows: > > def myfunction This is another syntax error. Where are the parens, the arguments, or the colon? > returndata = [] Your email program destroyed the indentation here, or is this just another retyping error? > # some code > ... > return ''.join(returndata) > > so now I can do myfunction(mysecondfunction(sys.argv[1],'ro')) > or mysecondfunction(myfunction(sys.argv[1],'ro')) This won't work either, but guessing what's wrong would require that you actually post the function prototypes and return values. I infer that you return a single string from each function, but the functions take two parameters. Incidentally, in case you're planning to use the second parameter 'ro' as a mode for opening the file, I don't believe that 'o' is a valid mode value. > so my question is philosophical. Is that the pythonian way or is there a > better/easier/more efficient way to pass data? No clue till you actually post some code. > To be honest I am still a bit stuck in how I did things when I programmed > in Delphi years ago and trying to make the paradigm shift and understanding > the data structures. > > Regards > > -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] creating a regularly placed fields in a line
Friends, I wrote a small piece of code (given below). The aim is to take each line in a file, split the fields, replace the first four fields and then write the new lines in a output file. The input and output are given in the attached file. The output contains fields which are irregularly placed depending up on the size of the field. I am thinking to fix the size of each field. Kindly provide me some way on how i can do the same or a better way to fix placement of fields. with open('tmp') as tp: for line in tp: line=line.split() p1=atm_type[line[0]];p2=atm_type[line[1]] p3=atm_type[line[2]];p4=atm_type[line[3]] new='\t'.join(line[4:10]) bond.write(' %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) ) -- C. Balasubramanian test.output Description: Binary data ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a regularly placed fields in a line
> I wrote a small piece of code (given below). The aim is to take each line > in a file, split the fields, replace the first four fields and then write > the new lines in a output file. The input and output are given in the > attached file. The output contains fields which are irregularly placed > depending up on the size of the field. I am thinking to fix the size of > each field. Kindly provide me some way on how i can do the same or a > better way to fix placement of fields. > > with open('tmp') as tp: > for line in tp: > line=line.split() > p1=atm_type[line[0]];p2=atm_type[line[1]] > p3=atm_type[line[2]];p4=atm_type[line[3]] > new='\t'.join(line[4:10]) > bond.write(' %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) ) Taking a look at your output file leads me think that it is not really meant to be human readable. If it is only for a program to use, then I would just make it comma separated using the csv module. If you are intending to make it readable to humans, I would suggest looking at string formatting. It is better than tabbing since most clients show tabs a little differently and a tab may still cause irregular columns if you have a large difference in each row's column size. The formatting mini-language can be a little complex but luckily the guide is useful. http://docs.python.org/library/string.html#format-specification-mini-language The examples on that page are useful and I think what you want is quoted below. " Aligning the text and specifying a width: >>> '{:<30}'.format('left aligned') 'left aligned ' >>> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***centered***' " Not really sure how to do the equivalent with % substitution. 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] creating a regularly placed fields in a line
On 25/04/2012 16:57, Prasad, Ramit wrote: Not really sure how to do the equivalent with % substitution. Ramit See http://docs.python.org/library/stdtypes.html#string-formatting-operations -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a regularly placed fields in a line
> > > > Not really sure how to do the equivalent with % substitution. > > > > See > http://docs.python.org/library/stdtypes.html#string-formatting-operations > > Mark Lawrence. Thanks Mark. Based on that, I find .format more intuitive (especially for alignment) than % substitution. Useful to know both though, since lots of people swear by % substitution. 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] passing results between functions
On 25/04/12 10:36, Gerhardus Geldenhuis wrote: Hi I wrote two functions which does different manipulations on text files. To start out with I passed the filename as a parameter and each function opened the file and saved it. I assume you mean it did some processing on the file data and then wrote it back? I then realized I would need to do that twice if I wanted to use both my functions on the same file. I the modified the functions to take the input as follows: myfunction(open(sys.argv[1]),'ro')) You mean it took a file object and a string? It still wasn't good enough so I modified the function to return data as follows: def myfunction returndata = [] # some code ... return ''.join(returndata) So it returns a string. so now I can do myfunction(mysecondfunction(sys.argv[1],'ro')) or mysecondfunction(myfunction(sys.argv[1],'ro')) Thats inconsistent since to one occasion you pass two arguments but on the other only one - the return value of the first function. And since the first parameter is expecting a file object the string will cause an error. But if you fixed the inconsistent data issue then the principle is fine. so my question is philosophical. Is that the pythonian way or is there a better/easier/more efficient way to pass data? It depends what kind of data and what you mean by "pass". You could use objects to pass more complex types of data, or tuples to pass multiple values. Or you could write the values into a shared database. It just depends on what you want to do, whether the function needs to e thread-safe, how big the data is, etc. To be honest I am still a bit stuck in how I did things when I programmed in Delphi years ago and trying to make the paradigm shift and understanding the data structures. Thee is virtually no difference between Delphi and Python in the way functions (and objects) work. I'm not sure what paradigmn shift you have in mind. Delphi can't return tuples, but other than that the options and styles are pretty similar. -- 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] creating a regularly placed fields in a line
On 25/04/12 16:17, Bala subramanian wrote: placed depending up on the size of the field. I am thinking to fix the size of each field. with open('tmp') as tp: for line in tp: ... bond.write(' %s\t%s\t%s\t%s\t%s\n' % (p1,p2,p3,p4,new) ) Add your widths to the format string: bond.write(' %10s%20s%30s%10s%25s\n' % (p1,p2,p3,p4,new) ) adjust the lengths to suit. if you write the data in several places you can ensure consistency by defining the format string as a variable: fmt = ' %10s%20s%30s%10s%25s\n' bond.write( fmt % (p1,p2,p3,p4,new) ) hth, -- 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