[FAQTS] Python Knowledge Base Update -- July 19th, 2000

Fiona Czuczman fiona at sitegnome.com
Wed Jul 19 09:17:35 EDT 2000


Hello All,

Yet another instalment of entries into http://python.faqts.com

regards,

Fiona Czuczman


## New Entries #################################################


-------------------------------------------------------------
grouping in regular expressions
http://www.faqts.com/knowledge-base/view.phtml/aid/4860
-------------------------------------------------------------
Fiona Czuczman
Wolfgang Strobl

Problem:

I am finding it difficult to match the ip address in the
following line using re module.

"       inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0"

Solution:

>>> import re
>>> s="       inet addr:192.168.0.1  Bcast:192.168.0.255 
Mask:255.255.255.0"
>>> p=re.compile('\s+inet addr:(\d+\.\d+\.\d+\.\d+)\s+.*')
>>> p.match(s).groups()
    ('192.168.0.1',)
>>>


-------------------------------------------------------------
How can I make it so the interpreter defaults to hexidecimal output?
http://www.faqts.com/knowledge-base/view.phtml/aid/4863
-------------------------------------------------------------
Fiona Czuczman
Jürgen Hermann

Problem:

what I mean is the following:

>>> i = 0xa
>>> print i
10
>>> i
10

what I really want is to get the following:

>>> i = 0xa
>>> i
0xa

Solution:

Then write a HexInt class that overloads __str__ and __repr__,
and use i = HexInt(0xa). I see no other way.


-------------------------------------------------------------
Does Python support any sort of floating point "not a number" (e.g. IEEE infinity)?
http://www.faqts.com/knowledge-base/view.phtml/aid/4864
-------------------------------------------------------------
Fiona Czuczman
Huaiyu Zhu, Warren Focke

This seems to work and is used in MatPy:

Inf = inf = 1e300**2
NaN = nan = inf - inf

-----------

Python floats inherit the behavior of your C platform's doubles, so on
an IEEE-754 system, they will act fairly IEEE-like, as Huaiyu
demonstrates:

>Inf = inf = 1e300**2
>NaN = nan = inf - inf

However, according to a recent post, the OpenVMS port of python uses
VAX-format math[1], which has less exponent range than IEEE.  I think
that that expression for Inf will not parse there.  I don't even know
if VAXen have Inf or NaN.

-----------

According to discussions on comp.lang.python, there is no universal way 
to treat NaN, but the following codes work on Windows (NT and 98), 
Solaris (2.7 with gcc) and Linux (RH6.1 with egcs).

It core dumps on Tru64 Unix on Compaq Alpha [Solution: When using DEC
(Compaq) compiler to build Python try using -ieee switch].

Supposedly it should work on any machine using IEEE arithmetics.

An alternative that sometimes works is
NaN = math.exp(1000) / math.exp(1000)


## Edited Entries ##############################################


-------------------------------------------------------------
How can I catch an error in re.compile?
How can I test for a bad regular expression?
http://www.faqts.com/knowledge-base/view.phtml/aid/1357
-------------------------------------------------------------
Nathan Wallace, Fiona Czuczman
Fredrik Lundh

Why not just compile a bad expression and see what happens? ;-)

>>> re.compile("(")
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "C:\py152\lib\re.py", line 79, in compile
    code=pcre_compile(pattern, flags, groupindex)
pcre.error: ('missing )', 1)

(to catch the exception, use try/except re.error)


-------------------------------------------------------------
Can a class have two __init__ functions with different argument lists?
http://www.faqts.com/knowledge-base/view.phtml/aid/4799
-------------------------------------------------------------
Mark Tomko, Michael Hudson


No, not really.

There are workarounds; the lowest-tech is something along the lines of:

class C:
    def __init__(*args):
        if len(args) == 1:
            apply(args[0].init0,args)
        else:
            apply(args[0].init1,args)
    def init0(self):
        print "no args!"
    def init1(self):
        print "one arg!"

Also, in some circumstances it may be possible to create the instance
using the new module and then choose which initialization function to
call.  This is definitely an experts only solution...  I have used it in
eg:

http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/bytecodehacks/bytecodehacks/code_editor.py?rev=1.7.2.1&content-type=text/x-cvsweb-markup&cvsroot=bytecodehacks

(nice long url, sorry)

about 40 lines in.


-------------------------------------------------------------
Could someone give me a simple explanation of regular expressions?
http://www.faqts.com/knowledge-base/view.phtml/aid/3870
-------------------------------------------------------------
Fiona Czuczman
Andrew Kuchling

Regular expressions are a mini-language for defining a certain set 
of strings; the re module then takes a regular expression and tells you
if a given string matches the expression.  For example, [abc] means
'a', 'b', or 'c'; \bA.*?\b means a word beginning with 'A' (\b is a
special sequence meaning "match a word boundary").  As you can see,
regular expressions are concise but can easily be obscure.

Regular expressions will practically never be faster than a simple
string.find, so if you're searching for a fixed string, don't use
regular expressions; their extra expressive power costs you in
performance.  But they can do things that aren't possible with the
string module alone, and would require several lines of Python code 
to implement.

Consult the Library Reference, or the Regular Expression HOWTO for
more info.  (http://www.python.org/doc/current/lib/module-re.html,
http://www.python.org/doc/howto/regex/)







More information about the Python-list mailing list