Re: "no variable or argument declarations are necessary."

2005-10-07 Thread Alex Martelli
Antoon Pardon <[EMAIL PROTECTED]> wrote: ... > >> egold = 0: > >> while egold < 10: > >> if test(): > >> ego1d = egold + 1 > >> > > > > Oh come on. That is a completely contrived example, > > No it is not. You may not have had any use for this > kind of code, but unfamiliary with

Re: "no variable or argument declarations are necessary."

2005-10-08 Thread Alex Martelli
Paul Rubin <http://[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] (Alex Martelli) writes: > > ap.py:4: No global (test) found > > ap.py:5: Local variable (ego1d) not used > > Helen:/tmp alex$ > > > > If you're so typo-prone and averse to unittests that

Re: Will python never intend to support private, protected and public?

2005-10-08 Thread Alex Martelli
Simon Brunning <[EMAIL PROTECTED]> wrote: > On 9/28/05, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > > > If *real* private and protected are *enforced*, Python will be the > > > poorer for it. See > > > . > > > > That's a wonder

Re: Will python never intend to support private, protected and public?

2005-10-08 Thread Alex Martelli
Kay Schluehr <[EMAIL PROTECTED]> wrote: > Honestly I like to use private/protect/public modifiers in C++ for the > sake of code documentation. I like to know which attributes are > dedicated to be known by other objects, which ones are for internal use > only and which ones should be at least publ

Re: Extending Python

2005-10-08 Thread Alex Martelli
Micah Elliott <[EMAIL PROTECTED]> wrote: > On Oct 05, Tuvas wrote: > > I am looking for a good tutorial on how to extend python with C > > code. I have an application built in C that I need to be able to use > > in Python. I have searched through various sources, starting of > > course with the Py

Re: Merging sorted lists/iterators/generators into one stream of values...

2005-10-08 Thread Alex Martelli
George Sakkis <[EMAIL PROTECTED]> wrote: > "Lasse Vågsæther Karlsen" <[EMAIL PROTECTED]> wrote: > > > Thanks, that looks like Mike's solution except that it uses the > > built-in heapq module. > > This make a big difference for the algorithmic complexity; replacing an > item in a heap is much mo

Re: Google Not Universal Panacea [was: Re: Where to find python c-sources]

2005-10-08 Thread Alex Martelli
Steve Holden <[EMAIL PROTECTED]> wrote: ... > >> Are people really too lazy to do elementary research on Google? > > goes a bit too far in imputing motives to the enquirer and overlooking > the fact that there are some very good reasons for *not* using Google. It's a good thing you don't actu

Re: Feature Proposal: Sequence .join method

2005-10-08 Thread Alex Martelli
Michele Simionato <[EMAIL PROTECTED]> wrote: ... > I have noticed a while ago that inside generators StopIteration is > automatically trapped, i.e. > > def g(): > yield 1 > raise StopIteration > yield "Never reached" > > only yields 1. Not sure if this is documented behavior, howev

Re: Python for search engine development

2005-10-08 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Well, Google applies some Python in their implementation, see > http://www-db.stanford.edu/~backrub/google.html "Some" is correct. As for writing a search engine in Python _only_, hmmm -- I honestly don't know. You could surely develop a working im

Re: Merging sorted lists/iterators/generators into one stream of values...

2005-10-09 Thread Alex Martelli
George Sakkis <[EMAIL PROTECTED]> wrote: ... > > manipulation of a heap to place an item in the right spot, but with 4-5 > > or a few more sources might not make an impact at all. > > Unless you're talking about hundreds or thousands sources, it probably > won't. I would still go for the heap s

Re: Question about parsing a string

2005-10-10 Thread Alex Martelli
Nico Grubert <[EMAIL PROTECTED]> wrote: > Hi there, > > I would like to parse a string in Python. > > If the string is e.g. '[url=http://www.whatever.org][/url]' I would like > to generate this string: > 'http://www.whatever.org";>http://www.whatever.org' > > If the string is e.g. '[url=http://

Re: What about letting x.( ... ? ... ) be equivalent to ( ... x ... )

2005-10-10 Thread Alex Martelli
al <[EMAIL PROTECTED]> wrote: > And it solve a problem that in all object oriented langages, a method > that process 2 or more different classes of objets belongs just to one > of those classes. Your use of the word "all" in the phrase "all object oriented languages" is erroneous. There ARE seve

Re: New Python book

2005-10-10 Thread Alex Martelli
hrh1818 <[EMAIL PROTECTED]> wrote: > This book is not a new book. It is an updated version of Magnus's 2002 > Practical Python book. Then it's probably a good book, because Practical Python sure was! Alex -- http://mail.python.org/mailman/listinfo/python-list

Re: Merging sorted lists/iterators/generators into one stream of values...

2005-10-10 Thread Alex Martelli
George Sakkis <[EMAIL PROTECTED]> wrote: ... > > i.e., a heap solution may be over 4 times faster than a sort-based one > > (in the following implementations). > > Interesting; I thought timsort on small almost ordered lists would be > practically as fast as the heap. Still, how is 0.10344 over

Re: Merging sorted lists/iterators/generators into one stream of values...

2005-10-10 Thread Alex Martelli
Mike C. Fletcher <[EMAIL PROTECTED]> wrote: ... > One thing to keep in mind (if you care about performance) is that you > one could use bisect, instead of sort, as the sorted list of streams is > already in order save for the one element you are processing. Btw, nice > trick with reverse to red

Re: A problem while using urllib

2005-10-11 Thread Alex Martelli
Johnny Lee <[EMAIL PROTECTED]> wrote: ... >try: > webPage = urllib2.urlopen(url) >except urllib2.URLError: ... >webPage.close() >return True > > >But every time when I ran to the 70 to 75 urls (that means 70-75 >

Re: How to do *args, **kwargs properly

2005-10-11 Thread Alex Martelli
Lasse Vågsæther Karlsen <[EMAIL PROTECTED]> wrote: ... > fn(1, 2, 3) > fn(1, 2, 3, cmp=lambda x, y: y-x) > fn(1, 2, 3, cpm=lambda x, y: y-x) # TypeError on this I assume these are your specs. > or is the "proper python" way simply this: > > def fn(*values, **options): > if "cmp" in optio

Re: Looking for info on Python's memory allocation

2005-10-11 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > > s = [k for k in iterable] > > > > if I know beforehand how many items iterable would possibly yield, would > > a construct like this be faster and "use" less memory? > > > > s = [0] * len(iterable) > > for i in xrange(len(iterable)): > >

Re: Python's garbage collection was Re: Python reliability

2005-10-11 Thread Alex Martelli
Tom Anderson <[EMAIL PROTECTED]> wrote: ... > Has anyone looked into using a real GC for python? I realise it would be a If you mean mark-and-sweep, with generational twists, that's what gc uses for cyclic garbage. > lot more complexity in the interpreter itself, but it would be faster, > more

Re: Adding a __filename__ predefined attribute to 2.5?

2005-10-12 Thread Alex Martelli
Rune Strand <[EMAIL PROTECTED]> wrote: > > those modules are already imported when Python gets to your code, so > > the only "overhead" you're saving is a little typing. > > I don't understand this. Could you please elaborate? - if sys or os > are not imported for any other causes how are they a

Re: Adding a __filename__ predefined attribute to 2.5?

2005-10-13 Thread Alex Martelli
Rune Strand <[EMAIL PROTECTED]> wrote: > Ok, Alex. I know a good explanation when I see one. Thanks! You're welcome! I've tried to give good (but shorter!-) explanations in the Nutshell, too, but of course it's easier to aim a specific explanation to a specific questioner than to try and clarify

Re: Python's garbage collection was Re: Python reliability

2005-10-13 Thread Alex Martelli
Tom Anderson <[EMAIL PROTECTED]> wrote: > On Tue, 11 Oct 2005, Alex Martelli wrote: > > > Tom Anderson <[EMAIL PROTECTED]> wrote: > > ... > >> Has anyone looked into using a real GC for python? I realise it would be a > > > > If you mean mark-

Re: confusion between global names and instantiated object variable names

2005-10-14 Thread Alex Martelli
wanwan <[EMAIL PROTECTED]> wrote: ... > when I run my example, an error shows: > "NameError: global name'menubar' is not defined" > > I wonder why it doesn't work. Isn't that the way to define an object > variable? The code you posted should not trigger this error. Most likely problem: you

Re: Problem splitting a string

2005-10-15 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > You can *almost* do that as a one-liner: No 'almost' about it... > L2 = [item.split('_') for item in mystr.split()] > > except that gives a list like this: > > [['this', 'NP'], ['is', 'VL'], ['funny', 'JJ']] > > which needs flattening.

Re: Problem splitting a string

2005-10-15 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: ... > A third alternative is to split once, then split the substrings a > second time and stitch the results back together: > > >>> sum([x.split('_') for x in mystr.split()], []) > ['this', 'NP', 'is', 'VL', 'funny', 'JJ'] > > Which is probably slow. To ba

Re: Some set operators

2005-10-15 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Sometimes I suggest to add things to the language (like adding some set > methods to dicts), but I've seen that I tend to forget the meaning of > six set/frozenset operators: > > s & t s &= t > s | t s |= t > s ^ t s ^= t > > My suggestion is to remove them, and k

Re: Some set operators

2005-10-16 Thread Alex Martelli
Giovanni Bajo <[EMAIL PROTECTED]> wrote: > Alex Martelli <[EMAIL PROTECTED]> wrote: > > > I still vaguely hope that in 3.0, where backwards incompatibilities > > can be introduced, Python may shed some rarely used operators such as > > these (for all types,

Re: How to get a raised exception from other thread

2005-10-17 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Nevermind. I found a better solution. I used shared memory to create > a keep-alive flag. I then use the select function with a specified > timeout, and recheck the keep-alive flag after each timeout. Definitely a better architecture. Anyway, one supported way for

Re: Comparing lists

2005-10-17 Thread Alex Martelli
Christian Stapfer <[EMAIL PROTECTED]> wrote: > This is why we would like to have a way of (roughly) > estimating the reasonableness of the outlines of a > program's design in "armchair fashion" - i.e. without > having to write any code and/or test harness. And we would also like to consume vast

Re: Queue question

2005-10-17 Thread Alex Martelli
Steve M <[EMAIL PROTECTED]> wrote: > According to my "Python in a Nutshell": > > q.get(block=True) > > is the signature, so, as you use it above, the call will hang until > something is on the queue. If block is false and the queue is empty, > q.get() will raise the exception Empty. > > q.get_n

Re: Problems with properties

2005-10-17 Thread Alex Martelli
Michael Schneider <[EMAIL PROTECTED]> wrote: > Thanks to all, I added the object as a subclass (should this be > required for 2.4.1 ???) It _IS_ required, because Python these days moves *very slowly indeed* before changing semantics of existing code in any way that is not backwards compatible

Re: generic xmerge ?

2005-10-17 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > > I was reading this recipe and am wondering if there is a generic > version of it floating around ? My list is a tuple (date, v1, v2, v3) > and I would like it to sort on date. The documentation doesn't mention > how the items are compared and

Re: generic xmerge ?

2005-10-17 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > oops, sorry. I meant > > l1=[(date,v1,v2,v3), ...] > l2=[ another set of tuples ] > > Thanks. so I have to concat the multiple lists first(all of them are > sorted already) ? You can do it either way -- simplest, and pretty fast, is to concatenate

Re: Run process with timeout

2005-10-17 Thread Alex Martelli
Natan <[EMAIL PROTECTED]> wrote: > Hi. > > I have a python script under linux where I poll many hundreds of > interfaces with mrtg every 5 minutes. Today I create some threads and > use os.system(command) to run the process, but some of them just hang. > I would like to terminate the process afte

Re: generic xmerge ?

2005-10-17 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > million thanks. So the default compare funcion of heapq also do it like > sort ? By defaults, all comparisons in Python occur by the same mechanisms: by preference, specific comparison operators such as < , <= , and so on (corresponding to special me

Re: Dynamic generation of doc-strings of dynamically generated classes

2005-10-17 Thread Alex Martelli
Mikael Olofsson <[EMAIL PROTECTED]> wrote: ... > Any ideas? Am I stuck with the clumsy exec-solution, or are there other > ways to dynamically generate doc-strings of classes? The best way to make classes on the fly is generally to call the metaclass with suitable parameters (just like, the bes

Re: reading hebrew text file

2005-10-17 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > I have a hebrew text file, which I want to read in python > I don't know which encoding I need to use & how I do that As for the "how", look to the codecs module -- but if you don't know what codec the textfile is written in, I know of no ways to guess from here!-)

Re: Comparing lists

2005-10-17 Thread Alex Martelli
Christian Stapfer <[EMAIL PROTECTED]> wrote: > "Alex Martelli" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Christian Stapfer <[EMAIL PROTECTED]> wrote: > > > >> This is why we would like to have a way of (roughly)

Re: Run process with timeout

2005-10-17 Thread Alex Martelli
Micah Elliott <[EMAIL PROTECTED]> wrote: > Is there any way to enable Python's subprocess module to do (implicit?) > group setup to ease killing of all children? If not, is it a reasonable > RFE? Not as far as I know. It might be a reasonable request in suitable dialects of Unix-like OSes, thou

Re: UI toolkits for Python

2005-10-18 Thread Alex Martelli
Paul Rubin wrote: > Torsten Bronger <[EMAIL PROTECTED]> writes: > > Because everybody is capable of running a JS engine, even on > > computers on which you don't have rights to install something. > > I don't think using JS so heavily without a compelling reason is > rea

Re: override a property

2005-10-18 Thread Alex Martelli
Robin Becker <[EMAIL PROTECTED]> wrote: > Bruno Desthuilliers wrote: > > Robin Becker a écrit : > > > >> Is there a way to override a data property in the instance? Do I need > >> to create another class with the property changed? > > > > Do you mean attributes or properties ? > > I mean proper

Re: Question on class member in python

2005-10-18 Thread Alex Martelli
Johnny Lee <[EMAIL PROTECTED]> wrote: ... > Thanks for your help, maybe I should learn how to turn an attibute into > a property first. Easy -- in your class's body, just code: def getFoo(self): ... def setFoo(self, value): ... def delFoo(self): ... foo = property(getFoo, setFoo, delFo

Re: Question on class member in python

2005-10-18 Thread Alex Martelli
Johnny Lee <[EMAIL PROTECTED]> wrote: > But I still wonder what's the difference between the A().getMember and > A().member besides the style Without parentheses after it, getMember is a method. The difference between a method object and an integer object (which is what member itself is in your

Re: Question on class member in python

2005-10-18 Thread Alex Martelli
Johnny Lee <[EMAIL PROTECTED]> wrote: > Alex Martelli ??? Now that's a peculiar question... > > Johnny Lee <[EMAIL PROTECTED]> wrote: > > > > > But I still wonder what's the difference between the A().getMember and > > > A().member

Re: UI toolkits for Python

2005-10-18 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: ... > What surprises me is that marketing types will accept turning away - > what's the current internet user base? 200 million? - 10 million > potential customers without a complaint. Or maybe they just don't get > told that that's what's going on. In firm

Re: List of strings to list of floats ?

2005-10-18 Thread Alex Martelli
Madhusudan Singh <[EMAIL PROTECTED]> wrote: ... > >> Say I have two lists of floats. And I wish to generate a list of floats > >> that is a user defined function of the two lists. > > > > result = [sqrt(x**2 + y**2) for x, y in zip(xs, ys)] > > Works perfectly. Thanks ! If zip works and map d

Re: UI toolkits for Python

2005-10-18 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] (Alex Martelli) writes: > > Mike Meyer <[EMAIL PROTECTED]> wrote: > >> What surprises me is that marketing types will accept turning away - > >> what's the current internet user base? 200 mi

Re: Sequence and/or pattern matching

2005-10-19 Thread Alex Martelli
Séb <[EMAIL PROTECTED]> wrote: > Hi everyone, > > I'm relatively new to python and I want to write a piece of code who do > the following work for data mining purpose : Essentially, if I understand correctly, you want to detect LOOPS given a sequence of directed connections A->B. "loop detectio

Re: sort problem

2005-10-21 Thread Alex Martelli
Michele Petrazzo <[EMAIL PROTECTED]> wrote: > Lasse Vågsæther Karlsen wrote: > > How about: > > > > list.sort(key=lambda x: x[3]) > > > > Does that work? > > Yes, on my linux-test-box it work, but I my developer pc I don't have > the 2.4 yet. I think that this is a good reason for update :) Up

Re: Python vs Ruby

2005-10-21 Thread Alex Martelli
Amol Vaidya <[EMAIL PROTECTED]> wrote: > Hi. I am interested in learning a new programming language, and have been > debating whether to learn Ruby or Python. How do these compare and contrast > with one another, and what advantages does one language provide over the > other? I would like to consi

Re: override a property

2005-10-21 Thread Alex Martelli
Robin Becker <[EMAIL PROTECTED]> wrote: ... > in answer to Bengt & Bruno here is what I'm sort of playing with. Alex > suggests class change as an answer, but that looks really clunky to me. > I'm not sure what Changing class is indeed 'clunky', though it might have been necessary depending on

Re: Question about inheritance...

2005-10-22 Thread Alex Martelli
KraftDiner <[EMAIL PROTECTED]> wrote: > Well here is a rough sketch of my code... > This is giving my two problems. > > 1) TypeError: super() argument 1 must be type, not classobj Make your classes new-style (have Shape inherit from object) to fix this. You're using the legacy (old-style) objec

Re: Python vs Ruby

2005-10-22 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: > > Every line = more labour for the developer = more cost and time. > > Every line = more places for bugs to exist = more cost and time. > > There were studies done in the 70s that showed that programmers > produced the same number of debugged lines of code

Re: C extension modules in Python

2005-10-22 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Hello, > > I'vre written an extension module to accelarate some code i've made in > python with numarray. Then i compiled an linke d it with swig, my > problem is that when i make the import in my python code it gives me an > error: ImportError: libnumarray.so: cannot

Re: IDE recommendation please

2005-10-22 Thread Alex Martelli
microsnot <[EMAIL PROTECTED]> wrote: > I'm new to Python but am wondering what IDE Python developers use? I use Mac > OS X 10.4.2. I have PythonIDE which comes with MacPython but I don't think > that has even rudimentary "intellisense". Xcode and Eclipse don't seem to > support Python out of the b

Re: Python vs Ruby

2005-10-23 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: ... > > Of course, these results only apply where the "complexity" (e.g., number > > of operators, for example) in a single line of code is constant. > > I'm not sure what you're trying to say here. The tests ranged over > things from PL/I to assembler. Are

Re: How to separate directory list and file list?

2005-10-23 Thread Alex Martelli
Gonnasi <[EMAIL PROTECTED]> wrote: > With > >glob.glob("*") > > or > >os.listdir(cwd) > > I can get a combined file list with directory list, but I just wanna a > bare file list, no directory list. How to get it? I see everybody's suggesting os.path.* solutions, and they're fine, but an interes

Re: Syntax across languages

2005-10-23 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: ... > - Information about the current line and file as Ruby: > __LINE__ __FILE__ > Instead of the python version: > inspect.stack()[0][2] inspect.stack()[0][1] __file__ is around in Python, too, but there's no __line__ (directly). > - identity function: "identity" as

Re: Syntax across languages

2005-10-23 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > just curious, how can this identity function be used ? In haskell, > because all functions are curried, I can sort of visualize/understand > how id is used. Not quite understand how it can be used in python. There was a very recent example posted to

Re: Syntax across languages

2005-10-23 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Alex> I've seen enough occurrences of "lambda x: x" in Python code with > Alex> a generally functional style that I'd love to have > Alex> operator.identity (and a few more trivial functions like that) for > Alex> readability;-) > > But, but, but [Skip

Re: calling a dylib in python on OS X

2005-10-23 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Hi > > Is there something similar to python's windll for calling DLLs on win32 > but meant for calling dylib's on OS X? ctypes should work just fine on Macs, just as well as on Windows and Linux machines as well as many other Unix dialects. Try http://starship.pytho

Re: Microsoft Hatred FAQ

2005-10-23 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: ... > David claimed that everyone had a right to do whatever they wanted > with their property. This is simply false throughout most of the > civilized world - zoning laws control what kinds of business you can Incidentally, the perfectly good rationale fo

Re: Syntax across languages

2005-10-23 Thread Alex Martelli
Tom Anderson <[EMAIL PROTECTED]> wrote: ... > What would approximate FP equality even mean? How approximate? In APL, it meant "to within [a certain quad-global whose name I don't recall] in terms of relative distance", i.e., if I recall correctly, "a=b" meant something like "abs(a-b)/(abs(a)+ab

Re: how to count and extract images

2005-10-23 Thread Alex Martelli
Joe <[EMAIL PROTECTED]> wrote: > I'm trying to get the location of the image uisng > > start = s.find(' stop = s.find('">Save File', > start) fileName = s[start:stop] > and then construct the url with the filename to download the image > which works fine as cause every image has the Save File l

Re: Tricky Areas in Python

2005-10-23 Thread Alex Martelli
PyPK <[EMAIL PROTECTED]> wrote: > What possible tricky areas/questions could be asked in Python based > Technical Interviews? I like to present code that seems like it should work, but has some kind of relatively subtle problem, either of correctness in some corner case, or of performance, etc --

Re: Tricky Areas in Python

2005-10-23 Thread Alex Martelli
Andrew Durdin <[EMAIL PROTECTED]> wrote: > On 10/24/05, Alex Martelli <[EMAIL PROTECTED]> wrote: > > I may branch out into more advanced stuff such as asking > > for an example use case for a closure, a custom descriptor, or an import > > hook, for example >

Re: High Order Messages in Python

2005-10-23 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > could someone enlighten me what is the advantage of block over named > function ? > > One thing that I can see a difference may be lexical scope ? "Yes, but" -- according to the latest Ruby book, the "mixed lexical scope" of blocks is a highly contr

Re: High Order Messages in Python

2005-10-24 Thread Alex Martelli
Kent Johnson <[EMAIL PROTECTED]> wrote: ... > For example to open a file and read from it uses two closures, one to wrap > a block with the file open/close, one to iterate lines (from the pickaxe > book): > > File.open("testfile") do |file| > file.each_line { |line| puts line } > end Good ex

Re: Tricky Areas in Python

2005-10-24 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > my hard-won ignorance, and admit that I don't see the > problem with the property examples: > > > class Sic: > > def getFoo(self): ... > > def setFoo(self): ... > > foo = property(getFoo, setFoo) Sorry for skipping t

Re: Python vs Ruby

2005-10-24 Thread Alex Martelli
Michele Simionato <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > ... remember Pascal's "Lettres Provinciales", > > and the famous apology about "I am sorry that this letter is so long, > > but I did not have the time to write a shorter one&q

Re: Python vs Ruby

2005-10-24 Thread Alex Martelli
Jorge Godoy <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] (Alex Martelli) writes: > > > forwards a lot to Python 3.0!-). But -- the "dream" solution would be > > to work closely with customers from the start, XP-style, so features go > > into the

Re: Tricky Areas in Python

2005-10-24 Thread Alex Martelli
beza1e1 <[EMAIL PROTECTED]> wrote: > let me try. > > 1) ''.join(lots_of_pieces) Yep. > 2) This doesn't even work, if something is removed, the list is too > short. So: > [x for x in somelist if not isbad(x)] > well, list comprehension is Python 2.4 and 2.3 is the standard in many > OSes, so it

Re: Tricky Areas in Python

2005-10-25 Thread Alex Martelli
Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > >> > class Sic: > >> > def getFoo(self): ... > >> > def setFoo(self): ... > >> > foo = property(getFoo, setFoo) > > > >

Re: Tricky Areas in Python

2005-10-25 Thread Alex Martelli
Tim Roberts <[EMAIL PROTECTED]> wrote: > "PyPK" <[EMAIL PROTECTED]> wrote: > > > >What possible tricky areas/questions could be asked in Python based > >Technical Interviews? > > What's the point of asking "tricky" questions? Aren't you really more > interested in what applications they have wor

Re: Set an environment variable

2005-10-26 Thread Alex Martelli
Grant Edwards <[EMAIL PROTECTED]> wrote: > On 2005-10-24, Eric Brunel <[EMAIL PROTECTED]> wrote: > > >> The only think you can export an environment variable to is a > >> child process > > > > Well, you know that, and I know that too. From my experience, > > many people don't... > > True. Using

Re: namespace dictionaries ok?

2005-10-26 Thread Alex Martelli
Ron Adam <[EMAIL PROTECTED]> wrote: ... > class namespace(dict): > def __getattr__(self, name): > return self.__getitem__(name) ... > Any thoughts? Any better way to do this? If any of the keys (which become attributes through this trick) is named 'update', 'keys'

Re: How to replace all None values with the string "Null" in a dictionary

2005-10-27 Thread Alex Martelli
Bengt Richter <[EMAIL PROTECTED]> wrote: ... > Which is probably more efficient than one-liner updating the dict with > > mydict.update((k,'Null') for k,v in mydict.items() if v is None) ...which in turn is probably better than _auxd = {None: "Null"} newd = dict((k, _auxd.get(k, c) for k,

Re: Double replace or single re.sub?

2005-10-27 Thread Alex Martelli
Iain King <[EMAIL PROTECTED]> wrote: > I have some code that converts html into xhtml. For example, convert > all tags into . Right now I need to do to string.replace calls > for every tag: > > html = html.replace('','') > html = html.replace('','') > > I can change this to a single call to r

Re: Generic utility class for passing data

2005-10-28 Thread Alex Martelli
Gordon Airporte <[EMAIL PROTECTED]> wrote: > I'm wondering if this is might be bad practice. Sometimes when I need to I hope not, 'cuz I suggested that years ago on the Cookbook (under the name of Bunch) with several successive refinements. > class Dummy: > pass > > Then when I need to pa

Re: How do I sort these?

2005-10-28 Thread Alex Martelli
Paul Rubin wrote: > "KraftDiner" <[EMAIL PROTECTED]> writes: > > In C++ you can specify a comparision method, how can I do this with > > python... > > Yes, see the docs. Just pass a comparison func to the sort method. Or, better, pass a key-extraction function, that's

Re: Newbie question: string replace

2005-10-28 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > > > So how to overwrite the config file directly in script.py instead of > > running script.py with two params? > > Don't overwrite the file directly. Save a copy, then rename it. That See module fileinput

Re: Newbie question: string replace

2005-10-28 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Fri, 28 Oct 2005 12:27:36 -0700, [EMAIL PROTECTED] wrote: > > > hm...Is there a way to get rid of the newline in "print"? > > Yes, by using another language *wink* Ending the print statement with a comma also works;-) > Or, instead of using pri

Re: Scanning a file

2005-10-28 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: ... > Except if you can't read the file into memory because it's to large, > there's a pretty good chance you won't be able to mmap it either. To > deal with huge files, the only option is to read the file in in > chunks, count the occurences in each chunk,

Re: Opaque documentation

2005-10-28 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote: > "Ben Sizer" <[EMAIL PROTECTED]> writes: > > Documentation is often a problem with Python and its libraries, sadly. > > The same almost certainly goes for most open source projects. > > You over-specified the last clause. It should say "most software > proj

Re: Why doesn't this work? :)

2005-10-28 Thread Alex Martelli
Jeremy Moles <[EMAIL PROTECTED]> wrote: > Am I misunderstanding something fundamental about the builtin __* > functions? Can they not be "static?" They can and must be static when their specification say they are (e.g., __new__) and they cannot and must not be static when their specification says

Re: suggestions between these two books

2005-10-28 Thread Alex Martelli
Micah Elliott <[EMAIL PROTECTED]> wrote: > On Oct 26, John Salerno wrote: > > Hi all. I'm fairly new to programming and I thought I'd like to try > > Python. I'm trying to decide between these two books: > > > > Learning Python (O'Reilly) > > Beginning Python: From Novice to Professional (APress)

Re: pickling class instances with __slots__

2005-10-28 Thread Alex Martelli
Alex <[EMAIL PROTECTED]> wrote: ... > I have a series of new classes with child-parent relationship and each > has unique __slots__. They don't have __dict__ . I need to be able to > pickle and unpickle them. As far as I could understand, I need to > provide __getstate__ and __setstate__ metho

Re: Expanding Python as a macro language

2005-10-29 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: ... > But the problem is that in Linux you can't even send a keystroke to > a running GUI application! Actually, if the app is running under X11 you may try to fake out a keystroke event (with low level calls, but ctypes might let you use it from Python). Of course,

Re: py.log using decorators for DRY

2005-10-29 Thread Alex Martelli
yoda <[EMAIL PROTECTED]> wrote: > I'm using py.log for logging and I find that I end up having the following > pattern emerge within my code (influenced by > http://agiletesting.blogspot.com/2005/06/keyword-based-logging-with-py-lib > rary.html): > > def foo(**kwargs): > log.foo(kwargs) >

Re: Scanning a file

2005-10-29 Thread Alex Martelli
Bengt Richter <[EMAIL PROTECTED]> wrote: ... > >>>while block: > >>>block = block[-overlap:] + f.read(blocksize-overlap) > >>>if block: yield block ... > I was thinking this was an example a la Alex's previous discussion > of interviewee code challenges ;-) > > What struc

Re: Scanning a file

2005-10-29 Thread Alex Martelli
Tim Roberts <[EMAIL PROTECTED]> wrote: ... > >> print file("filename", "rb").read().count("\x00\x00\x01\x00") > > > >Funny you should say that, because I can't stand unnecessary one-liners. > > > >In any case, you are assuming that Python will automagically close the > >file when you are done. >

Re: extracting numbers from a file, excluding fixed words

2005-10-29 Thread Alex Martelli
dawenliu <[EMAIL PROTECTED]> wrote: > Hi, I have a file with this content: > xxx xx x xxx > 1 > 0 > 0 > 0 > 1 > 1 > 0 > (many more 1's and 0's to follow) > y yy yyy yy y yyy > > The x's and y's are FIXED and known words which I will ignore, such as > "This is t

Re: Recursive generators and backtracking search

2005-10-29 Thread Alex Martelli
Talin <[EMAIL PROTECTED]> wrote: > even simpler - for examle, the idea of being able to return the output > of one generator directly from another instead of having to iterate > through all of the results and then re-yield them has already been > discussed in this forum. I missed those discussion

Re: lambda functions within list comprehensions

2005-10-29 Thread Alex Martelli
Max Rybinsky <[EMAIL PROTECTED]> wrote: ... > >>> funcs = [lambda n: x * y / n for x, y in a] ... > It seems, all functions have x and y set to 9. > What's wrong with it? Is it a bug? It's known as *late binding*: names x and y are looked up when the lambda's body is executing, and at that t

Re: lambda functions within list comprehensions

2005-10-29 Thread Alex Martelli
Max Rybinsky <[EMAIL PROTECTED]> wrote: > Thank you for explanation, Alex. > It appears that almost every beginner to Python gets in trouble with > this ...feature. :) Almost every beginner to Python gets in trouble by expecting "do what I'm thinking of RIGHT NOW"-binding, which no language offer

Re: Scanning a file

2005-10-29 Thread Alex Martelli
Paul Watson <[EMAIL PROTECTED]> wrote: > "Alex Martelli" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > > In today's implementations of Classic Python, yes. In other equally > > valid implementations of the language, such as J

Re: Scanning a file

2005-10-29 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > I should also point out that for really serious work, the idiom: > > f = file("parrot") > handle(f) > f.close() > > is insufficiently robust for production level code. That was a detail I > didn't think I needed to drop on the original newbie po

Re: Expanding Python as a macro language

2005-10-29 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: ... > For the other Alex observations (about Mac OsX and my examples of > automation centered on web automation) I have a PC, and the fact that > Python is very good at dealing with the web, doesn't help too much > in this case... All of your sensible use cases were

Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: ... > def foo(**kwargs): > expected_form1_kwargs = ["arg1", "arg2"] > > for name in expected_form1_kwargs: > if name not in kwargs: > kwargs[name]=None > > for name in kwargs: > if name in kwargs and name not

Re: Scanning a file

2005-10-30 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: ... > > Don't ever catch and ``handle'' exceptions in such ways. In particular, > > each time you're thinking of writing a bare 'except:' clause, think > > again, and you'll most likely find a much better approach. > > What would you -- or anyone else

  1   2   3   4   5   6   7   8   9   10   >