Re: [Tutor] constructing semi-arbitrary functions

2014-02-18 Thread spir

On 02/18/2014 12:02 AM, Oscar Benjamin wrote:

On 17 February 2014 22:15, "André Walker-Loud "
 wrote:

This particular case is easily solved:

def f_lambda(x,pars):
 return lambda x: poly(x,*pars)

You let the closure take care of pars and return a function that takes
exactly one argument x.


Hi Oscar,

This is the opposite of what I am trying to do.  In the example, x represents the data 
and pars represent the parameters I want to determine, so it is the pars which I need 
passed into the "func_code.co_varnames" part of f.

Maybe your suggestion gets me in that direction, but I don't see how.


No, you're right. I misunderstood this example.

Are you able to see/alter the source code of the 3rd party function?
As I said earlier my preferred solution would be to rewrite the
outermost part of that.

The core inner minimisation routine will (I'm guessing) be something
that really doesn't care about the names of these parameters and just
needs to know the dimensionality of the space it is exploring. If you
can access that routine directly then you can bypass the (IMO
unfortunate) interface that you're currently trying to contort your
problems into.


I think so. However it works, the minimising algo only needs values, not names 
(but it needs to know which meaning/role each value corresponds to, indeed). It 
is also infortunate that it looks for this information in the (conceptually 
private) metadata of the function itself, instead of having a dedicated input 
slot in its interface.


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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-18 Thread spir

On 02/17/2014 08:23 PM, "André Walker-Loud " wrote:

Hello python tutors,

I am utilizing a 3rd party numerical minimization routine.  This routine 
requires an input function, which takes as arguments, only the variables with 
which to solve for.  But I don’t want to define all possible input functions, 
in a giant switch, but rather, if I know I am fitting a polynomial, I would 
like to just pass a list of parameters and have the code know how to construct 
this function.

To construct for example, a chisq function, you must pass not only the 
variables to solve for, but also the data, uncertainties, and perhaps other 
arguments.  So it requires a little hacking to get it to work.  With the help 
of my friends and looking at similar code, I have come up with two ways that 
work under my simple test cases, and I have a few questions about them.

The 3rd party minimizer utilizes the .func_code.co_varnames and 
.func_code.co_argcount to determine the name and number of variables to 
minimize.  eg.


g = lambda x,c_0,c_1: c_0 + c_1 * x
g.func_code.co_varnames

('x', 'c_0', 'c_1’)

g.func_code.co_argcount

3

so what is needed is a function

def f(c_0,c_1):
…#construct chi_sq(c_0,c_1,x,y,…)


What prevents you to make a simple function factory (see example below) is that 
the 3rd party module needs to use func_code.co_varnames & func_code.co_argcount, 
right? If yes, it is indeed annoying... and I have no good solution.


# func factory example:
def poly (*coefs):
# coefs are here in reverse order for simplicity
# but we could iterate backward below
n = len(coefs)
def f (x):
y = 0
for i in range(n):
y += coefs[i] * x**i
return y
return f

# y = 3 + 2*x + 1*x^2
poly3 = poly(3,2,1)
print(poly3(1)) # 6
print(poly3(2)) # 11
print(poly3(3)) # 18

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


Re: [Tutor] for: how to skip items

2014-02-18 Thread Neil Cerutti
On 2014-02-17, Oscar Benjamin  wrote:
> On 17 February 2014 16:17, Gabriele Brambilla
> wrote:
>> Doesn't exist a way in Python to do like in C
>>
>> for i=0, i<100, i=i+10
>>
>> ? without creating a list of index?
>
> You haven't said which Python version you're using. In Python 2
> the range function returns a list but the xrange function
> returns an iterator. In Python 3 the range function returns an
> iterator.

In Python 3 it range returns a range object, which is a sequence
type.

>>> x = range(100)
>>> x
range(0, 100)

You can use slice notation on range objects.

>>> x[::10]
range(0, 100, 10)

So we've got *that* going for us.

-- 
Neil Cerutti

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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-18 Thread Oscar Benjamin
On 18 February 2014 00:51, "André Walker-Loud "
 wrote:
>>
>> BTW if you're trying to fit the coefficients of a polynomial then a
>> general purpose optimisation function is probably not what you want to
>> use. I would probably solve (in a least squares sense and after
>> suitable scaling) the Vandermonde matrix.
>>
>> (I can explain that more if desired.)
>
> That looks interesting (just reading the wikipedia entry on the Vandermonde 
> matrix).
> Is there a good algorithm for picking the values the polynomial is evaluated 
> at to construct the matrix?

If I understand correctly your data points are given and it is the
polynomial coefficients you are trying to solve for. In that case you
would use your data points. So if I have data points (x1, y1), ...
(x4, y4) and I want to fit a quadratic polynomial of the form y = f(x)
then I can choose to minimise the squared error in estimating yi from
xi and the coefficients. That is:

a * x1^2 + b * x1 + c = y1e
...
a * x4^2 + b * x4 + c = y4e

And you want to choose a, b and c to minimise the total squared error
E = |y1 - y1e|^2 + ... + |y4 - y4e|^2. We can write this as a matrix
equation that you want to solve in a least squares sense:

|x1^2   x1   1 | | y1 |
|x2^2   x2   1 |  | a |  | y2 |
|x3^2   x3   1 |  | b |   =  | y3 |
|x4^2   x4   1 |  | c |  | y4 |

Or in other words  M(x) k = y where M(x) is the Vandermonde matrix of
the x coordinates, y is a column vector of the y coordinates (in the
same order) and k is a column vector of the coefficients. You want the
least squares solution of this linear system of equations which is the
exact solution of:

M' M c = M' y

where M' means the transpose of M. I mentioned scaling as the
Vandermonde matrix can be badly scaled if the x values are either much
smaller than or much bigger than which leads to numerical instability.
If you rescale your x values to between zero and one before doing this
you may get a more accurate result.

> Is there a benefit to this method vs a standard linear least squares?

It's the same except that you're using an analytic solution rather
than a black box solver.

> Given the Vandermonde matrix, how do you determine the uncertainty in the 
> resulting parameters?

Assuming that you have confidence in the accuracy of your input data:
http://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#Parameter_confidence_limits

> I guess yes, I am interested to learn more.
>
> The most common problem I am solving is fitting a sum of real exponentials to 
> noisy data, with the model function
>
> C(t) = sum_n A_n e^{- E_n t}
>
> the quantities of most interest are E_n, followed by A_n so I solve this with 
> non-linear regression.
> To stabilize the fit, I usually do a linear-least squares for the A_n first, 
> solving as a function of the E_n, and then do a non-linear fit for the E_n.

That doesn't sound optimal to me. Maybe I've misunderstood but fitting
over one variable and then over another is not in any way guaranteed
to produce an optimal fit.

Let's say I want to choose x* and y* to minimise f(x, y). If I choose
y0 as a guess and then find x1 that minimises f(x, y0) and then I use
that x1 and find the y that minimises f(x1, y) then I have my guess x1
and y1. These are unlikely to be close to the true optimal choices x*
and y*. If you continue the algorithm choosing x2 to minimise f(x, y1)
and y2 to minimise f(x2, y) generating x2, y2, x3, y3 and so on then
it will eventually converge (if the problem is convex).

In the xy plane this algorithm would be zig-zagging horizontally and
vertically down a valley towards the optimal solution. You can see a
picture of this on page 414 of this (free online) book:
http://apps.nrbook.com/c/index.html

> Often, we construct multiple data sets which share values of E_n but have 
> different A_n, where A_n is promoted to a matrix, sometimes square, sometimes 
> not.  So I want a wrapper on my chisq which can take the input parameter 
> values and deduce the fit function, and pass the variable names to my current 
> minimizer - hence my original question, and why I don't want to write a 
> special case for each possible combination of n_max, and the shape of A_n.

Well it sounds like your approach so far is working for now but as I
say the real fix is to improve or bypass the interface you're using.
One limitation that you may at some point hit is that in Python you
can't have an unbounded number of formal parameters for a function:

$ python3 tmp2.py
  File "tmp2.py", line 1
def f(x0,  x1,  x2,  x3,  x4,  x5,  x6,  x7,  x8,  x9,  x10,  x11,
 x12,  x13,  x14,
 ^
SyntaxError: more than 255 arguments


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


[Tutor] Using for loops for combinations

2014-02-18 Thread Chinanu 'Chinex' Onyekachi

Find all possible combinations of a specific word (string) using any 
combination of upper case and lower case letters with a for loop (no 
itertools). 



An example:

string_input = hat

comboination = ['hat', 'Hat', 'HAt', 'HAT', 'hAT', 'haT', 'HaT', 'hAt']



What I've tried so far, I’m trying to do this using knowledge from loops, if 
and else, strings and lists.
gear = ['hat']

for i in range(len(gear[0])):
gearlist1 = list(gear[0])
gearlist2 = [c.upper() for c in gearlist1]
gearlist3 = [gearlist1[0].join(gearlist2)]


print 'list is: %r' % (gearlist3 ,),___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] OCR Library

2014-02-18 Thread Khalid Al-Ghamdi
Hi,

Do you have any recommendations regarding a good OCR library (preferably
Py3)?

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


[Tutor] cx_freeze windows 7 python 3.3

2014-02-18 Thread KEVIN ROBINSON
I am new to this site so please forgive me if I am forwarding this question to 
the wrong person.
 
I am trying to use cx_freeze on windows 7 with python 3.3.
 
I can create a build folder and a dist folder using the command line without 
any problems.
 
When I click on the created .exe file I receive the following error:-
 
"Cannot import traceback module.
Exception:cannot import name MAXREPEAT
Original Exception:cannot import name MAXREPEAT"
 
I have looked for a solution but cannot find one.
 
Can anyone please help.
 
Thanks Kevin___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Regular expression - I

2014-02-18 Thread Santosh Kumar
Hi All,

If you notice the below example, case I is working as expected.

Case I:
In [41]: string = "test"

In [42]: re.match('',string).group()
Out[42]: ''

But why is the raw string 'r' not working as expected ?

Case II:

In [43]: re.match(r'',string).group()
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 re.match(r'',string).group()

AttributeError: 'NoneType' object has no attribute 'group'

In [44]: re.match(r'',string)



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


Re: [Tutor] constructing semi-arbitrary functions

2014-02-18 Thread Peter Otten
Oscar Benjamin wrote:

> On 17 February 2014 20:13, Peter Otten <__pete...@web.de> wrote:
>> André Walker-Loud  wrote:
>>>
>>> The 3rd party minimizer utilizes the .func_code.co_varnames and
>>> .func_code.co_argcount to determine the name and number of variables to
>>> minimize.  eg.
>>>
>>> =
>>> METHOD 2: use strings, exec and globals to construct function
>>> def minimize(pars,x,y,dy):
>>> global g_x, g_y, g_dy
>>> g_x = x; g_y = y; g_dy = dy
>>> argnames = ['c_%i'%(i) for i in range(len(pars))]
>>> funcargs = ", ".join(argnames)
>>> funcdef = 'def chisq_mn(' + funcargs + '):\n'
>>> funcdef += 'global g_x, g_y, g_dy\n'
>>> funcdef += 'return chisq(['+funcargs+'],g_x,g_y,g_dy)\n' #chisq
>>> is defined in same file
>>> # evaluate string and build function
>>> print "funcdef=", funcdef
>>> exec funcdef in globals()
>>
>> I think you are looking for closures:
> 
> Closures would be a good fit for this if the third-party minimisation
> routine didn't assume that it always receives a function whose formal
> parameters are the variables to be minimised and have the names that
> are intended for display.

Thank you for reading the OP more thoroughly and for taking the time to 
explain to those who don't ;)

> This one would work (assuming that we want to minimise over x):
> 
>> def make_poly(coeff):
>> def poly(x):
>> return sum(c * x ** n for n, c in enumerate(coeff))
>> return poly
> 
> This one won't work:
> 
>> def minimize(x, y, dy):
>> def chisq_mn(*args):
>> return chisq(args, x, y, dy)
>> return chisq_mn
> 
 def minimize(x, y, dy):
> ... def chisq_mn(*args):
> ... return chisq(args, x, y, dy)
> ... return chisq_mn
> ...
 f = minimize(2, 4, 0.1)
 f.func_code.co_varnames
> ('args',)
 f.func_code.co_argcount
> 0
> 
> The OP wants to pass this function to a routine that looks like:
> 
> def minimise(func):
> num = func.func_code.co_argcount
> names = func.func_code.co_varnames
> # Actual minimisation code
> return _minimise(func, num, names)


I don't know if the OP may use it, but there seems to be a version of minuit 
that allows to override the function signature:

"""
class iminuit.Minuit
Minuit(fcn, throw_nan=False, pedantic=True, frontend=None, 
forced_parameters=None, print_level=1, errordef=None, **kwds)

Construct minuit object from given fcn
Arguments:

[...]

forced_parameters: tell Minuit not to do function signature detection and 
use this argument instead. (Default None (automagically detect signature)
"""

http://iminuit.github.io/iminuit/api.html


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


Re: [Tutor] Regular expression - I

2014-02-18 Thread Santosh Kumar
Steve,

i am trying to under r - raw string notation. Am i understanding it wrong.
Rather than using "\", it says we can use the "r" option.

http://docs.python.org/2/library/re.html

Check the first paragraph for the above link.

Thanks,
santosh



On Tue, Feb 18, 2014 at 11:33 PM, Steve Willoughby wrote:

> Because the regular expression  means “match an angle-bracket
> character, zero or more H characters, followed by a close angle-bracket
> character” and your string does not match that pattern.
>
> This is why it’s best to check that the match succeeded before going ahead
> to call group() on the result (since in this case there is no result).
>
>
> On 18-Feb-2014, at 09:52, Santosh Kumar  wrote:
>
> >
> > Hi All,
> >
> > If you notice the below example, case I is working as expected.
> >
> > Case I:
> > In [41]: string = "test"
> >
> > In [42]: re.match('',string).group()
> > Out[42]: ''
> >
> > But why is the raw string 'r' not working as expected ?
> >
> > Case II:
> >
> > In [43]: re.match(r'',string).group()
> >
> ---
> > AttributeErrorTraceback (most recent call
> last)
> >  in ()
> > > 1 re.match(r'',string).group()
> >
> > AttributeError: 'NoneType' object has no attribute 'group'
> >
> > In [44]: re.match(r'',string)
> >
> >
> >
> > Thanks,
> > santosh
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>
>


-- 
D. Santosh Kumar
RHCE | SCSA
+91-9703206361


Every task has a unpleasant side .. But you must focus on the end result
you are producing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular expression - I

2014-02-18 Thread Steve Willoughby
The problem is not the use of the raw string, but rather the regular expression 
inside it.

In regular expressions, the * means that whatever appears before it may be 
repeated zero or more times.  So if you say H* that means zero or more H’s in a 
row.  I think you mean an H followed by any number of other characters which 
would be H.*  (the . matches any single character, so .* means zero or more of 
any characters).

On the other hand, H\* means to match an H followed by a literal asterisk 
character.

Does that help clarify why one matched and the other doesn’t?

steve

On 18-Feb-2014, at 10:09, Santosh Kumar  wrote:

> Steve,
> 
> i am trying to under r - raw string notation. Am i understanding it wrong.
> Rather than using "\", it says we can use the "r" option.
> 
> http://docs.python.org/2/library/re.html
> 
> Check the first paragraph for the above link.
> 
> Thanks,
> santosh
> 
> 
> 
> On Tue, Feb 18, 2014 at 11:33 PM, Steve Willoughby  wrote:
> Because the regular expression  means “match an angle-bracket character, 
> zero or more H characters, followed by a close angle-bracket character” and 
> your string does not match that pattern.
> 
> This is why it’s best to check that the match succeeded before going ahead to 
> call group() on the result (since in this case there is no result).
> 
> 
> On 18-Feb-2014, at 09:52, Santosh Kumar  wrote:
> 
> >
> > Hi All,
> >
> > If you notice the below example, case I is working as expected.
> >
> > Case I:
> > In [41]: string = "test"
> >
> > In [42]: re.match('',string).group()
> > Out[42]: ''
> >
> > But why is the raw string 'r' not working as expected ?
> >
> > Case II:
> >
> > In [43]: re.match(r'',string).group()
> > ---
> > AttributeErrorTraceback (most recent call last)
> >  in ()
> > > 1 re.match(r'',string).group()
> >
> > AttributeError: 'NoneType' object has no attribute 'group'
> >
> > In [44]: re.match(r'',string)
> >
> >
> >
> > Thanks,
> > santosh
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> 
> -- 
> D. Santosh Kumar
> RHCE | SCSA 
> +91-9703206361
> 
> 
> Every task has a unpleasant side .. But you must focus on the end result you 
> are producing.
> 



signature.asc
Description: Message signed with OpenPGP using GPGMail
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular expression - I

2014-02-18 Thread Steve Willoughby
Because the regular expression  means “match an angle-bracket character, 
zero or more H characters, followed by a close angle-bracket character” and 
your string does not match that pattern.

This is why it’s best to check that the match succeeded before going ahead to 
call group() on the result (since in this case there is no result).


On 18-Feb-2014, at 09:52, Santosh Kumar  wrote:

> 
> Hi All,
> 
> If you notice the below example, case I is working as expected.
> 
> Case I:
> In [41]: string = "test"
> 
> In [42]: re.match('',string).group()
> Out[42]: ''
> 
> But why is the raw string 'r' not working as expected ?
> 
> Case II:
> 
> In [43]: re.match(r'',string).group()
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 re.match(r'',string).group()
> 
> AttributeError: 'NoneType' object has no attribute 'group'
> 
> In [44]: re.match(r'',string)
> 
> 
> 
> Thanks,
> santosh
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] OCR Library

2014-02-18 Thread Danny Yoo
On Tue, Feb 18, 2014 at 7:16 AM, Khalid Al-Ghamdi  wrote:
> Hi,
>
> Do you have any recommendations regarding a good OCR library (preferably
> Py3)?


Unsure.  This is somewhat of a specialized question, and probably not
beginner material.  You might want to check up with the general
discussion mailing list for this one.

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

Preliminary Google searches suggest that "pytesser" might be an
interesting library.  You probably want to talk with folks who have
direct experience with OCR though; Tutor might not be the best place
to find OCR experts.


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


Re: [Tutor] Using for loops for combinations

2014-02-18 Thread Danny Yoo
On Tue, Feb 18, 2014 at 8:37 AM, Chinanu 'Chinex' Onyekachi
 wrote:
> Find all possible combinations of a specific word (string) using any
> combination of upper case and lower case letters with a for loop (no
> itertools).
>
>
> An example:
>
> string_input = hat
>
> comboination = ['hat', 'Hat', 'HAt', 'HAT', 'hAT', 'haT', 'HaT', 'hAt']

[code cut]


This appears to be homework, so unfortunately the help that can be
provided will need to be limited; otherwise there's a risk of
violating your institution's Honor Code.

Can you talk more about what difficulty are you running into?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cx_freeze windows 7 python 3.3

2014-02-18 Thread Danny Yoo
On Tue, Feb 18, 2014 at 3:59 AM, KEVIN ROBINSON
 wrote:
> I am new to this site so please forgive me if I am forwarding this question
> to the wrong person.
>
> I am trying to use cx_freeze on windows 7 with python 3.3.


Unfortunately, this may not be the best mailing list to help with
cx_freeze issues.


You may want to check with the cx_freeze_users mailing list:

http://sourceforge.net/mailarchive/forum.php?forum_name=cx-freeze-users
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular expression - I

2014-02-18 Thread Zachary Ware
Hi Santosh,

On Tue, Feb 18, 2014 at 9:52 AM, Santosh Kumar  wrote:
>
> Hi All,
>
> If you notice the below example, case I is working as expected.
>
> Case I:
> In [41]: string = "test"
>
> In [42]: re.match('',string).group()
> Out[42]: ''
>
> But why is the raw string 'r' not working as expected ?
>
> Case II:
>
> In [43]: re.match(r'',string).group()
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 re.match(r'',string).group()
>
> AttributeError: 'NoneType' object has no attribute 'group'
>
> In [44]: re.match(r'',string)

It is working as expected, but you're not expecting the right thing
;).  Raw strings don't escape anything, they just prevent backslash
escapes from expanding.  Case I works because "\*" is not a special
character to Python (like "\n" or "\t"), so it leaves the backslash in
place:

   >>> ''
   ''

The equivalent raw string is exactly the same in this case:

   >>> r''
   ''

The raw string you provided doesn't have the backslash, and Python
will not add backslashes for you:

   >>> r''
   ''

The purpose of raw strings is to prevent Python from recognizing
backslash escapes.  For example:

   >>> path = 'C:\temp\new\dir' # Windows paths are notorious...
   >>> path   # it looks mostly ok... [1]
   'C:\temp\new\\dir'
   >>> print(path)  # until you try to use it
   C:  emp
   ew\dir
   >>> path = r'C:\temp\new\dir'  # now try a raw string
   >>> path   # Now it looks like it's stuffed full of backslashes [2]
   'C:\\temp\\new\\dir'
   >>> print(path)  # but it works properly!
   C:\temp\new\dir

[1] Count the backslashes in the repr of 'path'.  Notice that there is
only one before the 't' and the 'n', but two before the 'd'.  "\d" is
not a special character, so Python didn't do anything to it.  There
are two backslashes in the repr of "\d", because that's the only way
to distinguish a real backslash; the "\t" and "\n" are actually the
TAB and LINE FEED characters, as seen when printing 'path'.

[2] Because they are all real backslashes now, so they have to be
shown escaped ("\\") in the repr.

In your regex, since you're looking for, literally, "", you'll
need to backslash escape the "*" since it is a special character *in
regular expressions*.  To avoid having to keep track of what's special
to Python as well as regular expressions, you'll need to make sure the
backslash itself is escaped, to make sure the regex sees "\*", and the
easiest way to do that is a raw string:

   >>> re.match(r'', string).group()
   ''

I hope this makes some amount of sense; I've had to write it up
piecemeal and will never get it posted at all if I don't go ahead and
post :).  If you still have questions, I'm happy to try again.  You
may also want to have a look at the Regex HowTo in the Python docs:
http://docs.python.org/3/howto/regex.html

Hope this helps,

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


Re: [Tutor] Regular expression - I

2014-02-18 Thread Mark Lawrence

On 18/02/2014 18:03, Steve Willoughby wrote:

Because the regular expression  means “match an angle-bracket character, 
zero or more H characters, followed by a close angle-bracket character” and your 
string does not match that pattern.

This is why it’s best to check that the match succeeded before going ahead to 
call group() on the result (since in this case there is no result).


On 18-Feb-2014, at 09:52, Santosh Kumar  wrote:



Hi All,

If you notice the below example, case I is working as expected.

Case I:
In [41]: string = "test"

In [42]: re.match('',string).group()
Out[42]: ''

But why is the raw string 'r' not working as expected ?

Case II:

In [43]: re.match(r'',string).group()
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 re.match(r'',string).group()

AttributeError: 'NoneType' object has no attribute 'group'

In [44]: re.match(r'',string)



Thanks,
santosh



Please do not top post on this list.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: [Tutor] Regular expression - I

2014-02-18 Thread Zachary Ware
On Tue, Feb 18, 2014 at 11:39 AM, Zachary Ware
 wrote:

>>>> ''
>''
>
> The equivalent raw string is exactly the same in this case:
>
>>>> r''
>''

Oops, I mistyped both of these.  The repr should be '' in both cases.

Sorry for the confusion!

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


Re: [Tutor] Regular expression - I

2014-02-18 Thread S Tareq
does any one know how to use 2to3 program to convert 2.7 coding 3.X please i 
need help sorry 



On Tuesday, 18 February 2014, 19:50, Zachary Ware 
 wrote:
 
On Tue, Feb 18, 2014 at 11:39 AM, Zachary Ware
 wrote:

>    >>> ''
>    ''
>
> The equivalent raw string is exactly the same in this case:
>
>    >>> r''
>    ''

Oops, I mistyped both of these.  The repr should be '' in both cases.

Sorry for the confusion!

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


Re: [Tutor] Regular expression - I

2014-02-18 Thread Albert-Jan Roskam


_
> From: Steve Willoughby 
>To: Santosh Kumar  
>Cc: python mail list  
>Sent: Tuesday, February 18, 2014 7:03 PM
>Subject: Re: [Tutor] Regular expression - I
> 
>
>Because the regular expression  means “match an angle-bracket character, 
>zero or more H characters, followed by a close angle-bracket character” and 
>your string does not match that pattern.
>
>This is why it’s best to check that the match succeeded before going ahead to 
>call group() on the result (since in this case there is no result).
>
>
>On 18-Feb-2014, at 09:52, Santosh Kumar  wrote:


You also might want to consider making it a non-greedy match. The explanation 
http://docs.python.org/2/howto/regex.html covers an example almost identical to 
yours:

Greedy versus Non-Greedy
When repeating a regular expression, as in a*, the resulting action is to
consume as much of the pattern as possible.  This fact often bites you when
you’re trying to match a pair of balanced delimiters, such as the angle brackets
surrounding an HTML tag.  The naive pattern for matching a single HTML tag
doesn’t work because of the greedy nature of .*.
>>>
>>> s = 'Title' >>> len(s) 32 >>> print 
>>> re.match('<.*>', s).span() (0, 32) >>> print re.match('<.*>', s).group() 
>>> Title 
The RE matches the '<' in , and the .* consumes the rest of
the string.  There’s still more left in the RE, though, and the > can’t
match at the end of the string, so the regular expression engine has to
backtrack character by character until it finds a match for the >.   The
final match extends from the '<' in  to the '>' in , which isn’t 
what you want.
In this case, the solution is to use the non-greedy qualifiers *?, +?, ??, or 
{m,n}?, which match as little text as possible.  In the above
example, the '>' is tried immediately after the first '<' matches, and
when it fails, the engine advances a character at a time, retrying the '>' at 
every step.  This produces just the right result:
>>>
>>> print re.match('<.*?>', s).group()  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cx_freeze windows 7 python 3.3

2014-02-18 Thread S Tareq
does any one know how to use 2to3 program to convert 2.7 python coding 3.X 
please i need help sorry  thank you ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using for loops for combinations

2014-02-18 Thread S Tareq
does any one know how to use 2to3 program to convert 2.7 python coding 3.X 
please i need help sorry  thank you 




On Tuesday, 18 February 2014, 19:18, Danny Yoo  wrote:
 
On Tue, Feb 18, 2014 at 8:37 AM, Chinanu 'Chinex' Onyekachi
 wrote:
> Find all possible combinations of a specific word (string) using any
> combination of upper case and lower case letters with a for loop (no
> itertools).
>
>
> An example:
>
> string_input = hat
>
> comboination = ['hat', 'Hat', 'HAt', 'HAT', 'hAT', 'haT', 'HaT', 'hAt']

[code cut]


This appears to be homework, so unfortunately the help that can be
provided will need to be limited; otherwise there's a risk of
violating your institution's Honor Code.

Can you talk more about what difficulty are you running into?

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


Re: [Tutor] Regular expression - I

2014-02-18 Thread Emile van Sebille

On 2/18/2014 11:42 AM, Mark Lawrence wrote:

On 18/02/2014 18:03, Steve Willoughby wrote:

Because the regular expression  means “match an angle-bracket





Please do not top post on this list.


Appropriate trimming is also appreciated.

Emile




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


[Tutor] could please convert this code to 3.X by using 2to3

2014-02-18 Thread S Tareq
i am to convert coding to python 3.X. and some one told me to use 2to3 program 
but  i don't how to use 2to3 program i was reading and followed the rules in 
python website ( 
http://docs.python.org/3/library/2to3.html?highlight=2to3#module-lib2to3 ) but 
it says error and don't work. is there any other way to convert. the original 
coding is used 2.7 python version. can you please help to convert this in any 
way. **sorry for bad English**  this is the coding:  


def quiz():
    print ("welcome")
    print ("""This is a quiz which will help you to learn and revise the
different Biology keywords and their definitions.

The question will be  repeated continuously until you have correctly matched 
every keyword with its definition exactly twice.

Good luck!
""")
    choose = int(input("1) Start Quiz  2) Exit Program "))
    if choose == 1:
        quiz2()
    elif choose == 2:
        quit()
def quiz2():
    # Import statements
    import random
    import datetime
    #Arrays to store the definitions and keywords read from the file
    keywords=[];
    definition=[];
    correctAnswer=[];
    #Counter for the wrong Answer and counter of number of definition in
    wrongAnswer=0;
    counter=0;
    # Taking User input for accepting the file name to be read
    filename= raw_input("Enter the File Name with extension  ")
    #Reading the file from start to the end
    for line in open(filename,'r').readlines():
        if(counter%2==0):
            keywords.append(line);
            counter=counter+1;
            correctAnswer.append(0)
        else:
            definition.append(line);
            keys=[];
            keys=line.split(" ");
            counter=counter+1;
    # Running two while loops to make the pattern recursive
    while True:
    # Starting the time for quiz and also, creating variables to make sure that 
same sequences and answers are not repeated
        a = datetime.datetime.now().replace(microsecond=0)
        prevWord=0
        prevSeq=0
        # While loop to run the code till each answer is correctly answered
        while correctAnswer.count(2)!=(counter/2):
            #While loop to generate an different random number from one the 
generated previously
            while True:
                word=random.randint(0,(counter/2)-1)
                if(correctAnswer[word]!=2):
                    break;
                if(prevWord==word):
                    continue;
            # Displaying the new keyword each time.
            print "Please Select the number which is the correct definition of 
the word:" ,keywords[word]
            #Generating an new sequence each time different from previous one
            while True:
                sequences =random.randint(0,2)
                if(prevSeq==sequences):
                    continue;
                else:
                    break
            #Generating an new incorrect answer each time different from 
previous one
            while True:
                incorrectAnswer=random.randint(0,len(correctAnswer)-1)
                if(incorrectAnswer==word):
                    continue;
                else :
                    break
            #Generating an new incorrect answer  each time different from 
previous one
            while True:
                incorrectAnswerSecond=random.randint(0,len(correctAnswer)-1);
                if (incorrectAnswer==incorrectAnswerSecond):
                    continue
                if(incorrectAnswerSecond==word):
                    continue
                else:
                    break
            # Displaying the options to the user based on the sequence number 
generated
            if (sequences==0):
                print "1.",definition[word]
                print "2.",definition[incorrectAnswer]
                print "3.",definition[incorrectAnswerSecond]
            elif (sequences==1):
                print "1.",definition[incorrectAnswer]
                print "2.",definition[incorrectAnswerSecond]
                print "3.",definition[word]
            elif (sequences==2):
                print "1.",definition[incorrectAnswerSecond]
                print "2.",definition[word]
                print "3.",definition[incorrectAnswer]
            #Taking the answer from user
            answer = raw_input("Enter the Correct Number between 1 to 3 ")
            # Assign the seq and word to preseq and word
            prevSeq=sequences
            prevWord=word
            #Checking the answer if they are corret.
            if(0 == sequences):
                if(answer == "1"):
                    print "success"
                    correctAnswer[word]=correctAnswer[word]+1
                else:
                    print "Wrong Answer"
                    print "Correct Answer: " ,definition[word]
                    wrongAnswer=wrongAnswer+1;
            elif(1 == sequences):
                if(answer == "3"):
                    print "success"
                    correctAnswer[word]

Re: [Tutor] could please convert this code to 3.X by using 2to3

2014-02-18 Thread Zachary Ware
On Tue, Feb 18, 2014 at 12:18 PM, S Tareq  wrote:
> i am to convert coding to python 3.X. and some one told me to use 2to3
> program but  i don't how to use 2to3 program i was reading and followed the
> rules in python website (
> http://docs.python.org/3/library/2to3.html?highlight=2to3#module-lib2to3 )
> but it says error and don't work. is there any other way to convert. the
> original coding is used 2.7 python version. can you please help to convert
> this in any way. **sorry for bad English**  this is the coding:



A few general points to try to help you:

- Don't spam the same question in several different threads, and
especially don't privately email list participants with the same
question.  Doing so is likely to get you ignored by people who would
otherwise be quite happy to help you.

- Don't ask people on this list to do something for you.  We're here
to teach, not do.

- When you have an error, post the exact error, in context.  If it's a
shell (including Windows Command Prompt) error, give the command line
you tried.  If it's a Python traceback, give the whole traceback.
Don't try to retype the error, copy and paste it.  If you're on
Windows and can't copy from the Command Prompt, use "Mark" from the
right click menu, and press Enter when you have highlighted what you
want to copy.  Without knowing what the error is, "it says error and
don't work" could mean any number of things.

- Try reading this: http://docs.python.org/3/howto/pyporting.html#use-2to3

- Keep in mind that there are some things that the 2to3 tool can't
convert, and so you may have to do some editing before or after
conversion, or both.

- Post in plain text, not HTML.  Some responders here don't get the
message at all if it includes HTML, and it's annoying to several
others.  We try to be friendly around here, but sometimes the most
friendliness we (individually) can muster is to ignore posts that
annoy us.  Help us help you, and we'll be happy to teach you whatever
you need to know!

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


Re: [Tutor] could please convert this code to 3.X by using 2to3

2014-02-18 Thread S Tareq
sorry, is there any other way to convert the coding



On Tuesday, 18 February 2014, 20:41, Zachary Ware 
 wrote:
 
On Tue, Feb 18, 2014 at 12:18 PM, S Tareq  wrote:
> i am to convert coding to python 3.X. and some one told me to use 2to3
> program but  i don't how to use 2to3 program i was reading and followed the
> rules in python website (
> http://docs.python.org/3/library/2to3.html?highlight=2to3#module-lib2to3 )
> but it says error and don't work. is there any other way to convert. the
> original coding is used 2.7 python version. can you please help to convert
> this in any way. **sorry for bad English**  this is the coding:



A few general points to try to help you:

- Don't spam the same question in several different threads, and
especially don't privately email list participants with the same
question.  Doing so is likely to get you ignored by people who would
otherwise be quite happy to help you.

- Don't ask people on this list to do something for you.  We're here
to teach, not do.

- When you have an error, post the exact error, in context.  If it's a
shell (including Windows Command Prompt) error, give the command line
you tried.  If it's a Python traceback, give the whole traceback.
Don't try to retype the error, copy and paste it.  If you're on
Windows and can't copy from the Command Prompt, use "Mark" from the
right click menu, and press Enter when you have highlighted what you
want to copy.  Without knowing what the error is, "it says error and
don't work" could mean any number of things.

- Try reading this: http://docs.python.org/3/howto/pyporting.html#use-2to3

- Keep in mind that there are some things that the 2to3 tool can't
convert, and so you may have to do some editing before or after
conversion, or both.

- Post in plain text, not HTML.  Some responders here don't get the
message at all if it includes HTML, and it's annoying to several
others.  We try to be friendly around here, but sometimes the most
friendliness we (individually) can muster is to ignore posts that
annoy us.  Help us help you, and we'll be happy to teach you whatever
you need to know!

-- 
Zach

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


Re: [Tutor] could please convert this code to 3.X by using 2to3

2014-02-18 Thread Mark Lawrence

On 18/02/2014 20:57, S Tareq wrote:

sorry, is there any other way to convert the coding



Use an editor?

Or try this:-

c:\Users\Mark>2to3 --help
Usage: 2to3 [options] file|dir ...

Options:
  -h, --helpshow this help message and exit
  -d, --doctests_only   Fix up doctests only
  -f FIX, --fix=FIX Each FIX specifies a transformation; default: all
  -j PROCESSES, --processes=PROCESSES
Run 2to3 concurrently
  -x NOFIX, --nofix=NOFIX
Prevent a transformation from being run
  -l, --list-fixes  List available transformations
  -p, --print-function  Modify the grammar so that print() is a function
  -v, --verbose More verbose logging
  --no-diffsDon't show diffs of the refactoring
  -w, --write   Write back modified files
  -n, --nobackups   Don't write backups for modified files
  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
Put output files in this directory instead of
overwriting the input files.  Requires -n.
  -W, --write-unchanged-files
Also write files even if no changes were required
(useful with --output-dir); implies -w.
  --add-suffix=ADD_SUFFIX
Append this string to all output filenames. 
Requires
-n if non-empty.  ex: --add-suffix='3' will 
generate

.py3 files.

And please *DON'T* top post.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: [Tutor] could please convert this code to 3.X by using 2to3

2014-02-18 Thread Danny Yoo
>
> - Don't spam the same question in several different threads, and
> especially don't privately email list participants with the same
> question.  Doing so is likely to get you ignored by people who would
> otherwise be quite happy to help you.


I'd like to make that comment stronger: by repeating the same question
over and over, the questioner is giving the impression of a petulant
child.

I _know_ that's not the intent of the original questioner.  But I have
to say exactly what kind of impression I'm getting from this.
Sometimes the person making the mistake doesn't realize how bad of a
mistake they're making.

The guidelines in:

http://www.catb.org/~esr/faqs/smart-questions.html

are that: just guidelines.  But there's a good reason they're there,
because violating them makes it much more difficult to get good
answers from a group of enthusiast volunteers.  It makes them less
enthusiastic.


I don't think there's been any other changes to the question as
originally asked since back in January, right?

https://mail.python.org/pipermail/tutor/2014-January/099466.html


In fact, when the question was asked again a few days later, I tried
the automatic 2to3 translator on the program.  I had no problem with
it: it translated directly.

https://mail.python.org/pipermail/tutor/2014-January/099591.html



So it begs the question: doesn't that kill the problem?  The natural
followup would be: why isn't the original questioner using 2to3?  Are
they having a problem with the tool?  So we asked, but we never got a
satisfactory answer to that question: the original questioner ignored
us.



And again, the original questioner asked the same exact question a few
days later:

https://mail.python.org/pipermail/tutor/2014-January/099708.html

with a similar response.


So I'm inclined to give it one more shot.  S Tareq, why not use "2to3"?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular expression - I

2014-02-18 Thread spir

On 02/18/2014 08:39 PM, Zachary Ware wrote:

Hi Santosh,

On Tue, Feb 18, 2014 at 9:52 AM, Santosh Kumar  wrote:


Hi All,

If you notice the below example, case I is working as expected.

Case I:
In [41]: string = "test"

In [42]: re.match('',string).group()
Out[42]: ''

But why is the raw string 'r' not working as expected ?

Case II:

In [43]: re.match(r'',string).group()
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 re.match(r'',string).group()

AttributeError: 'NoneType' object has no attribute 'group'

In [44]: re.match(r'',string)


It is working as expected, but you're not expecting the right thing
;).  Raw strings don't escape anything, they just prevent backslash
escapes from expanding.  Case I works because "\*" is not a special
character to Python (like "\n" or "\t"), so it leaves the backslash in
place:

>>> ''
''

The equivalent raw string is exactly the same in this case:

>>> r''
''

The raw string you provided doesn't have the backslash, and Python
will not add backslashes for you:

>>> r''
''

The purpose of raw strings is to prevent Python from recognizing
backslash escapes.  For example:

>>> path = 'C:\temp\new\dir' # Windows paths are notorious...
>>> path   # it looks mostly ok... [1]
'C:\temp\new\\dir'
>>> print(path)  # until you try to use it
C:  emp
ew\dir
>>> path = r'C:\temp\new\dir'  # now try a raw string
>>> path   # Now it looks like it's stuffed full of backslashes [2]
'C:\\temp\\new\\dir'
>>> print(path)  # but it works properly!
C:\temp\new\dir

[1] Count the backslashes in the repr of 'path'.  Notice that there is
only one before the 't' and the 'n', but two before the 'd'.  "\d" is
not a special character, so Python didn't do anything to it.  There
are two backslashes in the repr of "\d", because that's the only way
to distinguish a real backslash; the "\t" and "\n" are actually the
TAB and LINE FEED characters, as seen when printing 'path'.

[2] Because they are all real backslashes now, so they have to be
shown escaped ("\\") in the repr.

In your regex, since you're looking for, literally, "", you'll
need to backslash escape the "*" since it is a special character *in
regular expressions*.  To avoid having to keep track of what's special
to Python as well as regular expressions, you'll need to make sure the
backslash itself is escaped, to make sure the regex sees "\*", and the
easiest way to do that is a raw string:

>>> re.match(r'', string).group()
''

I hope this makes some amount of sense; I've had to write it up
piecemeal and will never get it posted at all if I don't go ahead and
post :).  If you still have questions, I'm happy to try again.  You
may also want to have a look at the Regex HowTo in the Python docs:
http://docs.python.org/3/howto/regex.html


In addition to all this:
* You may confuse raw strings with "regex escaping" (a tool func that escapes 
special regex characters for you).
* For simplicity, always use raw strings for regex formats (as in your second 
example); this does not prevent you to escape special characters, but you only 
have to do it once!


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


Re: [Tutor] Using for loops for combinations

2014-02-18 Thread Danny Yoo
On Tue, Feb 18, 2014 at 5:15 PM, Chinanu 'Chinex' Onyekachi
 wrote:
> I’m having difficulties uppercasing a single portion (e.g. hAt) and two
> portions (e.g. HaT) of the string.


[Adding tutor@python.org to CC.  Please use reply-to-all in future
emails on this list.  Thanks!]


Hi Chinanu,


There is a crucial key to this problem.  You must work with a _simpler
problem_ and a _partial solution_, and keeping it up to date as you
learn more and more about the problem and solution.


What does a "simpler problem" mean here?  What does a "partial solution" mean?



Let's take the case where we're trying to compute the combinations for "hat".

Let's call this C("hat").  From your problem statement about, we know
what C("hat") looks like.



Here's a  subquestion of C("hat"): what are the combinations for "h"?

That is, what do we know about C("h")?

C("h")   -->   ["h", "H"]

Trivial case, but worth thinking about.




Here's another subquestion of C("hat"): what are the combinations for "ha"?

That is, what do we know about C("ha")?

C("ha")   -->   ["ha", "Ha", "hA", "HA"]

This we know without having to do any coding.



But it's worth thinking: can we get C("ha") easily out of C("h")?

Yes.

Just take the results of C("h"), and put "a" or "A" at the end of each.

C("h")   -->   ["h", "H"]
C("ha")   -->  ["ha", "hA",
"Ha", "HA"]




Here's another subquestion of C("hat"): what are the combinations for "hat"?

But it's worth thinking: can we get C("hat") easily out of C("ha")?

Yes.




Hopefully that sketches out what I think is the intent of this problem.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using for loops for combinations

2014-02-18 Thread Danny Yoo
On Tue, Feb 18, 2014 at 6:34 PM, Chinanu 'Chinex' Onyekachi
 wrote:
> Thanks for taking you time to explain.
>
> minus = ['subtract'] #creates a list for subtract
>
>
> for i in minus:
>
> minuslist1 = [i[0].upper() + i[1:]]
>
> minuslist2 = [i[0] + i[1].upper() + i[2:]]
>
> minuslist3 = [i[0:].upper()]
>
> minus.extend(minuslist1)
>
> minus.extend(minuslist2)
>
> minus.extend(minuslist3)
>
> print 'list is: %r' % (minus ,),
>
> I get this output  ['subtract', 'Subtract', 'sUbtract', 'SUBTRACT']
> but I feel it is going to take ages to do it this way and get all the
> possible combinations. Would use another for loop be the only way to speed
> things up, and if so how? I’m clearly struggling at forming loops.


Please read:

http://www.catb.org/~esr/faqs/smart-questions.html#uselists



Also, if you have worked all night on this particular problem, as
mentioned in the Stack Overflow question you posted,


http://stackoverflow.com/questions/21856705/find-all-possible-combinations-of-a-specific-word-string-using-any-combination

then know that you are probably not in a good frame of mind for
understanding the problem.  Take a break from it today, and talk to
your instructor the next day if you are still confused.



Also note that if you are trying to normalize input, it's usually the
case that you want to clean it up, rather than have to deal with it
with all its messiness.  For example, if you're trying to read:

 "uppercase"
or

 "UpperCase"

or

 "UPPERCASE"

or

 ...

in all its varieties in a uniform way, then just upper() the input.
Normalize it.  That kind of data digestion makes case analysis easier
sometimes.  Why do you think that computer systems use UPPERCASE
sometimes?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular expression - I

2014-02-18 Thread Santosh Kumar
Thank you all. I got it. :)
I need to read more between lines .


On Wed, Feb 19, 2014 at 4:25 AM, spir  wrote:

> On 02/18/2014 08:39 PM, Zachary Ware wrote:
>
>> Hi Santosh,
>>
>> On Tue, Feb 18, 2014 at 9:52 AM, Santosh Kumar 
>> wrote:
>>
>>>
>>> Hi All,
>>>
>>> If you notice the below example, case I is working as expected.
>>>
>>> Case I:
>>> In [41]: string = "test"
>>>
>>> In [42]: re.match('',string).group()
>>> Out[42]: ''
>>>
>>> But why is the raw string 'r' not working as expected ?
>>>
>>> Case II:
>>>
>>> In [43]: re.match(r'',string).group()
>>> 
>>> ---
>>> AttributeErrorTraceback (most recent call
>>> last)
>>>  in ()
>>> > 1 re.match(r'',string).group()
>>>
>>> AttributeError: 'NoneType' object has no attribute 'group'
>>>
>>> In [44]: re.match(r'',string)
>>>
>>
>> It is working as expected, but you're not expecting the right thing
>> ;).  Raw strings don't escape anything, they just prevent backslash
>> escapes from expanding.  Case I works because "\*" is not a special
>> character to Python (like "\n" or "\t"), so it leaves the backslash in
>> place:
>>
>> >>> ''
>> ''
>>
>> The equivalent raw string is exactly the same in this case:
>>
>> >>> r''
>> ''
>>
>> The raw string you provided doesn't have the backslash, and Python
>> will not add backslashes for you:
>>
>> >>> r''
>> ''
>>
>> The purpose of raw strings is to prevent Python from recognizing
>> backslash escapes.  For example:
>>
>> >>> path = 'C:\temp\new\dir' # Windows paths are notorious...
>> >>> path   # it looks mostly ok... [1]
>> 'C:\temp\new\\dir'
>> >>> print(path)  # until you try to use it
>> C:  emp
>> ew\dir
>> >>> path = r'C:\temp\new\dir'  # now try a raw string
>> >>> path   # Now it looks like it's stuffed full of backslashes [2]
>> 'C:\\temp\\new\\dir'
>> >>> print(path)  # but it works properly!
>> C:\temp\new\dir
>>
>> [1] Count the backslashes in the repr of 'path'.  Notice that there is
>> only one before the 't' and the 'n', but two before the 'd'.  "\d" is
>> not a special character, so Python didn't do anything to it.  There
>> are two backslashes in the repr of "\d", because that's the only way
>> to distinguish a real backslash; the "\t" and "\n" are actually the
>> TAB and LINE FEED characters, as seen when printing 'path'.
>>
>> [2] Because they are all real backslashes now, so they have to be
>> shown escaped ("\\") in the repr.
>>
>> In your regex, since you're looking for, literally, "", you'll
>> need to backslash escape the "*" since it is a special character *in
>> regular expressions*.  To avoid having to keep track of what's special
>> to Python as well as regular expressions, you'll need to make sure the
>> backslash itself is escaped, to make sure the regex sees "\*", and the
>> easiest way to do that is a raw string:
>>
>> >>> re.match(r'', string).group()
>> ''
>>
>> I hope this makes some amount of sense; I've had to write it up
>> piecemeal and will never get it posted at all if I don't go ahead and
>> post :).  If you still have questions, I'm happy to try again.  You
>> may also want to have a look at the Regex HowTo in the Python docs:
>> http://docs.python.org/3/howto/regex.html
>>
>
> In addition to all this:
> * You may confuse raw strings with "regex escaping" (a tool func that
> escapes special regex characters for you).
> * For simplicity, always use raw strings for regex formats (as in your
> second example); this does not prevent you to escape special characters,
> but you only have to do it once!
>
>
> d
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
D. Santosh Kumar
RHCE | SCSA
+91-9703206361


Every task has a unpleasant side .. But you must focus on the end result
you are producing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor