Re: [Tutor] 'Installing' Python at runtime? (Civilization)

2011-02-04 Thread Japhy Bartlett
a common approach is to embed python in a compiled binary

On Thu, Feb 3, 2011 at 9:18 PM, Steven D'Aprano  wrote:
> Alan Gauld wrote:
>>
>> "C.Y. Ruhulessin"  wrote
>>
>>> When I load up Civilization IV, a Firaxis game, the loading screen tells
>>> me
>>> "Loading Python".
>>>
>>> However, I can't seem to find out where it installs python
>>
>> It probably doesn't actually install Python it is simply loading
>> the interpreter into memory.
>
> That's what it says... it says "Loading Python", not installing it. It would
> include a version of Python when the game was installed, possibly embedded
> in the Civilization game itself. *Installing* Python each time you start the
> game would be silly.
>
> To find out where it is installed, use your operating system's Find Files
> utility to search for a file named "python". If you don't find it, that
> could mean they have renamed it something else, or it is embedded in the
> game where you can't get to it.
>
>
>> It probably uses Python as its macro language for configuration
>> or customisation. To execute those scripts it will need to load
>> a DLL containing the interpreter code. I don't know for sure
>> but I'd guess that's what it means.
>
> Many games use Python as a scripting language. (Lua is another popular
> choice.) The multiplayer game EVE maintains a special port of Python called
> "stackless".
>
>
> --
> Steven
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ideas and good examples

2011-02-04 Thread Alan Gauld

"David Goering"  wrote

It says v3 is Under Construction, how long do you expect it to be 
like this.


The basic v3 tutor is all done except for the Case Study - which is
currently about 75% complete. Its certainly usable.

The Practical Python topics have not been started yet, but any v3 user
who got that far should be able to adapt the v2 versions pretty 
easily,

they are mostly about modules that haven't changed much. So
apart from adding () to print and using input() instead of raw_input()
they should be fine. In fact I'm thinking about putting a redirect
link on those pages as a temporary measure...


Is it worth waiting or should I just go ahead with v2 ?


v2 is still better supported than v3 on forums and in tutorials etc.
And a few libraries have not been poprtted to v3 yet, but the
number is reducing every week. My own view is that a beginner
with no immediate specific project could probably start with v3.
But if you are learning Python because you need it for a specific
project then stick with v2 for now - its a lower risk option.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Alan Gauld

"Alex Hall"  wrote


I am wondering what the best way to do the following would be: throw
an exception, or always return an object but set an error flag if
something goes wrong? Here is an example:


Throw an exception is the short general case answer...


class c:
def __init__(self):
 self.error=False
def func(self, val):
 if val!=10: self.error=True


But note that neither of these methods returns "an object"
- unless you count None as an object of course.


Which is the "standard" way when dealing with objects? Throw
exceptions or always return an object, even if said object has an
error and may therefore not have data beyond an error code and
message?


I'm not sure what you have in mind here but remember that
init() is an initialiser and not really a constructor so the object
already exists when init() is called. init() does not return the
object.

And most methods do not return the object of which they
are a part (in Python at least, in SmallTalk they do).

But if you are using object in the general sense of a return
value (since everything in Python is an object) then yes
you should return an object and let the exception signal
failure.



If I go the exception route, can I somehow put a message into
the exception, maybe adding it as an attribute of my custom 
exception

class?


Yes, or you can just pass a string to any exception when you raise it.

Alan G. 



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


Re: [Tutor] RE module is working ?

2011-02-04 Thread Peter Otten
Karim wrote:

> Recall:
> 
>  >>> re.subn(r'([^\\])?"', r'\1\\"', expression)
> 
> Traceback (most recent call last):
>  File "", line 1, in
>  File "/home/karim/build/python/install/lib/python2.7/re.py", line
> 162, in subn
>return _compile(pattern, flags).subn(repl, string, count)
>  File "/home/karim/build/python/install/lib/python2.7/re.py", line
> 278, in filter
>return sre_parse.expand_template(template, match)
>  File "/home/karim/build/python/install/lib/python2.7/sre_parse.py",
> line 787, in expand_template
>raise error, "unmatched group"
> sre_constants.error: unmatched group
> 
> 
> Found the solution: '?' needs to be inside parenthesis (saved pattern)
> because outside we don't know if the saved match argument
> will exist or not namely '\1'.
> 
>  >>> re.subn(r'([^\\]?)"', r'\1\\"', expression)
> 
> (' \\"\\" ', 2)
> 
> sed unix command is more permissive: sed 's/\([^\\]\)\?"/\1\\"/g'
> because '?' can be outside parenthesis (saved pattern but escaped for
> sed). \1 seems to not cause issue when matching is found. Perhaps it is
> created only when match occurs.

Thanks for reporting the explanation.

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


Re: [Tutor] RE module is working ?

2011-02-04 Thread Peter Otten
Karim wrote:

> That is not the thing I want. I want to escape any " which are not
> already escaped.
> The sed regex  '/\([^\\]\)\?"/\1\\"/g' is exactly what I need (I have
> made regex on unix since 15 years).

Can the backslash be escaped, too? If so I don't think your regex does what 
you think it does.

r'\\\"' # escaped \ followed by escaped "

should not be altered, but:

$ echo '\\\"' | sed 's/\([^\\]\)\?"/\1\\"/g'
" # two escaped \ folloed by a " that is not escaped



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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Alex Hall
On 2/4/11, Alan Gauld  wrote:
> "Alex Hall"  wrote
>
>> I am wondering what the best way to do the following would be: throw
>> an exception, or always return an object but set an error flag if
>> something goes wrong? Here is an example:
>
> Throw an exception is the short general case answer...
>
>> class c:
>> def __init__(self):
>>  self.error=False
>> def func(self, val):
>>  if val!=10: self.error=True
>
> But note that neither of these methods returns "an object"
> - unless you count None as an object of course.
There should have been a function below that which returned the object
with or without the error attribute.
>
>> Which is the "standard" way when dealing with objects? Throw
>> exceptions or always return an object, even if said object has an
>> error and may therefore not have data beyond an error code and
>> message?
>
> I'm not sure what you have in mind here but remember that
> init() is an initialiser and not really a constructor so the object
> already exists when init() is called. init() does not return the
> object.
Right, that second function should have done it, but I likely wrote it
wrong (what I get for making up an example on the spot).
>
> And most methods do not return the object of which they
> are a part (in Python at least, in SmallTalk they do).
That seems like it would get rather confusing...
>
> But if you are using object in the general sense of a return
> value (since everything in Python is an object) then yes
> you should return an object and let the exception signal
> failure.
Will do, and I was talking about an object as in an instance of my
class, not generally, though I do like Python's model of treating
*everything* as an object.
>
>
>> If I go the exception route, can I somehow put a message into
>> the exception, maybe adding it as an attribute of my custom
>> exception
>> class?
>
> Yes, or you can just pass a string to any exception when you raise it.
Good to know. For this, I wanted my own exception, so I made this:
class ApiError(Exception):
 #for use with any bad requests from Bookshare's api

 def __init__(self, number, message):
  self.number=number
  self.message=message

 def __str__(self):
  return str(self.number)+": "+self.message

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


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Bill Felton

On Feb 4, 2011, at 8:03 AM, Alex Hall wrote:

> On 2/4/11, Alan Gauld  wrote:
>> "Alex Hall"  wrote
>> 
>>> I am wondering what the best way to do the following would be: throw
>>> an exception, or always return an object but set an error flag if
>>> something goes wrong? Here is an example:
>> 
>> Throw an exception is the short general case answer...
>> 
>>> class c:
>>> def __init__(self):
>>> self.error=False
>>> def func(self, val):
>>> if val!=10: self.error=True
>> 
>> But note that neither of these methods returns "an object"
>> - unless you count None as an object of course.
> There should have been a function below that which returned the object
> with or without the error attribute.
>> 
>>> Which is the "standard" way when dealing with objects? Throw
>>> exceptions or always return an object, even if said object has an
>>> error and may therefore not have data beyond an error code and
>>> message?
>> 
>> I'm not sure what you have in mind here but remember that
>> init() is an initialiser and not really a constructor so the object
>> already exists when init() is called. init() does not return the
>> object.
> Right, that second function should have done it, but I likely wrote it
> wrong (what I get for making up an example on the spot).
>> 
>> And most methods do not return the object of which they
>> are a part (in Python at least, in SmallTalk they do).
> That seems like it would get rather confusing...

Um, not quite correct -- methods *without a specified return value* always 
return self, that is, the object which executed the method.
This is useful, but is hardly the most common situation.
It is used most often when a method modifies the receiver and the sender(s) 
is/are likely to be interested in the updated object itself rather than in one 
of the parameters to the method or some other object.
However, it can be a source of error, as many methods which modify return self, 
but many common collection protocols return the argument rather than the 
modified collection.  Just one of those things you need to learn, which all 
languages have ;-)

cheers,
Bill

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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread ALAN GAULD


> > And most methods do not return the object of which  they
> > are a part (in Python at least, in SmallTalk they do).
> That  seems like it would get rather confusing...

Actually, from an OOP point of view, its a very powerful 
default and I wish more Python methods did it. It allows 
you to chain operations together. Using Python notation:

employee.updateName('fred).updateAddress(number=7).resetPassword()

Using the Python idiom of not using a return value for updates 
you'd need to use 3 separate lines to do the updates... Of 
course 3 lines are easier to debug, but often chaining 
operations like this is useful. If the return value is not 
needed then you can just ignore it.

But I often use 'return self' as a kind of default option in 
my own classes.

Alan G.

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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Emile van Sebille

On 2/4/2011 5:35 AM Bill Felton said...


Um, not quite correct -- methods *without a specified return value* always 
return self, that is, the object which executed the method.


Um, no.  They return None.

>>> class Test:
...   def __init__(self):pass
...   def test(self):pass
...
>>> a=Test().test()
>>> a
>>> print a
None
>>>

Emile

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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Bill Felton
Um, yes, emphatically yes.  You missed the context, which was Smalltalk, and it 
is terms of Smalltalk that my reply is couched.
Yes, Python returns None.
Smalltalk returns self by default.
Given the dropped context, you are correct.
Given the context meant, I am.

regards,
Bill

On Feb 4, 2011, at 9:28 AM, Emile van Sebille wrote:

> On 2/4/2011 5:35 AM Bill Felton said...
> 
>> Um, not quite correct -- methods *without a specified return value* always 
>> return self, that is, the object which executed the method.
> 
> Um, no.  They return None.
> 
> >>> class Test:
> ...   def __init__(self):pass
> ...   def test(self):pass
> ...
> >>> a=Test().test()
> >>> a
> >>> print a
> None
> >>>
> 
> Emile
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Alex Hall
On 2/4/11, Bill Felton  wrote:
> Um, yes, emphatically yes.  You missed the context, which was Smalltalk, and
> it is terms of Smalltalk that my reply is couched.
> Yes, Python returns None.
> Smalltalk returns self by default.
> Given the dropped context, you are correct.
> Given the context meant, I am.
I was following this until now... what is this smalltalk and drop?
>
> regards,
> Bill
>
> On Feb 4, 2011, at 9:28 AM, Emile van Sebille wrote:
>
>> On 2/4/2011 5:35 AM Bill Felton said...
>>
>>> Um, not quite correct -- methods *without a specified return value*
>>> always return self, that is, the object which executed the method.
>>
>> Um, no.  They return None.
>>
>> >>> class Test:
>> ...   def __init__(self):pass
>> ...   def test(self):pass
>> ...
>> >>> a=Test().test()
>> >>> a
>> >>> print a
>> None
>> >>>
>>
>> Emile
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Bill Felton
Smalltalk is an OO programming language that had some dominance in the late 
80's through much of the 90s.  It has proceeded to more or less commit suicide 
through a variety of mechanisms.
By 'drop' I meant 'drop the context of the remarks', which in the original post 
was discussing Smalltalk's default method return.
It isn't a major deal, overall, I think the most interesting remark has been 
Alan Gauld's musings immediately prior to Emile's to which I responded.
Python defaults to None.
Smalltalk defaults to self, which is always the object executing the code 
involved.
Alan points out that this is extremely convenient and can reduce the code bulk 
involved when one wishes to string multiple operations together.
It is stylistically possible in Python to set methods to return 'self' if/when 
that would be useful, but if it is not a language level feature, it becomes a 
convention that one can only rely on in ones own code.

regards,
Bill

On Feb 4, 2011, at 11:56 AM, Alex Hall wrote:

> On 2/4/11, Bill Felton  wrote:
>> Um, yes, emphatically yes.  You missed the context, which was Smalltalk, and
>> it is terms of Smalltalk that my reply is couched.
>> Yes, Python returns None.
>> Smalltalk returns self by default.
>> Given the dropped context, you are correct.
>> Given the context meant, I am.
> I was following this until now... what is this smalltalk and drop?
>> 
>> regards,
>> Bill
>> 
>> On Feb 4, 2011, at 9:28 AM, Emile van Sebille wrote:
>> 
>>> On 2/4/2011 5:35 AM Bill Felton said...
>>> 
 Um, not quite correct -- methods *without a specified return value*
 always return self, that is, the object which executed the method.
>>> 
>>> Um, no.  They return None.
>>> 
>> class Test:
>>> ...   def __init__(self):pass
>>> ...   def test(self):pass
>>> ...
>> a=Test().test()
>> a
>> print a
>>> None
>> 
>>> 
>>> Emile
>>> 
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>> 
> 
> 
> -- 
> Have a great day,
> Alex (msg sent from GMail website)
> mehg...@gmail.com; http://www.facebook.com/mehgcap

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


Re: [Tutor] best practice: throw exception or set a flag?

2011-02-04 Thread Alex Hall
I see, thanks!

On 2/4/11, Bill Felton  wrote:
> Smalltalk is an OO programming language that had some dominance in the late
> 80's through much of the 90s.  It has proceeded to more or less commit
> suicide through a variety of mechanisms.
> By 'drop' I meant 'drop the context of the remarks', which in the original
> post was discussing Smalltalk's default method return.
> It isn't a major deal, overall, I think the most interesting remark has been
> Alan Gauld's musings immediately prior to Emile's to which I responded.
> Python defaults to None.
> Smalltalk defaults to self, which is always the object executing the code
> involved.
> Alan points out that this is extremely convenient and can reduce the code
> bulk involved when one wishes to string multiple operations together.
> It is stylistically possible in Python to set methods to return 'self'
> if/when that would be useful, but if it is not a language level feature, it
> becomes a convention that one can only rely on in ones own code.
>
> regards,
> Bill
>
> On Feb 4, 2011, at 11:56 AM, Alex Hall wrote:
>
>> On 2/4/11, Bill Felton  wrote:
>>> Um, yes, emphatically yes.  You missed the context, which was Smalltalk,
>>> and
>>> it is terms of Smalltalk that my reply is couched.
>>> Yes, Python returns None.
>>> Smalltalk returns self by default.
>>> Given the dropped context, you are correct.
>>> Given the context meant, I am.
>> I was following this until now... what is this smalltalk and drop?
>>>
>>> regards,
>>> Bill
>>>
>>> On Feb 4, 2011, at 9:28 AM, Emile van Sebille wrote:
>>>
 On 2/4/2011 5:35 AM Bill Felton said...

> Um, not quite correct -- methods *without a specified return value*
> always return self, that is, the object which executed the method.

 Um, no.  They return None.

>>> class Test:
 ...   def __init__(self):pass
 ...   def test(self):pass
 ...
>>> a=Test().test()
>>> a
>>> print a
 None
>>>

 Emile

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>> --
>> Have a great day,
>> Alex (msg sent from GMail website)
>> mehg...@gmail.com; http://www.facebook.com/mehgcap
>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RE module is working ?

2011-02-04 Thread Karim

On 02/04/2011 02:36 AM, Steven D'Aprano wrote:

Karim wrote:


*Indeed what's the matter with RE module!?*

You should really fix the problem with your email program first;
Thunderbird issue with bold type (appears as stars) but I don't know 
how to fix it yet.


A man when to a doctor and said, "Doctor, every time I do this, it 
hurts. What should I do?"


The doctor replied, "Then stop doing that!"

:)


Yes this these words made me laugh. I will keep it in my funny box.




Don't add bold or any other formatting to things which should be 
program code. Even if it looks okay in *your* program, you don't know 
how it will look in other people's programs. If you need to draw 
attention to something in a line of code, add a comment, or talk about 
it in the surrounding text.



[...]
That is not the thing I want. I want to escape any " which are not 
already escaped.
The sed regex  '/\([^\\]\)\?"/\1\\"/g' is exactly what I need (I have 
made regex on unix since 15 years).


Mainly sed, awk and perl sometimes grep and egrep. I know this is the 
jungle.


Which regex? Perl regexes? sed or awk regexes? Extended regexes? GNU 
posix compliant regexes? grep or egrep regexes? They're all different.


In any case, I am sorry, I don't think your regex does what you say. 
When I try it, it doesn't work for me.


[steve@sylar ~]$ echo 'Some \"text"' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \\"text\"


I give you my word on this. Exact output I redid it:

#MY OS VERSION
karim@Requiem4Dream:~$ uname -a
Linux Requiem4Dream 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 
UTC 2011 x86_64 GNU/Linux

#MY SED VERSION
karim@Requiem4Dream:~$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

GNU sed home page: .
General help using GNU software: .
E-mail bug reports to: .
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
#MY SED OUTPUT COMMAND:
karim@Requiem4Dream:~$  echo 'Some ""' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \"\"
# THIS IS WHAT I WANT 2 CONSECUTIVES IF THE FIRST ONE IS ALREADY ESCAPED 
I DON'T WANT TO ESCAPED IT TWICE.

karim@Requiem4Dream:~$ echo 'Some \""' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \"\"
# BY THE WAY THIS ONE WORKS:
karim@Requiem4Dream:~$ echo 'Some "text"' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \"text\"
# BUT SURE NOT THIS ONE NOT COVERED BY MY REGEX (I KNOW IT AND WANT 
ORIGINALY TO COVER IT):
karim@Requiem4Dream:~$ echo 'Some \"text"' | sed -e 
's/\([^\\]\)\?"/\1\\"/g'

Some \\"text\"

By the way in all sed version I work with the '?'  (0 or one match) 
should be escaped that's the reason I have '\?' same thing with save 
'\(' and '\)' to store value. In perl, grep you don't need to escape.


# SAMPLE FROM http://www.gnu.org/software/sed/manual/sed.html

|\+|
   same As |*|, but matches one or more. It is a GNU extension.
|\?|
   same As |*|, but only matches zero or one. It is a GNU extension


I wouldn't expect it to work. See below.

By the way, you don't need to escape the brackets or the question mark:

[steve@sylar ~]$ echo 'Some \"text"' | sed -re 's/([^\\])?"/\1\\"/g'
Some \\"text\"



For me the equivalent python regex is buggy: r'([^\\])?"', r'\1\\"'


No it is not.



Yes I know, see my latest post in detail I already found the solution. I 
put it again the solution below:


#Found the solution: '?' needs to be inside parenthesis (saved pattern) 
because outside we don't know if the saved match argument

#will exist or not namely '\1'.

>>> re.subn(r'([^\\]?)"', r'\1\\"', expression)

(' \\"\\" ', 2)


The pattern you are matching does not do what you think it does. "Zero 
or one of not-backslash, followed by a quote" will match a single 
quote *regardless* of what is before it. This is true even in sed, as 
you can see above, your sed regex matches both quotes.


\" will match, because the regular expression will match zero 
characters, followed by a quote. So the regex is correct.


>>> match = r'[^\\]?"'  # zero or one not-backslash followed by quote
>>> re.search(match, r'aaa\"aaa').group()
'"'

Now watch what happens when you call re.sub:


>>> match = r'([^\\])?"'  # group 1 equals a single non-backslash
>>> replace = r'\1\\"'  # group 1 followed by \ followed by "
>>> re.sub(match, replace, '')  # no matches
''
>>> re.sub(match, replace, 'aa"aa')  # one match
'aa\\"aa'
>>> re.sub(match, replace, '"')  # one match, but there's no group 1
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.1/re.py", line 166, in sub
return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/local/lib/python3.1/re.py", line 303, in filter
return sre_parse.expand_template(template, match)
  File "/usr/local/lib/python3.1/sre_parse.

Re: [Tutor] RE module is working ?

2011-02-04 Thread Karim

On 02/04/2011 11:26 AM, Peter Otten wrote:

Karim wrote:


That is not the thing I want. I want to escape any " which are not
already escaped.
The sed regex  '/\([^\\]\)\?"/\1\\"/g' is exactly what I need (I have
made regex on unix since 15 years).

Can the backslash be escaped, too? If so I don't think your regex does what
you think it does.

r'\\\"' # escaped \ followed by escaped "

should not be altered, but:

$ echo '\\\"' | sed 's/\([^\\]\)\?"/\1\\"/g'
" # two escaped \ folloed by a " that is not escaped




By the way you are right:

I changed an I added sed command for the ' "" ':

karim@Requiem4Dream:~$ echo 'prima " "' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \" \"
karim@Requiem4Dream:~$ echo 'prima ""' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \"\"
karim@Requiem4Dream:~$ echo 'prima "Ich Karim"' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \"Ich Karim\"
karim@Requiem4Dream:~$ echo 'prima "Ich Karim"' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \"Ich Karim\"

Sorry, for the incomplete command. You pointed it out, many thanks Peter!

Regards
Karim



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


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


Re: [Tutor] RE module is working ?

2011-02-04 Thread Karim


By the way with your helper function algorithm Steven and Peter comments 
you made me think of this change:


karim@Requiem4Dream:~$ echo 'prima " "' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \" \"
karim@Requiem4Dream:~$ echo 'prima ""' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \"\"
karim@Requiem4Dream:~$ echo 'prima "Ich Karim"' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \"Ich Karim\"
karim@Requiem4Dream:~$ echo 'prima "Ich Karim"' | sed -e 
's/""/\\"\\"/g;s/\([^\]\)"/\1\\"/g'

prima \"Ich Karim\"

Regards
Karim


On 02/04/2011 08:07 PM, Karim wrote:

On 02/04/2011 02:36 AM, Steven D'Aprano wrote:

Karim wrote:


*Indeed what's the matter with RE module!?*

You should really fix the problem with your email program first;
Thunderbird issue with bold type (appears as stars) but I don't know 
how to fix it yet.


A man when to a doctor and said, "Doctor, every time I do this, it 
hurts. What should I do?"


The doctor replied, "Then stop doing that!"

:)


Yes this these words made me laugh. I will keep it in my funny box.




Don't add bold or any other formatting to things which should be 
program code. Even if it looks okay in *your* program, you don't know 
how it will look in other people's programs. If you need to draw 
attention to something in a line of code, add a comment, or talk 
about it in the surrounding text.



[...]
That is not the thing I want. I want to escape any " which are not 
already escaped.
The sed regex  '/\([^\\]\)\?"/\1\\"/g' is exactly what I need (I 
have made regex on unix since 15 years).


Mainly sed, awk and perl sometimes grep and egrep. I know this is the 
jungle.


Which regex? Perl regexes? sed or awk regexes? Extended regexes? GNU 
posix compliant regexes? grep or egrep regexes? They're all different.


In any case, I am sorry, I don't think your regex does what you say. 
When I try it, it doesn't work for me.


[steve@sylar ~]$ echo 'Some \"text"' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \\"text\"


I give you my word on this. Exact output I redid it:

#MY OS VERSION
karim@Requiem4Dream:~$ uname -a
Linux Requiem4Dream 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 
23:42:43 UTC 2011 x86_64 GNU/Linux

#MY SED VERSION
karim@Requiem4Dream:~$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE,

to the extent permitted by law.

GNU sed home page: .
General help using GNU software: .
E-mail bug reports to: .
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
#MY SED OUTPUT COMMAND:
karim@Requiem4Dream:~$  echo 'Some ""' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \"\"
# THIS IS WHAT I WANT 2 CONSECUTIVES IF THE FIRST ONE IS ALREADY 
ESCAPED I DON'T WANT TO ESCAPED IT TWICE.

karim@Requiem4Dream:~$ echo 'Some \""' | sed -e 's/\([^\\]\)\?"/\1\\"/g'
Some \"\"
# BY THE WAY THIS ONE WORKS:
karim@Requiem4Dream:~$ echo 'Some "text"' | sed -e 
's/\([^\\]\)\?"/\1\\"/g'

Some \"text\"
# BUT SURE NOT THIS ONE NOT COVERED BY MY REGEX (I KNOW IT AND WANT 
ORIGINALY TO COVER IT):
karim@Requiem4Dream:~$ echo 'Some \"text"' | sed -e 
's/\([^\\]\)\?"/\1\\"/g'

Some \\"text\"

By the way in all sed version I work with the '?'  (0 or one match) 
should be escaped that's the reason I have '\?' same thing with save 
'\(' and '\)' to store value. In perl, grep you don't need to escape.


# SAMPLE FROM http://www.gnu.org/software/sed/manual/sed.html

|\+|
same As |*|, but matches one or more. It is a GNU extension.
|\?|
same As |*|, but only matches zero or one. It is a GNU extension


I wouldn't expect it to work. See below.

By the way, you don't need to escape the brackets or the question mark:

[steve@sylar ~]$ echo 'Some \"text"' | sed -re 's/([^\\])?"/\1\\"/g'
Some \\"text\"



For me the equivalent python regex is buggy: r'([^\\])?"', r'\1\\"'


No it is not.



Yes I know, see my latest post in detail I already found the solution. 
I put it again the solution below:


#Found the solution: '?' needs to be inside parenthesis (saved 
pattern) because outside we don't know if the saved match argument

#will exist or not namely '\1'.

>>> re.subn(r'([^\\]?)"', r'\1\\"', expression)

(' \\"\\" ', 2)


The pattern you are matching does not do what you think it does. 
"Zero or one of not-backslash, followed by a quote" will match a 
single quote *regardless* of what is before it. This is true even in 
sed, as you can see above, your sed regex matches both quotes.


\" will match, because the regular expression will match zero 
characters, followed by a quote. So the regex is correct.


>>> match = r'[^\\]?"'  # zero or one not-backslash followed by quote
>>> re.search(match, r'aaa\"aaa').group()
'"'

Now watch what happens when you call re.sub:


>>> match = r'([^\\])?"'  # group 1 equals a single n