Re: [Tutor] Package installation on Mac does not detect newer version of python

2011-08-30 Thread Walter Prins
On 29 August 2011 11:50, Elisha Rosensweig  wrote:

> Hi All,
>
> I'm trying to use easy_install on my Mac to get the Python networx package.
> On my machine I have installed version 2.5 AND 2.6 of Python. However, wehn
> I try to install this package, I get:
>
> Best match: networkx 1.5
> Downloading http://networkx.lanl.gov/download/networkx/networkx-1.5.zip
> Processing networkx-1.5.zip
> Running networkx-1.5/setup.py -q bdist_egg --dist-dir
> /var/folders/RF/RFpRZ44UEoO77eeJ7t1eOE+++TM/-Tmp-/easy_install-mTZw0S/networkx-1.5/egg-dist-tmp-TGWeRV
> *NetworkX requires Python version 2.6 or later (2.5 detected).*
> error: Setup script exited with -1
>
>
>
> this, despite the fact that my default python is version 2.6:
>
> python
> Python *2.6.2* (r262:71600, Apr 16 2009, 09:17:39)
> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
>
>
>
>  Any ideas how to solve this?
>
>
Offhand, this seems to imply that you have easy_install installed on your
Python 2.5 installation, and not on your 2.6 installation, or alternatively
if you do have easy_install installed on both versions of python then
perhaps somehow easy_install on python 2.5 is the first one found on your
path.

The solution is to a) ensure you have easy_install installed on Python 2.6
and b) Explicitly run that to ensure you're running the correct version (or,
fix the paths if neccesary to ensure the correct version gets run without a
path specified.)

Regards

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] select particular directories and files

2011-08-30 Thread questions anon
Thanks, that also works really well.
One of the hardest things I am finding with programming is that there is
always more than one way to do something. This is a good and bad thing. Bad
for beginners!!
thanks

On Tue, Aug 30, 2011 at 4:45 PM, Peter Otten <__pete...@web.de> wrote:

> questions anon wrote:
>
> > I am trying to select particular files within a particular subdirectory,
> I
> > have been able to do both but not together!
> > When I try to select my files within the dir loop nothing comes up, but
> > when I leave the files outside the dir loops it selects all the files
> > not just the ones in the dirs I have selected.
> > The code I am using is:
> >
> > import os
> >
> > MainFolder=r"D:/samples/"
> >
> > for (path, dirs, files) in os.walk(MainFolder):
> > for dir in dirs:
>
> dirs contains the names of subdirectories of the directory you are
> currently
> processing, but you are interested in the parent directory which you get
> with with os.path.basename(path)
>
> > if dir=='01':
> > print "selected directories are:",dir
> >
> > for ncfile in dir:
> >   if ncfile[-3:]=='.nc':
> > print "ncfiles are:", ncfile
> >
> > Any feedback will be greatly appreciated!!
>
> import os
>
> mainfolder = "d:/samples"
> foldername = "01"
> fileext = ".nc"
>
> for path, folders, files in os.walk(mainfolder):
>if os.path.basename(path) == foldername:
>for file in files:
>if file.endswith(fileext):
>print os.path.join(path, file)
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Re: largest palindrome number

2011-08-30 Thread Chris Fuller

I played around with this puzzle a bit.  I made a palindrome function a bit 
like Bob's, only i iterated through the string (stopping at the first mismatch) 
instead of doing the whole thing at once.

The rest of the code is even more amenable to speedups.  The first thing I saw 
was the outer "i" loop can start at 100, rather than one.  I also saw that 
using PNum = max(palindrome(i*j), PNum) would replace a half dozen lines in 
the first try.  But I discarded this approach.  I figured it would be much 
faster to work backwards from 999*999, stopping at the first palindrome that 
was the product of two three digit numbers.

This code is quite terse, and uses some advanced features (generator 
functions, shortcutting), but I'll try to explain it.


n = 999*999

while True:
   if palindrome(n) and \
  any( (100<=i<1000 and 100<=n/i<1000 for i in \
 (j for j in range(1,1+int(n**.5)) if n%j==0))):
  break
   else:
  n -= 1

print n

The inner "j" loop simply finds factors of n (n**.5 is the square root of n); 
the outer loop checks to see if the factor and its complementary factor are 
three digit numbers.  The generator expressions (like list comprehensions, but 
bounded by parentheses) only calculate one true/false check on a single factor 
at a time.  The any() built-in function returns as soon as it encounters are 
true value, so a lot of computation is avoided versus checking all the factors 
and their three-digit status.

A note on the "if palindrome(n) and \" line.  Python (and C. and most 
languages) will stop computing through and and statement if the first 
expression is false, and likewise for an or statement if the first statement is 
true.  This is the same way any() and all() work, only these functions can 
operate on iterators of arbitrary length.  So by putting the less 
computationally intensive palindrome check first, a few more cpu cycles are 
saved.

I got around an order of magnitude speedup with this code.

Cheers
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml parsing without a root element

2011-08-30 Thread rail shafigulin
On Tue, Aug 30, 2011 at 2:20 PM, Peter Otten <__pete...@web.de> wrote:

> rail shafigulin wrote:
>
> > hello everyone.
> >
> > i need to parse a an xml-like file. the problem that i'm facing is that
> > this file doesn't have the root element but in all other terms it is the
> > same as xml, i.e
> >
> > 
> > 
> >
> > 
> > 
> >
> > 
> >
> > does anybody know if there is a module in python that allows to process
> an
> > xml file without a root element? i tried ElementTree but it didn't work.
>
> There may be more sophisticated ways, but I'd start with a simple idea: add
> a root element to your data and have ElementTree parse the result.
>
> $ cat almost.xml
> foo
> bar
> baz
> $ cat xml_no_root.py
> from StringIO import StringIO
> from xml.etree.ElementTree import ElementTree
>
> filename = "almost.xml"
> tree = ElementTree()
> with open(filename, "rb") as f:
>data = f.read()
> pseudo_file = StringIO("%s" % data )
> tree.parse(pseudo_file)
>
> for link in tree.getiterator("a"):
>print link.text
> $ python xml_no_root.py
> foo
> bar
> baz
>
> If the file is large you can read the file in smaller chunks. Have a look
> at
> the ElementTree.parse() source code to see how to do that.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

thanks everyone. i'm going to encapsulate the file with the root tag
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: largest palindrome number

2011-08-30 Thread bob gailer



 Original Message 
Subject:Re: [Tutor] largest palindrome number
Date:   Tue, 30 Aug 2011 23:24:09 +0530
From:   surya k 
To: bob gailer 



Mr Gailer,

That's an amazing way of writing palindrome function. Actually, I'm
still using my old C logic's here.

Thanks for sharing.


On 8/30/11, bob gailer  wrote:

 On 8/25/2011 12:49 PM, surya k wrote:

 Hi,

 I'm doing a puzzle where it asked me to find the largest palindrome
 number formed by the product of two three-digit numbers. They
 mentioned an example saying that 9009 is the largest palindrome number
 formed by two two-digit numbers (99 * 91).

 I've written my code this way.. and I tested it with the given example
 and I got it right!

 /Logic I used :/
 largest two digit number is 99 and three digit number is 999.. so
 largest product of two two-digit numbers is<  100*100 and for
 three-digit numbers is<  1000*1000.
 So, I used a for loop and it assigns a palindromic value to /PNum/
 till it is<  100*100 (for 2 digit number) and<  1000*1000 (for
 three-digit number)..
 Thus it stops at the max possible palindromic value, which is what we
 want.


 def palindrome (n) :
 TempN = n
 rev  = 0
 while n != 0 :
 k = n % 10
 rev = (rev * 10) + k
 n = n / 10
 if  TempN == rev :
 return TempN # Palindrome
 else :
 return 0 # not Palindrome



 zTwice as fast on my computer!

 def palindrome (n):
  s = '%s' % n
  return s == s[::-1]




 for i in range (1,100) :
 for j in range (i,100) :
 Temp = palindrome(i*j)
 if Temp<  1 and Temp != 0 :
PNum = Temp
 print PNum

 So, for getting the largest palindrome number formed by two
 three-digit numbers, I changed 100 to 1000 and 1,00,00 to 1,000,000 in
 the highlighted area. Thus I got the answer to be 88. When I
 submitted the answer, its saying wrong!

 Where I'm going wrong ?
 help me, please !






 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



 --
 Bob Gailer
 919-636-4239
 Chapel Hill NC




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml parsing without a root element

2011-08-30 Thread davidheiserca

Can you encapsulate the contents of the file in a root element before 
processing?


  - Original Message - 
  From: rail shafigulin 
  To: tutor@python.org 
  Sent: Tuesday, August 30, 2011 10:27 AM
  Subject: [Tutor] xml parsing without a root element


  hello everyone. 

  i need to parse a an xml-like file. the problem that i'm facing is that this 
file doesn't have the root element but in all other terms it is the same as 
xml, i.e

  
  

  
  

  

  does anybody know if there is a module in python that allows to process an 
xml file without a root element? i tried ElementTree but it didn't work.

  any help is appreciated
  thanks



--


  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml parsing without a root element

2011-08-30 Thread Peter Otten
rail shafigulin wrote:

> hello everyone.
> 
> i need to parse a an xml-like file. the problem that i'm facing is that
> this file doesn't have the root element but in all other terms it is the
> same as xml, i.e
> 
> 
> 
> 
> 
> 
> 
> 
> 
> does anybody know if there is a module in python that allows to process an
> xml file without a root element? i tried ElementTree but it didn't work.

There may be more sophisticated ways, but I'd start with a simple idea: add 
a root element to your data and have ElementTree parse the result.

$ cat almost.xml
foo
bar
baz
$ cat xml_no_root.py
from StringIO import StringIO
from xml.etree.ElementTree import ElementTree

filename = "almost.xml"
tree = ElementTree()
with open(filename, "rb") as f:
data = f.read()
pseudo_file = StringIO("%s" % data )
tree.parse(pseudo_file)

for link in tree.getiterator("a"):
print link.text
$ python xml_no_root.py
foo
bar
baz

If the file is large you can read the file in smaller chunks. Have a look at 
the ElementTree.parse() source code to see how to do that.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml parsing without a root element

2011-08-30 Thread Joel Goldstick
On Tue, Aug 30, 2011 at 1:27 PM, rail shafigulin
wrote:

> hello everyone.
>
> i need to parse a an xml-like file. the problem that i'm facing is that
> this file doesn't have the root element but in all other terms it is the
> same as xml, i.e
>
> 
> 
>
> 
> 
>
> 
>
> does anybody know if there is a module in python that allows to process an
> xml file without a root element? i tried ElementTree but it didn't work.
>
> any help is appreciated
> thanks
>
> Why not read the file into a buffer, put the xml tag at the top and wrap
the buffer with a root element.  Then try ElementTree

> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] xml parsing without a root element

2011-08-30 Thread rail shafigulin
hello everyone.

i need to parse a an xml-like file. the problem that i'm facing is that this
file doesn't have the root element but in all other terms it is the same as
xml, i.e









does anybody know if there is a module in python that allows to process an
xml file without a root element? i tried ElementTree but it didn't work.

any help is appreciated
thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] fortune-like utility

2011-08-30 Thread Cranky Frankie
On Tue, Aug 30, 2011 at 5:23 AM, Martin A. Brown  wrote:
>
> Good morning and welcome to the list,
>
>  : Just planning it out in my head so far, like pseudocode. I hope
>  : to get started soon. I'm just looking to have a little dialog box
>  : come up, display a random quote of the day, and then hit enter
>  : and it's gone. Should be a nice, simple way to get started with
>  : Python.
>
> N.B. Most of the questions and answers here are about the python 2.x
> series, so just note that there are some minor differences with
> python 3.x.
>
> I would suggest starting with the data/format.

snip

Thanks Martin for the excellent suggestions.

I read in the book that you have to use escape like /n when you want
to span lines. I don't want to have to do that to each of my quotes,
many of which span several lines, so I will look into your
suggestions.

Thanks again.

-- 
Frank L. "Cranky Frankie" Palmeri
Risible Riding Raconteur & Writer

" . . . and the extended forecast,
until you come back to me, baby,
is high tonight, low tomorrow,
and precipitation is expected."
- Tom Waits, "Emotional Weather Report"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] fortune-like utility was [Tutor Digest ...]

2011-08-30 Thread Martin A. Brown

Good morning and welcome to the list,

 : Just planning it out in my head so far, like pseudocode. I hope 
 : to get started soon. I'm just looking to have a little dialog box 
 : come up, display a random quote of the day, and then hit enter 
 : and it's gone. Should be a nice, simple way to get started with 
 : Python.

N.B. Most of the questions and answers here are about the python 2.x 
series, so just note that there are some minor differences with 
python 3.x.

I would suggest starting with the data/format.  I think somebody 
else has already remarked that with a few thousand quotations, you 
will not have much difficulty loading it all into memory, so here 
are some tips on how to approach the problem by creating a CLI 
application first, moving onto the GUI parts after you have worked 
out the functionality to read a file and produce the quotation.

I don't have the book you are recommending, but here are some things 
that I would be thinking about if I were writing a utility which 
behaves like the venerable unix game called 'fortune'.

  1. Learn how to read line-oriented data from a file [0].  Let's 
 assume that your first pass at this has only one quotation per 
 line.

 This little functon works much like the 'cat' utility:

   import sys
   def main(fname):
   f = open(fname,'r')
   for line in f:
   print line.rstrip()
   f.close()
   main(sys.argv[1])  # python this.py 

 Now, put the lines into a list instead of printing them:

   def main(fname):
   quotations = list()
   f = open(fname,'r')
   for line in f:
   quotations.append( line.rstrip() )
   f.close()
   return quotations

  2. Play with the random module to get your 'pick a quotation at 
 random' behaviour.

import random
print random.choice(quotations)

  3. Experiment with the vagaries of 'print'.  It produces a 
 newline.  Learn how to suppress the newline when printing 
 (even if you don't want to do that this time).

  4. Experiment with string handling.  Learn how to break a string 
 into lists.  Learn how to make a single string out of the 
 contents of a list.  In short, become familiar with using

split()  http://docs.python.org/library/stdtypes.html#str.split
join()   http://docs.python.org/library/stdtypes.html#str.join

  5. Start thinking about what you would have to change in order to 
 handle multi-line or formatted quotations.  This is where you 
 can start thinking about treating your data as records rather 
 than lines, as above.

I suspect that you will have all of the above understood in fairly 
short order.  At that point, you could make the foray into the 
various GUI bits.  I will be no help there, as I have never used any 
of the GUI toolkits--but this list has many others who should be 
able to help here.

I hope the 'Sweet Irene the Disco Queen' flooding in your region was 
not too bad, Frank.

-Martin

P.S. Here's a possibly obnoxious tip, assuming the 'fortune' file 
 format [1] suits your tastes for storing your quotations:

   f = open(fname,'r')
   quotations = ''.join(f.readlines()).split('%')

 [0] http://docs.python.org/library/stdtypes.html#file-objects
 [1] http://www.faqs.org/docs/artu/ch05s02.html


-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor