PEP ? os.listdir enhancement
Hi, I noticed that when I use os.listdir I need to work with absolute paths 90% of times. While I can use a for cycle, I'd prefere to use a list comprehension, but it becomes too long. I propose to add an 'abs' keyword which would make os.listdir return the absolute path of files instead of a relative path. This would bring only advantages, I think. I show two examples from os import listdir from os.path import isdir,join,getsize,isfile ### e.g. 1 part 1 - getting a list of directories ### dirs=[] for i in os.listdir(path): tmp_path=os.path.join(path,i) if os.path.isdir(tmp_path): dirs.append(tmp_path) ### e.g. 1 part 2 ### dirs=[join(path,x) for x in listdir(path) if isdir(join(path,x))] here the list comprehension is still clear, but only because we have direct references to join and friends. moreover whe need to use join twice for each directory. ### e.g. 2 part 1 - getting a list of (file,size) tuples ### path_size=[] for i in os.listdir(path): tmp_path=os.path.join(path,i) if os.path.isfile(tmp_path): path_size.append((tmp_path,getsize(tmp_path)) ### e.g. 2 part 2 ### dirs=[(join(path,x),getsize(join(path,x)) for x in listdir(path) if isfile(join(path,x))] now list comprehension is way too long, and in the worst case we must use join 3 times for each iteration. adding an 'abs' keyword to os.listdir would give benefits both to for cycle and list comprehensions. for cycle would lose the tmp_path assignment and list comprehensions ... ### e.g. 1 part 2 bis ### dirs=[x for x in listdir(path,abs=True) if isdir(x)] here we gain clearness and speed. ### e.g. 2 part 2 bis ### dirs=[(x,getsize(x)) for x in listdir(path,abs=True) if isfile(x)] and here we gain clearness, speed and a truely _usable_ list comprehension What do you think about this ? Thanks for reading, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP ? os.listdir enhancement
On Wed, 22 Jun 2005 11:27:06 -0500, Jeff Epler wrote: > Why not just define the function yourself? Not every 3-line function > needs to be built in. Of course I can code such a function, and I agree with the second sentence, but I think that obtaining absolutes path is a task so commonly needed that adding a keyword to an existing function would give a plus to the library without adding complexity (speaking of number of modules). Usually when you use os.listdir do you end using os.path.join to obtain absolutes path? I'm interested to know if the task is so common as I think, or if I'm wrong. Thank you, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP ? os.listdir enhancement
On Thu, 23 Jun 2005 11:34:02 +0200, Andreas Kostyrka wrote: > What's wrong with > > (os.path.join(d, x) for x in os.listdir(d)) > > It's short, and easier to understand then some obscure option ;) > > Andreas how does it help in using list comprehension, as the ones in the first post? -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP ? os.listdir enhancement
On Thu, 23 Jun 2005 12:56:08 +0300, Konstantin Veretennicov wrote: > On 6/22/05, Riccardo Galli <[EMAIL PROTECTED]> wrote: > >> I propose to add an 'abs' keyword which would make os.listdir return the >> absolute path of files instead of a relative path. > > What about os.listdir(dir='relative/path', abs=True)? Should listdir call > abspath on results? Should we add another keyword rel? Would it complicate > listdir unnecessarily? > > - kv keyword dir not exists (don't know if you added as example or not) and abs set to true would return abspath on result. What else could it do ? -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP ? os.listdir enhancement
On Thu, 23 Jun 2005 13:25:06 +0200, Daniel Dittmar wrote: > He probably meant that a 'join' option would be more natural than an 'abs' > option. After all, your examples use os.path.join to create a valid path > that can be used as the argument to other module os functions. Whether the > results are absolute or relative should depend on the initial argument to > os.listdir. > > Daniel I got the point, and you're right. I didn't though about that and 'abs' as keyword becomes nonsense. Needing a more general kewyword, as pointed out by Peter Otten -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: User interfaces in console (dialog like)
On Thu, 23 Jun 2005 05:45:07 -0700, Negroup wrote: > Hi all. > > I need to provide to my users a graphical interface to be used from os' > command line. > Initially I thought something equivalent to Unix dialog, and googling > around I have found Python Dialog > (http://pythondialog.sourceforge.net/). This would be the perfect solution > for me if it could be cross platform. However, it doesn't work on Windows, > just on Linux/Unix. > > Do you guys know an alternative that fits my needings without moving from > Python? > > Thanks, > -ng It doesn't depend on the language. There aren't truely portable graphic interface libraries, which show more than a coloured rectangle. For *nix, there is also curses-extra, which offers various widgets (textview,combobox,radio and checkbuttons and so on). http://www.sideralis.net/index.php?action=4&pjid=20 Bye, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP ? os.listdir enhancement
On Thu, 23 Jun 2005 09:21:55 -0600, Ivan Van Laningham wrote: > Mmmm, how about: > > # mylistdir.py > import os, os.path > import sys > > def mylistdir(dir, join=False): > for file in os.listdir(dir): > if join: > yield join(dir, file) > else: > yield file > > print list(mylistdir(sys.argv[1])) > > or > > print list(mylistdir(sys.argv[1],os.path.join)) > > That way I could def my own join and call it as > > print list(mylistdir(sys.argv[1],myjoin)) I think that the implementation of listdir is done in C, so the functionality would be added in C too. by the way, I think 'join' (cute keyword) should be a boolean and not a callable: the obvious way is that we join path using os.path.join, and not some sophisticated function. With a boolean keyword code is smaller and if we want to use our special join function we can do it simply. e.g def func(dir,join=False): return (join and join(dir,x) or x for x in os.listdir(dir)) os.listdir is actually supposed not to be a generator, like you suggested. Are there known future changes ? Bye, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there something similar to ?: operator (C/C++) in Python?
On Fri, 24 Jun 2005 09:00:04 -0500, D H wrote: >> Bo Peng wrote: >> >>> I need to pass a bunch of parameters conditionally. In C/C++, I can >>> do func(cond1?a:b,cond2?c:d,.) >>> >>> Is there an easier way to do this in Python? >> >> > The answer is simply no, just use an if statement instead. That's not true. One used form is this: result = cond and value1 or value2 which is equal to if cond: result=value1 else: result=value2 another form is: result = [value2,value1][cond] the first form is nice but value1 must _always_ have a true value (so not None,0,'' and so on), but often you can handle this. Bye, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Boss wants me to program
On Tue, 28 Jun 2005 12:57:36 -0700, xeys_00 wrote: > The other alternative > is to install console mode linux on it and hope that the ncurses library > can be used by python. Hi, for curses module and linux, I made a library which give you various widgets (combobox, buttons,checkbox,menubar,...). It's hard to find googling so I let you know it here. curses-extra is its name http://www.sideralis.net/index.php?action=4&pjid=20 Bye, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast text display?
On Wed, 08 Jun 2005 13:58:00 -0700, Paul Rubin wrote: > Christopher Subich <[EMAIL PROTECTED]> writes: >> > Use tkinter if you care about cross-platform operation. Everything >> > else requires downloading and installing separate toolkits. >> >> Oh, the downloading and installing isn't a big deal. If in the >> far-flung future anyone else uses this program, they'll be big boys who >> can install things themselves. :) > > No, it's a big pain. I'm a big boy and gave up on trying to install > wxpython for bittorrent on FC3 (the problem was with wxwidgets needing an > obsolete version of gtk, not with wxpython itself). There's just no > compelling reason to want to deal with this stuff. Tkinter has its warts > but it allows making functional gui's without all that much fuss. Using tkinter doesn't need downloading and installing only in Windows. In *nix is not so common to have tcl/tk installed (and probably in Mac too) GUI cross platform need external support, in a OS or in another. Bye, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
dynamic
Hi all. It's easier if I show an example first. Say I have class A(object): def speak(self): print 'A!' class B(object): def speak(self): print 'B!' I also have a factory function like this. def foo(kind,*args,**kwds): if kind=='a': return A(*args,**kwds) else: return B(*args,**kwds) I need foo to be a class, so that I could inherit from it and still use it as a factory, so that I can do: Foo('a').speak() Foo('b'.speak() class Final(Foo): def __init__(self,kind,*args,**kwds): super(Foo,self).__init__(kind,*args,*kwds) Can I do it? How ? If it is possible, I'm pretty sure it involves using __new__ on Foo, but I can't figure out how to make it works. Any help is appreciated. Thank you, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
__new__ and dynamic inheriting
Hi all. It's easier if I show an example first. Say I have class A(object): def speak(self): print 'A!' class B(object): def speak(self): print 'B!' I also have a factory function like this. def foo(kind,*args,**kwds): if kind=='a': return A(*args,**kwds) else: return B(*args,**kwds) I need foo to be a class, so that I could inherit from it and still use it as a factory, so that I can do: Foo('a').speak() Foo('b'.speak() class Final(Foo): def __init__(self,kind,*args,**kwds): super(Foo,self).__init__(kind,*args,*kwds) Can I do it? How ? If it is possible, I'm pretty sure it involves using __new__ on Foo, but I can't figure out how to make it works. Any help is appreciated. Thank you, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic
On Wed, 15 Jun 2005 03:12:07 -0700, Michele Simionato wrote: > Having a class that returns instances of some other class is horrible, but > since you asked for it: > > class A(object): pass > class B(object): pass > > class Foo(object): > def __new__(cls, arg): > if arg=="a": > return A() > else: > return B() > > print Foo("a") > print Foo("b") > > Michele Simionato > > P.S. don't do it! Ciao Michele. I have n classes wich share the same interface. I then have a single class which add functionality to all the n classes, using their interface. The only way I see to make this single class inherith from the choosed nx class is this one. If there is a better approach, I can implement it. Thank you for the answer, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic
On Wed, 15 Jun 2005 11:50:27 +0200, Riccardo Galli wrote: Hi. Thanks to all who answered. Scott David posted a solution (clever, scott) which was what I asked for, but I noticed some oddness while using it (about __init__). I also understood that this way should be avoided. I ended up using a proxy approach, wich works. Thank you, Riccardo -- Riccardo Galli Sideralis Programs http://www.sideralis.net -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: Python GUI development using XULRunner
On 17 Set, 03:29, Todd Whiteman <[EMAIL PROTECTED]> wrote: > I've put together a tutorial that shows off how to build a GUI > application using XULRunner (same architectural components as Firefox > uses) that can be used in conjunction with the Python programming language. > > The tutorial covers how to build a Python/XULRunner GUI > application:http://pyxpcomext.mozdev.org/no_wrap/tutorials/pyxulrunner/python_xul... > > The details in this tutorial covers the initial setup to full > packaging/deployment, mostly targeting a Windows/Linux platform (MacOSX > is possible with a few deviations, I have tried to cover these > deviations where applicable). > > Feedback is welcome. > > Cheers, > Todd well, it just works. Now let's see how :-) Nice job, Riccardo p.s. I'm on a Gentoo Linux 64 bit, no problems at all -- http://mail.python.org/mailman/listinfo/python-list