[Tutor] loop performance in global namespace (python-2.6.1)

2009-03-12 Thread Poor Yorick

In the following snippet, the loop in the global namespace takes twice as long
as the loop in the function namespace.  Why?

limit = 5000

def f1():
counter = 0
while counter < limit:
counter += 1
time1 = time.time()
f1()
print(time.time() - time1)
print('number of locals: ', len(locals()))

time1 = time.time()
counter = 0
while counter < limit:
counter += 1
print(time.time() - time1)
print('number of locals: ', len(locals()))

--
Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reversing

2005-07-20 Thread Poor Yorick
Kevin Bixler wrote:

> I was asked to make a program that reverses the text that a user would 
> input. I have tried what I thought would work which is 
> phrase.reverse(). phrase = the raw_input. Help!
> Thank you,
> Kevin Bixler

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> a = 'hello there'
 >>> a[::-1]
'ereht olleh'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading random line from a file

2007-07-18 Thread Poor Yorick
>  ---Original Message---
>  From: Kent Johnson <[EMAIL PROTECTED]>
>  Subject: Re: [Tutor] reading random line from a file
>  Sent: 2007-07-18 10:19
>  
[SNIP]
>  
>  It probably doesn't matter, but this will pick longer lines more often
>  than short ones.
>  

This method only keeps one line in memory, only reads through the file once, 
and does not favor lines based on any characteristic of the line.  It's 
probably fast enough to not even bother keeping an index around:

#!/bin/env python 

import os
import random

text = 'shaks12.txt'
if not os.path.exists(text):
   os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt')

f = file(text, 'rb')

def randline(f):
   for i,j in enumerate(f):
  if random.randint(0,i) == i:
 line = j 
   return line

print randline(f) 


--- 
Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] opening a pipe?

2008-02-12 Thread Poor Yorick
>  From: James Hartley <>
>  Subject: [Tutor] opening a pipe?
>  Sent: 2008-02-12 09:24
>  
>  A Perl script can easily serve as a filter within a pipe as seen in
>  the following:
>  
>  use strict;
>  use warnings;
>  
>  open(IN, 'cat hello.txt |') or die 'unable to open file';
>  while () {
>  print;
>  }
>  close(IN);
>  
>  Can I do the same within Python?  Thanks.
>  

os.popen can do this.  The 'commands' module provides even more flexibility.  
If you are on a posix platform and want to avoid shell interpretation, you 
could also try my pipeline package at 

http://pypi.python.org/pypi/pipeline.py/0.1

-- 
Yorick
"Our servers are using too much electricity.  We need to virtualize."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why won't it enter the quiz? (off topic)

2005-09-16 Thread Poor Yorick
Byron wrote:
> Hi Nathan C,
> 
> PS> No, (for the record only), there is a HUGE difference between 
> selling "skills" and selling code that someone else wrote.  :-D
> 

Once again you're implying that there's something wrong with Nathan asking for 
help on this list.  Blow it out your hairdo...

Bye,
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do fix this error?

2005-09-16 Thread Poor Yorick
Nathan Pinno wrote:

> guess = float(raw_input(a," + ",b," = "))
> TypeError: [raw_]input expected at most 1 arguments, got 4
>  

> guess = float(raw_input(a," + ",b," = "))
> return answer, guess
>  
hint:  how many arguments are you passing to raw_input here?

--
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why won't it enter the quiz? (off topic)

2005-09-17 Thread Poor Yorick
Alan G wrote:
 > 
> Technically there may be something wrong in that if he is
> claiming copyright and selling for commerxcial gain rather
> than selling it as open source  he has to prove that he

Then I recommend that Byron and anyone else interested in this stuff go browse 
http://www.benedict.com/ instead posting off-topic commentaries about people 
who turn to the list with questions.

Bye,
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do you organize code snippets?

2005-09-18 Thread Poor Yorick
List wrote:

> Is there a way of naming or organizing snippets that might help? (For 
> example, file i/o snippets, text processing snippets, etc.)
> 

If you really want to have fun, you can use LEO, 
http://webpages.charter.net/edreamleo/front.html to store your code snippets 
and generate files from them.  The fun part is that you can clone each node 
which may contain some code and move the clone to a different point in the 
hierarchy.  When you change your code in any of the clones, it gets changed in 
all of the clones.  At the most basic level, you can use LEO as on outliner, 
like Treepad.  If you decide to get more complicated, you can use LEO as a 
templating tool to generate your resulting .py files from your snippets.

--
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python, vim, indentation, and wrapped lines

2005-09-21 Thread Poor Yorick
I use Vim as my primary Python code editor.  I've always wondered if 
there is a way to make wrapped lines take on the indentation of the 
previous line.  Of course I mean *in the display only*, since I don't 
want any newlines introduced.  This would also be handy for me when 
editing indented html/xml documents.  Anyone know how to do this in vim?

--
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Editors (particualrly Vim)

2005-09-22 Thread Poor Yorick
Ed Singleton wrote:

>Okay, I've also found this:
>http://cream.sourceforge.net/features.html
>
>  
>
It seems to me that all the functionality listed here can easily be done 
in regular vim.  I would still recommend investing time in learning 
plain old vim or gvim.  The payoff for proficiency is high.

--
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: Re: USB Capture Image WebCAM

2005-09-22 Thread Poor Yorick
Alberto Troiano wrote:

> 
> But it doesn't works so fine, I think its because I cracked and didn't buy 
> it *grin*
> But I need like 200 licenses so it would be a little expensive. That's why 
> I'm trying to make my own Python Gengis Cam *grin*
> 

You just admitted on a public forum to committing a crime.  Not wise, 
friend.

--
Poor Yorick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Challenge [was Re: Why won't it enter the quiz?]

2005-09-22 Thread Poor Yorick
bob wrote:
> At 06:42 PM 9/22/2005, Nathan Pinno wrote:
> 
>> The URL is http://zoffee.tripod.com/purchasecomprogs.htm
> 
> 
> [snip]
> 
> At your invitation I visited the site. I personally would not purchase 
> anything listed there due to insufficient information. I don't know what 
> I'm getting!
> 
> I suggest you dedicate a page to each program with at least one 
> screenshot and explanation of what the program does.

The shareware system would work best for these little programs. If it
were me, I would just use the honor system -- a pop-up window on program
startup that says "friendly reminder: This program costs $12.99. Have
you paid me for it or contributed to charity in my name yet? (yes/no)" 
Even though some (or even many, who knows?) people won't pay, it's worth 
the exposure. Another advantage is if people like what they download, 
they will come back and look for other things. In effect, your previous 
programs can advertise your newer programs.  Exposure is valuable.

--
Poor Yorick

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Binary 2 text & text 2 binary

2005-09-24 Thread Poor Yorick
Pujo Aji wrote:
> If your symbol are specific it is better to use dictionary.
> then if the user give an input you can take character by character and 
> translate into your binary.
> This is the code textTobinary:
> mydic = {'A' : "0101", 'B' : "0110", 'C' : "0111"}
> strinput = 'ABBC'
> result = [mydic[x] for x in strinput_process]
> print result
>  

You might also want to look at "Number to String in Arbirtrary Base" recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/365468

It uses a more innovative approach.

--
Poor Yorick

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Prevent "Coder's Remorse"?

2005-09-28 Thread Poor Yorick
Danny Yoo wrote:

>You may want to change the data structure.
>
>Rather than have points all be peers, reorganize them so that there's some
>kind of hierarchy: that'll help you directly represent the "sharing" of
>attributes.  Explicitly:
>
>#
>class Point:
>def __init__(self, lat, lon, atts, parent=None):
>(self.lat, self.lon, self.atts, self.parent) = (
>  lat, long, atts, parent)
>def lookup(self, name):
>if name in self.atts:
>return self.atts[name]
>if self.parent:
>return self.parent.lookup(name)
>raise KeyError
>#
>
>  
>

Many thanks Danny.  This example was an moment of epiphany for me, one
that will probably drastically influence me in the future.  I owe you
big time!

--
Poor Yorick

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor