Re: unpickle from URL problem
On Wed, 10 Oct 2007 05:58:51 +, Alan Isaac wrote: > I am on a Windows box. > > I pickle a tuple of 2 simple objects with the pickle module. > It pickles fine. It unpickles fine. > > I upload to a server. > I try to unpickle from the URL. No luck. Try it: > x1, x2 = > pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1')) > > I change the filetype to unix. I upload again. > I try to unpickle from the URL. Now it works. Try it: > x1, x2 = > pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2')) > > Why the difference? Pickles are *binary* files, not text files, so make sure you always treat them as binary, e.g. opening the files with mode 'rb' and 'wb' and don't transmit them in text mode over FTP etc. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
Diez B. Roggisch schrieb: >>> Yes, it is. >> >> I'm afraid not. >> >> As I admitted in my reply to Marc, I overstated my case by saying that >> L isn't rebound at all. Of course it is rebound, but to itself. >> >> However, it is not true that += "always leads to a rebinding of a to >> the result of the operation +". The + operator for lists creates a new >> list. += for lists does an in-place modification: > > > It still is true. > > a += b > > rebinds a. Period. Which is the _essential_ thing in my post, because > this rebinding semantics are what confused the OP. Which I just came around to show in a somewhat enhanced example I could have used the first time: class Foo(object): a = [] @classmethod def increment(cls): print cls cls.a += [1] class Bar(Foo): pass Foo.increment() Bar.increment() print Foo.a print Bar.a Bar.a = [] print Foo.a print Bar.a 192:~/projects/SoundCloud/ViewAnimationTest deets$ python /tmp/test.py [1, 1] [1, 1] [1, 1] [] Which makes the rebinding-part of __iadd__ pretty much an issue, don't you think? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
>> Yes, it is. > > I'm afraid not. > > As I admitted in my reply to Marc, I overstated my case by saying that L > isn't rebound at all. Of course it is rebound, but to itself. > > However, it is not true that += "always leads to a rebinding of a to the > result of the operation +". The + operator for lists creates a new list. > += for lists does an in-place modification: It still is true. a += b rebinds a. Period. Which is the _essential_ thing in my post, because this rebinding semantics are what confused the OP. L = [] M = L L += [1] M > [1] > > Compare with: > L = [] M = L L = L + [1] M > [] > > You said: > > "I presume you got confused by the somewhat arbitrary difference between > __add__ and __iadd__ that somehow suggest there is an in-place- > modification going on in case of mutables but as the following snippet > shows - that's not the case: ..." Admittedly, I miss _one_ word here: necessarily before the "an". > That's an explicit denial that in-place modification takes place, and > that's *way* off the mark. I was concentrating so hard on showing in- > place modification that I glossed over the "return self" part. And I was concetrating so hard on the rebinding-part, I glossed over the in-place-modification part. Diez -- http://mail.python.org/mailman/listinfo/python-list
APOLOGY (was: pytz has so many timezones!)
I apologize for being a dick. It won't happen again. I was actually thinking that the 25 standard timezones (which make a reasonable list or can be easily generated by clicking a map) could serve as a filter to make the detail list reasonably sized. Something like this: import pytz import datetime import sqlite3 import random utc = pytz.timezone('UTC') utc_dt = datetime.datetime(2007,1,1,0,0,0,tzinfo=utc) offset_zone = [] for i in pytz.all_timezones: the_zone = pytz.timezone(i) loc_dt = utc_dt.astimezone(the_zone) loc_offset = loc_dt.strftime('%z') offset_zone.append((loc_offset,the_zone.zone)) con = sqlite3.connect(":memory:") cur = con.cursor() cur.executescript(""" create table tz( offset, zone ); """) cur.executemany(""" INSERT INTO tz(offset, zone) VALUES (?,?)""" , offset_zone) cur.execute(""" SELECT DISTINCT offset FROM tz WHERE offset LIKE '+%' ORDER BY offset; """) east = cur.fetchall() cur.execute(""" SELECT DISTINCT offset FROM tz WHERE offset LIKE '-%' ORDER BY offset DESC; """) west = cur.fetchall() distinct_offsets = east + west print print 'Choose your GMT offset:' for i in distinct_offsets: if i[0]=='+' or i[0]=='+0600' or i[0]=='+1200' or i[0]=='-1200' or i[0]=='-0600': print print i[0], print print the_choice = random.choice(distinct_offsets) print 'Offset',the_choice[0],'chosen.' print cur.execute(""" SELECT offset, zone FROM tz WHERE offset=? ORDER BY offset; """,the_choice) list_o_zones = cur.fetchall() print 'Which timezone?' print for i in list_o_zones: print i[0],i[1] ## Choose your GMT offset: ## ## + +0100 +0200 +0300 +0330 +0400 +0430 +0500 +0530 +0545 ## +0600 +0630 +0700 +0800 +0900 +0930 +0945 +1000 +1030 +1100 +1130 ## +1200 +1300 +1345 +1400 ## -1200 -1100 -1000 -0930 -0900 -0800 -0700 ## -0600 -0500 -0400 -0330 -0300 -0200 -0100 ## ## Offset -0930 chosen. ## ## Which timezone? ## ## -0930 Pacific/Marquesas ## ## ## ## Choose your GMT offset: ## ## + +0100 +0200 +0300 +0330 +0400 +0430 +0500 +0530 +0545 ## +0600 +0630 +0700 +0800 +0900 +0930 +0945 +1000 +1030 +1100 +1130 ## +1200 +1300 +1345 +1400 ## -1200 -1100 -1000 -0930 -0900 -0800 -0700 ## -0600 -0500 -0400 -0330 -0300 -0200 -0100 ## ## Offset +0945 chosen. ## ## Which timezone? ## ## +0945 Australia/Eucla ## ## ## ## Choose your GMT offset: ## ## + +0100 +0200 +0300 +0330 +0400 +0430 +0500 +0530 +0545 ## +0600 +0630 +0700 +0800 +0900 +0930 +0945 +1000 +1030 +1100 +1130 ## +1200 +1300 +1345 +1400 ## -1200 -1100 -1000 -0930 -0900 -0800 -0700 ## -0600 -0500 -0400 -0330 -0300 -0200 -0100 ## ## Offset -0100 chosen. ## ## Which timezone? ## ## -0100 America/Scoresbysund ## -0100 Atlantic/Azores ## -0100 Atlantic/Cape_Verde ## -0100 Etc/GMT+1 ## ## ## ## Choose your GMT offset: ## ## + +0100 +0200 +0300 +0330 +0400 +0430 +0500 +0530 +0545 ## +0600 +0630 +0700 +0800 +0900 +0930 +0945 +1000 +1030 +1100 +1130 ## +1200 +1300 +1345 +1400 ## -1200 -1100 -1000 -0930 -0900 -0800 -0700 ## -0600 -0500 -0400 -0330 -0300 -0200 -0100 ## ## Offset +1000 chosen. ## ## Which timezone? ## ## +1000 Antarctica/DumontDUrville ## +1000 Asia/Sakhalin ## +1000 Asia/Vladivostok ## +1000 Australia/Brisbane ## +1000 Australia/Lindeman ## +1000 Australia/Queensland ## +1000 Etc/GMT-10 ## +1000 Pacific/Guam ## +1000 Pacific/Port_Moresby ## +1000 Pacific/Saipan ## +1000 Pacific/Truk ## +1000 Pacific/Yap ## ## ## ## Choose your GMT offset: ## ## + +0100 +0200 +0300 +0330 +0400 +0430 +0500 +0530 +0545 ## +0600 +0630 +0700 +0800 +0900 +0930 +0945 +1000 +1030 +1100 +1130 ## +1200 +1300 +1345 +1400 ## -1200 -1100 -1000 -0930 -0900 -0800 -0700 ## -0600 -0500 -0400 -0330 -0300 -0200 -0100 ## ## Offset +0300 chosen. ## ## Which timezone? ## ## +0300 Africa/Addis_Ababa ## +0300 Africa/Asmara ## +0300 Africa/Asmera ## +0300 Africa/Dar_es_Salaam ## +0300 Africa/Djibouti ## +0300 Africa/Kampala ## +0300 Africa/Khartoum ## +0300 Africa/Mogadishu ## +0300 Africa/Nairobi ## +0300 Antarctica/Syowa ## +0300 Asia/Aden ## +0300 Asia/Baghdad ## +0300 Asia/Bahrain ## +0300 Asia/Kuwait ## +0300 Asia/Qatar ## +0300 Asia/Riyadh ## +0300 Etc/GMT-3 ## +0300 Europe/Moscow ## +0300 Europe/Volgograd ## +0300 Indian/Antananarivo ## +0300 Indian/Comoro ## +0300 Indian/Mayotte ## +0300 W-SU ## Choose your GMT offset: ## ## + +0100 +0200 +0300 +0330 +0400 +0430 +0500 +0530 +0545 ## +0600 +0630 +0700 +0800 +0900 +0930 +0945 +1000 +1030 +1100 +1130 ## +1200 +1300 +1345 +1400 ## -1200 -1100 -1000 -0930 -0900 -0800 -0700 ## -0600 -0500 -0400 -0330 -0300 -0200 -0100 ## ## Offset -0600 chosen. ## ## Which timezone? ## ## -0600 America/Belize ## -0600 America/Cancun ## -0600 America/Chicago ## -0600 America/Costa_Rica ## -0600 America/El_Salvador ## -0600 America/Guatemala ## -0600 America/Indiana/Knox ## -0600 America/Indiana/Petersburg ## -0600 America/Indiana/Tell_City
button binding isn't triggering event like it's supposed to
hi. i am trying to learn event binding. i have a form with a couple of buttons for different tags, which all call the same function. the text on the buttons changes from record to record. this is the problem area: def showrecords(self): """this function updates the form labels to show the current record""" global gmax global gctr global CurrTitle global CurrAuthor global CurrTag1 global CurrTag2 global CurrTag3 global CurrTag4 global CurrTag5 global CurrAbstract global CurrLocation global CurrID global HuntFor self.Lbn.config(text=gmax) self.Lbnof.config(text=gctr+1) self.LbTitle.config(text=CurrTitle, justify=LEFT) self.LbName.config(text=CurrAuthor) self.LbPath.config(text=CurrLocation) self.BtTag1.bind('',self.calltotags(tag=CurrTag1)) self.BtTag1.config(text=CurrTag1) self.BtTag2.bind('',self.calltotags(tag=CurrTag2)) self.BtTag2.config(text=CurrTag2) self.BtTag3.bind('',self.calltotags(tag=CurrTag3)) self.BtTag3.config(text=CurrTag3) self.BtTag4.bind('',self.calltotags(tag=CurrTag4)) self.BtTag4.config(text=CurrTag4) self.BtTag5.bind('',self.calltotags(tag=CurrTag5)) self.BtTag5.config(text=CurrTag5) self.LbAbs.config(text=CurrAbstract) def calltotags(event, tag): # This function sets the search variable HuntFor value, and calls the tagwork class. global HuntFor global toggle HuntFor=tag toggle="Tagcluster" print HuntFor tagwork() when i click on the buttons, none of them call the function. i only see the huntfor value in the prompt when i load the program. can someone please enlighten me as to why this happens? (the buttons are loaded on the root window in an earlier function, and i have the same problem even if i try binding the events there) also, can someone suggest a good (and free!) code editor for python please? thanks in advance. mridula. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unexpected behaviour with HTMLParser...
Just Another Victim of the Ambient Morality schrieb: > "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Just Another Victim of the Ambient Morality schrieb: >>> HTMLParser is behaving in, what I find to be, strange ways and I >>> would like to better understand what it is doing and why. >>> >>> First, it doesn't appear to translate HTML escape characters. I >>> don't know the actual terminology but things like & don't get >>> translated into & as one would like. Furthermore, not only does >>> HTMLParser not translate it properly, it seems to omit it altogether! >>> This prevents me from even doing the translation myself, so I can't even >>> working around the issue. >>> Why is it doing this? Is there some mode I need to set? Can anyone >>> else duplicate this behaviour? Is it a bug? >> Without code, that's hard to determine. But you are aware of e.g. >> >> handle_entityref(name) >> handle_charref(ref) >> >> ? > > Actually, I am not aware of these methods but I will certainly look into > them! > I was hoping that the issue would be known or simple before I commited > to posting code, something that is, to my chagrin, not easily done with my > news client... > > >>> Secondly, HTMLParser often calls handle_data() consecutively, without >>> any calls to handle_starttag() in between. I did not expect this. In >>> HTML, you either have text or you have tags. Why split up my text into >>> successive handle_data() calls? This makes no sense to me. At the very >>> least, it does this in response to text with & like escape sequences >>> (or whatever they're called), so that it may successively avoid those >>> translations. >> That's the way XML/HTML is defined - there is no guarantee that you get >> text as whole. If you must, you can collect the snippets yourself, and on >> the next end-tag deliver them as whole. > > I think there's some miscommunication, here. > You can't mean "That's the way XML/HTML is defined" because those format > specifications say nothing about how the format must be parsed. As far as I > can tell, you either meant to say that that's the way HTMLParser is > specified or you're referring to how text in XML/HTML can be broken up by > tags, in which case I've already addressed that in my post. I expected to > see handle_starttag() calls in between calls to handle_data(). > Unless I'm missing something, it simply makes no sense to break up > contiguous text into multiple handle_data() calls... I meant that's the way XML/HTML-parsing is defined, yes. >>> Again, why is it doing this? Is there some mode I need to set? Can >>> anyone else duplicate this behaviour? Is it a bug? >> No. It's the way it is, because it would require buffering with unlimited >> capacity to ensure this property. > > It depends on what you mean by "unlimited capacity." Is it so bad to > buffer with as much memory as you have? ...or, at least, have a setting for > such operation? Moreover, you know that you'll never have to buffer more > than there is HTML, so you hardly need "unlimited capacity..." For > instance, I believe Xerces does this translation for you 'cause, really, why > wouldn't you want it to? I've been dealing with XML-files that are several gigbytes of size and never fit into physical memory. So buffering would severely impact the whole system if it was the default of the parser. And you are wrong - xerces (the SAX-parser, which is the equivalent to HTMLParser) explicitly does not do that. It is not guaranteed that the character-data is passed in one chunk. DOM is an etirely different subject, it _has_ to be fully parsed. But then, it's often problematic because of that. >>> These are serious problems for me and I would greatly appreciate a >>> deeper understanding of these issues. >> HTH, and read the docs. > > This does help, thank you. I have obviously read the docs, since I can > use HTMLParser enough to find this behaviour. I don't find the docs to be > very explanatory (perhaps I'm reading the wrong docs) and I think they > assume you already know a lot about HTML and parsing, which may be necessary > assumptions but are not necessarily true... Well, you at least overlooked the methods I mentioned. Diez -- http://mail.python.org/mailman/listinfo/python-list
unpickle from URL problem
I am on a Windows box. I pickle a tuple of 2 simple objects with the pickle module. It pickles fine. It unpickles fine. I upload to a server. I try to unpickle from the URL. No luck. Try it: x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1')) I change the filetype to unix. I upload again. I try to unpickle from the URL. Now it works. Try it: x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2')) Why the difference? Thank you, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with argument parsing
On 9 Okt, 17:18, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > lgwe wrote: > > I have a python-script: myscript, used to start a program on another > > computer and I use OptionParser in optpars. > > I use it like this: myscript -H host arg1 -x -y zzz > > I would like OptionParser to ignore all arguments after arg1, because > > these are options that should be used by the program started on the > > oter computer arg1 -x -y zzz. > > One solution is to write: myscript -H host "arg1 -x -y zzz" > > But is it in some way possible to instruct OptionParser to ignore -x - > > y zzz without the "..."? > > Use -- to separate arguments to be parsed by OptionParser from the > to-be-ignored-ones, which is documented btw. > > Diez Thanks! I understand that this must be documented. But I can't find if anywhere in http://docs.python.org/lib/module-optparse.html. (maybee I just cant see it, but it is there) -- http://mail.python.org/mailman/listinfo/python-list
Re: tarfile...bug?
Hi, Have any one faced such problem, I assume it must be common if it can be replicated so easily , or something wrong with my system Also if I use tar.members instead of tar.getmembers() it works so what is the diff. between tar.members and tar.getmembers() rgds Anurag -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
> If nothing else, please use Google. Many will thank you. > http://www.google.com/search?hl=en&q=Definitional+Interpreters+for+Higher-Order+Functions&btnG=Search http://www.brics.dk/~hosc/vol11/contents.html Definitional Interpreters for Higher-Order Programming Languages Definitional Interpreters Revisited -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
Corrected the links... 1. Programming Languages: Application and Interpretation Shriram Krishnamurthi Part VII Continuations http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf 2. Essentials of Programming Languages (2nd edition) Friedman, Wand and Haynes Chapter 7 Continuation-Passing Interpreters Chapter 8 Continuation-Passing Style http://www.cs.indiana.edu/eopl/ 3. Theories of Programming Languages, John C. Reynolds 5.7 Continuation Semantics [of imperative languages] Chapter 12 Continuations in a Functional Language http://www.cs.cmu.edu/~jcr/tpl.html -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
> Can anyone explain: > > (1) its origin >From the Bibliographic Notes of Chapter 12 Continuations in a Functional Language, Theories of Programming Languages by John C. Reynolds, page 370: "A history of the repeated discoveries of continuations (occurring largely in the context of functional languages) is given in Reynolds [1993]; relevant original papers include those by van Wijngaarden [1996], F. L. Morris [1993], Strachey and Wadsworth [1974], J. H. Morris [1972], Fischer [1972; 1993], and Abdali [1976]. The operations callcc and throw first appeared in Scheme, but are descendents of Landin's [1965b] "J-operator". Both the continuation-passing transformation from direct to continuation semantics and defunctionalization were described, in the setting of programs for interpreting eager-evaluation functional languages, by Reynolds [1972a]." "Beginning with the implementation of Scheme [Sussman and Steele Jr., 1975] continuations and the continuation-passing transformation have played a major role in the design of compilers. More recently, this topic has been explored at book length by Appel [1992]." Reynolds [1993] The Discoveries of Continuations. van Wijngaarden [1996] Recursive Definition of Syntax and Semantics F. L. Morris [1993] The Next 700 Formal Language Descriptions Strachey and Wadsworth [1974] Continuations, A Mathematical Semantics for Handling Full Jumps. J. H. Morris [1972] A Bonus from van Wijngarden's Device Fischer [1972, 1993] Lambda Calculus Schemata Abdali [1976] A Lambda Calculus Model of Programming Languages - I. Simple Constructs, II. Jumps and Procedures Sussman and Steele Jr. [1975] SCHEME: An Interpreter for Extended Lambda Calculus Compiling With Continuations, Andrew W. Appel, 2007 - - - - - - - - - - > (2) its syntax and semantics in emacs lisp, common lisp, scheme The Scheme Programming Language, R. Kent Dybvig 3.3 Continuations 5.5 Continuations http://www.scheme.com/tspl3/ Scheme and the Art of Programming, Springer and Friedman Chapter 16 Introduction to Continuations Chapter 17 Using Continuations - - - - - - - - - - - - - > (6) any good readable references that explain it lucidly ? 1. Programming Languages: Application and Interpretation, Shriram Krishnamurthi Part VII Continuations http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf 2. Essentials of Programming Languages, Friedman, Wand and Haynes Chapter 7 Continuation-Passing Interpreters Chapter 8 Continuation-Passing Style http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf 3. Theories of Programming Languages, John C. Reynolds 5.7 Continuation Semantics [of imperative languages] Chapter 12 Continuations in a Functional Language http://www.cs.indiana.edu/eopl/ >From the Bibliographic Notes of Chapter 5 Failure, Input-Output and Continuations, Theories of Programming Languages, John C. Reynolds "Most of the literature on continuations discusses the concept in the setting of functional languages (where we will return to continuations in Section 12.1). However, the properties of continuation semantics for imperative languages are described, perhaps to excess, by Reynolds [1977]." Reynolds [1977] Semantics of the Domain of Flow Diagrams -- http://mail.python.org/mailman/listinfo/python-list
Re: mutable objects as dictionary keys
On Oct 9, 2007, at 12:44 PM, Andreas Kraemer wrote: > Hi everyone, > > I know that the subject of mutable objects as dictionary keys has > been discussed a number of times in this forum (see for instance > "freezing" of classes), but I would love to hear the thoughts of > the experts on the approach below. > > The use case that I encounter frequently is the classification of > objects according to certain rules or properties: Say, I have > objects A, B, C, ... (e.g. instances of a class, dict, etc), I can > write > > d = {} > d.setdefault(A,[]).append(A) > d.setdefault(B,[]).append(B) > ... > > so objects that map to the same hash key will end up in the same > bucket. (A "real world" example I had to deal with recently was for > instance the construction of a directed acyclic graph from many > directed trees where identical subtrees needed to be identified.) > > The easiest way is of course to define custom __hash__() and __eq__ > () methods, but this breaks if objects are mutated (accidentally) > after having been inserted into the dictionary. The "best" working > approach I came up with so far is to generate an "immutable view" V > of a mutable object O according to my classification rule, delegate > O.__hash__ and O.__eq__ to V, and make sure that the V is memoized > and cannot (easily) be altered later, even when O is mutated: > > def hashable_mutable_factory(mutable,rule): > class _mutable(mutable): > def __init__(self,*args,**kw): > self._view_cache = {} > super(_mutable,self).__init__(*args,**kw) > def _view(self): > id_ = id(self) > if not self._view_cache.has_key(id_): > self._view_cache[id_] = rule(self) > return self._view_cache[id_] > def __hash__(self): > return hash(self._view()) > def __eq__(self,other): > return self._view() == other._view() > return _mutable > > E.g.: > > >>> hashable_dict = hashable_mutable_factory(dict,lambda obj: > frozenset(obj.iteritems())) > >>> h = hashable_dict(a=1,b=2) > >>> d = {} > >>> d[h] = 'foo' > >>> d > {{'a': 1, 'b': 2}: 'foo'} > >>> h['c'] = 'bar' > >>> d > {{'a': 1, 'c': 'bar', 'b': 2}: 'foo'} > >>> g = hashable_dict(a=1,b=2) > >>> h > {'a': 1, 'c': 'bar', 'b': 2} > >>> g > {'a': 1, 'b': 2} > >>> id(g) == id(h) > False > >>> g == h > True > > I slightly favor the factory function idiom above over defining the > rule in a super class (this would have to be done for each mutable > type and rule function separately), especially since I read that > future versions of python (2.6 ?, 3.0 ?) will contain class > decorators and allow syntax like class A(*bases): pass > > Is there a better approach? Any comments are appreciated. > > I have been seriously using Python for one year know, mostly in the > context of graph algorithms etc., and it has always been a > delightful coding experience! I can definitely see how this would be useful for the real world example you mentioned for pruning trees and what-not. Erik Jones Software Developer | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com -- http://mail.python.org/mailman/listinfo/python-list
Re: storing meta data on dictionary keys
On Oct 9, 2007, at 7:37 PM, Andreas Kraemer wrote: > From: Chris Mellon <[EMAIL PROTECTED]> > Sent: Tuesday, October 9, 2007 1:51:04 PM > > > Because, by definition, if you have the key then you don't need > to get > > it from the dict. What you're doing here is conflating 2 mappings > into > > one: string value->person and person->values. Use 2 explicit > dicts to > > make it clear what you're going, or use one dict and store a > tuple of > > values: person, friends = d["John"]. > > No, that was not the point. > > I don't want the meta data to be used in the dictionary look-up, > and in fact I want to be able to modify or delete it later w/o > affecting dictionary look-up. In the toy example "John" and Str > ("John") are two different objects that map to the same value in > the dictionary, and "John" == Str("John") is True, since class Str > (str) inherits __hash__() and __eq__() from str. IOW, if John dyes > his hair, his friends shouldn't change :-) > > Apart from this silly example I really encountered this issue when > using the networkx library. As I mentioned, graph nodes are stored > as dictionary keys there, i.e. nodes can be arbitrary objects with > the only requirement to be hashable, and I am stuck with this data > structure when using the library. In my example nodes are uniquely > identified by their name (a string) but may carry other attributes, > like their display color and shape, that are not used to identify a > node. Therefore, I thought, subclassing str would be the simplest, > most straightforward structure for a node object. > > Of course there are workarounds (e.g. get all keys with keys()), > but I thought something similar to a get_key() dictionary method > would be the easiest way to retrieve the actually stored key > object, and I was just surprised to discover that no such method > does exist So, do you not keep references to your nodes anywhere but the actual graph dict? I kind of agree with Chris here in that two dicts will work. One for the nodes, indexed by their strings. And, to use the actual nodes as keys simply override __hash__ in your Node object classes. >>> class Node(object): ... def __init__(self, name, **kwargs): ... self.name = name ... for k, v in kwargs.items(): ... self.__dict__[k] = v ... ... def __hash__(self): ... return hash(self.name) ... >>> nodes = {} >>> graph = {} >>> >>> n = Node('foo') >>> m = Node('blah', baz=5) >>> >>> nodes[n.name] = n >>> nodes[m.name] = m >>> >>> for name, node in nodes.items(): ... graph[node] = "whatever for node %s" % name ... >>> nodes{'blah': <__main__.Node object at 0x76c50>, 'foo': <__main__.Node object at 0x76d30>} >>> graph{<__main__.Node object at 0x76c50>: 'whatever for node blah', <__main__.Node object at 0x76d30>: 'whatever for node foo'} >>> graph[nodes['foo']]'whatever for node foo' Erik Jones Software Developer | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com -- http://mail.python.org/mailman/listinfo/python-list
Exceptions: Logging TB and local variables?
Hi, My code looks like this: for item in bigset: self.__sub1(item) self.__sub2(item) self.__sub3(item) # the subX functions, in turn, use various 3rd party modules. Now, I would like to do this: for item in bigset: try: self.__sub1(item) self.__sub2(item) self.__sub3(item) except StandardError: # Log error and continue to next item in set. log_error_to_file() In the error log, I would like to record various local variables that existed in subX at the time the Exception was thrown... (even though the actuall exception may have been thrown from deep inside some 3rd party module that subX called) How can I do this? Thank you, Allen -- http://mail.python.org/mailman/listinfo/python-list
M2Crypto possible intermittent bug
I just "upgraded" from M2Crypto 0.17 to M2Crypto 0.18, and I'm running my regression tests. I'm seeing occasional cases where M2Crypto raises the exception SSL.SSLError, and the associated error is "(0, 'Error')", which is the bogus error you get if you feed 0 to "perror". It failed once on "verisign.com"'s cert, then worked on the next try. This is on Windows, running Python 2.4 (which is what M2Crypto supports on Windows.) I'm trying to make this reproduceable. More later. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Yet another comparison of Python Web Frameworks
Since you are starting a new project you may want to look into something new and different http://mdp.cti.depaul.edu/examples -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Python and C classes in a module
On Oct 10, 3:32 pm, "Nicholas Bastin" <[EMAIL PROTECTED]> wrote: > On 10/9/07, Chris Mellon <[EMAIL PROTECTED]> wrote: > > > > > On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > > > > Is it possible to mix classes defined in both Python and C in the same > > > module? Ideally I would like to be able to do: > > > > from some.module import MyPythonClass, MyCClass > > > > I guess that would mean that this would look like this on disk: > > > > some/ > > >__init__.py > > >module.py (contains MyPythonClass) > > >module.so (contains MyCClass) > > > > But would this work? > > > No, you'll need to make module a package, and import from (differently > > named) implementation packages. > > Nah, you can do it, just not in this way. You can either futz with > ihooks, or name module.py something like _module.py, import it into C, > and then re-export as 'module', after attaching all your C types. > > -- > Nick It is easier to do it the other way around. Create module.py and _module.so and in module.py write: from _module.so import * -- http://mail.python.org/mailman/listinfo/python-list
xyplot in Control desk from dspace
Hello everyone, i'm new user of python, in fact, i'm using Control Desk from dsPACE. I just want to change the programm a bit to fit to my application. I draw xyplot using virsual instrument xyplot in Control Desk, and i want to delect any curses if it's needed. But in Control Desk the xyplot is time signal, i can not simply delect it. The visual instrument is set up from python programming. So i want to know the princip of xyplot in python, and then i can draw the needed curses in backgroud's color. In this way, the curses seems as delected. I appreciate very much if anyone can give me help or useful advice. -- http://mail.python.org/mailman/listinfo/python-list
Jython import coventions
When importing Java packages in Jython, what is the convention to simplify imports? For example: import java.util as util Is this correct? Or should I use from * import *? -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Python and C classes in a module
On 10/9/07, Chris Mellon <[EMAIL PROTECTED]> wrote: > On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > > > > Is it possible to mix classes defined in both Python and C in the same > > module? Ideally I would like to be able to do: > > > > from some.module import MyPythonClass, MyCClass > > > > I guess that would mean that this would look like this on disk: > > > > some/ > >__init__.py > >module.py (contains MyPythonClass) > >module.so (contains MyCClass) > > > > But would this work? > > > > No, you'll need to make module a package, and import from (differently > named) implementation packages. Nah, you can do it, just not in this way. You can either futz with ihooks, or name module.py something like _module.py, import it into C, and then re-export as 'module', after attaching all your C types. -- Nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
Bruno Desthuilliers a écrit : (snip) > And it is *not* rebound: Doh. Stupid me. Of course it is - but to a ref to the same object... >>> class A: ... l = [] ... >>> class B(A): pass ... >>> B.__dict__ {'__module__': '__main__', '__doc__': None} >>> B.l [] >>> B.l += [1] >>> B.__dict__ {'__module__': '__main__', '__doc__': None, 'l': [1]} >>> Thanks Diez for pointing this out. And as far as I'm concerned: time to bed :( -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
Marc 'BlackJack' Rintsch a écrit : > On Tue, 09 Oct 2007 18:08:34 +, Steven D'Aprano wrote: > > >L = [] >id(L) >> >>3083496716L >> >L += [1] >id(L) >> >>3083496716L >> >>It's the same L, not rebound at all. > > It *is* rebound. To the same object, but it *is* assigned to `L` and not > just mutated in place. > > In [107]: class A: >.: a = list() >.: > > In [108]: class B(A): >.: pass >.: > > In [109]: B.a += [42] > > In [110]: A.a > Out[110]: [42] > > In [111]: B.a > Out[111]: [42] > > If it was just mutation then `B.a` would have triggered an `AttributeError`. Nope. >>> class A: ... l = [] ... >>> class B(A): pass ... >>> A.l [] >>> A.l += [1] >>> A.l [1] >>> B.l [1] >>> >>> B.l is A.l True And it is *not* rebound: >>> B.l += [2] >>> A.l [1, 2] >>> B.l [1, 2] >>> A.l is B.l True >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
[EMAIL PROTECTED] a écrit : > Hi. > > I've got a question on the differences and how to define static and > class variables. What's a "static" variable ? A variable that doesn't move ?-) > Hence, my understanding is that static variables must be bound to the > class defining the variables You mean "class attributes". Thinking in terms of another language won't help understanding Python's objec model. > and shared by children of parent class > where the variable is defined. But, please have a look at this code in > which a guy told me that the variable a is static: Then don't listen what this guy says. class Foo: > > a = 1 > @classmethod > def increment(cls): > cls.a += 1 > print cls.a > > Here, I am defining variable a which, I believe is class variable, a class attribute > i.e., variable that is not bound to Foo itself. Yes it is. It's an attribute of the class object Foo. > Rather, a is bound to > the class which is accessing the variable. False. >>> dir(Foo) ['__doc__', '__module__', 'a', 'increment'] >>> Foo.__dict__ {'a': 1, '__module__': '__main__', '__doc__': None, 'increment': } > The code that corroborates > this idea is as follows: > class Child1(Foo): > pass > Child1.increment() > 4 ??? > class Child2(Foo): > pass > Child2.increment() > 4 I don't have the same result here. But anyway: the increment method is rebinding 'a', so the first time it's called on a child class, it creates an attribute 'a' for this class. Just change your code this way and you'll see what happens: def increment(cls): print dir(cls) cls.a += 1 print dir(cls) FWIW, if you don't get this, then you may encounter another common gotcha: class Truc: a = 1 def gotcha(self): self.a += 1 print self.a print Truc.a t = Truc() print t.a t.gotcha() print Truc.a t2 = Truc() print t2.a t2.gotcha() print Truc.a t.gotcha() print t.a print t2.a print Truc.a Enjoy !-) Now back to your snippet : if you don't want this behaviour, wrap 'a' into a mutable container: class Foo: a = [1] @classmethod def increment(cls): cls.a[0] += 1 print cls.a[0] class Child(Foo): pass And if you want to hide that, use properties (warning: you need nex style classes here): class Foo(object): _a = [1] @classmethod def increment(cls): cls._a[0] += 1 print cls._a[0] @apply def a(): def fget(self): return type(self)._a[0] def fset(self, val): type(self)._a[0] = val return property(**locals()) class Child(Foo): pass > This means that Child1 and Child2 does not share variable a which > means that variable a is class variable rather than static variable. Nope. This only means that you don't understand the (somewhat peculiar) semantics of bindings, immutable objects and attribute lookup rules in Python. > Could you please comment on this? Is a static or class variable? It's a class attribute. > What's the most recent way of defining 'class' and 'static' variables? No 'static variable' in Python. And what you call a 'class variable' is nothing else than an ordinary of the class object -- http://mail.python.org/mailman/listinfo/python-list
Re: The Modernization of Emacs: terminology buffer and keybinding
On Oct 8, 7:32 am, Joost Kremers <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Don't both "man" and those words for measurement come ultimately from > > words for "hand" (similarly to words like "manual", as in labor)? > > no. Do not bluntly contradict me in public. > "manual" is derived from latin "manus" meaning "hand". the word "man" > is related to (though not directly derived from) "mind", and the latin word > "mens", which means "mind". So you assert, but "man" bears a much closer resemblance to "manus" than it does to "mens". Or are you proposing that the plural word "men" came first? That would be ... odd. -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
[EMAIL PROTECTED] writes: > Matthias, thanks for the reference, but I dont have access to an > engineering library. I would appreciate, if you have access to paper/ > scanner or electronic copy to help many of us out, you are > not just helping me but many will thank you. Given that you seem to be hanging out at the internets, I expect you do know how to use the google... -- http://mail.python.org/mailman/listinfo/python-list
Re: storing meta data on dictionary keys
From: Chris Mellon <[EMAIL PROTECTED]> Sent: Tuesday, October 9, 2007 1:51:04 PM > Because, by definition, if you have the key then you don't need to get > it from the dict. What you're doing here is conflating 2 mappings into > one: string value->person and person->values. Use 2 explicit dicts to > make it clear what you're going, or use one dict and store a tuple of > values: person, friends = d["John"]. No, that was not the point. I don't want the meta data to be used in the dictionary look-up, and in fact I want to be able to modify or delete it later w/o affecting dictionary look-up. In the toy example "John" and Str("John") are two different objects that map to the same value in the dictionary, and "John" == Str("John") is True, since class Str(str) inherits __hash__() and __eq__() from str. IOW, if John dyes his hair, his friends shouldn't change :-) Apart from this silly example I really encountered this issue when using the networkx library. As I mentioned, graph nodes are stored as dictionary keys there, i.e. nodes can be arbitrary objects with the only requirement to be hashable, and I am stuck with this data structure when using the library. In my example nodes are uniquely identified by their name (a string) but may carry other attributes, like their display color and shape, that are not used to identify a node. Therefore, I thought, subclassing str would be the simplest, most straightforward structure for a node object. Of course there are workarounds (e.g. get all keys with keys()), but I thought something similar to a get_key() dictionary method would be the easiest way to retrieve the actually stored key object, and I was just surprised to discover that no such method does exist -Andreas -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 22:43:16 +, Steven D'Aprano wrote: > On Tue, 09 Oct 2007 19:46:35 +, Marc 'BlackJack' Rintsch wrote: > >> On Tue, 09 Oct 2007 18:08:34 +, Steven D'Aprano wrote: >> >> L = [] >> id(L) >>> 3083496716L >> L += [1] >> id(L) >>> 3083496716L >>> >>> It's the same L, not rebound at all. >> >> It *is* rebound. To the same object, but it *is* assigned to `L` and >> not just mutated in place. > > Picky picky. > > Yes, technically there is an assignment of L to itself. I was sloppy to > say "not rebound at all", because when you write an augmented assignment > method you have to return self if you want to implement in-place > mutation. But I hardly call "rebinding to itself" any sort of rebinding > worth the name :) Maybe picky but that detail was the source of the OPs confusion because it introduced a new attribute on the subclass. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: List loops
On 10/9/07, John Machin <[EMAIL PROTECTED]> wrote: > On 10/10/2007 1:33 AM, Hamilton, William wrote: > >> From: Tommy Grav > >> > >> Hi everyone, > >> > >>I have a list of objects where I have want to do two loops. > >> I want to loop over the list and inside this loop, work on all > >> the elements of the list after the one being handled in the outer > > The man said "after" ... > > > > alist = range(3) > for index, i in enumerate(alist): > > for jndex, j in enumerate(alist[index:]): > > ... so you need index+1 ... > > > print index, jndex, i, j > > > > > > 0 0 0 0 > > ... to avoid the above unwanted output. > Hey, if I got it right, he'd have no work to do himself. :) -- -Bill Hamilton -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 21:25:38 +0200, Laszlo Nagy wrote: > a = 1 > a+= 1 # The compiler will probably optimize this and the Python bytecode > interpreter will not rebind 'a' here, just increment the integer in > memory. No. This is Python, not C. You can't increment integers in memory. Integers are immutable objects, not raw bytes: >>> x = 1 >>> id(x) 150830896 >>> x += 1 >>> id(x) 150830884 -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 22:27:47 +0200, Diez B. Roggisch wrote: > Steven D'Aprano schrieb: >> On Tue, 09 Oct 2007 19:23:37 +0200, Diez B. Roggisch wrote: >> >>> Your believes aside, this is simply wrong. The statement >>> >>> a += x >>> >>> always leads to a rebinding of a to the result of the operation +. >> >> Not true. > > > Yes, it is. I'm afraid not. As I admitted in my reply to Marc, I overstated my case by saying that L isn't rebound at all. Of course it is rebound, but to itself. However, it is not true that += "always leads to a rebinding of a to the result of the operation +". The + operator for lists creates a new list. += for lists does an in-place modification: >>> L = [] >>> M = L >>> L += [1] >>> M [1] Compare with: >>> L = [] >>> M = L >>> L = L + [1] >>> M [] You said: "I presume you got confused by the somewhat arbitrary difference between __add__ and __iadd__ that somehow suggest there is an in-place- modification going on in case of mutables but as the following snippet shows - that's not the case: ..." That's an explicit denial that in-place modification takes place, and that's *way* off the mark. I was concentrating so hard on showing in- place modification that I glossed over the "return self" part. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 19:46:35 +, Marc 'BlackJack' Rintsch wrote: > On Tue, 09 Oct 2007 18:08:34 +, Steven D'Aprano wrote: > > L = [] > id(L) >> 3083496716L > L += [1] > id(L) >> 3083496716L >> >> It's the same L, not rebound at all. > > It *is* rebound. To the same object, but it *is* assigned to `L` and > not just mutated in place. Picky picky. Yes, technically there is an assignment of L to itself. I was sloppy to say "not rebound at all", because when you write an augmented assignment method you have to return self if you want to implement in-place mutation. But I hardly call "rebinding to itself" any sort of rebinding worth the name :) Diez is still wrong though, even though I overstated my case. See my reply to his post. [snip code] > If it was just mutation then `B.a` would have triggered an > `AttributeError`. Why? Don't Python classes have inheritance? (That's a rhetorical question. Yes they do, and no B.a would not raise AttributeError because it would inherit from A.) -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: SOAPpy and ArrayOfKeyValue
It's cool when you solve your own problems. It was as simple as this: def dictToKeyValue(self, mydict): soap_list = [] for key,value in mydict.items(): inner = {"key":key,"value":value} soap_list.append(inner) return soap_list It's just a list of dictionaries with the actual words "key" and "value" as keys in the dictionary. I was making it much harder than it needed to be. SOAPpy is smart enough to handle the rest. Thanks SOAPpy! /alex On 10/9/07, Alex Ezell <[EMAIL PROTECTED]> wrote: > Can anyone offer any assistance as to how to convert a basic python > dictionary, list, or even tuple into the SOAP type "ArrayOfKeyValue"? > > I am currently using SOAPpy, but would be willing to change to ZSI or > something else if it made this conversion easier. > > I have tried with the arrayType and structType methods in > SOAPpy.Types, yet they don't seem to do what I need. I suspect there > might not be just a single method call, so, I wrote something like > this: > > def dictToKeyValue(self, mydict): > soap_list = [] > for key,value in mydict.items(): > inner = [] > inner.append(SOAPpy.stringType(key,'key')) > inner.append(SOAPpy.stringType(value,'value')) > soap_list.append(SOAPpy.arrayType(inner)) > print soap_list > return SOAPpy.arrayType(soap_list) > > As you can see, it's pretty nasty and doesn't do what I need either. I > have a client that was written in PHP which I am converting. It uses > this function (which calls the PEAR SOAP library): > > function assocArrayToKeyValue($array) { > $soap_array = array(); > foreach($array as $key=>$value) { > $inner = array(); > $inner[] =& new SOAP_Value('key', 'string', $key); > $inner[] =& new SOAP_Value('value', 'string', $value); > $soap_array[] =& new SOAP_Value("item", > "{{$this->wsdl_urn}}KeyValue", $inner); > } > return $soap_array; > } > > Unfortunately, I don't really have a way to see exactly what > $soap_array is so that I could emulate it in my Python code. > > Any pointers or suggestions are very much appreciated. > > /alex > -- http://mail.python.org/mailman/listinfo/python-list
Re: Unexpected behaviour with HTMLParser...
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Just Another Victim of the Ambient Morality schrieb: >> HTMLParser is behaving in, what I find to be, strange ways and I >> would like to better understand what it is doing and why. >> >> First, it doesn't appear to translate HTML escape characters. I >> don't know the actual terminology but things like & don't get >> translated into & as one would like. Furthermore, not only does >> HTMLParser not translate it properly, it seems to omit it altogether! >> This prevents me from even doing the translation myself, so I can't even >> working around the issue. >> Why is it doing this? Is there some mode I need to set? Can anyone >> else duplicate this behaviour? Is it a bug? > > Without code, that's hard to determine. But you are aware of e.g. > > handle_entityref(name) > handle_charref(ref) > > ? Actually, I am not aware of these methods but I will certainly look into them! I was hoping that the issue would be known or simple before I commited to posting code, something that is, to my chagrin, not easily done with my news client... >> Secondly, HTMLParser often calls handle_data() consecutively, without >> any calls to handle_starttag() in between. I did not expect this. In >> HTML, you either have text or you have tags. Why split up my text into >> successive handle_data() calls? This makes no sense to me. At the very >> least, it does this in response to text with & like escape sequences >> (or whatever they're called), so that it may successively avoid those >> translations. > > That's the way XML/HTML is defined - there is no guarantee that you get > text as whole. If you must, you can collect the snippets yourself, and on > the next end-tag deliver them as whole. I think there's some miscommunication, here. You can't mean "That's the way XML/HTML is defined" because those format specifications say nothing about how the format must be parsed. As far as I can tell, you either meant to say that that's the way HTMLParser is specified or you're referring to how text in XML/HTML can be broken up by tags, in which case I've already addressed that in my post. I expected to see handle_starttag() calls in between calls to handle_data(). Unless I'm missing something, it simply makes no sense to break up contiguous text into multiple handle_data() calls... >> Again, why is it doing this? Is there some mode I need to set? Can >> anyone else duplicate this behaviour? Is it a bug? > > No. It's the way it is, because it would require buffering with unlimited > capacity to ensure this property. It depends on what you mean by "unlimited capacity." Is it so bad to buffer with as much memory as you have? ...or, at least, have a setting for such operation? Moreover, you know that you'll never have to buffer more than there is HTML, so you hardly need "unlimited capacity..." For instance, I believe Xerces does this translation for you 'cause, really, why wouldn't you want it to? >> These are serious problems for me and I would greatly appreciate a >> deeper understanding of these issues. > > HTH, and read the docs. This does help, thank you. I have obviously read the docs, since I can use HTMLParser enough to find this behaviour. I don't find the docs to be very explanatory (perhaps I'm reading the wrong docs) and I think they assume you already know a lot about HTML and parsing, which may be necessary assumptions but are not necessarily true... -- http://mail.python.org/mailman/listinfo/python-list
Re: List loops
On 10/10/2007 1:00 AM, Chris Mellon wrote: > On 10/9/07, Tommy Grav <[EMAIL PROTECTED]> wrote: >> Hi everyone, >> >>I have a list of objects where I have want to do two loops. >> I want to loop over the list and inside this loop, work on all >> the elements of the list after the one being handled in the outer >> loop. I can of course do this with indexes: >> >> >>> alist = range(3) >> >>> for i in xrange(len(alist)): >> ... for j in xrange(i+1,len(alist)): >> ... print i,j,alist[i],alist[j] >> ... >> 0 1 0 1 >> 0 2 0 2 >> 1 2 1 2 >> >>> >> >> >> Is there a way to do this without using indexes? >> > for idx, i in enumerate(alist): > ... for jdx, j in enumerate(range(1,4)): # BZZT Holy hardwired hogwash, Batman! > ... print idx, jdx, i, j > ... > 0 0 0 1 > 0 1 0 2 > 0 2 0 3 # BZZZT no such animal as 3 > 1 0 1 1 # BZZZT "after"??? > 1 1 1 2 > 1 2 1 3 # BZZZT no such animal as 3 > 2 0 2 1 > 2 1 2 2 > 2 2 2 3 # BZZZT "after"??? -- http://mail.python.org/mailman/listinfo/python-list
Re: List loops
On 10/10/2007 1:33 AM, Hamilton, William wrote: >> From: Tommy Grav >> >> Hi everyone, >> >>I have a list of objects where I have want to do two loops. >> I want to loop over the list and inside this loop, work on all >> the elements of the list after the one being handled in the outer The man said "after" ... > alist = range(3) for index, i in enumerate(alist): > for jndex, j in enumerate(alist[index:]): ... so you need index+1 ... > print index, jndex, i, j > > > 0 0 0 0 ... to avoid the above unwanted output. -- http://mail.python.org/mailman/listinfo/python-list
Re: List loops
On 10/10/2007 12:30 AM, Tommy Grav wrote: > Hi everyone, > > I have a list of objects where I have want to do two loops. > I want to loop over the list and inside this loop, work on all > the elements of the list after the one being handled in the outer > loop. I can of course do this with indexes: > > >>> alist = range(3) If you REALLY want this to work ONLY on lists of the form range(n), then say so. Otherwise give a generic example e.g. alist = ['foo', 42, 3.14159] so that the channel is not clogged with red-herring responses :-) > >>> for i in xrange(len(alist)): > ... for j in xrange(i+1,len(alist)): > ... print i,j,alist[i],alist[j] > ... > 0 1 0 1 > 0 2 0 2 > 1 2 1 2 > >>> > > > Is there a way to do this without using indexes? > Probably not efficiently, if your list size is huge and you want to avoid making copies of sub-lists e.g. alist[i+1:]. A somewhat more efficient version of your code: n = len(any_old_list) for i in xrange(n-1): # n-1 avoids possible problems if you need to use i here for j in xrange(i+1, n): # etc How big is n likely to be? -- http://mail.python.org/mailman/listinfo/python-list
Re: Unexpected behaviour with HTMLParser...
Just Another Victim of the Ambient Morality schrieb: > HTMLParser is behaving in, what I find to be, strange ways and I would > like to better understand what it is doing and why. > > First, it doesn't appear to translate HTML escape characters. I don't > know the actual terminology but things like & don't get translated into > & as one would like. Furthermore, not only does HTMLParser not translate it > properly, it seems to omit it altogether! This prevents me from even doing > the translation myself, so I can't even working around the issue. > Why is it doing this? Is there some mode I need to set? Can anyone > else duplicate this behaviour? Is it a bug? Without code, that's hard to determine. But you are aware of e.g. handle_entityref(name) handle_charref(ref) ? > Secondly, HTMLParser often calls handle_data() consecutively, without > any calls to handle_starttag() in between. I did not expect this. In HTML, > you either have text or you have tags. Why split up my text into successive > handle_data() calls? This makes no sense to me. At the very least, it does > this in response to text with & like escape sequences (or whatever > they're called), so that it may successively avoid those translations. That's the way XML/HTML is defined - there is no guarantee that you get text as whole. If you must, you can collect the snippets yourself, and on the next end-tag deliver them as whole. > Again, why is it doing this? Is there some mode I need to set? Can > anyone else duplicate this behaviour? Is it a bug? No. It's the way it is, because it would require buffering with unlimited capacity to ensure this property. > These are serious problems for me and I would greatly appreciate a > deeper understanding of these issues. HTH, and read the docs. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
>> who says that timezones have to be separated by one hour each? > > The Earth says. It takes 24 hours to revolve. Your kidding me, aren't you? Beside the fact that the earth does not revolve in 24h (leap seconds, if you like to google), even the 24 hours of a day are as arbitrary as you prefer. I'm writing this at 23:26, but I bet quite a few people call that 11:26PM. So - who's right? >> Why aren't they separated by 30minutes, or 20, or 10? Or 2 hours? > > Why isn't an hour defined to be 30 minutes? > >> Or why don't we have a global time? > > Like UTC? Which I'm sure is the time you have on your wristwatch? > Not the same way at all. The 25 timezones I speak of are > not merely an abstraction, but related to longitude. Again: BS. Deciding that the the actual noon (the time the sun is at it's highest point) is the 12:00 on my clock was defined by someone. It's sensible, but arbitrary. As is the decision about the width of such a timezone. >> as are the 400 apparently in use by people all over the world > > Where the correlation to longitude is much looser. > Granted, it doesn't need to be for non-navigational > purposes. And although governments can legislate things > like DST, they can't legislate longitude. They can. The greniwch meridian is nothing but a convention. >> - and last time I checked, there was no >> fundamental law in physics or such that limited the allowed or sensible >> number of timezones... > > Isn't there some law somewhere that says the circumference > of a sphere is 360deg? Doesn't that same law mean that no two > points on a sphere can be seperated by more than 180deg > longitude? Doesn't that make GMT+13 non-sensible? Others have given you reasons why it _is_ sensible to have GMT+13. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
>> who says that timezones have to be separated by one hour each? > > The Earth says. It takes 24 hours to revolve. Wrong >> Why aren't they separated by 30minutes, or 20, or 10? Or 2 hours? > > Why isn't an hour defined to be 30 minutes? > >> Or why don't we have a global time? > > Like UTC? > >> Your 25 timezones are an abstraction the same way > > Not the same way at all. The 25 timezones I speak of are > not merely an abstraction, but related to longitude. > >> as are the 400 apparently in use by people all over the world > > Where the correlation to longitude is much looser. > Granted, it doesn't need to be for non-navigational > purposes. And although governments can legislate things > like DST, they can't legislate longitude. > >> - and last time I checked, there was no >> fundamental law in physics or such that limited the allowed or sensible >> number of timezones... > > Isn't there some law somewhere that says the circumference > of a sphere is 360deg? Doesn't that same law mean that no two > points on a sphere can be seperated by more than 180deg > longitude? Doesn't that make GMT+13 non-sensible? > >> Diez > -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Python and C classes in a module
On 10/10/2007 12:56 AM, Stefan Arentz wrote: > Is it possible to mix classes defined in both Python and C in the same > module? Ideally I would like to be able to do: > > from some.module import MyPythonClass, MyCClass > > I guess that would mean that this would look like this on disk: > > some/ >__init__.py >module.py (contains MyPythonClass) >module.so (contains MyCClass) > Alternative to other suggestions: Instead of module.so, call it _module.so. Then down the end of module.py: try: from _module import * except ImportError: # either panic or shrug as appropriate Note: the "shrug" response would be appropriate for the use case where "module" contains all necessary functionality, and the optionally-present "_module" contains replacement classes/types and/or functions that are better in some sense e.g. more memory/CPU efficient. HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Unexpected behaviour with HTMLParser...
HTMLParser is behaving in, what I find to be, strange ways and I would like to better understand what it is doing and why. First, it doesn't appear to translate HTML escape characters. I don't know the actual terminology but things like & don't get translated into & as one would like. Furthermore, not only does HTMLParser not translate it properly, it seems to omit it altogether! This prevents me from even doing the translation myself, so I can't even working around the issue. Why is it doing this? Is there some mode I need to set? Can anyone else duplicate this behaviour? Is it a bug? Secondly, HTMLParser often calls handle_data() consecutively, without any calls to handle_starttag() in between. I did not expect this. In HTML, you either have text or you have tags. Why split up my text into successive handle_data() calls? This makes no sense to me. At the very least, it does this in response to text with & like escape sequences (or whatever they're called), so that it may successively avoid those translations. Again, why is it doing this? Is there some mode I need to set? Can anyone else duplicate this behaviour? Is it a bug? These are serious problems for me and I would greatly appreciate a deeper understanding of these issues. Thank you... -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
On Oct 9, 2007, at 3:32 PM, . wrote: > On Tue, 09 Oct 2007 19:20:06 +, gnuist006 wrote: > >> On Oct 8, 11:09 pm, "." <[EMAIL PROTECTED]> wrote: >>> On Tue, 09 Oct 2007 05:15:49 +, gnuist006 wrote: >> >>> Can anyone explain: >>> (1) its origin >>> >>> One of the lambda papers, I think. I don't remember which. >> >> Hey no-name "dot" you are the only one who says its origin is in >> one of the old lambda papers. Give me a reference or someone >> give me a reference. I dont have access to any ACM journals or >> other conferences. So > > I said "I think." Matthias corrected me. They're all on > readscheme.org > ( http://library.readscheme.org/page1.html ) though, and well worth > reading. > > I note that I'm being mocked for not using my real name by someone not > using his/her real name. Thank you, no-name gnuist006, you make me > laugh. Relax, ., I hardly think he was mocking you. He probably assumed that using a . in a sentence as a form of address would be as unintelligible as it has been in these two sentences. Erik Jones Software Developer | Emma® [EMAIL PROTECTED] 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
[EMAIL PROTECTED] wrote in article <[EMAIL PROTECTED]> in comp.lang.functional: > > One of the most lucid explanations of definitional interpreters -- > > including those that are based on continuation-passing -- are > > explained in J. Reynolds' famous 1971 "Definitional Interpreters for > > Higher-Order Functions" paper. (It has been re-published in 1998 in > > HOSC.) The paper also explains how to perform defunctionalization, > > which can be seen as a way to compile (and even hand-compile) > > higher-order programs. > > Matthias, thanks for the reference, but I dont have access to an > engineering library. I would appreciate, if you have access to paper/ > scanner or electronic copy to help many of us out, you are > not just helping me but many will thank you. If nothing else, please use Google. Many will thank you. http://www.google.com/search?hl=en&q=Definitional+Interpreters+for+Higher-Order+Functions&btnG=Search -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig If monads encapsulate effects and lists form a monad, do lists correspond to some effect? Indeed they do, and the effect they correspond to is choice. Wadler 1995, Monads for fn'l programming -- http://mail.python.org/mailman/listinfo/python-list
Re: storing meta data on dictionary keys
On 10/9/07, Andreas Kraemer <[EMAIL PROTECTED]> wrote: > > I sometimes find it useful to store meta data on dictionary keys, like in > the following example: > > class Dict(dict): > def __init__(self,*args,**kw): > self.key_dict = {} > super(Dict,self).__init__(*args,**kw) > def __setitem__(self,k,v): > self.key_dict[k] = k > super(Dict,self).__setitem__(k,v) > def get_key(self,key): > return self.key_dict[key] > > class Str(str): pass > > >>> friends = Dict() > >>> friends[Str('John')] = ['Bill','Jack','Isabelle'] > >>> friends[Str('Jim')] = ['John','Bob'] > >>> friends[Str('Isabelle')] = ['John','Christine','Anabelle'] > >>> friends.get_key('John').hair_color = 'brown' > >>> friends.get_key('Jim').hair_color = 'red' > >>> friends.get_key('Isabelle').hair_color = 'green' > >>> friends > {'Jim': ['John', 'Bob'], 'John': ['Bill', 'Jack', 'Isabelle'], 'Isabelle': > ['John', 'Christine', 'Anabelle']} > >>> friends.get_key('Isabelle').hair_color > 'green' > > A more sensible, realistic example are attributes of graph nodes (e.g. > color, shape, etc) in the networkx package, where node objects are stored > as keys of (nested) dictionaries. > > Is there any particular reason why the built-in dictionary does not define a > get_key() method, but only keys(), iterkeys(), and has_key() ? > > Cheers, > > Andreas > Because, by definition, if you have the key then you don't need to get it from the dict. What you're doing here is conflating 2 mappings into one: string value->person and person->values. Use 2 explicit dicts to make it clear what you're going, or use one dict and store a tuple of values: person, friends = d["John"]. -- http://mail.python.org/mailman/listinfo/python-list
Re: RuntimeWarning: tp_compare
On 10/9/07, Bruno Barberi Gnecco <[EMAIL PROTECTED]> wrote: > I'm getting the following exception when I call an external extension > (pytst): > > /usr/lib/python2.5/threading.py:697: RuntimeWarning: tp_compare didn't return > -1 or -2 for > exception >return _active[_get_ident()] > Traceback (most recent call last): >File "testDataMiner2.py", line 77, in > testPlace() >File "testDataMiner2.py", line 41, in testPlace > data = db.getDescription(event['id']) >File "testDataMiner2.py", line 28, in getDescription > return self.getRow(query, (id,)) >File "../database.py", line 73, in getRow > self.readlock.acquire() >File "/usr/lib/python2.5/threading.py", line 94, in acquire > me = currentThread() >File "/usr/lib/python2.5/threading.py", line 697, in currentThread > return _active[_get_ident()] > UnicodeEncodeError: 'ascii' codec can't encode character u'\xfa' in position > 52: ordinal > not in range(128) > awer > > /usr/lib/python2.5/threading.py(700)currentThread() > -> return _DummyThread() > > > Note that the error occurs *after* the call that I isolated as > affecting it (pytst.scan(), in the case). This doesn't happen for simple, > isolated cases, but googling for "tp_compare threading" shows a lot of > similar issues. Does anybody here know what this could be about? Any ideas > to debug or work around it? > The various thread issues in the traceback aside, it looks like the problem is that you passed a unicode object to pytst, which only accepts plain (ascii) strings. -- http://mail.python.org/mailman/listinfo/python-list
RuntimeWarning: tp_compare
I'm getting the following exception when I call an external extension (pytst): /usr/lib/python2.5/threading.py:697: RuntimeWarning: tp_compare didn't return -1 or -2 for exception return _active[_get_ident()] Traceback (most recent call last): File "testDataMiner2.py", line 77, in testPlace() File "testDataMiner2.py", line 41, in testPlace data = db.getDescription(event['id']) File "testDataMiner2.py", line 28, in getDescription return self.getRow(query, (id,)) File "../database.py", line 73, in getRow self.readlock.acquire() File "/usr/lib/python2.5/threading.py", line 94, in acquire me = currentThread() File "/usr/lib/python2.5/threading.py", line 697, in currentThread return _active[_get_ident()] UnicodeEncodeError: 'ascii' codec can't encode character u'\xfa' in position 52: ordinal not in range(128) awer > /usr/lib/python2.5/threading.py(700)currentThread() -> return _DummyThread() Note that the error occurs *after* the call that I isolated as affecting it (pytst.scan(), in the case). This doesn't happen for simple, isolated cases, but googling for "tp_compare threading" shows a lot of similar issues. Does anybody here know what this could be about? Any ideas to debug or work around it? -- Bruno Barberi Gnecco I can read your mind, and you should be ashamed of yourself. -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
On Tue, 09 Oct 2007 19:20:06 +, gnuist006 wrote: > On Oct 8, 11:09 pm, "." <[EMAIL PROTECTED]> wrote: >> On Tue, 09 Oct 2007 05:15:49 +, gnuist006 wrote: > >> >> > Can anyone explain: >> >> > (1) its origin >> >> One of the lambda papers, I think. I don't remember which. > > Hey no-name "dot" you are the only one who says its origin is in > one of the old lambda papers. Give me a reference or someone > give me a reference. I dont have access to any ACM journals or > other conferences. So I said "I think." Matthias corrected me. They're all on readscheme.org ( http://library.readscheme.org/page1.html ) though, and well worth reading. I note that I'm being mocked for not using my real name by someone not using his/her real name. Thank you, no-name gnuist006, you make me laugh. -- http://mail.python.org/mailman/listinfo/python-list
Re: NUCULAR fielded text searchable indexing
On Oct 9, 3:35 pm, Istvan Albert <[EMAIL PROTECTED]> wrote: > > why would it be late? is the future of you own work not worth the time > it takes to rename it? Compared to all the other work it took ... it > is just a mere inconvenience. >From a sourceforge perspective I think it's more than in inconvenience -- in fact I'm not sure it can be done -- unless I get on the phone with the guys over there and beg for some special exception or something. [btw, I like using sourceforge: lot's of neat packaged services; no real complaints.] The approved http://nucular.sourceforge.net and I don't think they'll approve another project with the same description. > people who invent a name will have a hard time accepting > that it is bad. Well I'd like people to use the software because they think it's good. If the name is a problem it's probably only the first of many reasons they won't want to use it or will not like it because it doesn't match their preconceptions. But if there's some way to change the name easily in the next week or so, I'll consider it anyway. hints? -- Aaron Watters === Why do you hang out with that sadist? Beats me! -- Kliban -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
Steven D'Aprano schrieb: > On Tue, 09 Oct 2007 19:23:37 +0200, Diez B. Roggisch wrote: > >> Your believes aside, this is simply wrong. The statement >> >> a += x >> >> always leads to a rebinding of a to the result of the operation +. > > Not true. Yes, it is. L = [] id(L) > 3083496716L L += [1] id(L) > 3083496716L > > It's the same L, not rebound at all. Just because the __iadd__-operation in the list implemented chose to returen a reference to itself - certainly a sensible choice, I never doubted that - doesn't change the fact that python rebinds the name on the left with whatever __iadd__ returned. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: tinyp2p - trying to get it to work
Marc Christiansen pisze: > > I had a (not so quick) look. The code proves its point (i.e. writing a > very small p2p application is possible), but it is horrible. With only > one server, the code is broken; maybe it works using multiple servers, I > didn't test. A quick fix seems to be to change the line > pr() and [aug(s) for s in aug(pr()[0])] > to > pr() and [aug(s) for s in aug(pr()[0])] or pr([myU]) > > Please don't take tinyp2p.py as an example on writing python. > > HTH Marc Ok, this seems to work "fine" (as much as a program designed to be minimalistic can do) except it can't download files larger than 10kb. It looks like a problem with SimpleXMLRPCServer (or at least I think so...) While trying to download file larger than 10kb the server itself doesn't crash but generates output like this: Exception happened during processing of request from ('127.0.0.1', 44817) Traceback (most recent call last): File "SocketServer.py", line 222, in handle_request self.process_request(request, client_address) File "SocketServer.py", line 241, in process_request self.finish_request(request, client_address) File "SocketServer.py", line 254, in finish_request self.RequestHandlerClass(request, client_address, self) File "SocketServer.py", line 521, in __init__ self.handle() File "BaseHTTPServer.py", line 316, in handle self.handle_one_request() File "BaseHTTPServer.py", line 310, in handle_one_request method() File "SimpleXMLRPCServer.py", line 445, in do_POST self.wfile.write(response) File "socket.py", line 248, in write self.flush() File "socket.py", line 235, in flush self._sock.sendall(buffer) error: (104, 'Connection reset by peer') The client that was trying to make a download generates this: Traceback (most recent call last): File "backup5.py", line 75, in ? (lambda fi:fi.write(proxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc")) File "backup5.py", line 75, in (lambda fi:fi.write(proxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc")) File "/usr/lib/python2.4/xmlrpclib.py", line 1096, in __call__ return self.__send(self.__name, args) File "/usr/lib/python2.4/xmlrpclib.py", line 1383, in __request verbose=self.__verbose File "/usr/lib/python2.4/xmlrpclib.py", line 1147, in request return self._parse_response(h.getfile(), sock) File "/usr/lib/python2.4/xmlrpclib.py", line 1281, in _parse_response p.feed(response) File "/usr/lib/python2.4/xmlrpclib.py", line 527, in feed self._parser.Parse(data, 0) xml.parsers.expat.ExpatError: not well-formed (invalid token): line 5, column 15 Should I post this in a separate thread or maybe one of experts visiting this group will take a look and hopefully solve my problem? Thank you in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 18:08:34 +, Steven D'Aprano wrote: L = [] id(L) > 3083496716L L += [1] id(L) > 3083496716L > > It's the same L, not rebound at all. It *is* rebound. To the same object, but it *is* assigned to `L` and not just mutated in place. In [107]: class A: .: a = list() .: In [108]: class B(A): .: pass .: In [109]: B.a += [42] In [110]: A.a Out[110]: [42] In [111]: B.a Out[111]: [42] If it was just mutation then `B.a` would have triggered an `AttributeError`. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
On Oct 9, 5:50 am, Matthias Blume <[EMAIL PROTECTED]> wrote: > "." <[EMAIL PROTECTED]> writes: > > On Tue, 09 Oct 2007 05:15:49 +, gnuist006 wrote: > > >> Again I am depressed to encounter a fundamentally new concept that I > >> was all along unheard of. Its not even in paul graham's book where i > >> learnt part of Lisp. Its in Marc Feeley's video. > > >> Can anyone explain: > > >> (1) its origin > > One of the lambda papers, I think. I don't remember which. > > This is a common misconception. There is very little that > originated from the "lambda" papers. But they did a marvelous job at > promoting some of the ideas that existed in the PL community for > years. > > As for the concept of continuations, there is Scott and Strachey's > work on denotational semantics, and there is Landin's J operator. > (There's probably more that I am forgetting right now.) > > >> (6) any good readable references that explain it lucidly ? > > One of the most lucid explanations of definitional interpreters -- > including those that are based on continuation-passing -- are > explained in J. Reynolds' famous 1971 "Definitional Interpreters for > Higher-Order Functions" paper. (It has been re-published in 1998 in > HOSC.) The paper also explains how to perform defunctionalization, > which can be seen as a way to compile (and even hand-compile) > higher-order programs. > > Matthias Matthias, thanks for the reference, but I dont have access to an engineering library. I would appreciate, if you have access to paper/ scanner or electronic copy to help many of us out, you are not just helping me but many will thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: NUCULAR fielded text searchable indexing
On Oct 9, 9:14 am, [EMAIL PROTECTED] wrote: > a great tradition of tounge-in-cheek package names, like > "Cold fusion", for example. Cold Fusion is a super cool name. Nobody will every think of it as representing something odd or silly. > too late now. sorry again, why would it be late? is the future of you own work not worth the time it takes to rename it? Compared to all the other work it took ... it is just a mere inconvenience. All I can say please do yourself a favor rename it. Imagine yourself in the position of someone who has no idea what the project is about and try to imagine what kind of thoughts and feelings the name will conjure. It matters a lot. It is a shame to put yourself at disadvantage. Anyway that's all I can say. FWIW the "Devil Framework" guys are sticking to their name, I think this is something that feels too personal, people who invent a name will have a hard time accepting that it is bad. i. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
Steven D'Aprano wrote: > On Tue, 09 Oct 2007 19:23:37 +0200, Diez B. Roggisch wrote: > > >> Your believes aside, this is simply wrong. The statement >> >> a += x >> >> always leads to a rebinding of a to the result of the operation +. >> > > Not true. > Hmm. Or you can write __iadd__ to rebind the name to the same object again? I think usually there is not much difference between "rebinding to the same object" and "not rebinding". But as we have seen, sometimes there is a difference. I think that Diez made his statement because it is possible to rebind from __iadd__ to a different object. So it is more safe to think that "inplace operations" will always rebind, than to think they will never rebind. I'm not sure about the language level. It is told that "+=" is an inplace operator, but how "in place" is defined? If we say "an inplace operator will change the state of an object, and will never create a new object" then "+=" is NOT an inplace operator. Well, at least not when you watch it from the language level. But what about the implementation level? Here is an example: a = 1 a+= 1 # The compiler will probably optimize this and the Python bytecode interpreter will not rebind 'a' here, just increment the integer in memory. This topic starts to be very interesting. Language lawyers and developers, please help us! How "in place operator" is defined in Python? What makes it an "in place operator"? Is it the syntax, or what? Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
> (6) any good readable references that explain it lucidly ? This was something that has been very interesting to me for a while now, and I'm actually still having a difficult time wrapping my head around it completely. The best written explanation that I've come across was in "The Scheme Programming Language" (http://mitpress.mit.edu/catalog/item/ default.asp?ttype=2&tid=9946). But perhaps others have better references. I'll attempt my own little explanation of call/cc. I'll butcher some of it, I'm sure, but hopefully those more knowledgeable will politely correct me. I will start with a loose analogy and point out a couple examples I came across that did make a lot of sense. First, the bad analogy I have (if you are coming from C programming like me) is setjmp and longjmp. This is a bad analogy in that you're talking about hardware and stack states as opposed to functions, but a good analogy in that it saves the current state of execution, and returns to that same state at a later time with a piece of data attached to it. My first example of using this would be to create a return function in Scheme. I hope I don't get this wrong, but the example would be something like this: (define (my-test x) (call/cc (lambda (return) (return x Now, here's my understanding of what is happening under-the-hood: 1. call/cc stores the current execution state and creates a function to restore to that state. 2. call/cc then calls its own argument with the function it created. The key here is that "return" is a function (created by call/cc) taking 1 argument, and it restores execution at the same state it was when the call/cc began (or immediately after it?). This line: (return x) is really just calling the function created by call/cc, which will restore the execution state to what it was just prior to the call/cc, along with a parameter (in this case, the value of x). My next example I don't follow 100%, and I won't attempt to reproduce it here, but it generates a continuation that modifies itself (bad?) to define a list iterator. http://blog.plt-scheme.org/2007/07/callcc-and-self-modifying-code.html I recommend putting that code into a Scheme interpreter and running it. You'll get it. Hope this helps, and I look forward to better explanations than mine that will help me along as well. :) Jeff M. -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
Special thanks to many of you for your very decent replies. On Oct 9, 11:18 am, George Neuner wrote: > On Tue, 09 Oct 2007 05:15:49 -, [EMAIL PROTECTED] wrote: > >Again I am depressed to encounter a fundamentally new concept that I > >was all along unheard of. Its not even in paul graham's book where i > >learnt part of Lisp. Its in Marc Feeley's video. > > >Can anyone explain: > > >(1) its origin > > Lambda calculus. Continuation is just a formal term for "what the > code does next". It manifests, literally, as the next instruction(s) > to be executed. > > >(2) its syntax and semantics in emacs lisp, common lisp, scheme > > Lisp does not have explicit continuations so there is no syntax for > them. Continuations in Lisp mainly take the form of function calls, > function returns, exceptions, conditions, etc. Sometimes code is > written in "continuation passing style" (CPS) in which each function > has one or more additional function parameters (the continuations) - > the function terminates by passing its result as an argument to one of > those continuation functions. > > Scheme has explicit continuations based on closures. Closure > continuations are created using CALL-WITH-CURRENT-CONTINUATION > (usually abbreviated as CALL/CC). Some Schemes also recognize a > LET/CC form used mainly for escape continuations (exceptions). > Scheme's closure continuations can be stored in data structures and > used for complex control forms such as multitasking. Like Lisp, > Scheme code also is sometimes written using CPS. > > >(3) Is it present in python and java ? > > It is present in all languages. It generally takes the form of > procedure or function calls, returns, exceptions, etc. > > >(4) Its implementation in assembly. for example in the manner that > >pointer fundamentally arises from indirect addressing and nothing new. > >So how do you juggle PC to do it. > > As I stated above, every function call or return _is_ a continuation > ... their implementation is obvious. > > For the closure form used in Scheme, the implementation is to create a > closure, a data structure containing the function address and some > method of accessing the function's free variables, and to call the > function. How you do this depends greatly on the instruction set. > > >(5) how does it compare to and superior to a function or subroutine > >call. how does it differ. > > Calling closure continuations is a little more complicated and a bit > slower than calling a normal function. Creating the closure in the > first place may be simple or complicated depending on the complexity > of the source code and the processor's instruction set. > > >Thanks a lot. > > >(6) any good readable references that explain it lucidly ? > > Get yourself a good textbook on compilers. Most of the techniques are > applicable to all languages - even for seemingly very different > languages, the differences in their compilers are simply in how the > basic compilation techniques are combined. > > My favorite intermediate-level books are > > Aho, Sethi & Ullman. "Compilers: Principles, Techniques and Tools". > 2nd Ed. 2006. ISBN 0-321-48681-1. > The first edition from 1986, ISBN 0-201-10088-6, is also worth having > if you can still find it. The 1st edition is mainly about procedural > languages, the 2nd gives more time to functional languages and modern > runtime issues like GC and virtual machines. > > Cooper & Torczon, "Engineering a Compiler", 2004. > ISBN 1-55860-698-X (hardcover), 1-55860-699-8 (paperback). > Also available as a restricted 90-day ebook > fromhttp://rapidshare.com/files/24382311/155860698X.Morgan_20Kaufmann.Eng... > > There are also some decent intro books available online. They don't > go into excruciating detail but they do cover the basics of code > shaping which is what you are interested in. > > Torben Mogensen. "Basics of Compiler > Design"http://www.diku.dk/~torbenm/Basics/ > > "Engineering a Compiler". I don't have this author's name, nor can > Google find it at the moment. I have a copy though (~2MB) - if you > are interested, contact me by email and I'll send it to you. > > Also Google for free CS books. Many older books (including some > classics) that have gone out of print have been released > electronically for free download. > > George > -- > for email reply remove "/" from address -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
On Oct 8, 11:09 pm, "." <[EMAIL PROTECTED]> wrote: > On Tue, 09 Oct 2007 05:15:49 +, gnuist006 wrote: > > > Can anyone explain: > > > (1) its origin > > One of the lambda papers, I think. I don't remember which. Hey no-name "dot" you are the only one who says its origin is in one of the old lambda papers. Give me a reference or someone give me a reference. I dont have access to any ACM journals or other conferences. So step 1 reference and the idea in it step 2 if you can upload it This is for historical and conceptual development at the same time as learning the ideas to use them. Thanks a lot. -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
On 10/9/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > On Oct 9, 8:34 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > [EMAIL PROTECTED] wrote: > > > On Oct 8, 1:03 pm, Carsten Haese <[EMAIL PROTECTED]> wrote: > > >> On Mon, 2007-10-08 at 10:41 -0700, [EMAIL PROTECTED] wrote: > > >> > For example, Windows has seperate listings for > > > > >> > Central America > > >> > Central Time (US & Canada) > > >> > Guadalahara, Mexico City, Monterry - New > > >> > Guadalahara, Mexico City, Monterry - Old > > >> > Saskatchewan > > > > >> > but they are all GMT-6 > > > > >> But they could have different rules for Daylight Saving Time. > > > > > Which only matters if you're setting your clock. > > > > That's BS. If I'm supposed to be attending a video-conference that spans a > > few continents which is scheduled using a web-app, it's VITAL that I get > > the invitation and reminder rendered in MY local timezone, DST included. > > > > And for the matter of > > > > """ > > There are only 25 timezones: -12, -11, ... -1, 0 (GMT), +1, ... +11, > > +12. > > """ > > > > who says that timezones have to be separated by one hour each? > > The Earth says. It takes 24 hours to revolve. It only takes 24 hours for the Earth to revolve once because we defined an hour as 1/24 of the time it takes for the Earth to revolve once. We could have said an hour was 1/10 that time, or 1/2, or 1/27.284. > > > Or why don't we have a global time? > > Like UTC? What about GMT? I hear that much more than UTC. > > > > > Your 25 timezones are an abstraction the same way > > Not the same way at all. The 25 timezones I speak of are > not merely an abstraction, but related to longitude. > > > as are the 400 apparently in use by people all over the world > > Where the correlation to longitude is much looser. > Granted, it doesn't need to be for non-navigational > purposes. And although governments can legislate things > like DST, they can't legislate longitude. > But your 25 timezones are only useful to the people that use those 25 timezones. And the time zone I use is not one of those 25 timezones. > > - and last time I checked, there was no > > fundamental law in physics or such that limited the allowed or sensible > > number of timezones... > > Isn't there some law somewhere that says the circumference > of a sphere is 360deg? Doesn't that same law mean that no two > points on a sphere can be seperated by more than 180deg > longitude? Doesn't that make GMT+13 non-sensible? A timezone is an arbitrary geographical designation. It has nothing to do with latitude or longitude. While some time zones may be defined as a geographical region between two longitudes, others may be defined by geographical borders or convienent terrain features. Take a look at the international date line. It doesn't follow a longitudinal line, but instead jogs east around Asia and then west around the Aleutian Islands. -- -Bill Hamilton -- http://mail.python.org/mailman/listinfo/python-list
Fwd: NUCULAR fielded text searchable indexing
On 10/9/07, Grant Edwards <[EMAIL PROTECTED]> wrote: > On 2007-10-09, Robin Becker <[EMAIL PROTECTED]> wrote: > > [EMAIL PROTECTED] wrote: > >> On Oct 9, 8:46 am, Istvan Albert <[EMAIL PROTECTED]> wrote: > >>> ps. there is a python project named "The Devil Framework", I cringe > >>> every time I hear about it.Nucularis not as bad, but it is close. > >> > >> Aw shucks. I thought it was funny. Can't I make fun of > >> politicians in my open source projects? Besides there is > >> a great tradition of tounge-in-cheek package names, like > >> "Cold fusion", for example. > >>... > > > > I think it's an excellent name :) > > And Bush would probably pronounce it "Nuke-lee-ur". I dislike Bush as much as the next guy, but could we please keep politics off the group? -- -Bill Hamilton -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
On 10/9/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > Why aren't they separated by 30minutes, or 20, or 10? Or 2 hours? > > Why isn't an hour defined to be 30 minutes? > > > Or why don't we have a global time? > > Like UTC? > > > > > Your 25 timezones are an abstraction the same way > > Not the same way at all. The 25 timezones I speak of are > not merely an abstraction, but related to longitude. > See, here's what the problem is. You've invented your own definition of "timezone", which has no relation to how anyone else uses the word, and are now arguing loudly and rudely about how your pet definition is right, and everyone else is wrong. You should have just said at the beginning that you were just redefining all the terms to match your preference, so I could have killfilled you then instead of reading all your posts in hope that you actually had something important and useful to say. -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
On Tue, Oct 09, 2007 at 11:21:41AM -0700, [EMAIL PROTECTED] wrote regarding Re: pytz has so many timezones!: > > The Earth says. It takes 24 hours to revolve. > > > Why aren't they separated by 30minutes, or 20, or 10? Or 2 hours? > > Why isn't an hour defined to be 30 minutes? > > > Or why don't we have a global time? > > Like UTC? > > > > > Your 25 timezones are an abstraction the same way > > Not the same way at all. The 25 timezones I speak of are > not merely an abstraction, but related to longitude. > > > as are the 400 apparently in use by people all over the world > > Where the correlation to longitude is much looser. > Granted, it doesn't need to be for non-navigational > purposes. And although governments can legislate things > like DST, they can't legislate longitude. > > > - and last time I checked, there was no > > fundamental law in physics or such that limited the allowed or sensible > > number of timezones... > > Isn't there some law somewhere that says the circumference > of a sphere is 360deg? Doesn't that same law mean that no two > points on a sphere can be seperated by more than 180deg > longitude? Doesn't that make GMT+13 non-sensible? > You seem to be talking about time zones as if they are a scientific abstraction based on the physical layout of the earth. They are not. They are an abstraction away from true scientific (solar) time to give us regular 24 hour days, and to simplify calculation to make sure that trains don't run into one another for having left their respective stations at times based on locally defined solar noon. Solar time is the only kind of time that doesn't have to take political considerations into account. GMT+13 is not non-sensible at all, if the major trade partners of the island in question are at GMT+12. Imagine the confusion not being able to schedule meetings on monday or friday because your next door neighbor, one time zone away, is actually off-calendar from you by one day. The IDL was arbitrarily placed in the middle of the pacific to limit this problem to as few people as possible, but the people of Kiribati have no reason to accept the disadvantage under which this (European) abstraction places them. What would be non-sensible is for them to live 23 hours offset from their closest neighbors and family, while living a mere three hours offset from people that they have minimal contact with. Cheers, Cliff -- http://mail.python.org/mailman/listinfo/python-list
Re: NUCULAR fielded text searchable indexing
On 2007-10-09, Robin Becker <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: >> On Oct 9, 8:46 am, Istvan Albert <[EMAIL PROTECTED]> wrote: >>> ps. there is a python project named "The Devil Framework", I cringe >>> every time I hear about it.Nucularis not as bad, but it is close. >> >> Aw shucks. I thought it was funny. Can't I make fun of >> politicians in my open source projects? Besides there is >> a great tradition of tounge-in-cheek package names, like >> "Cold fusion", for example. >>... > > I think it's an excellent name :) And Bush would probably pronounce it "Nuke-lee-ur". -- Grant Edwards grante Yow! I like your SNOOPY at POSTER!! visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
On Oct 9, 8:34 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > On Oct 8, 1:03 pm, Carsten Haese <[EMAIL PROTECTED]> wrote: > >> On Mon, 2007-10-08 at 10:41 -0700, [EMAIL PROTECTED] wrote: > >> > For example, Windows has seperate listings for > > >> > Central America > >> > Central Time (US & Canada) > >> > Guadalahara, Mexico City, Monterry - New > >> > Guadalahara, Mexico City, Monterry - Old > >> > Saskatchewan > > >> > but they are all GMT-6 > > >> But they could have different rules for Daylight Saving Time. > > > Which only matters if you're setting your clock. > > That's BS. If I'm supposed to be attending a video-conference that spans a > few continents which is scheduled using a web-app, it's VITAL that I get > the invitation and reminder rendered in MY local timezone, DST included. > > And for the matter of > > """ > There are only 25 timezones: -12, -11, ... -1, 0 (GMT), +1, ... +11, > +12. > """ > > who says that timezones have to be separated by one hour each? The Earth says. It takes 24 hours to revolve. > Why aren't they separated by 30minutes, or 20, or 10? Or 2 hours? Why isn't an hour defined to be 30 minutes? > Or why don't we have a global time? Like UTC? > > Your 25 timezones are an abstraction the same way Not the same way at all. The 25 timezones I speak of are not merely an abstraction, but related to longitude. > as are the 400 apparently in use by people all over the world Where the correlation to longitude is much looser. Granted, it doesn't need to be for non-navigational purposes. And although governments can legislate things like DST, they can't legislate longitude. > - and last time I checked, there was no > fundamental law in physics or such that limited the allowed or sensible > number of timezones... Isn't there some law somewhere that says the circumference of a sphere is 360deg? Doesn't that same law mean that no two points on a sphere can be seperated by more than 180deg longitude? Doesn't that make GMT+13 non-sensible? > > Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
On Tue, 09 Oct 2007 05:15:49 -, [EMAIL PROTECTED] wrote: >Again I am depressed to encounter a fundamentally new concept that I >was all along unheard of. Its not even in paul graham's book where i >learnt part of Lisp. Its in Marc Feeley's video. > >Can anyone explain: > >(1) its origin Lambda calculus. Continuation is just a formal term for "what the code does next". It manifests, literally, as the next instruction(s) to be executed. >(2) its syntax and semantics in emacs lisp, common lisp, scheme Lisp does not have explicit continuations so there is no syntax for them. Continuations in Lisp mainly take the form of function calls, function returns, exceptions, conditions, etc. Sometimes code is written in "continuation passing style" (CPS) in which each function has one or more additional function parameters (the continuations) - the function terminates by passing its result as an argument to one of those continuation functions. Scheme has explicit continuations based on closures. Closure continuations are created using CALL-WITH-CURRENT-CONTINUATION (usually abbreviated as CALL/CC). Some Schemes also recognize a LET/CC form used mainly for escape continuations (exceptions). Scheme's closure continuations can be stored in data structures and used for complex control forms such as multitasking. Like Lisp, Scheme code also is sometimes written using CPS. >(3) Is it present in python and java ? It is present in all languages. It generally takes the form of procedure or function calls, returns, exceptions, etc. >(4) Its implementation in assembly. for example in the manner that >pointer fundamentally arises from indirect addressing and nothing new. >So how do you juggle PC to do it. As I stated above, every function call or return _is_ a continuation ... their implementation is obvious. For the closure form used in Scheme, the implementation is to create a closure, a data structure containing the function address and some method of accessing the function's free variables, and to call the function. How you do this depends greatly on the instruction set. >(5) how does it compare to and superior to a function or subroutine >call. how does it differ. Calling closure continuations is a little more complicated and a bit slower than calling a normal function. Creating the closure in the first place may be simple or complicated depending on the complexity of the source code and the processor's instruction set. >Thanks a lot. > >(6) any good readable references that explain it lucidly ? Get yourself a good textbook on compilers. Most of the techniques are applicable to all languages - even for seemingly very different languages, the differences in their compilers are simply in how the basic compilation techniques are combined. My favorite intermediate-level books are Aho, Sethi & Ullman. "Compilers: Principles, Techniques and Tools". 2nd Ed. 2006. ISBN 0-321-48681-1. The first edition from 1986, ISBN 0-201-10088-6, is also worth having if you can still find it. The 1st edition is mainly about procedural languages, the 2nd gives more time to functional languages and modern runtime issues like GC and virtual machines. Cooper & Torczon, "Engineering a Compiler", 2004. ISBN 1-55860-698-X (hardcover), 1-55860-699-8 (paperback). Also available as a restricted 90-day ebook from http://rapidshare.com/files/24382311/155860698X.Morgan_20Kaufmann.Engineering_20a_20Compiler.pdf There are also some decent intro books available online. They don't go into excruciating detail but they do cover the basics of code shaping which is what you are interested in. Torben Mogensen. "Basics of Compiler Design" http://www.diku.dk/~torbenm/Basics/ "Engineering a Compiler". I don't have this author's name, nor can Google find it at the moment. I have a copy though (~2MB) - if you are interested, contact me by email and I'll send it to you. Also Google for free CS books. Many older books (including some classics) that have gone out of print have been released electronically for free download. George -- for email reply remove "/" from address -- http://mail.python.org/mailman/listinfo/python-list
storing meta data on dictionary keys
I sometimes find it useful to store meta data on dictionary keys, like in the following example: class Dict(dict): def __init__(self,*args,**kw): self.key_dict = {} super(Dict,self).__init__(*args,**kw) def __setitem__(self,k,v): self.key_dict[k] = k super(Dict,self).__setitem__(k,v) def get_key(self,key): return self.key_dict[key] class Str(str): pass >>> friends = Dict() >>> friends[Str('John')] = ['Bill','Jack','Isabelle'] >>> friends[Str('Jim')] = ['John','Bob'] >>> friends[Str('Isabelle')] = ['John','Christine','Anabelle'] >>> friends.get_key('John').hair_color = 'brown' >>> friends.get_key('Jim').hair_color = 'red' >>> friends.get_key('Isabelle').hair_color = 'green' >>> friends {'Jim': ['John', 'Bob'], 'John': ['Bill', 'Jack', 'Isabelle'], 'Isabelle': ['John', 'Christine', 'Anabelle']} >>> friends.get_key('Isabelle').hair_color 'green' A more sensible, realistic example are attributes of graph nodes (e.g. color, shape, etc) in the networkx package, where node objects are stored as keys of (nested) dictionaries. Is there any particular reason why the built-in dictionary does not define a get_key() method, but only keys(), iterkeys(), and has_key() ? Cheers, Andreas -- http://mail.python.org/mailman/listinfo/python-list
SOAPpy and ArrayOfKeyValue
Can anyone offer any assistance as to how to convert a basic python dictionary, list, or even tuple into the SOAP type "ArrayOfKeyValue"? I am currently using SOAPpy, but would be willing to change to ZSI or something else if it made this conversion easier. I have tried with the arrayType and structType methods in SOAPpy.Types, yet they don't seem to do what I need. I suspect there might not be just a single method call, so, I wrote something like this: def dictToKeyValue(self, mydict): soap_list = [] for key,value in mydict.items(): inner = [] inner.append(SOAPpy.stringType(key,'key')) inner.append(SOAPpy.stringType(value,'value')) soap_list.append(SOAPpy.arrayType(inner)) print soap_list return SOAPpy.arrayType(soap_list) As you can see, it's pretty nasty and doesn't do what I need either. I have a client that was written in PHP which I am converting. It uses this function (which calls the PEAR SOAP library): function assocArrayToKeyValue($array) { $soap_array = array(); foreach($array as $key=>$value) { $inner = array(); $inner[] =& new SOAP_Value('key', 'string', $key); $inner[] =& new SOAP_Value('value', 'string', $value); $soap_array[] =& new SOAP_Value("item", "{{$this->wsdl_urn}}KeyValue", $inner); } return $soap_array; } Unfortunately, I don't really have a way to see exactly what $soap_array is so that I could emulate it in my Python code. Any pointers or suggestions are very much appreciated. /alex -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: generator_tools-0.1 released
On Oct 9, 7:17 pm, Steven Bethard <[EMAIL PROTECTED]> wrote: > Kay Schluehr wrote: > > Originally I came up with the idea of a pure Python implementation for > > copyable generators as an ActiveState Python Cookbook recipe. Too bad, > > it was badly broken as Klaus Müller from the SimPy project pointed > > out. Two weeks and lots of tests later I got finally a running version > > that works not only in the very most simple use cases. As the code > > size increased it turned out to not really fit anymore into the > > "recipe" category and I created a package called "generator_tools". > > > generator_tools-0.1 is available for both Python 2.5 and Python 3.0 > > (!) > [snip] > pickler = GeneratorPickler("test.pkl") > pickler.pickle_generator(g_gen) > k_gen = pickler.unpickle_generator() > list(g_gen) == list(k_gen) > > True > > This seems like an odd syntax for pickling. Does the package support the > normal pickle machinery? That is, does it register the appropriate > methods with the copy_reg module? > > STeVe The picklegenerators module defines two functions: pickle_generator that wraps the generator into a GeneratorSnapshot object and unpickle_generator that calls copy_generator on the unpickled GeneratorSnapshot. The only reason I also created the GeneratorPickler class was some simplification of testing. So I didn't invest time yet keeping the API in line with Pythons normal pickle machinery but will do so as the project advances i.e. in the next release. Kay -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 19:23:37 +0200, Diez B. Roggisch wrote: > Your believes aside, this is simply wrong. The statement > > a += x > > always leads to a rebinding of a to the result of the operation +. Not true. >>> L = [] >>> id(L) 3083496716L >>> L += [1] >>> id(L) 3083496716L It's the same L, not rebound at all. > I presume you got confused by the somewhat arbitrary difference between > > __add__ > > and > > __iadd__ > > that somehow suggest there is an in-place-modification going on in case > of mutables. The __iFOO__ methods are supposed to do in-place modification if possible, but it is up to the class writer to make them do so. In the case of your example, you specifically created an __iadd__ method that didn't even attempt in-place modification. What did you expect it to do? > but as the following snippet shows - that's not the case: > > > class Foo(object): > def __add__(self, o): > return "__add__" > > def __iadd__(self, o): > return "__iadd__" > > > a = Foo() > a += 1 > print a > > a = Foo() > b = Foo() > c = a + b > print c > > So you see, the first += overrides a with the returned value of > __iadd__. That's because you told it to do that. If you told it to do something more sensible, it would have done so. Lists know how to do in-place modification: >>> id(L) # from above 3083496716L >>> L *= 5 >>> id(L) 3083496716L >>> L = L*5 >>> id(L) 3083496972L -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
mutable objects as dictionary keys
Hi everyone, I know that the subject of mutable objects as dictionary keys has been discussed a number of times in this forum (see for instance "freezing" of classes), but I would love to hear the thoughts of the experts on the approach below. The use case that I encounter frequently is the classification of objects according to certain rules or properties: Say, I have objects A, B, C, ... (e.g. instances of a class, dict, etc), I can write d = {} d.setdefault(A,[]).append(A) d.setdefault(B,[]).append(B) ... so objects that map to the same hash key will end up in the same bucket. (A "real world" example I had to deal with recently was for instance the construction of a directed acyclic graph from many directed trees where identical subtrees needed to be identified.) The easiest way is of course to define custom __hash__() and __eq__() methods, but this breaks if objects are mutated (accidentally) after having been inserted into the dictionary. The "best" working approach I came up with so far is to generate an "immutable view" V of a mutable object O according to my classification rule, delegate O.__hash__ and O.__eq__ to V, and make sure that the V is memoized and cannot (easily) be altered later, even when O is mutated: def hashable_mutable_factory(mutable,rule): class _mutable(mutable): def __init__(self,*args,**kw): self._view_cache = {} super(_mutable,self).__init__(*args,**kw) def _view(self): id_ = id(self) if not self._view_cache.has_key(id_): self._view_cache[id_] = rule(self) return self._view_cache[id_] def __hash__(self): return hash(self._view()) def __eq__(self,other): return self._view() == other._view() return _mutable E.g.: >>> hashable_dict = hashable_mutable_factory(dict,lambda obj: >>> frozenset(obj.iteritems())) >>> h = hashable_dict(a=1,b=2) >>> d = {} >>> d[h] = 'foo' >>> d {{'a': 1, 'b': 2}: 'foo'} >>> h['c'] = 'bar' >>> d {{'a': 1, 'c': 'bar', 'b': 2}: 'foo'} >>> g = hashable_dict(a=1,b=2) >>> h {'a': 1, 'c': 'bar', 'b': 2} >>> g {'a': 1, 'b': 2} >>> id(g) == id(h) False >>> g == h True I slightly favor the factory function idiom above over defining the rule in a super class (this would have to be done for each mutable type and rule function separately), especially since I read that future versions of python (2.6 ?, 3.0 ?) will contain class decorators and allow syntax like class A(*bases): pass Is there a better approach? Any comments are appreciated. I have been seriously using Python for one year know, mostly in the context of graph algorithms etc., and it has always been a delightful coding experience! Best regards, Andreas -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory leak/gc.get_objects()/Improved gc in version 2.5
On Oct 9, 7:54 am, "Chris Mellon" <[EMAIL PROTECTED]> wrote: > On 10/8/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > I'm running a python program that simulates a wireless network > > protocol for a certain number of "frames" (measure of time). I've > > observed the following: > > > 1. The memory consumption of the program grows as the number of frames > > I simulate increases. > > > To verify this, I've used two methods, which I invoke after every > > frame simulated: > > > -- Parsing the /proc//status file as in: > >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286222 > > -- Using ps vg | grep python | awk '!/grep/ {print " ",$8}' in an > > os.system() call. > > > The memory usage vs. frame number graph shows some big "jumps" at > > certain points, and, after a large number of frames, shows a steady > > upward slope > > This would be expected if you're creating ever-larger amounts of > objects - python uses memory pools and as the number of simultaneous > objects increases, the size of the pool will need to increase. This > isn't expected if the total number of objects you create is pretty > much static, but the way you're trying to determine that is flawed > (see below). > > > > > 2. I think I've verified that the objects I instantiate are actually > > freed-- I'm therefore assuming that this "leak" is "caused" by > > python's garbage collection mechanism. I count the number of objects I > > generate that are being tracked by gc as follows: > > > gc.collect() > > objCount = {} > > objList = gc.get_objects() > > for obj in objList: > > if getattr(obj, "__class__", None): > > name = obj.__class__.__name__ > > if objCount.has_key(name): > > objCount[name] += 1 > > else: > > objCount[name] = 1 > > > for name in objCount: > > print name, " :", objCount[name] > > >del objList > > > Running this snippet every hundred frames or so, shows that the number > > of objects managed by gc is not growing. > > > I upgraded to Python 2.5. in an attempt to solve this problem. The > > only change in my observations from version 2.4 is that the absolute > > memory usage level seems to have dropped. However, I still see the > > jumps in memory usage at the same points in time. > > > Can anybody explain why the memory usage shows significant jumps (~200 > > kB or ~500 kb) over time (i.e. "frames") even though there is no > > apparent increase in the objects managed by gc? Note that I'm calling > > gc.collect() regularly. > > You're misunderstanding the purpose of Pythons GC. Python is > refcounted. The GC exists only to find and break reference cycles. If > you don't have ref cycles, the GC doesn't do anything and you could > just turn it off. > > gc.get_objects() is a snapshot of the currently existing objects, and > won't give you any information about peak object count, which is the > most direct correlation to total memory use. > > > Thanks for your attention, > > > Arvind > > > -- > >http://mail.python.org/mailman/listinfo/python-list Chris, Thanks for your reply. To answer the earlier question, I used CPython 2.4.3 and ActivePython 2.5.1 in my analysis above. No custom modules added. Interpreter banners are at the end of this message. In my program, I do keep instantiating new objects every "frame". However, these objects are no longer needed after a few frames, and the program no longer maintains a reference to old objects. Therefore, I expect the reference-counting mechanism built into python (whatever it is, if not gc) to free memory used by these objects and return it to the "pool" from which they were allocated. Further, I would expect that in time, entire pools would become free, and these free pools should be reused for new objects. Therefore the total number of pools allocated (and therefore "arenas"?) should not grow over time, if pools are being correctly reclaimed. Is this not expected behavior? Also, since I sample gc.get_objects() frequently, I would expect that I would stumble upon a "peak" memory usage snapshot, or at the very least see a good bit of variation in the output. However, this does not occur. Finally, if I deliberately hold on to references to old objects, gc.get_objects() clearly shows an increasing number of objects being tracked in each snapshot, and the memory leak is well explained. Python version info: ActivePython 2.5.1.1 (ActiveState Software Inc.) based on Python 2.5.1 (r251:54863, May 2 2007, 08:46:07) [GCC 3.3.4 (pre 3.3.5 20040809)] on linux2 AND Python 2.4.3 (#1, Mar 14 2007, 19:01:42) [GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2 Arvind -- http://mail.python.org/mailman/listinfo/python-list
Re: pytz has so many timezones!
On Oct 9, 2:58 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Mon, 08 Oct 2007 10:41:03 -0700, "[EMAIL PROTECTED]" > <[EMAIL PROTECTED]> declaimed the following in comp.lang.python: > > > > > There are only 25 timezones: -12, -11, ... -1, 0 (GMT), +1, ... +11, > > +12. > > Uhm... -12 and +12 would be the same, wouldn't they? No, they are seperated by the International Date Line, so although zones -12 & +12 have the same clock time, they are different dates. > > There are only 24 divisions 15deg wide in longitude, 7.5deg to each > size of zone center, and Greenwich is center of 0... And zones -12 & +12 are each nominally 7.5deg wide, so 23*15 + 2*7.5 gives you 25 zones spanning 360deg. > > However, consider that the former Soviet Union is reputed to have > used only ONE timezone for the entire width of the country (sunrise at > 6AM say in Moscow would be near sunset on the east coast) When was this, in the days before air travel when it took 3 weeks to cross Siberia by train? > > So while there are only 24 hourly offsets, there can be many "zones" > to reflect variations in daylight time changes or non-standard > offsets... The point I was trying to make is that although there may be 400 zones, there aren't 400 offsets. And aren't these offsets part of the timezone record? > -- > WulfraedDennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
> In point #3, you really bind a name to a value. As you probably know, in > Python, there are names and objects. The initial value of the name 'a' > is 1. It is an immutable object. The "+=" operator usually increments a > value of an object. However, because the 'int' type is immutable, the += > operator will rather rebind this variable to a newly created value. I > believe this is what is happening here. Your believes aside, this is simply wrong. The statement a += x always leads to a rebinding of a to the result of the operation +. I presume you got confused by the somewhat arbitrary difference between __add__ and __iadd__ that somehow suggest there is an in-place-modification going on in case of mutables. but as the following snippet shows - that's not the case: class Foo(object): def __add__(self, o): return "__add__" def __iadd__(self, o): return "__iadd__" a = Foo() a += 1 print a a = Foo() b = Foo() c = a + b print c So you see, the first += overrides a with the returned value of __iadd__. The reason for the difference though is most probably what you yourself expected: thus it's possible to alter a mutable in place. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Oct 9, 9:16 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hi. > > I've got a question on the differences and how to define static and > class variables. AFAIK, class methods are the ones which receives the > class itself as an argument, while static methods are the one which > runs statically with the defining class. > > Hence, my understanding is that static variables must be bound to the > class defining the variables and shared by children of parent class > where the variable is defined. But, please have a look at this code in > which a guy told me that the variable a is static: > > >>> class Foo: > > a = 1 > @classmethod > def increment(cls): > cls.a += 1 > print cls.a > > Here, I am defining variable a which, I believe is class variable, > i.e., variable that is not bound to Foo itself. Rather, a is bound to > the class which is accessing the variable. The code that corroborates > this idea is as follows: > > >>> class Child1(Foo): > > pass > > >>> Child1.increment() > > 4 > > >>> class Child2(Foo): > > pass > > >>> Child2.increment() > > 4 > > This means that Child1 and Child2 does not share variable a which > means that variable a is class variable rather than static variable. > > Could you please comment on this? Is a static or class variable? > What's the most recent way of defining 'class' and 'static' variables? In Python `a' is considered a class variable. By modifying `a' using a class method though, you are essentially re-binding `a' to the class that it is called from. So, it will be shared by multiple instances of the same class, but not derivatives. The re-binding only occurs when `increment' is called. This makes for some very confusing behavior. I'm not aware of a way, in python, to explicitly specify a variable as static. For the behavior I think you are looking for you just need to modify it carefully. This can be done with a static, class or instance method. Though, using a class method is kind of silly. I'm sure some would argue that using an instance method is also silly. code: class C(object): a = 0 def inc(self): C.a += 1 # directly modify the variable on the base class that it is attached to return C.a # or you could use a static method, which is fine since the instance isn't being used. This # will also allow the method to be called without instantiating the class. class C(object): a = 0 @staticmethod def inc(): C.a += 1 return C.a Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: NUCULAR fielded text searchable indexing
[EMAIL PROTECTED] wrote: > On Oct 9, 8:46 am, Istvan Albert <[EMAIL PROTECTED]> wrote: >> ps. there is a python project named "The Devil Framework", I cringe >> every time I hear about it.Nucularis not as bad, but it is close. > > Aw shucks. I thought it was funny. Can't I make fun of > politicians in my open source projects? Besides there is > a great tradition of tounge-in-cheek package names, like > "Cold fusion", for example. >... I think it's an excellent name :) -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: generator_tools-0.1 released
Kay Schluehr wrote: > Originally I came up with the idea of a pure Python implementation for > copyable generators as an ActiveState Python Cookbook recipe. Too bad, > it was badly broken as Klaus Müller from the SimPy project pointed > out. Two weeks and lots of tests later I got finally a running version > that works not only in the very most simple use cases. As the code > size increased it turned out to not really fit anymore into the > "recipe" category and I created a package called "generator_tools". > > generator_tools-0.1 is available for both Python 2.5 and Python 3.0 > (!) [snip] pickler = GeneratorPickler("test.pkl") pickler.pickle_generator(g_gen) k_gen = pickler.unpickle_generator() list(g_gen) == list(k_gen) > True This seems like an odd syntax for pickling. Does the package support the normal pickle machinery? That is, does it register the appropriate methods with the copy_reg module? STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
> Your question "is variable a > static or class variable?" has no real answer. After running the > increment() method on a descendant class, e.g. Child1 will rebind the > name Child1.a, creating a new name in the namespace of the class. So the > variable Foo.a is still there, but you are accessing Child1.a instead. > Please notice, that theoretically there is no way to "change the value" of Foo.a in any way, because this is a NAME that references to an IMMUTABLE OBJECT. It means that you can only rebind the variable. You cannot change its value, because when we are talking about its value, we mean the state of the referenced object. The object referenced by the name is an integer instance, namely it is "one". The object "one" always remains "one", this cannot be changed. You can add one to one, and you will get two, but that is another object. (I'm sorry, probably you already knew this.) Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: The fundamental concept of continuations
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Again I am depressed to encounter a fundamentally new concept that I | was all along unheard of. Its not even in paul graham's book where i | learnt part of Lisp. Its in Marc Feeley's video. | | Can anyone explain: | | (1) its origin | (2) its syntax and semantics in emacs lisp, common lisp, scheme | (3) Is it present in python and java ? | (4) Its implementation in assembly. for example in the manner that | pointer fundamentally arises from indirect addressing and nothing new. | So how do you juggle PC to do it. | (5) how does it compare to and superior to a function or subroutine | call. how does it differ. | | Thanks a lot. | | (6) any good readable references that explain it lucidly ? I am starting with the Wikipedia article 'Continuation' which has references both to other W. articles and several other books and papers. -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
On Tue, 09 Oct 2007 09:16:12 -0700, [EMAIL PROTECTED] wrote: > I've got a question on the differences and how to define static and > class variables. First you have to define what you mean by "static". > AFAIK, class methods are the ones which receives the > class itself as an argument, while static methods are the one which > runs statically with the defining class. `classmethod`\s receive the class as first arguments, `staticmethod`\s are just functions bound to the class object. > Hence, my understanding is that static variables must be bound to the > class defining the variables and shared by children of parent class > where the variable is defined. But, please have a look at this code in > which a guy told me that the variable a is static: Ask the guy what he means by "static". class Foo: > a = 1 > @classmethod > def increment(cls): > cls.a += 1 > print cls.a > > Here, I am defining variable a which, I believe is class variable, > i.e., variable that is not bound to Foo itself. No you define a class attribute that *is* bound to the class `Foo`. > Rather, a is bound to the class which is accessing the variable. The code > that corroborates this idea is as follows: > class Child1(Foo): > pass > Child1.increment() > 4 Four!? Hard to believe. class Child2(Foo): > pass > Child2.increment() > 4 > > This means that Child1 and Child2 does not share variable a which means > that variable a is class variable rather than static variable. > > Could you please comment on this? Is a static or class variable? What's > the most recent way of defining 'class' and 'static' variables? There is no such thing as a "static" variable. Think of attributes that are bound to objects. All dynamically. What happens is: you bind a 1 to the attribute `Foo.a` in the `Foo` class definition. When you call `Child1.increment()` the class method will be called with `Child1` as first argument. Now ``cls.a += 1`` is executed which is somewhat like a short form of ``cls.a = cls.a + 1``. So this is reading the attribute `a` from `Child1` and then bind the result to `Child1`. `Child1` doesn't have an attribute `a`, so it is looked up in the parent class. But the result is then bound to `Child1`. So you are reading from `Foo` and writing to `Child1`. That's it. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Static variable vs Class variable
[EMAIL PROTECTED] wrote: > Hi. > > I've got a question on the differences and how to define static and > class variables. AFAIK, class methods are the ones which receives the > class itself as an argument, while static methods are the one which > runs statically with the defining class. > > Hence, my understanding is that static variables must be bound to the > class defining the variables and shared by children of parent class > where the variable is defined. But, please have a look at this code in > which a guy told me that the variable a is static: > > class Foo: > a = 1 > @classmethod > def increment(cls): > cls.a += 1 > print cls.a > In your increment() method, you do this: cls.a += 1 It does the following thing: #1. read cls.a #2. add one #3. assign this value to cls.a In point #3, you really bind a name to a value. As you probably know, in Python, there are names and objects. The initial value of the name 'a' is 1. It is an immutable object. The "+=" operator usually increments a value of an object. However, because the 'int' type is immutable, the += operator will rather rebind this variable to a newly created value. I believe this is what is happening here. Your question "is variable a static or class variable?" has no real answer. After running the increment() method on a descendant class, e.g. Child1 will rebind the name Child1.a, creating a new name in the namespace of the class. So the variable Foo.a is still there, but you are accessing Child1.a instead. If you want to HANDLE a as a static variable, you can handle it with a static method. That won't bind a new name in the descendant class. (However, you can still rebind it, e.g. "Child.a=42") Now here is a good question: how do you handle a variable as static, from a class (not static) method? Here is an example: >>> class Foo(object): ... a = 1 ... @classmethod ... def increment(cls): ... Foo.a += 1 ... print cls.a ... >>> class Child1(Foo): ... pass ... >>> Child1.increment() 2 >>> Child1.increment() 3 >>> Foo.a 3 >>> Child1.a = 10 >>> Child1.increment() 10 >>> Child1.increment() 10 >>> Child1.increment() 10 >>> Foo.a 6 >>> However, the question is: why would you do this? :-) BTW you should use new style classes whenever it is possible. Old style classes will have gone... Hope this helps, Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: Native class methods
On 09 Oct 2007 17:45:12 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > "Chris Mellon" <[EMAIL PROTECTED]> writes: > > > On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > > > > > > Is there an easy way to implement a specific method of a Python class > > > in C? Like a native method in Java? I would really like to do the > > > majority of my class code in Python and just do one or two methods > > > in C. > > > > > > S. > > > > > > > Weave kinda does this - you can use it write inline C code, which it > > extracts and compiles for you. (http://scipy.org/Weave) > > > > You might also want to look at Pyrex and/or Cython, which let you > > write in a Python-like language that is compiled to C. > > (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ and > > http://cython.org). > > > > Depending on what you want to do in C, just writing it as a normal > > shared library and calling it with ctypes might also be an effective > > solution. (in the standard library, as of 2.5) > > Yeah I'm really trying to do this without any dependencies on external > libraries. The ctypes way looks interesting but I had really hoped for > something more JNI-like :-/ > > S. Weave is a runtime solution, but Pyrex and Cython are both compilers (they compile a Python like language to C, which you the compile into a single extension module), and ctypes is in the standard library. Using ctypes would be pretty much just like JNI, except it's dynamic and not horrible. -- http://mail.python.org/mailman/listinfo/python-list
Static variable vs Class variable
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the variables and shared by children of parent class where the variable is defined. But, please have a look at this code in which a guy told me that the variable a is static: >>> class Foo: a = 1 @classmethod def increment(cls): cls.a += 1 print cls.a Here, I am defining variable a which, I believe is class variable, i.e., variable that is not bound to Foo itself. Rather, a is bound to the class which is accessing the variable. The code that corroborates this idea is as follows: >>> class Child1(Foo): pass >>> Child1.increment() 4 >>> class Child2(Foo): pass >>> Child2.increment() 4 This means that Child1 and Child2 does not share variable a which means that variable a is class variable rather than static variable. Could you please comment on this? Is a static or class variable? What's the most recent way of defining 'class' and 'static' variables? Thanks. - Minkoo -- http://mail.python.org/mailman/listinfo/python-list
Re: Native class methods
On 09 Oct 2007 17:45:12 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: >"Chris Mellon" <[EMAIL PROTECTED]> writes: > >> On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: >> > >> > Is there an easy way to implement a specific method of a Python class >> > in C? Like a native method in Java? I would really like to do the >> > majority of my class code in Python and just do one or two methods >> > in C. >> > >> > S. >> > >> >> Weave kinda does this - you can use it write inline C code, which it >> extracts and compiles for you. (http://scipy.org/Weave) >> >> You might also want to look at Pyrex and/or Cython, which let you >> write in a Python-like language that is compiled to C. >> (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ and >> http://cython.org). >> >> Depending on what you want to do in C, just writing it as a normal >> shared library and calling it with ctypes might also be an effective >> solution. (in the standard library, as of 2.5) > >Yeah I'm really trying to do this without any dependencies on external >libraries. The ctypes way looks interesting but I had really hoped for >something more JNI-like :-/ > JNI is awful. I can't imagine why you'd want something like it. However, since you do, why don't you just use the CPython/C API? It's the direct equivalent of JNI (sorry, it's not quite as complex or horrible, though). http://docs.python.org/api/api.html Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Native class methods
"Chris Mellon" <[EMAIL PROTECTED]> writes: > On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > > > > Is there an easy way to implement a specific method of a Python class > > in C? Like a native method in Java? I would really like to do the > > majority of my class code in Python and just do one or two methods > > in C. > > > > S. > > > > Weave kinda does this - you can use it write inline C code, which it > extracts and compiles for you. (http://scipy.org/Weave) > > You might also want to look at Pyrex and/or Cython, which let you > write in a Python-like language that is compiled to C. > (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ and > http://cython.org). > > Depending on what you want to do in C, just writing it as a normal > shared library and calling it with ctypes might also be an effective > solution. (in the standard library, as of 2.5) Yeah I'm really trying to do this without any dependencies on external libraries. The ctypes way looks interesting but I had really hoped for something more JNI-like :-/ S. -- http://mail.python.org/mailman/listinfo/python-list
RE: List loops
> From: Tommy Grav > > Hi everyone, > >I have a list of objects where I have want to do two loops. > I want to loop over the list and inside this loop, work on all > the elements of the list after the one being handled in the outer > loop. I can of course do this with indexes: > > >>> alist = range(3) > >>> for i in xrange(len(alist)): > ... for j in xrange(i+1,len(alist)): > ... print i,j,alist[i],alist[j] > ... > 0 1 0 1 > 0 2 0 2 > 1 2 1 2 > >>> > > > Is there a way to do this without using indexes? > You have to use indices because you are printing the indices. Given that, the following loop does what it looks like you are trying to do. >>> alist = range(3) >>> for index, i in enumerate(alist): for jndex, j in enumerate(alist[index:]): print index, jndex, i, j 0 0 0 0 0 1 0 1 0 2 0 2 1 0 1 1 1 1 1 2 2 0 2 2 >>> -- -Bill Hamilton -- http://mail.python.org/mailman/listinfo/python-list
Re: Native class methods
Stefan Arentz wrote: > > Is there an easy way to implement a specific method of a Python class > in C? Like a native method in Java? I would really like to do the > majority of my class code in Python and just do one or two methods > in C. ctypes or subclassing C-implemented classes. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Native class methods
On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > > Is there an easy way to implement a specific method of a Python class > in C? Like a native method in Java? I would really like to do the > majority of my class code in Python and just do one or two methods > in C. > > S. > Weave kinda does this - you can use it write inline C code, which it extracts and compiles for you. (http://scipy.org/Weave) You might also want to look at Pyrex and/or Cython, which let you write in a Python-like language that is compiled to C. (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ and http://cython.org). Depending on what you want to do in C, just writing it as a normal shared library and calling it with ctypes might also be an effective solution. (in the standard library, as of 2.5) -- http://mail.python.org/mailman/listinfo/python-list
Re: Query
> I want to make a binary file , which would execute on it's own. First do $ which python to get the location of your python binary. The default, i think, is just /usr/bin/python. Then add this line to the top of your file: #!/usr/bin/python (or whatever the `which` command returned) then finally do this command: $ chmod +x .py This makes executable ( that's what the x stands for ). now run it with: $ ./.py you can also trim the .py from the file and it will work just the same. To have your script work like installed binaries, put it in a folder in your PATH variable. For example, if you added the path /home//bin/ to your path variable ( PATH=$PATH:/home//bin/ ) Bash would search that directory when you typed in a command to execute. What this means is if you rename your .py to just and stick it in /home//bin/ you could just do $ at any time to run your program. hope my verbosity is helpful. -- -Nate -- http://mail.python.org/mailman/listinfo/python-list
Re: List loops
On 10/9/07, Tommy Grav <[EMAIL PROTECTED]> wrote: > Hi everyone, > >I have a list of objects where I have want to do two loops. > I want to loop over the list and inside this loop, work on all > the elements of the list after the one being handled in the outer > loop. I can of course do this with indexes: > > >>> alist = range(3) > >>> for i in xrange(len(alist)): > ... for j in xrange(i+1,len(alist)): > ... print i,j,alist[i],alist[j] > ... > 0 1 0 1 > 0 2 0 2 > 1 2 1 2 > >>> > > > Is there a way to do this without using indexes? > >>> for idx, i in enumerate(alist): ... for jdx, j in enumerate(range(1,4)): ... print idx, jdx, i, j ... 0 0 0 1 0 1 0 2 0 2 0 3 1 0 1 1 1 1 1 2 1 2 1 3 2 0 2 1 2 1 2 2 2 2 2 3 >>> -- http://mail.python.org/mailman/listinfo/python-list
Native class methods
Is there an easy way to implement a specific method of a Python class in C? Like a native method in Java? I would really like to do the majority of my class code in Python and just do one or two methods in C. S. -- http://mail.python.org/mailman/listinfo/python-list
Re: List loops
> >>> alist = range(3) > >>> for i in xrange(len(alist)): > ... for j in xrange(i+1,len(alist)): > ... print i,j,alist[i],alist[j] > ... > 0 1 0 1 > 0 2 0 2 > 1 2 1 2 > >>> > > > Is there a way to do this without using indexes? The following works for me, replicating your code, >>> alist = range(3) >>> for i, itemA in enumerate(alist): ... for j, itemB in enumerate(alist[i+1:]): ... print i,j+i+1,itemA, itemB ... 0 1 0 1 0 2 0 2 1 2 1 2 and is swappable if your alist has other values in it too: >>> alist = ['dog', 'cat', 'mouse'] >>> for i, itemA in enumerate(alist): ... for j, itemB in enumerate(alist[i+1:]): ... print i,j+i+1,itemA, itemB ... 0 1 dog cat 0 2 dog mouse 1 2 cat mouse However, if your list only has those range() values in it, there's nothing wrong with doing something like >>> for i in alist: ... for j in alist[i+1:]: ... print i,j,i,j ... It looks like you're trying to do permutations/combinations of things, in which case you might also find this helpful: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465 -tim -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with argument parsing
lgwe wrote: > I have a python-script: myscript, used to start a program on another > computer and I use OptionParser in optpars. > I use it like this: myscript -H host arg1 -x -y zzz > I would like OptionParser to ignore all arguments after arg1, because > these are options that should be used by the program started on the > oter computer arg1 -x -y zzz. > One solution is to write: myscript -H host "arg1 -x -y zzz" > But is it in some way possible to instruct OptionParser to ignore -x - > y zzz without the "..."? Use -- to separate arguments to be parsed by OptionParser from the to-be-ignored-ones, which is documented btw. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Python and C classes in a module
On Oct 9, 4:56 pm, Stefan Arentz <[EMAIL PROTECTED]> wrote: > Is it possible to mix classes defined in both Python and C in the same > module? Ideally I would like to be able to do: > > from some.module import MyPythonClass, MyCClass > > I guess that would mean that this would look like this on disk: > > some/ >__init__.py >module.py (contains MyPythonClass) >module.so (contains MyCClass) > > But would this work? > > S. Yes, but you need to write your own importer and customize it using ihooks.py. The builtin imp module also contains a function new_module that lets you allow creating a module without any file reference. This can be returned containg the names of both module.py and module.so. -- http://mail.python.org/mailman/listinfo/python-list
Re: Mixing Python and C classes in a module
On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote: > > Is it possible to mix classes defined in both Python and C in the same > module? Ideally I would like to be able to do: > > from some.module import MyPythonClass, MyCClass > > I guess that would mean that this would look like this on disk: > > some/ >__init__.py >module.py (contains MyPythonClass) >module.so (contains MyCClass) > > But would this work? > No, you'll need to make module a package, and import from (differently named) implementation packages. -- http://mail.python.org/mailman/listinfo/python-list
Re: supplying password to subprocess.call('rsync ...'), os.system('rsync ...')
On Oct 7, 1:01 pm, Michael Torrie <[EMAIL PROTECTED]> wrote: > timw.google wrote: > > Hi > > > I want to write a python script that runs rsync on a given directory > > and host. I build the command line string, but when I try to run > > subprocess.call(cmd), or p=subprocess.Popen(cmd, shell=True),or > > os.system(cmd), I get prompted for my login password. I expected this, > > but when I try to give my password, it's echoed back to the terminal > > and the special characters in the password is (I think) getting > > interpreted by the shell (zsh) > > > I can't ssh w/o supplying a password. That's the way the security is > > set up here. > > > How do I use python to do this, or do I just have to write a zsh > > script? > > You need to use the pexpect module. > > > > > Thanks. Thanks to all the suggestions on getting this to work w/ python. I'll look into this more when I get the chance. I don't have root access, so setting up some kind of server is out. I may not be able to try the other suggestions either, as they have things locked down pretty tight around here. -- http://mail.python.org/mailman/listinfo/python-list
Mixing Python and C classes in a module
Is it possible to mix classes defined in both Python and C in the same module? Ideally I would like to be able to do: from some.module import MyPythonClass, MyCClass I guess that would mean that this would look like this on disk: some/ __init__.py module.py (contains MyPythonClass) module.so (contains MyCClass) But would this work? S. -- http://mail.python.org/mailman/listinfo/python-list
Re: Virtual filesystem in python ( Fuse or WebDav ? )
On 2007-10-09, Thomas W <[EMAIL PROTECTED]> wrote: > I want to create a virtual filesystem based on a relational > database. [...] http://en.wikipedia.org/wiki/Filesystem_in_Userspace http://fuse.sourceforge.net/ -- Grant Edwards grante Yow! I'm a nuclear at submarine under the visi.compolar ice cap and I need a Kleenex! -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory leak/gc.get_objects()/Improved gc in version 2.5
On 10/8/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I'm running a python program that simulates a wireless network > protocol for a certain number of "frames" (measure of time). I've > observed the following: > > 1. The memory consumption of the program grows as the number of frames > I simulate increases. > > To verify this, I've used two methods, which I invoke after every > frame simulated: > > -- Parsing the /proc//status file as in: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286222 > -- Using ps vg | grep python | awk '!/grep/ {print " ",$8}' in an > os.system() call. > > The memory usage vs. frame number graph shows some big "jumps" at > certain points, and, after a large number of frames, shows a steady > upward slope > This would be expected if you're creating ever-larger amounts of objects - python uses memory pools and as the number of simultaneous objects increases, the size of the pool will need to increase. This isn't expected if the total number of objects you create is pretty much static, but the way you're trying to determine that is flawed (see below). > 2. I think I've verified that the objects I instantiate are actually > freed-- I'm therefore assuming that this "leak" is "caused" by > python's garbage collection mechanism. I count the number of objects I > generate that are being tracked by gc as follows: > > gc.collect() > objCount = {} > objList = gc.get_objects() > for obj in objList: > if getattr(obj, "__class__", None): > name = obj.__class__.__name__ > if objCount.has_key(name): > objCount[name] += 1 > else: > objCount[name] = 1 > > for name in objCount: > print name, " :", objCount[name] > >del objList > > Running this snippet every hundred frames or so, shows that the number > of objects managed by gc is not growing. > > I upgraded to Python 2.5. in an attempt to solve this problem. The > only change in my observations from version 2.4 is that the absolute > memory usage level seems to have dropped. However, I still see the > jumps in memory usage at the same points in time. > > Can anybody explain why the memory usage shows significant jumps (~200 > kB or ~500 kb) over time (i.e. "frames") even though there is no > apparent increase in the objects managed by gc? Note that I'm calling > gc.collect() regularly. > You're misunderstanding the purpose of Pythons GC. Python is refcounted. The GC exists only to find and break reference cycles. If you don't have ref cycles, the GC doesn't do anything and you could just turn it off. gc.get_objects() is a snapshot of the currently existing objects, and won't give you any information about peak object count, which is the most direct correlation to total memory use. > Thanks for your attention, > > Arvind > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
List loops
Hi everyone, I have a list of objects where I have want to do two loops. I want to loop over the list and inside this loop, work on all the elements of the list after the one being handled in the outer loop. I can of course do this with indexes: >>> alist = range(3) >>> for i in xrange(len(alist)): ... for j in xrange(i+1,len(alist)): ... print i,j,alist[i],alist[j] ... 0 1 0 1 0 2 0 2 1 2 1 2 >>> Is there a way to do this without using indexes? Cheers Tommy -- http://mail.python.org/mailman/listinfo/python-list
Re: why did MIT drop scheme for python in intro to computing?
On 2007-10-09, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Comparing apples with apples, how do you compare the scheme > gui library with wxpython ? Isnt it better than Tkinter ? I haven't used the newer Scheme GUI bindings. I switched from STk to tkinter, and then from tkinter to PyGTK and wxPython. I found Scheme much easier to use than TCL (an horrible, horrible language, IMO), but Python is easier yet than Scheme except for the Tk integration features. The GTK and wxWidgets Python bindings are native bindings rather than the TCL-hack used for tkinter, so I would expect a native GTK or wxWidgets Scheme binding to be quite similar to a native Python binding for the same library. However, a lot of extra work has gone into polishing the wxPython features above and beyond simply the library bindings, so I wouldn't be surprised if wxPython is better than a vanilla Scheme wxWidgets biding. -- Grant Edwards grante Yow! Did an Italian CRANE at OPERATOR just experience visi.comuninhibited sensations in a MALIBU HOT TUB? -- http://mail.python.org/mailman/listinfo/python-list