Re: regex question

2007-04-28 Thread proctor
On Apr 27, 8:26 am, Michael Hoffman <[EMAIL PROTECTED]> wrote:
> proctorwrote:
> > On Apr 27, 1:33 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> >> On Apr 27, 1:33 am,proctor<[EMAIL PROTECTED]> wrote:
> >>> rx_test = re.compile('/x([^x])*x/')
> >>> s = '/xabcx/'
> >>> if rx_test.findall(s):
> >>> print rx_test.findall(s)
> >>> 
> >>> i expect the output to be ['abc'] however it gives me only the last
> >>> single character in the group: ['c']
>
> >> As Josiah already pointed out, the * needs to be inside the grouping
> >> parens.
> > so my question remains, why doesn't the star quantifier seem to grab
> > all the data.
>
> Because you didn't use it *inside* the group, as has been said twice.
> Let's take a simpler example:
>
>  >>> import re
>  >>> text = "xabc"
>  >>> re_test1 = re.compile("x([^x])*")
>  >>> re_test2 = re.compile("x([^x]*)")
>  >>> re_test1.match(text).groups()
> ('c',)
>  >>> re_test2.match(text).groups()
> ('abc',)
>
> There are three places that match ([^x]) in text. But each time you find
> one you overwrite the previous example.
>
> > isn't findall() intended to return all matches?
>
> It returns all matches of the WHOLE pattern, /x([^x])*x/. Since you used
> a grouping parenthesis in there, it only returns one group from each
> pattern.
>
> Back to my example:
>
>  >>> re_test1.findall("xabcxaaaxabc")
> ['c', 'a', 'c']
>
> Here it finds multiple matches, but only because the x occurs multiple
> times as well. In your example there is only one match.
>
> > i would expect either 'abc' or 'a', 'b', 'c' or at least just
> > 'a' (because that would be the first match).
>
> You are essentially doing this:
>
> group1 = "a"
> group1 = "b"
> group1 = "c"
>
> After those three statements, you wouldn't expect group1 to be "abc" or
> "a". You'd expect it to be "c".
> --
> Michael Hoffman

thank you all again for helping to clarify this for me.  of course you
were exactly right, and the problem lay not with python or the text,
but with me.  i mistakenly understood the text to be attempting to
capture the C style comment, when in fact it was merely matching it.

apologies.

sincerely,
proctor

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


Re: regex question

2007-04-27 Thread proctor
On Apr 27, 8:50 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> On Apr 27, 9:10 am, proctor <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Apr 27, 1:33 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
>
> > > On Apr 27, 1:33 am, proctor <[EMAIL PROTECTED]> wrote:
>
> > > > hello,
>
> > > > i have a regex:  rx_test = re.compile('/x([^x])*x/')
>
> > > > which is part of this test program:
>
> > > > 
>
> > > > import re
>
> > > > rx_test = re.compile('/x([^x])*x/')
>
> > > > s = '/xabcx/'
>
> > > > if rx_test.findall(s):
> > > > print rx_test.findall(s)
>
> > > > 
>
> > > > i expect the output to be ['abc'] however it gives me only the last
> > > > single character in the group: ['c']
>
> > > > C:\test>python retest.py
> > > > ['c']
>
> > > > can anyone point out why this is occurring?  i can capture the entire
> > > > group by doing this:
>
> > > > rx_test = re.compile('/x([^x]+)*x/')
> > > > but why isn't the 'star' grabbing the whole group?  and why isn't each
> > > > letter 'a', 'b', and 'c' present, either individually, or as a group
> > > > (group is expected)?
>
> > > > any clarification is appreciated!
>
> > > > sincerely,
> > > > proctor
>
> > > As Josiah already pointed out, the * needs to be inside the grouping
> > > parens.
>
> > > Since re's do lookahead/backtracking, you can also write:
>
> > > rx_test = re.compile('/x(.*?)x/')
>
> > > The '?' is there to make sure the .* repetition stops at the first
> > > occurrence of x/.
>
> > > -- Paul
>
> > i am working through an example from the oreilly book mastering
> > regular expressions (2nd edition) by jeffrey friedl.  my post was a
> > snippet from a regex to match C comments.   every 'x' in the regex
> > represents a 'star' in actual usage, so that backslash escaping is not
> > needed in the example (on page 275).  it looks like this:
>
> > ===
>
> > /x([^x]|x+[^/x])*x+/
>
> > it is supposed to match '/x', the opening delimiter, then
>
> > (
> > either anything that is 'not x',
>
> > or,
>
> > 'x' one or more times, 'not followed by a slash or an x'
> > ) any number of times (the 'star')
>
> > followed finally by the closing delimiter.
>
> > ===
>
> > this does not seem to work in python the way i understand it should
> > from the book, and i simplified the example in my first post to
> > concentrate on just one part of the alternation that i felt was not
> > acting as expected.
>
> > so my question remains, why doesn't the star quantifier seem to grab
> > all the data.  isn't findall() intended to return all matches?  i
> > would expect either 'abc' or 'a', 'b', 'c' or at least just
> > 'a' (because that would be the first match).  why does it give only
> > one letter, and at that, the /last/ letter in the sequence??
>
> > thanks again for replying!
>
> > sincerely,
> > proctor- Hide quoted text -
>
> > - Show quoted text -
>
> Again, I'll repeat some earlier advice:  you need to move the '*'
> inside the parens - you are still leaving it outside.  Also, get in
> the habit of using raw literal notation (that is r"slkjdfljf" instead
> of "lsjdlfkjs") when defining re strings - you don't have backslash
> issues yet, but you will as soon as you start putting real '*'
> characters in your expression.
>
> However, when I test this,
>
> restr = r'/x(([^x]|x+[^/])*)x+/'
> re_ = re.compile(restr)
> print re_.findall("/xabxxcx/ /x123xxx/")
>
> findall now starts to give a tuple for each "comment",
>
> [('abxxc', 'xxc'), ('123xx', 'xx')]
>
> so you have gone beyond my limited re skill, and will need help from
> someone else.
>
> But I suggest you add some tests with multiple consecutive 'x'
> characters in the middle of your comment, and multiple consecutive 'x'
> characters before the trailing comment.  In fact, from my
> recollections of trying to implement this type of comment recognizer
> by hand a long time ago in a job far, far away, test with both even
> and odd numbers of 'x' characters.
>
> -- Paul

