Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 09:04:15 -0500
Kent Johnson <[EMAIL PROTECTED]> wrote:

> thomas coopman wrote:
> > 
> > also,
> > Is it better to use super in FooList? and how should I use it then?
> 
> Actually I would say that FooList is not pulling its weight.
> SortedList already allows specialization by the compare function, so
> to create the equivalent of a FooList you just call
> SortedList(Foo.compare).
I know that in this example it's rather useless to create FooList, but
the subclasses I have of SortedList have other specializations so I
will keep using them.

> 
> If you do want to keep FooList then you should call 
> SortedList.__init__() to set the compare function.
> SortedList.__init__ is an unbound function so you call it like this:
>SortedList.__init__(self, Foo.compare)
This works.
I think I'll need to reed some more about super because I thought I had
to use super in this case.

> 
> Another design you might want to consider - if you will always be 
> sorting each type of list by the same compare method - is to define a 
> __cmp__() method in each class that will be part of a SortedList and
> use plain comparison operators (< > etc) to do the compare.
That's the problem.  I define for most of my classes a __cmp__() method
but, some classes have another way of being sorted, and that's why I
give the compare method as an argument in SortedList

> 
> Finally note that Python's sort() function is very fast and flexible
> and it might be better just to sort the list when you need it to be
> sorted, rather than keeping it sorted. Or maybe what you really need
> is a heap (see the heapq module). If you really want to keep a sorted
> list, you should look at the bisect module, it might help.
I now use sort() when there is some data initially.  After creation, I
insert data with my own bisect method, I also use it to search the data.

I mostly need the sorted list, because I want to detect when a object
is added with the same value of some item in the list and take
appropriate actions.

> 
> Kent
> 

Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 07:55:54 -0500
Kent Johnson <[EMAIL PROTECTED]> wrote:

> thomas coopman wrote:
> > Hi,
> > 
> > I want to do something like this, don't know how to properly
> > explain it, so I just give you some example code
> > 
> >>>> class Foo(object):
> >>>>  def method(self, arg):
> >>>>  print arg
> > 
> >>>> def doSomething(object, func):
> >>>>  object.func("test")
> > 
> >>>> object = Foo()
> >>>> doSomething(object, Foo.method)
> > 
> > I want to execute the class method given as argument,
> > but this obvious doesn't work, but I don't know how to
> > get it work,
> 
> First a quick note - don't use object as a parameter or variable
> name, it will hide the definition of the built-in object class.
Yes, I know, it was a bad example
> 
> You are very close. Foo.method is called an 'unbound method' of class 
> Foo. The syntax for calling an unbound method is to pass a class 
> instance as the first argument, followed by the actual argument list.
> It is actually the same argument list that you use when you declare
> the function (starting with self).
> 
> So your example can be written this way:
> 
> In [4]: class Foo(object):
> ...: def method(self, arg):
> ...: print arg
> 
> In [6]: o=Foo()
> 
> In [8]: def doSomething(obj, func):
> ...: func(obj, "test")
> 
> In [9]: doSomething(o, Foo.method)
> test
> 
> 
> A more common way to do this is to use a 'bound method'. That is what 
> you get when you refer to instance.method instead of Class.method. A 
> bound method includes a reference to the particular instance and can
> be called like an ordinary function. Rather than passing the instance
> and the unbound method to doSomething(), just pass the bound method
> as a single argument:
> 

> In [10]: def doSomething(func):
> : func("test")
> 
> In [12]: doSomething(o.method)
> test
> 
> In summary:
> Class.method => unbound method, call with instance as first arg
> instance.method => bound method, call with normal argument list
> 
> Kent
> 

Thank you for the explanation of bound and unbound methods.
I understand that it is more common to use a bound method, but I don't
think that I can use this because at the time I save the method, I don't
know anything about the instances.

I use this for making a sorted list using any method you give as
argument when you create the list.  This class only gets to know it's
instances when you add them.

In [3]: class SortedList(object):
   ...: def __init__(self, compare):
   ...: self.compare = compare
   ...: def __add_(self, object):
   ...:     for i in self.data:
   ...: self.compare(object, i)
   ...:

In [4]: class FooList(SortedList):
   ...: def __init__(self):
   ...: self.compare = Foo.compare


__add__ doesn't do anything here of course, it is just an example,
but I don't think that I can use a bound method in this case?

also,
Is it better to use super in FooList? and how should I use it then?

Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 22:10:52 +1000
Jonathan McManus <[EMAIL PROTECTED]> wrote:

> It's pretty easy to make this work, actually. The issue is in the
> "doSomething" method.
> 
> > >>>class Foo(object):
> > >>> def method(self, arg):
> > >>> print arg
> > 
> > >>>def doSomething(object, func):
> > >>> object.func("test")
> 
> Here, it's looking for a method of the "Foo" object (object) called
> "func" (AttributeError: 'Foo' object has no attribute 'func'), instead
> of replacing "func" with "Foo.method". 
> 
> What you need to do is:
> 
> >>> def doSomething (object, func):
> >>>   func(object, "test")
> 
> This means that you are calling the method (explicity passed as being
> a method of Foo), as well as supplying the actual object as the first
> argument (the required self for classes).
> 
> >>> object = Foo()
> >>> doSomething(object, Foo.method)
> test
> 
> Hope that helps.
> 


Thanks,
that I've didn't come up with that myself!
It's indeed easy!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] class methods as argument

2007-02-10 Thread thomas coopman
Hi,

I want to do something like this, don't know how to properly explain it,
so I just give you some example code

>>>class Foo(object):
>>> def method(self, arg):
>>> print arg

>>>def doSomething(object, func):
>>> object.func("test")

>>>object = Foo()
>>>doSomething(object, Foo.method)

I want to execute the class method given as argument,
but this obvious doesn't work, but I don't know how to
get it work,
Is it possible?
and how?

Thanks

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comparing almost equal strings

2007-02-08 Thread thomas coopman
Hi,

On Thu, 08 Feb
2007 13:07:41 +0100
Christopher Arndt <[EMAIL PROTECTED]> wrote:

> thomas coopman schrieb:
> > I need a function that groups almost equal strings.  It seems most
> > easy to me, to do this with a hash function.
> 
> What do you mean be "almost equal"? By which criterium? Spelling,
> Pronounciation? Semantics?
> 
> > I think I once read something about it, but I can't find it, 
> > does somebody know how to do this?
> 
> Maybe you mean the "soundex" algorithm? See, for example, here:
> 
> http://diveintopython.org/performance_tuning/index.html

This is what I was looking for!
> 
> Chris
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Thanks!

Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] comparing almost equal strings

2007-02-08 Thread thomas coopman
Hi,

I need a function that groups almost equal strings.  It seems most easy
to me, to do this with a hash function.

So I would write a hash function like this:
string = string.replace(" ", "").lower()[0:6]

and then hash the string chars, but it should detect minor typo's, so
words with one different char in the 6 chars, should have the same hash.

I think I once read something about it, but I can't find it, 
does somebody know how to do this?

Also do you think this is a good way, or do some of you have experience
with this and know a better way?

Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best IDE for Python

2007-01-26 Thread Mark Thomas
On 1/26/07, OkaMthembo <[EMAIL PROTECTED]> wrote:
> how useable is vim on Windows?

Very! I've used Vim on Windows since the 5.3 version, it only gets
better. Like Alan said there is a learning curve, but once you've "got
it" there is no going back.

> i wish i could learn Unix. which distro do you think is good to learn?

If you just want to have the power of some Unix tools then take a look
at http://gnuwin32.sourceforge.net/ , they all run fine in the
standard Windows shell. If you want to try a Unix variant then find
one of the many "Live" CD's out there which you can use before you
slice up your disk. I'm a big fan of NetBSD but at the moment I'm
running OpenBSD 4.0 on my PIII laptop and it's very nice.

-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Thomas Coopman wrote:
> When I run One.py from in the directory A, doesn't look python in it's
> parent directory when it can't find the module?
> And if that's not the case where and what should I add to the sys.path
> variable?

No, Python won't look in parent directories for imports. Try running
One.py from the parent dir of M by typing
> python M/A/One.py

or add the parent dir of M to sys.path.

Kent

when I try to run it from the parent dir of M, I still get the error,

where and when should I add the the parent dir to sys.path?

when I try from M.A import One in a shell executed in the parent dir of M
than it works.

The thing is that I would like to run One.py from anywhere and not just from
that one directory.
--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Thomas Coopman wrote:
> Well I don't really
> need the circular imports but I would like to know how to do the imports
correct.
>
> Suppose that in the example that I showed only One needs Two.
>
> So then we have this:
>
> M/
> __init__.py
> A/
> __init__.py
> One.py
> B/
> __init__.py
> Two.py
>
> One.py
> #!/usr/bin/python
>
> from M.B import Two
>
>
> when I run this I get this error:
> ImportError: No Module named M.B

Is the directory containing M in sys.path?

Kent



When I run One.py from in the directory A, doesn't look python in it's
parent directory when it can't find the module?
And if that's not the case where and what should I add to the sys.pathvariable?


--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


thomas coopman wrote:
> On Tue, 16 Jan 2007 10:06:37 -
> "Alan Gauld" <[EMAIL PROTECTED]> wrote:
>
>> "Thomas Coopman" <[EMAIL PROTECTED]> wrote
>> .
>>> I wondered if it was possible to do something like this:
>>>
>>> src/
>>>-a_module/
>>>-sub_module/
>>> test/
>>>-a_module/
>>>-sub_module/
>>>
>> I don;t see any reason why not although its slightly more work.
>> Personally I tend to keep the tests with the code, but thats
>> mainly because tools such as editors tend to remember the last
>> folder opened and its a pain navigating between the two folders.
>>
>> The other system I have used(in C++ not Python) is to have
>> a test folder inside each src folder like:
>>
>> src/
>>mod1/
>>f1.py
>>test/
>>   testf1.py
>> mod2/
>>f2.py
>>f3.py
>>test/
>>testf1.py
>>testf2.py
>>
>> etc.
>>
>> This minimises navigation and keeps the tests separate.
>> Its also relatively easy to filter out the tests when it comes
>> time to package upp the code for distribution (assuming
>> you want to lose them!)
>>
>>> I have something like this but I don't know how to organize the
>>> imports in
>>> the tests and I don't know if this is a good idea.  What do you
>>> think?
>> I think in Python you should create a package structure for
>> your code so that import can find the modules more easily.
>> But I've never tried this in Python, my Python projects are rarely
>> big enough to warrant it.
>>
>>
>
> well, I still have problems with import
>
> suppose I have something like this:
>
> M/
>   __init__.py
>   A/
> __init__.py
> One.py
>   B/
> __init__.py
> Two.py
>
> One.py
> #!/usr/bin/python
>
> from M.B import Two
>
> Two.py
> #!/usr/bin/python
>
> from M.A import One
>
>
> when I try to run One.py or Two.py I get import errors and I don't know
> how to fix them.  I tried to set the __all__ variable in the __init__
> files but nothing worked so far.

What error do you get?

It's better to avoid this kind of circular import if you can. Rather
than having One depend on Two and Two depend on One, look for some piece
you can factor out into a new module that both One and Two use (or it
may only be needed by One or Two).

There are several problems with circular imports, but the technical
problem you are having is probably like this:

module One starts to execute
One imports Two
Two starts to execute
Two imports One
-- Note that at this point, the body of One has not executed, so
anything defined in One after the import of Two is not yet defined.
Two tries to use something in One. Since One is not fully defined, it
gets an AttributeError.

Anyway, try to split up your modules or post the exact error you get.

Kent



Well I don't really
need the circular imports but I would like to know how to do the
imports correct.
Suppose that in the example that I showed only One needs Two.

So then we have this:

M/
__init__.py
A/
__init__.py
One.py
B/
__init__.py
Two.py

One.py
#!/usr/bin/python

from M.B import Two


when I run this I get this error:
ImportError: No Module named M.B


--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread thomas coopman
On Tue, 16 Jan 2007 10:06:37 -
"Alan Gauld" <[EMAIL PROTECTED]> wrote:

> 
> "Thomas Coopman" <[EMAIL PROTECTED]> wrote
> .
> > I wondered if it was possible to do something like this:
> >
> > src/
> >-a_module/
> >-sub_module/
> > test/
> >-a_module/
> >-sub_module/
> >
> 
> I don;t see any reason why not although its slightly more work.
> Personally I tend to keep the tests with the code, but thats
> mainly because tools such as editors tend to remember the last
> folder opened and its a pain navigating between the two folders.
> 
> The other system I have used(in C++ not Python) is to have
> a test folder inside each src folder like:
> 
> src/
>mod1/
>f1.py
>test/
>   testf1.py
> mod2/
>f2.py
>f3.py
>test/
>testf1.py
>testf2.py
> 
> etc.
> 
> This minimises navigation and keeps the tests separate.
> Its also relatively easy to filter out the tests when it comes
> time to package upp the code for distribution (assuming
> you want to lose them!)
> 
> > I have something like this but I don't know how to organize the 
> > imports in
> > the tests and I don't know if this is a good idea.  What do you 
> > think?
> 
> I think in Python you should create a package structure for
> your code so that import can find the modules more easily.
> But I've never tried this in Python, my Python projects are rarely
> big enough to warrant it.
> 
> 

well, I still have problems with import

suppose I have something like this:

M/
  __init__.py
  A/
__init__.py
One.py
  B/
__init__.py
Two.py

One.py
#!/usr/bin/python

from M.B import Two

Two.py
#!/usr/bin/python

from M.A import One


when I try to run One.py or Two.py I get import errors and I don't know
how to fix them.  I tried to set the __all__ variable in the __init__
files but nothing worked so far.

I would like to run the files from anywhere for example.
What am I doing wrong?
I read the information about modules on python.org
(http://www.python.org/doc/2.0.1/tut/node8.html) but I don't find a
solution

Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-16 Thread thomas coopman
On Tue, 16 Jan 2007 10:06:37 -
"Alan Gauld" <[EMAIL PROTECTED]> wrote:

> 
> "Thomas Coopman" <[EMAIL PROTECTED]> wrote
> .
> > I wondered if it was possible to do something like this:
> >
> > src/
> >-a_module/
> >-sub_module/
> > test/
> >-a_module/
> >-sub_module/
> >
> 
> I don;t see any reason why not although its slightly more work.
> Personally I tend to keep the tests with the code, but thats
> mainly because tools such as editors tend to remember the last
> folder opened and its a pain navigating between the two folders.
> 
> The other system I have used(in C++ not Python) is to have
> a test folder inside each src folder like:
> 
> src/
>mod1/
>f1.py
>test/
>   testf1.py
> mod2/
>f2.py
>f3.py
>test/
>testf1.py
>testf2.py
> 
> etc.
> 
> This minimises navigation and keeps the tests separate.
> Its also relatively easy to filter out the tests when it comes
> time to package upp the code for distribution (assuming
> you want to lose them!)

I think I will use something like this.

> 
> > I have something like this but I don't know how to organize the 
> > imports in
> > the tests and I don't know if this is a good idea.  What do you 
> > think?
> 
> I think in Python you should create a package structure for
> your code so that import can find the modules more easily.
> But I've never tried this in Python, my Python projects are rarely
> big enough to warrant it.
> 
> 
Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] import and unittest

2007-01-16 Thread Thomas Coopman

Hi,

the documentation of pyunit
say that it is good to place the unittest in a seperate module.
I wondered if it was possible to do something like this:

src/
   -a_module/
   -sub_module/
test/
   -a_module/
   -sub_module/

So the test are in a complete different directory than the code and the test
dir is a complete copy
of the src dir but with unittest instead of code.

I have something like this but I don't know how to organize the imports in
the tests and I don't know
if this is a good idea.  What do you think?

Thanks

--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problems with a Class

2007-01-13 Thread thomas coopman
On Sat, 13 Jan 2007 01:08:53 +0100
Carlos <[EMAIL PROTECTED]> wrote:

> Hello,
> 
> I'm just about to finish my project, but something is causing me 
> trouble. I need to find out how to get some information out of the 
> genetic algorithm that Im using. This algorithm is generating some 
> coordinates for a collection of objects, until now I needed only the 
> final result of the process. But I discovered that the final
> information is not enough, I need to get some intermediate data to
> actualize the positions of the objects and then evaluate those
> positions.
> 
> This is the area of the algorith that I think holds the info that I
> need:
> 
> def evolve(self, generations = 100):
>   
> self.halt_reached = 0
>
> for gen in range(generations):
> 
> #Changed.
> print 'GEN:',gen, self.entities
>
> self.do_mutation()
> self.do_crossover()
> print "Average fitness generation " + str(gen) + ": " + 
> str(self.avg_fitness())
> if self.debug == 1:
> self.echo_fitness()
> if self.halt >= 0:
> max_entity = self.get_max_fitness()
> fit = self.calc_fitness(max_entity)
> if fit >= halt:
> self.halt_reached = 1
> return [max_entity]
> 
> The line marked as #Changed is what I changed,  with this I was able
> to print the info, but I'm sorry to say that I have no clue at how to
> apply something like return() that would give me this self.entities
> list. As you can see there is a for loop and I need the info every
> time that it gets updated by the loop.
> 
> Hopefully I'm making myself clear and the info is enough.
> 
> Thanks for your help in advance
> Carlos
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


Hi,

when you call return, you return from the function so
when you have something like this:

def loop():
for i in range(10):
return i

print loop()

This will print just 0.

when you want to get all the numbers, you can't use return in the loop
you could save them in a list or something, and after the loop return
the list.

def loop():
x = []
for i in range(10):
x.append(i)
return x

print loop()

this will print a list with all the numbers.

I hope this is what you mean.


Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Organizing 15500 records, how?

2006-12-12 Thread Thomas
I'm writing a program to analyse the profiles of the 15500 users of my
forum. I have the profiles as html files stored locally and I'm using
ClientForm to extract the various details from the html form in each
file.

My goal is to identify lurking spammers but also to learn how to
better spot spammers by calculating statistical correlations in the
data against known spammers.

I need advise with how to organise my data. There are 50 fields in
each profile, some fields will be much more use than others so I
though about creating say 10 files to start off with that contained
dictionaries of userid to field value. That way I'm dealing with 10 to
50 files instead of 15500.

Also, I am inexperienced with using classes but eager to learn and
wonder if they would be any help in this case.

Any advise much appreciated and thanks in advance,
Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] difflib.SequenceMatcher with get_matching_blocks is incorrect

2006-12-04 Thread Thomas
I'm trying to write a program to test someone's typing speed and show
them their mistakes. However I'm getting weird results when looking
for the differences in longer (than 100 chars) strings:

import difflib

# a tape measure string (just makes it easier to locate a given index)
a = 
'1-3-5-7-9-12-15-18-21-24-27-30-33-36-39-42-45-48-51-54-57-60-63-66-69-72-75-78-81-84-87-90-93-96-99-103-107-111-115-119-123-127-131-135-139-143-147-151-155-159-163-167-171-175-179-183-187-191-195--200'

# now with a few mistakes
b = 
'1-3-5-7-l-12-15-18-21-24-27-30-33-36-39o42-45-48-51-54-57-60-63-66-69-72-75-78-81-84-8k-90-93-96-9l-103-107-111-115-119-12b-1v7-131-135-139-143-147-151-m55-159-163-167-a71-175j179-183-187-191-195--200'

s = difflib.SequenceMatcher(None, a ,b)
ms = s.get_matching_blocks()

print ms


[(0, 0, 8), (200, 200, 0)]


Have I made a mistake or is this function designed to give up when the
input strings get too long?

Thanks in advance,
Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A Million Sevens

2006-11-17 Thread Thomas
On 18/11/06, Chris Hengge <[EMAIL PROTECTED]> wrote:
> Not that it changes your reply, but just for my own sanity:
> int('7' * 10 ** 6) <- does this not just type-cast a char into an int?
>
> Meaning that rather then consuming 1024k as you stated, it would consume 
> 2048k at the peak of the calculation(2bytes per char? * 1m = 2048k) then 
> typecasting to int would drop it back down to 1k (1byte per int * 1m = 1024k
>
> So, just for sake of getting to a point since I missed the one by the 
> original poster.. why would you not convert that 7 from a char to an int 
> first? That calculation is almost instant, and it doesn't require the memory 
> rollercoaster that this calculation would require..
>
> Anyways.. back to the poster...

Thanks, original poster here, I was just reading my new book, Core
Python Programming (2nd Edition) and it said that there was no
in-built limit to the size of a long in python (as compared to C) so I
wanted to test it out. I just typed the first way of getting a massive
number that came into my head.

> Perhaps you wanted an error like this?
>
> print '=' * 10
> Traceback (most recent call last):
>   File "", line 1, in ?
> MemoryError

Now that is exactly what I thought I might get. I'll try it again with
some of the variations mentioned on Monday when I get back to that
computer.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] A Million Sevens

2006-11-17 Thread Thomas

Earlier today I typed the following into my pythonwin interactive
interpreter in windows xp:


int('7' * 10 ** 6)


I expected either an error message or it to get stuck and require me to
stop the process manually.

I read that unlike long integers in C, longs in python are only limited by
the amount of memory (and virtual memory) your system has.

Can you guess what it did?

I'm temped to end the post here, but I'm new to this list and its possible
that people might be annoyed by me not getting to the point within my
initial post, so here's what it did:

It thought about it for about 2 seconds then restarted my pc! explanations
welcome.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Read the builtin module source, also itchy ellipses.

2006-11-17 Thread Thomas

Hi,

I sometimes find it useful to read the source code of a module and for
example I can type string.__file__ to find the location of the string
module.

However the .__file__ method is not available for the module builtin. Is it
possible to read the source code for built in functions and if so how do I
find them? I realise some/all may be written in C rather than python but it
would still be interesting to read them.

In an unrelated follow-up may I also ask: can the ellipsis (...) be used in
code? I tried a googlecode search but none of the examples worked for me.
Can someone post an example line of code that uses ... in it. This has
been causing an itch in my brain, please reply with the scratch.

Thanks in advance,
Tom
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] property built-in

2006-10-19 Thread thomas
Hi,


I was wondering some things about property.

suppose I have a class like this:

class A(object):
def __init__(self, x, y):
   self.__x = x

def x():
   def get(self):
  return self.__x

   def set(self, x):
  self.__x = x
  #and some other code that is important

x= property(**x())

questions:
1: why doesn't there have to be a self in x()?
2: how do I address the setter of x in the init?

I think these are basic questions, but I can't seem to find out how to 
do it.


Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] extracting matches by paragraph

2006-10-11 Thread Thomas A. Schmitz

On Oct 11, 2006, at 2:48 PM, Kent Johnson wrote:

> Thomas A. Schmitz wrote:
>> On Oct 11, 2006, at 12:06 PM, Kent Johnson wrote:
>>
>>> I would take out the join in this, at least, and return a list of
>>> lines. You don't really have a paragraph, you have structured data.
>>> There is not need to throw away the structure.
>>>
>>> It might be even more useful to return a dictionary that maps field
>>> names to values. Also there doesn't seem to be any reason to make
>>> FileIterator a class, you can use just a generator function (Dick
>>> Moores take notice!):
>>>
>>> def readparagraphs(fw):
>>> self._fw = fw
>>>
>>> data = {}
>>> for line in fw:
>>> if line.isspace():
>>> if data:
>>> yield data
>>> data = {}
>>> else:
>>> key, value = line.split(' : ')
>>> data[key] = value
>>> if data:
>>> yield data
>>>
>>> Now you don't need a regexp, you have usable data directly from the
>>> iterator.
>>>
>>
>> Thank you for your help, Kent! But I'm not sure if this is
>> practicable. As I said, a line-by-line approach does not work,
>
> What I have outlined is not a line-by-line approach, it is still
> returning data for a paragraph at a time, but in a more usable format
> than your original iterator.
>
> Try printing out the values you get from the iterator, the same way  
> you
> did with your original paragraph iterator.
>
>> for
>> two reasons:
>> 1. I want to combine and translate the results from two lines;
>
> You can do that with this approach.
>
>> 2. in the file, there are lines of the form
>> Publication : Denver, University of Colorado Press, 1776
>> from which I need to extract three values (address, publisher, date),
>> and I may need to discard some other stuff from other lines. So I do
>> need a regex, I think. Unfortunately, the structure is not strong
>> enough to make a one on one translation viable, so I do need to
>> extract the values...
>
> Ok, so the dict from the iterator is still raw data that needs some
> processing. Something like
>
> for para in readparagraphs(open('mydata.txt')):
># Your previous example
>if para.get('Type de notice') == 'monographie':
>  print "@Book{,"
>
># publication data
>pubData = para.get('Publication')
>if pubData:
>  address, publisher, date = pubData.split(', ')
>  # do something with address, etc.
>
> Kent
>
> PS Please reply on-list.
>

Kent,

thanks again, I begin to see some light and will try to make sense of  
it. I had developed the bad habit of just treating such data as  
strings, but of course it makes much more sense to keep the original  
format and then convert it to another format.

Sorry for mindlessly hitting the reply button, didn't mean to intrude  
on you.

Thanks a lot

Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] extracting matches by paragraph

2006-10-11 Thread Thomas A. Schmitz
Please excuse this long mail. I have read several tutorials and  
googled for
three days, but haven't made any real progress on this question,  
probably
because I'm an absolute novice at python. I'd be very grateful for  
some help.

1. My problem:

I have several files in a structured database format. They
contain entries like this:

Type de notice : monographie
Auteur(s) : John Doe
Titre(s) : Argl bargl
Publication : Denver, University of Colorado Press, 1776

Type de notice : article
Auteur(s) : Richard Doe
Titre(s) : wurgl burgl

Type de notice : recueil
Titre(s) : orgl gorgl

I want to translate this into a BibTeX format. My approach was to  
read the file
in by paragraphs, then extract the values of the fields that interest  
me and
write these values to another file. I cannot go line by line since I  
want to
reuse, e.g., the value of the "Auteur(s)" and "Titre(s)" fields to  
generate a
key for every item, in the form of "doeargl" or "doewurgl" (via the  
split and
join functions) The problem is that not every entry contains every  
field (in my
example, #3 doesn't have an author), so I guess I need to test for the
existence of these fields before I can use their values.

2. The approach:

There is code here
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408996/
which allows to read a file by paragraphs. I copied this to my script:

class FileIterator(object):
""" A general purpose file object iterator cum
file object proxy """

def __init__(self, fw):
self._fw = fw

def readparagraphs(self):
""" Paragraph iterator """

# This re-uses Alex Martelli's
# paragraph reading recipe.
# Python Cookbook 2nd edition 19.10, Page 713
paragraph = []
for line in self._fw:
if line.isspace():
if paragraph:
yield "".join(paragraph)
paragraph = []
else:
paragraph.append(line)
if paragraph:
yield "".join(paragraph)

When I now run a very basic test:

for item in iter.readparagraphs():
 print item

The entire file is reprinted paragraph by paragraph, so this code  
appears to work.

I can also match the first line of every paragraph like so:

reBook = re.compile('^Type de notice : monographie')
for item in iter.readparagraphs():
 m1 = reBook.match(item)
 if m1:
print "@Book{,"

this will print a line @Book{, for every "monographie" in the  
database -- a
good start, I thought!

3. The problem that's driving me insane

But as soon as I try to match anything inside of the paragraph:

reAuthor = re.compile('^Auteur\(s\) : (?P.+)$')
m2 = reAuthor.match(item)
if m2:
author = m2.group('author')
print "author = {%s}," % author

I get no matches at all. I have tried to remove the ^ and the $ from  
the regex,
or to add the "re.DOTALL" flag, but to no avail.

4. My aim

I would like to have dictionary with fixed keys (the BibTeX field)  
and values
extracted from my file for every paragraph and then write this, in a  
proper
format, to a bibtex file. If a paragraph does not provide a value for a
particular key, I could then, in a second pass over the bibtex file,  
delete
these lines. But that means I first have to match and extract the  
values from
my parapgraphs.

What am I doing wrong? Or is the entire approach flawed? What  
alternative
method would you suggest?

Thanks for any help on this

Thomas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New Tutorial topic available

2006-02-26 Thread Mark Thomas
On 2/26/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
> I've just uploaded the latest tutorial topic covering inter-process
> communications. It covers pipes and the use of fork().
>
> Enjoy, as ever feedback is welcomed.

Thanks Alan, your pages are a great source of information for us newbies.

--
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pyGtk using combobox

2006-02-10 Thread thomas
Hi,

I would like to make a combobox with a list of strings, but I have many
problems with it.  I know how to make the combobox and how to add
strings, but is it possible to get a list of strings from the combobox
and also is it possible to update the combobox with a list of strings?
(something like combo.set_popdown_strings(list))
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to strip both single and double quotes

2005-10-05 Thread Mark Thomas
On 10/5/05, Dick Moores <[EMAIL PROTECTED]> wrote:
> The text will contain "words" beginning or ending with non-alphabetic
> characters. To strip them off I use string.strip([chars]). My question is
> how to use strip just once to get rid of both kinds of quotes, single and
> double. It seems necessary to do it something like this:

Not sure what you mean by 'words' but how about something like this.
[x.strip('\' \" () * & ^ % $ # < ') for x in word.split()]

Example:
>>> str = 'this has special characters \" \' ()*&^%$# << '
>>> [x.strip('\' \" () * & ^ % $ # < ') for x in str.split()]
['this', 'has', 'special', 'characters', '', '', '', '']

--
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2005-07-06 Thread Mark Thomas
On 7/6/05, Michael Huster <[EMAIL PROTECTED]> wrote:
> In python under windows, how can I create and access a file on the Desktop? 
> The following do NOT work:
 
How about...

>>> userp = os.getenv('USERPROFILE') + '\\Desktop\\MyFile.txt'
>>> fo = file(userp, 'w')
-- 
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple string processing problem

2005-05-13 Thread Mark Thomas
On 13 May 2005 21:59:58 +0100, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> The file:
> 
> ScerACTAACAAGCTGGTTTCTCC-TAGTACTGCTGTTTCTCAAGCTG
> Sparactaacaagctggtttctcc-tagtactgctgtttctcaagctg
> Smikactaacaagctgtttcctcttgaaatagtactgctgcttctcaagctg
> Sbayactaacaagcactgattgaaatagtactgctgtctctcaagctg
>   * ** **     ***   * ***  *
> 
> ScerTGCTCACCAATTTATCCCAATTGGTTTCGGTATCAAGAAGTTGCAAATTAACTGTG
> SparTGCTCACCAATTTATCCCAATTGGTTTCGGTATCAAGAAGTTGCAAATTAACTGTG
> SmikTGCTCACCAATTCATCCCAATTGGTTTCGGTATCAAGAAGTTGCAAATTAACTGTG
> SbayTGCTCACCAATTCATCCCAATTGGTTTCGGTATCAAGAAATTGCAAATTAACTGTG
> * ** *   *   *  ** * ** 
> 
> ScerACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAAaaggctt-ataa
> SparACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAAaaagctttataa
> SmikACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAAgaagctctataa
> SbayACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAAgaagctctataa
>  * ***  
> 
> Sceractataattaacattaa---agcacaacattgtaaagattaaca
> Sparactataataaacatcaa---agcacaacattgtaaagattaaca
> Smikactataattaacatcgacacgacaacaacaacattgtaaagattaaca
> Sbayactataacttagcaacaacaacaacaacaacatcaacaacattgtaaagattaaca
> ***     * **  **

How about some RE action.

>>> import re
>>> pat = re.compile('^(S[a-z]{3}\s*[A-Z]+).*$')
>>> fr = file('dna','r')
>>> for line in fr:
... m = pat.match(line)
... if m:
... print m.group(1)
...
ScerACTAACAAGCTGGTTTCTCC
ScerTGCTCACCAATTTATCCCAATTGGTTTCGGTATCAAGAAGTTGCAAATTAACTGTG
SparTGCTCACCAATTTATCCCAATTGGTTTCGGTATCAAGAAGTTGCAAATTAACTGTG
SmikTGCTCACCAATTCATCCCAATTGGTTTCGGTATCAAGAAGTTGCAAATTAACTGTG
SbayTGCTCACCAATTCATCCCAATTGGTTTCGGTATCAAGAAATTGCAAATTAACTGTG
ScerACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAA
SparACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAA
SmikACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAA
SbayACCACGTCCAATCTACCGATATTGCTGCTATGCATTATAA


-- 
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] website information

2005-04-18 Thread Williams, Thomas








Does anyone know how to prevent this error
from occurring: IOError: [Errno socket error] (10060, 'Operation timed out'). 

 

I have searched for solutions without any
success.

 

Tom Williams

 

 






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cryptography Toolkit

2005-04-01 Thread Mark Thomas
On Apr 1, 2005 6:45 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:
> It works if you make a new XOR object for the decryption:
> 
> from Crypto.Cipher import XOR
> 
> obj_xor = XOR.new("string")
> str_encrypt = "encrypt this string"
> print str_encrypt
> 
> xored = obj_xor.encrypt(str_encrypt)
> print xored
> 
> obj_xor = XOR.new("string")
> print obj_xor.decrypt(xored)
> 
> Kent

Excellent !!
Many thanks Kent.
-- 
 _
( ) Mark Thomas ASCII ribbon campaign
  X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cryptography Toolkit

2005-03-31 Thread Mark Thomas
On Thu, 31 Mar 2005 09:14:03 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote:
> If you post your code and the complete error message including the stack 
> trace we may be able to help.
> 
> Kent

Thanks Ken

I'm getting closer to making this work using the XOR cipher. Here's
what I'm doing.



from Crypto.Cipher import XOR

obj_xor = XOR.new("string")
str_encrypt = "encrypt this string"
xored = obj_xor.encrypt(str_encrypt)
xored
'\x16\x1a\x11\x1b\x17\x17\x07T\x06\x01\x07\x14S\x07\x06\x1b\x07\t\x14'
obj_xor.decrypt(xored)
"bhxupds&oo`g'uou`z`" <== *confused by this output*



Close but no cigar!! *grin*

-- 
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Cryptography Toolkit

2005-03-31 Thread Mark Thomas
Does anyone have some examples on the use of A.M. Kuchling's Python
Cryptography Toolkit? I've tried his examples but get "AttributeError"
and "TypeError". What I'm trying to do is encrypt/decrypt a file. I'm
using Python 2.3 on xp pro.

Thanks
-- 
 _
( ) Mark Thomas ASCII ribbon campaign
 X www.theswamp.org   - against HTML email
/ \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] updating Oracle tables via python

2005-03-29 Thread Williams, Thomas








Greetings,

I am attempting to update an Oracle table
using python.  When I execute the code, the python script appears to hang,
in that nothing else happens.

 

As always, any assistance you can provide
will be appreciated.

 

Code:

 

connection = cx_Oracle.connect("db/[EMAIL PROTECTED]")

cursor = connection.cursor()

strUpdate = " UPDATE table SET firstname
= 'JOSEPH'  WHERE lastname = 'SMITH' "

cursor.execute(strUpdate)

cursor.close()

connection.close()

 

 

Tom Williams
DSHS - Research and Data Analysis
Division 
14th and Jefferson, MS: 45204 
  Olympia, WA 
 98504-5204

360.902.0764

 






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] executing SAS and passing parameters

2005-02-09 Thread Williams, Thomas
Here is the code I am using to invoke SAS:

import os
import sys

shell = os.environ.get('COMSPEC')
if shell is None: shell = os.environ.get('SHELL')
if shell is None: shell = 'an unknown command processor'
print 'Running under', shell

os.execl('C:\Program Files\SAS Institute\SAS\V8\SAS.exe')


However, the program in question is c:\work\run_ratios.sas, with 2
parameters: incov, and outcov.  This program was initially invoked from an
aml program.  The code that invoked SAS from this aml is:

&SYSTEM %.SASLOC% -SYSPARM %sas_parm% -log ~
%.ARCLOC%\ratios\log\%.file%.log -SYSIN ~
%.ARCLOC%\sas_code\ratios\new_ratio.sas

%.SASLOC%: the SAS executable file ('C:\Program Files\SAS
Institute\SAS\V8\SAS.exe')
%sas_parm%: the list of parameters to be passed onto SAS
%.file%: the name of the log file that is generated during the execution of
the SAS program.
%.ARCLOC%: directory of the SAS program (c:\work)

I think this is an excellent forum to share ideas and solve problems.  Thank
you ever so much for your assistance with this.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] executing SAS and passing parameters

2005-02-08 Thread Williams, Thomas
I'll do my best to answer these questions for you.

I am able to start the SAS executable from python, but not the specific SAS
program in question.  When this executable is executed, the typical SAS
environment is displayed (SAS editor Window, SAS libraries, and the output
and log windows).  I want to have a specific SAS program executing with the
assigned parameters that will be read into a SAS macro.  

This SAS program was originally called from an AML (Arc Macro Language),
again, with the parameters passed to it.

OS: WindowsNT

Let me know if you need further information.

Thanks again,
Tom


-Original Message-
From: Alan Gauld [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, February 08, 2005 1:57 PM
To: Williams, Thomas; tutor@python.org
Subject: Re: [Tutor] executing SAS and passing parameters

> I am trying to use python to run a SAS program by passing the needed
> parameters.  I am able to start SAS, but unable to start the correct
SAS
> program with its parameters.

Not being familiar with SAS or its parameters we'll need more clues...

> Any assistance you could provide will be appreciated.

Can you show us what you used to start SAS?
Can you tell us exactly what happened? - any errors etc?
Can you show us how you'd do it outside of Python?
Can you tell us which OS you are using?

With that info we should be able to make a stab at it.

Alan G.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] executing SAS and passing parameters

2005-02-08 Thread Williams, Thomas








Greetings,

 

I am
trying to use python to run a SAS program by passing the needed
parameters.  I am able to start SAS, but unable to start the correct SAS
program with its parameters.

 

Any
assistance you could provide will be appreciated.

 

Tom Williams
DSHS - Research and Data Analysis
Division 
14th and Jefferson, MS: 45204 
  Olympia, WA 
 98504-5204

360.902.0764

 






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] XP and python 2.4, some progress

2005-01-08 Thread Jeffrey Thomas Peery








I wasn’t able to get the IDLE started in windows XP. I
had it working then I upgraded to 2.4, then it didn’t work so I switched
back to 2.3, still didn’t work so I’m back to 2.4.  I did some
looking around and I was able to get the IDLE started by setting the shortcut
on my desktop to:

 

C:\Python24\python.exe
C:\Python24\Lib\idlelib\idle.pyw –n

 

Not sure what this does. But is seems to get it going. What is
going on here? Also I get a console that appears with this message - I have no
idea what it means:

 

Warning: configHandler.py – IdleConf.GetThemeDict
– problem retrieving theme element ‘builtin-background’

For theme ‘Jeff’

Returning default value: ‘#ff’

 

Anyhow if anyone can explain what is going on here I would
appreciate the help… and is there anyway to get the IDLE icon in my
windows start menu to work? 

Ok thanks for the help!

 

Jeff






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2005-01-08 Thread Jeffrey Thomas Peery








Hello I can’t seem to get the IDLE to start up in my
windows XP by clicking on the desktop icon. To start it I have to drop a .py
file on the icon. Any ideas?

 

Also I can’t seem to get xp to recognize .py files
belonging to python. Right now the icons for .py is a windows default and not
the python icon. So if I double click on a script that I want to work on it
doesn’t open the idle. I have to open the script from IDLE.  Any ideas on
this problem? 






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] implementing a table data structure in python?

2004-12-21 Thread Thomas Clive Richards
On Wednesday December 22 2004 11:58 am, Alan Gauld wrote:
>
> Hows about a dictionary of lists. A key per column. The
> users pick which columns and you retrieve the lists. And
> of course shelve will treat a file like a dictionary...
>

Ahhh that's better..


I guess I should clarify: it's not lists per se that I find "silly" (a poor 
choice of words), just their use for this particular problem...

Anyway, I took a look at KirbyBase, and decided to use that... it does almost 
exactly what I was trying to write myself, and does it reasonably quickly 
too!


Thanks for all your help.

-- 

Thomi Richards,
[EMAIL PROTECTED]
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] implementing a table data structure in python?

2004-12-20 Thread Thomas Clive Richards

Hi,

I'm trying to implement a reasonably efficient table data structure, but ith a 
few quirks...

The application I'm building is a CGI program which allows users to view a 
large table of data. In addition, users can restrict the data shown. Some 
sample data might look like this: (note you'll need a fixed width font for 
this)

D   xyz   val   q
-
1|  123   230.1
13   |  234   240.15
24   |  112   120.2
45   |  235   1 0.12
116  |  627   270.18
211  |  946   280.45

Actually, the real data I'm using is very different - but this is easy to 
type :)

The entire table is stored in a plain text file, and could contain up to 500 
KB of data.

Every time my CGI script is run, it must read this file from disk, and return 
a 2D array of values; However, I need to be able to restrict the columns 
shown.

For example, user A might not want to see the D & val columns - I need a way 
of trimming these easily..


Normally I'd use a database like mysql, postgreSQL, or even SQLite for this 
sort of application, but the server I'm working on (which is outside my 
control) does not have any of these installed.

The CGI will not see heavy use, and I'm not very worried about race conditions 
or collisions...


Does anyone have any advice for me? implementing this as a simple 2D array (or 
list of lists) seems a little silly - surely there's a better way?


Actually, python bindings to the STL (or something similar) would be useful in 
many situations



Thanks,
-- 

Thomi Richards,
[EMAIL PROTECTED]
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


<    1   2