Re: Can I overload the compare (cmp()) function for a Lists ([]) index function?

2007-09-28 Thread irstas
On Sep 28, 8:30 pm, xkenneth <[EMAIL PROTECTED]> wrote:
> Looking to do something similair. I'm working with alot of timestamps
> and if they're within a couple seconds I need them to be indexed and
> removed from a list.
> Is there any possible way to index with a custom cmp() function?
>
> I assume it would be something like...
>
> list.index(something,mycmp)
>
> Thanks!

Wouldn't it be enough to get the items that are "within a couple of
seconds" out of the list and into another list. Then you can process
the other list however you want. Like this:

 def isNew(x):
 return x < 5

 data = range(20)
 print data
 out, data = filter(isNew, data), filter(lambda x: not isNew(x), data)
 print out, data

Why do you want to use 'index'?

Your suggestion "list.index.__cmp__ = mycmp" certainly doesn't do
anything good. In fact, it just fails because the assignment is
illegal.. I don't think any documentation suggests doing that, so why
are you even trying to do that? It's just not a good idea to invent
semantics and hope that they work, in general.

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


Re: Regex doesn't support MULTILINE?

2007-07-22 Thread irstas
On Jul 22, 7:56 am, Gilles Ganault <[EMAIL PROTECTED]> wrote:
> On Sat, 21 Jul 2007 22:18:56 -0400, Carsten Haese
>
> <[EMAIL PROTECTED]> wrote:
> >That's your problem right there. RE is not the right tool for that job.
> >Use an actual HTML parser such as BeautifulSoup
>
> Thanks a lot for the tip. I tried it, and it does look interesting,
> although I've been unsuccessful using a regex with BS to find all
> occurences of the pattern.
>
> Incidently, as far as using Re alone is concerned, it appears that
> re.MULTILINE isn't enough to get Re to include newlines: re.DOTLINE
> must be added.
>
> Problem is, when I add re.DOTLINE, the search takes less than a second
> for a 500KB file... and about 1mn30 for a file that's 1MB, with both
> files holding similar contents.
>
> Why such a huge difference in performance?
>
> pattern = "(\d+:\d+).*?"

That .*? can really slow it down if the following pattern
can't be found. It may end up looking until the end of the file for
proper continuation of the pattern and fail, and then start again.
Without DOTALL it would only look until the end of the line so
performance would stay bearable. Your 1.5MB file might have for
example
'13:34< /span>'*1 as its contents. Because
the < /span> doesn't match , it would end up looking till
the end of the file for  and not finding it. And then move
on to the next occurence of '),
you could maybe use negated char range:

"(\d+:\d+)[^<]*"

This pattern should be very fast for all inputs because the [^<]*
can't
match stuff indefinitely until the end of the file - only until the
next HTML element comes around. Or if you don't care about anything
but
those numbers, you should just match this:

"(\d+:\d+)"

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


Re: Should: "for k,v in **dictionary_instance" work?

2007-06-16 Thread irstas
On Jun 16, 5:27 am, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> Currently, *t and **d are syntax errors outside of function calls and
> definitions. (Any other places?) But if they were allowed, what would they
> mean?

Actually since you asked, I had to try this out

x = range(10)
a, *b = x

I would take it to mean a = x[0], b = x[1:] (or more precicely, it
would
probaby have to use the iter protocol). Unfortunately it fails..
Since these are roughly equivalent:

t = 1, (2, 3)

a, (b, c) = t

def foo(a, (b, c)): print a,b,c
foo(*t)

I would like to see the assignment enhanced so that this works too

t = 1, (2, 3), 4, 5

a, (b, c), *d = t

def foo(a, (b, c), *d): print a,b,c,d
foo(*t)

The latter one with function definition works fine, the former
assignment doesn't (SyntaxError). It's a bit surprising. Is there
any reason not to support this syntax? It would be neatly symmetric
in my opinion, so one wouldn't have to learn anything new. In fact,
this addition would reduce the amount of things one must learn
(unexpected behaviour).

def rawr((a, b, (c, *d)), (e, *f), **g): pass

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


Re: c[:]()

2007-05-31 Thread irstas
On May 31, 5:59 pm, "Warren Stringer" <[EMAIL PROTECTED]> wrote:
> Still  I prefer
>
> funcs[:]()
>
> Because, I can describe it in English as "call everything that funcs has"

Wouldn't funcs() be even better? It's shorter, and it seems like
that's the only thing you're striving for. Why do you want to put the
[:] in there all the time?

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


Re: c[:]()

2007-05-30 Thread irstas
On May 31, 12:31 am, "Warren Stringer" <[EMAIL PROTECTED]> wrote:
> This is inconsistent:
>
> why does c[:][0]() work but c[:]() does not?
> Why does c[0]() has exactly the same results as c[:][0]() ?
> Moreover, c[:][0]() implies that a slice was invoked

It's not inconsistent, but [:] probably does something different than
you think it does. All it does is create a copy (not in general, but
at least if c is a list or a tuple). Since in your example c is a
tuple and tuples are immutable, making a copy of it is essentially
useless. Why not just use the original? I.e. instead of c[:] you could
just write c. That's why c[:][0]() has exactly the same effect as c[0]
(), although the former is likely to be slightly slower.

c[:]() tries to call the copied tuple. Tuples aren't callable.
c[:][0]() calls the first element in the copied tuple, and that
element happens to be callable.

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


Re: Inheriting from Python list object(type?)

2007-05-24 Thread irstas
On May 23, 10:07 pm, Mangabasi <[EMAIL PROTECTED]> wrote:
> This is the winner:
>
> class Point(list):
> def __init__(self, x, y, z = 1):
> super(Point, self).__init__([x, y, z])
> self.x = x
> self.y = y
> self.z = z
>
> def __getattr__(self, name):
> if name == 'x':
> return self[0]
> elif name == 'y':
> return self[1]
> elif name == 'z':
> return self[2]
> else:
> return self.__dict__[name]
>
> def __setattr__(self, name, value):
> if name == 'x':
> self[0] = value
> elif name == 'y':
> self[1] = value
> elif name == 'z':
> self[2] = value
> else:
> self.__dict__[name] = value


Inheritation is an "is a" relation. Point, however, is not a list.
This adds some weird behaviour to Point:

p = Point(1,2,3)
p.append(4)
print p[3]

That makes no sense but your Point implementation allows it. This
is probably even more surprasing behaviour:

p = Point(1,2,3) + Point(4,5,6)
print p

One might expect the points to be added to Point(5,7,9), not
into a list containing [1,2,3,4,5,6].

I'd suggest you don't inherit Point from anything, and just add
an __iter__ member function that iterates through x,y and z. E.g.

def __iter__(self):
yield self.x
yield self.y
yield self.z

Then you can convert a Point p to a list by doing list(p). Or to
a tuple with tuple(p). __array__ could also be implemented for
convenince with numpy (if I understood that correctly).

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


Re: block scope?

2007-04-07 Thread irstas
On Apr 7, 6:48 am, James Stroud <[EMAIL PROTECTED]> wrote:
> Neal Becker wrote:
> > One thing I sometimes miss, which is common in some other languages (c++),
> > is idea of block scope.  It would be useful to have variables that did not
> > outlive their block, primarily to avoid name clashes.  This also leads to
> > more readable code.  I wonder if this has been discussed?
>
> Probably, with good code, block scope would be overkill, except that I
> would welcome list comprehensions to have a new scope:

Generator expressions have a new scope, and in Python 3.0 list
comprehensions will have one as well (according to 
http://www.python.org/dev/peps/pep-0289/
). It's a fix that might break existing code so it couldn't be
introduced in "minor" versions like 2.4 and 2.5.

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


Re: BeautifulSoup vs. real-world HTML comments

2007-04-04 Thread irstas

Carl Banks wrote:
> On Apr 4, 2:08 pm, John Nagle <[EMAIL PROTECTED]> wrote:
> > The syntax that browsers understand as HTML comments is much less
> > restrictive than what BeautifulSoup understands.  I keep running into
> > sites with formally incorrect HTML comments which are parsed happily
> > by browsers.  Here's yet another example, this one from
> > "http://www.webdirectory.com";.  The page starts like this:
> >
> > 
> > 
> > 
> >
> > 
> > Environment Web Directory
> >
> > Those are, of course, invalid HTML comments. But Firefox, IE, etc. handle 
> > them
> > without problems.
> >
> > BeautifulSoup can't parse this page usefully at all.
> > It treats the entire page as a text chunk.  It's actually
> > HTMLParser that parses comments, so this is really an HTMLParser
> > level problem.
>
> Google for a program called "tidy".  Install it, and run it as a
> filter on any HTML you download.  "tidy" has invested in it quite a
> bit of work understanding common bad HTML and how browsers deal with
> it.  It would be pointless to duplicate that work in the Python
> standard library; let HTMLParser be small and tight, and outsource the
> handling of floozy input to a dedicated program.

That's a good suggestion. In fact it looks like there's a Python API
for tidy:
http://utidylib.berlios.de/
Tried it, seems to get rid of  just fine.

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


Re: how to remove multiple occurrences of a string within a list?

2007-04-03 Thread irstas
On Apr 3, 10:13 pm, "bahoo" <[EMAIL PROTECTED]> wrote:
> On Apr 3, 2:31 pm, "Matimus" <[EMAIL PROTECTED]> wrote:
>
> > It depends on your application, but a 'set' might really be what you
> > want, as opposed to a list.
>
> > >>> s = set(["0024","haha","0024"])
> > >>> s
>
> > set(["0024","haha"])>>> s.remove("0024")
> > >>> s
>
> > set(["haha"])
>
> This sounds cool.
> But is there a command I can convert the "set" back to a "list"?

Beware that converting a list to set and then back to list won't
preserve the order of the items, because the set-type loses the order.

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


Re: Retrieve an item from a dictionary using an arbitrary object as the key

2007-04-03 Thread irstas
On Apr 3, 7:26 pm, "abcd" <[EMAIL PROTECTED]> wrote:
> Hi,
> I have a class such as,
>
> class Type:
> def __init__(self, val):
> self.val = val
>
> class Person:
> def __init__(self, name, age):
> self.name = name
> self.age = age
>
> So I have a dictionary which maps an instance of Type to an instance
> of Person.  Now I need to retrieve a particular Person given a Type
> object.  What method in Type do I need to implement to allow it to be
> retrieved?
>
> For example (this is just an example):
>
> t = Type(19)
> p = Person("bob", 99)
>
> x = {t : p}
>
> 
>
> def getPerson(val):
> return x[Type(val)]
>
> getPerson(19)  should return me the Person with name "bob" and age
> 99.  I am thinking there is some method that is used by the dictionary
> to know if the key exists, just not sure which.
>
> thanks

You'll need __eq__ for testing if two objects are equivalent, and
__hash__ for calculating object's hash value.

class Type:
def __init__(self, val):
self.val = val

def __eq__(self, other):
return self.val == other.val

def __hash__(self):
return hash(self.val)


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


Re: Stack experiment

2007-04-03 Thread irstas
On Apr 3, 7:14 pm, "Richard Brodie" <[EMAIL PROTECTED]> wrote:
> <[EMAIL PROTECTED]> wrote in message
> >There may be something wrong with the "re" code in your example,
> >but I don't know enough about that to help in that area.
>
> There is a stray leading space in it.

Nah, I'd say there's a stray ([^0-9]) after the space. With just space
in it (same as basic split) one would get the tokens 56, 47, +, 2 and
*. Without the space one would get: ['56', ' ', '47', ' + ', '2', '
*', '']

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


Re: How to have a list of lists (or array of lists)

2007-04-03 Thread irstas
On Apr 3, 7:12 pm, "bahoo" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I want to have many lists, such as list0, list1, list2, ..., each one
> holding different number of items.
> Is there something like
> list[0]
> list[1]
> list[2]
>
> so that I can iterate through this list of lists?
>
> Thanks!
> bahoo

listOfLists = [[1,2], [5,7,9], [4,2,1,4,6,6]]

No problem there. The lists can contain any objects, including lists.

for x in listOfLists:
print 'list:',
for y in x:
print y,
print

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


Re: Stack experiment

2007-04-03 Thread irstas

[EMAIL PROTECTED] wrote:
> Hi! Im new to Python and doing exercise found from internet. It is
> supposed to evaluate expression given with postfix operator using
> Stack() class.
>
> class Stack:
>  def __init__(self):
>  self.items = []
>
>  def push(self, item):
>  self.items.append(item)
>
>  def pop(self):
>  return self.items.pop()
>
>  def isEmpty(self):
>  return (self.items == [])
>
> def evaluatePostfix(expr):
>  import re
>  tokenList = re.split(" ([^0-9])", expr)
>  stack = Stack()
>  for token in tokenList:
>  if token == '' or token == ' ':
>  continue
>  if token == '+':
>  sum = stack.pop() + stack.pop()
>  stack.push(sum)
>  elif token == '*':
>  product = stack.pop() * stack.pop()
>  stack.push(product)
>  else:
>  stack.push(int(token))
>  return stack.pop()
>
> print evaluatePostfix("56 47 + 2 *")
>
> Errormsg:
> Traceback (most recent call last):
>File "C:\*\postfix1.py", line 31, in 
>  print evaluatePostfix("56 47 + 2 *")
>File "C:\*\postfix1.py", line 28, in evaluatePostfix
>  stack.push(int(token))
> ValueError: invalid literal for int() with base 10: '56 47'
>
> How can I avoid the error and get desired result?

Obviously your tokens are wrong since one of the tokens is '56 47' as
the error message indicates.

import re
print re.split(" ([^0-9])", "56 47 + 2 *")

It looks like you'd just want expr.split().

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


Re: numpy performance and list comprehension

2007-04-03 Thread irstas
On Apr 3, 5:42 pm, "TG" <[EMAIL PROTECTED]> wrote:
> Hi there.
>
> Reading the page on python performance (http://scipy.org/PerformancePython
> ) made me realize that I can achieve tremendous code acceleration with
> numpy just by using "u[:,:]" kind of syntax the clever way.
>
> Here is a little problem (Oja's rule of synaptic plasticity)
>
> * W is a matrix containing the weights of connections between elements
> i
> and j
> * V is an array containing the values of elements
>
> I want to make W evolve with this rule :
>
> dW[i,j] / dt = alpha * (V[i] * V[j] - W[i,j] * V[i]^2)
>
> (don't pay attention to the derivate and stuff)
>
> So, how would you write it in this nifty clever way ?
>
> As a begining I wrote this :
>
>   W += V.flatten().reshape((V.size,1)) *
> V.flatten().reshape((1,V.size))
>
> But it is not complete and, I guess, not efficient.

alpha * (V[i] * V[j] - W[i,j] * V[i]^2) =
alpha * V[i] * (V[j] - W[i,j] * V[i])

Assuming that V is a column vector, you could do it like this:

V = array([[5],[3],[7]])
W = array([[1,5,3],[2,2,7],[3,9,8]])
W += alpha * V * (transpose(V) - W * V)

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


Re: Parsing Problems

2007-04-03 Thread irstas

[EMAIL PROTECTED] wrote:
> Hi,
>
>  I have just started learning python.I need to parse an XML file
> and present the contents in a particular format.The format is called
> as "ini" file.I have written some code.A section of the format needs
> the data to be present in format as given below:
>
> [Services]
> supported=0x10,0x1A,0x3B,0x20,0x27,0x28,0x34,0x36,0x3E,0xA2,0xA5,0x2D,
> 0x22,0xA9,0x04,0xAA,0xAE
>
> My code for this section parses the xml file and gives :
> [Services]
> Supported=['0x3e', '0x28', '0x3b', '0x22', '0x20', '0x27', '0xaa',
> '0x10', '0xae', '0x34', '0x36', '0x2d', '0xa9', '0xa5', '0x4', '0xa2',
> '0x1a']
>
> {forget the numericals matching}.As you can see there
> are single quotes around numericals ,which is not desired .I think the
> problem lies in me using a list for storing and later printing out
> values.I have attached few lines of code,not sure how clear it can be
> to you:
>
> for l in range(0,len(ser_par),
> 1):
> if
> ser_par[l].getAttribute('Semantics')==u'serviceId':
> if
> tag_exists(ser_par[l].childNodes,'VALUE'):
> val =
> ser_par[l].getElementsByTagName('VALUE')
> value =
> str(val[0].getAttribute('value'))
> valu = hex(int(value))
> rval.append(valu)
> ser_par_num = ser_par_num + 1
> prim_num = prim_num + 1
>
> service_num = service_num + 1
> variant_num = variant_num + 1
> ser = list(set(rval))
>
> print "\n[Services]"
> print "Supported="+str(ser)
>
>   How can i get rid of those single quotes.
>
>  Thanking you
>
> shakeel

Hi,
Instead of using str(ser), you should create a string which combines
the individual strings in ser, separated by commas. Luckily for you,
there's a string method does just that: join.
http://docs.python.org/lib/string-methods.html

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


Re: How do you escape % when formatting a string?

2007-04-02 Thread irstas
On Apr 2, 10:52 pm, [EMAIL PROTECTED] wrote:
> On Apr 2, 10:50 pm, "erikcw" <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I'm trying to format a string like so:
>
> > string = "You have a 75% chance of success with %s, don't use %s" %(a,
> > b)
>
> > This gives me:
> > TypeError: not enough arguments for format string
>
> > I've tried 75\%, but that doesn't seem to help.  What am I missing?
>
> > Thanks!
> > Erik
>
> Use %%.
>
> >>> "%d%%" % 75
>
> "75"

Oups, I made a typo in the result which should've been "75%"

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


Re: How do you escape % when formatting a string?

2007-04-02 Thread irstas
On Apr 2, 10:50 pm, "erikcw" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm trying to format a string like so:
>
> string = "You have a 75% chance of success with %s, don't use %s" %(a,
> b)
>
> This gives me:
> TypeError: not enough arguments for format string
>
> I've tried 75\%, but that doesn't seem to help.  What am I missing?
>
> Thanks!
> Erik

Use %%.

>>> "%d%%" % 75
"75"


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


Re: RSS feed parser

2007-04-02 Thread irstas
On Apr 2, 10:20 pm, Florian Lindner <[EMAIL PROTECTED]> wrote:
> Some of the question I have but found answered nowhere:
>
> I have a feedparser object that was created from a string. How can I trigger
> a update (from a new string) but the feedparser should treat the new string
> like the same feed (thus setting feed.updated etc.).

Hmm. Do you mean that the feed object should stay the same? Like the
difference between "a = [1,2,3]; a = [1,2,3]+[4]" and "a = [1,2,3];
a.append(4)"? I glanced at the parse function in the source code and
it looks like it's not directly possible. You could modify it so that
the "result" dictionary is optionally given as an argument, so when
updating you'd do: feedparser.parse(string, oldFeed). You'd also have
to clear the oldFeed object before update.

But you might also be able to solve the problem by using an additional
layer of indirection. Instead of passing around the "feed" object,
you'd pass around a proxy object like this:

class Empty: pass
proxy = Empty()
proxy.feed = feedparser.parse(string)
storeProxyForLaterUse(proxy)
proxy.feed = feedparser.parse(string2)
useStoredProxy() #this would use the updated feed through the proxy

Then just use proxy.feed.updated everywhere instead of directly
feed.updated. A smarter proxy would automatically translate
proxy.updated into proxy.feed.updated so usage would stay as simple as
without the proxy. Doing this is quite easy in Python (search for
__getattr__ examples).

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


Re: Clean "Durty" strings

2007-04-02 Thread irstas
On Apr 2, 10:08 pm, Michael Hoffman <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > But it could be that he just wants all HTML tags to disappear, like in
> > his example. A code like this might be sufficient then: re.sub(r'<[^>]
> > +>', '', s).
>
> Won't work for, say, this:
>
> 
> --
> Michael Hoffman

True, but is that legal? I think the alt attribute needs to use <
and >. Although I know what you're going to reply. That
BeautifulSoup probably parses it even if it's invalid HTML. And I'd
say that I agree, using BeautifulSoup is a better solution than custom
regexps.

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


Re: Math with unicode strings?

2007-04-02 Thread irstas
On Apr 2, 10:09 pm, "erikcw" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm parsing xml data with xml.sax and I need to perform some
> arithmetic on some of the xml attributes.  The problem is they are all
> being "extracted" as unicode strings, so whenever I try to perform
> math operations on the variables, I get this error:
>
> cr[0] = data['cls1']/data['ims1'];
> TypeError: unsupported operand type(s) for /: 'unicode' and 'unicode'
>
> What do I need to do to extract the intergers from these unicode
> strings (or better yet, parse them as intergers in the first place.).
> I'm using the SAX method attrs.get('cls1',"") to parse the xml.  Can I
> "cast" the string into an interger?
>
> Thanks!
> Erik

int(u'123') == 123
float(u'123') == 123.0

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


Re: RSS feed parser

2007-04-02 Thread irstas
On Apr 2, 7:22 pm, Florian Lindner <[EMAIL PROTECTED]> wrote:
> Hello,
> I'm looking for python RSS feed parser library. 
> Feedparserhttp://feedparser.org/does not seem to maintained anymore.
>
> What alternatives are recommendable?
>
> Thanks,
>
> Florian

Well, even if it's not maintained anymore (where does it say that?),
it works fine and the API is great. Although of course I do realize
that when a new version of RSS appears, feedparser won't be able to
support it unless someone updates it. But RSS 2.0 appeared already in
2002 and no new versions have come since. So I wouldn't worry too much
about new RSSs popping up every month. Maybe the feedparser code
hasn't been updated in a while because it's already perfect and
there's nothing to add to it?-)

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


Re: Clean "Durty" strings

2007-04-02 Thread irstas
On Apr 2, 4:05 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> > If the OP is constrained to standard libraries, then it may be a
> > question of defining what should be done more clearly. The extraneous
> > spaces can be removed by tokenizing the string and rejoining the
> > tokens. Replacing portions of a string with equivalents is standard
> > stuff. It might be preferable to create a function that will accept
> > lists of from and to strings and translate the entire string by
> > successively applying the replacements. From what I've seen so far,
> > that would be all the OP needs for this task. It might take a half-
> > dozen lines of code, plus the from/to table definition.
>
> The OP had -tags in his text. Which is _more_ than a half dozen lines of
> code to clean up. Because your simple replacement-approach won't help here:
>
> foo  bar 
>
> Which is perfectly legal HTML, but nasty to parse.
>
> Diez

But it could be that he just wants all HTML tags to disappear, like in
his example. A code like this might be sufficient then: re.sub(r'<[^>]
+>', '', s). For whitespace, re.sub(r'\s+', ' ', s). For XML
characters like é, re.sub(r'&(\w+);', lambda mo:
unichr(htmlentitydefs.name2codepoint[mo.group(1)]), s) and
re.sub(r'&#(\d+);', lambda mo: unichr(int(mo.group(1))), s). That's it
pretty much.

I'd like to see how this transformation can be done with
BeautifulSoup. Well, the last two regexps can be replaced with this:

unicode(BeautifulStoneSoup(s,convertEntities=BeautifulStoneSoup.HTML_ENTITIES).contents[0])

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


Re: Cascading ifs

2007-04-02 Thread irstas
On Apr 2, 4:20 pm, Ernesto García García
<[EMAIL PROTECTED]> wrote:
> Hi experts,
>
> How would you do this without the more and more indenting cascade of ifs?:
>
> match = my_regex.search(line)
> if match:
>    doSomething(line)
> else:
>    match = my_regex2.search(line)
>    if match:
>      doSomething2(line)
>    else:
>      match = my_regex3.search(line)
>      if match:
>        doSomething3(line)
>
> etc.
>
> Thanks in advance and regards,
> Ernesto


You might be able to use "guard clauses":
http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html


match = my_regex.search(line)
if match:
   doSomething(line)
   return

match = my_regex2.search(line)
if match:
   doSomething2(line)
   return


But if all of the blocks contain the same code like they do in your
example, you're better off using the solutions provided by Wojciech
Muła and Duncan Booth, because they abstract away the repetition.

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

Re: Problem with global variables

2007-04-02 Thread irstas
On Apr 2, 5:29 pm, Bjoern Schliessmann  wrote:
> Laurent Pointal wrote:
> > And so the solution to add "global foo" before using it.
>
> Didn't you read his final question?
>
> | All of a sudden, tiny() can see the global variable "foo".  Very
> | confusing!  Why is it that tiny() sometimes can, and sometimes
> | can't, see the global variable "foo"?
>
> I have no explanation for this, but I'm interested in one, too.
>
> Regards,
>
> Björn

Laurent Pointal did explain this. "foo" is considered to be a local
variable (scope limited to the function) if it's being assigned to in
a function and there's no "global foo" statement. So even the "foo"
before the actual assignment will refer to the local variable. But the
local variable hasn't been assigned yet at that point and an exception
is thrown. Yes, one could have a perfectly working function, then add
an innocent-looking assignment statement at the very end of the
function, and suddenly you'd get an exception thrown at the beginning
of the function where that variable is used - Far away from the place
where the modification was made.

Why does it work like this? Couldn't it be somehow more uniform or
less surprising?

Well, if one had to define "global foo" even when one were to just
read the variable foo, that'd be a pain in the ass. You'd have to use
"global math" to use math module in a function and "global
anotherFunction" to call anotherFunction, and so on. There's plenty of
stuff in the global scope that you don't normally assign to.

Another option would be to scrap "global" keyword and let "foo = bar"
do an assignment to the global variable if a global variable named
"foo" exists, but create (or assign) to a function local variable if
it doesn't exist. That'd work but programming would be horrible. Now
you'd have to know all the symbols that exist at the global scope,
because if you accidentally accessed a global variable instead of
local, your function would probably have undesirable side-effects.

Now, name clashes could be solved with a naming convention (which
could be enforced at the language level). It could be possible that
all accesses to global scope would have to be prefixed by some symbol
like $. E.g. "$foo = 4; foo = 6" where former assigns to global foo,
latter to local foo. That's one option that might work and be somewhat
readable.. But not a good tradeoff IMO, considering how rarely global
assignment is needed and how often global variables are read.

A remaining option is to use different operator for assignment and
initialization. So you'd have to do e.g. "a := 4; a = 7". I'm not sure
Pythonistas would prefer this either, although many other languages
choose this route (through lets or declarations typically).

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


Re: reverse engineering Excel spreadsheet

2007-04-01 Thread irstas
On Apr 1, 6:59 pm, Duncan Smith <[EMAIL PROTECTED]> wrote:
> Hello,
>  I am currently implementing (mainly in Python) 'models' that come
> to me as Excel spreadsheets, with little additional information.  I am
> expected to use these models in a web application.  Some contain many
> worksheets and various macros.
>
> What I'd like to do is extract the data and business logic so that I can
> figure out exactly what these models actually do and code it up.  An
> obvious (I think) idea is to generate an acyclic graph of the cell
> dependencies so that I can identify which cells contain only data (no
> parents) and those that depend on other cells.  If I could also extract
> the relationships (functions), then I could feasibly produce something
> in pure Python that would mirror the functionality of the original
> spreadsheet (using e.g. Matplotlib for plots and more reliable RNGs /
> statistical functions).
>
> The final application will be running on a Linux server, but I can use a
> Windows box (i.e. win32all) for processing the spreadsheets (hopefully
> not manually).  Any advice on the feasibility of this, and how I might
> achieve it would be appreciated.
>
> I assume there are plenty of people who have a better knowledge of e.g.
> COM than I do.  I suppose an alternative would be to convert to Open
> Office and use PyUNO, but I have no experience with PyUNO and am not
> sure how much more reliable the statistical functions of Open Office
> are.  At the end of the day, the business logic will not generally be
> complex, it's extracting it from the spreadsheet that's awkward.  Any
> advice appreciated.  TIA.  Cheers.
>
> Duncan

I'm not sure I understood what kind of information you want to
get out of the Excel sheet, sorry. But I hope this'll get you started
(at least it has a few nice tokens that might help you in googling):

import win32com.client

class Excel:
def __init__(self, filename):
self.closed = True
self.xlApp =
win32com.client.dynamic.Dispatch('Excel.Application')
self.xlBook = self.xlApp.Workbooks.Open(filename)
self.closed = False

def sheet(self, sheetName):
return self.xlBook.Worksheets(sheetName)

def __del__(self):
if not self.closed:
self.close()

def close(self):
self.xlBook.Close(SaveChanges=1)
self.xlApp.Quit()
self.closed = True

excel = Excel('file.xls')
sheet = excel.sheet(1)
print sheet.Cells(6, 3)


I used it a few years ago to read and populate spreadsheet cells.

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


Re: Extract information from HTML table

2007-04-01 Thread irstas
On Apr 1, 3:13 pm, "Ulysse" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm trying to extract the data from HTML table. Here is the part of
> the HTML source :
>
> 
>
> Do you know the way to do it ?

Beautiful Soup is an easy way to parse HTML (that may be broken).
http://www.crummy.com/software/BeautifulSoup/

Here's a start of a parser for your HTML:

soup = BeautifulSoup(txt)
for tr in soup('tr'):
dateTd, textTd = tr('td')[1:]
print 'Date :', dateTd.contents[0].strip()
print textTd #element still needs parsing

where txt is the string in your message.

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


Re: re.findall() hangs in python

2007-04-01 Thread irstas
On Apr 1, 6:12 am, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> But when 'data' does not contain pattern, it just hangs at
> 're.findall'
>
> pattern = re.compile("(.*) re.S)

That pattern is just really slow to evaluate. What you want is
probably something more like this:

re.compile(r']*src\s*=\s*"([^"]*img[^"]*)"')

"dot" is usually not so great. Prefer "NOT end-character", like [^>]
or [^"].

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


Re: Any "consumer review generators" available?

2007-03-29 Thread irstas
On Mar 29, 9:19 pm, [EMAIL PROTECTED] wrote:
> I am looking for a fake consumer review generator that could generate 
> realistic looking reviews for any products, kind of like on amazon.com but 
> generated by Artificial Intelligence. Is there a package available in your 
> favorite programing language... thx alan

Python's standard module "ai" has a function called
"generateFakeReview". I believe it does what you want, but I haven't
used it myself.

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


Re: How can I get the content of a web site using http library

2007-03-29 Thread irstas
On Mar 29, 10:49 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> Thanks. Can you please tell me how can I do a Post form submission
> using the urlLib2 library? I look at your link, but i cant find an
> example there.
>
> Thank you.

http://www.python.org/doc/current/lib/module-urllib2.html

Look into urlopen's data parameter.

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


Re: How can I catch all exception in python?

2007-03-27 Thread irstas
On Mar 27, 9:15 pm, [EMAIL PROTECTED] wrote:
> Technically speaking, you can catch all errors as follows:
>
> try:
># do something
> except Exception, e:
>print e

That won't catch exceptions/errors that don't derive from
Exception class. For example a string won't be caught:

try:
   raise "foo"
except Exception, e:
   print e

But this will catch all exceptions:

try:
   raise "foo"
except:
   print sys.exc_info()

(there may be other ways I don't know of)

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


Re: SPE question

2007-03-27 Thread irstas
On Mar 27, 11:39 am, "alain" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Could someone tell me how to uninstall SPE under windows?
>
> Alain

Dunno about SPE, but most Python modules I've installed can
be uninstalled from control panel/add remove programs.

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


Re: functions, classes, bound, unbound?

2007-03-26 Thread irstas
On Mar 26, 7:15 pm, "7stud" <[EMAIL PROTECTED]> wrote:
> On Mar 25, 3:09 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
>
> > Here's another way of looking at it::
>
> >  >>> class Test(object):
> >  ... pass
> >  ...
> >  >>> def greet():
> >  ... print 'Hello'
> >  ...
> >>> Test.greet = greet
> >>> Test.greet
> >   
>
> Interesting.  After playing around with that example a bit and finally
> thinking I understood bound v. unbound, I found what appears to be an
> anomaly:
> 
> class Test(object):
> pass
>
> def greet(x):
> print "hello"
>
> Test.func = greet
> print Test.func
>
> t = Test()
> print t.func
>
> def sayBye(x):
> print "bye"
>
> t.bye = sayBye
> print t.bye
> output:
> 
> >
> 
>
> Why doesn't t.bye cause a method object to be created?

Because that would be bad and unexpected behaviour. I might want to
store callback functions to an object as attributes. It wouldn't make
sense for the functions to be bound to the container object when I'd
access them.

Of course, "Test.func = greet; print Test.func" is also "unexpected
behaviour" as Test.func is not same as Test.__dict__['func']. But it's
unexpected in a good way, because it facilitates the way classes are
usually used. The whole point of the magic is to make common idioms
simple.

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


Re: Zip file writing progress (callback proc)

2007-03-26 Thread irstas
On Mar 26, 4:41 pm, durumdara <[EMAIL PROTECTED]> wrote:
> Hi!
>
> I want to check my zip file writings.
> I need some callback procedure to show a progress bar.
> Can I do that?
> I don't want to modify the PyLib module to extend it, because if I get
> another py, the changes are lost.
> This happening too if I copy the zip module to modify it.
> Any solution?
>
> Thanks for it:
> dd

Would it be enough to show progress based on how many files have
been added to the zip? If you're zipping just one huge file, I'm
afraid you can't get a progress bar using the zipfile module.

If it's not necessary that the one file is a zip but simply
compressed,
you can use zlib or bz2 modules to compress incrementally:
http://www.python.org/doc/lib/node303.html


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


Re: Fortran vs Python - Newbie Question

2007-03-26 Thread irstas
On Mar 26, 4:47 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> > You can get the speed of fortran in Python by using libraries like
> > Numeric without losing the readability of Python.
>
> Can you back this up with some source??
> Chris

If you execute one command in Python which tells a super-optimized
Fortran-routine to do lots of work, the overhead of Python will be
neglible. That's all there is to it, no need for a source.

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


Re: with timeout(...):

2007-03-26 Thread irstas
On Mar 26, 3:16 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> But to make that work reliably, it has to be ensured that no sideeffects
> occur while being in some_long_running_stuff. which doesn't only extend to
> python itself, but also external modules and systems (file writing, network
> communications...). Which can't be done, unless you use a time-machine.

Hey hey, isn't the Python mantra that we're all adults here? It'd
be the programmers responsibility to use only code that has no
side effects. I certainly can ensure that no side-effects occur in the
following code: 1+2. I didn't even need a time machine to do that :P
Or the primitive could be implemented so that Python
throws a TimeoutException at the earliest opportunity. Then one
could write except-blocks which deal with rolling back any undesirable
side effects. (I'm not saying such timeout feature could be
implemented in
Python, but it could be made by modifying the CPython implementation)

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


Re: functions, classes, bound, unbound?

2007-03-25 Thread irstas
On Mar 25, 9:13 am, "7stud" <[EMAIL PROTECTED]> wrote:
> MyClass.someFunc
>
> Is there some other way to retrieve a user-defined function object
> from a class other than using the class name or an instance?

What Steven B. already said, MyClass.__dict__['someFunc'], is a
different way than MyClass.someFunc that produces different results.
Since the method is an attribute of a class, what other kinds of means
are you expecting to be possible?

You can use reflection to dig up MyClass-object from the module
dictionary if referring to object or class by name in the code is
something you want to get rid of.

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


Re: Idiom for running compiled python scripts?

2007-03-25 Thread irstas
On Mar 25, 2:46 am, Mark <[EMAIL PROTECTED]> wrote:
> On Sat, 24 Mar 2007 07:21:21 -0700, irstas wrote:
> > A simple implementation that "works":
>
> Not quite irstas BTW ..

I was expecting this, hence the quotes around 'works' :P.
Another known issue is that globals() dictionary is somewhat
different when ran with my module. You should probably
look into the runpy's source code for better environment
compability (Python25/Lib/runpy.py). Although no quarantees
that it'd help.

> Something about the environment is not quite the same. Any ideas?

What Gabriel said may be the correct fix (I actually couldn't
reproduce the
bug so I couldn't test), but here's another idea: You could wrap
your main method in a try-finally block:

def main():
try:
do things
finally:
remove temp files

The finally-block will be executed even if you call sys.exit
inside the try-block. This change will also make it possible
to invoke your script many times, with temp files getting deleted
after each invocation, should this ever be useful to you.
(well, not if you use sys.exit :P)

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


Re: Problem with game

2007-03-24 Thread irstas
On Mar 24, 5:20 pm, [EMAIL PROTECTED] wrote:
> >  File "Python-episode12-Zahlenraten.py", line 15
> > print "Die eingegebene Zahl ist kleiner als die generierte Zahl."
>
> IndentationError: expected an indented block

You should indent stuff inside if-statement deeper than the if itself.

I.e. NOT LIKE THIS:

if x==3:
print x


But like this:

if x==3:
print x


If in your editor it looks like you have done this, the error
is probably that you are mixing tabs and spaces as indentation
and your editor doesn't display tabs as 8 spaces. This leads to
an amazing visual illusion cunnigly developed to turn people away
from Python.


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


Re: creating "jsp-like" tool with python

2007-03-24 Thread irstas

jd wrote:
> I'd like to create a program that takes files with "jsp-like" markup
> and processes the embedded code (which would be python) to produce the
> output file.  There would be two kinds of sections in the markup file:
> python code to be evaluated, and python code that returns a value that
> would be inserted into the output.
>
> This seems like it would be straightforward in python, and maybe
> there's even a library that I could use for this, but as a newbie to
> Python, I don't know the landscape very well.  I am not looking for a
> big framework, just something small and simple that will do just this
> job.  Suggestions or pointers would be greatly appreciated.
>
> Thanks...
>
> -- jeff

Sadly, there's probably as many template languages as there are web
frameworks for Python.
Maybe even more. For example:

http://www.djangoproject.com/documentation/templates/
http://nick.borko.org/pse/index.html
http://www.turbogears.org/about/kid.html

Pick your poison.

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


Re: Idiom for running compiled python scripts?

2007-03-24 Thread irstas
On Mar 23, 9:30 am, Mark <[EMAIL PROTECTED]> wrote:
> A small script called "python_compile_and_run" in "pseudo" code:
>
> #!/usr/bin/env python
> import sys
>
> # Following is invalid syntax unfortunately :(
> from sys.argv[1].rstrip('.py') import main
>
> sys.argv = sys.argv[1:]
> if __name__ == "__main__":
> main()
>
> so I could just do a "python_compile_and_run myscript.py" and it would
> do what I want, i.e. run myscript.pyc if available and valid, generate
> and run it if necessary.

There's __import__ which allows you to do what you tried:

m = __import__(sys.argv[1].rstrip('.py'))

Also, rstrip doesn't work like you think it does.
'pyxyypp.py'.rstrip('.py') == 'pyx'

Answering also to your later message:

> So the general purpose invoking bash script e.g. "runpy" is merely something
like:

Curiously enough, Python 2.5 has a module called runpy:
http://docs.python.org/lib/module-runpy.html

which seems to almost do what you want. It doesn't compile the modules
but you could make a modification which does. The benefit over just
using __import__("module").main() would be
that your scripts wouldn't necessarily need a function called "main",
but would still work with scripts that use the __name__ == '__main__'
idiom.
A simple implementation that "works":


import imp, sys, os
c = sys.argv[1]
if not os.path.exists(c + 'c') or os.stat(c).st_mtime > os.stat(c +
'c').st_mtime:
import compiler
compiler.compileFile(c)
del sys.argv[0]
imp.load_compiled('__main__', c + 'c')


I timed it against running plain .py and running .pyc directly.
It seemed to be roughly on par with running .pyc directly, and about
18ms
faster than running .py. The file had 600 lines (21kb) of code.

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