thanks paul,

the reason the regex now give tuples is that there are now 2 groups,
the inner and outer parens.  so group 1 matches with the star, and
group 2 matches without the star.

sincerely,
proctor

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


Re: regex question

2007-04-27 Thread proctor
On Apr 27, 8:37 am, Duncan Booth <[EMAIL PROTECTED]> wrote:
> proctor <[EMAIL PROTECTED]> wrote:
> > so my question remains, why doesn't the star quantifier seem to grab
> > all the data.  isn't findall() intended to return all matches?  i
> > would expect either 'abc' or 'a', 'b', 'c' or at least just
> > 'a' (because that would be the first match).  why does it give only
> > one letter, and at that, the /last/ letter in the sequence??
>
> findall returns the matched groups. You get one group for each
> parenthesised sub-expression, and (the important bit) if a single
> parenthesised expression matches more than once the group only contains
> the last string which matched it.
>
> Putting a star after a subexpression means that subexpression can match
> zero or more times, but each time it only matches a single character
> which is why your findall only returned the last character it matched.
>
> You need to move the * inside the parentheses used to define the group,
> then the group will match only once but will include everything that it
> matched.
>
> Consider:
>
> >>> re.findall('(.)', 'abc')
> ['a', 'b', 'c']
> >>> re.findall('(.)*', 'abc')
> ['c', '']
> >>> re.findall('(.*)', 'abc')
>
> ['abc', '']
>
> The first pattern finds a single character which findall manages to
> match 3 times.
>
> The second pattern finds a group with a single character zero or more
> times in the pattern, so the first time it matches each of a,b,c in turn
> and returns the c, and then next time around we get an empty string when
> group matched zero times.
>
> In the third pattern we are looking for a group with any number of
> characters in it. First time we get all of the string, then we get
> another empty match.

thank you this is interesting.  in the second example, where does the
'nothingness' match, at the end?  why does the regex 'run again' when
it has already matched everything?  and if it reports an empty match
along with a non-empty match, why only the two?

sincerely,
proctor

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


Re: regex question

2007-04-27 Thread proctor
On Apr 27, 8:26 am, Michael Hoffman <[EMAIL PROTECTED]> wrote:
> proctor wrote:
> > On Apr 27, 1:33 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> >> On Apr 27, 1:33 am, proctor <[EMAIL PROTECTED]> wrote:
> >>> rx_test = re.compile('/x([^x])*x/')
> >>> s = '/xabcx/'
> >>> if rx_test.findall(s):
> >>> print rx_test.findall(s)
> >>> 
> >>> i expect the output to be ['abc'] however it gives me only the last
> >>> single character in the group: ['c']
>
> >> As Josiah already pointed out, the * needs to be inside the grouping
> >> parens.
> > so my question remains, why doesn't the star quantifier seem to grab
> > all the data.
>
> Because you didn't use it *inside* the group, as has been said twice.
> Let's take a simpler example:
>
>  >>> import re
>  >>> text = "xabc"
>  >>> re_test1 = re.compile("x([^x])*")
>  >>> re_test2 = re.compile("x([^x]*)")
>  >>> re_test1.match(text).groups()
> ('c',)
>  >>> re_test2.match(text).groups()
> ('abc',)
>
> There are three places that match ([^x]) in text. But each time you find
> one you overwrite the previous example.
>
> > isn't findall() intended to return all matches?
>
> It returns all matches of the WHOLE pattern, /x([^x])*x/. Since you used
> a grouping parenthesis in there, it only returns one group from each
> pattern.
>
> Back to my example:
>
>  >>> re_test1.findall("xabcxaaaxabc")
> ['c', 'a', 'c']
>
> Here it finds multiple matches, but only because the x occurs multiple
> times as well. In your example there is only one match.
>
> > i would expect either 'abc' or 'a', 'b', 'c' or at least just
> > 'a' (because that would be the first match).
>
> You are essentially doing this:
>
> group1 = "a"
> group1 = "b"
> group1 = "c"
>
> After those three statements, you wouldn't expect group1 to be "abc" or
> "a". You'd expect it to be "c".
> --
> Michael Hoffman

ok, thanks michael.

so i am now assuming that either the book's example assumes perl, and
perl is different from python in this regard, or, that the book's
example is faulty.  i understand all the examples given since my
question, and i know what i need to do to make it work.  i am raising
the question because the book says one thing, but the example is not
working for me.  i am searching for the source of the discrepancy.

i will try to research the differences between perl's and python's
regex engines.

thanks again,

sincerely,
proctor

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


Re: regex question

2007-04-27 Thread proctor
On Apr 27, 1:33 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> On Apr 27, 1:33 am, proctor <[EMAIL PROTECTED]> wrote:
>
>
>
> > hello,
>
> > i have a regex:  rx_test = re.compile('/x([^x])*x/')
>
> > which is part of this test program:
>
> > 
>
> > import re
>
> > rx_test = re.compile('/x([^x])*x/')
>
> > s = '/xabcx/'
>
> > if rx_test.findall(s):
> > print rx_test.findall(s)
>
> > 
>
> > i expect the output to be ['abc'] however it gives me only the last
> > single character in the group: ['c']
>
> > C:\test>python retest.py
> > ['c']
>
> > can anyone point out why this is occurring?  i can capture the entire
> > group by doing this:
>
> > rx_test = re.compile('/x([^x]+)*x/')
> > but why isn't the 'star' grabbing the whole group?  and why isn't each
> > letter 'a', 'b', and 'c' present, either individually, or as a group
> > (group is expected)?
>
> > any clarification is appreciated!
>
> > sincerely,
> > proctor
>
> As Josiah already pointed out, the * needs to be inside the grouping
> parens.
>
> Since re's do lookahead/backtracking, you can also write:
>
> rx_test = re.compile('/x(.*?)x/')
>
> The '?' is there to make sure the .* repetition stops at the first
> occurrence of x/.
>
> -- Paul

i am working through an example from the oreilly book mastering
regular expressions (2nd edition) by jeffrey friedl.  my post was a
snippet from a regex to match C comments.   every 'x' in the regex
represents a 'star' in actual usage, so that backslash escaping is not
needed in the example (on page 275).  it looks like this:

===

/x([^x]|x+[^/x])*x+/

it is supposed to match '/x', the opening delimiter, then

(
either anything that is 'not x',

or,

'x' one or more times, 'not followed by a slash or an x'
) any number of times (the 'star')

followed finally by the closing delimiter.

===

this does not seem to work in python the way i understand it should
from the book, and i simplified the example in my first post to
concentrate on just one part of the alternation that i felt was not
acting as expected.

so my question remains, why doesn't the star quantifier seem to grab
all the data.  isn't findall() intended to return all matches?  i
would expect either 'abc' or 'a', 'b', 'c' or at least just
'a' (because that would be the first match).  why does it give only
one letter, and at that, the /last/ letter in the sequence??

thanks again for replying!

sincerely,
proctor

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


regex question

2007-04-26 Thread proctor
hello,

i have a regex:  rx_test = re.compile('/x([^x])*x/')

which is part of this test program:



import re

rx_test = re.compile('/x([^x])*x/')

s = '/xabcx/'

if rx_test.findall(s):
print rx_test.findall(s)



i expect the output to be ['abc'] however it gives me only the last
single character in the group: ['c']

C:\test>python retest.py
['c']

can anyone point out why this is occurring?  i can capture the entire
group by doing this:

rx_test = re.compile('/x([^x]+)*x/')
but why isn't the 'star' grabbing the whole group?  and why isn't each
letter 'a', 'b', and 'c' present, either individually, or as a group
(group is expected)?

any clarification is appreciated!

sincerely,
proctor

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


Re: recursion depth problem

2007-04-23 Thread proctor
On Apr 22, 5:51 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> Oops!  Note to self: *ALWAYS* try code before posting to a public
> forum :-(
>
> def binary(val, width):
> print '%10s = the sum of' % val
> for i in [2 ** x for x in range(width - 1, -1, -1)]:
> a = val / i
> print ' ' * 13 + '%s * (2 ** %s)' % (a, width)
> val -= i * a
> width -= 1
>
> binary(233, 8)

hi michael,

just a quick clarification...

it seems to me that the self-documenting part of the code should be
more like this:

print ' ' * 13 + '%s * (2 ** %s)' % (a, width-1)

instead of

print ' ' * 13 + '%s * (2 ** %s)' % (a, width)

is this correct, or am i mixed?

sincerely,
proctor

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 8:23 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
> Steven Bethard <[EMAIL PROTECTED]> wrote:
>
>...
>
>
>
> > >>>>> import sys
> > >>>>> def ch4(item, n=0):
> > >>>>>if n < len(item):
> > >>>>>if item[n] == '0':
> > >>>>>item[n] = '1'
> > >>>>>print ''.join(item)
> > >>>>>ch4(item)
> > >>>>>elif item[n] == '1':
> > >>>>>item[n] = '0'
> > >>>>>ch4(item, n+1)
> > >>>>> ch4(list(sys.argv[1]))
>...
> > > for interest sake:  is my method unredeemable?
>
> > Let's just say that I don't currently see an obvious way of redeeming
> > it. ;-)
>
> Change the outer if into a while, and the recursive calls into proper
> assignments to n.  They're both tail-recursive calls, so this won't
> change the semantics, as it happens.
>
> Alex

really helpful!  thank you very much!

proctor.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 9:28 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On 22 Apr 2007 19:13:31 -0700, proctor <[EMAIL PROTECTED]> declaimed the
> following in comp.lang.python:
>
>
>
> > :-)
>
> > this is good stuff.  for learning especially!  thank you again!
>
> Took me some time to find... My 30year old BugBooks* are in storage,
> along with all the other raw comp-sci texts.
>
> What you see above is a Python implementation of a 2 1-bit input,
> 1-bit sum and 1-bit carry output, full-adder from basic AND/OR/XOR gates
> -- you can build the hardware equivalent (except for the variable
> length) using old-fashioned 74xx chips (are 74xx TTL chips still made,
> or will one need to use the 74Cxx CMOS variants ) [heck; is the
> multi-function ALU chip still made?]
>
> *   The Virginia Tech shootings were just a blip on my radar until I
> read that this was Blacksburg -- as I once had most of the Blacksburg
> BugBooks
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED] [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

well i'm going to make it a project to understand properly this
program.

proctor.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 7:34 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> proctor wrote:
> > On Apr 22, 2:06 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> >> proctor wrote:
> >>> On Apr 22, 1:24 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> >>>> On Apr 22, 2007, at 1:49 PM, proctor wrote:
> >>>>> i have a small function which mimics binary counting.  it runs fine as
> >>>>> long as the input is not too long, but if i give it input longer than
> >>>>> 8 characters it gives
> >>>>> RuntimeError: maximum recursion depth exceeded in cmp
> >>>>> i'm not too sure what i am doing improperly.  is there really a lot of
> >>>>> recursion in this code?
> >>>>> ==
> >>>>> import sys
> >>>>> def ch4(item, n=0):
> >>>>>if n < len(item):
> >>>>>if item[n] == '0':
> >>>>>item[n] = '1'
> >>>>>print ''.join(item)
> >>>>>ch4(item)
> >>>>>elif item[n] == '1':
> >>>>>item[n] = '0'
> >>>>>ch4(item, n+1)
> >>>>> ch4(list(sys.argv[1]))
> >>>>> ==
> >>>> Yes.  There really is *that* much recursion in that code.  502 levels
> >>>> with input length of 8 characters, 1013 with 9 characters, 2035 with
> >>>> 10 characters...  There's a pattern there ;-)
> >>> ok, thanks michael!
> >>> is there a way to avoid it here?  how could i write this better, (if
> >>> at all)?
> >> Google for permutation-like recipies:
>
> >>  http://www.google.com/search?q=Python+permutations
>
> >> Use the code from the first hit::
>
> >>  >>> for x in xselections('01', 8):
> >>  ... print ''.join(x)
> >>  ...
> >>  
> >>  0001
> >>  0010
> >>  ...
> >>  1101
> >>  1110
> >>  
>
> >> Explaining to your teacher why your code uses generators when you
> >> haven't been taught them yet is left as an exercise to the reader. ;-)
>
> >> STeVe
>
> > this is really nice, thanks steve.  much slicker than my code.
>
> > for interest sake:  is my method unredeemable?
>
> Let's just say that I don't currently see an obvious way of redeeming
> it. ;-)
>
> STeVe

lol.  okay.

proctor.


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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 7:10 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On 22 Apr 2007 17:06:18 -0700, proctor <[EMAIL PROTECTED]> declaimed the
> following in comp.lang.python:
>
> > > else:
> > > # only one of carry in, b1, or b2 is set
>
> #or none is set! Missed the 0,0,0 condition 
>
> > > return (0, b1 + b2 + c)
>
> > thank you.  you guys are keeping me busy!
>
> Heh... I'm sure what I scratched together could be optimized more
> (make functions out of the input/output conversions; add some error
> checking on input data, allow for non-list arguments in adder()...)
>
> After all, if one wants a binary counter, one should implement a
> binary addition operation, and basic digital has ways... Unfortunately,
> Python now has a Boolean type, so boolean AND/OR doesn't give the
> desired results... And using bitwise seems wasteful  However...
> replace the badd() with the following obscure thing if you really want
> to get close to hardware emulation...
>
> def badd(b1, b2, ci=0):
> """
> badd(b1, b2, ci) => (co, sum)
> """
> (co1, hs1) = (b1 & b2, b1 ^ b2)
> (co2, hs2) = (ci & hs1, ci ^ hs1)
> return (co1 | co2, hs2)
>
> A pair of "half-adders"; and the extra bit to make a "full-adder".
> It wouldn't be efficient to do it as one long return, just due to
> duplicated (b1 ^ b2) sub-terms
>
> return ( (b1 & b2) | (ci & (b1 ^ b2)), (ci ^ (b1 ^ b2)))
>
> but it does work 
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED] [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

:-)

this is good stuff.  for learning especially!  thank you again!

proctor.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 5:51 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Sun, 22 Apr 2007 17:37:05 -0500, Michael Bentley
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
>
> > Anything that can be done with recursion can be done without
> > recursion.  If you really wanted to mimic a binary counter, why not
> > write a function that converts a number to binary?
>
> > Then you could just count up from 0, printing the return from your
> > binary conversion function...
>
> > And the binary converter would be pretty easy too: just think of it
> > as making change -- for a given number you just divide the number by
> > each of [2 ** x for x in range(len(sys.argv[1]), 0, -1)] and append
> > the string representations of the answers to a list and if the answer
> > is 1, subtract that much from the number...
>
> Or a simple lookup dictionary to convert from hex to binary, and use
> string interpolation to produce the hex...
>
> Might also make it more natural -- the original recursive loop is
> producing output with LSB on the left, most folks expect the MSB on the
> left (where, in this case, B is "bit").
>
> Of course, if I were to be masochistic, I'd write a binary adder()
> composed of bit adders with carry... Such as {I hope this was just a
> personal learning experience and not a homework project}...
>
> -=-=-=-=-=-=-
> """
> masochistic exercise in generating a binary adder
> """
> import sys
>
> def badd(b1, b2, c=0):
> """
> badd(b1, b2, c) => (carry, sum)
> """
> if b1 and b2:
> # 1 + 1 + c => carry out, and 0/1 from carry in
> return (1, c)
> elif (b1 and c) or (b2 and c):
> # either b1 or b2 is set, and carry in is set
> return (1, 0)
> else:
> # only one of carry in, b1, or b2 is set
> return (0, b1 + b2 + c)
>
> def adder(w1, w2):
> """
> adder(w1, w2) => w3
> where w1, w2, w3 are lists of 0s and 1s
> """
> #equalize list lengths
> if len(w1) > len(w2):
> w2 = [0] * (len(w1) - len(w2)) + w2
> elif len(w1) < len(w2):
> w1 = [0] * (len(w2) - len(w1)) + w1
>
> w3 = [0] * len(w1)
>
> carry = 0   #initialize
> end = len(w3) - 1
> for i in range(end + 1):
> (carry, w3[end-i]) = badd(w1[end-i], w2[end-i], carry)
>
> if carry:
> return "OVERFLOW"   #should be an exception
>
> return w3
>
> if __name__ == "__main__":
> #sloppy conversion from character to binary
> binary = [ {True:1, False:0}[c == "1"] for c in sys.argv[1] ]
>
> #simple counter
>
> while binary != "OVERFLOW":
> print "".join(["%1s" % b for b in binary])
> binary = adder(binary, [ 1 ])   #need to supply a list
>
> print "Overflow must have occurred"
>
> binary1 = [ {True:1, False:0}[c == "1"] for c in sys.argv[2] ]
> binary2 = [ {True:1, False:0}[c == "1"] for c in sys.argv[3] ]
>
> print "".join(["%1s" % b for b in adder(binary1, binary2)])
>
> -=-=-=-=-=- (watch out for line wrap on the command line
> E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>python binadd.py
>  0010 111
>
> 
> 0001
> 0010
> 0011
> 0100
> 0101
> 0110
> 0111
> 1000
> 1001
> 1010
> 1011
> 1100
> 1101
> 1110
> 
> Overflow must have occurred
> 1001
>
> E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>
> E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>python binadd.py
> 010 001 0
>
> 010
> 011
> 100
> 101
> 110
> 111
> Overflow must have occurred
> 1
>
> E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>
> E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>python binadd.py
> 00 1 1
>
> 00
> 01
> 10
> 11
> Overflow must have occurred
> OVERFLOW
>
> E:\UserData\Dennis Lee Bieber\My Documents\Python Progs>
>
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED] [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/

thank you.  you guys are keeping me busy!

ps.  no, i'm not in school.  not in the traditional sense anyway :-)
just books, practice, and forums like this.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 5:51 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> Oops!  Note to self: *ALWAYS* try code before posting to a public
> forum :-(
>
> def binary(val, width):
> print '%10s = the sum of' % val
> for i in [2 ** x for x in range(width - 1, -1, -1)]:
> a = val / i
> print ' ' * 13 + '%s * (2 ** %s)' % (a, width)
> val -= i * a
> width -= 1
>
> binary(233, 8)

very cool.  thanks a lot michael.  very interesting.

proctor.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 5:05 pm, tac-tics <[EMAIL PROTECTED]> wrote:
> Yes, you should use a for loop in this situation.
>
> Certain functional languages, such as Scheme and various LISP dialects
> allow for what is called "tail recursion" which effectively eliminates
> this problem by internally converting recursion to iteration. Python
> isn't really cut out for heavy recursive work, and the "Pythonic" way
> of doing things is through the lovely for loop.

hi tac-tics,

by using a for loop, do you mean in the same sense that the
"xselections" function uses a for loop on the page:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
?

or are you referring to a different technique for this?

thanks for your help.  i really appreciate it.

proctor.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 4:37 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> On Apr 22, 2007, at 4:08 PM, proctor wrote:
>
>
>
> > On Apr 22, 2:55 pm, [EMAIL PROTECTED] wrote:
> >> On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote:
>
> >>> hello,
>
> >>> i have a small function which mimics binary counting.  it runs
> >>> fine as
> >>> long as the input is not too long, but if i give it input longer
> >>> than
> >>> 8 characters it gives
>
> >>> RuntimeError: maximum recursion depth exceeded in cmp
>
> >>> i'm not too sure what i am doing improperly.  is there really a
> >>> lot of
> >>> recursion in this code?
>
> >>> ==
>
> >>> import sys
>
> >>> def ch4(item, n=0):
> >>> if n < len(item):
> >>> if item[n] == '0':
> >>> item[n] = '1'
> >>> print ''.join(item)
> >>> ch4(item)
> >>> elif item[n] == '1':
> >>> item[n] = '0'
> >>> ch4(item, n+1)
>
> >>> ch4(list(sys.argv[1]))
>
> >>> ==
>
> >>> this function expects input in the form of a string of zeros, like
> >>> this:
>
> >>> python test-bin.py 
>
> >>> and is expected to output a list of permutations like this:
>
> >>> $ python test-bin.py 
> >>> 1000
> >>> 0100
> >>> 1100
> >>> 0010
> >>> 1010
> >>> 0110
> >>> 1110
> >>> 0001
> >>> 1001
> >>> 0101
> >>> 1101
> >>> 0011
> >>> 1011
> >>> 0111
> >>> 
>
> >>> thanks for all help!
>
> >>> sincerely,
> >>> proctor
>
> >> If you just want to make it work as ischeck
>
> >> sys.setrecursionlimit()
>
> >> ~Sean
>
> > very nice.  thanks sean.  so is the structure of my original code
> > unrescuable?  i cannot rearrange it to bypass the recursion?
>
> Anything that can be done with recursion can be done without
> recursion.  If you really wanted to mimic a binary counter, why not
> write a function that converts a number to binary?
>
> Then you could just count up from 0, printing the return from your
> binary conversion function...
>
> And the binary converter would be pretty easy too: just think of it
> as making change -- for a given number you just divide the number by
> each of [2 ** x for x in range(len(sys.argv[1]), 0, -1)] and append
> the string representations of the answers to a list and if the answer
> is 1, subtract that much from the number...

cool...  i'm going to have to think about this one... :-)

proctor

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 2:06 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> proctor wrote:
> > On Apr 22, 1:24 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> >> On Apr 22, 2007, at 1:49 PM, proctor wrote:
>
> >>> i have a small function which mimics binary counting.  it runs fine as
> >>> long as the input is not too long, but if i give it input longer than
> >>> 8 characters it gives
> >>> RuntimeError: maximum recursion depth exceeded in cmp
> >>> i'm not too sure what i am doing improperly.  is there really a lot of
> >>> recursion in this code?
> >>> ==
> >>> import sys
> >>> def ch4(item, n=0):
> >>>if n < len(item):
> >>>if item[n] == '0':
> >>>item[n] = '1'
> >>>print ''.join(item)
> >>>ch4(item)
> >>>elif item[n] == '1':
> >>>item[n] = '0'
> >>>ch4(item, n+1)
> >>> ch4(list(sys.argv[1]))
> >>> ==
> >> Yes.  There really is *that* much recursion in that code.  502 levels
> >> with input length of 8 characters, 1013 with 9 characters, 2035 with
> >> 10 characters...  There's a pattern there ;-)
>
> > ok, thanks michael!
>
> > is there a way to avoid it here?  how could i write this better, (if
> > at all)?
>
> Google for permutation-like recipies:
>
>  http://www.google.com/search?q=Python+permutations
>
> Use the code from the first hit::
>
>  >>> for x in xselections('01', 8):
>  ... print ''.join(x)
>  ...
>  
>  0001
>  0010
>  ...
>  1101
>  1110
>  
>
> Explaining to your teacher why your code uses generators when you
> haven't been taught them yet is left as an exercise to the reader. ;-)
>
> STeVe

this is really nice, thanks steve.  much slicker than my code.

for interest sake:  is my method unredeemable?

thanks very much!

sincerely,
proctor

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 2:55 pm, [EMAIL PROTECTED] wrote:
> On Apr 22, 11:49 am, proctor <[EMAIL PROTECTED]> wrote:
>
>
>
> > hello,
>
> > i have a small function which mimics binary counting.  it runs fine as
> > long as the input is not too long, but if i give it input longer than
> > 8 characters it gives
>
> > RuntimeError: maximum recursion depth exceeded in cmp
>
> > i'm not too sure what i am doing improperly.  is there really a lot of
> > recursion in this code?
>
> > ==
>
> > import sys
>
> > def ch4(item, n=0):
> > if n < len(item):
> > if item[n] == '0':
> > item[n] = '1'
> > print ''.join(item)
> > ch4(item)
> > elif item[n] == '1':
> > item[n] = '0'
> > ch4(item, n+1)
>
> > ch4(list(sys.argv[1]))
>
> > ==
>
> > this function expects input in the form of a string of zeros, like
> > this:
>
> > python test-bin.py 
>
> > and is expected to output a list of permutations like this:
>
> > $ python test-bin.py 
> > 1000
> > 0100
> > 1100
> > 0010
> > 1010
> > 0110
> > 1110
> > 0001
> > 1001
> > 0101
> > 1101
> > 0011
> > 1011
> > 0111
> > 
>
> > thanks for all help!
>
> > sincerely,
> > proctor
>
> If you just want to make it work as ischeck
>
> sys.setrecursionlimit()
>
> ~Sean

very nice.  thanks sean.  so is the structure of my original code
unrescuable?  i cannot rearrange it to bypass the recursion?

proctor.

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


Re: recursion depth problem

2007-04-22 Thread proctor
On Apr 22, 1:24 pm, Michael Bentley <[EMAIL PROTECTED]> wrote:
> On Apr 22, 2007, at 1:49 PM, proctor wrote:
>
>
>
> > i have a small function which mimics binary counting.  it runs fine as
> > long as the input is not too long, but if i give it input longer than
> > 8 characters it gives
>
> > RuntimeError: maximum recursion depth exceeded in cmp
>
> > i'm not too sure what i am doing improperly.  is there really a lot of
> > recursion in this code?
>
> > ==
>
> > import sys
>
> > def ch4(item, n=0):
> >if n < len(item):
> >if item[n] == '0':
> >item[n] = '1'
> >print ''.join(item)
> >ch4(item)
> >elif item[n] == '1':
> >item[n] = '0'
> >ch4(item, n+1)
>
> > ch4(list(sys.argv[1]))
>
> > ==
>
> Yes.  There really is *that* much recursion in that code.  502 levels
> with input length of 8 characters, 1013 with 9 characters, 2035 with
> 10 characters...  There's a pattern there ;-)

ok, thanks michael!

is there a way to avoid it here?  how could i write this better, (if
at all)?

proctor.

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


recursion depth problem

2007-04-22 Thread proctor
hello,

i have a small function which mimics binary counting.  it runs fine as
long as the input is not too long, but if i give it input longer than
8 characters it gives

RuntimeError: maximum recursion depth exceeded in cmp

i'm not too sure what i am doing improperly.  is there really a lot of
recursion in this code?

==

import sys

def ch4(item, n=0):
if n < len(item):
if item[n] == '0':
item[n] = '1'
print ''.join(item)
ch4(item)
elif item[n] == '1':
item[n] = '0'
ch4(item, n+1)

ch4(list(sys.argv[1]))

==

this function expects input in the form of a string of zeros, like
this:

python test-bin.py 

and is expected to output a list of permutations like this:

$ python test-bin.py 
1000
0100
1100
0010
1010
0110
1110
0001
1001
0101
1101
0011
1011
0111


thanks for all help!

sincerely,
proctor

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


Re: regex question

2007-01-08 Thread proctor

Paul McGuire wrote:
> "proctor" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> >
> >
> > it does work now...however, one more question:  when i type:
> >
> > rx_a = re.compile(r'a|b|c')
> > it works correctly!
> >
>
> Do you see the difference between:
>
> rx_a = re.compile(r'a|b|c')
>
> and
>
> rx_a = re.compile("r'a|b|c'")
>
> There is no difference in the variable datatype between "string" and "raw
> string".  Raw strings are just a notational helper when creating string
> literals that have lots of backslashes in them (as happens a lot with
> regexps).
>
> r'a|b|c'  is the same as 'a|b|c'
> r'\d' is the same as '\\d'
>
> There is no reason to "add raw strings" to your makeRE method, since you
> don't have a single backslash anywhere.  And even if there were a backslash
> in the 'w' argument, it is just a string - no need to treat it differently.
> 
> -- Paul

thanks paul.  this helps.

proctor.

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


Re: regex question

2007-01-08 Thread proctor

Mark Peters wrote:
> > is there any way i would be successful then, in using raw string inside
> > my makeRE() function?
>
> Why do you think you even need a raw string?
>
> Just build and return the string 'a|b|c' (NOTE: DON'T add the quotes to
> the string)

yes, i suppose you are right.  i can't think of a reason i would NEED a
raw string in this situation.

all very helpful!  thanks very much.

sincerely,
proctor.

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


Re: regex question

2007-01-08 Thread proctor

Steven D'Aprano wrote:
> On Sun, 07 Jan 2007 23:57:00 -0800, proctor wrote:
>
> > it does work now...however, one more question:  when i type:
> >
> > rx_a = re.compile(r'a|b|c')
> > it works correctly!
> >
> > shouldn't:
> > rx_a = re.compile(makeRE(test))
> > give the same result since makeRE(test)) returns the string "r'a|b|c'"
>
> Those two strings are NOT the same.
>
> >>> s1 = r'a|b|c'
> >>> s2 = "r'a|b|c'"
> >>> print s1, len(s1)
> a|b|c 5
> >>> print s2, len(s2)
> r'a|b|c' 8
>
> A string with a leading r *outside* the quotation marks is a raw-string.
> The r is not part of the string, but part of the delimiter.
>
> A string with a leading r *inside* the quotation marks is just a string
> with a leading r. It has no special meaning.
>
>
>
> --
> Steven D'Aprano

thanks steven,

is there any way i would be successful then, in using raw string inside
my makeRE() function?

proctor.

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


Re: regex question

2007-01-08 Thread proctor

Paul McGuire wrote:
> "proctor" <[EMAIL PROTECTED]> wrote in message
> news:<[EMAIL PROTECTED]>...
> > hello,
> >
> > i hope this is the correct place...
> >
> > i have an issue with some regex code i wonder if you have any insight:
> >
> > 
>
> There's nothing actually *wrong* wth your regex.  The problem is your
> misunderstanding of raw string notation.  In building up your regex, do not
> start the string with "r'" and end it with a "'".
>
> def makeRE(w):
> print w + " length = " + str(len(w))
> # reString = "r'" + w[:1]
> reString = w[:1]
> w = w[1:]
> if len(w) > 0:
> for c in (w):
> reString += "|" + c
> # reString += "'"
> print "reString = " + reString
> return reString
>
> Or even better:
>
> def makeRE(w):
> print w + " length = " + str(len(w))
> reString = "|".join(list(w))
> return reString
>
> Raw string notation is intended to be used when the string literal is in
> your Python code itself, for example, this is a typical use for raw strings:
>
> ipAddrRe = r'\d{1,3}(\.\d{1,3}){3}'
>
> If I didn't have raw string notation to use, I'd have to double up all the
> backslashes, as:
>
> ipAddrRe = '\\d{1,3}(\\.\\d{1,3}){3}'
>
> But no matter which way I create the string, it does not actually start with
> "r'" and end with "'", those are just notations for literals that are part
> of your Python source.
>
> Does this give you a better idea of what is happening?
>
> -- Paul

yes!  thanks so much.

it does work now...however, one more question:  when i type:

rx_a = re.compile(r'a|b|c')
it works correctly!

shouldn't:
rx_a = re.compile(makeRE(test))
give the same result since makeRE(test)) returns the string "r'a|b|c'"

are you saying that the "r'" and "'" are being interpreted differently
in the second case than in the first?  if so, how would i go about
using raw string notation in such a circumstance (perhaps if i need to
escape "\b" or the like)?  do i have to double up in this case?

proctor.

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


regex question

2007-01-07 Thread proctor
hello,

i hope this is the correct place...

i have an issue with some regex code i wonder if you have any insight:



import re, sys

def makeRE(w):
print w + " length = " + str(len(w))
reString = "r'" + w[:1]
w = w[1:]
if len(w) > 0:
for c in (w):
reString += "|" + c
reString += "'"
print "reString = " + reString
return reString

test = sys.argv[1]
stg = sys.argv[2]

while test:
print "test = ", test
print "stg = ", stg
rx_a = re.compile(makeRE(test))
i = rx_a.search(stg).start()
print "i = " + str(i)
id = test.find(stg[i])
test = test[:id] + test[id+1:]
print "test == ", test
stg = stg[:i] + stg[i+1:]
print



i get the following output:



test =  abc
stg =  defabc
abc length = 3
reString = r'a|b|c'
i = 4
test ==  ac

test =  ac
stg =  defac
ac length = 2
reString = r'a|c'
Traceback (most recent call last):
  File ".py", line 21, in ?
i = rx_a.search(stg).start()
AttributeError: 'NoneType' object has no attribute 'start'

=

i am fairly new to this, and can't see the reason for the error.  what
am i missing?

btw, i think there are simpler ways to go about this, but i am doing it
this way (regexs) for a bit of a challenge and learning experience.

thanks to all!

sincerely,
proctor

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