Re: Beginner: Portable Python, BeautifulSoup & ScrapeNFeed

2009-04-17 Thread Brian
Was this code a complete waste of my time?

On Wed, Apr 15, 2009 at 1:09 AM, Brian  wrote:

> On Ubuntu:
>
> sudo apt-get install python-pyrss2gen python-beautifulsoup # download
> ScrapeNFeed
>
> Python:
> Not sure what's wrong with this but it's most of the code you'll need:
> ---
> from urllib import urlopen
> from BeautifulSoup import BeautifulSoup
> from PyRSS2Gen import RSSItem, Guid
> import ScrapeNFeed
> import re
>
> url='http://jobs.spb.ca.gov/exams_title.cfm'
> job_html = urlopen(url).read()
> job_soup = BeautifulSoup(job_html)
> jobs = job_soup.findAll('strong', text=re.compile('.*RESEARCH.*'))
>
> class JobFeed(ScrapeNFeed.ScrapedFeed):
> def HTML2RSS(self, headers, body):
> items = [RSSItem(title=job,
>  link=url,
>  description=job_soup.h2.string.strip())
>  for job in jobs]
>
> self.addRSSItems(jobs)
>
> JobFeed.load(job_soup.title.string.strip(),
>  url,
>  'jobs.rss',
>  'jobs.pickle',
>  managingEditor='',
>  )
>
>
>
>
>
>
>
> On Tue, Apr 14, 2009 at 4:17 PM, Joe Larson  wrote:
>
>> Hello list!
>>
>> I am a Python Beginner. I thought a good beginning project would be to use
>> the Portable Python environment http://www.portablepython.com/ with
>> Beautiful Soup http://www.crummy.com/software/BeautifulSoup/ and Scrape
>> 'N' Feed http://www.crummy.com/software/ScrapeNFeed/ to create and RSS
>> feed of this page http://jobs.spb.ca.gov/exams_title.cfm - ideally
>> filtering just for positions with the string 'Research Analyst'.
>>
>> In my day job I work on the Windows OS (hence the Portable Python) - at
>> home I use Ubuntu but also carry Portable Ubuntu as well.
>>
>> I just wanted to shoot this to the list - see if anyone had any
>> suggestions or tips. I'm reading O'Reilly's Learning Python and The Python
>> Tutorial, but it's still very challenging as this is my first programming
>> language. Thanks all! Sincerely ~ joelar
>>  --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: question about xrange performance

2009-04-17 Thread bearophileHUGS
Paul McGuire:

>xrange is not really intended for "in" testing,<

Let's add the semantic of a good and fast "in" to xrange (and to the
range of Python3). It hurts no one, allows for a natural idiom
(especially when you have a stride you don't want to re-invent the
logic of skipping absent numbers), and it's readable.

My xrange() I have written in D language has a fast OpIn_r() method. I
haven't found any bad side effect to such capability yet.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Domain Driven Design and Python

2009-04-17 Thread Terry Reedy

José María wrote:


DDD is more or less a methodology. I've used it with C# and I like it
very much.
The objetive of DDD is to create software that mirror the domain of
the problem,
you isolate this domain from the technical "problems" (persistence,
services,GUI).


I presume that many Python programmers do this without calling it 'DDD'.

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


Re: Overriding methods per-object

2009-04-17 Thread George Sakkis
On Apr 17, 9:22 pm, Pavel Panchekha  wrote:

> I've got an object which has a method, __nonzero__
> The problem is, that method is attached to that object not that class
>
> > a = GeneralTypeOfObject()
> > a.__nonzero__ = lambda: False
> > a.__nonzero__()
>
> False
>
> But:
>
> > bool(a)
>
> True
>
> What to do?


FYI this works as you expect if GeneralTypeOfObject is an old-style
class (i.e. does not inherit from object). If this feature is so more
important than all those that come with new-style classes, you have
control over the involved classes and don't care about Python 3.x
(where old-style classes are gone), you may choose to downgrade
GeneralTypeOfObject to old-style.

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


Re: unpythonic use of property()?

2009-04-17 Thread Carl Banks
On Apr 17, 4:00 pm, Scott David Daniels  wrote:
> Carl Banks wrote:
> > On Apr 17, 10:21 am, J Kenneth King  wrote:
> >> Consider:
>
> >> code:
> >> 
>
> >> class MyInterface(object):
>
> >>     def __get_id(self):
> >>         return self.__id
>
> >>     id = property(fget=__get_id)
>
> >>     def __init__(self, id, foo):
> >>         self.__id = id
> >>         self.foo = foo
>
> >> class MyInterface2(object):
>
> >>     def __init__(self, id, foo):
> >>         self._id = id
> >>         self.foo = foo
>
> >>     @property
> >>     def id(self):
> >>         return self._id
>
> ...
> >> I was recently informed that it was 'unpythonic' and have since been a
> >> little confused by the term. I've heard it bandied about before but
> >> never paid much attention. What is 'unpythonic'? What about this example
> >> is unpythonic?
>
> > There are different reasons someone might have said it.
> > ...
> > Some people think attribute name-mangling is unpythonic.  It's true
> > that people sometimes mistakenly treat it a solid information hiding
> > mechanism, but I wouldn't call its usage unpythonic when used as
> > intended: as a way to avoid name-collisions.  If you think it's
> > worthwhile to protect an attribute from being overwritten, you might
> > as well guard against accidental conflict with the underlying name.
>
> Here you are assuming that a user of your class could not possibly have a
> valid reason for getting to the underlying variable.  Don't make those
> decisions for someone else, in Python, "we are all adults here."

They can use the demangled name of the internal variable if they want
access to it.


> > Finally, some people think read-only attributes are unpythonic.  I
> > think that's ridiculous, although in general I'd advise against making
> > attributes read-only willy-nilly.  But there's a time and place for
> > it.
>
> Generally, properties are for doing some form of calculation, not
> for making things read-only.

That might be how properties are "generally" used, but if for some
reason I wanted a read-only attribute, that's how I'd do it.


[snip strawman stuff]
> It is not
> your job to protect those users who do not use your code properly from
> themselves; that way lies madness.

I'm sorry, but the universe is not as simple as you are making it out
to be.  Blanket statements like the one you just gave here are not
something that should ever be blindly adhered to.

If, in my judgment, users would be prone to overwrite one of my
attributes, and if I designed the system to rely on that attribute,
and if the results of changing it are bad enough, then by golly I'm
going to make the attribute harder than usual to modify.  And yes,
that is my job.

Users who want to change it anyway can curse me and then go demangle
the name themselves.


Carl Banks

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread norseman

Steven D'Aprano wrote:

On Fri, 17 Apr 2009 14:00:18 -0700, Mensanator wrote:


...(snip)
Pascal has GOTOs. People rarely used them, because even in the 1970s and 
80s they knew that unstructured gotos to arbitrary places was a terrible 
idea.




Even in primarily assembly only days that was true.

GOTO in Pascal required that you defined a label in your code, then you 
could jump to that label. You can't jump to arbitrary parts of the 
program, only within the current procedure.



===

"...only within the current procedure."   That was one of the "why 
Pascal didn't hang on" as long as it might have.  Another was it's COBAL 
structure in defining things. Just like today - the more typing the more 
errors, the longer to 'in service'.  Imitating Pascal's short jump only 
was Intel's lack of actual popularity among the Pro's of the day. Zilog 
had the better cpu, but Intel teamed with Gates, shoved interrupt only 
on everyone and the rest is history.  In fairness to Pascal, the 
enforcement of no "goto" helped force the mass of new programmers 
(desperately needed now that 'desktops' were here) to think about their 
strategy.  So did Ashton Tate's dBASE, which probably had more lines of 
code world wide in the first two years of its existence than any other 
(baring assembly) programming language in equal time. And no internet to 
help it. Every one who speaks bad of assembly has never had the 
satisfaction of truly feeling the power. ("'cause they got no proper 
background" - says the old man)  The power of assembly is simple - if 
the machine can do it, it's allowed.  No need to worry about "if the 
compiler will allow" or "work around that compiler bug" or "Oops - they 
changed the ...(compiler or interpreter) and now we start over".  The 
average programmer, who takes a moment to think it out, can out optimize 
all but the best commercial compilers. The meticulous individual can 
usually match or best the best commercials with fewer 'iterations' of 
review when using assembly.  Since one is already looking at the 
registers and addresses, self optimization is simple.


I still have my Z80 pre-assembler. It allows  Do, While, For and Loop 
along with If..Then..Else (and/or Elseif) statements in assembly 
programming. Z80 had both mandatory and full conditional  call, jump, 
return ... anywhere to/from in memory.  Intel's conditional jump forward 
was limited to 126 BYTES. Even with megabytes of memory. Worse than Pascal.
"full conditional" - On Zero, plus, minus, overflow, underflow and some 
I don't remember.  Most were 1byte commands. (Destination had to be 
added, but not return - the microcode took care of that.)

Oh - and TRUE = 0, FALSE != 0  (Zero is good - no error)

VisualBasic IS Microsoft's blend of Python and Basic. IMHO a bad blend.
I am currently in the process of "converting" (re-writing is a better 
term) the key VB programs to Python/Tkinter for the department. The 
primary vendor finally got smart and told us VB wasn't going to be in 
their next release. Python is in, VB is out.

Everybody jump up and shout HURRAH!:)

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


Re: Domain Driven Design and Python

2009-04-17 Thread Kay Schluehr
On 16 Apr., 19:44, José María  wrote:
> Hi,
>
> I've been searching for information about the application of DDD
> principles in
> Python and I did'nt found anything!
> Is DDD obvious in Python or is DDD inherent to static languages like
> Java or C#?

If you couldn't find anything I conclude that no one in the Python
community takes much care about mapping concepts like layers,
entities, aggregates, value objects etc. onto Python or even feels
that this improves the state of the art.

I'm pretty sure though that DDD is not bound to a particular type
system.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Condition.wait(0.5) doesn't respect it's timeout

2009-04-17 Thread Gabriel Genellina

En Fri, 17 Apr 2009 22:20:11 -0300,  escribió:


I have a problem with Condition.wait(), it doesn't return after the
given timeout. The thing is that if I try to create a simple program,
it works as expected, but in the actual code, the timeout is not
respected (albeit the notify()s work as expected). [...]

Has anyone the slightest idea on what I may be doing wrong? Or am I
just lucky enough to have stumbled across a bug?


Looks like a real bug :(


Maybe pollution from
another module in other parts of the code? (Like gobject...)

Anyway just for completeness here is a sample program that works for
me:

from threading import Thread
from threading import Condition

def some_func():
while True:
cond.acquire()
while True:
cond.wait(0.5)
print "Hello, world!"

cond = Condition()
thread = Thread(target=some_func)
thread.start()


If another thread has acquired the lock, cond.wait() doesn't return. Add  
these lines at the end of your test and see:


sleep(2)
print "Main thread - cond.acquire()"
cond.acquire()
sleep(2)
print "Main thread - cond.release()"
cond.release()
sleep(2)
sys.exit()

The timeout is detected, but the wait method doesn't return, it's stuck at  
the last line (trying to restore a saved RLock state).

I don't understand the logic behind that.
Please file a bug report at http://bugs.python.org/

--
Gabriel Genellina

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


Re: Something weird about re.finditer()

2009-04-17 Thread Aaron Brady
On Apr 17, 9:37 pm, Steven D'Aprano  wrote:
> On Sat, 18 Apr 2009 12:37:09 +1200, Lawrence D'Oliveiro wrote:
> > In message ,
> > Steven D'Aprano wrote:
>
> >> BTW, testing for None with == is not recommended, because one day
> >> somebody might pass your function some strange object that compares
> >> equal to None.
>
> > Presumably if it compares equal to None, that is by design, precisely so
> > it would work in this way.
>
> In context, no. We're not talking about somebody creating an object which
> is equivalent to None when treated as a value, but using None as a
> sentinel. Sentinels are markers, and it is important that nothing else
> can be mistaken for that marker or breakage will occur.
>
> Of course, if the caller knows how the sentinel is used, then he might
> choose to duplicate that usage but pass some other object. But that would
> be stupid and should be discouraged. I mean, what would be the point? I
> can think of use-cases for creating something that returns equal to None
> -- the Null object pattern comes to mind. But what would be the point of
> creating an object that was not None but would fool a function into
> treating it as the same sentinel as None?

In that case, they could use separate sentinels, that are instances of
a class or classes that have defined behavior for comparing to each
other.

It might get as bad as setting a flag on the class or sentinel, though
you'd have to be careful about concurrency and especially nested
calls.

You'd have to rely on the user function to use equality instead of
identity testing, since 'sentinel is None' won't return true no matter
what you do to it.
--
http://mail.python.org/mailman/listinfo/python-list


Variables vs attributes [was Re: Inheriting dictionary attributes and manipulate them in subclasses]

2009-04-17 Thread Steven D'Aprano
On Fri, 17 Apr 2009 17:48:55 +0200, Diez B. Roggisch wrote:

> No, because you are creating *classvariables* when declaring things like
> this:
...
> OTOH, when assigning to an instance, this will create an
> *instance*-variable. Which is what


If an integer variable is an integer, and a string variable is a string, 
and float variable is a float, and a list variable is a list (there's a 
pattern here), shouldn't a class variable be a class and an instance 
variable be an instance?

I had never noticed the usage of "variable" to mean attribute until a few 
months ago. What's going on? Why did people decide that confusing 
variables and attributes of variables was a good thing? What's next, 
describing dictionary keys as "dictionary variables"?

Sorry to pick on Diez, he's not the only one, just the latest example.


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


Re: binary file compare...

2009-04-17 Thread Steven D'Aprano
On Fri, 17 Apr 2009 11:19:31 -0700, Adam Olsen wrote:

> Actually, *cryptographic* hashes handle that just fine.  Even for files
> with just a 1 bit change the output is totally different.  This is known
> as the Avalanche Effect.  Otherwise they'd be vulnerable to attacks.
> 
> Which isn't to say you couldn't *construct* a pattern that it would be
> vulnerable to.  Figuring that out is pretty much the whole point of
> attacking a cryptographic hash.  MD5 has significant vulnerabilities by
> now, and other will in the future.  That's just a risk you need to
> manage.

There are demonstrated methods for constructing MD5 collisions against an 
arbitrary file in about a minute on a laptop computer.

http://en.wikipedia.org/wiki/MD5



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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Mensanator
On Apr 17, 9:43 pm, Steven D'Aprano  wrote:
> On Fri, 17 Apr 2009 14:00:18 -0700, Mensanator wrote:
> > On Apr 17, 3:37 pm, baykus  wrote:
> >> Hi
>
> >> I am looking for one of those experimental languages that might be
> >> combination of python+basic. Now thta sounds weird and awkward I know.
>
> > That's a clue you won't find anyone seriously contemplating such idiocy.
>
> >> The reason I am asking is that I always liked how I could reference-
> >> call certain line number back in the days.
>
> > A bad idea. If you really want to write bad code, learn C.
>
> >> It would be interesting to get similar functionality in Python.
>
> > Yeah, it would "interesting" just as a train wreck is "interesting", as
> > long as you're not the one who has to live through it.
>
> Nevertheless, somebody *has* implemented such functionality in Python.
> Not just GOTO, but also COMEFROM.

Really? Well, _I_ for one, won't be beating a path to his door.

>
> http://entrian.com/goto/
>
> > I once translated a BASIC program to Pascal (hint: no goto allowed).
>
> Pascal has GOTOs.

I know. _I'm_ the one who didn't allow them. And the code ended up
pretty damn bulletproof.

> People rarely used them, because even in the 1970s and
> 80s they knew that unstructured gotos to arbitrary places was a terrible
> idea.

That was obvious from the BASIC code, enough to make you shake
your head in disbelief.

>
> GOTO in Pascal required that you defined a label in your code, then you
> could jump to that label. You can't jump to arbitrary parts of the
> program, only within the current procedure.

And I deliberately made no effort to learn how to use them. And I
never
had a situation I couldn't solve the "proper" way.

>
> --
> Steven

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


Re: question about xrange performance

2009-04-17 Thread Steven D'Aprano
On Fri, 17 Apr 2009 13:58:54 -0700, ~flow wrote:

>> One might wonder why you are even writing code to test for existence
>> "in" a range list, when "blee <= blah < bloo" is obviously going to
>> outperform this kind of code.
>> -- Paul
> 
> the reason is simply the idiomacy and convenience. i use (x)xranges to
> implement unicode blocks and similar things. it is natural to write `if
> cid in latin_1` and so on.

[soapbox]
Speaking about idiomacy, it is grammatically incorrect to start sentences 
in English with lower-case letters, and it is rude because it harms the 
reading ability of people reading your posts. If it saves you 0.01ms of 
typing time to avoid pressing the shift key, and 100 people reading your 
post take 0.01ms more mental processing time to comprehend your writing 
because of the lack of a clear sentence break, then the harm you do to 
others is 100 times greater than the saving you make for yourself. You're 
not e.e. cummings, who was a dick anyway, and as a programmer you're 
supposed to understand about precision in language, syntax and grammar.
[end soapbox]

I think testing y in xrange() is a natural thing to do, but as I recall, 
it was actually removed from xrange a few years ago to simplify the code. 
I thought that was a silly decision, because the code was written and 
working and it's not like the xrange object was likely to change, but 
what do I know?


> i always assumed it would be about the
> fastest and memory-efficient to use xrange for this. 

If you don't need to iterate over the actual codepoints, the most memory-
efficient would be to just store the start and end positions, as a tuple 
or possibly even a slice object, and then call t[0] <= codepoint < t[1].

If you do need to iterate over them, perhaps some variant of this would 
suit your needs:

# Untested
class UnicodeBlock(object):
def __init__(self, start, end):
self.start = start
self.end = end
self._current = start
def __contains__(self, value):
if isinstance(value, (int, long)):
 return self.start <= value < self.end
def __iter__(self):
return self
def next(self):
if self._current < self.end:
self._current += 1
return self._current
raise StopIterator
def reset(self):
self._current = self.start


[...]
> the `( x == int( x ) )` is not easily being done away with if emulation
> of present x/range behavior is desired (x.0 floats are in, all others
> are out).

x.0 floats working with xrange is an accident, not a deliberate design 
decision, and has been deprecated in Python 2.6, which means it will 
probably be gone in a few years:

>>> r = xrange(2.0, 5.0)
__main__:1: DeprecationWarning: integer argument expected, got float



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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Steven D'Aprano
On Fri, 17 Apr 2009 14:00:18 -0700, Mensanator wrote:

> On Apr 17, 3:37 pm, baykus  wrote:
>> Hi
>>
>> I am looking for one of those experimental languages that might be
>> combination of python+basic. Now thta sounds weird and awkward I know.
> 
> That's a clue you won't find anyone seriously contemplating such idiocy.
> 
>> The reason I am asking is that I always liked how I could reference-
>> call certain line number back in the days.
> 
> A bad idea. If you really want to write bad code, learn C.
> 
>> It would be interesting to get similar functionality in Python.
> 
> Yeah, it would "interesting" just as a train wreck is "interesting", as
> long as you're not the one who has to live through it.

Nevertheless, somebody *has* implemented such functionality in Python. 
Not just GOTO, but also COMEFROM.

http://entrian.com/goto/

 
> I once translated a BASIC program to Pascal (hint: no goto allowed). 

Pascal has GOTOs. People rarely used them, because even in the 1970s and 
80s they knew that unstructured gotos to arbitrary places was a terrible 
idea.

GOTO in Pascal required that you defined a label in your code, then you 
could jump to that label. You can't jump to arbitrary parts of the 
program, only within the current procedure.



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


Re: Overriding methods per-object

2009-04-17 Thread Steven D'Aprano
On Fri, 17 Apr 2009 18:22:49 -0700, Pavel Panchekha wrote:

> I've got an object which has a method, __nonzero__ The problem is, that
> method is attached to that object not that class
> 
>> a = GeneralTypeOfObject()
>> a.__nonzero__ = lambda: False
>> a.__nonzero__()
> False
> 
> But:
> 
>> bool(a)
> True
> 
> What to do?

(1) Don't do that.

(2) If you *really* have to do that, you can tell the class to look at 
the instance:

class GeneralTypeOfObject(object):
def __nonzero__(self):
try:
return self.__dict__['__nonzero__']
except KeyError:
return something

(3) But a better solution might be to subclass the class and use an 
instance of that instead. You can even change the class on the fly.

a.__class__ = SubclassGeneralTypeOfObject  # note the lack of brackets




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


Re: Python Goes Mercurial

2009-04-17 Thread Lawrence D'Oliveiro
Python and Git should get along great.



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


Re: Something weird about re.finditer()

2009-04-17 Thread Steven D'Aprano
On Sat, 18 Apr 2009 12:37:09 +1200, Lawrence D'Oliveiro wrote:

> In message ,
> Steven D'Aprano wrote:
> 
>> BTW, testing for None with == is not recommended, because one day
>> somebody might pass your function some strange object that compares
>> equal to None.
> 
> Presumably if it compares equal to None, that is by design, precisely so
> it would work in this way.

In context, no. We're not talking about somebody creating an object which 
is equivalent to None when treated as a value, but using None as a 
sentinel. Sentinels are markers, and it is important that nothing else 
can be mistaken for that marker or breakage will occur.

Of course, if the caller knows how the sentinel is used, then he might 
choose to duplicate that usage but pass some other object. But that would 
be stupid and should be discouraged. I mean, what would be the point? I 
can think of use-cases for creating something that returns equal to None 
-- the Null object pattern comes to mind. But what would be the point of 
creating an object that was not None but would fool a function into 
treating it as the same sentinel as None?



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


Re: Overriding methods per-object

2009-04-17 Thread Aaron Brady
On Apr 17, 9:28 pm, Pavel Panchekha  wrote:
> > The docs don't say you can do that:
>
> Thanks, hadn't noticed that.
>
> > Should you be able to?
>
> I'd say so. In my case, I need a class that can encapsulate any
> object, add a few methods to it, and spit something back that works
> just like the object, but also has those extra methods. I can't just
> add the methods, because it has to work on e.g. lists. So I'll have to
> end up defining all the possible methods on that class (and that's
> still not best because I can't use hasattr to test if, for example,
> addition is allowed on that object).
>
> On the other hand, I see how this severely restricts the possibly
> optimizations that can be made in the interpreter.

Can you dynamically subclass it:

def subclasser( obj ):
  class newclass( obj.__class__ ):
def __nonzero__.
  obj.__class__= newclass

FYI, the __class__ member is assignable, though I've personally never
done it in practice.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Overriding methods per-object

2009-04-17 Thread Pavel Panchekha
> The docs don't say you can do that:

Thanks, hadn't noticed that.

> Should you be able to?

I'd say so. In my case, I need a class that can encapsulate any
object, add a few methods to it, and spit something back that works
just like the object, but also has those extra methods. I can't just
add the methods, because it has to work on e.g. lists. So I'll have to
end up defining all the possible methods on that class (and that's
still not best because I can't use hasattr to test if, for example,
addition is allowed on that object).

On the other hand, I see how this severely restricts the possibly
optimizations that can be made in the interpreter.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Puzzled about this regex

2009-04-17 Thread Chris Rebert
On Fri, Apr 17, 2009 at 7:17 PM, Jean-Claude Neveu
 wrote:
> Hello,
>
> I wonder if someone could tell me where I am going wrong with my regular
> expression, please. My regex only matches the text I'm looking for (a number
> followed by a distance unit) when it appears at the beginning of the string.
> But I am not using the ^ character (which would indicate that I only want a
> match if it is at the start).

Reading the fine manual, http://docs.python.org/library/re.html :

re.match(pattern, string[, flags])
If zero or more characters at the beginning of string match the
regular expression pattern, return a corresponding MatchObject
instance. Return None if the string does not match the pattern; note
that this is different from a zero-length match.
***Note: If you want to locate a match anywhere in string, use
search() instead.***

re.search(pattern, string[, flags])
Scan through string looking for a location where the regular
expression pattern produces a match, and return a corresponding
MatchObject instance. Return None if no position in the string matches
the pattern; note that this is different from finding a zero-length
match at some point in the string.

Cheers,
Chris
-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Puzzled about this regex

2009-04-17 Thread Jean-Claude Neveu

Hello,

I wonder if someone could tell me where I am going wrong with my 
regular expression, please. My regex only matches the text I'm 
looking for (a number followed by a distance unit) when it appears at 
the beginning of the string. But I am not using the ^ character 
(which would indicate that I only want a match if it is at the start).


---
#
import re

regex1 = re.compile("[0-9]+ (feet|meters)", re.IGNORECASE)

def is_distance(str):
   if regex1.match(str):
  print "distance"
   else:
  print "not distance"

# First one matches, second does not -- WHY?
is_distance("300 feet is the distance")
is_distance("The distance is 300 feet")
---

Thank you!

J-C

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


Re: send() to a generator in a "for" loop with continue(val)??

2009-04-17 Thread Aaron Brady
On Apr 17, 3:59 pm, Dale Roberts  wrote:
> I've started using generators for some "real" work (love them!), and I
> need to use send() to send values back into the yield inside the
> generator. When I want to use the generator, though, I have to
> essentially duplicate the machinery of a "for" loop, because the "for"
> loop does not have a mechanism to send into the generator. Here is a
> toy example:
>
> def TestGen1():
>     for i in xrange(3):
>         sendval = yield i
>         print "   got %s in TestGen()" % sendval
>
> g = TestGen1()
> sendval = None
> try:
>     while True:
>         val = g.send(sendval)
>         print 'val in "while" loop %d' % val
>         sendval = val * 10
> except StopIteration: pass
>
> I have to explicitly create the generator with an assignment, send an
> initial None to the generator on the first go, then have to catch the
> StopIteration exception. In other words, replicate the "for"
> mechanism, but use send() instead of next().
>
> It would be nice if I could just do this instead:
>
> for val in TestGen1():
>     print 'val in "for" loop %d' % val
>     continue(val*10)
>
> ...or something similar. Is this an old idea? Has it been shot down in
> the past already? Or is it worth pursuing? I Googled around and saw
> one hit 
> here:http://mail.python.org/pipermail/python-ideas/2009-February/003111.html,
> but not much follow-up.
>
> I wonder if people want to keep the idea of an "iterator" style
> generator (where send() is not used) separate from the idea of a "co-
> routine" style generator (where send() is used). Maybe combining the
> two idioms in this way would cause confusion?
>
> What do folks think?
>
> dale

You can do it with a wrapping generator.  I'm not sure if it
interferes with your needs.  It calls 'next' the first time, then just
calls 'send' on the parameter with the value you send it.

>>> def genf( ):
... x= True
... for _ in range( 10 ):
... i= yield( x )
... if i is None:
... i= not x
... x= i
...
>>> def for_send( gen ):
... x= next( gen )
... while 1:
... x= yield( x )
... x= gen.send( x )
...
>>> a= for_send( genf( ) )
>>> i= 0
>>> for x in a:
... print( i, x )
... i+= 1
... if not i% 3:
... _= a.send( False )
...
0 True
1 False
2 True
3 True
4 False
5 True
6 True
7 False

As you can see, it skips a beat every third iteration as directed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: large db question about no joins

2009-04-17 Thread Aaron Brady
On Apr 17, 3:16 pm, "Martin P. Hellwig" 
wrote:
> Daniel Fetchinson wrote:
>
> 
>
>
>
> > In an relational database setting you would have a table for artists,
> > a table for cd's and a table for songs and a table for comments where
> > people can comment on songs. All of this with obvious foreign keys.
> > Now you want to display on your website the total number of cd's, the
> > total number of songs and the total number of comments because that
> > looks very cool on top of every page: 1242342 cd's and 134242342342
> > songs and 284284728347284728 comments!
>
> > Okay, so you need to issue a query with joins in your relational db.
> > Or in another query, you want to select all comments of an artist,
> > meaning all comments that were comments on a song belonging to cd's of
> > a given artist. This would also involve joins.
>
> > How would I do these queries if I can't have joins or in other words
> > how would I lay out my data storage?
>
> > Thanks by the way for your help and replies,
> > Daniel
>
> What actually is happening here is that feature x which is usually in an
> abstraction layer (like a RDBM) is not available to you. Which means you
> have to write code which does the same but on your side of the code.
>
> I played a bit around with the GAE some time ago and designed the
> storage of all data in an sort of EAV model
> (http://en.wikipedia.org/wiki/Entity-Attribute-Value_model)
> The abstraction layer had all my meta code, counting stuff up was a
> matter of creating a list of id's from one section and with that list
> selecting the appropriate records on the other section, then count up
> the number of results or use the result again, it is very likely you
> have to do quite a lot of queries before you get the result.
>
> So in other words, just lay out the data which makes the most sense to
> you, the pain of recreating SQL like logic is there no matter what
> layout you choose.
>
> --
> mph

Instead of creating a new table, just create an iterator that returns
successive entries in it.  Just don't change the size of either table
during iteration.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Overriding methods per-object

2009-04-17 Thread Aaron Brady
On Apr 17, 8:22 pm, Pavel Panchekha  wrote:
> I've got an object which has a method, __nonzero__
> The problem is, that method is attached to that object not that class
>
> > a = GeneralTypeOfObject()
> > a.__nonzero__ = lambda: False
> > a.__nonzero__()
>
> False
>
> But:
>
> > bool(a)
>
> True
>
> What to do?

The docs don't say you can do that:

Special method names
A class can implement certain operations that are invoked by special
syntax (such as arithmetic operations or subscripting and slicing) by
defining methods with special names.

Should you be able to?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and XML Help

2009-04-17 Thread ookrin
On Apr 15, 9:43 am, Scott David Daniels  wrote:
> ookrin wrote:
> >  I am still learning. And it's not that I won't take the advice
> > for using ElementTree, I just currently don't know anything about it.
> > I just didn't want to say, "I have no idea what you're talking about!"
> > to Scott cause I figured that would be rude, but I guess so is not
> > saying anything, sorry. So I'll need to read up on it before I
> > actually try to do anything with that approach.
>
> > Seeing the errors - I changed the two classes to this:
> >   ... [some code that actually says a _little_ bit about his target] 
>
> If you had done a better job of asking your question, you'd have found a
> quick answer.  That is why everyone points people at smartquestions, not
> to pick on them.
>
> Presuming you have an xml structure like:
>      txt = ''' something 
>              Some contents. 
>             Other contents.
>             '''
>      import xml.etree.ElementTree as ET
>      structure = ET.fromstring(xmls)
>      for node in structure.findall('def/name'):
>          print '%s: %s / %s: %s' % (node.tag, node.attrib['name'],
>                                node.attrib['characterID'], node.text)
>
> or a file named 'my.xml' with the same contents:
>      import xml.etree.ElementTree as ET
>      structure = ET.parse('my.xml')
>      for node in structure.findall('def/name'):
>          print '%s: %s / %s: %s' % (node.tag, node.attrib['name'],
>                                node.attrib['characterID'], node.text)
>
> --Scott David Daniels
> scott.dani...@acm.org

Thanks very much for your patience with me and thanks for the help
everyone.
This is my end result:

self.api = apiConnection(self.userID,self.apiKey)
xmlData = self.api.returnXml()
if(xmlData != "None"):
struct = ET.fromstring(xmlData)
self.charIDs =[]
i=0
for node in struct.findall('result/rowset/row'):
char = node.attrib['name']
id = node.attrib['characterID']
temp = char, id
#append them to the variable for later
self.charIDs.append(temp)
i+=1
#Setup the select window and run it
#selects which character I want
self.selectCharUi = selectChar(self,self.charIDs)
self.selectCharUi.selectWindow.exec_()

#Get the name that was selected
self.selectedCharName = self.selectCharUi.returnValue
()
#Set the text widget with the name
self.lE_charID.setText(self.selectedCharName)
#enable the Ok button
self.pbOk.setEnabled(True)

It collects the names and IDs, sends them to a pyqt4 window with a
list widget to list the names, a name gets selected and then I get the
name that was selected. Though now that I think about it, maybe using
a dictionary would be better. Elsewhere I gather up the userID, apiID,
characterName and characterID and write it t a file. I hope it doesn't
look too horrible.

John - my programming experience was two semesters of c++ about 6
years ago. Everything else I've learned from the internet.  =/

I'll try to make my questions better next time.

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


Re: Overriding methods per-object

2009-04-17 Thread Chris Rebert
On Fri, Apr 17, 2009 at 6:22 PM, Pavel Panchekha  wrote:
> I've got an object which has a method, __nonzero__
> The problem is, that method is attached to that object not that class
>
>> a = GeneralTypeOfObject()
>> a.__nonzero__ = lambda: False
>> a.__nonzero__()
> False
>
> But:
>
>> bool(a)
> True

This is as documented. See
http://docs.python.org/3.0/reference/datamodel.html#special-lookup

>
> What to do?

Either wrap the object in another object of a class that does define
__bool__, or change GeneralTypeOfObject so it defines __bool__(),
possibly having its __bool__ delegate to another method, which you
could then override on a per-object basis similar to your current
code.

Cheers,
Chris
-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Man Bites Python

2009-04-17 Thread Aaron Brady
On Apr 17, 7:03 pm, "AD."  wrote:
> On Apr 17, 11:11 pm, Aaron Brady  wrote:
>
> > Man bites python.
> > Python bites dog.
> > Dog bites man.
>
> or just:
>
> man,python bites python,man
>
> No need for the temporary value in Python.

Is Python a mutable type?
Just don't pass it by reference.
--
http://mail.python.org/mailman/listinfo/python-list


Overriding methods per-object

2009-04-17 Thread Pavel Panchekha
I've got an object which has a method, __nonzero__
The problem is, that method is attached to that object not that class

> a = GeneralTypeOfObject()
> a.__nonzero__ = lambda: False
> a.__nonzero__()
False

But:

> bool(a)
True

What to do?
--
http://mail.python.org/mailman/listinfo/python-list


Condition.wait(0.5) doesn't respect it's timeout

2009-04-17 Thread stephane . bisinger
Hi all,
I have a problem with Condition.wait(), it doesn't return after the
given timeout. The thing is that if I try to create a simple program,
it works as expected, but in the actual code, the timeout is not
respected (albeit the notify()s work as expected).
You can find the code I am talking about here:
http://github.com/Kjir/amsn2/blob/6688da4c0b7cc16c0fe04d6d6018bc1b16d992a6/amsn2/gui/front_ends/curses/contact_list.py

If you clone the repository, then you can run the program like this:
$ python amsn2.py -f curses 2>> run.log
and in another term watch for prints with a tail -f run.log (You need
an MSN account). You'll notice that after the initial downloading of
the contact list, there won't be any logs (unless some of your
contacts changes status, which will trigger a notify)

Has anyone the slightest idea on what I may be doing wrong? Or am I
just lucky enough to have stumbled across a bug? Maybe pollution from
another module in other parts of the code? (Like gobject...)

Anyway just for completeness here is a sample program that works for
me:

from threading import Thread
from threading import Condition

def some_func():
while True:
cond.acquire()
while True:
cond.wait(0.5)
print "Hello, world!"

cond = Condition()
thread = Thread(target=some_func)
thread.start()

thanks,

Stéphane Bisinger
--
http://mail.python.org/mailman/listinfo/python-list


Are there any python modules available to extract form field names and form data from a non-flatten pdf file?

2009-04-17 Thread monogeo
I tried pyPdf, it doesn't extract form field names and form data from
a non-flatten pdf file, thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex alternation problem

2009-04-17 Thread Jesse Aldridge
On Apr 17, 5:30 pm, Paul McGuire  wrote:
> On Apr 17, 5:28 pm, Paul McGuire  wrote:> -- Paul
>
> > Your find pattern includes (and consumes) a leading AND trailing space
> > around each word.  In the first string "I am an american", there is a
> > leading and trailing space around "am", but the trailing space for
> > "am" is the leading space for "an", so " an "- Hide quoted text -
>
> Oops, sorry, ignore debris after sig...

Alright, I got it.  Thanks for the help guys.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Something weird about re.finditer()

2009-04-17 Thread Lawrence D'Oliveiro
In message , Steven 
D'Aprano wrote:

> BTW, testing for None with == is not recommended, because one day
> somebody might pass your function some strange object that compares equal
> to None.

Presumably if it compares equal to None, that is by design, precisely so it 
would work in this way.

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


Re: [OT] large db question about no joins

2009-04-17 Thread Daniel Fetchinson
>> In an relational database setting you would have a table for artists,
>> a table for cd's and a table for songs and a table for comments where
>> people can comment on songs. All of this with obvious foreign keys.
>> Now you want to display on your website the total number of cd's, the
>> total number of songs and the total number of comments because that
>> looks very cool on top of every page: 1242342 cd's and 134242342342
>> songs and 284284728347284728 comments!
>>
>> Okay, so you need to issue a query with joins in your relational db.
>> Or in another query, you want to select all comments of an artist,
>> meaning all comments that were comments on a song belonging to cd's of
>> a given artist. This would also involve joins.
>>
>> How would I do these queries if I can't have joins or in other words
>> how would I lay out my data storage?
>>
>> Thanks by the way for your help and replies,
>> Daniel
>>
>
> What actually is happening here is that feature x which is usually in an
> abstraction layer (like a RDBM) is not available to you. Which means you
> have to write code which does the same but on your side of the code.
>
> I played a bit around with the GAE some time ago and designed the
> storage of all data in an sort of EAV model
> (http://en.wikipedia.org/wiki/Entity-Attribute-Value_model)
> The abstraction layer had all my meta code, counting stuff up was a
> matter of creating a list of id's from one section and with that list
> selecting the appropriate records on the other section, then count up
> the number of results or use the result again, it is very likely you
> have to do quite a lot of queries before you get the result.
>
> So in other words, just lay out the data which makes the most sense to
> you, the pain of recreating SQL like logic is there no matter what
> layout you choose.

Thanks, this wikipedia entry was actually very useful as well as your
other comments.

Thanks again,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread norseman

baykus wrote:

Hi

I am looking for one of those experimental languages that might be
combination of python+basic. Now thta sounds weird and awkward I know.
The reason I am asking is that I always liked how I could reference-
call certain line number back in the days. It would be interesting to
get similar functionality in Python.
--
http://mail.python.org/mailman/listinfo/python-list


===
Yeah - after they took at look at Python:
Microsoft calls it VisualBasic

There are some that will enjoy the joke. :)

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


Re: binary file compare...

2009-04-17 Thread Lawrence D'Oliveiro
In message , Nigel 
Rantor wrote:

> Adam Olsen wrote:
>
>> The chance of *accidentally* producing a collision, although
>> technically possible, is so extraordinarily rare that it's completely
>> overshadowed by the risk of a hardware or software failure producing
>> an incorrect result.
> 
> Not when you're using them to compare lots of files.
> 
> Trust me. Been there, done that, got the t-shirt.

Not with any cryptographically-strong hash, you haven't.

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


Re: Registering Cron using CronTab

2009-04-17 Thread Lawrence D'Oliveiro
In message , Philip 
Semanchuk wrote:

> 
> On Apr 17, 2009, at 9:51 AM, gurcharan.sa...@gmail.com wrote:
> 
>> I'm stuck with the issue - if we execute the code from Apache the
>> crontab is not getting updated, while it get updated if we run it from
>> Django in-built webserver.
> 
> Sure sounds like a permissions problem to me.

Same here. Note that you can run a CGI under its owning user in Apache with 
the suexec feature. That might give you the permissions you need.

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Martin P. Hellwig

Michael Torrie wrote:

Aahz wrote:

Why do you want to do that?  Before you answer, make sure to read this:


http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html

Somebody better tell the Linux kernel developers about that!  They
apparently haven't read that yet.  Better tell CPU makers too.  In
assembly it's all gotos.


Well CPU's wouldn't work as well if they didn't had a way to jump to  a 
predefined instruction when certain conditions are met.
IMHO for people who are users, that is you are not writing machine code 
or assembly, you should only use these 'goto's if you are capable of 
writing machine code or assembly.


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


Re: Man Bites Python

2009-04-17 Thread AD.
On Apr 17, 11:11 pm, Aaron Brady  wrote:
> Man bites python.
> Python bites dog.
> Dog bites man.

or just:

man,python bites python,man

No need for the temporary value in Python.

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Tim Rowe
2009/4/17 Michael Torrie :

> Spaghetti code can be written in *any* language.

I challenge you to write spahgetti code in SPARK!

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Mensanator
On Apr 17, 5:02 pm, Michael Torrie  wrote:
> Mensanator wrote:
> > I once translated a BASIC program to Pascal (hint: no goto allowed).
> > The original code had GOSUBs that never executed a REURN because
> > the programmer jumped away to line numbers on a whim. Biggest piece
> > of crap I ever had to misfortune to deal with.
>
> It's clear that you haven't done anything in BASIC since the 80s.  

Not hardly, I use VBA every day in Excel & Access. The example
I mentioned WAS from the 80's.

> And
> probably the original poster hasn't either.  So let's just clear the air
> here.

I don't see any need. Of course, you're the one who's view is muddied.

>
> I haven't seen a "GOTO" in BASIC code in probably almost 20 years, ever
> since BASIC gained true structure.  

Try UBASIC. And I didn't say modern BASICs were like those of the
80's,
I questioned why anyone would want to return to such systems as
existed
in the 80's.

> In fact as BASIC is used today, it's
> really similar to Pascal, but a lot nicer to work with.  

And I wasn't refering to that, I was specifically criticizing
the jumping to random line numbers within the program. Can't do
that in a modern BASIC? Fine, but who's asking for that? The OP.

> BASIC is a very
> structure language, and in VB, it's also object-oriented, although I'm
> sure lots of crap is written in VB.  You may shudder at the thought, but
> BASIC is very much a modern language now.  

As I said, I use it (VBA) every day and have probably written
more BASIC programs than you've had hot dinners.

> If you're bored, check out freebasic.net.  

Thanks, but I'll give it a miss.

> Not that I recommend you use FreeBASIC for anything (nor
> do I recommend most languages but python!).
>
> Spaghetti code can be written in *any* language.  It's nothing inherent
> to BASIC.  I have seen spaghetti python, particularly projects that are
> designed around the twisted framework.  Tracing execution through
> twisted is very painful.

Of course, but why would the OP think that someone's trying to make
a language that makes spaghetti code easier?

>
> That said, what the original poster is looking for is very silly.

That was my point.

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Scott David Daniels

Michael Torrie wrote:

baykus wrote:

I am looking for one of those experimental languages that might be
combination of python+basic. Now thta sounds weird and awkward I know.
The reason I am asking is that I always liked how I could reference-
call certain line number back in the days. It would be interesting to
get similar functionality in Python.


*No one* in the BASIC world uses line numbers anymore.  Why would you
want to? ...


The problem I see is that Basic as you use it above is not a language,
but a family of languages.  Different Basics share as much (and as
little) as different SQLs.   For my money, Visual Basic 5.0 is a
language.  THe different Microsoft Basics usually have a lot of language
change, rather than being library additions.  A lot of very different
languages have been called Basic, with the attendant confusion as old
syntax or semantics are abandoned or changed.  The changes to Python 3.x
is a language change, but Python has been _very_ conservative about
changing (as opposed to extending) the language.

There are only a few languages that might plausibly called "Basic",
and Dartmouth Basic has maybe the best claim to that name.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


RE: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Leguia, Tony
>Somebody better tell the Linux kernel developers about that!  They
>apparently haven't read that yet.  Better tell CPU makers too.  In
>assembly it's all gotos.

There a very big difference between high level programming, and assembly 
programming. 
Python is a high level language. 
I shouldn't have to say anymore. 

From: python-list-bounces+leguiato=grinnell@python.org 
[python-list-bounces+leguiato=grinnell@python.org] On Behalf Of Michael 
Torrie [torr...@gmail.com]
Sent: Friday, April 17, 2009 5:07 PM
Cc: python-list@python.org
Subject: Re: Is there a programming language that is combination of Python  
and Basic?

Aahz wrote:
> Why do you want to do that?  Before you answer, make sure to read this:
>
http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html

Somebody better tell the Linux kernel developers about that!  They
apparently haven't read that yet.  Better tell CPU makers too.  In
assembly it's all gotos.
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 2.6.2

2009-04-17 Thread Ned Deily
In article ,
 "Russell E. Owen"  wrote:
> In article ,
>  Ned Deily  wrote:
> > In article ,
> >  Russell Owen  wrote:
> > > I installed the Mac binary on my Intel 10.5.6 system and it works,  
> > > except it still uses Apple's system Tcl/Tk 8.4.7 instead of my  
> > > ActiveState 8.4.19 (which is in /Library/Frameworks where one would  
> > > expect).
> > > 
> > > I just built python from source and that version does use ActiveState  
> > > 8.4.19.
> > > 
> > > I wish I knew what's going on. Not being able to use the binary  
> > > distros is a bit of a pain.
> > 
> > You're right, the tkinter included with the 2.6.2 installer is not 
> > linked properly:
> > 
> > Is:
> > $ cd /Library/Frameworks/Python.framework/Versions/2.6
> > $ cd lib/python2.6/lib-dynload
> > $ otool -L _tkinter.so 
> > _tkinter.so:
> >/System/Library/Frameworks/Tcl.framework/Versions/8.4/Tcl 
> > (compatibility version 8.4.0, current version 8.4.0)
> >/System/Library/Frameworks/Tk.framework/Versions/8.4/Tk 
> > (compatibility version 8.4.0, current version 8.4.0)
> >/usr/lib/libSystem.B.dylib [...]
> > 
> > should be:
> > _tkinter.so:
> >/Library/Frameworks/Tcl.framework/Versions/8.4/Tcl (compatibility 
> > version 8.4.0, current version 8.4.19)
> >/Library/Frameworks/Tk.framework/Versions/8.4/Tk (compatibility 
> > version 8.4.0, current version 8.4.19)
> >/usr/lib/libSystem.B.dylib [...]
> 
> Just for the record, when I built Python 2.6 from source I got the 
> latter output (the desired result).
> 
> If someone can point me to instructions I'm willing to try to make a 
> binary installer and make it available (though I'd much prefer to debug 
> the standard installer).

I suspect Ronald will be fixing this in the standard installer soon.

-- 
 Ned Deily,
 n...@acm.org

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


Re: QT , Wxwidgets are not just UI framework ?

2009-04-17 Thread Дамјан Георгиевски
>> However, mostly people agree that Qt is the most powerful, but often
>> was debunked because of it's licensing. This has changed to the much
>> more liberal LGPL for Qt4.5.
>>
>> Now it might be though that you'd still need to buy a license from
>> Phil Thompson for his excellent PyQt-wrapping - but I'd personally
>> say it's more worth than it actually costs given the power of Qt.

> considering that wxwidget is open source and free do you think that QT
> lisencing is worth it ?

Both Qt and PyQt *ARE* open-source AND free (libre) software.
Qt is GPL and LGPL (you choose) and PyQt is GPL only.

http://www.qtsoftware.com/products/licensing
http://www.riverbankcomputing.co.uk/software/pyqt/license

If you don't like GPL or LGPL (why wouldn't you ?!?) then you can also 
get a commercial license too.



-- 
дамјан ( http://softver.org.mk/damjan/ )

Our national drug is alcohol,
we tend to regard the use any other drug with special horror.
-- William S. Burroughs

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


http://wwwx.cs.unc.edu/~gb/wp/blog/2007/02/04/python-opencv-wrapper-using-ctypes/

2009-04-17 Thread r-w

http://wwwx.cs.unc.edu/~gb/wp/blog/2007/02/04/python-opencv-wrapper-using-ctypes/
--
http://mail.python.org/mailman/listinfo/python-list


Re: unpythonic use of property()?

2009-04-17 Thread Scott David Daniels

Carl Banks wrote:

On Apr 17, 10:21 am, J Kenneth King  wrote:

Consider:

code:


class MyInterface(object):

def __get_id(self):
return self.__id

id = property(fget=__get_id)

def __init__(self, id, foo):
self.__id = id
self.foo = foo

class MyInterface2(object):

def __init__(self, id, foo):
self._id = id
self.foo = foo

@property
def id(self):
return self._id


...

I was recently informed that it was 'unpythonic' and have since been a
little confused by the term. I've heard it bandied about before but
never paid much attention. What is 'unpythonic'? What about this example
is unpythonic?


There are different reasons someone might have said it.
...
Some people think attribute name-mangling is unpythonic.  It's true
that people sometimes mistakenly treat it a solid information hiding
mechanism, but I wouldn't call its usage unpythonic when used as
intended: as a way to avoid name-collisions.  If you think it's
worthwhile to protect an attribute from being overwritten, you might
as well guard against accidental conflict with the underlying name.


Here you are assuming that a user of your class could not possibly have a
valid reason for getting to the underlying variable.  Don't make those
decisions for someone else, in Python, "we are all adults here."


Finally, some people think read-only attributes are unpythonic.  I
think that's ridiculous, although in general I'd advise against making
attributes read-only willy-nilly.  But there's a time and place for
it.


Generally, properties are for doing some form of calculation, not
for making things read-only.  If the thing you were returning were
in some way calculated, it would make sense to use a property.  We
normally use properties to allow a later attribute access to provide
the same interface as the original did with a simple attribute access.

So, I would advocate using:

class SimplerInterface(object):
def __init__(self, id, foo):
self.id = id
self.foo = foo

Note that this allows:

q = SimplerInterface('big', 'foo')
print (q.id)
q.id = 'small'
print (q.id)

But this is only a more straightforward way of writing:

q = MyInterface2('big', 'foo')
print (q.id)
q._id = 'small'
print (q.id)

or even:

q = MyInterface('big', 'foo')
print (q.id)
q._MyInterface__id = 'small'
print (q.id)

See, someone can work around your protection.  If you say, "don't do
that," I'd reply, "just tell your users not to change id."  It is not
your job to protect those users who do not use your code properly from
themselves; that way lies madness.


Last thing I'll advise is don't get too hung up on terms like
"Pythonic".  Treat such labels are more of a red flag and get people
who throw out the term to explain why.


Think of it as a comment like "that has a bad code smell" -- you can do
it if you like, but you should have satisfied yourself that your case is
sufficiently special as to require an unusual approach.  The PSU will
not come after you, as they are busy hunting down Barry & Guido.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] RELEASED Python 2.6.2

2009-04-17 Thread Russell E. Owen
In article ,
 Ned Deily  wrote:

> In article ,
>  Russell Owen  wrote:
> > I installed the Mac binary on my Intel 10.5.6 system and it works,  
> > except it still uses Apple's system Tcl/Tk 8.4.7 instead of my  
> > ActiveState 8.4.19 (which is in /Library/Frameworks where one would  
> > expect).
> > 
> > I just built python from source and that version does use ActiveState  
> > 8.4.19.
> > 
> > I wish I knew what's going on. Not being able to use the binary  
> > distros is a bit of a pain.
> 
> You're right, the tkinter included with the 2.6.2 installer is not 
> linked properly:
> 
> Is:
> $ cd /Library/Frameworks/Python.framework/Versions/2.6
> $ cd lib/python2.6/lib-dynload
> $ otool -L _tkinter.so 
> _tkinter.so:
>/System/Library/Frameworks/Tcl.framework/Versions/8.4/Tcl 
> (compatibility version 8.4.0, current version 8.4.0)
>/System/Library/Frameworks/Tk.framework/Versions/8.4/Tk 
> (compatibility version 8.4.0, current version 8.4.0)
>/usr/lib/libSystem.B.dylib [...]
> 
> should be:
> _tkinter.so:
>/Library/Frameworks/Tcl.framework/Versions/8.4/Tcl (compatibility 
> version 8.4.0, current version 8.4.19)
>/Library/Frameworks/Tk.framework/Versions/8.4/Tk (compatibility 
> version 8.4.0, current version 8.4.19)
>/usr/lib/libSystem.B.dylib [...]

Just for the record, when I built Python 2.6 from source I got the 
latter output (the desired result).

If someone can point me to instructions I'm willing to try to make a 
binary installer and make it available (though I'd much prefer to debug 
the standard installer).

-- Russell

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


Re: regex alternation problem

2009-04-17 Thread Paul McGuire
On Apr 17, 5:28 pm, Paul McGuire  wrote:
> -- Paul
>
> Your find pattern includes (and consumes) a leading AND trailing space
> around each word.  In the first string "I am an american", there is a
> leading and trailing space around "am", but the trailing space for
> "am" is the leading space for "an", so " an "- Hide quoted text -
>
Oops, sorry, ignore debris after sig...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sending directory with files in it via sockets

2009-04-17 Thread MRAB

Ryniek90 wrote:

Hi.
Last time i've got problem with sending big files, but i've already 
dealt with it.
Now, when i want send directory (with some files in it) i iterate that 
directory for files in it, and then in while loop open iterated files, 
read them and send. But something's not working. It iterate's first file 
from directory and sends it all the time (only first file). So, the 
iteration is written bad. Could you tell me how to modify it?


Here's that method:

def send_dir(self):
   print "Gathering information's..."


(FYI, "information" doesn't have a plural, and if it did it wouldn't
have an apostrophe!)


   self.direc = os.walk(self.data)
   for files in self.direc:
   lst_files = files[2]


Please not that you're binding files[2] to lst_files each time so that
it's final value is whatever the final result of self.direc was. I don't 
know whether that's intentional.



   for fl in lst_files:
   print "Sending %s from directory %s" % (fl, 
os.path.split(self.data)[1])

   while True:
   files = open("%s/%s" % (self.data,fl), 'rb')
   self.read = files.read(51200)
   self.konn.send(self.read)
   if not self.read:
   break

>files.close()

This 'while' loop is opening a file and sending the first 51200 bytes,
but then either leaving the loop if nothing was read or repeating the
whole thing if something was read. I think you need:

   data_file = open("%s/%s" % (self.data, fl), 'rb')
   while True:
   data = files.read(51200)
   if not data:
   break
   self.konn.sendall(data)
   data_file.close()

Notice that I used sendall() instead of send(). This is because send()
doesn't guarantee to send _all_ the data (it's in the documentation).



"self.data" is variable for the "self.konn.recv(4096")  method.  
self.konn is from "self.konn, self.addr = self.sok.accept()"


"self.direc = os.walk(self.data)
   for files in self.direc:
   lst_files = files[2]"

Iteration result looks like this:

 >>> import os
 >>> direc = os.walk('/media/DOWNLOAD/Obrazy płyt/Half-Life 2')
 >>> for files in direc:
   lst_files = files[2]

  >>> lst_files
['hl2_1.nrg', 'hl2_2.nrg', 'hl2_4.nrg', 'hl2_5.nrg']
 >>> for fl in lst_files:
   print fl

  hl2_1.nrg
hl2_2.nrg
hl2_4.nrg
hl2_5.nrg
 >>>



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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread baykus
On Apr 17, 5:02 pm, Michael Torrie  wrote:
> Mensanator wrote:
>
> It's clear that you haven't done anything in BASIC since the 80s.  And
> probably the original poster hasn't either.  So let's just clear the air
> here.


Michael you are kind of rigtht, I did use basic in early 90s :) Thanks
for the more insightful comments, I understand the drawbacks and
backwardness of Basic, I am not here to defend it. I guess I have a
different perception when it comes to Basic. I think many people think
those "lines" as numbered steps or numbered bricks that are sitting on
eachother but I see them as timelines or like filmstrips. Anyways it
sounds like such a toy programming language does not exists except
Arnaud surprisingly efficient code.  and I will search my dream
somewhere else :)

thanks for all the negative and positive comments :)


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


Re: regex alternation problem

2009-04-17 Thread Paul McGuire
On Apr 17, 4:49 pm, Jesse Aldridge  wrote:
> import re
>
> s1 = "I am an american"
>
> s2 = "I am american an "
>
> for s in [s1, s2]:
>     print re.findall(" (am|an) ", s)
>
> # Results:
> # ['am']
> # ['am', 'an']
>
> ---
>
> I want the results to be the same for each string.  What am I doing
> wrong?

Does it help if you expand your RE to its full expression, with '_'s
where the blanks go:

"_am_" or "_an_"

Now look for these in "I_am_an_american".  After the first "_am_" is
processed, findall picks up at the leading 'a' of 'an', and there is
no leading blank, so no match.  If you search through
"I_am_american_an_", both "am" and "an" have surrounding spaces, so
both match.

Instead of using explicit spaces, try using '\b' meaning word break:

>>> import re
>>> re.findall(r"\b(am|an)\b", "I am an american")
['am', 'an']
>>> re.findall(r"\b(am|an)\b", "I am american an")
['am', 'an']

-- Paul




Your find pattern includes (and consumes) a leading AND trailing space
around each word.  In the first string "I am an american", there is a
leading and trailing space around "am", but the trailing space for
"am" is the leading space for "an", so " an "
--
http://mail.python.org/mailman/listinfo/python-list


Re: Find and replace is appending when run more than once ...

2009-04-17 Thread MRAB

paul.scipi...@aps.com wrote:

Hello,
 
I grabbed some sample code from the web and put together this python 
script which searches all subdirectories for a specific file type, then 
replaces a string with a new one.  Out of curiosity I ran it again.  
Here's what happens (this also happens for the files I am looking to 
replace OutputPath=Z:\ with OutputPath=Z:\DXF):
 
1st run:  replaces OutputPath=Z:\ with OutputPath=Z:\CSV )

2nd run: replaces OutputPath=Z:\CSV with OutputPath=Z:\CSV\CSV
3rd run:  replaces OutputPath=Z:\CSV\CSV with OutputPath=Z:\CSV\CSV\CSV
And so on.
 
I cant figure out why it's doing this if I am searching a certain string 
value and replacing it.  I am also perplexed on why it is only adding 
the "CSV" each time rather than the full string.  What I want it to do 
is only replace once.  Next time I run it it shouldn't do anything.
 
import fnmatch,os,sys
 
mydir = 'C:\\!DOMSExtractor'

findStr = 'OutputPath=Z:\\'
 
def replaceStringInFile(findStr,replStr,filePath):

tempName=filePath+'~~~'
input = open(filePath)
output = open(tempName,'w')
 
for s in input:

print findStr
print replStr
output.write(s.replace(findStr,replStr))


This is replacing 'OutputPath=Z:\\' with 'OutputPath=Z:\\CSV\\' because
findStr is 'OutputPath=Z:\\' and replStr is 'OutputPath=Z:\\CSV\\'.
Unfortunately replStr starts with findStr, so if you had:

'OutputPath=Z:\\foo'

then you would get:

'OutputPath=Z:\\CSV\\foo'

Notice that this string still contains 'OutputPath=Z:\\', so doing it
again would result in:

'OutputPath=Z:\\CSV\\CSV\\foo'

Perhaps you could do something like this:

# Start with:
# 'fooOutputPath=Z:\\bar'
# or:
# 'fooOutputPath=Z:\\CSV\\bar'

new_s = s.replace(findStr, replStr + '*')

# Now have:
# 'fooOutputPath=Z:\\CSV\\*bar'
# or:
# 'fooOutputPath=Z:\\CSV\\*CSV\\bar'

new_s = new_s.replace('\\*CSV', '')

# Now have:
# 'fooOutputPath=Z:\\CSV\\*bar'
# or:
# 'fooOutputPath=Z:\\CSV\\bar'

new_s = new_s.replace('CSV\\*', 'CSV\\')

# Now have:
# 'fooOutputPath=Z:\\CSV\\bar'
# or:
# 'fooOutputPath=Z:\\CSV\\bar'


output.close()
input.close()
os.remove(filePath)
os.rename(tempName,filePath)
print filePath
 
def myfun(dummy, dirr, filess):

for child in filess:
filePath = dirr+'/'+child
if '.ini' == os.path.splitext(child)[1] and 
os.path.isfile(filePath):

print file(filePath, 'rb').read().find(findStr)
if file(filePath, 'rb').read().find(findStr) <> -1:
if fnmatch.fnmatch(dirr+'/'+child, '*BGExtract.ini'):

replaceStringInFile(findStr,'OutputPath=Z:\\DXF\\',filePath)

elif fnmatch.fnmatch(dirr+"/"+child, '*GISExtract.ini'):

replaceStringInFile(findStr,'OutputPath=Z:\\CSV\\',filePath)
 
os.path.walk(mydir, myfun, 3)
 

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Brian Blais

On Apr 17, 2009, at 16:37 , baykus wrote:


Hi

I am looking for one of those experimental languages that might be
combination of python+basic. Now thta sounds weird and awkward I know.
The reason I am asking is that I always liked how I could reference-
call certain line number back in the days. It would be interesting to
get similar functionality in Python.



If all you want is the goto, or equivalent, in python then you can  
always use:


http://entrian.com/goto/

Notice that this is, as everyone else says, a bad idea.  It was made  
as a joke, but it is actually functional.



bb

--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais



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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Michael Torrie
Aahz wrote:
> Why do you want to do that?  Before you answer, make sure to read this:
>
http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html

Somebody better tell the Linux kernel developers about that!  They
apparently haven't read that yet.  Better tell CPU makers too.  In
assembly it's all gotos.
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex alternation problem

2009-04-17 Thread Tim Chase

s1 = "I am an american"

s2 = "I am american an "

for s in [s1, s2]:
print re.findall(" (am|an) ", s)

# Results:
# ['am']
# ['am', 'an']

---

I want the results to be the same for each string.  What am I doing
wrong?


In your first case, the regexp is consuming the " am " (four 
characters, two of which are spaces), leaving no leading space 
for the second one to find.  You might try using \b as a 
word-boundary:


  re.findall(r"\b(am|an)\b", s)

-tkc




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


Re: regex alternation problem

2009-04-17 Thread Robert Kern

On 2009-04-17 16:49, Jesse Aldridge wrote:

import re

s1 = "I am an american"

s2 = "I am american an "

for s in [s1, s2]:
 print re.findall(" (am|an) ", s)

# Results:
# ['am']
# ['am', 'an']

---

I want the results to be the same for each string.  What am I doing
wrong?


findall() finds non-overlapping matches. " am  an " would work, but not
" am an ".

Instead of including explicit spaces in your pattern, I suggest using the \b 
"word boundary" special instruction.


>>> for s in [s1, s2]:
... print re.findall(r"\b(am|an)\b", s)
...
['am', 'an']
['am', 'an']

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Michael Torrie
Mensanator wrote:
> I once translated a BASIC program to Pascal (hint: no goto allowed).
> The original code had GOSUBs that never executed a REURN because
> the programmer jumped away to line numbers on a whim. Biggest piece
> of crap I ever had to misfortune to deal with.

It's clear that you haven't done anything in BASIC since the 80s.  And
probably the original poster hasn't either.  So let's just clear the air
here.

I haven't seen a "GOTO" in BASIC code in probably almost 20 years, ever
since BASIC gained true structure.  In fact as BASIC is used today, it's
really similar to Pascal, but a lot nicer to work with.  BASIC is a very
structure language, and in VB, it's also object-oriented, although I'm
sure lots of crap is written in VB.  You may shudder at the thought, but
BASIC is very much a modern language now.  If you're bored, check out
freebasic.net.  Not that I recommend you use FreeBASIC for anything (nor
do I recommend most languages but python!).

Spaghetti code can be written in *any* language.  It's nothing inherent
to BASIC.  I have seen spaghetti python, particularly projects that are
designed around the twisted framework.  Tracing execution through
twisted is very painful.

That said, what the original poster is looking for is very silly.
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex alternation problem

2009-04-17 Thread Robert Kern

On 2009-04-17 16:57, Eugene Perederey wrote:

According to documentation re.findall takes a compiled pattern as a
first argument. So try
patt = re.compile(r'(am|an)')
re.findall(patt, s1)
re.findall(patt, s2)


No, it will take a string pattern, too.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Michael Torrie
baykus wrote:
> I am looking for one of those experimental languages that might be
> combination of python+basic. Now thta sounds weird and awkward I know.
> The reason I am asking is that I always liked how I could reference-
> call certain line number back in the days. It would be interesting to
> get similar functionality in Python.

*No one* in the BASIC world uses line numbers anymore.  Why would you
want to?

If you just want basic, get freebasic from freebasic.net

Personally I can't see any reason to use any dialect of BASIC over
Python.  If you pine for the bad old days of jumping to random line
numbers, maybe just create a list of function objects in python (a call
table) and call a random one.
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex alternation problem

2009-04-17 Thread Eugene Perederey
According to documentation re.findall takes a compiled pattern as a
first argument. So try
patt = re.compile(r'(am|an)')
re.findall(patt, s1)
re.findall(patt, s2)

2009/4/18 Jesse Aldridge :
> import re
>
> s1 = "I am an american"
>
> s2 = "I am american an "
>
> for s in [s1, s2]:
>    print re.findall(" (am|an) ", s)
>
> # Results:
> # ['am']
> # ['am', 'an']
>
> ---
>
> I want the results to be the same for each string.  What am I doing
> wrong?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Sincerely yours, Eugene Perederey
--
http://mail.python.org/mailman/listinfo/python-list


regex alternation problem

2009-04-17 Thread Jesse Aldridge
import re

s1 = "I am an american"

s2 = "I am american an "

for s in [s1, s2]:
print re.findall(" (am|an) ", s)

# Results:
# ['am']
# ['am', 'an']

---

I want the results to be the same for each string.  What am I doing
wrong?
--
http://mail.python.org/mailman/listinfo/python-list


Re: question about xrange performance

2009-04-17 Thread MRAB

~flow wrote:

One might wonder why you are even writing code to test for existence
"in" a range list, when "blee <= blah < bloo" is obviously going to
outperform this kind of code.
-- Paul


the reason is simply the idiomacy and convenience. i use (x)xranges to
implement unicode blocks and similar things. it is natural to write
`if cid in latin_1` and so on. i always assumed it would be about the
fastest and memory-efficient to use xrange for this. i stumbled
against a first wall, tho, when i realized that `xrange` does not
accept long integers. so i wrote the class above to get an xrange-like
object that would accept them. stepping was not of concern---i recall
few times that i was in need of a third argument to xrange at all. i
then added iteration when i needed it; shows that iteration over
xxranges is outperformed by xrange by a factor of over twenty:

class xxrange( object ):
  def __iter__( self ):
i = self.start
while i < self.stop:
  yield i
  i += 1

containment
xxrange0.027 CPU seconds
xrange90.365 CPU seconds
set0.002 CPU seconds

iteration
xxrange0.262 CPU seconds
xrange 0.019 CPU seconds
set0.031 CPU seconds # iterated sorted list; tested as `for x
in sorted(my_set)`

however, these numbers also show still an incredible and unexpected
ratio in favor of xxrange for containment; they also show that a set
is (with 200'000 integer numbers) 10 times as fast for containment and
are only 1.6 times slower than xxranges for iteration.

this means that an optimized implemtation of x/range should choose
between different implementations depending on size of collection,
memory available, and purpose if they want to achieve better
efficiency. the least of changes could be

class xrange( object ):
  def __old_contains__:
...
  def __contains__( self ):
if self step != 1:
  return self.__old_contains__()
return ( x == int( x ) ) and self.start <= x < self.stop

the `( x == int( x ) )` is not easily being done away with if
emulation of present x/range behavior is desired (x.0 floats are in,
all others are out).

frankly speaking, i would like to say that finding out whether xrange
is a good solution for containment tests will cause some time of
reading or, of course, dedicated profiling; making Python make that
choice might be a better idea.

my guess would be that most xranges are in fact used with step 1, so
even sorting out that single bottleneck would have noticable effect.
now that xrange has taken over range in py3k people will get some
bumps on the transition way anyhow. i for one submit to my fancy
xxranges for the time.

cheers and thanks for the explanations!


In that case, I suppose that you need some sort of 'rangeset', a
class which is optimised for sets which include ranges of values.
--
http://mail.python.org/mailman/listinfo/python-list


class variables and class methods

2009-04-17 Thread krishnapostings
I have a class whose job is to serve several other objects, this
object is in a module 'M1', let's say it writes logs, no matter who
calls it, (once it started writing to a file) it must continue writing
to the same file, the file pointer could be a class variable here and
there is also no need to have object methods, all could be class
methods (my understanding of benefits between class and objects is not
good, see below).
1. Should I have one object 'O1'  that serves all i.e., no class
variables, class methods, if so, since this class is in a module
('M1'), and in several other modules this module is imported, should I
have in this 'M1'  a global variable 'g'  (let's call it module
variable) that points to the object, thus whoever wants to use the
object must import the module and through this module variable get
pointer to the object, thus no matter how many times who ever imports
there is only one object serving everyone as the object referred
through 'g' is in one namespace 'M1'.
This doesn't look elegant.
2. Should I have a class which does the job, and the class has a class
variable 'cv' which should point to the file pointer and the methods
should all be class methods. Now, from resource point of view, how
much is the benefit of making a method class method? how about in this
case where there are no object variables? Maybe the answers to these
questions answers the following question, Is there a case when we
would want to have a class with no object variables and all methods
class methods.

Thanks,
Krishna.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help improve program for parsing simple rules

2009-04-17 Thread Paul McGuire
On Apr 17, 2:40 pm, prueba...@latinmail.com wrote:
> On Apr 17, 11:26 am, Paul McGuire  wrote:
>
>
>
>
>
> > On Apr 16, 10:57 am, prueba...@latinmail.com wrote:
>
> > > Another interesting task for those that are looking for some
> > > interesting problem:
> > > I inherited some rule system that checks for programmers program
> > > outputs that to be ported: given some simple rules and the values it
> > > has to determine if the program is still working correctly and give
> > > the details of what the values are. If you have a better idea of how
> > > to do this kind of parsing please chime in. I am using tokenize but
> > > that might be more complex than it needs to be. This is what I have
> > > come up so far:
>
> > I've been meaning to expand on pyparsing's simpleArith.py example for
> > a while, to include the evaluation of the parsed tokens.  Here is the
> > online version,http://pyparsing.wikispaces.com/file/view/eval_arith.py,
> > it will be included in version 1.5.2 (coming shortly).  I took the
> > liberty of including your rule set as a list of embedded test cases.
>
> > -- Paul
>
> That is fine with me. I don't know how feasible it is for me to use
> pyparsing for this project considering I don't have admin access on
> the box that is eventually going to run this. To add insult to injury
> Python is in the version 2->3 transition (I really would like to push
> the admins to install 3.1 by the end of the year before the amount of
> code written by us gets any bigger) meaning that any third party
> library is an additional burden on the future upgrade. I can't
> remember if pyparsing is pure Python. If it is I might be able to
> include it alongside my code if it is not too big.- Hide quoted text -
>
> - Show quoted text -

It *is* pure Python, and consists of a single source file for the very
purpose of ease-of-inclusion.  A number of projects include their own
versions of pyparsing for version compatibility management, matplotlib
is one that comes to mind.

The upcoming version 1.5.2 download includes a pyparsing_py3.py file
for Python 3 compatibility, I should have that ready for users to
download *VERY SOON NOW*!

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Arnaud Delobelle
baykus  writes:

> Hi
>
> I am looking for one of those experimental languages that might be
> combination of python+basic. Now thta sounds weird and awkward I know.
> The reason I am asking is that I always liked how I could reference-
> call certain line number back in the days. It would be interesting to
> get similar functionality in Python.

I am currently working on such a "Python-Basic" programming language.
Here is the current implementation:

-- basic.py 
class GotoLine(Exception): pass

def goto(newlno): raise GotoLine(newlno)

def run(program):
lno = 0
env = { 'goto': goto }
try:
while lno <= max(program):
try:
if lno in program:
exec program[lno] in env
lno += 1
except GotoLine, g:
lno, = g.args
except Exception:
print "? Syntax error in line", lno
print "OK."


Example of use
==

marigold:junk arno$ python -i basic.py
>>> program = {
... 5: "# REM Python-Basic example program",
... 6: "# REM ",
... 10: "i = 0",
... 20: "print 'Hello, world', i",
... 30: "i = i + 1",
... 40: "if i < 10: goto(20)",
... 50: "name = raw_input('What is your name? ')",
... 60: "print 'Welcome,', name",
... 70: "gosub(80)"
... } 
>>> run(program)
Hello, world 0
Hello, world 1
Hello, world 2
Hello, world 3
Hello, world 4
Hello, world 5
Hello, world 6
Hello, world 7
Hello, world 8
Hello, world 9
What is your name? Arnaud
Welcome, Arnaud
? Syntax error in line 70
OK.
>>> 

As you can see, I haven't implemented gosub() yet.

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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread baykus
I guess I did not articulate myself well enough. I was just looking
for a toy to play around. I never suggested that Python+Basic would be
better than Python and everyone should use it. Python is Python and
Basic is Basic. I am not comparing them at all. I understand the
merits of Python but that does not mean I can play with ideas?

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


Sending directory with files in it via sockets

2009-04-17 Thread Ryniek90

Hi.
Last time i've got problem with sending big files, but i've already 
dealt with it.
Now, when i want send directory (with some files in it) i iterate that 
directory for files in it, and then in while loop open iterated files, 
read them and send. But something's not working. It iterate's first file 
from directory and sends it all the time (only first file). So, the 
iteration is written bad. Could you tell me how to modify it?


Here's that method:

def send_dir(self):
   print "Gathering information's..."
   self.direc = os.walk(self.data)
   for files in self.direc:
   lst_files = files[2]
   for fl in lst_files:
   print "Sending %s from directory %s" % (fl, 
os.path.split(self.data)[1])

   while True:
   files = open("%s/%s" % (self.data,fl), 'rb')
   self.read = files.read(51200)
   self.konn.send(self.read)
   if not self.read:
   break
   files.close()

"self.data" is variable for the "self.konn.recv(4096")  method.  
self.konn is from "self.konn, self.addr = self.sok.accept()"


"self.direc = os.walk(self.data)
   for files in self.direc:
   lst_files = files[2]"

Iteration result looks like this:

>>> import os
>>> direc = os.walk('/media/DOWNLOAD/Obrazy płyt/Half-Life 2')
>>> for files in direc:
   lst_files = files[2]

  
>>> lst_files

['hl2_1.nrg', 'hl2_2.nrg', 'hl2_4.nrg', 'hl2_5.nrg']
>>> for fl in lst_files:
   print fl

  
hl2_1.nrg

hl2_2.nrg
hl2_4.nrg
hl2_5.nrg
>>>

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


RE: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Leguia, Tony
Though I don't know why you would want to reference lines numbers, I assume 
it's for goto statements or something similar. 
With that said please read: 
1) 
http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html

I would also like to put forth my opinion, shared by many in the community, 
that Basic is actually dangerous as an educational programming language, and 
that writing 
large professional code in it is hard, and actually hampered by the language. 
I'm not trying to to start a flame war here but this post almost made me cry. 

Also python is functional, it's so powerful. Grow and learn to take advantage 
of that. Why hold yourself back?
 

From: python-list-bounces+leguiato=grinnell@python.org 
[python-list-bounces+leguiato=grinnell@python.org] On Behalf Of baykus 
[baykusde...@gmail.com]
Sent: Friday, April 17, 2009 3:37 PM
To: python-list@python.org
Subject: Is there a programming language that is combination of Python and  
Basic?

Hi

I am looking for one of those experimental languages that might be
combination of python+basic. Now thta sounds weird and awkward I know.
The reason I am asking is that I always liked how I could reference-
call certain line number back in the days. It would be interesting to
get similar functionality in Python.
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list


Re: cPickle and subclassing lists?

2009-04-17 Thread Carl Banks
On Apr 17, 10:13 am, Reckoner  wrote:
> I have a large class that is a child of list. I need to pickle it, but
> it's not working. For example, I have reduced it to the following:
>
> class Mylist(list):
>     def __init__(self,x=[]):
>         list.__init__(self,x)
>
> and I cannot even get this to pickle right.
>
> >> w=Mylist([1,2,3])
> >> dumps(w)
>
> PicklingError: Can't pickle : attribute lookup
> __main__.p fa
> iled
>
> I'm using python 2.5 on win32.
>
> any help appreciated.

It worked for me.

Wild guess: you've defined Mylist in a module (or, rather, you defined
class p in an module and assigned Mylist to it), but you are exec'ing
the module rather than importing it, ostensibly because importing a
module won't load any changes you've made into the interpreter.  Well,
doing that will cause funny things to happen when pickling.  If you're
doing that, consider the reload function instead (although it has it's
own problems).

I'd highly recommend against pickling an instance of a class that
isn't defined in, and loaded from, a regular module.  Don't pickle
instances of classes defined at the interpreter or through exec.


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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Mensanator
On Apr 17, 3:37 pm, baykus  wrote:
> Hi
>
> I am looking for one of those experimental languages that might be
> combination of python+basic. Now thta sounds weird and awkward I know.

That's a clue you won't find anyone seriously contemplating
such idiocy.

> The reason I am asking is that I always liked how I could reference-
> call certain line number back in the days.

A bad idea. If you really want to write bad code, learn C.

> It would be interesting to get similar functionality in Python.

Yeah, it would "interesting" just as a train wreck is "interesting",
as long as you're not the one who has to live through it.

I once translated a BASIC program to Pascal (hint: no goto allowed).
The original code had GOSUBs that never executed a REURN because
the programmer jumped away to line numbers on a whim. Biggest piece
of crap I ever had to misfortune to deal with.

No one has ever missed what you're pining for.

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


Re: script question

2009-04-17 Thread python
Peter,

> Another eval-free variant:
> 
> [x() for x in vars().values() if hasattr(x, "_included")]
> 
> If you use getattr(x, "_included", False) instead of hasattr()
> you can "un-include" functions with ...

YES! That's what I was struggling to do with my in-elegant use of
eval(), eg. replacing eval() to expand dir() strings with a dictionary
of local objects.

Question: why vars() vs. globals()? My tests in IDLE (Python 2.6) show
vars() and globals() returning the same items.

Thanks,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list


Find and replace is appending when run more than once ...

2009-04-17 Thread Paul . Scipione
Hello,

I grabbed some sample code from the web and put together this python script 
which searches all subdirectories for a specific file type, then replaces a 
string with a new one.  Out of curiosity I ran it again.  Here's what happens 
(this also happens for the files I am looking to replace OutputPath=Z:\ with 
OutputPath=Z:\DXF):

1st run:  replaces OutputPath=Z:\ with OutputPath=Z:\CSV )
2nd run: replaces OutputPath=Z:\CSV with OutputPath=Z:\CSV\CSV
3rd run:  replaces OutputPath=Z:\CSV\CSV with OutputPath=Z:\CSV\CSV\CSV
And so on.

I cant figure out why it's doing this if I am searching a certain string value 
and replacing it.  I am also perplexed on why it is only adding the "CSV" each 
time rather than the full string.  What I want it to do is only replace once.  
Next time I run it it shouldn't do anything.

import fnmatch,os,sys

mydir = 'C:\\!DOMSExtractor'
findStr = 'OutputPath=Z:\\'

def replaceStringInFile(findStr,replStr,filePath):
tempName=filePath+'~~~'
input = open(filePath)
output = open(tempName,'w')

for s in input:
print findStr
print replStr
output.write(s.replace(findStr,replStr))
output.close()
input.close()
os.remove(filePath)
os.rename(tempName,filePath)
print filePath

def myfun(dummy, dirr, filess):
for child in filess:
filePath = dirr+'/'+child
if '.ini' == os.path.splitext(child)[1] and os.path.isfile(filePath):
print file(filePath, 'rb').read().find(findStr)
if file(filePath, 'rb').read().find(findStr) <> -1:
if fnmatch.fnmatch(dirr+'/'+child, '*BGExtract.ini'):
replaceStringInFile(findStr,'OutputPath=Z:\\DXF\\',filePath)
elif fnmatch.fnmatch(dirr+"/"+child, '*GISExtract.ini'):
replaceStringInFile(findStr,'OutputPath=Z:\\CSV\\',filePath)

os.path.walk(mydir, myfun, 3)


Thanks!

Paul J. Scipione
GIS Database Administrator
work: 602-371-7091
cell: 480-980-4721



Email Firewall made the following annotations

-
--- NOTICE ---

This message is for the designated recipient only and may contain confidential, 
privileged or proprietary information.  If you have received it in error, 
please notify the sender immediately and delete the original and any copy or 
printout.  Unintended recipients are prohibited from making any other use of 
this e-mail.  Although we have taken reasonable precautions to ensure no 
viruses are present in this e-mail, we accept no liability for any loss or 
damage arising from the use of this e-mail or attachments, or for any delay or 
errors or omissions in the contents which result from e-mail transmission.

-

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


send() to a generator in a "for" loop with continue(val)??

2009-04-17 Thread Dale Roberts
I've started using generators for some "real" work (love them!), and I
need to use send() to send values back into the yield inside the
generator. When I want to use the generator, though, I have to
essentially duplicate the machinery of a "for" loop, because the "for"
loop does not have a mechanism to send into the generator. Here is a
toy example:

def TestGen1():
for i in xrange(3):
sendval = yield i
print "   got %s in TestGen()" % sendval

g = TestGen1()
sendval = None
try:
while True:
val = g.send(sendval)
print 'val in "while" loop %d' % val
sendval = val * 10
except StopIteration: pass

I have to explicitly create the generator with an assignment, send an
initial None to the generator on the first go, then have to catch the
StopIteration exception. In other words, replicate the "for"
mechanism, but use send() instead of next().

It would be nice if I could just do this instead:

for val in TestGen1():
print 'val in "for" loop %d' % val
continue(val*10)

...or something similar. Is this an old idea? Has it been shot down in
the past already? Or is it worth pursuing? I Googled around and saw
one hit here: 
http://mail.python.org/pipermail/python-ideas/2009-February/003111.html,
but not much follow-up.

I wonder if people want to keep the idea of an "iterator" style
generator (where send() is not used) separate from the idea of a "co-
routine" style generator (where send() is used). Maybe combining the
two idioms in this way would cause confusion?

What do folks think?

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


Re: question about xrange performance

2009-04-17 Thread ~flow
> One might wonder why you are even writing code to test for existence
> "in" a range list, when "blee <= blah < bloo" is obviously going to
> outperform this kind of code.
> -- Paul

the reason is simply the idiomacy and convenience. i use (x)xranges to
implement unicode blocks and similar things. it is natural to write
`if cid in latin_1` and so on. i always assumed it would be about the
fastest and memory-efficient to use xrange for this. i stumbled
against a first wall, tho, when i realized that `xrange` does not
accept long integers. so i wrote the class above to get an xrange-like
object that would accept them. stepping was not of concern---i recall
few times that i was in need of a third argument to xrange at all. i
then added iteration when i needed it; shows that iteration over
xxranges is outperformed by xrange by a factor of over twenty:

class xxrange( object ):
  def __iter__( self ):
i = self.start
while i < self.stop:
  yield i
  i += 1

containment
xxrange0.027 CPU seconds
xrange90.365 CPU seconds
set0.002 CPU seconds

iteration
xxrange0.262 CPU seconds
xrange 0.019 CPU seconds
set0.031 CPU seconds # iterated sorted list; tested as `for x
in sorted(my_set)`

however, these numbers also show still an incredible and unexpected
ratio in favor of xxrange for containment; they also show that a set
is (with 200'000 integer numbers) 10 times as fast for containment and
are only 1.6 times slower than xxranges for iteration.

this means that an optimized implemtation of x/range should choose
between different implementations depending on size of collection,
memory available, and purpose if they want to achieve better
efficiency. the least of changes could be

class xrange( object ):
  def __old_contains__:
...
  def __contains__( self ):
if self step != 1:
  return self.__old_contains__()
return ( x == int( x ) ) and self.start <= x < self.stop

the `( x == int( x ) )` is not easily being done away with if
emulation of present x/range behavior is desired (x.0 floats are in,
all others are out).

frankly speaking, i would like to say that finding out whether xrange
is a good solution for containment tests will cause some time of
reading or, of course, dedicated profiling; making Python make that
choice might be a better idea.

my guess would be that most xranges are in fact used with step 1, so
even sorting out that single bottleneck would have noticable effect.
now that xrange has taken over range in py3k people will get some
bumps on the transition way anyhow. i for one submit to my fancy
xxranges for the time.

cheers and thanks for the explanations!
--
http://mail.python.org/mailman/listinfo/python-list


Re: cPickle and subclassing lists?

2009-04-17 Thread Reckoner
On Apr 17, 11:16 am, Piet van Oostrum  wrote:
> > Reckoner  (R) wrote:
> >R> I have a large class that is a child of list. I need to pickle it, but
> >R> it's not working. For example, I have reduced it to the following:
> >R> class Mylist(list):
> >R> def __init__(self,x=[]):
> >R>   list.__init__(self,x)
> >R> and I cannot even get this to pickle right.
>  w=Mylist([1,2,3])
>  dumps(w)
> >R> PicklingError: Can't pickle : attribute lookup
> >R> __main__.p fa
> >R> iled
>
> Where does the 'p' come from?
> What is w.__class__ ?
>
> >R> I'm using python 2.5 on win32.
>
> No problem with 2.6.2 on Mac OS X.
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

Thanks for all the great feedback. This turns out to be a problem with
the IPython interpreter,
and not a Python problem.

sorry for the confusion.


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


Re: unpythonic use of property()?

2009-04-17 Thread Carl Banks
On Apr 17, 10:21 am, J Kenneth King  wrote:
> Consider:
>
> code:
> 
>
> class MyInterface(object):
>
>     def __get_id(self):
>         return self.__id
>
>     id = property(fget=__get_id)
>
>     def __init__(self, id, foo):
>         self.__id = id
>         self.foo = foo
>
> class MyInterface2(object):
>
>     def __init__(self, id, foo):
>         self._id = id
>         self.foo = foo
>
>     @property
>     def id(self):
>         return self._id
>
> 
>
> The situation is that an API to an external resource must enforce
> consistency with the resource's expectations of data (ie: ID's must be
> immutable).
>
> The Python docs clearly show the use of the property() decorator in both
> the example classes shown here. Both ensure that an assignment to the
> class objects' 'id' attribute will raise an AttributeError
> exception. Both will satisfy the requirements.
>
> I assume we all know what the implementation differences are between the
> two examples here. What I am curious about is what use case would the
> MyInterface example have? My guess is that it's added protection for
> attribute names on objects generated from factories or generators. I'd
> like a more concrete explanation from someone who has encountered a
> scenario where it was required.

The MyInterface2 version is preferred because it avoids creating an
extra symbol in the class's namespace.  That's pretty much it.

The MyInterface example was how you had to do it before there were
decorators.  It still works, and the world won't end if you do it that
way.  There is not really much practical difference between the two
versions to someone using the class.

Also, the MyInterface version stores its data in a name-mangled
attribute (two underscores), but you can also use a name-mangled
attribute for the MyInterface2 version if you want.

Neither version is foolproof.


> I was recently informed that it was 'unpythonic' and have since been a
> little confused by the term. I've heard it bandied about before but
> never paid much attention. What is 'unpythonic'? What about this example
> is unpythonic?

There are different reasons someone might have said it.

It's probably just for the reason I mentioned.  Python likes to have
an quasi-official way to do basic things such as define properties.
("There should be one--and preferrably only one--obvious way to do
it.")  That current quasi-official way is to use property is as a
decorator, for the reason I gave: it avoids namespace pollution.

Some people think attribute name-mangling is unpythonic.  It's true
that people sometimes mistakenly treat it a solid information hiding
mechanism, but I wouldn't call its usage unpythonic when used as
intended: as a way to avoid name-collisions.  If you think it's
worthwhile to protect an attribute from being overwritten, you might
as well guard against accidental conflict with the underlying name.

Finally, some people think read-only attributes are unpythonic.  I
think that's ridiculous, although in general I'd advise against making
attributes read-only willy-nilly.  But there's a time and place for
it.

Last thing I'll advise is don't get too hung up on terms like
"Pythonic".  Treat such labels are more of a red flag and get people
who throw out the term to explain why.


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


Re: Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread Aahz
In article ,
baykus   wrote:
>
>I am looking for one of those experimental languages that might be
>combination of python+basic. Now thta sounds weird and awkward I know.
>The reason I am asking is that I always liked how I could reference-
>call certain line number back in the days. It would be interesting to
>get similar functionality in Python.

Why do you want to do that?  Before you answer, make sure to read this:

http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pathological regular expression

2009-04-17 Thread franck
> To my mind, this is a bug in the RE engine. Is there any reason to not
> treat it as a bug?

It's not a bug, it's a feature! ;-)
Indeed, in order to handle constructs like (?P=name), RE engines have
to use inefficient algorithms. In my opinion, this is not the problem
of using a pathological regular expression, but rather a problem of
the RE engine that is not optimized to use the faster approach when
possible.

This is well known problem very well explained on:
http://swtch.com/~rsc/regexp/regexp1.html

Cheers,
Franck
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-17 Thread Hrvoje Niksic
"J. Cliff Dyer"  writes:

> On Thu, 2009-04-16 at 13:33 +0200, Hrvoje Niksic wrote:
>> mousemeat  writes:
>> 
>> > Correct me if i am wrong, but i can pickle an object that contains a
>> > bound method (it's own bound method).
>> 
>> No, you can't:
>> 
>> >>> import cPickle as p
>> >>> p.dumps([])
>> '(l.'
>> >>> p.dumps([].append)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> TypeError: expected string or Unicode object, NoneType found
>
> Yes he can.  mousemeat stated that he could pickle an object that
> *contains* a bound method, not that he could pickle the method
> itself.

To pickle an object that contains something, that something still
needs to be picklable.

>>> import cPickle as p
>>> class X(object): pass
...
>>> x = X()
>>> x.foo = [].append
>>> p.dumps(x)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: expected string or Unicode object, NoneType found

> That said, you can make an instance method out of a lambda, just as
> well as any named function, and you can pickle that object, too:
[...]

The object in your example doesn't contain an instance method, its
class contains a function which gets converted to a bound method
if/when it is accessed through the instance.  Pickling the object
doesn't pickle the method at all, because it's left up to the class to
provide it after unpickling.

On the other hand, the OP had a use case where he tried to pickle an
object that actually contained lambdas as part of its data, such as:

class Foo(object):
pass

foo = Foo()
foo.a = lambda x: x+1

Now pickling foo requires pickling the lambda, which doesn't work.  My
point was that this, on its own, is no reason to avoid lambdas in
general, because by that logic one would also avoid bound methods,
which are also unpicklable.
--
http://mail.python.org/mailman/listinfo/python-list


Is there a programming language that is combination of Python and Basic?

2009-04-17 Thread baykus
Hi

I am looking for one of those experimental languages that might be
combination of python+basic. Now thta sounds weird and awkward I know.
The reason I am asking is that I always liked how I could reference-
call certain line number back in the days. It would be interesting to
get similar functionality in Python.
--
http://mail.python.org/mailman/listinfo/python-list


after_cancel?

2009-04-17 Thread W. eWatson
I'm looking a program that I'm not real familiar with that uses an 
after_cancel method and after_id variable.  Are they related to some 
particular widget and what is there function? Perhaps they are related to a 
Cancel button on a widget?

--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: [OT] large db question about no joins

2009-04-17 Thread Martin P. Hellwig

Daniel Fetchinson wrote:


In an relational database setting you would have a table for artists,
a table for cd's and a table for songs and a table for comments where
people can comment on songs. All of this with obvious foreign keys.
Now you want to display on your website the total number of cd's, the
total number of songs and the total number of comments because that
looks very cool on top of every page: 1242342 cd's and 134242342342
songs and 284284728347284728 comments!

Okay, so you need to issue a query with joins in your relational db.
Or in another query, you want to select all comments of an artist,
meaning all comments that were comments on a song belonging to cd's of
a given artist. This would also involve joins.

How would I do these queries if I can't have joins or in other words
how would I lay out my data storage?

Thanks by the way for your help and replies,
Daniel



What actually is happening here is that feature x which is usually in an 
abstraction layer (like a RDBM) is not available to you. Which means you 
have to write code which does the same but on your side of the code.


I played a bit around with the GAE some time ago and designed the 
storage of all data in an sort of EAV model 
(http://en.wikipedia.org/wiki/Entity-Attribute-Value_model)
The abstraction layer had all my meta code, counting stuff up was a 
matter of creating a list of id's from one section and with that list 
selecting the appropriate records on the other section, then count up 
the number of results or use the result again, it is very likely you 
have to do quite a lot of queries before you get the result.


So in other words, just lay out the data which makes the most sense to 
you, the pain of recreating SQL like logic is there no matter what 
layout you choose.


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


Re: script question

2009-04-17 Thread Peter Otten
pyt...@bdurham.com wrote:

> For completeness, here is the corrected version of my original code for
> the archives:
> 
> [ eval(x)() for x in dir() if hasattr( eval(x), '_included') ]

Another eval-free variant:

[x() for x in vars().values() if hasattr(x, "_included")]

If you use getattr(x, "_included", False) instead of hasattr() you
can "un-include" functions with 

f._included = False 

instead of 

del f._included

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


Re: Help improve program for parsing simple rules

2009-04-17 Thread Aaron Brady
On Apr 17, 2:01 pm, Paul McGuire  wrote:
> On Apr 17, 1:26 pm, Aaron Brady  wrote:
>
> > Hi, not to offend; I don't know your background.  
>
> Courtesy on Usenet!!!  I'm going to go buy a lottery ticket!
>
> Not to worry, I'm a big boy.  People have even called my baby ugly,
> and I manage to keep my blood pressure under control.
>
>
>
> > One thing I like
> > about Python is it and the docs are careful about short-circuiting
> > conditions.  ISTR that C left some of those details up to the compiler
> > at one point.
>
> > >>> def f():
>
> > ...     print( 'in f' )
> > ...     return 10
> > ...>>> 0
> > in f
> > True>>> 0
> > in f
> > in f
> > True
>
> > Therefore, if op{n} has side effects, 'op1 operator1 op2 AND op2
> > operator2 op3' is not equivalent to 'op1 optor1 op2 optor2 op3'.
>
> Interesting point, but I don't remember that "A < B < C" is valid C
> syntax, are you perhaps thinking of a different language?
>
> By luck, my implementation of EvalComparisonOp.eval does in fact
> capture the post-eval value of op2, so that if its evaluation caused
> any side effects, they would not be repeated.
>
> -- Paul

It was very hazy in my memory and is easily solved by a web search.
Perhaps I was thinking of the short-circuiting of 'and', and mixing it
up with the warning about parentheses in macros.

-Macros?  NO, Batman.
-I'm afraid so Robin.  Macros.
--
http://mail.python.org/mailman/listinfo/python-list


Re: script question

2009-04-17 Thread python
Scott,

Thank you for your example - very clean.

For completeness, here is the corrected version of my original code for
the archives:

[ eval(x)() for x in dir() if hasattr( eval(x), '_included') ]

Regards,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help improve program for parsing simple rules

2009-04-17 Thread pruebauno
On Apr 17, 11:26 am, Paul McGuire  wrote:
> On Apr 16, 10:57 am, prueba...@latinmail.com wrote:
>
> > Another interesting task for those that are looking for some
> > interesting problem:
> > I inherited some rule system that checks for programmers program
> > outputs that to be ported: given some simple rules and the values it
> > has to determine if the program is still working correctly and give
> > the details of what the values are. If you have a better idea of how
> > to do this kind of parsing please chime in. I am using tokenize but
> > that might be more complex than it needs to be. This is what I have
> > come up so far:
>
> I've been meaning to expand on pyparsing's simpleArith.py example for
> a while, to include the evaluation of the parsed tokens.  Here is the
> online version,http://pyparsing.wikispaces.com/file/view/eval_arith.py,
> it will be included in version 1.5.2 (coming shortly).  I took the
> liberty of including your rule set as a list of embedded test cases.
>
> -- Paul

That is fine with me. I don't know how feasible it is for me to use
pyparsing for this project considering I don't have admin access on
the box that is eventually going to run this. To add insult to injury
Python is in the version 2->3 transition (I really would like to push
the admins to install 3.1 by the end of the year before the amount of
code written by us gets any bigger) meaning that any third party
library is an additional burden on the future upgrade. I can't
remember if pyparsing is pure Python. If it is I might be able to
include it alongside my code if it is not too big.
--
http://mail.python.org/mailman/listinfo/python-list


Re: script question

2009-04-17 Thread Arnaud Delobelle
"D'Arcy J.M. Cain"  writes:

> On 17 Apr 2009 07:03:18 -0700
> a...@pythoncraft.com (Aahz) wrote:
>> Go to all that trouble, you might as well make it easier:
>> 
>> for func in funclist:
>> func()
>
> And if you need the return values:
>
> retlist = [func() for func in funclist]

And if you don't want to be Python 3 compliant:

retlist = map(apply, funclist)

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


Re: script question

2009-04-17 Thread Scott David Daniels

pyt...@bdurham.com wrote:

Scott,

Newbie question (and I'm not the OP): What are your thoughts on having
your decorator add an attribute to the functions vs. placing the
functions in a global variable?

def _included(f):
f._included = True
return f

I tried experimenting with this technique, but could not find a way to
use dir() to discover all my functions with an _included attribute.

The following convoluted comprehension works to execute functions with
an _included attribute when a list of function names is available
However, I can't figure out a way to replace my list of hard-coded
function names with the output from the dir() command.


Here's how I'd do it:

def call_all(module):
for name in dir(module):
contents = getattr(module, name)
if hasattr(contents, '_included'):
yield contents()

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: question about xrange performance

2009-04-17 Thread Paul McGuire
On Apr 17, 1:39 pm, _wolf  wrote:
>
> can it be that a simple diy-class outperforms a python built-in by a
> factor of 180? is there something i have done the wrong way?
> omissions, oversights? do other people get similar figures?
>
> cheers

I wouldn't say you are outperforming xrange until your class also
supports:

for i in xxrange( 1, 2 ):
# do something with i

Wouldn't be difficult, but you're not there yet.

And along the lines with MRAB's comments, xrange is not really
intended for "in" testing, it is there for iteration over a range
without constructing the list of range elements first, which one
notices right away when looping over xrange(1e8) vs. range(1e8).

Your observation is especially useful to keep in mind as Python 3 now
imbues "range" with "xrange" behavior, so if you have code that tests
"blah in range(blee,bloo):", you will get similar poor results.)

And of course, you are cheating a bit with your xxrange "in" test,
since you aren't really verifying that the number is actually in the
given list, you are just testing against the extrema, and relying on
your in-built knowledge that xrange (as you are using it) contains all
the intermediate values.  Compare to testing with xrange(1,100,2) and
you'll find that 10 is *not* in this range, even though 1 <= 10 <
100.  (Extending xxrange to do this as well is also

One might wonder why you are even writing code to test for existence
"in" a range list, when "blee <= blah < bloo" is obviously going to
outperform this kind of code.

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


Re: question about xrange performance

2009-04-17 Thread Peter Otten
_wolf wrote:

> lately i realized a slow running portion of my application, and a
> quick profiling nourished the suspicion that, of all things, calls to
> `xrange().__contains__` (`x in b` where `b = xrange(L,H)`) is the
> culprit. to avoid any other influences, i wrote this test script with
> class `xxrange` being a poor man’s `xrange` replacement:
> 
> 
> 
> class xxrange( object ):
>   def __init__( self, start, stop ):
> self.start  = start
> self.stop   = stop
>   def __contains__( self, x ):
> return ( x == int( x ) ) and self.start <= x < self.stop

I haven't read 

http://mail.python.org/pipermail/python-3000/2007-July/008971.html

completely, but from what I've read a patch might be accepted. Personally I
will continue to write

start <= x < stop

I don't remember an occasion where I needed the int(x) == x incantation or a
step (that you didn't implement).

Peter

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


Re: Help improve program for parsing simple rules

2009-04-17 Thread Paul McGuire
On Apr 17, 1:26 pm, Aaron Brady  wrote:
> Hi, not to offend; I don't know your background.  

Courtesy on Usenet!!!  I'm going to go buy a lottery ticket!

Not to worry, I'm a big boy.  People have even called my baby ugly,
and I manage to keep my blood pressure under control.

> One thing I like
> about Python is it and the docs are careful about short-circuiting
> conditions.  ISTR that C left some of those details up to the compiler
> at one point.
>
> >>> def f():
>
> ...     print( 'in f' )
> ...     return 10
> ...>>> 0
> in f
> True>>> 0
> in f
> in f
> True
>
> Therefore, if op{n} has side effects, 'op1 operator1 op2 AND op2
> operator2 op3' is not equivalent to 'op1 optor1 op2 optor2 op3'.

Interesting point, but I don't remember that "A < B < C" is valid C
syntax, are you perhaps thinking of a different language?

By luck, my implementation of EvalComparisonOp.eval does in fact
capture the post-eval value of op2, so that if its evaluation caused
any side effects, they would not be repeated.

-- Paul

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


Re: Lambda alternative?

2009-04-17 Thread Aaron Brady
On Apr 17, 1:43 pm, "J. Cliff Dyer"  wrote:
> On Thu, 2009-04-16 at 13:33 +0200, Hrvoje Niksic wrote:
> > mousemeat  writes:
>
> > > Correct me if i am wrong, but i can pickle an object that contains a
> > > bound method (it's own bound method).
>
> > No, you can't:
>
> > >>> import cPickle as p
> > >>> p.dumps([])
> > '(l.'
> > >>> p.dumps([].append)
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > TypeError: expected string or Unicode object, NoneType found
>
> Yes he can.  mousemeat stated that he could pickle an object that
> *contains* a bound method, not that he could pickle the method itself.
>
> That said, you can make an instance method out of a lambda, just as well
> as any named function, and you can pickle that object, too:
snip

'Contains' here is ambiguous.  If the object contains a bound method,
that is, if a bound method is in its dictionary, you can't.

>>> import pickle as p
>>> class A: pass
...
>>> a= A()
>>> class A:
... def f( self ): print( 'f' )
...
>>> a= A()
>>> class B: pass
...
>>> b= B()
>>> b.f= a.f
>>> b.f()
f
>>> p.dumps( b )
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Programs\Python30\lib\pickle.py", line 1329, in dumps
Pickler(f, protocol).dump(obj)
_pickle.PicklingError: Can't pickle : attribute
lookup
builtins.method failed

In this example, 'b' contains a bound method, 'a.f'.  However, for
other definitions of 'contains', such as if 'b' is an instance of a
class that contains methods, you can.  But in that case, the method is
not in 'b.__dict__'.

>>> b.__dict__
{'f': >}

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


Re: question about xrange performance

2009-04-17 Thread MRAB

_wolf wrote:

lately i realized a slow running portion of my application, and a
quick profiling nourished the suspicion that, of all things, calls to
`xrange().__contains__` (`x in b` where `b = xrange(L,H)`) is the
culprit. to avoid any other influences, i wrote this test script with
class `xxrange` being a poor man’s `xrange` replacement:



class xxrange( object ):
  def __init__( self, start, stop ):
self.start  = start
self.stop   = stop
  def __contains__( self, x ):
return ( x == int( x ) ) and self.start <= x < self.stop

import cProfile
from random import randint
test_integers = [ randint 0, 5000 ) for i in xrange( 8000 ) ]
test_range_a  = xxrange( 1, 2 )
test_range_b  = xrange(  1, 2 )

def a():
  print test_range_a.__class__.__name__
  for x in test_integers:
x in test_range_a

def b():
  print test_range_b.__class__.__name__
  for x in test_integers:
x in test_range_b

cProfile.run('a()')
cProfile.run('b()')


now this is the output, surprise:


xxrange
 8003 function calls in 0.026 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno
(function)
10.0000.0000.0260.026 :1()
10.0120.0120.0260.026 xrange-profiler.py:18(a)
 80000.0140.0000.0140.000 xrange-profiler.py:9
(__contains__)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}


xrange
 3 function calls in 4.675 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno
(function)
10.0000.0004.6754.675 :1()
14.6754.6754.6754.675 xrange-profiler.py:23(b)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}


can it be that a simple diy-class outperforms a python built-in by a
factor of 180? is there something i have done the wrong way?
omissions, oversights? do other people get similar figures?


xrange() returns an xrange object, which generates its values on demand.
It doesn't have a __contains__ method, so 'in' uses its iterator, making
the xrange object yield each value until either the desired value is
produced or there are no more values.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [OT] large db question about no joins

2009-04-17 Thread J. Cliff Dyer
On Thu, 2009-04-16 at 14:11 -0700, John Fabiani wrote:
> Daniel Fetchinson wrote:
> 
> > Hi folks, I've come across many times the claim that 'joins are bad'
> > for large databases because they don't scale
> 
> IMO that's bull...

OK.  That makes four legs so far

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

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


Re: Lambda alternative?

2009-04-17 Thread J. Cliff Dyer
On Thu, 2009-04-16 at 13:33 +0200, Hrvoje Niksic wrote:
> mousemeat  writes:
> 
> > Correct me if i am wrong, but i can pickle an object that contains a
> > bound method (it's own bound method).
> 
> No, you can't:
> 
> >>> import cPickle as p
> >>> p.dumps([])
> '(l.'
> >>> p.dumps([].append)
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: expected string or Unicode object, NoneType found

Yes he can.  mousemeat stated that he could pickle an object that
*contains* a bound method, not that he could pickle the method itself.

That said, you can make an instance method out of a lambda, just as well
as any named function, and you can pickle that object, too:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle as p
>>> class Foo(object):
... a = lambda self, x: x+1
>>> foo.a(1)
2
>>> type(foo.a)

>>> p.dumps(foo)
'ccopy_reg\n_reconstructor\np1\n(c__main__\nFoo\np2\nc__builtin__
\nobject\np3\nNtRp4\n.'

Cheers,
Cliff


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


question about xrange performance

2009-04-17 Thread _wolf
lately i realized a slow running portion of my application, and a
quick profiling nourished the suspicion that, of all things, calls to
`xrange().__contains__` (`x in b` where `b = xrange(L,H)`) is the
culprit. to avoid any other influences, i wrote this test script with
class `xxrange` being a poor man’s `xrange` replacement:



class xxrange( object ):
  def __init__( self, start, stop ):
self.start  = start
self.stop   = stop
  def __contains__( self, x ):
return ( x == int( x ) ) and self.start <= x < self.stop

import cProfile
from random import randint
test_integers = [ randint 0, 5000 ) for i in xrange( 8000 ) ]
test_range_a  = xxrange( 1, 2 )
test_range_b  = xrange(  1, 2 )

def a():
  print test_range_a.__class__.__name__
  for x in test_integers:
x in test_range_a

def b():
  print test_range_b.__class__.__name__
  for x in test_integers:
x in test_range_b

cProfile.run('a()')
cProfile.run('b()')


now this is the output, surprise:


xxrange
 8003 function calls in 0.026 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno
(function)
10.0000.0000.0260.026 :1()
10.0120.0120.0260.026 xrange-profiler.py:18(a)
 80000.0140.0000.0140.000 xrange-profiler.py:9
(__contains__)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}


xrange
 3 function calls in 4.675 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno
(function)
10.0000.0004.6754.675 :1()
14.6754.6754.6754.675 xrange-profiler.py:23(b)
10.0000.0000.0000.000 {method 'disable' of
'_lsprof.Profiler' objects}


can it be that a simple diy-class outperforms a python built-in by a
factor of 180? is there something i have done the wrong way?
omissions, oversights? do other people get similar figures?

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


Re: need to start a new project , can python do all that ?

2009-04-17 Thread Hyuga
On Apr 15, 10:54 am, Deep_Feelings  wrote:
> On Apr 15, 4:05 pm, Tim Rowe  wrote:
>
>
>
> > 2009/4/15 Deep_Feelings :
>
> > > I want to start programming a new program (electronic health care
> > > center) in python and before start learning python i wanna make sure
> > > that python does have all the features i need to accomplish this
> > > project so i wanna ask you does python able to support these
> > > features :
>
> > > 1- cross platform (windows + linux)
> > > 2- mysql database access
> > > 3- 2D graphs (curves)
> > > 4- support of international languages
> > > 5- can access a scanner and input pictures from it.
>
> > > and possibly be able to import data from labratory machines (such as
> > > CBC machines) to automatically register patient investigations result
> > > into the system (not mandatory)
>
> > What are the safety and security requirements? If you're handling
> > patient investigation results then there are certainly security issues
> > because of patient confidentiality, and there may be safety issues
> > (could a software fault contribute to a patient receiving incorrect
> > treatment, or failing to receive necessary treatment?)
>
> > You almost certainly need to contact the appropriate regulatory
> > authority to check whether they have any requirements for languages in
> > such applications (and for specific development processes), or you
> > could find yourself either with an application you can't use or a very
> > big lawsuit and possibly jail if it goes wrong.
>

> thank you so much ,rest assured that the code will me tested very well
> (in real world situation) before using it.

I'm not too assured... What are the actual requirements for this
software?  Is this intended for real world use in health care?  I'm
not too comfortable with a single individual with apparently limited
experience in Python developing something like that.  Not that it's
any of my business...or for all I know it may be!  You might as well
have started this post "I want to start programming a new program (air
traffic control system) in python and before start learning python..."
--
http://mail.python.org/mailman/listinfo/python-list


Re: binary file compare...

2009-04-17 Thread Adam Olsen
On Apr 17, 9:59 am, SpreadTooThin  wrote:
> You know this is just insane.  I'd be satisfied with a CRC16 or
> something in the situation i'm in.
> I have two large files, one local and one remote.  Transferring every
> byte across the internet to be sure that the two files are identical
> is just not feasible.  If two servers one on one side and the other on
> the other side both calculate the CRCs and transmit the CRCs for
> comparison I'm happy.

Definitely use a hash, ignore Nigel.  SHA-256 or SHA-512.  Or, if you
might need to update one of the files, look at rsync.  Rsync still
uses MD4 and MD5 (optionally!), but they're fine in a trusted
environment.
--
http://mail.python.org/mailman/listinfo/python-list


ANN: ConfigObj 4.6.0 and Validate 1.0.0 released

2009-04-17 Thread Fuzzyman
Finally a fresh release ConfigObj and Validate.

* ConfigObj Home page: http://www.voidspace.org.uk/python/configobj.html
* Validate Home page: http://www.voidspace.org.uk/python/validate.html

**ConfigObj** is a simple to use but powerful Python library for the
reading and writing of configuration (ini) files. Through **Validate**
it integrates a config file validation and type conversion system.

Features of ConfigObj include:

* Nested sections (subsections), to any level
* List values
* Multiple line values
* Full Unicode support
* String interpolation (substitution)
* Integrated with a powerful validation system

- including automatic type checking/conversion
- and allowing default values
- repeated sections

* All comments in the file are preserved
* The order of keys/sections is preserved
* Powerful ``unrepr`` mode for storing/retrieving Python data-types

Release 4.6.0 fixes bugs and adds new features, particularly making
configspec handling more flexible.

Full details on the changes can be found at:
http://www.voidspace.org.uk/python/weblog/arch_d7_2009_04_11.shtml#e1078

The changelog for ConfigObj 4.6.0 is:

* Pickling of ConfigObj instances now supported (thanks to Christian
Heimes)
* Hashes in confgspecs are now allowed (see note below)
* Replaced use of hasattr (which can swallow exceptions) with getattr
* ``__many__`` in configspecs can refer to scalars (ordinary values)
as well as sections
* You can use ``___many___`` (three underscores!) where you want to
use ``__many__`` as well
* You can now have normal sections inside configspec sections that use
``__many__``
* You can now create an empty ConfigObj with a configspec,
programmatically set values and then validate
* A section that was supplied as a value (or vice-versa) in the actual
config file would cause an exception during validation (the config
file is still broken of course, but it is now handled gracefully)
* Added ``as_list`` method
* Removed the deprecated ``istrue``, ``encode`` and ``decode`` methods
* Running test_configobj.py now also runs the doctests in the
configobj module
* Through the use of validate 1.0.0 ConfigObj can now validate multi-
line values

As the public API for Validate is stable, and there are no outstanding
issues or feature requests, I've bumped the version number to 1.0.0.
The full change log is:

* BUGFIX: can now handle multiline strings
* Addition of 'force_list' validation option
--
http://mail.python.org/mailman/listinfo/python-list


Re: binary file compare...

2009-04-17 Thread Adam Olsen
On Apr 17, 9:59 am, norseman  wrote:
> The more complicated the math the harder it is to keep a higher form of
> math from checking (or improperly displacing) a lower one.  Which, of
> course, breaks the rules.  Commonly called improper thinking. A number
> of math teasers make use of that.

Of course, designing a hash is hard.  That's why the *recommended*
ones get so many years of peer review and attempted attacks first.

I'd love of Nigel provided evidence that MD5 was broken, I really
would.  It'd be quite interesting to investigate, assuming malicious
content can be ruled out.  Of course even he doesn't think that.  He
claims that his 42 trillion trillion to 1 odds happened not just once,
but multiple times.
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >