regular expressions help

2019-09-19 Thread Pradeep Patra
Hi all,

I was playing around with regular expressions and testing the simple
regular expression and its notworking for some reason.

I want to search "my-dog" at any of the place in a string and return the
index but its not working. I tried both in python 3.7.3 and 2.7.x. Can
anyone please help?
I tried re.search, re.finditer, re.findall and none of them is not working
for me.
import re

mystr= "where is my-dog"

pattern=re.compile(r'^my\-dog$')
matches = re.search(mystr)

print(matches)

In the above example both cases(match/not match) the matches returns "None"

I tried re.finditer() and then a loop to find all the occurences of the
pattern in the string but even if there is no error but i could not find
the match.

Can anyone help me in this regard?

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


Re: regular expressions help

2019-09-19 Thread David
On Thu, 19 Sep 2019 at 17:51, Pradeep Patra  wrote:
>
> pattern=re.compile(r'^my\-dog$')
> matches = re.search(mystr)
>
> In the above example both cases(match/not match) the matches returns "None"

Hi, do you know what the '^' character does in your pattern?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expressions help

2019-09-19 Thread Pradeep Patra
I am using python 2.7.6 but I also tried on python 3.7.3.

On Thursday, September 19, 2019, Pradeep Patra 
wrote:

> Beginning of the string. But I tried removing that as well and it still
> could not find it. When I tested at www.regex101.com and it matched
> successfully whereas I may be wrong. Could you please help here?
>
> On Thursday, September 19, 2019, David  wrote:
>
>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra 
>> wrote:
>> >
>> > pattern=re.compile(r'^my\-dog$')
>> > matches = re.search(mystr)
>> >
>> > In the above example both cases(match/not match) the matches returns
>> "None"
>>
>> Hi, do you know what the '^' character does in your pattern?
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Spread a statement over various lines

2019-09-19 Thread Manfred Lotz
On Thu, 19 Sep 2019 08:36:04 +0200
Peter Otten <__pete...@web.de> wrote:

> Manfred Lotz wrote:
> 
> >> Not related to your question, but:
> >> You seem to try to convert a Windows wildcard pattern to a regex
> >> pattern.  
> > 
> > No, I'm on Linux.
> > 
> > Shortly, after I had posted the question I discovered fnmatch() in
> > the standard library, and I changed my code accordingly.  
> 
> I would have pointed to fnmatch, but I didn't recognize the '%' that
> you used instead of the usual '?', and fnmatch doesn't either, I
> think.
> 
> Where does '%' come from?
> 

'%' was a mistake as I had replied myself to my initial question.



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


Re: regular expressions help

2019-09-19 Thread David
On Thu, 19 Sep 2019 at 18:41, Pradeep Patra  wrote:
> On Thursday, September 19, 2019, Pradeep Patra  
> wrote:
>> On Thursday, September 19, 2019, David  wrote:
>>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra  
>>> wrote:

>>> > pattern=re.compile(r'^my\-dog$')
>>> > matches = re.search(mystr)

>>> > In the above example both cases(match/not match) the matches returns 
>>> > "None"

>>> Hi, do you know what the '^' character does in your pattern?

>> Beginning of the string. But I tried removing that as well and it still 
>> could not find it. When I tested at www.regex101.com and it matched 
>> successfully whereas I may be wrong. Could you please help here?

> I am using python 2.7.6 but I also tried on python 3.7.3.

$ python2
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> mystr= "where is my-dog"
>>> pattern=re.compile(r'my-dog$')
>>> matches = re.search(mystr)  # this is syntax error, but it is what you 
>>> showed above
Traceback (most recent call last):
  File "", line 1, in 
TypeError: search() takes at least 2 arguments (1 given)
>>> matches = re.search(pattern, mystr)
>>> matches.group(0)
'my-dog'
>>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expressions help

2019-09-19 Thread Pradeep Patra
Thanks David for your quick help. Appreciate it. When I tried on python
2.7.3 the same thing you did below I got the error after matches.group(0)
as follows:

AttributeError: NoneType object has no attribute 'group'.

I tried to check 'None' for no match for re.search as the documentation
says but it's not working.

Unfortunately I cannot update the python version now to 2.7.13 as other
programs are using this version and need to test all and it requires more
testing. Any idea how I can fix this ? I am ok to use any other re
method(not only tied to re.search) as long as it works.

On Thursday, September 19, 2019, David  wrote:

> On Thu, 19 Sep 2019 at 18:41, Pradeep Patra 
> wrote:
> > On Thursday, September 19, 2019, Pradeep Patra 
> wrote:
> >> On Thursday, September 19, 2019, David  wrote:
> >>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra 
> wrote:
>
> >>> > pattern=re.compile(r'^my\-dog$')
> >>> > matches = re.search(mystr)
>
> >>> > In the above example both cases(match/not match) the matches returns
> "None"
>
> >>> Hi, do you know what the '^' character does in your pattern?
>
> >> Beginning of the string. But I tried removing that as well and it still
> could not find it. When I tested at www.regex101.com and it matched
> successfully whereas I may be wrong. Could you please help here?
>
> > I am using python 2.7.6 but I also tried on python 3.7.3.
>
> $ python2
> Python 2.7.13 (default, Sep 26 2018, 18:42:22)
> [GCC 6.3.0 20170516] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re
> >>> mystr= "where is my-dog"
> >>> pattern=re.compile(r'my-dog$')
> >>> matches = re.search(mystr)  # this is syntax error, but it is what you
> showed above
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: search() takes at least 2 arguments (1 given)
> >>> matches = re.search(pattern, mystr)
> >>> matches.group(0)
> 'my-dog'
> >>>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: regular expressions help

2019-09-19 Thread David
On Thu, 19 Sep 2019 at 19:34, Pradeep Patra  wrote:

> Thanks David for your quick help. Appreciate it. When I tried on python 2.7.3 
> the same thing you did below I got the error after matches.group(0) as 
> follows:
>
> AttributeError: NoneType object has no attribute 'group'.
>
> I tried to check 'None' for no match for re.search as the documentation says 
> but it's not working.
>
> Unfortunately I cannot update the python version now to 2.7.13 as other 
> programs are using this version and need to test all and it requires more 
> testing. Any idea how I can fix this ? I am ok to use any other re method(not 
> only tied to re.search) as long as it works.

Hi again Pradeep,

We are now on email number seven, so I am
going to try to give you some good advice ...

When you ask on a forum like this for help, it is very
important to show people exactly what you did.
Everything that you did. In the shortest possible
way that demonstrates whatever issue you are
facing.

It is best to give us a recipe that we can follow
exactly that shows every step that you do when
you have the problem that you need help with.

And the best way to do that is for you to learn
how to cut and paste between where you run
your problem code, and where you send your
email message to us.

Please observe the way that I communicated with
you last time. I sent you an exact cut and paste
from my terminal, to help you by allowing you to
duplicate exactly every step that I made.

You should communicate with us in the same
way. Because when you write something like
your most recent message

> I got the error after matches.group(0) as follows:
> AttributeError: NoneType object has no attribute 'group'.

this tells us nothing useful!! Because we cannot
see everything you did leading up to that, so we
cannot reproduce your problem.

For us to help you, you need to show all the steps,
the same way I did.

Now, to help you, I found the same old version of
Python 2 that you have, to prove to you that it works
on your version.

So you talking about updating Python is not going
to help. Instead, you need to work out what you
are doing that is causing your problem.

Again, I cut and paste my whole session to show
you, see below. Notice that the top lines show that
it is the same version that you have.

If you cut and paste my commands into
your Python then it should work the same way
for you too.

If it does not work for you, then SHOW US THE
WHOLE SESSION, EVERY STEP, so that we can
reproduce your problem. Run your python in a terminal,
and copy and paste the output you get into your message.

$ python
Python 2.7.3 (default, Jun 20 2016, 16:18:47)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> mystr = "where is my-dog"
>>> pattern = re.compile(r'my-dog$')
>>> matches = re.search(pattern, mystr)
>>> matches.group(0)
'my-dog'
>>>

I hope you realise that the re module has been used
by thousands of programmers, for many years.
So it's extremely unlikely that it "doesn't work" in a way that
gets discovered by someone who hardly knows how to use it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
> In all cases, if the optional parts are omitted, the code is executed in the 
> current scope. ...
> 
> 
> You can see from it that "globals" is optional.
> And that, if "globals" is missing, then
> "exec" is executed in the current scope ("f1" in your case).

Thank you for your answer, and that is exactly what confuses me?
Where does x come from? If I only would read x then I would understand why
it can be found/read but I alter it and as such I either have to provide the
info that this is a global variable, declare it inside of f1 or provide 
the globals dict to exec. But I don't do any of it. Why is exec able to use
the global x?

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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
> Then it should be clear that the name 'test01' is put into globals(), if 
> load_module() doesn't throw an exception. No sharing or nesting of 
> namespaces takes place.

Thank you too for your answer. Ok, that means that in every case when exec
imports something it has its own global namespace, right?
Is there a way to manipulate this namespace before any code from that module
gets executed? 

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


Re: Spread a statement over various lines

2019-09-19 Thread Peter Otten
Manfred Lotz wrote:

>> Where does '%' come from?
>> 
> 
> '%' was a mistake as I had replied myself to my initial question.

Oh, sorry. I missed that.

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


Re: exec and globals and locals ...

2019-09-19 Thread Richard Damon
On 9/19/19 6:16 AM, Eko palypse wrote:
>> In all cases, if the optional parts are omitted, the code is executed in the 
>> current scope. ...
>>
>>
>> You can see from it that "globals" is optional.
>> And that, if "globals" is missing, then
>> "exec" is executed in the current scope ("f1" in your case).
> Thank you for your answer, and that is exactly what confuses me?
> Where does x come from? If I only would read x then I would understand why
> it can be found/read but I alter it and as such I either have to provide the
> info that this is a global variable, declare it inside of f1 or provide 
> the globals dict to exec. But I don't do any of it. Why is exec able to use
> the global x?
>
> Eren

I think the issue is that x += 1 isn't exactly like x = x + 1, and this
is one case that shows it. x = x + 1 is an assignment to the symbol x,
which makes x a local, and thus the read becomes an undefined symbol. x
+= 1 is different, it isn't a plain assignment so doesn't create the
local. The read of x is inherently tied to the writing of x so x stays
referring to the global.

-- 
Richard Damon

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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon:
> On 9/19/19 6:16 AM, Eko palypse wrote:
> >> In all cases, if the optional parts are omitted, the code is executed in 
> >> the current scope. ...
> >>
> >>
> >> You can see from it that "globals" is optional.
> >> And that, if "globals" is missing, then
> >> "exec" is executed in the current scope ("f1" in your case).
> > Thank you for your answer, and that is exactly what confuses me?
> > Where does x come from? If I only would read x then I would understand why
> > it can be found/read but I alter it and as such I either have to provide the
> > info that this is a global variable, declare it inside of f1 or provide 
> > the globals dict to exec. But I don't do any of it. Why is exec able to use
> > the global x?
> >
> > Eren
> 
> I think the issue is that x += 1 isn't exactly like x = x + 1, and this
> is one case that shows it. x = x + 1 is an assignment to the symbol x,
> which makes x a local, and thus the read becomes an undefined symbol. x
> += 1 is different, it isn't a plain assignment so doesn't create the
> local. The read of x is inherently tied to the writing of x so x stays
> referring to the global.
> 
> -- 
> Richard Damon

Thank you that would never have come to my mind.
I thought +=1 is just syntactic sugar which clearly isn't.
If I do the regular x = x + 1 then I do get the expected exception.

Thank you
Eren
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote:

>> Then it should be clear that the name 'test01' is put into globals(), if
>> load_module() doesn't throw an exception. No sharing or nesting of
>> namespaces takes place.
> 
> Thank you too for your answer. Ok, that means that in every case when exec
> imports something it has its own global namespace, right?
> Is there a way to manipulate this namespace before any code from that
> module gets executed?

Create a module object, preload its namespace, then exec the module's code, 
like:

$ python3
Python 3.4.3 (default, Nov 12 2018, 22:25:49) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, types
>>> module = types.ModuleType("module")
>>> module.x = 42
>>> exec("print(x)\ndef f(): return x * x", module.__dict__)
42
>>> sys.modules["module"] = module
>>> import module
>>> module.f()
1764

The importlib probably has an official way, but I've yet to wrap my head 
around that part of Python.

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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 12:56:59 UTC+2 schrieb Peter Otten:
> Eko palypse wrote:
> 
> >> Then it should be clear that the name 'test01' is put into globals(), if
> >> load_module() doesn't throw an exception. No sharing or nesting of
> >> namespaces takes place.
> > 
> > Thank you too for your answer. Ok, that means that in every case when exec
> > imports something it has its own global namespace, right?
> > Is there a way to manipulate this namespace before any code from that
> > module gets executed?
> 
> Create a module object, preload its namespace, then exec the module's code, 
> like:
> 
> $ python3
> Python 3.4.3 (default, Nov 12 2018, 22:25:49) 
> [GCC 4.8.4] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys, types
> >>> module = types.ModuleType("module")
> >>> module.x = 42
> >>> exec("print(x)\ndef f(): return x * x", module.__dict__)
> 42
> >>> sys.modules["module"] = module
> >>> import module
> >>> module.f()
> 1764
> 
> The importlib probably has an official way, but I've yet to wrap my head 
> around that part of Python.

Thank you, I'm currently investigating importlib and read that __builtins__
might be another alternative.
My ultimate goal would be to have objects available without the need to import 
them, regardless whether used in a script directly or used in an imported 
module.

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


Re: exec and globals and locals ...

2019-09-19 Thread Richard Damon
On 9/19/19 6:52 AM, Eko palypse wrote:
> Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon:
>> On 9/19/19 6:16 AM, Eko palypse wrote:
 In all cases, if the optional parts are omitted, the code is executed in 
 the current scope. ...


 You can see from it that "globals" is optional.
 And that, if "globals" is missing, then
 "exec" is executed in the current scope ("f1" in your case).
>>> Thank you for your answer, and that is exactly what confuses me?
>>> Where does x come from? If I only would read x then I would understand why
>>> it can be found/read but I alter it and as such I either have to provide the
>>> info that this is a global variable, declare it inside of f1 or provide 
>>> the globals dict to exec. But I don't do any of it. Why is exec able to use
>>> the global x?
>>>
>>> Eren
>> I think the issue is that x += 1 isn't exactly like x = x + 1, and this
>> is one case that shows it. x = x + 1 is an assignment to the symbol x,
>> which makes x a local, and thus the read becomes an undefined symbol. x
>> += 1 is different, it isn't a plain assignment so doesn't create the
>> local. The read of x is inherently tied to the writing of x so x stays
>> referring to the global.
>>
>> -- 
>> Richard Damon
> Thank you that would never have come to my mind.
> I thought +=1 is just syntactic sugar which clearly isn't.
> If I do the regular x = x + 1 then I do get the expected exception.
>
> Thank you
> Eren

For some objects, += even (possibly) calls a different function __radd__
instead of __add__ (if __radd__ doesn't exist then python will fall back
to using __add__)

This can have a big difference in the case of sharing mutable objects.

x = [1, 2]

y = x

x += [3]

# Now x and y are [1, 2, 3]

x = x + [4]

# Now x = [1, 2, 3, 4] but y is still [1, 2, 3]


-- 
Richard Damon

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


Re: exec and globals and locals ...

2019-09-19 Thread Bev In TX
I’m not the OP, but I want to thank you for that clarification.  I had 
previously not understood the ramifications of the following in section “7. 
Simple statements” in “The Python Language Reference”:

“An augmented assignment expression like x += 1 can be rewritten as x = x + 1 
to achieve a similar, but not exactly equal effect. In the augmented version, x 
is only evaluated once. Also, when possible, the actual operation is performed 
in-place, meaning that rather than creating a new object and assigning that to 
the target, the old object is modified instead.”

Thanks again.

Bev

> On Sep 19, 2019, at 5:45 AM, Richard Damon  wrote:
> 
> I think the issue is that x += 1 isn't exactly like x = x + 1, and this
> is one case that shows it. x = x + 1 is an assignment to the symbol x,
> which makes x a local, and thus the read becomes an undefined symbol. x
> += 1 is different, it isn't a plain assignment so doesn't create the
> local. The read of x is inherently tied to the writing of x so x stays
> referring to the global.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Richard Damon wrote:

> On 9/19/19 6:16 AM, Eko palypse wrote:
>>> In all cases, if the optional parts are omitted, the code is executed in
>>> the current scope. ...
>>>
>>>
>>> You can see from it that "globals" is optional.
>>> And that, if "globals" is missing, then
>>> "exec" is executed in the current scope ("f1" in your case).
>> Thank you for your answer, and that is exactly what confuses me?
>> Where does x come from? If I only would read x then I would understand
>> why it can be found/read but I alter it and as such I either have to
>> provide the info that this is a global variable, declare it inside of f1
>> or provide the globals dict to exec. But I don't do any of it. Why is
>> exec able to use the global x?
>>
>> Eren
> 
> I think the issue is that x += 1 isn't exactly like x = x + 1, and this
> is one case that shows it. x = x + 1 is an assignment to the symbol x,
> which makes x a local, and thus the read becomes an undefined symbol. x
> += 1 is different, it isn't a plain assignment so doesn't create the
> local. The read of x is inherently tied to the writing of x so x stays
> referring to the global.

I think you are wrong; both x += 1 and x = x + 1 turn x into a local 
variable: 

>>> def f():
...x += 1
... 
>>> x = 42
>>> f()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in f
UnboundLocalError: local variable 'x' referenced before assignment
>>> def g():
... x = x + 1
... 
>>> g()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in g
UnboundLocalError: local variable 'x' referenced before assignment

The difference is that

x = x + 1

is evaluated as

x = x.__add__(1)

while x += 1 becomes

x = x.__iadd__(1)

For mutable x this allows modifying x in place with consequences likely to 
surprise when you see it for the first time:

>>> t = ["Nobody expects "],
>>> t[0] = t[0] + ["the Spanish inquisition"]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment
>>> t
(['Nobody expects '],)

The above creates a new list which cannot become the first item of the 
(immutable) tuple.

>>> t[0] += ["the Spanish inquisition"]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

The above changes the first item of the tuple in place, but afterwards the 
attempt to rebind t[0] still fails.

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


Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote:

> Thank you, I'm currently investigating importlib and read that
> __builtins__ might be another alternative.
> My ultimate goal would be to have objects available without the need to
> import them, regardless whether used in a script directly or used in an
> imported module.

I'm not sure this is a good idea. Or rather, I'm pretty sure this isn't a 
good idea ;)

To quote the Zen of Python

>>> import this
The Zen of Python, by Tim Peters
[...]
Namespaces are one honking great idea -- let's do more of those!

It looks like you are replacing that line with

"Let's put everything into builtins -- so convenient, no!"

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


Re: regular expressions help

2019-09-19 Thread Pradeep Patra
Thanks  David /Anthony for your help. I figured out the issue myself. I
dont need any ^, $ etc to the regex pattern and the plain string (for exp
my-dog) works fine. I am looking at creating a generic method so that
instead of passing my-dog i can pass my-cat or blah blah. I am thinking of
creating a list of probable combinations to search from the list. Anybody
have better ideas?

On Thu, Sep 19, 2019 at 3:46 PM David  wrote:

> On Thu, 19 Sep 2019 at 19:34, Pradeep Patra 
> wrote:
>
> > Thanks David for your quick help. Appreciate it. When I tried on python
> 2.7.3 the same thing you did below I got the error after matches.group(0)
> as follows:
> >
> > AttributeError: NoneType object has no attribute 'group'.
> >
> > I tried to check 'None' for no match for re.search as the documentation
> says but it's not working.
> >
> > Unfortunately I cannot update the python version now to 2.7.13 as other
> programs are using this version and need to test all and it requires more
> testing. Any idea how I can fix this ? I am ok to use any other re
> method(not only tied to re.search) as long as it works.
>
> Hi again Pradeep,
>
> We are now on email number seven, so I am
> going to try to give you some good advice ...
>
> When you ask on a forum like this for help, it is very
> important to show people exactly what you did.
> Everything that you did. In the shortest possible
> way that demonstrates whatever issue you are
> facing.
>
> It is best to give us a recipe that we can follow
> exactly that shows every step that you do when
> you have the problem that you need help with.
>
> And the best way to do that is for you to learn
> how to cut and paste between where you run
> your problem code, and where you send your
> email message to us.
>
> Please observe the way that I communicated with
> you last time. I sent you an exact cut and paste
> from my terminal, to help you by allowing you to
> duplicate exactly every step that I made.
>
> You should communicate with us in the same
> way. Because when you write something like
> your most recent message
>
> > I got the error after matches.group(0) as follows:
> > AttributeError: NoneType object has no attribute 'group'.
>
> this tells us nothing useful!! Because we cannot
> see everything you did leading up to that, so we
> cannot reproduce your problem.
>
> For us to help you, you need to show all the steps,
> the same way I did.
>
> Now, to help you, I found the same old version of
> Python 2 that you have, to prove to you that it works
> on your version.
>
> So you talking about updating Python is not going
> to help. Instead, you need to work out what you
> are doing that is causing your problem.
>
> Again, I cut and paste my whole session to show
> you, see below. Notice that the top lines show that
> it is the same version that you have.
>
> If you cut and paste my commands into
> your Python then it should work the same way
> for you too.
>
> If it does not work for you, then SHOW US THE
> WHOLE SESSION, EVERY STEP, so that we can
> reproduce your problem. Run your python in a terminal,
> and copy and paste the output you get into your message.
>
> $ python
> Python 2.7.3 (default, Jun 20 2016, 16:18:47)
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re
> >>> mystr = "where is my-dog"
> >>> pattern = re.compile(r'my-dog$')
> >>> matches = re.search(pattern, mystr)
> >>> matches.group(0)
> 'my-dog'
> >>>
>
> I hope you realise that the re module has been used
> by thousands of programmers, for many years.
> So it's extremely unlikely that it "doesn't work" in a way that
> gets discovered by someone who hardly knows how to use it.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode UCS2, UCS4 and ... UCS1

2019-09-19 Thread Gregory Ewing

Eli the Bearded wrote:

There isn't anything called UCS1.


Apparently there is, but it's not a character set, it's a loudspeaker.

https://www.bhphotovideo.com/c/product/1205978-REG/yorkville_sound_ucs1_1200w_15_horn_loaded.html

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


Unable to start Python with Windows 7

2019-09-19 Thread cdoare.ext
Hi,
I am no more able to start Python from Windows 7 environment.
I have the following message :
"The Application was unable to start correctly, (0xC142). Click OK to close 
the application"

Do you have any idea where the problem is ?

Bests regards,


___
Christian Doaré (Groupe Open)
Tél :  +33 2 96 07 02 63
Mobile :  +33 6 33 51 41 15
cdoare@orange.com


_

Ce message et ses pieces jointes peuvent contenir des informations 
confidentielles ou privilegiees et ne doivent donc
pas etre diffuses, exploites ou copies sans autorisation. Si vous avez recu ce 
message par erreur, veuillez le signaler
a l'expediteur et le detruire ainsi que les pieces jointes. Les messages 
electroniques etant susceptibles d'alteration,
Orange decline toute responsabilite si ce message a ete altere, deforme ou 
falsifie. Merci.

This message and its attachments may contain confidential or privileged 
information that may be protected by law;
they should not be distributed, used or copied without authorisation.
If you have received this email in error, please notify the sender and delete 
this message and its attachments.
As emails may be altered, Orange is not liable for messages that have been 
modified, changed or falsified.
Thank you.

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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
No, I have to correct myself

x = 5
def f1():
exec("x = x + 1; print('f1 in:', x)")
return x
print('f1 out', f1())

results in the same, for me confusing, results.

f1 in: 6
f1 out 5

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


Re: Unable to start Python with Windows 7

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 17:52:48 UTC+2 schrieb cdoa...@orange.com:
> Hi,
> I am no more able to start Python from Windows 7 environment.
> I have the following message :
> "The Application was unable to start correctly, (0xC142). Click OK to 
> close the application"
> 
> Do you have any idea where the problem is ?
> 
> Bests regards,
> 

A shot in the dark, you do have Win7 32bit and tried to start python 64bit
Eren
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote:

> No, I have to correct myself
> 
> x = 5
> def f1():
> exec("x = x + 1; print('f1 in:', x)")
> return x
> print('f1 out', f1())
> 
> results in the same, for me confusing, results.
> 
> f1 in: 6
> f1 out 5

Inside a function exec assignments go to a *copy* of the local namespace.
Also LOAD_NAME is used to look up names. Therefore you can read and then 
shade a global name with its local namesake.

Inside a function the namespace is determined statically. As f1() has no 
assignment to x (code inside exec(...) is not considered) x is looked up in 
directly the global namespace using LOAD_GLOBAL.

If you want to access the local namespace used by exec() you have to provide 
one explicitly:

>>> x = 5
>>> def f():
... ns = {}
... exec("x += 1", globals(), ns)
... return ns["x"]
... 
>>> f()
6
>>> x
5

By the way, in Python 2 where exec was a statement the local namespace is 
shared:

>>> x = 5
>>> def f():
... exec "x += 1"
... return x
... 
>>> f()
6
>>> x
5


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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
First thank you for all the answers, very much appreciated.
I assume the root cause might be explained by the zen of python as well.

If the implementation is hard to explain, it's a bad idea.

Maybe I need to rethink my implementation :-)

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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten:
> Eko palypse wrote:
> 
> > No, I have to correct myself
> > 
> > x = 5
> > def f1():
> > exec("x = x + 1; print('f1 in:', x)")
> > return x
> > print('f1 out', f1())
> > 
> > results in the same, for me confusing, results.
> > 
> > f1 in: 6
> > f1 out 5
> 
> Inside a function exec assignments go to a *copy* of the local namespace.
> Also LOAD_NAME is used to look up names. Therefore you can read and then 
> shade a global name with its local namesake.
> 
> Inside a function the namespace is determined statically. As f1() has no 
> assignment to x (code inside exec(...) is not considered) x is looked up in 
> directly the global namespace using LOAD_GLOBAL.
> 
> If you want to access the local namespace used by exec() you have to provide 
> one explicitly:
> 
> >>> x = 5
> >>> def f():
> ... ns = {}
> ... exec("x += 1", globals(), ns)
> ... return ns["x"]
> ... 
> >>> f()
> 6
> >>> x
> 5
> 
> By the way, in Python 2 where exec was a statement the local namespace is 
> shared:
> 
> >>> x = 5
> >>> def f():
> ... exec "x += 1"
> ... return x
> ... 
> >>> f()
> 6
> >>> x
> 5

Sorry, missed that. 
Thank you, may I ask you how I could have come myself
to that explanation? What do I have to read to get that understanding?
Hopefully you don't say read the C code, because that is something
I tried but failed miserably.

Thank you
Eren
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Unicode UCS2, UCS4 and ... UCS1

2019-09-19 Thread MRAB

On 2019-09-19 09:55, Gregory Ewing wrote:

Eli the Bearded wrote:

There isn't anything called UCS1.


Apparently there is, but it's not a character set, it's a loudspeaker.

https://www.bhphotovideo.com/c/product/1205978-REG/yorkville_sound_ucs1_1200w_15_horn_loaded.html

The OP might mean Py_UCS1, which is an implementation detail of the 
Flexible String Representation.

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


Re: regular expressions help

2019-09-19 Thread Chris Angelico
On Fri, Sep 20, 2019 at 1:07 AM Pradeep Patra  wrote:
>
> Thanks  David /Anthony for your help. I figured out the issue myself. I
> dont need any ^, $ etc to the regex pattern and the plain string (for exp
> my-dog) works fine. I am looking at creating a generic method so that
> instead of passing my-dog i can pass my-cat or blah blah. I am thinking of
> creating a list of probable combinations to search from the list. Anybody
> have better ideas?
>

If you just want to find a string in another string, don't use regular
expressions at all! Just ask Python directly:

>>> print("my-cat" in "This is where you can find my-cat, look, see!")
True

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


Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote:

> Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten:
>> Eko palypse wrote:
>> 
>> > No, I have to correct myself
>> > 
>> > x = 5
>> > def f1():
>> > exec("x = x + 1; print('f1 in:', x)")
>> > return x
>> > print('f1 out', f1())
>> > 
>> > results in the same, for me confusing, results.
>> > 
>> > f1 in: 6
>> > f1 out 5
>> 
>> Inside a function exec assignments go to a *copy* of the local namespace.
>> Also LOAD_NAME is used to look up names. Therefore you can read and then
>> shade a global name with its local namesake.
>> 
>> Inside a function the namespace is determined statically. As f1() has no
>> assignment to x (code inside exec(...) is not considered) x is looked up
>> in directly the global namespace using LOAD_GLOBAL.
>> 
>> If you want to access the local namespace used by exec() you have to
>> provide one explicitly:
>> 
>> >>> x = 5
>> >>> def f():
>> ... ns = {}
>> ... exec("x += 1", globals(), ns)
>> ... return ns["x"]
>> ...
>> >>> f()
>> 6
>> >>> x
>> 5
>> 
>> By the way, in Python 2 where exec was a statement the local namespace is
>> shared:
>> 
>> >>> x = 5
>> >>> def f():
>> ... exec "x += 1"
>> ... return x
>> ...
>> >>> f()
>> 6
>> >>> x
>> 5
> 
> Sorry, missed that.
> Thank you, may I ask you how I could have come myself
> to that explanation? What do I have to read to get that understanding?
> Hopefully you don't say read the C code, because that is something
> I tried but failed miserably.

https://docs.python.org/3/library/functions.html#exec
https://docs.python.org/3/reference/executionmodel.html#naming-and-binding

(I had to google for the second link.)

I usually experiment with code and the disassembler. I find its output quite 
readable, 

>>> def f(): x += 1
... 
>>> import dis
>>> dis.dis(f)
  1   0 LOAD_FAST0 (x)
  3 LOAD_CONST   1 (1)
  6 INPLACE_ADD
  7 STORE_FAST   0 (x)
 10 LOAD_CONST   0 (None)
 13 RETURN_VALUE
>>> dis.dis(compile("x += 1", "", "exec"))
  1   0 LOAD_NAME0 (x)
  3 LOAD_CONST   0 (1)
  6 INPLACE_ADD
  7 STORE_NAME   0 (x)
 10 LOAD_CONST   1 (None)
 13 RETURN_VALUE

and you can limit yourself to small snippets.

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


Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 20:24:49 UTC+2 schrieb Peter Otten:
> Eko palypse wrote:
> 
> > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten:
> >> Eko palypse wrote:
> >> 
> >> > No, I have to correct myself
> >> > 
> >> > x = 5
> >> > def f1():
> >> > exec("x = x + 1; print('f1 in:', x)")
> >> > return x
> >> > print('f1 out', f1())
> >> > 
> >> > results in the same, for me confusing, results.
> >> > 
> >> > f1 in: 6
> >> > f1 out 5
> >> 
> >> Inside a function exec assignments go to a *copy* of the local namespace.
> >> Also LOAD_NAME is used to look up names. Therefore you can read and then
> >> shade a global name with its local namesake.
> >> 
> >> Inside a function the namespace is determined statically. As f1() has no
> >> assignment to x (code inside exec(...) is not considered) x is looked up
> >> in directly the global namespace using LOAD_GLOBAL.
> >> 
> >> If you want to access the local namespace used by exec() you have to
> >> provide one explicitly:
> >> 
> >> >>> x = 5
> >> >>> def f():
> >> ... ns = {}
> >> ... exec("x += 1", globals(), ns)
> >> ... return ns["x"]
> >> ...
> >> >>> f()
> >> 6
> >> >>> x
> >> 5
> >> 
> >> By the way, in Python 2 where exec was a statement the local namespace is
> >> shared:
> >> 
> >> >>> x = 5
> >> >>> def f():
> >> ... exec "x += 1"
> >> ... return x
> >> ...
> >> >>> f()
> >> 6
> >> >>> x
> >> 5
> > 
> > Sorry, missed that.
> > Thank you, may I ask you how I could have come myself
> > to that explanation? What do I have to read to get that understanding?
> > Hopefully you don't say read the C code, because that is something
> > I tried but failed miserably.
> 
> https://docs.python.org/3/library/functions.html#exec
> https://docs.python.org/3/reference/executionmodel.html#naming-and-binding
> 
> (I had to google for the second link.)
> 
> I usually experiment with code and the disassembler. I find its output quite 
> readable, 
> 
> >>> def f(): x += 1
> ... 
> >>> import dis
> >>> dis.dis(f)
>   1   0 LOAD_FAST0 (x)
>   3 LOAD_CONST   1 (1)
>   6 INPLACE_ADD
>   7 STORE_FAST   0 (x)
>  10 LOAD_CONST   0 (None)
>  13 RETURN_VALUE
> >>> dis.dis(compile("x += 1", "", "exec"))
>   1   0 LOAD_NAME0 (x)
>   3 LOAD_CONST   0 (1)
>   6 INPLACE_ADD
>   7 STORE_NAME   0 (x)
>  10 LOAD_CONST   1 (None)
>  13 RETURN_VALUE
> 
> and you can limit yourself to small snippets.

Thank you very much, really, very much appreciated.
Normally I do use docs.python.org as my first resource to look 
for an answer to my questions but I'm not always convinced that
my understanding/interpretation is correct.
Then I research on the web to see if there are similar questions and
try to understand these explanations but ...
and in this particular case I wasn't even able to find an answer. 

The dis module is nice to see differences but I assume more useful
to software developers than to hobby programmers like me.
At least not in the current state of knowledge I have.
I was under the impression that I have a good understanding of the 
python language before I started my current project but now ...
Enough whining :-)

Once again,
Thank you very much
Eren
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python?

2019-09-19 Thread Cecil Westerhof
Paul Rubin  writes:

> Python 3.7.3 (default, Apr  3 2019, 05:39:12)
> Type "help", "copyright", "credits" or "license" for more information.
> >>> a = range(10)
> >>> b = reversed(a)
> >>> sum(a) == sum(b)
> True
> >>> sum(b) == sum(a)
> False

Why does this happen?

By the way, when you change the last statement to:
sum(a) == sum(b)

you also get False.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python?

2019-09-19 Thread Chris Angelico
On Fri, Sep 20, 2019 at 5:16 AM Cecil Westerhof  wrote:
>
> Paul Rubin  writes:
>
> > Python 3.7.3 (default, Apr  3 2019, 05:39:12)
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> a = range(10)
> > >>> b = reversed(a)
> > >>> sum(a) == sum(b)
> > True
> > >>> sum(b) == sum(a)
> > False
>
> Why does this happen?
>
> By the way, when you change the last statement to:
> sum(a) == sum(b)
>
> you also get False.

>>> sum(range(10)) == sum(reversed(range(10)))
True

If you actually want a reversed range, use slice notation instead of
the reversed() function, which is more parallel to iter().

>>> a = range(10)
>>> b = a[::-1]
>>> sum(a) == sum(b)
True
>>> sum(b) == sum(a)
True

Now you actually have a range that runs the other direction, instead
of an iterator that runs through the range backwards.

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


Re: Spread a statement over various lines

2019-09-19 Thread Ralf M.

Am 18.09.2019 um 22:22 schrieb Chris Angelico:

On Thu, Sep 19, 2019 at 6:20 AM Ralf M.  wrote:


Am 17.09.2019 um 20:59 schrieb Manfred Lotz:

I have a function like follows

def regex_from_filepat(fpat):
  rfpat = fpat.replace('.', '\\.') \
.replace('%', '.')  \
.replace('*', '.*')

  return '^' + rfpat + '$'


As I don't want to have the replace() functions in one line my
question is if it is ok to spread the statement over various lines as
shown above, or if there is a better way?

Thanks.



Not related to your question, but:
You seem to try to convert a Windows wildcard pattern to a regex
pattern. However, wildcards sometimes behave a bit different than what
you assume. I know for instance that *.* matches any filename, even if
the filename doesn't contain a dot.


Hmm, why do you assume it's a Windows wildcard pattern specifically?

ChrisA

I think I jumped to that conclusion because the example didn't treat [ ] 
as special characters. [ ] are special in a unix shell, but not at a cmd 
prompt. Thinking it over, [ ] would need a much differnt treatment and 
might be left out of the example for that reason, though.


Also I may be biased: I mostly use Windows, Linux only occasionally.

Ralf M.

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


Re: Spread a statement over various lines

2019-09-19 Thread Ralf M.

Am 18.09.2019 um 22:24 schrieb Alexandre Brault:


On 2019-09-18 4:01 p.m., Ralf M. wrote:


I don't know the exact rules of Windows wildcards, so there may be
even more cases of unexpected behavior.
If anyone knows where to find the complete rules (or a python module
that implements them), I would be interested.



fnmatch in the standard library has a translate function that transforms
a glob pattern to a regex

https://docs.python.org/3.7/library/fnmatch.html#fnmatch.translate

Alex


Thank you for the pointer.
However, from the documentation of module fnmatch:
"This module provides support for Unix shell-style wildcards"

And Unix shell-style wildcards differ from Windows cmd wildcards.
For one, [ ] are special in Unix shells, but not in Windows cmd.
For another, cmd wildcards have several quirks, e.g. *.* matching 
filenames that don't contain a dot, or "a???" matching "ab".


Several years ago, when I needed DOS-style globbing, I copied fnmatch.py 
and glob.py from the standard library and modified them to treat [ ] as 
not special. However, that didn't help with the cmd quirks, and I don't 
even know all the rules about cmd wildcards.


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


Re: exec and globals and locals ...

2019-09-19 Thread jfong
>>> x = 3
>>> def foo():
... exec("print(globals(), locals()); x = x + 1; print(globals(), 
locals())")
...
>>> foo()
{'foo': , '__package__': None, '__builtins__': 
, '__loader__': , '__doc__': None, '__name__': '__main__', 
'__spec__': None, 'x': 3} {}
{'foo': , '__package__': None, '__builtins__': 
, '__loader__': , '__doc__': None, '__name__': '__main__', 
'__spec__': None, 'x': 3} {'x': 4}
>>> def goo():
... print(globals(), locals())
... x = x + 1
... print(globals(), locals())
...
>>> goo()
{'foo': , '__package__': None, '__builtins__': 
, '__loader__': , '__doc__': None, '__name__': '__main__', 
'__spec__': None, 'goo': , 'x': 3} {}
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in goo
UnboundLocalError: local variable 'x' referenced before assignment
>>>

Can't figure out what makes it different:-(

--Jach

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


Re: numpy results in segmentation fault

2019-09-19 Thread Aldwin Pollefeyt
use:

num_arr1 = numpy.array(tgt_arr1, dtype=int)
num_arr2 = numpy.array(tgt_arr2, dtype=int)


On Mon, Sep 16, 2019 at 5:36 PM Pradeep Patra 
wrote:

> Yes it is crashing in the hackerrank site and the testcases fails with
> segmentation fault. I tried to install numpy with 3.7.3 and it is for some
> reason not working and after import when I run import numpy at python
> console and press enter I get >>? i,e its not working properly.
>
> Can you please help letting me know the python and numpy compatibility
> matrix or I am missing anything?
>
> I tried some of the numpy code from the other github and it also fails with
> the segmentation fault :-(. I am guessing some numpy version compatility
> issue or some environment issue.
>
> On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans  wrote:
>
> > On 12/09/2019 15.53, Pradeep Patra wrote:
> > > Hi ,
> > >
> > > I was trying to solve the hackerrank and was using python 3.7.x.
> > > https://www.hackerrank.com/challenges/np-concatenate/problem
> > >
> > > While running the code sometimes I get success result and sometimes it
> > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue
> > why
> > > the code is crashing ? Does anyone have any idea?
> >
> >
> > Are you sure it's your code that's crashing, and not something beyond
> > your control? (Such as the software that is starting Python for you)
> > Does it depend on the input? Can you reproduce the issue in a controlled
> > environment (i.e. on your own PC)?
> >
> >
> > >
> > > Regards
> > > Pradeep
> > >
> > > import numpy
> > >
> > > n,m,p = map(int,input().split())
> > > tgt_arr1 = []
> > > for i in range(n):
> > > row = list(map(int,input().split()))
> > > tgt_arr1.append(row)
> > > tgt_arr2 = []
> > > for j in range(m):
> > > row = list(map(int,input().split()))
> > > tgt_arr2.append(row)
> > >
> > > num_arr1 = numpy.array(tgt_arr1,int)
> > > num_arr2 = numpy.array(tgt_arr2,int)
> > >
> > > print(numpy.concatenate((num_arr1,num_arr2),axis=0))
> >
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list