Re: That's really high-level: bits of beautiful python

2006-02-22 Thread gene tani

Jeffrey Schwab wrote:
> Max wrote:
> > I was just thinking perhaps we should create some kind of collection of
> > bits of "impressive" code like this.
>
> Do you mean something like the ASPN Cookbooks?
>
>   http://aspn.activestate.com/ASPN/Cookbook/
>

repositories, indexes and search engines for py code (pardon the small
number ;-}

http://www.vex.net/parnassus/
http://directory.google.com/Top/Computers/Programming/Languages/Python/Modules/

http://cheeseshop.python.org/

http://dmoz.org/Computers/Programming/Languages/Python/Modules/

http://aspn.activestate.com/ASPN/Cookbook/Python
http://python.codezoo.com/
http://sourceforge.net/softwaremap/trove_list.php?form_cat=178&xdiscrim=178

http://www.bigbold.com/snippets/

http://www.voidspace.org.uk/cgi-bin/pysearch/search.py
http://koders.com/
http://krugle.com/
http://www.codefetch.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: That's really high-level: bits of beautiful python

2006-02-22 Thread Jeffrey Schwab
Max wrote:
> I have a friend who has been programming in C for many years, and he is 
> a great fan of the language. However, he (and I) are about to start a 
> python course, and he has been asking me a lot of questions. He often 
> responds to my answers with "Urgh! Object-orientation!" and suchlike.

After "many years" of C programming, he's still wary of object orientation?

> But today we were discussing the problem of running externally-provided 
> code (e.g. add-on modules). Neither of us knew how to do it in C, though 
> I suggested using DLLs.

It depends on how the module was provided, and on the platform and tool 
chain being used to build the code.  It's typically not too hard on a 
given platform, once you get used to it, but there's certainly no single 
correct answer.

> However, I quickly installed python on his 
> laptop and coded this:
> 
> exec "import %s as ext_mod" % raw_input("Module: ")
> ext_mod.do()

exec'ing raw_input'd code gives me the willies.

> And created to sample modules with do() functions to demonstrate. He was 
> impressed ("That's really high-level" were his words).

It is cool, isn't it?  :)

> I was just thinking perhaps we should create some kind of collection of 
> bits of "impressive" code like this.

Do you mean something like the ASPN Cookbooks?

http://aspn.activestate.com/ASPN/Cookbook/

If you keep track of some examples of "cool" stuff, I'll format them and 
get some web space to post them.  Try to give credit for each example.

> He also liked 99 Bottles in one line:
> 
> print '\n'.join(["%d bottles of beer on the wall." % i for i in 
> range(100,0,-1)])

A little shorter:

for i in range(99, 0, -1): print("%d bottles of beer on the wall." % i)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: That's really high-level: bits of beautiful python

2006-02-21 Thread Rocco Moretti
Max wrote:

> But today we were discussing the problem of running externally-provided 
> code (e.g. add-on modules). Neither of us knew how to do it in C, though 
> I suggested using DLLs. However, I quickly installed python on his 
> laptop and coded this:
> 
> exec "import %s as ext_mod" % raw_input("Module: ")
> ext_mod.do()

Be careful with this - its fine for developer only use, but I'd avoid it 
in production code. You leave the possibility for hackers to try to 
import the module named 'os; os.system('rm -rf /'); import', or other 
such deviousness.

Probably a better version:

ext_mod_name = raw_input("Module: ")
ext_mod = __import__(ext_mod_name, globals(), locals(), ['__dict__'])
ext_mod.do()

But granted, it's less cool than the original.

P.S. The ", globals(), locals(), ['__dict__']" is there so that the 
proper thing is done when you provide the code with a dotted module name.
-- 
http://mail.python.org/mailman/listinfo/python-list


That's really high-level: bits of beautiful python

2006-02-21 Thread Max
I have a friend who has been programming in C for many years, and he is 
a great fan of the language. However, he (and I) are about to start a 
python course, and he has been asking me a lot of questions. He often 
responds to my answers with "Urgh! Object-orientation!" and suchlike.

But today we were discussing the problem of running externally-provided 
code (e.g. add-on modules). Neither of us knew how to do it in C, though 
I suggested using DLLs. However, I quickly installed python on his 
laptop and coded this:

exec "import %s as ext_mod" % raw_input("Module: ")
ext_mod.do()

And created to sample modules with do() functions to demonstrate. He was 
impressed ("That's really high-level" were his words).

I was just thinking perhaps we should create some kind of collection of 
bits of "impressive" code like this.

He also liked 99 Bottles in one line:

print '\n'.join(["%d bottles of beer on the wall." % i for i in 
range(100,0,-1)])

--Max
-- 
http://mail.python.org/mailman/listinfo/python-list