Re: [Tutor] Scrolling through output in shell

2008-11-17 Thread Shawn Milochik
On Sun, Nov 16, 2008 at 1:21 PM, Mike Hoy [EMAIL PROTECTED] wrote:
 I'm writing a small program that writes to a text file. I want to be
 able to view the contents of the text file inside of shell. But the
 file is too large for a small shell window. Is there a way for the
 user to 'scroll' through the contents of file that has been read into
 the program? I noticed that on the man pages that you can do that
 although I'm sure it's not written in python. Do I need to find a new
 language to write this in? Maybe use a different language for the
 output and still use python? Any help appreciated.

 --
 Mike Hoy
 http://www.mikehoy.net



As Alan has noted, your request isn't perfectly clear. So, I'm going
to change your question and answer it. If I picked the wrong question,
please be more explicit in your next reply.

Question: How can I read a text file from the command line if the file
is too large to fit on the screen at once?

Answer: more or less
If you're in Windows, you can use the more command: more file.txt
That will allow you to scroll up and down.

If you're on pretty much any other OS, you can use more or less. I
prefer less, because it has more features. You use it the same way you
use more:  less file.txt
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running a script from another folder

2008-11-12 Thread Shawn Milochik
On Wed, Nov 12, 2008 at 12:58 PM, greg whittier [EMAIL PROTECTED] wrote:
 it looks like you're on linux - so at the beginning of your script put
 #!/usr/bin/env python (I believe) and then chmod +x myscript.py

 then you can call it from the command line.


 You'll also need to make sure ~myID/bin is in your PATH.
 _

This is the answer, assuming you have the shebang line and executable
flags mentioned above.

Method (assuming you are using bash):

Add this to your .bashrc file in your home directory:

export PATH=$PATH:${HOME}/bin

I used ${HOME} instead of ~ to make this more portable. However, if
your system doesn't work that way, use the ~ or the full path.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building RSS reader with Python

2008-11-05 Thread Shawn Milochik
On Wed, Nov 5, 2008 at 3:26 PM,  [EMAIL PROTECTED] wrote:
 Hi everyone,

 I'm new to programming and am building a very basic rss reader for my first
 major project with python and GUI. As it is, I have it set up so that if I
 input an exact rss feed address (ex http://news.google.com/?output=rss) I
 can retrieve stories. Id like to make it so that I can enter a site (like
 http://news.google.com) and have it search for the address of rss feeds on
 the site, so that I don't have to know exact page addresses for the rss,
 since most sites don't have such a straightforward location for the RSS
 page. Is there a way to do this?

 Thanks,
 Luke
 ___



My gut reaction is to say that it's a lost cause. Everybody does their
own thing on the Internet, and you'll never be able to describe all of
the ways people do it.

However, there are *some* standards out there. Check with the big CMS
tools (WordPress, Drupal, Zope, Plone, etc.) and see if they have a
standard. That'll help. Hit up some of the big sites (Google News,
Reuters, CNN, BBC) and see what they do.

You'll end up with a database of maybe a few dozen things to try
(example.com/rss.xml, example.com/blog?feed=rss2,
example.com/feeds.atom, and many more), and you can search for them by
default.

Having said that, it'll never be perfect, and it may not even be close
enough to perfect to be worth the maintenance. I refer back to my gut
reaction above. But you never know -- you could be the guy who
maintains the most complete reference to RSS URLs on the Internet, and
all will download your list for their own projects. Somebody has to be
the best in that market.

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


Re: [Tutor] fast list traversal

2008-10-30 Thread Shawn Milochik
On Thu, Oct 30, 2008 at 2:40 PM, bob gailer [EMAIL PROTECTED] wrote:
 Dinesh B Vadhia wrote:

 I need to process a large number ( 20,000) of long and variable length
 lists ( 5,000 elements) ie.
  for element in long_list:
do something with element# the result of this operation is
 not a list
  The performance is reasonable but I wonder if there are faster Python
 methods?


You might try using dictionaries instead. I've had phenomenal speed
gains by switching lists to dictionaries before, although that may
have had more to do with the fact that I needed to access certain
values, rather than iterating through them in sequential order like
you're doing.

It seems counter-intuitive because  a dictionary has a key and a
value, and you really only need the key (you can leave all the values
blank, or set them to zero or something), but it's a lot faster to
find a dictionary element than a list element. It has something to do
with the underlying code, but I have been assured that the same thing
applies to hashes and arrays in Perl, etc.

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


Re: [Tutor] fast list traversal

2008-10-30 Thread Shawn Milochik
On Thu, Oct 30, 2008 at 4:05 PM, Kent Johnson [EMAIL PROTECTED] wrote:
 On Thu, Oct 30, 2008 at 2:46 PM, Shawn Milochik [EMAIL PROTECTED] wrote:

 You might try using dictionaries instead. I've had phenomenal speed
 gains by switching lists to dictionaries before, although that may
 have had more to do with the fact that I needed to access certain
 values, rather than iterating through them in sequential order like
 you're doing.

 Looking up individual values (i.e. testing for membership) is much
 faster for dicts and sets than for lists. Problems of the form

 for item in long_list_1:
  if item in long_list_2:
do something with item

 will be much faster if long_list_2 is changed to a set. This is
 because the complexity goes from O(len(long_list_1) *
 len(long_list_2)) to O(len(long_list_1)).

 Iterating is probably faster on lists, certainly it is not
 phenomenally faster to iterate a dict than a list.

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




I just ran a very crude test.

Results: Lists load more quickly but iterate more slowly. Dictionaries
take longer to load but iteration takes about half the time.

Sample output:



Results for 999 iterations:
List:
Load: 4.75 seconds
Process: 6.64 seconds

Dictionary:
Load: 7.54 seconds
Process: 3.31 seconds

Comparison:
List loads 0.63 times the speed of a dictionary.
List processes loads 2.00 times the speed of a dictionary.







Results for 7654321 iterations:
List:
Load: 3.29 seconds
Process: 4.72 seconds

Dictionary:
Load: 5.47 seconds
Process: 2.41 seconds

Comparison:
List loads 0.60 times the speed of a dictionary.
List processes loads 1.96 times the speed of a dictionary.







Code:


#!/usr/bin/env python


import time

the_dict = {}
the_list = []

iter_num = 7654321


fill_list_time = time.time()

for x in range(iter_num):
the_list.append(x)

fill_list_time = time.time() - fill_list_time

fill_dict_time = time.time()

for x in range(iter_num):
the_list.append(x)
the_dict[x] = 0

fill_dict_time = time.time() - fill_dict_time





proc_list_time = time.time()

for thing in the_list:

thing = thing * 2

proc_list_time = time.time() - proc_list_time


proc_dict_time = time.time()

for thing in the_dict:

thing = thing * 2

proc_dict_time = time.time() - proc_dict_time


print Results for %d iterations: % iter_num
print \tList:
print \t\tLoad: %.2f seconds % fill_list_time
print \t\tProcess: %.2f seconds % proc_list_time
print
print \tDictionary:
print \t\tLoad: %.2f seconds % fill_dict_time
print \t\tProcess: %.2f seconds % proc_dict_time
print
print Comparison: 
print \tList loads %.2f times the speed of a dictionary. %
(fill_list_time / fill_dict_time)
print \tList processes loads %.2f times the speed of a dictionary. %
(proc_list_time / proc_dict_time)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] fast list traversal

2008-10-30 Thread Shawn Milochik
On Thu, Oct 30, 2008 at 6:06 PM, wesley chun [EMAIL PROTECTED] wrote:
 based on the all the performance questions, i would say agree that
 dictionary access is faster than lists (hashes exist cuz they're fast)
 but they use up more memory, as shown in shawn's numbers. also, one of
 the reasons why slots was added to classes was because the attribute
 dictionary began to impact memory (when creating many instances), so
 converting that to __slots__ list of attributes instead made it more
 conservative of resources.

 thus, i would suggest using sets instead. if the data doesn't change,
 then frozensets. a for-loop thru sets would be very fast; and in
 Python 3.0, you can use *set comprehensions* too. :-)

 shawn, do you have time to run some numbers for sets and frozensets?

 anyway, just a suggestion...
 -wesley


They seem pretty similar. Here are two tests (code follows). Perhaps I
could have loaded them differently and it would have made more of a
difference. In this case I just made a list and populated the sets
from it.

Results for 999 iterations:
Set:
Load: 1.24 seconds
Process: 4.43 seconds

Frozenset:
Load: 0.96 seconds
Process: 4.09 seconds

Comparison:
Set loads 1.30 times the speed of a frozenset.
Set processes loads 1.08 times the speed of a frozenset.





Results for 999 iterations:
Set:
Load from list: 0.89 seconds
Process: 3.72 seconds

Frozenset:
Load from list: 0.94 seconds
Process: 4.67 seconds

Comparison:
Set loads 0.94 times the speed of a frozenset.
Set processes loads 0.80 times the speed of a frozenset.


Results for 7654321 iterations:
Set:
Load from list: 0.81 seconds
Process: 2.52 seconds

Frozenset:
Load from list: 0.76 seconds
Process: 2.52 seconds

Comparison:
Set loads 1.07 times the speed of a frozenset.
Set processes loads 1.00 times the speed of a frozenset.






#!/usr/bin/env python


import time

the_list = []

iter_num = 7654321


fill_list_time = time.time()

for x in range(iter_num):
the_list.append(x)

fill_list_time = time.time() - fill_list_time

fill_frozenset_time = time.time()
the_frozenset = frozenset(the_list)
fill_frozenset_time = time.time() - fill_frozenset_time


fill_set_time = time.time()
the_set = set(the_list)
fill_set_time = time.time() - fill_set_time



proc_set_time = time.time()

for thing in the_set:

thing = thing * 2

proc_set_time = time.time() - proc_set_time


proc_frozenset_time = time.time()

for thing in the_frozenset:

thing = thing * 2

proc_frozenset_time = time.time() - proc_frozenset_time


print Results for %d iterations: % iter_num
print \tSet:
print \t\tLoad from list: %.2f seconds % fill_set_time
print \t\tProcess: %.2f seconds % proc_set_time
print
print \tFrozenset:
print \t\tLoad from list: %.2f seconds % fill_frozenset_time
print \t\tProcess: %.2f seconds % proc_frozenset_time
print
print Comparison: 
print \tSet loads %.2f times the speed of a frozenset. %
(fill_set_time / fill_frozenset_time)
print \tSet processes loads %.2f times the speed of a frozenset. %
(proc_set_time / proc_frozenset_time)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to compare the first line from multiple files

2008-01-31 Thread Shawn Milochik

-Original Message-
From: Sorghum Crow [EMAIL PROTECTED]
To: tutor@python.org
Subject: [Tutor] How to compare the first line from multiple files
Date: Thu, 31 Jan 2008 14:19:55 -0500

How to compare the first line from multiple files

Greetings,

 I'm new on the forum and relatively new to Python. I have been tasked
at work with checking some files for consistency.

 What I need to do is verify that the first lines of a group of text
files are identical.
 The files are all in the same directory.
 I know what the first line should be (or I can take the first line
from the first file as the reference.)

How would I go about doing this.

Thanks in advance.

Regards,
Sorghum Crow
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor





Try this:

Find code which will loop through each file in a directory. Easy to do.

Open each file and read the first line into a variable.

Then do something fun with this info, such as adding it to a dictionary 
and incrementing the value for that dictionary value each time you find that 
line.

When you're done, you can check the length of the dictionary to see if there is 
more than
one value, and then view the contents of the dictionary to find out what the 
unique values are.


Shawn



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