Re: [Tutor] __new__ over __init__

2010-08-30 Thread Peter Otten
Payal wrote:

> Can someone please give a very simple example of using __new__ wherein
> __init__ cannot be used?

Subclasses of immutable types, e. g. tuple:

>>> class A(tuple):
... def __init__(self, a, b):
... pass
...
>>> a = A(1,2)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: tuple() takes at most 1 argument (2 given)
>>> class A(tuple):
... def __new__(cls, a, b):
... return tuple.__new__(cls, (a, b))
...
>>> A(1, 2)
(1, 2)
>>> type(_)


Peter

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


[Tutor] __new__ over __init__

2010-08-30 Thread Payal
Hi all,

Can someone please give a very simple example of using __new__ wherein
__init__ cannot be used?

With warm regards,
-Payal
-- 


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


Re: [Tutor] Newbie - regex question

2010-08-30 Thread Marc Tompkins
On Mon, Aug 30, 2010 at 2:56 PM, Hugo Arts  wrote:

> To solve, we have the non-greedy patterns. They eat not as much
> possible, but as little as possible. To make a qualifier non-greedy,
> simply add an asterix at its end:
>
> r'WORD1-.*?'
>
> I would also like to offer one small correction: an asterisk may be greedy,
but Asterix is a cheerful, brave Gaul.


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


Re: [Tutor] Newbie - regex question

2010-08-30 Thread Hugo Arts
On Tue, Aug 31, 2010 at 12:02 AM, Serdar Tumgoren  wrote:
>  To make a qualifier non-greedy,
>>
>> simply add an asterix at its end:
>>
>> r'WORD1-.*?'
>>
>
> Hugo explains this nicely, but I just wanted to make one minor correction
>  -- the non-greedy qualifier is the question mark AFTER an the asterisk
> (which is what Hugo's code shows but I believe he accidentally left off).
>

ah, sorry, a bit of cognitive dissonance on my end. You're right of
course. That should've read "To make a qualifier non-greedy, add a
*question mark* at the end."

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


Re: [Tutor] Newbie - regex question

2010-08-30 Thread bob gailer

 On 8/30/2010 4:52 PM, Sam M wrote:

Hi Guys,

I'd like remove contents between tags  that matches pattern 
"WORD1" as follows:


Change
"stuff word1-emai...@domain.com more stuff 
word1-emai...@domain.com still more stuff 
word2-emai...@domain.com stuff after WORD2 
word1-emai...@domain.com"


To
"stuff  more stuff  still more stuff 
word2-emai...@domain.com stuff after WORD2 "


The following did not work
newl = re.sub (r'WORD1-.*',"",line)


Please remember to explain "did not work".
Did you get an exception?
Did you get unexpected results? If so what were they?

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Newbie - regex question

2010-08-30 Thread Serdar Tumgoren
 To make a qualifier non-greedy,

> simply add an asterix at its end:
>
> r'WORD1-.*?'
>
>
Hugo explains this nicely, but I just wanted to make one minor correction
 -- the non-greedy qualifier is the question mark AFTER an the asterisk
(which is what Hugo's code shows but I believe he accidentally left off).

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


Re: [Tutor] Newbie - regex question

2010-08-30 Thread Hugo Arts
On Mon, Aug 30, 2010 at 10:52 PM, Sam M  wrote:
> Hi Guys,
>
> I'd like remove contents between tags  that matches pattern "WORD1"
> as follows:
>
> Change
> "stuff word1-emai...@domain.com more stuff
> word1-emai...@domain.com still more stuff
> word2-emai...@domain.com stuff after WORD2
> word1-emai...@domain.com"
>
> To
> "stuff  more stuff  still more stuff word2-emai...@domain.com
> stuff after WORD2 "
>
> The following did not work
> newl = re.sub (r'WORD1-.*',"",line)
>

This precise problem is actually described in the re documentation on
python.org:

http://docs.python.org/howto/regex.html#greedy-versus-non-greedy

In short: .* is greedy and gobbles up as much as it can. That means
 will resolve to the last  tag in the line, and all
the previous ones are simply eaten by .*

To solve, we have the non-greedy patterns. They eat not as much
possible, but as little as possible. To make a qualifier non-greedy,
simply add an asterix at its end:

r'WORD1-.*?'

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


[Tutor] Newbie - regex question

2010-08-30 Thread Sam M

Hi Guys,

I'd like remove contents between tags  that matches pattern "WORD1"  
as follows:


Change
"stuff word1-emai...@domain.com more stuff  
word1-emai...@domain.com still more stuff  
word2-emai...@domain.com stuff after WORD2  
word1-emai...@domain.com"


To
"stuff  more stuff  still more stuff  
word2-emai...@domain.com stuff after WORD2 "


The following did not work
newl = re.sub (r'WORD1-.*',"",line)

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


Re: [Tutor] Help with Object Oriented Programming

2010-08-30 Thread Alan Gauld


"Tino Dai"  wrote

  I'm beefing up my Object-Oriented Analysis and Design - 
getting

the gaps in my
knowledge filled in through the Head First OOAD book


I don't know it although I've seen a couple of others in the series.

My recommendations for general OOAD books are:

Timothy Budd - OOP. It's not Python but covers the basic principles 
well.


Grady Booch - OOAD - The second edition is all in C++, The first 
edition, if you can find one,
is in 5 different langiages and IMHO much better for it. It stops you 
focusing on the

language and helps focus on the OO principles.

Bruce Eckel - Thinking in Java - One of the very few books on Java 
that does a good
job of teaching OO. He was going to do a Thinking in Python but I 
think it died :-(


And finally the original Design Patterns book by the Gang of Four. Its 
a bit heavy

but the information is excellent.

HTH,


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


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


Re: [Tutor] Help with Object Oriented Programming

2010-08-30 Thread Knacktus

You could google for

1) Alex Martelli, Design Patterns
He's a Pyton guru and there're some online talks (at Google and some 
conferences) about DP; a bit difficult to understand, well, he's guru ;-)


2) http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html
I like that one.

Also, there're some presentations about "the lack of design patterns in 
Python" on the web. Google should help or look at the PyCon archives. 
Very useful stuff there.


And finally, 1 gramm of practice weighs more than 1 ton of theory. I see 
it right now after some month absence from coding.


Cheers,

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


Re: [Tutor] can this be done easerly

2010-08-30 Thread ALAN GAULD
Ok, Looks like you need the replace() on anything that could be a separator...

BTW.
Did you see Steven's implementation? He used a list comprehension 
which is probably a little beyond your "comprehension" for now (sorry, 
I couldn't resist! :-) but can be translated easily back to an explicit loop. 


He is using your original character based approach but with isalpha() 
instead of the ord() checks

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



>
>From: Roelof Wobben 
>To: alan.ga...@btinternet.com
>Sent: Monday, 30 August, 2010 18:41:58
>Subject: RE: [Tutor] can this be done easerly
>
> Hello, 
> 
>It don't work for me:
> 
>I changed everything to this :
> 
>def extract_words(s):
>"""
>  >>> extract_words('Now is the time!  "Now", is the time? Yes, now.')
>  ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']
>  >>> extract_words('she tried to curtsey as she spoke--fancy')
>  ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
>"""
>s2=[]
>s = s.lower()
>for word in s.split():
>test = word.strip('!?,.--')
>s2.append(test)
>return s2
>
>if __name__ == '__main__':
>import doctest
>doctest.testmod()
> 
>But now the doctest failes with this :
> 
>Expected:
>['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
>Got:
>['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke--fancy']
>**
>1 items had failures:
>1 of 2 in __main__.extract_words
***Test Failed*** 1 failures.
>
> 
>
 Date: Mon, 30 Aug 2010 17:01:04 +
>From: alan.ga...@btinternet.com
>Subject: Re: [Tutor] can this be done easerly
>To: rwob...@hotmail.com
>CC: tutor@python.org
>
> 
>
>
>> I tried your suggestion with strip and it looks like this :
> 
>> import string
>
>You don't need this now
>
>> def extract_words(s):
>>word= ""
>
>You dont need this
>
>> s2=[]
>> s = string.lower(s)
>> s = s.replace( "--", " ")
>
>You probably don't need this either - just add '-' to the strip set.
>
>> s = s.split()
>> for word in s:
>
>And you could combine those to
>
>for word in s.split():
>
>> test = word 
>> test = word.strip('!?,.')
>
>You don't need test, just use
>
>word = word.strip('!?",.-')  #added " and - to your set...
>
>> s2.append(test)
>> return s2
>
>> It works well only I can't get it to work the strip with ". 
>> I always get a EOF error message. I tried it with 
>> word.strip('!?,.") and with word.strip('!?,./"'') but no go.
>
>It looks like you are mixing quote styles.
>Notice how I included the " in the strip() above... 
>See if that works for you.
>
>Alan G.
>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Object Oriented Programming

2010-08-30 Thread Knacktus

Am 30.08.2010 20:13, schrieb Tino Dai:

Hi Everybody,

I'm beefing up my Object-Oriented Analysis and Design - getting
the gaps in my
knowledge filled in through the Head First OOAD book
(http://headfirstlabs.com/books/hfooad/).
  That book focuses on Java - is there a comparable book for Python? I
have already read the
Alan Gauld's intro on classes, but I'm looking for more. My goal is to
be able to design
and code in Python in an OO fashion exploiting the OO paradigm as it related to
Python at the level of  Kent Johnston (hey, if I'm going to dream,
might as well dream big! :)  ).
Currently, some of the things such as inheritance and encapsulation
apply across OO languages
but interfaces (I know that Zope has this) and mixin's are language
specific constructs.
If anybody could point me in the right direction, that would be great!

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

Hi Tino,

I've started in a very simliar fashion. My first OO-app was a procedural 
Fortran-style Java application ;-)
Then I stumbled over Python but was aware that I had no clue about 
OO-Programming. I read the book Head First OOAD, too. And also stuff 
about design patterns.
At the beginning, it's a bit confusing. There's a lot of overhead 
involved due to Java's static typing. You have to use inheritance, 
interfaces, abstract classes etc. to achieve certain things, e.g. make 
code general ("develop for interface not implementation ...") With 
Python the same things can be achieved a loot easier and in the end 
clearer (at least to me). One example are the iterator and visitor 
patterns. Those can be done so smoothly in Python thanks to "everything 
is a first class object". It would be fatal to try to translate Java 
Code from the DP book to python.
Overall, Java OO and DP are helpful to know and understanding the ideas 
behind them will help you with Python. But things are done differently 
with Pyhton (don't try to mimick Java!) and you will be very very happy 
about that.

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


Re: [Tutor] Help with Object Oriented Programming

2010-08-30 Thread Serdar Tumgoren
I haven't read it yet myself, but the below book just came out:

http://www.amazon.com/Python-3-Object-Oriented-Programming/dp/1849511268/ref=cm_cr_pr_sims_t

I'm not aware of any other book that focuses exclusively on OO in Python,
though you'll find good intros to the topic in a number of the "classics,"
such as the newest Learning Python (4th Edition) and Core Python (2nd
Edition).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Conditional attribute access / key access

2010-08-30 Thread Knacktus

Am 30.08.2010 17:53, schrieb Francesco Loffredo:


Two questions and one doubt for you:
1- How many "generations" do you want to keep in a single item (call it
dictionary or list, or record, whatever)? I mean, what if some children
have children too, and some of those have more children, etc ?
There's always one level (generation) of children in an item. An item 
can have zero or more direct children. And a lot of grandchildren and 
grandgrandchildren etc. The item-structure represent an assembly 
hierarchy of the parts of a car. So overall the structure can be up to 
about 20 levels "deep" and consist of up to 20 items overall, where 
the application needs to handle several structures.


2- Are you SURE that there are no circular references in your database?
In your example, what if item_3 was
item_3 = {"id": 3, "name": "child_2", "children_ids": [6, 1, 8]}? Is't
it possible that those recursion limit problems you had could come from
some circular reference in your data?
That's a good hint. But the recursion limit doesn't come from that (the 
test data actually had no children. I used a single instance of my dict.)


d- If the number of data items is really huge, are you sure that you
want to keep the whole family in memory at the same time? It depends on
the answer you gave to my question #1, of course, but if retrieving an
item from your database is quick as it should be, you could use a query
to resolve the references on demand, and you wouldn't need a special
structure to hold "the rest of the family". If the retrieval is slow or
difficult, then the creation of your structure could take a significant
amount of time.
One thing is, that I have to do some additional calculations when 
resolving the structure. The items will get some kind of 
labels/conditions and versions, further, when resolving the structure a 
set of rules for those conditions is given. At my first shot I'll have 
to do those calculations in the Python code (even if it would be very 
wicked to do stuff like that with SQL). So, I will always have a large 
number of items in memory, as I don't want to call the database for each 
structure-level I want to expand. Also, I'm using a pyqt-treeview 
(AbstractItemModel) for a client-site gui. For this purpose I need to 
create an additional structure, as in the original data items can have 
more than one parent, which is not permitted in the model for the treeview.
The whole idea of replacing the id-references to object-references is to 
enhance performance and make the application code easier.


Thanks for the feedback so far.


Hope this helps,
Francesco




Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3100 -  Data di rilascio: 
08/29/10 08:34:00




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


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


[Tutor] Help with Object Oriented Programming

2010-08-30 Thread Tino Dai
Hi Everybody,

   I'm beefing up my Object-Oriented Analysis and Design - getting
the gaps in my
knowledge filled in through the Head First OOAD book
(http://headfirstlabs.com/books/hfooad/).
 That book focuses on Java - is there a comparable book for Python? I
have already read the
Alan Gauld's intro on classes, but I'm looking for more. My goal is to
be able to design
and code in Python in an OO fashion exploiting the OO paradigm as it related to
Python at the level of  Kent Johnston (hey, if I'm going to dream,
might as well dream big! :)  ).
Currently, some of the things such as inheritance and encapsulation
apply across OO languages
but interfaces (I know that Zope has this) and mixin's are language
specific constructs.
If anybody could point me in the right direction, that would be great!

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


Re: [Tutor] Adding to a CSV file?

2010-08-30 Thread Luke Paireepinart
On Mon, Aug 30, 2010 at 12:04 PM,   wrote:
> I checked out the csv module and got a little further along, but still can't
> quite figure out how to iterate line by line properly.
>  > # But when I try to add columns, I'm only filling in some static value. So
> there's something wrong with my looping.
>
> testReader=csv.reader(open('test-8-29-10.csv', 'rb'))
> for line in testReader:
>  for MyWord, Category, Ct, CatCt in testReader:
>    text=nltk.word_tokenize(MyWord)
>    word2=wnl.lemmatize(word)
>    word3=porter.stem(word)

here's your problem.  MyWord is the value that is changing in each
iteration of the loop, not word.
Change your lines to
word2=wnl.lemmatize(MyWord)
word3=proter.stem(MyWord)
and it should work fine.

You should've gotten an undefined variable exception, unless you
defined word somewhere else.

Watch those variable names!
HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding to a CSV file?

2010-08-30 Thread aeneas24

I checked out the csv module and got a little further along, but still can't 
quite figure out how to iterate line by line properly. 

# This shows that I'm reading the file in correctly:

input_file=open("test-8-29-10.csv","rb")
for row in input_file:
   print row

MyWord,Category,Ct,CatCt
!,A,2932,456454
!,B,2109,64451
a,C,7856,9
abandoned,A,11,456454


# But when I try to add columns, I'm only filling in some static value. So 
there's something wrong with my looping.

testReader=csv.reader(open('test-8-29-10.csv', 'rb'))
for line in testReader:
 for MyWord, Category, Ct, CatCt in testReader:
   text=nltk.word_tokenize(MyWord)
   word2=wnl.lemmatize(word)
   word3=porter.stem(word)
   print 
MyWord+","+Category+","+Ct+","+CatCt+","+word+","+word2+","+word3+"\r\n"
  
!,A,2932,456454,yrs,yr,yr
!,B,2109,64451,yrs,yr,yr
a,C,7856,9,yrs,yr,yr
abandoned,A,11,456454,yrs,yr,yr
...

# I tried adding another loop, but it gives me an error.

testReader=csv.reader(open('test-8-29-10.csv', 'rb'))
for line in testReader:
   for MyWord, Category, Ct, CatCt in line:  # I thought this line inside the 
other was clever, but, uh, not so much
   text=nltk.word_tokenize(MyWord)
   word2=wnl.lemmatize(word)
   word3=porter.stem(word)
  print MyWord+","+Category+","+Ct+","+CatCt+","+word+","+word2+","+word3+"\r\n"
  
Traceback (most recent call last):
  File "", line 2, in 
for MyWord, Category, Ct, CatCt in line:
ValueError: too many values to unpack



My hope is that once I can figure out this problem, it'll be easy to write the 
csv file with the csv module. But I'm stumped about the looping.

Thanks for any suggestions,

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


Re: [Tutor] can this be done easerly

2010-08-30 Thread ALAN GAULD


> I tried your suggestion with strip and it looks like this :
 
> import string

You don't need this now

> def extract_words(s):
>word= ""

You dont need this

> s2=[]
> s = string.lower(s)
> s = s.replace( "--", " ")

You probably don't need this either - just add '-' to the strip set.

> s = s.split()
> for word in s:

And you could combine those to

for word in s.split():

> test = word 
> test = word.strip('!?,.')

You don't need test, just use

word = word.strip('!?",.-')  #added " and - to your set...

> s2.append(test)
> return s2

> It works well only I can't get it to work the strip with ". 
> I always get a EOF error message. I tried it with 
> word.strip('!?,.") and with word.strip('!?,./"'') but no go.

It looks like you are mixing quote styles.
Notice how I included the " in the strip() above... 
See if that works for you.

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


Re: [Tutor] Conditional attribute access / key access

2010-08-30 Thread Francesco Loffredo

On 30/08/2010 16.44, Knacktus wrote:

Hey everyone,

I have a huge number of data items coming from a database. So far
there're no restrictions about how to model the items. They can be
dicts, objects of a custom class (preferable with __slots__) or namedTuple.

Those items have references to each other using ids. Fresh from the
database the items look like this (using dicts as examples):

item_1 = {"id": 1, "name": "root", "children_ids": [2, 3]}
item_2 = {"id": 2, "name": "child_1", "children_ids": [4]}
item_3 = {"id": 3, "name": "child_2", "children_ids": [6, 7, 8]}

Now I'd like to resolve the references on demand.


Two questions and one doubt for you:
1- How many "generations" do you want to keep in a single item (call it 
dictionary or list, or record, whatever)? I mean, what if some children 
have children too, and some of those have more children, etc ?


2- Are you SURE that there are no circular references in your database? 
In your example, what if item_3 was
item_3 = {"id": 3, "name": "child_2", "children_ids": [6, 1, 8]}? Is't 
it possible that those recursion limit problems you had could come from 
some circular reference in your data?


d- If the number of data items is really huge, are you sure that you 
want to keep the whole family in memory at the same time? It depends on 
the answer you gave to my question #1, of course, but if retrieving an 
item from your database is quick as it should be, you could use a query 
to resolve the references on demand, and you wouldn't need a special 
structure to hold "the rest of the family". If the retrieval is slow or 
difficult, then the creation of your structure could take a significant 
amount of time.


Hope this helps,
Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3100 -  Data di rilascio: 
08/29/10 08:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Conditional attribute access / key access

2010-08-30 Thread Knacktus

Hey everyone,

I have a huge number of data items coming from a database. So far 
there're no restrictions about how to model the items. They can be 
dicts, objects of a custom class (preferable with __slots__) or namedTuple.


Those items have references to each other using ids. Fresh from the 
database the items look like this (using dicts as examples):


item_1 = {"id": 1, "name": "root", "children_ids": [2, 3]}
item_2 = {"id": 2, "name": "child_1", "children_ids": [4]}
item_3 = {"id": 3, "name": "child_2", "children_ids": [6, 7, 8]}

Now I'd like to resolve the references on demand. For that purpose I 
think about adding another entry in my dicts ("children_obj_refs"):


item_1 = {"id" = 1, "nam"e = "root", "children_ids" = [2, 3], 
"children_obj_refs" = [item_2, item_3]}


To achieve that substitution dynamically on demand I could use a function:

def get_children(item):
try:
return item["children_obj_refs"]
except AttributeError:
# pseudocode for retrieving items from db based on ids
fresh_items_from_db = get_items_from_db(item["children_ids"])
# Now create new dict entry for future usage
item["children_obj_refs"] = fresh_items_from_db

However, I dislike to have to call a function all the time. I'd rather 
get this done under the hood when accessing the dict with 
item["children_obj_refs"]. So I've looked into subclassing a dict and 
overwriting the __getitem__() method like this:


class TestSubDict(dict):

def __getitem__(self, key):
try:
return self[key]
except KeyError:
# load item from db and so on ...

But I run into recursion limit problems.

Any recommendations about that? (I'm not restricted to dicts, custom 
classes are OK to, but with __slots__ to limit memory consumption.)


Many thanks in advance and cheers,

Jan

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


Re: [Tutor] can this be done easerly

2010-08-30 Thread Steven D'Aprano
On Mon, 30 Aug 2010 07:44:06 pm Roelof Wobben wrote:
> Hello,
>
>
>
> For a exerise I made this one :"
>
>
>
> import string
>
> def extract_words(s):
[...]

> But now I wonder if this can be done more easerly ?


def extract_words(s):
    """
    >>> extract_words('Now is the time!  "Now", is the time?')
    ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time']
    >>> extract_words('she tried to curtsy as she spoke--fancy')
    ['she', 'tried', 'to', 'curtsy', 'as', 'she', 'spoke', 'fancy']
    """
    s = s.lower()
# Replace anything that isn't a letter with a space.
chars = [c if c.isalpha() else " " for c in s]
s = ''.join(chars)
words = s.split()
return words


import doctest
doctest.run_docstring_examples(extract_words, globals())
print extract_words("Hello, world! I ate 6 slices of cheese.")

=> prints:
['hello', 'world', 'i', 'ate', 'slices', 'of', 'cheese']




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


Re: [Tutor] can this be done easerly

2010-08-30 Thread Alan Gauld

"Roelof Wobben"  wrote


import string


I know your tutorial uses the string module but you really should
get out of that habit. It will not work in the newer versions of 
Python

and is less efficient that using the builtin methods.
And invariably requires more typing! :-)

def extract_words(s):
   """
 >>> extract_words('Now is the time!  "Now", is the time? Yes, 
now.')
 ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 
'now']


This is a bad example since it uses " twice in the same string.
It should probably read

 >>> extract_words('Now is the time!  'Now', is the time? Yes, 
now.")


   word= ""
   s=string.lower(s)

So this should be
   s = s.lower()


   for char in s :
   if ord(char) >=65 and ord(char) <= 122 or ord(char)==32 or 
ord(char)==45:

 word= word + char


You don't really need to process it letter by letter and even if you 
do

you don't need the ord() tests, just use isalpha() etc

if char.isalpha(): word += char


   word=string.split(word, "--")
   word=string.join(word, " ")


Becomes

word = " " .join(word.split('--'))

But you could just have used strip() - see below - or even replace()

>   word=word.replace ("  ", " ")

But oddly you use the string method here rather than string.replace() 
?

A good choice.


   word=string.split(word, " ")


   word = word.split(" ")


   return word


if __name__ == '__main__':
   import doctest
   doctest.testmod()


But now I wonder if this can be done more easily ?


Yes, you can do it all at the word level and you can remove
the characters you don't want rather than testing for their
presence etc.

Look at the documentation for strip() you will see that you
can provide a list of characters that you want removed.
So a single call to split() followed by strip() on each word
should do the vast bulk of the work for you.

HTH,

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


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


Re: [Tutor] can this be done easerly

2010-08-30 Thread Roelof Wobben


 

> Subject: Re: [Tutor] can this be done easerly
> From: evert@gmail.com
> Date: Mon, 30 Aug 2010 12:04:08 +0200
> CC: tutor@python.org
> To: rwob...@hotmail.com
> 
> > For a exerise I made this one :"
> > 
> > import string
> > def extract_words(s):
> > """
> > >>> extract_words('Now is the time! "Now", is the time? Yes, now.')
> > ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']
> > >>> extract_words('she tried to curtsey as she spoke--fancy')
> > ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
> > """
> > word= ""
> > s=string.lower(s)
> > for char in s :
> > if ord(char) >=65 and ord(char) <= 122 or ord(char)==32 or ord(char)==45:
> > word= word + char 
> > word=string.split(word, "--")
> > word=string.join(word, " ")
> > word=word.replace (" ", " ")
> > word=string.split(word, " ")
> > return word
> > 
> > if __name__ == '__main__':
> > import doctest
> > doctest.testmod()
> > 
> > But now I wonder if this can be done more easerly ?
> 
> Using regular expressions could work, depending on your view of regular 
> expressions being 'easy':
> 
> import re
> re.split('\W+', s.lower()) 
> 
> will do most of what you want (though you'll end up with the occasional empty 
> string.
> 
> Evert
> 

 

Hello Evert, 

 

Thank you for the answer.
I following this tutorial 
(http://openbookproject.net/thinkcs/python/english2e/) and till chapter 10 
there is no talking about regular expressions.
So this is not easy for me.

But thanks , I will read on regular expressions so I understand that one too.

 

Roelof


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


Re: [Tutor] can this be done easerly

2010-08-30 Thread Evert Rol
> For a exerise I made this one :"
>  
> import string
> def extract_words(s):
> """
>   >>> extract_words('Now is the time!  "Now", is the time? Yes, now.')
>   ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']
>   >>> extract_words('she tried to curtsey as she spoke--fancy')
>   ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
> """
> word= ""
> s=string.lower(s)
> for char in s :
> if ord(char) >=65 and ord(char) <= 122 or ord(char)==32 or 
> ord(char)==45:
>   word= word + char 
> word=string.split(word, "--")
> word=string.join(word, " ")
> word=word.replace ("  ", " ")
> word=string.split(word, " ")
> return word
> 
> if __name__ == '__main__':
> import doctest
> doctest.testmod()
>  
> But now I wonder if this can be done more easerly ?

Using regular expressions could work, depending on your view of regular 
expressions being 'easy':

import re
re.split('\W+', s.lower()) 

will do most of what you want (though you'll end up with the occasional empty 
string.

  Evert

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


[Tutor] can this be done easerly

2010-08-30 Thread Roelof Wobben

Hello,

 

For a exerise I made this one :"

 

import string

def extract_words(s):
"""
  >>> extract_words('Now is the time!  "Now", is the time? Yes, now.')
  ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']
  >>> extract_words('she tried to curtsey as she spoke--fancy')
  ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
"""
word= ""
s=string.lower(s)
for char in s :
if ord(char) >=65 and ord(char) <= 122 or ord(char)==32 or 
ord(char)==45:
  word= word + char 
word=string.split(word, "--")
word=string.join(word, " ")
word=word.replace ("  ", " ")
word=string.split(word, " ")
return word

if __name__ == '__main__':
import doctest
doctest.testmod()

 

But now I wonder if this can be done more easerly ?

 

Roelof

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