Need help about for loop in python 3.2

2011-06-22 Thread kkiranmca
Hi i am new for this version and could please help me .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python 3 constant

2011-06-22 Thread Waldek M.
Dnia Wed, 22 Jun 2011 22:17:49 +0100, Noah Hall napisał(a):

> On Wed, Jun 22, 2011 at 7:54 PM, sidRo  wrote:
>> How to declare a constant in python 3?
> 
> There aren't true constants in Python, but instead we use a standard
> defined by PEP 8, which states constants are in all caps, for example,
> PI = 3.14, as opposed to pi = 3.14 which could change (according to
> PEP 8, that is)

That said, there are some workarounds for that:
http://code.activestate.com/recipes/65207-constants-in-python/?in=user-97991

Still, I'd reallly like to have constants as a built-in...

Best regards,
Waldek
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 2.7 and cmd on Windows 7 64 (files lost)

2011-06-22 Thread Michel Claveau - MVP
Hi!

(sorry for my bad english...)

On Win 7 64 bits:
  Command-Line
  CD \Python27
  dir C:\Windows\System32\SoundRecorder.exe:==> OK
  Python.exe

>>> import os
>>> os.system("dir C:\\Windows\\System32\\SoundRecorder.exe")  

==> Do not found the file !!!

and os.system("cmd /k")  then  "dir C:\Windows\System32\SoundRecorder.exe"  do 
not found
anyway.

But:
  {Ctrl-Z} in Python
  then  dir C:\Windows\System32\SoundRecorder.exe  run OK

Therefore, is the problem only in Python? 


@+
-- 
Michel Claveau 

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


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Tim Roberts
Mel  wrote:
>
>It certainly can be done faster.  I ran it against the factor finder that I 
>wrote, and it popped up the answer
>
>mwilson@tecumseth:~$ bin/factors.py 600851475143
>71 839 1471 ...
>
>before I could glance at my watch.  factors.py works, as does yours, by 
>testing for small factors first, but it divides them out as it goes, so it 
>tends to do its work on smallish numbers.  And since the smallest factors 
>are taken out as soon as possible, they have to be the prime ones.

That's a great hint, and I'm not sure it would have occurred to me on my
own.  Using your hint, I was able to write a 16-line script that also
produced a result instantaneously.  Very satisfying.

I'm going to save that one...
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: connect windows share

2011-06-22 Thread FunAt Work
On my cygwin system I just do the following for my network drive 'q'

import commands
print commands.getoutput('ls /cygdrive/q')

Run it as - python fileList.py

Here is the output:

DataTables
Functions
Object_Repositories
Recovery_Scenarios
Scripts
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what happens inside?

2011-06-22 Thread FunAt Work
Do the same thing with an interconversion of tuple and list and you will be off 
to the older way:
a=(1,2,3)
b=list(a)
b[0]=11
print a
print b

Output:
(1, 2, 3)
[11, 2, 3]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: User Authentication

2011-06-22 Thread Anurag
On Jun 22, 7:01 pm, Adam Tauno Williams 
wrote:
> On Wed, 2011-06-22 at 06:34 -0700, Anurag wrote:
> > Hi All,
>
> > I am working on application which needs to do a authentication against
> > LDAP, if LDAP not installed then local system account (administrator
> > user in windows and root user in Linux). This should work on both
> > Windows and Linux.
>
> See python-ldap

I looked into python-ldap, it supports ldap authentication. But I
didn't find anything that support local system account authentication
when no LDAP both in windows and Linux.

Ond more thing, somebody suggested me to use PAM. Is it a good choice
and whether it supports both Windows and Linux? Please let me know
which is best to use.

Regards,
Anurag
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread FunAt Work
Don't relate it anyhow to foreach of perl I would say, although the behaviour 
may be same in some aspect
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parse date string having "EDT"

2011-06-22 Thread Tim Roberts
Ben Finney  wrote:

>Tim Roberts  writes:
>
>> Right, because strptime doesn't support %Z.
>
>Au contraire:
>
>Support for the %Z directive is based on the values contained in
>tzname and whether daylight is true. Because of this, it is
>platform-specific except for recognizing UTC and GMT which are
>always known (and are considered to be non-daylight savings timezones).

I do keep forgetting that Python's strptime does not just pass through to
the C library.  C's strptime does not support %Z.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run a function in SPE on Python 2.5

2011-06-22 Thread FunAt Work
Not tried SPE. But in PyScripter, as simple as that.

import sys
def scramble2Decrypt(cipherText):
halfLength = len(cipherText) // 2
oddChars = cipherText[:halfLength]
evenChars = cipherText[halfLength:]
plainText = ""

for i in range(halfLength):
plainText = plainText + evenChars[i]
plainText = plainText + oddChars[i]

if len(oddChars) < len(evenChars):
plainText = plainText + evenChars[-1]

return plainText



if __name__ == '__main__':
   print sys.argv[1]
   print scramble2Decrypt(sys.argv[1])


Run as inside the scripter in debug mode either or run from command line as:

Run-1
UserXP@prive-9dc0b2aec ~/PLScripts
$ python encrypt.py asdf
asdf
dafs

Run-2
UserXP@prive-9dc0b2aec ~/PLScripts
$ python encrypt.py 

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


Re: writable iterators?

2011-06-22 Thread Carl Banks
On Wednesday, June 22, 2011 4:10:39 PM UTC-7, Neal Becker wrote:
> AFAIK, the above is the only python idiom that allows iteration over a 
> sequence 
> such that you can write to the sequence.  And THAT is the problem.  In many 
> cases, indexing is much less efficient than iteration.

Well, if your program is such that you can notice a difference between indexing 
and iteration, you probably have better things to worry about.  But whatever.  
You can get the effect you're asking for like this:


class IteratorByProxy(object):
def __init__(self,iterable):
self.set(iterable)
def __iter__(self):
return self
def next(self):
return self.current_iter.next()
def set(self,iterable):
self.current_iter = iter(iterable)

s = IteratorByProxy(xrange(10))
for i in s:
print i
if i == 6:
s.set(xrange(15,20))


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


Re: How to run a function in SPE on Python 2.5

2011-06-22 Thread MRAB

On 23/06/2011 03:19, bill lawhorn wrote:

I have a program: decrypt2.py which contains this function:


def scramble2Decrypt(cipherText):
 halfLength = len(cipherText) // 2
 oddChars = cipherText[:halfLength]
 evenChars = cipherText[halfLength:]
 plainText = ""

 for i in range(halfLength):
 plainText = plainText + evenChars[i]
 plainText = plainText + oddChars[i]


[snip]
FYI, this can be shortened to:

plainText = cipherText[0 : : 2] + cipherText[1 : : 2]
--
http://mail.python.org/mailman/listinfo/python-list


How to run a function in SPE on Python 2.5

2011-06-22 Thread bill lawhorn
I have a program: decrypt2.py which contains this function:


def scramble2Decrypt(cipherText):
halfLength = len(cipherText) // 2
oddChars = cipherText[:halfLength]
evenChars = cipherText[halfLength:]
plainText = ""

for i in range(halfLength):
plainText = plainText + evenChars[i]
plainText = plainText + oddChars[i]

if len(oddChars) < len(evenChars):
plainText = plainText + evenChars[-1]

return plainText

When I load this program in SPE and click on debug with winpdb i'm not
sure what to do next. In Idle I would just
type in scramble2Decrypt(""), hit enter an get a result.

I have tried to call this function from the Command box in winpdb and
also at the >> in SPE with no luck.

I want to be able to step through the program one line at a time.

Any help would be appreciated for this nube.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handling import errors

2011-06-22 Thread Guillaume Martel-Genest
I did not think about using a global variable, and the top-level
try...except solution is interesting. After further thinking, I have
to reformulate my initial question:

How do I manage to run code before my imports?

For example, I want to make sure that I can use the logging module in
the case an import fails, so I want to call logging.basicConfig()
before this particular import. Likewise, I could want to import a
module whose path is relative to an environment variable, and would
want to test if this variable is set before doing so.

I have come up with 2 solution templates :

>>> import logging
>>>
>>> main()
>>>
>>> def pre_import():
... logging.basicConfig(format='%(message)s')
>>>
>>> def import():
... global foo
... import foo
>>>
>>> def main():
... pre_import()
... import()

>>> import logging
>>> logging.basicConfig(format='%(message)s')
>>> import foo
>>>
>>> main()
>>>
>>> def main():
... pass

To me, the latter looks better, but I could be missing something. In
any case, surrounding the entire program with try...except would look
like the following?

>>> try:
... import logging
... logging.basicConfig(format='%(message)s')
... import foo
...
... main()
>>> except Exception:
... # Display simple error message
>>>
>>> def main():
... pass
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread Steven D'Aprano
On Thu, 23 Jun 2011 09:30 am Thomas 'PointedEars' Lahn wrote:

> Mel wrote:
> 
>> Steven D'Aprano wrote:
>>> I *guess* that what you mean by "writable iterators" is that rebinding e
>>> should change seq in place, i.e. you would expect that seq should now
>>> equal [42, 42]. Is that what you mean? It's not clear.
>>> 
>>> Fortunately, that's not how it works, and far from being a "limitation",
>>> it would be *disastrous* if iterables worked that way. I can't imagine
>>> how many bugs would occur from people reassigning to the loop variable,
>>> forgetting that it had a side-effect of also reassigning to the
>>> iterable. Fortunately, Python is not that badly designed.
>> 
>> And for an iterator like
>> 
>> def things():
>> yield 1
>> yield 11
>> yield 4
>> yield 9
>> 
>> I don't know what it could even mean.
> 
> 
> 
> You could have tried to debug.

I think you have missed the point of Mel's comment. He knows what the yield
statement does. He doesn't know what it would mean to "write to" an
iterator like things().

Neither do I.



-- 
Steven

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


Re: writable iterators?

2011-06-22 Thread Steven D'Aprano
On Thu, 23 Jun 2011 09:10 am Neal Becker wrote:

> Steven D'Aprano wrote:
> 
>> On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:
>> 
>>> AFAICT, the python iterator concept only supports readable iterators,
>>> not write. Is this true?
>>> 
>>> for example:
>>> 
>>> for e in sequence:
>>>   do something that reads e
>>>   e = blah # will do nothing
>>> 
>>> I believe this is not a limitation on the for loop, but a limitation on
>>> the python iterator concept.  Is this correct?
>> 
>> Have you tried it? "e = blah" certainly does not "do nothing", regardless
>> of whether you are in a for loop or not. It binds the name e to the value
>> blah.
>> 
> 
> Yes, I understand that e = blah just rebinds e.  I did not mean this as an
> example of working code.  I meant to say, does Python have any idiom that
> allows iteration over a sequence such that the elements can be assigned?

Yes. I already gave one:

for i, e in enumerate(seq):
seq[i] = e + 42


If you look at code written before the enumerate built-in, you will often
find code like this:

for i in range(len(seq)):
e = seq[i]
seq[i] = e + 42


Sometimes you'll find code that does this:

i = 0
while i < len(seq):
e = seq[i]
seq[i] = e + 42
i += 1

but don't do that, it's slow.

Or you can do this:

seq[:] = [e+42 for e in seq]

There are others.

[...]
> AFAIK, the above is the only python idiom that allows iteration over a
> sequence
> such that you can write to the sequence.  And THAT is the problem.  In
> many cases, indexing is much less efficient than iteration.

Are you aware that iteration is frequently based on indexing?

In the cases that it isn't, that's because the iterator generates values
lazily, without ever storing them. You *can't* write to them because
there's nowhere to write to! If you want to store the values so they are
writable, then you have to use indexing.

What makes you think that this is a problem in practice? Can you give an
example of some task you can't solve because you (allegedly) can't write to
a sequence?


-- 
Steven

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


Why are my signals being ignored?

2011-06-22 Thread Anthony Papillion
Hello Everyone,

So I figured out the last problem about why I couldn't load my UI files but
now I've got something that has be totally stumped. I've worked on it most
of the day yesterday, Google'd it, and fought with it today and I'm
admitting defeat and coming to the group with hat in hand asking for help.

For some reason, in the code below, my signals are being completely ignored.
For example, you'll notice that I connected the 'btnExit' to the method
called 'clickedButton()' but, for some reason, when the button is clicked,
the method is never called. I've been following tutorials and this seems to
be the proper way to wire signals yet mine simply don't work.

Would anyone be so kind as to look over the following code and give me a bit
of advice (or direction) as to what I might be doing wrong?

Thanks!
Anthony

Code:

#!/usr/bin/env python

import sys

try:
import pygtk
 pygtk.require("2.0")
except:
print "Error. PYGTK could not be loaded."
 sys.exit(1)
try:
import gtk
import gtk.glade
except:
print "GTK not present or not loaded."
sys,exit(1)
 class TestClass(object):
 def __init__(self):
 self.uiFile = "MainWindow.glade"
self.wTree = gtk.Builder()
self.wTree.add_from_file(self.uiFile)
 self.window = self.wTree.get_object("winMain")
if self.window:
 self.window.connect("destroy", gtk.main_quit)
 dic = { "on_btnExit_clicked" : self.clickButton, "on_winMain_destroy" :
gtk.main_quit }
 self.wTree.connect_signals(dic)
self.window.show()
else:
 print "Could not load window"
sys.exit(1)
  def clickButton(self, widget):
print "You clicked exit!"
  def exit(self, widget):
 gtk.main_quit()
 def update_file_selection(self, widget, data=None):
 selected_filename = FileChooser.get_filename()
print selected_filename
 if __name__ == "__main__":
Tester = TestClass()
gtk.main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread MRAB

On 23/06/2011 00:10, Neal Becker wrote:

Steven D'Aprano wrote:


On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:


AFAICT, the python iterator concept only supports readable iterators,
not write. Is this true?

for example:

for e in sequence:
   do something that reads e
   e = blah # will do nothing

I believe this is not a limitation on the for loop, but a limitation on
the python iterator concept.  Is this correct?


Have you tried it? "e = blah" certainly does not "do nothing", regardless
of whether you are in a for loop or not. It binds the name e to the value
blah.



Yes, I understand that e = blah just rebinds e.  I did not mean this as an
example of working code.  I meant to say, does Python have any idiom that allows
iteration over a sequence such that the elements can be assigned?


[snip]
Python has references to objects, but not references to references.
--
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread Thomas 'PointedEars' Lahn
Mel wrote:

> Steven D'Aprano wrote:
>> I *guess* that what you mean by "writable iterators" is that rebinding e
>> should change seq in place, i.e. you would expect that seq should now
>> equal [42, 42]. Is that what you mean? It's not clear.
>> 
>> Fortunately, that's not how it works, and far from being a "limitation",
>> it would be *disastrous* if iterables worked that way. I can't imagine
>> how many bugs would occur from people reassigning to the loop variable,
>> forgetting that it had a side-effect of also reassigning to the iterable.
>> Fortunately, Python is not that badly designed.
> 
> And for an iterator like
> 
> def things():
> yield 1
> yield 11
> yield 4
> yield 9
> 
> I don't know what it could even mean.



You could have tried to debug.

Please trim your quotes to the relevant minimum.

-- 
PointedEars

Bitte keine Kopien per E-Mail. / Please do not Cc: me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread Chris Kaynor
You could probably implement something like this using generators and the
send method (note the example is untested and intended for 2.6: I lack
Python on this machine):

def gen(list_):
  for i, v in enumerate(list_):
list_[i] = yield v

def execute():
  data = range(10)
  iterator = gen(data)
  lastValue = iterator.next()
  while True:
print lastValue
try:
  lastValue = iterator.send(lastValue + 1)
except StopIteration:
  break
  print data

>>> execute()
0
1
2
3
4
5
6
7
8
9
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]




Chris


On Wed, Jun 22, 2011 at 4:10 PM, Neal Becker  wrote:

> Steven D'Aprano wrote:
>
> > On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:
> >
> >> AFAICT, the python iterator concept only supports readable iterators,
> >> not write. Is this true?
> >>
> >> for example:
> >>
> >> for e in sequence:
> >>   do something that reads e
> >>   e = blah # will do nothing
> >>
> >> I believe this is not a limitation on the for loop, but a limitation on
> >> the python iterator concept.  Is this correct?
> >
> > Have you tried it? "e = blah" certainly does not "do nothing", regardless
> > of whether you are in a for loop or not. It binds the name e to the value
> > blah.
> >
>
> Yes, I understand that e = blah just rebinds e.  I did not mean this as an
> example of working code.  I meant to say, does Python have any idiom that
> allows
> iteration over a sequence such that the elements can be assigned?
>
> ...
> > * iterators are lazy sequences, and cannot be changed because there's
> > nothing to change (they don't store their values anywhere, but calculate
> > them one by one on demand and then immediately forget that value);
> >
> > * immutable sequences, like tuples, are immutable and cannot be changed
> > because that's what immutable means;
> >
> > * mutable sequences like lists can be changed. The standard idiom for
> > that is to use enumerate:
> >
> > for i, e in enumerate(seq):
> > seq[i] = e + 42
> >
> >
> AFAIK, the above is the only python idiom that allows iteration over a
> sequence
> such that you can write to the sequence.  And THAT is the problem.  In many
> cases, indexing is much less efficient than iteration.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread Neal Becker
Steven D'Aprano wrote:

> On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:
> 
>> AFAICT, the python iterator concept only supports readable iterators,
>> not write. Is this true?
>> 
>> for example:
>> 
>> for e in sequence:
>>   do something that reads e
>>   e = blah # will do nothing
>> 
>> I believe this is not a limitation on the for loop, but a limitation on
>> the python iterator concept.  Is this correct?
> 
> Have you tried it? "e = blah" certainly does not "do nothing", regardless
> of whether you are in a for loop or not. It binds the name e to the value
> blah.
> 

Yes, I understand that e = blah just rebinds e.  I did not mean this as an 
example of working code.  I meant to say, does Python have any idiom that 
allows 
iteration over a sequence such that the elements can be assigned?

...
> * iterators are lazy sequences, and cannot be changed because there's
> nothing to change (they don't store their values anywhere, but calculate
> them one by one on demand and then immediately forget that value);
> 
> * immutable sequences, like tuples, are immutable and cannot be changed
> because that's what immutable means;
> 
> * mutable sequences like lists can be changed. The standard idiom for
> that is to use enumerate:
> 
> for i, e in enumerate(seq):
> seq[i] = e + 42
> 
> 
AFAIK, the above is the only python idiom that allows iteration over a sequence 
such that you can write to the sequence.  And THAT is the problem.  In many 
cases, indexing is much less efficient than iteration.

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


Re: what happens inside?

2011-06-22 Thread Terry Reedy

On 6/22/2011 11:45 AM, Chetan Harjani wrote:

why tuples are immutable whereas list are mutable?


Because tuples do not have mutation methods, which lists do.
Tuple and lists both have .__getitem__ but tuples do not have 
.__setitem__ or .__delitem__ (or .append, .extend, .sort, or .reverse).



why when we do x=y where y is a list and then change a element in x, y
changes too( but the same is not the case when we change the whole value
in x ), whereas, in tuples when we change x, y is not affected and also
we cant change each individual element in tuple. Someone please clarify.


For specific answers, you should give specific examples.

(1,2)[0] = 3 does not work because there is no tuple.__setitem__. But note:
>>> a = ([1,2], 3)
>>> b = a
>>> a[0][0] = 4
>>> b
([4, 2], 3)
Tuples containing mutables are not immutable all the way down.

They are also not hashable (if any element is not hashable) and cannot 
be used as dict keys.

>>> hash(a)
Traceback (most recent call last):
  File "", line 1, in 
hash(a)
TypeError: unhashable type: 'list'

--
Terry Jan Reedy

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


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Terry Reedy

On 6/22/2011 1:32 AM, Paul Rubin wrote:

Terry Reedy  writes:

If the best C program for a problem takes 10 seconds or more, then
applying the same 1 minute limit to Python is insane, and contrary to
the promotion of good algorithm thinking.



The Euler problems


are not the only algorithm problems posted on the web.

 > the Euler project started in 2001, when personal computers were probably

10x slower than they are today.


Yes, and I am still using a 2003 XP/Athlon machine, which is adequate 
for running my Python programs, though not recent games.


--
Terry Jan Reedy

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


PyPad 2.7.1 Update 4

2011-06-22 Thread Jon Dowdall

Hi All,

I'm pleased to announce that PyPad 2.7.1 (Update 4), a simple python 
environment for the iPad / iPhone, is now available in the iTunes 
store. It can be found at: 
http://itunes.apple.com/au/app/pypad/id428928902?mt=8


Update 4 adds the ability to create multiple modules and import them 
into one another.  The documentation is lagging a bit but the 
functionality can be found in the action menu of the accessory bar.


Further updates planed include:
Updated documentation
Improving cursor interaction in interactive mode
Providing access to interactive mode command history
Adding iOS specific modules
Syntax highlighting
and more...

Regards,

Jon Dowdall

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


Re: Unicode codepoints

2011-06-22 Thread Vlastimil Brom
2011/6/22 Saul Spatz :
> Thanks.  I agree with you about the generator.  Using your first suggestion, 
> code points above U+ get separated into two "surrogate pair" characters 
> fron UTF-16.  So instead of U=10 I get U+DBFF and U+DFFF.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Hi,
If you realy need the wide unicode functionality on a narrow unicode
python build and only need to get the string index of characters
including surrogate pairs counting as one item, you can build a list
of single characters or surrogate pairs, e.g.:

>>> surrog_txt=u"a𐌰 𐌱 𐌲 𐌳"
>>> surrog_txt
u'a\U00010330 \U00010331 \U00010332 \U00010333'
>>> print surrog_txt
a𐌰 𐌱 𐌲 𐌳
>>> list(surrog_txt)
[u'a', u'\ud800', u'\udf30', u' ', u'\ud800', u'\udf31', u' ',
u'\ud800', u'\udf32', u' ', u'\ud800', u'\udf33']
>>> import re
>>> re.findall(ur"(?s)(?:[\ud800-\udbff][\udc00-\udfff])|.", surrog_txt)
[u'a', u'\U00010330', u' ', u'\U00010331', u' ', u'\U00010332', u' ',
u'\U00010333']
>>>

this way, the indices, slices and len() would work on the
supplementary list as expected for a normal string; however it
probably won't be very efficient for longer texts.
Note that surrogates are not the only asymmetry between code points,
characters (and glyphs - to take the visual representation of those
into account) - there are combining diacritical marks, in various
combinations with precomposed diacritical characters, multiple
normalisation modes etc.

regards,
   vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writable iterators?

2011-06-22 Thread Mel
Steven D'Aprano wrote:

> On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:
> 
>> AFAICT, the python iterator concept only supports readable iterators,
>> not write. Is this true?
>> 
>> for example:
>> 
>> for e in sequence:
>>   do something that reads e
>>   e = blah # will do nothing
>> 
>> I believe this is not a limitation on the for loop, but a limitation on
>> the python iterator concept.  Is this correct?
> 
> Have you tried it? "e = blah" certainly does not "do nothing", regardless
> of whether you are in a for loop or not. It binds the name e to the value
> blah.
> 
 seq = [1, 2]
 for e in seq:
> ... print(e)
> ... e = 42
> ... print(e)
> ...
> 1
> 42
> 2
> 42
> 
> 
> I *guess* that what you mean by "writable iterators" is that rebinding e
> should change seq in place, i.e. you would expect that seq should now
> equal [42, 42]. Is that what you mean? It's not clear.
> 
> Fortunately, that's not how it works, and far from being a "limitation",
> it would be *disastrous* if iterables worked that way. I can't imagine
> how many bugs would occur from people reassigning to the loop variable,
> forgetting that it had a side-effect of also reassigning to the iterable.
> Fortunately, Python is not that badly designed.

And for an iterator like

def things():
yield 1
yield 11
yield 4
yield 9

I don't know what it could even mean.

Mel.

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


Re: writable iterators?

2011-06-22 Thread Steven D'Aprano
On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:

> AFAICT, the python iterator concept only supports readable iterators,
> not write. Is this true?
> 
> for example:
> 
> for e in sequence:
>   do something that reads e
>   e = blah # will do nothing
> 
> I believe this is not a limitation on the for loop, but a limitation on
> the python iterator concept.  Is this correct?

Have you tried it? "e = blah" certainly does not "do nothing", regardless 
of whether you are in a for loop or not. It binds the name e to the value 
blah.

>>> seq = [1, 2]
>>> for e in seq:
... print(e)
... e = 42
... print(e)
...
1
42
2
42


I *guess* that what you mean by "writable iterators" is that rebinding e 
should change seq in place, i.e. you would expect that seq should now 
equal [42, 42]. Is that what you mean? It's not clear.

Fortunately, that's not how it works, and far from being a "limitation", 
it would be *disastrous* if iterables worked that way. I can't imagine 
how many bugs would occur from people reassigning to the loop variable, 
forgetting that it had a side-effect of also reassigning to the iterable. 
Fortunately, Python is not that badly designed.

If you want to change the source iterable, you have to explicitly do so. 
Whether you can or not depends on the source:

* iterators are lazy sequences, and cannot be changed because there's 
nothing to change (they don't store their values anywhere, but calculate 
them one by one on demand and then immediately forget that value);

* immutable sequences, like tuples, are immutable and cannot be changed 
because that's what immutable means;

* mutable sequences like lists can be changed. The standard idiom for 
that is to use enumerate:

for i, e in enumerate(seq):
seq[i] = e + 42



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


Re: python 3 constant

2011-06-22 Thread Noah Hall
On Wed, Jun 22, 2011 at 7:54 PM, sidRo  wrote:
> How to declare a constant in python 3?

There aren't true constants in Python, but instead we use a standard
defined by PEP 8, which states constants are in all caps, for example,
PI = 3.14, as opposed to pi = 3.14 which could change (according to
PEP 8, that is)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python 3 constant

2011-06-22 Thread Benjamin Kaplan
On Jun 22, 2011 12:03 PM, "sidRo"  wrote:
>
> How to declare a constant in python 3?
> --

You don't. Python doesn't have declarations (other than global and
nonlocal). Convention is that anything in all caps  should be considered a
constant but there's no language-level enforcement of it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: connect windows share

2011-06-22 Thread Benjamin Kaplan
On Jun 22, 2011 11:44 AM, "Travis Altman"  wrote:
>
> I want to be able to connect to a windows share via python.  My end goal
is to be able to recursively search through windows shares.  I want to do
this in Linux as well.  So given a share such as \\computer\test I would
like to search through the test directory and any sub directories for any
file names of interest.  What's the best way of going about this?  I know
LDAP / AD creds may play an essential part in this as well.  Thanks for your
input.
>
> --

I haven't done anything with this myself, but my guess would be that there's
a Python samba wrapper somewhere that you'll want to use for this.

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


Re: writable iterators?

2011-06-22 Thread Benjamin Kaplan
On Jun 22, 2011 12:31 PM, "Neal Becker"  wrote:
>
> AFAICT, the python iterator concept only supports readable iterators, not
write.
> Is this true?
>
> for example:
>
> for e in sequence:
>  do something that reads e
>  e = blah # will do nothing
>
> I believe this is not a limitation on the for loop, but a limitation on
the
> python iterator concept.  Is this correct?

consider the following code

x = [1,2,3]
y = x[0]
y = 5

Would you expect x to be [5,2,3] now? Assignment in python assigns a name to
an object. It doesn't change the value of a variable.

If you have a generator, you can use send() to put data in you need the
actual generator itself to do that.

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


Re: writable iterators?

2011-06-22 Thread Ethan Furman

Neal Becker wrote:
AFAICT, the python iterator concept only supports readable iterators, not write.  
Is this true?


for example:

for e in sequence:
  do something that reads e
  e = blah # will do nothing

I believe this is not a limitation on the for loop, but a limitation on the 
python iterator concept.  Is this correct?


No.  e = blah will rebind the indentifier 'e' with 'blah' whatever that 
is.  That is how python works.


Now, if e is mutable, say a list, you can do

   e.append(blah)

and, since the name 'e' is not being rebound, you would see the change 
in 'sequence'.


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


writable iterators?

2011-06-22 Thread Neal Becker
AFAICT, the python iterator concept only supports readable iterators, not 
write.  
Is this true?

for example:

for e in sequence:
  do something that reads e
  e = blah # will do nothing

I believe this is not a limitation on the for loop, but a limitation on the 
python iterator concept.  Is this correct?

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


Re: connect windows share

2011-06-22 Thread Tim Golden

On 22/06/2011 19:38, Travis Altman wrote:

I want to be able to connect to a windows share via python.  My end goal
is to be able to recursively search through windows shares.  I want to
do this in Linux as well.  So given a share such as \\computer\test I
would like to search through the test directory and any sub directories
for any file names of interest.  What's the best way of going about
this?  I know LDAP / AD creds may play an essential part in this as
well.  Thanks for your input.


Unless there's more to this than meets the eye, the answer's
really easy: just do it.

On Windows, you just do, eg:

import os

for dirpath, dirnames, filenames in os.walk (r"\\computer\test"):
  # do stuff with stuff

Presumably on Linux you have to use whatever mechanism allows
you to address Windows shares in the equivalent way. Maybe

os.walk ("smb://computer/test") ?? I'm not really a Linux person,
I'm afraid

TJG

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


python 3 constant

2011-06-22 Thread sidRo
How to declare a constant in python 3?
-- 
http://mail.python.org/mailman/listinfo/python-list


connect windows share

2011-06-22 Thread Travis Altman
I want to be able to connect to a windows share via python.  My end goal is
to be able to recursively search through windows shares.  I want to do this
in Linux as well.  So given a share such as \\computer\test I would like to
search through the test directory and any sub directories for any file names
of interest.  What's the best way of going about this?  I know LDAP / AD
creds may play an essential part in this as well.  Thanks for your input.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: running an existing script

2011-06-22 Thread Ethan Furman

Adam Chapman wrote:

Thanks again Ethan, It did begin to run nfold.py this time, after I
added the environment variable "CLASSPATH" to my system. It threw back
a java error, but I guess this isn;t the right place to be asking
about that

C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py --
booster=Adaboost --folds=5 --data=spambase.data --spec=spambase.spec --
rounds=500 --tree=ADD_ALL --generate
nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate
java.lang.NoClassDefFoundError: jboost/controller/Controller
Caused by: java.lang.ClassNotFoundException:
jboost.controller.Controller
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: jboost.controller.Controller.  Program
will exit.
Exception in thread "main" k: 0 start:0 end:920



Looking at the batch file I see a line that modifies the CLASSPATH, so 
try this before you run nfold.py (cut across two lines, but it's really 
just one):
set CLASSPATH= 
%CLASSPATH%;../dist/jboost.jar;../lib/jfreechart-1.0.10.jar;../lib/jcommon-1.0.8.jar 




Good luck!

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


Re: running an existing script

2011-06-22 Thread Adam Chapman
On Jun 22, 6:13 pm, Adam Chapman 
wrote:
> On Jun 22, 5:51 pm, Adam Chapman 
> wrote:
>
>
>
>
>
>
>
>
>
> > On Jun 22, 5:51 pm, Ethan Furman  wrote:
>
> > > Adam Chapman wrote:
> > > > On Jun 22, 4:54 pm, Adam Chapman 
> > > > wrote:
> > > >> On Jun 21, 9:12 pm, Adam Chapman 
> > > >> wrote:
>
> > > >>> On Jun 21, 8:00 pm, Ethan Furman  wrote:
> > >  Adam Chapman wrote:
> > > > Thanks Ethan
> > > > No way could I have worked that out in my state of stress!
> > > > For your second idea, would I need to type that into the python 
> > > > command
> > > > line interface (the one that looks like a DOS window?
> > >  If you are actually in a python CLI, at the top of that screen does 
> > >  it
> > >  say something like
> > >  Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> > >  (Intel)] on win32
> > >  Type "help", "copyright", "credits" or "license" for more 
> > >  information.
> > >  ?
> > >  If yes, then what I wrote earlier should actually work (I downloaded
> > >  jBoost and looked at the nfold.py script).  Here it is again:
> > >  --> import os
> > >  --> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
> > >  --> import nfold
> > >  --> import sys
> > >  --> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
> > >  ... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
> > >  ... "--generate" ]
> > >  ...
> > >  --> nfold.main()
> > >  I fixed the sys.argv line from last time.
> > >  Good luck!
> > >  ~Ethan~
> > > >>> Thanks to both of you for your help.
> > > >>> It's getting late here, I'll give it another try tomorrow
> > > >> I've added the python directories to the environment variable "path"
> > > >> in my computer (http://showmedo.com/videotutorials/video?
> > > >> name=96&fromSeriesID=96), which means I can now call python from
> > > >> the windows DOS-style command prompt.
>
> > > >> My formatting must be wrong when calling the nfold.py script to run.
> > > >> My connad prompt call and the computer's response look like this:
>
> > > >> C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
> > > >> nfold.py
> > > >>   File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
> > > >> \nfold.py", line 13
> > > >>     print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
> > > >> generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
> > > >> tree=treetype]'
>
> > > >> ^
> > > >> SyntaxError: invalid syntax
>
> > > >> What I dont understand is that some of the parameters in the syntax it
> > > >> printed back are in <> brackets, and others in [] brackets.
>
> > > >> I assume this is something a regular python user could notice straight
> > > >> away.
>
> > > >> Please let me know, I'd be very grateful
>
> > > > I just tried
>
> > > > nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
> > > > spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate --dir=C:
> > > > \Users\Adam\Desktop\cvdata
>
> > > > in the dos-style command prompt. It didn'g vive a syntax error this
> > > > time, it just repeated my command back to me in text. I assume I
> > > > called code correctly, but it didn't make a new folder full of data
> > > > like it should have.
>
> > > Which version of jBoost, and which version of Python?
>
> > > ~Ethan~
>
> > jboost 2.2, python 2.7
>
> > somehow I've just managed to delete all of the code in nfold.py, now
> > downloading it again...
>
> Thanks a lot, must be getting close now...
> I changed the indentation one lines 136-168, and put in the command
> window:
>
> nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
> spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate
>
> no syntax errors this time, it just said:
> nfold.py: Your CLASSPATH is not set. You must place jboost.jar in your
> CLASSPATH.
>
> is that the chdir() command in python? and can I somehow set that in
> the dos command window?

Thanks again Ethan, It did begin to run nfold.py this time, after I
added the environment variable "CLASSPATH" to my system. It threw back
a java error, but I guess this isn;t the right place to be asking
about that

C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py --
booster=Adaboost --folds=5 --data=spambase.data --spec=spambase.spec --
rounds=500 --tree=ADD_ALL --generate
nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate
java.lang.NoClassDefFoundError: jboost/controller/Controller
Caused by: java.lang.ClassNotFoundException:
jboost.controller.Controller
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 

Re: doing cross platform file work

2011-06-22 Thread John Gordon
In  Tim Hanson 
 writes:

> For the more general case, I am a Linux user interested in making my scripts 
> platform neutral, which would include Linux, Unix (including Mac), and 
> Windows.  I have looked at the python.org os segment and didn't get an answer.

> Is there a library (bonus would be some tutorial material) for making sure my 
> Linux scripts access files and directories on the other platforms 
> transparently?  I don't need the information immediately, but at some point...

Your code should work on multiple platforms as long as you use standard
python functions like open(), print(), close(), etc.

Filesystem paths are a little trickier, as different platforms disallow
different characters in filenames, but as long as you stick to a common
set of characters that are legal in all cases you should be fine.  And of
course use os.sep.

Beyond that, what did you specifically have in mind?

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: sorry, possibly too much info. was: Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Irmen de Jong
On 22-6-2011 5:02, John Salerno wrote:

> Thanks. So far they are helping me with Python too, but definitely not
> as much as more general exercises would, I'm sure. The part about
> writing the code is fun, but once that's done, I seem to end up stuck
> with an inefficient implementation because I don't know the math
> tricks behind the problem.
> 
> I'll check out rubyquiz.com. I've been searching for some Python
> exercises to do but haven't found too many sites with them, at least
> not in such a nice and organized way as Project Euler.

There's also SPOJ; http://www.spoj.pl/problems/classical/
It has tons of problems, many math related, but also silly ones like this:
https://www.spoj.pl/problems/JAVAC/
and perhaps even useful ones like this:
https://www.spoj.pl/problems/ONP/

Irmen


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


doing cross platform file work

2011-06-22 Thread Tim Hanson
Thanks for your responses to my student question about using OS paths in 
Python.

For the more general case, I am a Linux user interested in making my scripts 
platform neutral, which would include Linux, Unix (including Mac), and 
Windows.  I have looked at the python.org os segment and didn't get an answer.

Is there a library (bonus would be some tutorial material) for making sure my 
Linux scripts access files and directories on the other platforms 
transparently?  I don't need the information immediately, but at some point...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Security test of embedded Python

2011-06-22 Thread Irmen de Jong
On 22-6-2011 4:44, Chris Angelico wrote:
> Followup: The test box has been administratively taken offline after
> about an hour of testing. Thank you to everyone who participated; it
> seems we have a lot of changes to make!
> 
> Monty failed the test. But it was an incredibly successful test. And
> hopefully, we'll be bringing things back online for another shot once
> things are sorted out!
> 
> Chris Angelico

Maybe you should have a look at sandboxed pypy?
http://pypy.org/features.html#sandboxing

(disclaimer: never used it myself)

Irmen

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


Re: running an existing script

2011-06-22 Thread Ethan Furman

Adam Chapman wrote:

Thanks a lot, must be getting close now...
I changed the indentation one lines 136-168, and put in the command
window:

nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate

no syntax errors this time, it just said:
nfold.py: Your CLASSPATH is not set. You must place jboost.jar in your
CLASSPATH.

is that the chdir() command in python? and can I somehow set that in
the dos command window?


CLASSPATH is an environment variable, jboost.jar is a java file (which 
I'm sure you know ;) -- so make sure CLASSPATH is set appropriately for 
your system (e.g. 'set CLASSPATH=c:\java\source'), and jboost.jar is 
whereever CLASSPATH points to.  (I'm not a Java fan, so can't provide 
much help in this area.)


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


Re: Unicode codepoints

2011-06-22 Thread jmfauth
On 22 juin, 16:07, Saul Spatz  wrote:
> Thanks very much.  This is the elegant kind of solution I was looking for.  I 
> had hoped there was a way to do it without even addressing the matter of 
> surrogates, but apparently not.  The reason I don't like this is that it 
> depends on knowing that python internally stores strings in UTF-16.  I 
> expected that there would be some built-in iterator that would return the 
> code points.  (Actually, this all started when I realized that s[k] wouldn't 
> necessarily give me the kth character of the string s.)

A character is not a code point.

Beside this, a very few knows (correct English?) a character may
have more than one code point.

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


Re: running an existing script

2011-06-22 Thread Adam Chapman
On Jun 22, 5:51 pm, Adam Chapman 
wrote:
> On Jun 22, 5:51 pm, Ethan Furman  wrote:
>
>
>
>
>
>
>
>
>
> > Adam Chapman wrote:
> > > On Jun 22, 4:54 pm, Adam Chapman 
> > > wrote:
> > >> On Jun 21, 9:12 pm, Adam Chapman 
> > >> wrote:
>
> > >>> On Jun 21, 8:00 pm, Ethan Furman  wrote:
> >  Adam Chapman wrote:
> > > Thanks Ethan
> > > No way could I have worked that out in my state of stress!
> > > For your second idea, would I need to type that into the python 
> > > command
> > > line interface (the one that looks like a DOS window?
> >  If you are actually in a python CLI, at the top of that screen does it
> >  say something like
> >  Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> >  (Intel)] on win32
> >  Type "help", "copyright", "credits" or "license" for more information.
> >  ?
> >  If yes, then what I wrote earlier should actually work (I downloaded
> >  jBoost and looked at the nfold.py script).  Here it is again:
> >  --> import os
> >  --> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
> >  --> import nfold
> >  --> import sys
> >  --> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
> >  ... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
> >  ... "--generate" ]
> >  ...
> >  --> nfold.main()
> >  I fixed the sys.argv line from last time.
> >  Good luck!
> >  ~Ethan~
> > >>> Thanks to both of you for your help.
> > >>> It's getting late here, I'll give it another try tomorrow
> > >> I've added the python directories to the environment variable "path"
> > >> in my computer (http://showmedo.com/videotutorials/video?
> > >> name=96&fromSeriesID=96), which means I can now call python from
> > >> the windows DOS-style command prompt.
>
> > >> My formatting must be wrong when calling the nfold.py script to run.
> > >> My connad prompt call and the computer's response look like this:
>
> > >> C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
> > >> nfold.py
> > >>   File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
> > >> \nfold.py", line 13
> > >>     print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
> > >> generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
> > >> tree=treetype]'
>
> > >> ^
> > >> SyntaxError: invalid syntax
>
> > >> What I dont understand is that some of the parameters in the syntax it
> > >> printed back are in <> brackets, and others in [] brackets.
>
> > >> I assume this is something a regular python user could notice straight
> > >> away.
>
> > >> Please let me know, I'd be very grateful
>
> > > I just tried
>
> > > nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
> > > spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate --dir=C:
> > > \Users\Adam\Desktop\cvdata
>
> > > in the dos-style command prompt. It didn'g vive a syntax error this
> > > time, it just repeated my command back to me in text. I assume I
> > > called code correctly, but it didn't make a new folder full of data
> > > like it should have.
>
> > Which version of jBoost, and which version of Python?
>
> > ~Ethan~
>
> jboost 2.2, python 2.7
>
> somehow I've just managed to delete all of the code in nfold.py, now
> downloading it again...

Thanks a lot, must be getting close now...
I changed the indentation one lines 136-168, and put in the command
window:

nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate

no syntax errors this time, it just said:
nfold.py: Your CLASSPATH is not set. You must place jboost.jar in your
CLASSPATH.

is that the chdir() command in python? and can I somehow set that in
the dos command window?





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


Re: running an existing script

2011-06-22 Thread Adam Chapman
On Jun 22, 5:51 pm, Ethan Furman  wrote:
> Adam Chapman wrote:
> > On Jun 22, 4:54 pm, Adam Chapman 
> > wrote:
> >> On Jun 21, 9:12 pm, Adam Chapman 
> >> wrote:
>
> >>> On Jun 21, 8:00 pm, Ethan Furman  wrote:
>  Adam Chapman wrote:
> > Thanks Ethan
> > No way could I have worked that out in my state of stress!
> > For your second idea, would I need to type that into the python command
> > line interface (the one that looks like a DOS window?
>  If you are actually in a python CLI, at the top of that screen does it
>  say something like
>  Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
>  (Intel)] on win32
>  Type "help", "copyright", "credits" or "license" for more information.
>  ?
>  If yes, then what I wrote earlier should actually work (I downloaded
>  jBoost and looked at the nfold.py script).  Here it is again:
>  --> import os
>  --> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
>  --> import nfold
>  --> import sys
>  --> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
>  ... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
>  ... "--generate" ]
>  ...
>  --> nfold.main()
>  I fixed the sys.argv line from last time.
>  Good luck!
>  ~Ethan~
> >>> Thanks to both of you for your help.
> >>> It's getting late here, I'll give it another try tomorrow
> >> I've added the python directories to the environment variable "path"
> >> in my computer (http://showmedo.com/videotutorials/video?
> >> name=96&fromSeriesID=96), which means I can now call python from
> >> the windows DOS-style command prompt.
>
> >> My formatting must be wrong when calling the nfold.py script to run.
> >> My connad prompt call and the computer's response look like this:
>
> >> C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
> >> nfold.py
> >>   File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
> >> \nfold.py", line 13
> >>     print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
> >> generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
> >> tree=treetype]'
>
> >> ^
> >> SyntaxError: invalid syntax
>
> >> What I dont understand is that some of the parameters in the syntax it
> >> printed back are in <> brackets, and others in [] brackets.
>
> >> I assume this is something a regular python user could notice straight
> >> away.
>
> >> Please let me know, I'd be very grateful
>
> > I just tried
>
> > nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
> > spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate --dir=C:
> > \Users\Adam\Desktop\cvdata
>
> > in the dos-style command prompt. It didn'g vive a syntax error this
> > time, it just repeated my command back to me in text. I assume I
> > called code correctly, but it didn't make a new folder full of data
> > like it should have.
>
> Which version of jBoost, and which version of Python?
>
> ~Ethan~

jboost 2.2, python 2.7

somehow I've just managed to delete all of the code in nfold.py, now
downloading it again...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rant on web browsers

2011-06-22 Thread Thomas 'PointedEars' Lahn
[I am biting only because this is my field of expertise, and I am really 
getting tired reading from people not having a shadow of a trace of a 
minimum clue what these languages that I like can and can't do.]

Chris Angelico wrote:

> Random rant and not very on-topic. Feel free to hit Delete and move on.

Why not post on-topic in the first place, and *ask* before being 
presumptuous?  Get better.
 
> I've just spent a day coding in Javascript,

There is no "Javascript".

> and wishing browsers supported Python instead (or as well).

Some browsers do in a way, because of the ECMAScript implementations they 
employ.  Mozilla.org JavaScript 1.7+ is pretty pythonic as compared to, e.g. 
Microsoft JScript.  Array comprehension, Array methods like filter(), and 
iterators/generators come to mind.  This is not a coincidence; Brendan Eich 
has explicitly referred to Python in his blog.

> All I needed to do was take two dates (as strings), figure out the
> difference in days, add that many days to both dates, and put the results
> back into DOM Input objects (form entry fields).

How that would be done would first depend on what you consider a day to be.
(We have been over this elsewhere.)  You can easily compute the difference 
between the time value of Date instances, which is stored as milliseconds 
since epoch, by subtraction (conversion to number is implicit, you do not 
have to call the getTime() method):

  var d1 = new Date(…, …, …, 12);
  var d2 = new Date(…, …, …, 12);
  var ms = d2 - d1;

[I have been told that the 12 hours into the day can avoid problems with DST 
changes.  You may use any hours value that you see fit, even none (which 
defaults to 0).]

Dividing that by the number of milliseconds per "day" (which is conveniently 
written 864e5) would give you a good idea of the number of "day"s (but watch 
out for floating-point precision).

You can also write a method (of Date instances, if desired!) that can take 
calendar days into account – like saying that there are two days "between" 
`new Date(2011, 5, 22)' and `new Date(2011, 5, 24)' –, since overflow on 
Date instances is easily detected:

  Date.prototype.diff = function(d2) {
// …
  };

  var d = new Date(…);
  var diffInDays = d.diff(new Date(…));

Finally, adding or subtracting calendar days to a date is very simple, of 
course:

  var d = new Date();
  d.setDate(d.getDate() + 2);

> Pretty simple, right? Javascript has a Date class,

No, it does not.  (Have you, by chance, read Flanagan's latest edition on 
the subject, full of misconceptions?)

For your purposes, "Javascript" has no classes at all; if accepted as an 
umbrella term (which I strongly recommend against¹), then the programming 
*languages* it describes in your context all use *prototype-based* 
inheritance.

> it should be fine. But no. First, the date object can't be
> outputted as a formatted string.

Yes, it can.  There are several methods on the Date prototype object for 
that, and you can always write and add your own.

> The only way to output a date is "Feb 21 2011".

Wrong.

> So I have to get the three components (oh and the month is
> 0-11, not 1-12) and emit those.

Not necessarily.  (Yes, the month value is zero-based.  This is not 
particular to ECMAScript implementations.)

> And Javascript doesn't have a simple format function that would force the
> numbers to come out with leading zeroes,

But it is easily written, simplified:

  function leadingZero(n, width)
  {
n = String(n);
var len = n.length;
if (len >= width) return n;

var a = [];
a.length = width - len + 1;
return a.join("0") + n;
  }

> so I don't bother with that.

Your problem alone.
 
> What if I want to accept any delimiter in the date - slash, hyphen, or
> dot?

The most simple way is to convert it so that the string format is understood 
by the Date constructor.  There is precedence for the formats accepted by 
implementations in Web browsers as they are specified in ECMAScript Ed. 5.

However, the most reliable way, unless you are using dates before 100 CE, is 
to parse the components and pass them as separate arguments to the Date 
constructor.  There is even a way to use an Array instance for that (a 
construct method added to Function.prototype or Date), so you can reuse the 
return value of String.prototype.match().

> Can I just do a simple translate, turn all slashes and dots into
> hyphens?

Yes, you can, but it depends on the input format.

> Nope. Have to go regular expression if you want to change
> more than the first instance of something.

Again, that depends on the input format.

> There's no nice string parse function (like sscanf with "%d-%d-%d"), so I
> hope every browser out there has a fast regex engine.

I think sscanf() can be easily written (and is going to be [once again]; 
still working on an efficient sprintf()).

But why accept the date as a string, which is known to be ambiguous, in the 
first place?  Date input should be facilitated b

Re: running an existing script

2011-06-22 Thread Ethan Furman

Adam Chapman wrote:

On Jun 22, 4:54 pm, Adam Chapman 
wrote:

On Jun 21, 9:12 pm, Adam Chapman 
wrote:










On Jun 21, 8:00 pm, Ethan Furman  wrote:

Adam Chapman wrote:

Thanks Ethan
No way could I have worked that out in my state of stress!
For your second idea, would I need to type that into the python command
line interface (the one that looks like a DOS window?

If you are actually in a python CLI, at the top of that screen does it
say something like
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
?
If yes, then what I wrote earlier should actually work (I downloaded
jBoost and looked at the nfold.py script).  Here it is again:
--> import os
--> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
--> import nfold
--> import sys
--> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
... "--generate" ]
...
--> nfold.main()
I fixed the sys.argv line from last time.
Good luck!
~Ethan~

Thanks to both of you for your help.
It's getting late here, I'll give it another try tomorrow

I've added the python directories to the environment variable "path"
in my computer (http://showmedo.com/videotutorials/video?
name=96&fromSeriesID=96), which means I can now call python from
the windows DOS-style command prompt.

My formatting must be wrong when calling the nfold.py script to run.
My connad prompt call and the computer's response look like this:

C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
nfold.py
  File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
\nfold.py", line 13
print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
tree=treetype]'

^
SyntaxError: invalid syntax

What I dont understand is that some of the parameters in the syntax it
printed back are in <> brackets, and others in [] brackets.

I assume this is something a regular python user could notice straight
away.

Please let me know, I'd be very grateful


I just tried

nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate --dir=C:
\Users\Adam\Desktop\cvdata

in the dos-style command prompt. It didn'g vive a syntax error this
time, it just repeated my command back to me in text. I assume I
called code correctly, but it didn't make a new folder full of data
like it should have.



Which version of jBoost, and which version of Python?

~Ethan~

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


Re: running an existing script

2011-06-22 Thread Adam Chapman
On Jun 22, 4:54 pm, Adam Chapman 
wrote:
> On Jun 21, 9:12 pm, Adam Chapman 
> wrote:
>
>
>
>
>
>
>
>
>
> > On Jun 21, 8:00 pm, Ethan Furman  wrote:
>
> > > Adam Chapman wrote:
> > > > Thanks Ethan
>
> > > > No way could I have worked that out in my state of stress!
>
> > > > For your second idea, would I need to type that into the python command
> > > > line interface (the one that looks like a DOS window?
>
> > > If you are actually in a python CLI, at the top of that screen does it
> > > say something like
>
> > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> > > (Intel)] on win32
> > > Type "help", "copyright", "credits" or "license" for more information.
>
> > > ?
>
> > > If yes, then what I wrote earlier should actually work (I downloaded
> > > jBoost and looked at the nfold.py script).  Here it is again:
>
> > > --> import os
> > > --> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
> > > --> import nfold
> > > --> import sys
> > > --> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
> > > ... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
> > > ... "--generate" ]
> > > ...
> > > --> nfold.main()
>
> > > I fixed the sys.argv line from last time.
>
> > > Good luck!
>
> > > ~Ethan~
>
> > Thanks to both of you for your help.
>
> > It's getting late here, I'll give it another try tomorrow
>
> I've added the python directories to the environment variable "path"
> in my computer (http://showmedo.com/videotutorials/video?
> name=96&fromSeriesID=96), which means I can now call python from
> the windows DOS-style command prompt.
>
> My formatting must be wrong when calling the nfold.py script to run.
> My connad prompt call and the computer's response look like this:
>
> C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
> nfold.py
>   File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
> \nfold.py", line 13
>     print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
> generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
> tree=treetype]'
>
> ^
> SyntaxError: invalid syntax
>
> What I dont understand is that some of the parameters in the syntax it
> printed back are in <> brackets, and others in [] brackets.
>
> I assume this is something a regular python user could notice straight
> away.
>
> Please let me know, I'd be very grateful

I just tried

nfold.py --booster=Adaboost --folds=5 --data=spambase.data --
spec=spambase.spec --rounds=500 --tree=ADD_ALL --generate --dir=C:
\Users\Adam\Desktop\cvdata

in the dos-style command prompt. It didn'g vive a syntax error this
time, it just repeated my command back to me in text. I assume I
called code correctly, but it didn't make a new folder full of data
like it should have.


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


Re: running an existing script

2011-06-22 Thread Ethan Furman

Adam Chapman wrote:

On Jun 21, 9:12 pm, Adam Chapman 
wrote:

On Jun 21, 8:00 pm, Ethan Furman  wrote:










Adam Chapman wrote:

Thanks Ethan
No way could I have worked that out in my state of stress!
For your second idea, would I need to type that into the python command
line interface (the one that looks like a DOS window?

If you are actually in a python CLI, at the top of that screen does it
say something like
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
?
If yes, then what I wrote earlier should actually work (I downloaded
jBoost and looked at the nfold.py script).  Here it is again:
--> import os
--> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
--> import nfold
--> import sys
--> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
... "--generate" ]
...
--> nfold.main()
I fixed the sys.argv line from last time.
Good luck!
~Ethan~

Thanks to both of you for your help.

It's getting late here, I'll give it another try tomorrow


I've added the python directories to the environment variable "path"
in my computer (http://showmedo.com/videotutorials/video?
name=96&fromSeriesID=96), which means I can now call python from
the windows DOS-style command prompt.

My formatting must be wrong when calling the nfold.py script to run.
My connad prompt call and the computer's response look like this:

C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
nfold.py
  File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
\nfold.py", line 13
print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
tree=treetype]'
 
^

SyntaxError: invalid syntax


Looks like you are using Python 3, but nfold is Python 2.  You're being 
tripped up by one of the non-compatible changes -- namely, print is now 
a function and so requires ().




What I dont understand is that some of the parameters in the syntax it
printed back are in <> brackets, and others in [] brackets.


Looking at nfold.py it seems that rounds and tree are optional, one of 
generate or dir is required, if dir is not given then data and spec must 
be, and booster and folds are required -- so I'm not really sure why 
they chose the mixture of <> and [].



Also, on the version of jBoost I downloaded there is at least one error 
on nfolds.py on line 134 -- it should be indented one more level.


Hope this helps.

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


Re: running an existing script

2011-06-22 Thread Chris Rebert
On Wed, Jun 22, 2011 at 8:54 AM, Adam Chapman
 wrote:

> I've added the python directories to the environment variable "path"
> in my computer (http://showmedo.com/videotutorials/video?
> name=96&fromSeriesID=96), which means I can now call python from
> the windows DOS-style command prompt.
>
> My formatting must be wrong when calling the nfold.py script to run.

No, it's a syntax error in the script itself, at least under the
version of Python you're using.

> My connad prompt call and the computer's response look like this:
>
> C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
> nfold.py
>  File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
> \nfold.py", line 13
>    print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
> generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
> tree=treetype]'
>
> ^
> SyntaxError: invalid syntax

You're probably running Python 3.x, which changed `print` from a
keyword to just a regular function; hence, `print foo` is illegal, and
one must write `print(foo)` instead.
Based on this, I'd say that nfold.py was written for Python 2.x rather
than Python 3.x; so you'll either need to port it to Python 3.x, or
install Python 2.x and run it under that.

Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what happens inside?

2011-06-22 Thread Tim Rowe
On 22 June 2011 16:53, Andrew Berg  wrote:
> On 2011.06.22 10:45 AM, Chetan Harjani wrote:
>> why tuples are immutable whereas list are mutable?
> Tuples are more efficient and more appropriate for a list of items that
> doesn't need to change.

And also it sometimes useful to be sure that something can't change.
In particular, efficient dictionary implementations need the keys to
be immutable, because it you change a key it /really/ fouls up the
look-up.

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


Re: what happens inside?

2011-06-22 Thread Noah Hall
On Wed, Jun 22, 2011 at 4:45 PM, Chetan Harjani
 wrote:
> why tuples are immutable whereas list are mutable?
Because an immutable data type was needed, and a mutable type was also needed ;)

> why when we do x=y where y is a list and then change a element in x, y
> changes too( but the same is not the case when we change the whole value in
> x ), whereas, in tuples when we change x, y is not affected and also we cant
> change each individual element in tuple. Someone please clarify.

That's because y points to an object. When you assign x = y, you say
"assign name x to object that's also pointed to by name y". When you
change the list using list methods, you're changing the actual object.
Since x and y both point to the same object, they both change.
However, if you then assign y = [1], name y no longer points to the
original object. x still remains pointing to the original object.

>>> a = [1,2] # assign name a to object [1,2]
>>> b = a # assign name b to object referred to by name a
>>> b
[1, 2]
>>> a = [3,4] # assign name a to object [3,4]
>>> b
[1, 2]
>>> a
[3, 4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: running an existing script

2011-06-22 Thread Adam Chapman
On Jun 21, 9:12 pm, Adam Chapman 
wrote:
> On Jun 21, 8:00 pm, Ethan Furman  wrote:
>
>
>
>
>
>
>
>
>
> > Adam Chapman wrote:
> > > Thanks Ethan
>
> > > No way could I have worked that out in my state of stress!
>
> > > For your second idea, would I need to type that into the python command
> > > line interface (the one that looks like a DOS window?
>
> > If you are actually in a python CLI, at the top of that screen does it
> > say something like
>
> > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> > (Intel)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
>
> > ?
>
> > If yes, then what I wrote earlier should actually work (I downloaded
> > jBoost and looked at the nfold.py script).  Here it is again:
>
> > --> import os
> > --> os.chdir('path/to/nfold.py') # don't include nfold.py  ;)
> > --> import nfold
> > --> import sys
> > --> sys.argv = ["nfold.py", "--folds=5", "--data=spambase.data",
> > ... "--spec=spambase.spec", "--rounds=500", "--tree=ADD_ALL",
> > ... "--generate" ]
> > ...
> > --> nfold.main()
>
> > I fixed the sys.argv line from last time.
>
> > Good luck!
>
> > ~Ethan~
>
> Thanks to both of you for your help.
>
> It's getting late here, I'll give it another try tomorrow

I've added the python directories to the environment variable "path"
in my computer (http://showmedo.com/videotutorials/video?
name=96&fromSeriesID=96), which means I can now call python from
the windows DOS-style command prompt.

My formatting must be wrong when calling the nfold.py script to run.
My connad prompt call and the computer's response look like this:

C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts>nfold.py
nfold.py
  File "C:\Users\Adam\Desktop\JBOOST\jboost-2.2\jboost-2.2\scripts
\nfold.py", line 13
print 'Usage: nfold.py <--booster=boosttype> <--folds=number> [--
generate | --dir=dir] [--data=file --spec=file] [--rounds=number --
tree=treetype]'
 
^
SyntaxError: invalid syntax


What I dont understand is that some of the parameters in the syntax it
printed back are in <> brackets, and others in [] brackets.

I assume this is something a regular python user could notice straight
away.

Please let me know, I'd be very grateful
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Ian Kelly
On Wed, Jun 22, 2011 at 6:01 AM, Anny Mous  wrote:
> def sieve():
>    """Yield prime integers efficiently.
>
>    This uses the Sieve of Eratosthenes, modified to generate the primes
>    lazily rather than the traditional version which operates on a fixed
>    size array of integers.
>    """
>    # This implementation based on a version by Melissa E. O'Neill,
>    # as reported by Gerald Britton:
>    # http://mail.python.org/pipermail/python-list/2009-January/1188529.html
>    innersieve = sieve()
>    prevsq = 1
>    table  = {}
>    i = 2
>    while True:
>        if i in table:
>            prime = table[i]
>            del table[i]
>            nxt = i + prime
>            while nxt in table:
>                nxt += prime
>            table[nxt] = prime
>        else:
>            yield i
>            if i > prevsq:
>                j = next(innersieve)
>                prevsq = j**2
>                table[prevsq] = j
>        i += 1

This appears to have a small bug in it, but perhaps it doesn't matter.
 At the "yield i" statement, it is possible that i > prevsq.  It could
be that i == next(innersieve)**2, in which case i is yielded as prime
despite being composite.  This would then cause additional errors
further along.

The only way this could happen would be if there were two consecutive
primes such that all the numbers between their squares are composite,
thereby failing to add the next prime to the table until after its
square has been reached.  This seems a rather unlikely scenario, but I
can't say for certain that it never happens.

The Haskell version does not contain this flaw, as far as I can tell.

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what happens inside?

2011-06-22 Thread Andrew Berg
On 2011.06.22 10:45 AM, Chetan Harjani wrote:
> why tuples are immutable whereas list are mutable?
Tuples are more efficient and more appropriate for a list of items that
doesn't need to change.
> why when we do x=y where y is a list and then change a element in x, y
> changes too( but the same is not the case when we change the whole
> value in x ), whereas, in tuples when we change x, y is not affected
> and also we cant change each individual element in tuple. Someone
> please clarify.
With x = y, x and y point to the same place in memory. Because lists are
mutable, that place in memory can change, so both x and y will point to
the same changed spot in memory. Because tuples are immutable, that
place in memory cannot change, so Python must make a new spot in memory
for the changed object. This is true for all immutable objects.
-- 
http://mail.python.org/mailman/listinfo/python-list


what happens inside?

2011-06-22 Thread Chetan Harjani
why tuples are immutable whereas list are mutable?
why when we do x=y where y is a list and then change a element in x, y
changes too( but the same is not the case when we change the whole value in
x ), whereas, in tuples when we change x, y is not affected and also we cant
change each individual element in tuple. Someone please clarify.

-- 
echo "Impossible" | cat > /dev/null
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread MRAB

On 22/06/2011 06:58, Chris Torek wrote:

Now that the exercise has been solved...

Instead of "really short code to solve the problem", how about
some "really long code"? :-)

I was curious about implementing prime factorization as a generator,
using a prime-number generator to come up with the factors, and
doing memoization of the generated primes to produce a program that
does what "factor" does, e.g.:

 $ factor 9
 9: 3 3 2071723 5363222357

The python code is rather too slow for this particular number (I
gave up on it finding 5363222357) but works quite well on 600851475143,
or even, e.g., 12186606004219:

 $ python factor.py 600851475143 12186606004219
 600851475143: 71 839 1471 6857
 12186606004219: 2071723 5882353


[snip]
This code isn't particularly efficient, but it's fast enough:

import math

n = 9
limit = math.sqrt(n)
test = 2
factors = []
while test <= limit:
if n % test == 0:
factors.append(test)
n //= test
limit = math.sqrt(n)
else:
test += 1

factors.append(n)

print(factors)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Regular Expressions

2011-06-22 Thread Peter Otten
Andy Barnes wrote:

> Hi,
> 
> I am hoping someone here can help me with a problem I'd like to
> resolve with Python. I have used it before for some other projects but
> have never needed to use Regular Expressions before. It's quite
> possible I am following completley the wrong tack for this task (so
> any advice appreciated).
> 
> I have a source file in csv format that follows certain rules. I
> basically want to parse the source file and spit out a second file
> built from some rules and the content of the first file.
> 
> Source File Format:
> 
> Name, Type, Create, Study, Read, Teach, Prerequisite
> # column headers
> 
> Distil Mana, Lore, n/a, 70, 38, 21
> Theurgic Lore, Lore, n/a, 105, 70, 30, Distil Mana
> Talismantic Lore, Lore, n/a, 150, 100, 50
> Advanced Talismantic Lore, Lore, n/a, 100, 60, 30, Talismantic Lore,
> Theurgic Lore
> 
> The input file I have has over 700 unique entries. I have tried to
> cover the four main exceptions above. Before I detail them - this is
> what I would like the above input file, to be output as (dot
> diagramming language incase anyone recognises it):
> 
> Name, Type, Create, Study, Read, Teach, Prerequisite
> # column headers
> 
> DistilMana [label="{ Distil Mana |{Type|7}|{70|38|21}}"];
> TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];
> DistilMana -> TheurgicLore;
> TalismanticLore [label="{ Talismantic Lore |{Lore|n/a}|{150|100|
> 50}}"];
> AdvanvedTalismanticLore [label="{ Advanced Talismantic Lore |{Lore|n/
> a}|{100|60|30}}"];
> TalismanticLore -> AdvanvedTalismanticLore;
> TheurgicLore -> AdvanvedTalismanticLore;
> 
> It's quite a complicated find and replace operation that can be broken
> down into some easy stages. The main thing the sample above showed was
> that some of the entries won't list any prerequisits - these only need
> the descriptor entry creating. Some of them have more than one
> prerequisite. A line is needed for each prerequisite listed, linking
> it to it's parent.
> 
> You can also see that the 'name' needs to have spaces removed and it's
> repeated a few times in the process. I Hope it's easy to see what I am
> trying to achieve from the above. I'd be very happy to accept
> assistance in automating the conversion of my ever expanding csv file,
> into the dot format described above.

Forget about regexes. If there's any complexity it's in writing the output 
rather than reading the input file. You can tackle that by putting your data 
into a dictionary and using a format string:

import sys

def camelized(s):
return "".join(s.split())

template = """%(camel)s [label="{ %(name)s |{%(type)s|%(create)s}|
{%(study)s|%(read)s|%(teach)s}}"];"""

def process(instream, outstream):
instream = (line for line in instream if not (line.isspace() or 
line.startswith("#")))
rows = (map(str.strip, line.split(",")) for line in instream)
headers = map(str.lower, next(rows))
for row in rows:
rowdict = dict(zip(headers, row))
camel = rowdict["camel"] = camelized(rowdict["name"])
print template % rowdict
for for_lack_of_better_name in row[len(headers)-1:]:
print "%s -> %s;" % (camelized(for_lack_of_better_name), camel)

if __name__ == "__main__":
from StringIO import StringIO
instream = StringIO("""\
Name, Type, Create, Study, Read, Teach, Prerequisite
# column headers

Distil Mana, Lore, n/a, 70, 38, 21
Theurgic Lore, Lore, n/a, 105, 70, 30, Distil Mana
Talismantic Lore, Lore, n/a, 150, 100, 50
Advanced Talismantic Lore, Lore, n/a, 100, 60, 30, Talismantic Lore, 
Theurgic Lore
""")
process(instream, sys.stdout)



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


Re: Python Regular Expressions

2011-06-22 Thread Neil Cerutti
On 2011-06-22, Andy Barnes  wrote:
> to expand. I have parsed one of the lines manually to try and break
> the process I'm trying to automate down.
>
> source:
> Theurgic Lore, Lore, n/a, 105, 70, 30, Distil Mana
>
> output:
> TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];
> DistilMana -> TheurgicLore;
>
> This is the steps I would take to do this conversion manually:

It seems to me that parsing the file into an intermediate model
and then using that model to serialize your output would be
easier to understand and more robust than modifying the csv
entries in place. It decouples deciphering the meaning of the
data from emitting the data, which is more robust and expansable.

The amount of ingenuity required is less, though. ;)

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


Re: Python Regular Expressions

2011-06-22 Thread Andy Barnes
to expand. I have parsed one of the lines manually to try and break
the process I'm trying to automate down.

source:
Theurgic Lore, Lore, n/a, 105, 70, 30, Distil Mana

output:
TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];
DistilMana -> TheurgicLore;

This is the steps I would take to do this conversion manually:

1) Take everything prior to the first comma and remove all the spaces,
insert it into a newline:

TheurgicLore

2) append the following string ' [label="{ '

TheurgicLore [label="{

3) append everything prior to the first comma (this time we don't need
to remove the spaces)

TheurgicLore [label="{ Theurgic Lore

4) append the following string ' |{'

TheurgicLore [label="{ Theurgic Lore |{

5) append everything between the 1st and 2nd comma of the source file
followed by a '|'

TheurgicLore [label="{ Theurgic Lore |{Lore|

6) append everything between the 2nd and 3rd comma of the source file
followed by a '}|{'

TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{

7) append everything between the 3rd and 4th comma of the source file
followed by a '|'

TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|

8) append everything between the 4th and 5th comma of the source file
followed by a '|'

TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|

9) append everything between the 5th and 6th comma of the source file
followed by a '}}"];'

TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];

Those 9 steps spit out my fist line of output file as above
"TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];" I
now have to parse the dependancies onto a newline.

# this next process needs to be repeated for each prerequisite, so if
there are two pre-requisites it would need to keep parsing for more
comma's.
1a) take everything between the 6th and 7th comma and put it at the
start of a new line (remove spaces)

DistilMana

2a) append '-> '

DistilMana ->

3a) append everything prior to the first comma, with spaces removed

DistilMana -> TheurgicLore

This should now be all the steps to spit out:

TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];
DistilMana -> TheurgicLore;
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode codepoints

2011-06-22 Thread Saul Spatz
Thanks very much.  This is the elegant kind of solution I was looking for.  I 
had hoped there was a way to do it without even addressing the matter of 
surrogates, but apparently not.  The reason I don't like this is that it 
depends on knowing that python internally stores strings in UTF-16.  I expected 
that there would be some built-in iterator that would return the code points.  
(Actually, this all started when I realized that s[k] wouldn't necessarily give 
me the kth character of the string s.)


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


Python Regular Expressions

2011-06-22 Thread Andy Barnes
Hi,

I am hoping someone here can help me with a problem I'd like to
resolve with Python. I have used it before for some other projects but
have never needed to use Regular Expressions before. It's quite
possible I am following completley the wrong tack for this task (so
any advice appreciated).

I have a source file in csv format that follows certain rules. I
basically want to parse the source file and spit out a second file
built from some rules and the content of the first file.

Source File Format:

Name, Type, Create, Study, Read, Teach, Prerequisite
# column headers

Distil Mana, Lore, n/a, 70, 38, 21
Theurgic Lore, Lore, n/a, 105, 70, 30, Distil Mana
Talismantic Lore, Lore, n/a, 150, 100, 50
Advanced Talismantic Lore, Lore, n/a, 100, 60, 30, Talismantic Lore,
Theurgic Lore

The input file I have has over 700 unique entries. I have tried to
cover the four main exceptions above. Before I detail them - this is
what I would like the above input file, to be output as (dot
diagramming language incase anyone recognises it):

Name, Type, Create, Study, Read, Teach, Prerequisite
# column headers

DistilMana [label="{ Distil Mana |{Type|7}|{70|38|21}}"];
TheurgicLore [label="{ Theurgic Lore |{Lore|n/a}|{105|70|30}}"];
DistilMana -> TheurgicLore;
TalismanticLore [label="{ Talismantic Lore |{Lore|n/a}|{150|100|
50}}"];
AdvanvedTalismanticLore [label="{ Advanced Talismantic Lore |{Lore|n/
a}|{100|60|30}}"];
TalismanticLore -> AdvanvedTalismanticLore;
TheurgicLore -> AdvanvedTalismanticLore;

It's quite a complicated find and replace operation that can be broken
down into some easy stages. The main thing the sample above showed was
that some of the entries won't list any prerequisits - these only need
the descriptor entry creating. Some of them have more than one
prerequisite. A line is needed for each prerequisite listed, linking
it to it's parent.

You can also see that the 'name' needs to have spaces removed and it's
repeated a few times in the process. I Hope it's easy to see what I am
trying to achieve from the above. I'd be very happy to accept
assistance in automating the conversion of my ever expanding csv file,
into the dot format described above.

Andy

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


Re: User Authentication

2011-06-22 Thread Adam Tauno Williams
On Wed, 2011-06-22 at 06:34 -0700, Anurag wrote:
> Hi All,
> 
> I am working on application which needs to do a authentication against
> LDAP, if LDAP not installed then local system account (administrator
> user in windows and root user in Linux). This should work on both
> Windows and Linux.

See python-ldap

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


Re: User Authentication

2011-06-22 Thread Tim Golden

On 22/06/2011 14:34, Anurag wrote:

Hi All,

I am working on application which needs to do a authentication against
LDAP, if LDAP not installed then local system account (administrator
user in windows and root user in Linux). This should work on both
Windows and Linux.

Which library I should use for that.


python-ldap seems the obvious choice:


  http://www.python-ldap.org/

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


Re: Unicode codepoints

2011-06-22 Thread Saul Spatz
Thanks.  I agree with you about the generator.  Using your first suggestion, 
code points above U+ get separated into two "surrogate pair" characters 
fron UTF-16.  So instead of U=10 I get U+DBFF and U+DFFF.
 
-- 
http://mail.python.org/mailman/listinfo/python-list


User Authentication

2011-06-22 Thread Anurag
Hi All,

I am working on application which needs to do a authentication against
LDAP, if LDAP not installed then local system account (administrator
user in windows and root user in Linux). This should work on both
Windows and Linux.

Which library I should use for that.

Regards,
Anurag
-- 
http://mail.python.org/mailman/listinfo/python-list


free computer ebooks updated 5 ebooks every day or more

2011-06-22 Thread basio basio
Denodev eBook - Blog PDF eBook. Free download eBook. Newest eBooks
updated everyday. All eBooks are completely free. Come and stay here.
subscribe for newsletters. subscribe for rss. www.denodev.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Chris Angelico
On Wed, Jun 22, 2011 at 10:01 PM, Anny Mous  wrote:
>            prime = table[i]
>            del table[i]
>

I don't fully understand your algorithm, but I think these two lines
can be rewritten as:
prime=table.pop(i)

Interesting algo. A recursive generator, not sure I've seen one of those before.

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


Re: Tkinter/scrollbar/canvas question

2011-06-22 Thread agb
Saul Spatz wrote:


> 
> You need to do the update_idletasks to force the canvas to be mapped
> before you figure out the bounding box.  Until the canvas is mapped to the
> screen, the bounding box is (0,0,1,1) so there no scrolling possible. 
> (You can call update_ideltasks through any widget.)

Many thanks--this fixed the issue.

> That said, I wonder why you're putting widgets in the frame instead of
> putting objects directly on the canvas.  The way you're doing it you can't
> use tags, which are what really give the canvas its power.

The power of canvas is not really what I'm after. What I would like is a
list of checkboxes (and, in a few other frames, several buttons); if a
scrollable frame were available in tkinter, I'd use that. While canvas is
powerful, its power is not needed for my purposes.

> 
> Saul

--
http://gall.mine.nu
free to get comics,free chat-1,dc++ (dcplusplus),
mute webcache,group update program,torrent,atomic time server,tool to find your 
ip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Anny Mous
Chris Torek wrote:

> Now that the exercise has been solved...
> 
> Instead of "really short code to solve the problem", how about
> some "really long code"? :-)
> 
> I was curious about implementing prime factorization as a generator,
> using a prime-number generator to come up with the factors, and
> doing memoization of the generated primes to produce a program that
> does what "factor" does, e.g.:
> 
> $ factor 9
> 9: 3 3 2071723 5363222357
> 
> The python code is rather too slow for this particular number (I
> gave up on it finding 5363222357) 

It shouldn't take more than a few seconds to factorise 10**17-1 even in pure
Python. On my not-very-powerful PC, using a home-brew pure-Python function
(code to follow), I get all four factors in under four seconds.

In comparison, I ran your script for over five minutes before giving up, it
still hadn't found the fourth factor.

Don't be disappointed though, you're in illustrious company. Getting the
Sieve of Eratosthenes *badly* wrong is one of the most common mistakes,
second only to getting involved in land wars in Asia. See this paper by
Melissa O'Neill:

http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf

What people often describe as the Sieve of Eratosthenes is frequently the
Sieve of Euler, which while mathematically elegant, is computationally
crap. As O'Neill calls it, the Unfaithful Sieve.

In my home-brew primes module, I have this to say about three common
algorithms, including the classic Unfaithful Sieve, Turner's algorithm:


# === Prime number generators to avoid ===

# The following three prime generators are supplied for educational
# purposes, specifically as a terrible warning on how NOT to do it.
#
# Their performance starts at bad in the case of trial_division(), falls to
# terrible with turner(), and ends up with the utterly awful
# naive_division(). None of these three have acceptable performance; they
# are barely tolerable even for the first 100 primes. Their only advantage
# is that they are somewhat easy to understand.


Here's my factors() function. Adapting it to be a generator is left as an
exercise.


def factors(n):
"""factors(integer) -> [list of factors]

>>> factors(231)
[3, 7, 11]

Returns the (mostly) prime factors of integer n. For negative integers,
-1 is included as a factor. If n is 0 or 1, [n] is returned as the only
factor. Otherwise all the factors will be prime.
"""
if n != int(n):
raise ValueError('argument must be an integer')
elif n in (0, 1, -1):
return [n]
elif n < 0:
return [-1] + factors(-n)
assert n >= 2
result = []
for p in sieve():
if p*p > n: break
while n % p == 0:
result.append(p)
n //= p
if n != 1:
result.append(n)
return result


def sieve():
"""Yield prime integers efficiently.

This uses the Sieve of Eratosthenes, modified to generate the primes
lazily rather than the traditional version which operates on a fixed
size array of integers.
"""
# This implementation based on a version by Melissa E. O'Neill,
# as reported by Gerald Britton:
# http://mail.python.org/pipermail/python-list/2009-January/1188529.html
innersieve = sieve()
prevsq = 1
table  = {}
i = 2
while True:
if i in table:
prime = table[i]
del table[i]
nxt = i + prime
while nxt in table:
nxt += prime
table[nxt] = prime
else:
yield i
if i > prevsq:
j = next(innersieve)
prevsq = j**2
table[prevsq] = j
i += 1


I don't claim that my version of the sieve above will break any
computational records, but it performs quite well for my needs.


-- 
Steven

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


Re: Emails backup in python 3.2

2011-06-22 Thread TheSaint
Michael Hrivnak wrote:

> Do you have a special reason for wanting to implement
> your own email storage?

Learning python :)

It seems very easy to get my mails with the poplib help.
Usually I work with Kmail which imports mbox files.
I'm not prone to set up a SMTP server on my PC.

-- 
goto /dev/null
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using django ORM from web browser and from command line apps

2011-06-22 Thread bruno.desthuilli...@gmail.com
On Jun 22, 2:21 am, News123  wrote:
> Out of curiousity: Do you know whether the imports would be executed for
> each potential command as soon as I call manage.py or only
> 'on demand'?

Why would you care ? Just importing the module shouldn't have any side
effect.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode codepoints

2011-06-22 Thread jmfauth
That seems to me correct.

>>> '\\u{:04x}'.format(ord(u'é'))
\u00e9
>>> '\\U{:08x}'.format(ord(u'é'))
\U00e9
>>>

because

>>> u'\U00e9'
  File "", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 0-5: end of string in escape sequence
>>> u'\U00e9'
é
>>> u'\u00e9'
é
>>>

from this:

>>> u'éléphant\N{EURO SIGN}'
éléphant€
>>> u = u'éléphant\N{EURO SIGN}'
>>> ''.join(['\\u{:04x}'.format(ord(c)) for c in u])
\u00e9\u006c\u00e9\u0070\u0068\u0061\u006e\u0074\u20ac
>>>

Skipping surrogate pairs is a little bit a non sense,
because the purpose is to display code points!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.2 for Windows reports version as 2.7.0?

2011-06-22 Thread Gabriel Genellina

En Sun, 19 Jun 2011 12:35:38 -0300,  escribió:

The version info comes from the DLL - I wonder if the DLL being found  
is somehow old?


Make sure:

>>> import sys
>>> win32api.GetModuleFileName(sys.dllhandle)

Is the DLL you expect.


After uninstalling and reinstalling for the current user only (vs. all
users), Python now reports the correct version number.

And running your code above confirms that the proper DLL is being loaded
(c:\Python27\Python27.dll).

My original version of Python 2.7.0 was installed for all users and when
I ran the 2.7.2 MSI I chose to install for all users as well.

After running the 2.7.2 MSI, my Python exe's had the correct timestamps,
but I failed to check the python27.dll timestamp to see if this file was
out-of-date.

I wonder if changing my install method to current user forced the
installation of the updated python27.dll? And perhaps the default 2.7.2
installation in all users mode (when one is updating an existing 2.7
installation) doesn't update the Python27.dll under some conditions?


In a "for all users" install, python27.dll goes into c:\windows\system32,  
not c:\python27


Maybe you installed 2.7.0 twice, "for all users" and also "for current  
user only", and both in the same directory (c:\python27). That could  
explain the old .dll in the install directory; the new one goes into  
system32, but the old one takes precedence.


--
Gabriel Genellina

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


Re: Unicode codepoints

2011-06-22 Thread Peter Otten
Saul Spatz wrote:

> Hi,
> 
> I'm just starting to learn a bit about Unicode. I want to be able to read
> a utf-8 encoded file, and print out the codepoints it encodes.  After many
> false starts, here's a script that seems to work, but it strikes me as
> awfully awkward and unpythonic.  Have you a better way?
> 
> def codePoints(s):
> ''' return a list of the Unicode codepoints in the string s '''
> answer = []
> skip = False
> for k, c in enumerate(s):
> if skip:
> skip = False
> answer.append(ord(s[k-1:k+1]))
> continue
> if not 0xd800 <= ord(c) <= 0xdfff:
> answer.append(ord(c))
> else:
> skip = True
> return answer
> 
> if __name__ == '__main__':
> s = open('test.txt', encoding = 'utf8', errors = 'replace').read()
> code = codePoints(s)
> for c in code:
> print('U+'+hex(c)[2:])
> 
> Thanks for any help you can give me.
> 
> Saul

Here's an alternative implementation that follows Chris' suggestion to use a 
generator:

def codepoints(s):
s = iter(s)
for c in s:
if 0xd800 <= ord(c) <= 0xdfff:
c += next(s, "")
yield ord(c)

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


coverage.py: Highlight hot spots in source code

2011-06-22 Thread Thomas Guettler
Hi,

I just used coverage.py for the first time, and like it very much.

Is it possible to display how many times a line was executed?

I want to see lines which are executed very often red and
lines which are executed not often green.

I know there are other tools like hotshot, but AFAIK they
don't have a nice HTML output for every python source file.

  Thomas


-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode codepoints

2011-06-22 Thread Vlastimil Brom
2011/6/22 Saul Spatz :
> Hi,
>
> I'm just starting to learn a bit about Unicode. I want to be able to read a 
> utf-8 encoded file, and print out the codepoints it encodes.  After many 
> false starts, here's a script that seems to work, but it strikes me as 
> awfully awkward and unpythonic.  Have you a better way?
>
> def codePoints(s):
>    ''' return a list of the Unicode codepoints in the string s '''
>    answer = []
>    skip = False
>    for k, c in enumerate(s):
>        if skip:
>            skip = False
>            answer.append(ord(s[k-1:k+1]))
>            continue
>        if not 0xd800 <= ord(c) <= 0xdfff:
>            answer.append(ord(c))
>        else:
>            skip = True
>    return answer
>
> if __name__ == '__main__':
>    s = open('test.txt', encoding = 'utf8', errors = 'replace').read()
>    code = codePoints(s)
>    for c in code:
>        print('U+'+hex(c)[2:])
>
> Thanks for any help you can give me.
>
> Saul
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi,
what functionality should codePoints(...) add over just iterating
through the characters in the unicode string directly (besides
filtering out the surrogates)?

It seems, that you can just use

s = open(r'C:\install\filter-utf-8.txt', encoding = 'utf8', errors
= 'replace').read()
for c in s:
print('U+'+hex(ord(c))[2:])

or eventually add the condition before the print:
if not 0xd800 <= ord(c) <= 0xdfff:

you can also use string formatting to do the hex conversion and a more
usual zero padding; the print(...) calls would be:

"older style formatting"
print("U+%04x"%(ord(c),))

or the newer, potentially more powerful way using format(...)
print("U+{:04x}".format(ord(c)))

hth,
   vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Don't understand SequenceMatcher from difflib

2011-06-22 Thread Antoon Pardon
On Tue, Jun 21, 2011 at 03:02:57PM -0400, Terry Reedy wrote:
> On 6/21/2011 9:43 AM, Antoon Pardon wrote:
> 
> >   matcher = SequenceMatcher(ls1, ls2)
> ...
> >What am I doing wrong?
> 
> Read the doc, in particular, the really stupid signature of the class:
> 
> "class difflib.SequenceMatcher(isjunk=None, a='', b='', autojunk=True)"
> You are passing isjunk = ls1, a = ls2, and by default, b=''. So
> there are no matches, len(a) = 36, len(b) = 0, and the dummy match
> is (36,0,0) as you got.

Yes my penny dropped an hour after I send the question.

But reading the doc in itself didn't help. I head read and reread it
a number of times, before I finaly posted the question.

Somehow this signature reminded me of the range function where if you
only provide one argument, this is considered to be the second parameter
with the first parameter taking on a default value. So somehow I assumed
that if you only provided two parameters, these would be shifted as in
range and the first parameter would default to None.

I know if you read the documentation carefully, it contradicts this, but
my assumption blinded me for seeing it.

> There are also several example in the doc, all like
> >>> s = SequenceMatcher(None, " abcd", "abcd abcd") # or
> >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
> 
> So you will get better results with
> matcher = SequenceMatcher(None, ls1, ls2) # or
> matcher = SequenceMatcher(a=ls1, b=ls2)
> 
> In the future, please try to simply examples before posting for help.

Yes my bad here. I should have prepared the question better. Frustration
got the better of me.

Thanks for responding anyway.

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


Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Ian Kelly
On Tue, Jun 21, 2011 at 11:58 PM, Chris Torek  wrote:
> I was curious about implementing prime factorization as a generator,
> using a prime-number generator to come up with the factors, and
> doing memoization of the generated primes to produce a program that
> does what "factor" does, e.g.:

This is a generator-based sieve I wrote a while back to solve the
PRIME1 problem at SPOJ.  The problem is to generate all the prime
numbers within specified ranges, where the numbers are great enough
that a full sieve would run out of memory, and the ranges are wide
enough that a O(sqrt(n)) test on each number would simply take too
long:

https://www.spoj.pl/problems/PRIME1/

The script is not terribly impressive from a technical standpoint, but
what tickles me about it is its bootstrappiness; the set that the
"primes" generator checks to determine whether each number is prime is
actually built from the output of the generator, which itself contains
no actual primality-testing logic.  Hope you like it:

8<
import math

def primes(m, n):
# Yield all the primes in the range [m, n), using the nonprimes set
# as a reference.  Except for 2, only odd integers are considered.
if m <= 2:
yield 2
m = 3
elif m % 2 == 0:
m += 1  # Force m to be odd.
for p in xrange(m, n, 2):
if p not in nonprimes:
yield p

# Read all the bounds to figure out what we need to store.
bounds = [map(int, raw_input().split(' ')) for t in xrange(input())]
limit = max(n for (m, n) in bounds)
sqrt_limit = int(math.sqrt(limit))

# Mark odd multiples of primes as not prime.  Even multiples
# do not need to be marked since primes() won't try them.
nonprimes = set()
for p in primes(3, sqrt_limit+1):
# Mark odd nonprimes within the base range.  p*3 is the first
# odd multiple of p; p+p is the increment to get to the next
# odd multiple.
nonprimes.update(xrange(p*3, sqrt_limit+1, p+p))

# Mark odd nonprimes within each of the requested ranges.
for (m, n) in bounds:
# Align m to the first odd multiple of p in the range
# (or the last odd multiple before the range).
m -= (m % (p + p) - p)
m = max(m, p*3)
nonprimes.update(xrange(m, n+1, p+p))

# Generate and write the primes over each input range.
first = True
for (m, n) in bounds:
if not first:
print
first = False
for p in primes(m, n+1):
print p
8<
-- 
http://mail.python.org/mailman/listinfo/python-list