[Tutor] How to save tkinter canvas....
Hi Folks, I have a window that contains bar chart on a Canvas (tkinter). Is there a way in which I can provide the user the option of saving it as an image file. Some pointers needed.. thanks in anticipation. Regards, Asrarahmed Kadri -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Of integers, relations and trees
Tor Hildrum wrote: > I have this problem which I thought would be trivial, but I can't > seem to figure out a decent way to do it. > > Say I have the following file: > 10 > -100 > -108 > --1080 > 12 > 20 > > In lack of a better explanation, here is how it works: > A level in the tree follows the following: > x * 10^level > > x * 10^1 belongs to level 1 in the three > x * 10^2 belongs to level 2 in the three. > etc. Here's a different way to look at it: the level in the tree is determined by the length of the string representation of the node. 2 long -> level 1. 3 long -> level 2. etc. > I decided to make this pretty straightforward so I wrote a Node class > and a Tree class. Are you sure you need a Tree class? The Node class itself may have all the facilities needed to manage an entire tree. The tree is then represented by a 'root' node. You'd then have: - root - 10 - 103 - 105 - 12 etc. > A Node has a key which is an integer, as well as some additional > information that isn't relevant to the structure. It also has a link > to it's sibling, which is the next node on the same level. And a link > to it's first child. A different way to implement this is for each node to have a (sorted) list of children and potentially a link to its parent. The story then looks like this: - root with the following children - 10 (with parent = root) with the following children: - 100 - 108 - 12 (with parent = root) etc. You then do not insert siblings, you add children (not tested, but the intention is what counts): class Node(object): def __init__(self, name): self.Name = name self.Parent = None self.Children = [] def addChild(self, newname): """Tries to add the node as a newNode. Returns True if successful, False otherwise.""" # check if the node is a (sub)child if newname[:len(self.Name)] <> self.Name: return False # check if it is a direct descendant if len(newname) == len(self.Name) + 1: newnode = Node(newname) newnode.Parent = self self.Children.append(newnode) self.Children.sort() return True else: # not a direct descendant -> add to one of the children for child in self.Children: if child.addChild(newname): return True # if we arrive here, it means that there's a missing level in the hierarchy -> add it self.addChild(newname[:len(newname)-1]) return self.addChild(newname) def show(self, indentation=0): print ' ' * indentation, '-', self.Name for child in self.Children: child.show(indentation + 2) def __cmp__(self, othernode): """Get sort() to work properly.""" return cmp(self.Name, othernode.Name) def hasChildren(self): return len(self.Children) > 0 def hasSiblings(self): return (self.Parent <> None) and (len(self.Parent.Children) > 1) root = Node('') root.addChild('10') root.addChild('12') root.addChild('0') root.addChild('20') root.addChild('108') root.addChild('5') root.addChild('678') root.show() This implementation will not handle the dot-style leaves properly, you'll need some extra logic for that. It will however 'fill in the blanks', so you can add node '678' without adding nodes '6' and '67' first and auto-sort the nodes. > How do I know if I have a sibling or a child? > Simple, I just check the length: > - > if( len(str(node1[key])) == len(str(node2[key])) ): > - > > If the length, amount of integers, is the same, they are siblings. With the alternative representation presented, it's more comfortable: - has child: len(self.Children) > 0 - has sibling: (self.Parent <> None) and (len(self.Parent.Children) > 1) > How do I determine that 2080 is not a child of 10. Or how do i determine > that 536798 is not a child of 536780? And how do I determine that it is a > child? See my code: just manipulate them as strings and it's suddenly very easy. Same length and the first (length - 1) characters are the same -> siblings. Different length: take the shortest node name; if the other node name starts with that string, it's a child, otherwise they're unrelated. > I can't seem to rack my brains around a solution for this. Maybe it's > my tree-structure that is making this more complex than it should be? Hierarchies are easier if you look at them as families: it's easier to ask a parent how many children it has, than it is to ask one of the siblings if there is any sibling younger than it, then ask that younger sibling if it has any younger siblings, etc. Yours, Andrei ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to save tkinter canvas....
"Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote > I have a window that contains bar chart on a Canvas (tkinter). Is > there a > way in which I can provide the user the option of saving it as an > image > file. Have you looked at the PIL library? I seem to recall seeing a function there that could work with Tkinter Canvas objects, and once in PIL it should be possible to save it. HTH. Alan G, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems pickling functions
Den 8. des. 2006 kl. 15.43 skrev Kent Johnson: > Arild B. Næss wrote: >> Den 8. des. 2006 kl. 14.05 skrev Kent Johnson: >>> Why do you need to pickle the function? Is it created >>> dynamically? Can you just pickle the data? >>> >>> Kent >>> >> Thanks. >> I guess it's not absolutely necessary to pickle the function. I >> tried to do this because I wanted to use the function in the >> interpreter without having to write it in there line by line. >> I'm used to working in R and Matlab, where you often run scripts >> from the active interpreter. In that way you can actually >> examine the data a script generates, instead of having the script >> print it to screen or file. >> I'm having trouble getting used to python like this because I get >> trouble trying to paste in several lines at once from emacs, and >> I haven't found a way to run scripts in the interpreter. > > Two suggestions: > - Use an editor / IDE that allows you to run Python scripts. IDLE > will do this. I think emacs has good support for Python too but > someone who uses emacs will have to help you with that one. > > - Save your function in a module and import the module from the > interpreter. Then you can run the function in the interpreter. > > For example if you have funcs.py in the working directory and it > contains a function > def doItAll(): > pass > > then in the interpreter you can type > >>> import funcs > >>> funcs.doItAll() > > to run the function. > > If you change the function in an external editor you have to reload > it in the interpreter to get the revised version: > >>> reload(funcs) Thanks. That worked. I haven't found out how to change the working directory in IDLE, though – and for some reason it seems to be a different one this session from the last one. Does anyone know? (I use a mac by the way.) > > PS Please reply to the list, not to me directly. Sorry about that. I didn't realize my client picked your adress and not the list's when I clicked reply. My mistake. regards, Arild Næss ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] help
HiI have got some python related queries. I am working on an MRes projectwhich involves a bit of programing in python. Actually, I am using apython program developed by someone, which gives pairwise geneticdistances between a set of sequences (I don't know how...) and outputs asimple text file of the following format...s1,s2,s3,s4,s54,7,2,38,6,43,67where s1, s2, s3...represent sequences and the second line describesthe pairwise distance between s1 and all other sequences,thid line isfor the distance between s2 and other sequences.and so on.1. I want to read this line into a data structure(most probably by making a list of lists like [[s1,s2,4],[s1,s2,7],[s1,s3,2] and so on) which gives each pair and the corresponding pairwise distances. Of course, I would do this by writing a function that reads this file into a data structure which gives the all the pairs of sequences and theircorresponding distance values, but I am not sure how to do this. 2. Secondly, I want to write another function which takes up three arguments, the data structure returned by the previous function and the names of two sequences. It then returns the corresponding value. Please helpKamranExpress yourself instantly with MSN Messenger! MSN Messenger Download today it's FREE! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
On Sat, 2006-12-09 at 20:19 +0500, Kamran Haider wrote: > Hi > > I have got some python related queries. I am working on an MRes > project > which involves a bit of programing in python. Actually, I am using a > python program developed by someone, which gives pairwise genetic > distances between a set of sequences (I don't know how...) and outputs > a > simple text file of the following format... > > s1,s2,s3,s4,s5 > 4,7,2,3 > 8,6,4 > 3,6 > 7 > where s1, s2, s3...represent sequences and the second line describes > the pairwise distance between s1 and all other sequences,thid line is > for the distance between s2 and other sequences. > and so on. > 1. I want to read this line into a data structure(most probably by > making a list of lists like [[s1,s2,4],[s1,s2,7],[s1,s3,2] and so on) > which gives each pair and the corresponding pairwise distances. I think a dictionary will server you better. {(s1,s2):4, (s1,s3):7, (s1,s4):2, } > Of course, I would do this by writing a function that reads this file > into a data structure which gives the all the pairs of sequences and > theircorresponding distance values, but I am not sure how to do > this. Well ask for help as you break this down. You should wind up with quite a few functions, not just one. Using a dictionary should be no harder than a list of lists. > 2. Secondly, I want to write another function which takes up three > arguments, the data structure returned by the previous function and > the names of two sequences. It then returns the corresponding value. With the dictionary, this function is trivial def distance(seqdistance, s1, s2): return seqdistance[(s1,s2)] Do you need to handle reversing the sequences? Presumably distance(s1,s2) == distance(s2,s1) > > Please help > Kamran > > > > > __ > Express yourself instantly with MSN Messenger! MSN Messenger Download > today it's FREE! > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Lloyd Kvam Venix Corp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to save tkinter canvas....
"Alan Gauld" <[EMAIL PROTECTED]> wrote > I seem to recall seeing a function there that could work with > Tkinter > Canvas objects, and once in PIL it should be possible to save it. Looks like it works the other way, you can put PIL images into a canvas. BUT there is a Canvas.postscript method that will create a postcript version of a canvas. That should do your printing for you... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor