with statement for two files

2008-07-27 Thread braver
Can open two files in a with statement: with open(src) as readin, open(dst,w) as writin: # WRONG: comma doesn't work ... -- so that you have transactional safety for two file descriptors? The comma syntax doesn't work, but is there a way, except for with open(src) as readin: with

sampling without replacement

2008-04-17 Thread braver
Greetings -- I am doing a sampling without replacement, taking out random elements from an array. The picked element is then removed from the array. When my arrays were on the order of 10,000 elements long, everything was fast. But when I increased them to 1,000,000 it suddenly was hours. I

Re: eof

2007-11-23 Thread braver
On Nov 22, 8:04 pm, Neil Cerutti [EMAIL PROTECTED] wrote: I think Python is well rid of such a seldomly useful source of confusion. So all that code folks wrote in Algol-like languages, -- e.g. this works in Ada, -- while not End_of_File(f) loop -- end if; -- are confusing? Why not

Re: the annoying, verbose self

2007-11-23 Thread braver
On Nov 24, 2:38 am, BJörn Lindqvist [EMAIL PROTECTED] wrote: The big deal is that self. occupies important horizontal screen real estate. That is, it is usually not self in itself that is problematic, Exactly. I understand and appreciate all the scoping qualification and explicitation needs,

Re: eof

2007-11-22 Thread braver
On Nov 22, 10:37 am, Wayne Brehaut [EMAIL PROTECTED] wrote: As others have already pointed out, because it's seldom necessary in Python. You know what? I've read this many times, and it's a lot of self- congratulation. There's lot of things which can be useful in Python. This lack of EOF is

Re: eof

2007-11-22 Thread braver
On Nov 22, 5:08 am, I V [EMAIL PROTECTED] wrote: On Wed, 21 Nov 2007 17:06:15 -0800, braver wrote: It looks like ruby internally buffers the stream itself, which is how come it can support this. According to the docs: Note that IO#eof? reads data to a input buffer. http://www.ruby-doc.org

Re: eof

2007-11-22 Thread braver
On Nov 22, 3:26 pm, Duncan Booth [EMAIL PROTECTED] wrote: This sounds like a case for writing a generator. Try this one: [...] Thanks, Duncan! Really cool useful. And yield is the Ruby way, too! (Wayne -- :P). Cheers, Alexy -- http://mail.python.org/mailman/listinfo/python-list

Re: the annoying, verbose self

2007-11-22 Thread braver
On Nov 22, 4:34 pm, Kay Schluehr [EMAIL PROTECTED] wrote: On 22 Nov., 00:51, braver [EMAIL PROTECTED] wrote: But things grow -- is there any metaprogramming tricks or whatnot we can throw on the self? http://docs.python.org/lib/compiler.html Indeed. Well, my current solution is to bow

Re: eof

2007-11-22 Thread braver
On Nov 22, 5:32 pm, Neil Cerutti [EMAIL PROTECTED] wrote: There's nothing special about Python except indentation, which gets screwed up between editors all the time. (It's much easier to flip- flop between TextMate and Emacs with Ruby than with Python, without setting your tabs and

Re: eof

2007-11-22 Thread braver
On Nov 22, 6:08 pm, J. Clifford Dyer [EMAIL PROTECTED] wrote: So why Python's IO cannot yield f.eof() as easily as Ruby's can? :) Because that's not how you compare languages. You compare languages by stating what you are actually trying to do, and figuring out the most natural solution

Re: eof

2007-11-22 Thread braver
On Nov 22, 6:10 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: Granted, they aren't part of the stdlib - but then, lots of things aren't. As Hendrik noticed, I can't even add my own f.eof() if I want to have buffering -- is that right? The tradeoff between speed and convenience is something I'd

Re: eof

2007-11-22 Thread braver
On Nov 22, 6:40 pm, J. Clifford Dyer [EMAIL PROTECTED] wrote: You yourself said that performance is a complaint of yours regarding Ruby, so why claim that Ruby's way is clearly better in a case where it causes a known performance hit? See Hrvoje's remark above -- we can have EOF and eat it

Re: the annoying, verbose self

2007-11-22 Thread braver
On Nov 23, 1:15 am, Paul Boddie [EMAIL PROTECTED] wrote: One wonders whether the people complaining so vehemently about self have ever encountered coding style guides. Dude, I'm also programming in Ada, 83 to 95 to 2005. Beautiful language, a living style guide. I love typing names dozens of

eof

2007-11-21 Thread braver
I'd like to check, for a filehandle f, that EOF has been reached on it. What's the way to do it? I don't want to try/except on EOF, I want to check, after I read a line, that now we're in the EOF state. In Ruby it's f.eof: In Ruby: f = File.open(jopa) = #File:jopa f.read() = jopa\n f.eof =

the annoying, verbose self

2007-11-21 Thread braver
Is there any trick to get rid of having to type the annoying, character-eating self. prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to use Ruby anywhere speed is not crucial especially for @ prefix is better- looking than self. But things

Re: eof

2007-11-21 Thread braver
On Nov 22, 3:41 am, Wayne Brehaut [EMAIL PROTECTED] wrote: If you have PythonWin 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32.for example, using Help, Index, eof gives: eof Token used to determine end of file. This will be set to the empty string (''), in

referencing a subhash for generalized ngram counting

2007-11-13 Thread braver
Greetings: I wonder how does one uses single-name variables to refer to nested sunhashes (subdictionaries). Here's an example: In [41]: orig = { 'abra':{'foo':7, 'bar':9}, 'ca':{}, 'dabra':{'baz': 4} } In [42]: orig Out[42]: {'abra': {'bar': 9, 'foo': 7}, 'ca': {}, 'dabra': {'baz': 4}} In

Re: referencing a subhash for generalized ngram counting

2007-11-13 Thread braver
Here's a working version of the ngram counter with nested dict, wonder how it can be improved! lines = [abra ca dabra, abra ca shvabra, abra movich roman, abra ca dabra, a bra cadadra] ngrams = [x.split() for x in lines] N = 3 N1 = N-1 orig = {} for ngram in

Re: optional arguments with compact reporting in optparse

2007-11-12 Thread braver
Steve -- thanks for your pointer to argparse, awesome progress -- optional arguments. However, I still wonder how I do reporting. The idea is that there should be a list with tuples of the form: (short, long, value, help) -- for all options, regardless of whether they were specified on the

Showing native 8-bit strings in Python interpreter

2007-11-08 Thread braver
I'm storing 8-bit characters from the 128-256 range in Python strings. They are Windows CP1251 Russian characters. When looking at those strings in the Python interpreter, they come up as codes inside the string. How can I teach Python to show those 8-bit characters in the native encoding of

optional arguments with compact reporting in optparse

2007-11-08 Thread braver
Posted to the Optik list, but it seems defunct. Optik is now Python's optparse. I wonder how do you implement optional arguments to Optik. I.e., you can have an option -P [file] -- the filename is optional, with a default data,pikl. It works as follows: -- if no -P is given, no pickle is

Syntax coloring in Python interpreter

2007-11-01 Thread braver
Greetings -- as a long time user of both Python and Ruby interpreters, I got used to the latter's syntax-coloring gem, wirble, which colorizes Ruby syntax on the fly. Is there anything similar for Python? -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-18 Thread braver
Giovanni Bajo wrote, dict.setdefault, as I already explained to you. I wonder about numerics too. Say we have a = None somewhere. I want to increment it, so I'd say a += 8. Now if this is a parsing app, the increment may happen everywhere -- so I'd write a function to do it if I worry about

Re: magical expanding hash

2006-01-17 Thread braver
Nice. What about pushing to leaves which are arrays, or incrementing leaves which are numbers? If the array leaf didn't exist, or a number wasn't set yet, must create an empty array and push the element from the RHS into it, and += must init the leaf to 0 and add the RHS to it. Here's the

Re: magical expanding hash

2006-01-17 Thread braver
Actually, the behavior is important to translate perl into ruby. Can it be implemented in python looking similarly? -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread braver
Exactly, as in C++/ruby streams. But notice the extra checks needed to see whether we want a new leaf which is an array or a number, or we create an intermediate hash level. Would the checks look the same in python? -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread braver
The point of this exercise is to compare how either ruby or python can implement perl's default behavior when dealing with hashes. Since these are bread and butter of scripting, having a MEH class handy can enable fast semantically equivalent translation. This can be beneficial for demonstrating

Re: magical expanding hash

2006-01-17 Thread braver
Can assigning to hash without intermediate levels, possibly adding to a numeric leaf or adding an element to a leaf array, be python code? h['a']['b']['c'] += 42 If it can, I'd like to have a class which supports it. Is keeping a list at the leaf of a hash python code? h['a']['b']['c'].push(7)

Re: magical expanding hash

2006-01-17 Thread braver
Well, I know some python, but since there are powerful and magical features in it, I just wonder whether there're some which address this issue better than others. -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread braver
Thanks, James! This is really helpful. : It would take a lot of coding to make that work right. Better is the pythonic : : m[key] = [value] : : Its really only one more keystroke than : : m[key] value But it's only for the first element, right? I'd have to say

magical expanding hash

2006-01-16 Thread braver
I need a magical expanding hash with the following properties: * it creates all intermediate keys meh['foo']['bar] = 1 -- works even if meh['foo'] didn't exist before * allows pushing new elements to leaves which are arrays meh['foo']['list] elem1 meh['foo']['list] elem2 * allows