[Tutor] pure symbol -- __subtype__

2010-02-18 Thread spir
Hello,

I was lately implementing a kind of "pure symbol" type. What I call pure 
symbols is these kinds of constants that refer to pure "idea", so that they 
have no real obvious value. We usually _arbitrarily_ give them as value an int, 
a string, a boolean, an empty object:
   BLACK, WHITE = False, True
   GUARD_FLAG = object()
   N,S,W,E = 1,2,3,4
   READ,WRITE = 'r','w'
   ...

Such symbols, especially the ones we give boolean values, often match the use 
of C preprocessor flags:
   #define GUARD_FLAG
   ...
   #ifdef GUARD_FLAG ...
When there is a set of symbols, they match Pascal enumerations:
   var
  direction: (N, S, W, E);
They are often used as func parameters.
   f = open("foo.txt", WRITE)
The latter case is so common that numerous (built-in or third-party) libraries 
define a whole load of "pure symbol" constant values to be used as func 
arguments:
   pattern = re.compile(format, re.MULTILINE)

This is very heavy, indeed. But alternatives I can imagine are worse:
* Use literal (unnamed) values: illegible.
* Use unprefixed names: pollutes global namespace.
I cannot find any good solution for this issue. This is my first question.

These pure symbol are, I guess, a very close notion to the one of "nominals" 
(see http://en.wikipedia.org/wiki/Nominal_number). And in fact pascal enums are 
nominal types. So, I wrote this for isolated symbols:
class Nominal(object):
count = 0
def __init__(self, value=None):
self.type = self.__class__
self.type.count += 1
# no need to restrict user-provided value, if any, to natural integer
self.value = value if value is not None else self.type.count
def __str__(self):
typeName = self.type.__name__
return "%s:(%s)" %(typeName,self.value)
x,y,z = Nominal(),Nominal(),Nominal()
print x,y,z # Nominal:(1) Nominal:(2) Nominal:(3)

The type here can only be Nominal; and the value is not really needed, indeed, 
but it can be useful in some cases. Especially, this type can be the base of 
derived Nominal types, i.e. pascal-like enums. Like in Pascal, making the 
instances comparable can be very handy:
def __lt__(self, other):
assert(isinstance(other, Nominal))
return self.value < other.value
class CardSuite(Nominal): pass
club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite()
print club,diamond,heart,spade  # CardSuite:(4) CardSuite:(5) CardSuite:(6) 
CardSuite:(7)
print(diamond < heart)  # True

An issue is that a subtupe should start with count==0. I could not find any way 
to do that, so I ended up writing a subtype factory. But this goes against the 
language, for the user cannot use anymore the dedicated idiom "class 
CardSuite(Nominal)". Also, the type's name has to be *stupidly* passed as 
argument. So, the content of the method itself clearly shows how artificial 
this solution is:
@classmethod
def subtype(cls, name):
class DummyName(cls): pass
DummyName.count = 0
DummyName.__name__ = name
return X
CardSuite = Nominal.subtype("CardSuite")
club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite()
print club,diamond,heart,spade  # CardSuite:(1) CardSuite:(2) CardSuite:(3) 
CardSuite:(4)
print(diamond < heart)  # True

Actually, what I need is a kind of __subtype__ magic method that acts for 
subtyping the same way __init__ does for instanciation. Then, I could write:
@staticmethod
def __subtype__(subtype):
subtype.count = 0
(Note that here I do not need a classmethod, as staticmethod is enough.)

So, do you see any other solution? (I have probably overlooked some)
And, what do you think of __subtype__?

Denis


la vita e estrany

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


Re: [Tutor] pure symbol -- __subtype__

2010-02-18 Thread spir
PS: see also on the topic: http://en.wikipedia.org/wiki/Enumerated_type

On Thu, 18 Feb 2010 10:13:33 +0100
spir  wrote:

> Hello,
> 
> I was lately implementing a kind of "pure symbol" type. What I call pure 
> symbols is these kinds of constants that refer to pure "idea", so that they 
> have no real obvious value. We usually _arbitrarily_ give them as value an 
> int, a string, a boolean, an empty object:
>BLACK, WHITE = False, True
>GUARD_FLAG = object()
>N,S,W,E = 1,2,3,4
>READ,WRITE = 'r','w'
>...
> 
> Such symbols, especially the ones we give boolean values, often match the use 
> of C preprocessor flags:
>#define GUARD_FLAG
>...
>#ifdef GUARD_FLAG ...
> When there is a set of symbols, they match Pascal enumerations:
>var
>   direction: (N, S, W, E);
> They are often used as func parameters.
>f = open("foo.txt", WRITE)
> The latter case is so common that numerous (built-in or third-party) 
> libraries define a whole load of "pure symbol" constant values to be used as 
> func arguments:
>pattern = re.compile(format, re.MULTILINE)
> 
> This is very heavy, indeed. But alternatives I can imagine are worse:
> * Use literal (unnamed) values: illegible.
> * Use unprefixed names: pollutes global namespace.
> I cannot find any good solution for this issue. This is my first question.
> 
> These pure symbol are, I guess, a very close notion to the one of "nominals" 
> (see http://en.wikipedia.org/wiki/Nominal_number). And in fact pascal enums 
> are nominal types. So, I wrote this for isolated symbols:
> class Nominal(object):
> count = 0
> def __init__(self, value=None):
> self.type = self.__class__
> self.type.count += 1
> # no need to restrict user-provided value, if any, to natural integer
> self.value = value if value is not None else self.type.count
> def __str__(self):
> typeName = self.type.__name__
> return "%s:(%s)" %(typeName,self.value)
> x,y,z = Nominal(),Nominal(),Nominal()
> print x,y,z   # Nominal:(1) Nominal:(2) Nominal:(3)
> 
> The type here can only be Nominal; and the value is not really needed, 
> indeed, but it can be useful in some cases. Especially, this type can be the 
> base of derived Nominal types, i.e. pascal-like enums. Like in Pascal, making 
> the instances comparable can be very handy:
> def __lt__(self, other):
> assert(isinstance(other, Nominal))
> return self.value < other.value
> class CardSuite(Nominal): pass
> club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite()
> print club,diamond,heart,spade# CardSuite:(4) CardSuite:(5) 
> CardSuite:(6) CardSuite:(7)
> print(diamond < heart)# True
> 
> An issue is that a subtupe should start with count==0. I could not find any 
> way to do that, so I ended up writing a subtype factory. But this goes 
> against the language, for the user cannot use anymore the dedicated idiom 
> "class CardSuite(Nominal)". Also, the type's name has to be *stupidly* passed 
> as argument. So, the content of the method itself clearly shows how 
> artificial this solution is:
> @classmethod
> def subtype(cls, name):
> class DummyName(cls): pass
> DummyName.count = 0
> DummyName.__name__ = name
> return X
> CardSuite = Nominal.subtype("CardSuite")
> club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite()
> print club,diamond,heart,spade# CardSuite:(1) CardSuite:(2) 
> CardSuite:(3) CardSuite:(4)
> print(diamond < heart)# True
> 
> Actually, what I need is a kind of __subtype__ magic method that acts for 
> subtyping the same way __init__ does for instanciation. Then, I could write:
> @staticmethod
> def __subtype__(subtype):
> subtype.count = 0
> (Note that here I do not need a classmethod, as staticmethod is enough.)
> 
> So, do you see any other solution? (I have probably overlooked some)
> And, what do you think of __subtype__?
> 
> Denis
> 
> 
> la vita e estrany
> 
> http://spir.wikidot.com/






la vita e estrany

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


[Tutor] How to delete the last line from a file efficiently?

2010-02-18 Thread Joson
Hi all,
Now I have a text file about 1MB.  Sometimes I need to remove the last
line.
The current method I use is reading all content, finding the last
sentence, remove it and write the new content to the file.
So, it need to read a 1MB file and write a 1MB file. It's ineffictient,
I think. Are there any other methods?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and algorithms

2010-02-18 Thread C.T. Matsumoto

Hello Tutors,

Can someone point me to any resources that can teach me about algorithms 
in python?


I'm interested in learning how to analyze and make an algorithm.

Oh, I have no background in math, but a sturdy knowledge of python, and 
I want to

make more efficient code.

Cheers,

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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Christian Witts

C.T. Matsumoto wrote:

Hello Tutors,

Can someone point me to any resources that can teach me about 
algorithms in python?


I'm interested in learning how to analyze and make an algorithm.

Oh, I have no background in math, but a sturdy knowledge of python, 
and I want to

make more efficient code.

Cheers,

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


You can try this out:
Data Structures and Algorithms with Object-Oriented Design Patterns in 
Python

http://www.brpreiss.com/books/opus7/html/book.html

--
Kind Regards,
Christian Witts


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


Re: [Tutor] Python and algorithms

2010-02-18 Thread C.T. Matsumoto

Thanks Christian,

I'd found that resource and at first glance it looks above my head. The 
book also
has examples based on existing algorithms and analyzes them. When I said 
analyze

I suppose I meant analyze a problem and make an algorithm out of that.

Not to mention the math is out of my skill set (for now).

I'm looking for the real basics. I'm not sure if I can get away with 
applying the

subject directly into python, without first studying some math. On the other
hand I've made it this far and I suppose there are other developers out 
there

that didn't have the math background and worked out how to efficiently solve
problems.

Thanks,

Todd

Christian Witts wrote:

C.T. Matsumoto wrote:

Hello Tutors,

Can someone point me to any resources that can teach me about 
algorithms in python?


I'm interested in learning how to analyze and make an algorithm.

Oh, I have no background in math, but a sturdy knowledge of python, 
and I want to

make more efficient code.

Cheers,

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


You can try this out:
Data Structures and Algorithms with Object-Oriented Design Patterns in 
Python

http://www.brpreiss.com/books/opus7/html/book.html



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


Re: [Tutor] How to delete the last line from a file efficiently?

2010-02-18 Thread Christian Witts

Joson wrote:

Hi all,
Now I have a text file about 1MB.  Sometimes I need to remove the 
last line.
The current method I use is reading all content, finding the last 
sentence, remove it and write the new content to the file.
So, it need to read a 1MB file and write a 1MB file. It's 
ineffictient, I think. Are there any other methods?



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
  
Take a look at file.seek to move to a specific section of the file and 
file.truncate to truncate a file at a specific size, rough 
implementation would be something like.


BUFFER = 2**10  # Set your buffer size
f = open(filename, 'r')
multiple = -1
found_it = False

while not found_it:
   f.seek(BUFFER * multiple, os.SEEK_END)
   # Seeks from the file endpoint the specified by your buffer * 
'iteration' of the app

   offset = f.tell()   # gets the current position
   data = f.read(BUFFER)   # read in your data and process it
   # perform operations to get your termination point offset
   if termination_point_not_found:
   multiple -= 1
   else:
   # once you find your position to end the file on truncate the 
file and exit loop

   f.truncate(offset + termination_point_offset)
   found_it = True

f.close()

It's untested code, but it should work just fine.
As for how fast it is by comparison, I unfortunately don't have time to 
benchmark it.


--
Kind Regards,
Christian Witts


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


Re: [Tutor] pure symbol -- __subtype__

2010-02-18 Thread Steven D'Aprano
On Thu, 18 Feb 2010 08:13:33 pm spir wrote:
> Hello,
>
> I was lately implementing a kind of "pure symbol" type. What I call
> pure symbols is these kinds of constants that refer to pure "idea",
> so that they have no real obvious value. We usually _arbitrarily_
> give them as value an int, a string, a boolean, an empty object:

If you are interested in this, there are various modules on PyPI for 
them, such as:

http://pypi.python.org/pypi/enum/

which probably does everything you want.

Unfortunately any enum solution is going to be rather heavyweight 
compared to (say) Pascal, which can simply give each enum an integer 
value and have the compiler enforce separation of types. (In other 
words, all the heavy complexity is in the compiler rather than the 
runtime environment.)

If you want a lightweight solution, just use constant strings or 
integers.


> Actually, what I need is a kind of __subtype__ magic method that acts
> for subtyping the same way __init__ does for instanciation. 

That's what metaclasses are for.



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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Christian Witts

C.T. Matsumoto wrote:

Thanks Christian,

I'd found that resource and at first glance it looks above my head. 
The book also
has examples based on existing algorithms and analyzes them. When I 
said analyze

I suppose I meant analyze a problem and make an algorithm out of that.

Not to mention the math is out of my skill set (for now).

I'm looking for the real basics. I'm not sure if I can get away with 
applying the
subject directly into python, without first studying some math. On the 
other
hand I've made it this far and I suppose there are other developers 
out there
that didn't have the math background and worked out how to efficiently 
solve

problems.

Thanks,

Todd

Christian Witts wrote:

C.T. Matsumoto wrote:

Hello Tutors,

Can someone point me to any resources that can teach me about 
algorithms in python?


I'm interested in learning how to analyze and make an algorithm.

Oh, I have no background in math, but a sturdy knowledge of python, 
and I want to

make more efficient code.

Cheers,

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


You can try this out:
Data Structures and Algorithms with Object-Oriented Design Patterns 
in Python

http://www.brpreiss.com/books/opus7/html/book.html





Hi Todd,

Possibly look around to see if you can get a copy of Introduction to 
Data Structures and Algorithms in Python 
(http://www.cs.luther.edu/~bmiller/Papers/booktoc.pdf), not sure if it's 
something printed outside of the university but their ToC might be more 
in line with what you're looking for.


Potentially:
Design & Analysis of Algorithms 
(http://www.personal.kent.edu/~rmuhamma/Algorithms/algorithm.html)

Algorithms Wikibook (http://en.wikibooks.org/wiki/Algorithms)
Intro to Algorithms 3rd Edition 
(http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=11866)


I've never personally seen a resource with the intention of teaching you 
how to analyse a problem and create an algorithmic solution without 
first teaching you some of the fundamental mathematics and already 
existing algorithmic solutions.


Hope some of that helps.

--
Kind Regards,
Christian Witts


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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Kent Johnson
On Thu, Feb 18, 2010 at 4:59 AM, C.T. Matsumoto  wrote:
> Hello Tutors,
>
> Can someone point me to any resources that can teach me about algorithms in
> python?
>
> I'm interested in learning how to analyze and make an algorithm.

I have been reading The Algorithm Design Manual
http://www.algorist.com/

I find it more readable than Cormen, et al.

But is that what you are asking for, or are you trying to sharpen your
problem-solving skills? Many progamming problems are solved by simple
loops and data structures without explicitly using any algorithms that
you would find in a book such as this.

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


Re: [Tutor] How to delete the last line from a file efficiently?

2010-02-18 Thread Rohit Kumar
Here is the efficient code than your code: 
==CODE== 
lines = file('file1.txt', 'r').readlines() 
print lines 
del lines[-1] 
print lines 
file('file1.txt', 'w').writelines(lines) 
 


Please let me know if any issues. 
Regards, 
Rohit Kumar 
== 
Software Developer - Trainee 
ZeOmega Infotech Private Limited 
RLP Building, Bangalore - 560047 
Phone: 080-41666070 Ext: 126 
kro...@zeomega.com 
www.zeomega.com 
Proven. Progressive. Partner. 

*** 
CONFIDENTIALITY NOTICE: 

This message (including any attachments) may contain ZeOmega's 
confidential information, protected by law. Forwarding it to 
individuals, other than those with a need to know, without the 
permission of the sender, is prohibited. This message is intended for 
specific individual(s) or entity to whom it is intended even if 
addressed incorrectly. If you are not the intended recipient, you 
should delete this message and are hereby notified that any 
disclosure,copying, or distribution of this message or taking any 
action based upon it, is strictly prohibited. 
*** 

- Original Message - 
From: "Joson"  
To: Tutor@python.org 
Sent: Thursday, February 18, 2010 3:00:48 PM 
Subject: [Tutor] How to delete the last line from a file efficiently? 

Hi all, 
Now I have a text file about 1MB. Sometimes I need to remove the last line. 
The current method I use is reading all content, finding the last sentence, 
remove it and write the new content to the file. 
So, it need to read a 1MB file and write a 1MB file. It's ineffictient, I 
think. Are there any other methods? 

___ 
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


Re: [Tutor] Python and algorithms

2010-02-18 Thread C.T. Matsumoto

Kent Johnson wrote:

On Thu, Feb 18, 2010 at 4:59 AM, C.T. Matsumoto  wrote:
  

Hello Tutors,

Can someone point me to any resources that can teach me about algorithms in
python?

I'm interested in learning how to analyze and make an algorithm.



I have been reading The Algorithm Design Manual
http://www.algorist.com/

I find it more readable than Cormen, et al.

But is that what you are asking for, or are you trying to sharpen your
problem-solving skills? Many progamming problems are solved by simple
loops and data structures without explicitly using any algorithms that
you would find in a book such as this.

Kent

  

I'd say sharpening my problem solving skills. I thought that was often
tied to building an algorithm. The example Walter Prins provided I
thought fit what I was looking for.

Cheers,

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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Shashwat Anand
One approach can be to go for CLRS and code the algorithms in python.
However I am not too sure you can grasp it at your level. Also if you think
your maths skills are not there yet, I suggest improve your math skills
first.

On Thu, Feb 18, 2010 at 6:47 PM, C.T. Matsumoto wrote:

> Kent Johnson wrote:
>
>> On Thu, Feb 18, 2010 at 4:59 AM, C.T. Matsumoto 
>> wrote:
>>
>>
>>> Hello Tutors,
>>>
>>> Can someone point me to any resources that can teach me about algorithms
>>> in
>>> python?
>>>
>>> I'm interested in learning how to analyze and make an algorithm.
>>>
>>>
>>
>> I have been reading The Algorithm Design Manual
>> http://www.algorist.com/
>>
>> I find it more readable than Cormen, et al.
>>
>> But is that what you are asking for, or are you trying to sharpen your
>> problem-solving skills? Many progamming problems are solved by simple
>> loops and data structures without explicitly using any algorithms that
>> you would find in a book such as this.
>>
>> Kent
>>
>>
>>
> I'd say sharpening my problem solving skills. I thought that was often
> tied to building an algorithm. The example Walter Prins provided I
> thought fit what I was looking for.
>
> Cheers,
>
>
> T
> ___
> 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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Kent Johnson
On Thu, Feb 18, 2010 at 8:17 AM, C.T. Matsumoto  wrote:
> Kent Johnson wrote:

>> But is that what you are asking for, or are you trying to sharpen your
>> problem-solving skills? Many progamming problems are solved by simple
>> loops and data structures without explicitly using any algorithms that
>> you would find in a book such as this.
>
> I'd say sharpening my problem solving skills. I thought that was often
> tied to building an algorithm. The example Walter Prins provided I
> thought fit what I was looking for.

I don't see Walter Prins' example.

It's true that solving a problem often involves creating an algorithm
in a broad sense. The formal study of algorithms studies specific
techniques and algorithms that have proven to be useful to solve many
hard problems. In my experience most programming problems do not
require use of these formal algorithms, at least not explicitly. Some
are used by Python under the hood, for example dicts are hash tables,
heapq is a priority queue, etc. It is very useful to know when to
apply these but you don't have to understand the details of how they
work.

Unfortunately I can't point you to a good resource, maybe that would
be a good project for me...

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


Re: [Tutor] Python and algorithms

2010-02-18 Thread C.T. Matsumoto

Kent Johnson wrote:

On Thu, Feb 18, 2010 at 8:17 AM, C.T. Matsumoto  wrote:
  

Kent Johnson wrote:



  

But is that what you are asking for, or are you trying to sharpen your
problem-solving skills? Many progamming problems are solved by simple
loops and data structures without explicitly using any algorithms that
you would find in a book such as this.
  

I'd say sharpening my problem solving skills. I thought that was often
tied to building an algorithm. The example Walter Prins provided I
thought fit what I was looking for.



I don't see Walter Prins' example.

It's true that solving a problem often involves creating an algorithm
in a broad sense. The formal study of algorithms studies specific
techniques and algorithms that have proven to be useful to solve many
hard problems. In my experience most programming problems do not
require use of these formal algorithms, at least not explicitly. Some
are used by Python under the hood, for example dicts are hash tables,
heapq is a priority queue, etc. It is very useful to know when to
apply these but you don't have to understand the details of how they
work.

Unfortunately I can't point you to a good resource, maybe that would
be a good project for me...

Kent

  

Here is the example.

"To keep this simple and practical, as a suggestion, consider the 
problem of sorting a list (a pack of cards, or a list of names or 
whatever you want) into order."


Yes, there are many built-ins that wrap good algorithms, so I guess I'm 
leaning more toward problem solving. The above example must be solved 
without using sorted() or list.sort().


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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Kent Johnson
On Thu, Feb 18, 2010 at 9:43 AM, C.T. Matsumoto  wrote:

> Here is the example.
>
> "To keep this simple and practical, as a suggestion, consider the problem of
> sorting a list (a pack of cards, or a list of names or whatever you want)
> into order."
>
> Yes, there are many built-ins that wrap good algorithms, so I guess I'm
> leaning more toward problem solving. The above example must be solved
> without using sorted() or list.sort().

To solve this without using the built-in sort then you will be
learning about sorting which is a major portion of the study of
algorithms.

So, if you want to learn about sorting algorithms, this is a good
problem to ponder. If you want to learn to be a better Python
programmer, I'm not sure it is helpful - the built-in sort is
excellent and you should be learning how to use it effectively, for
example, given a list of (first name, last name) print the list sorted
by first name, then sorted by last name.

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


Re: [Tutor] Python and algorithms

2010-02-18 Thread C.T. Matsumoto

Kent Johnson wrote:

On Thu, Feb 18, 2010 at 9:43 AM, C.T. Matsumoto  wrote:

  

Here is the example.

"To keep this simple and practical, as a suggestion, consider the problem of
sorting a list (a pack of cards, or a list of names or whatever you want)
into order."

Yes, there are many built-ins that wrap good algorithms, so I guess I'm
leaning more toward problem solving. The above example must be solved
without using sorted() or list.sort().



To solve this without using the built-in sort then you will be
learning about sorting which is a major portion of the study of
algorithms.

So, if you want to learn about sorting algorithms, this is a good
problem to ponder. If you want to learn to be a better Python
programmer, I'm not sure it is helpful - the built-in sort is
excellent and you should be learning how to use it effectively, for
example, given a list of (first name, last name) print the list sorted
by first name, then sorted by last name.

Kent

  

Cheers Kent. I'll take your example too. As for the other example
I'm already quite stuck.

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


[Tutor] pyMVPA and OSError

2010-02-18 Thread Juli
Dear All,

I am very much new to python, therefore I am likely to feel stupid
about asking this. I need pyMPVA module to play around with some fMRI
data. I have installed Python2.6 on Mac OS X Leopard. When I input >>>
import mvpa i get a deprecation warning, which is not a problem,
however when I try the following: >>> >>> import mvpa.suite as mvpa i
do not get a deprecating warning however I get a number of errors that
are as follows:
>>> import mvpa.suite as mvpa

Traceback (most recent call last):
  File "", line 1, in 
import mvpa.suite as mvpa
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mvpa/suite.py",
line 38, in 
from mvpa.algorithms.cvtranserror import *
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mvpa/algorithms/cvtranserror.py",
line 15, in 
from mvpa.measures.base import DatasetMeasure
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mvpa/measures/base.py",
line 31, in 
from mvpa.clfs.stats import autoNullDist
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mvpa/clfs/stats.py",
line 772, in 
if externals.exists('pylab'):
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mvpa/base/externals.py",
line 432, in exists
exec _KNOWN[dep]
  File "", line 1, in 
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mvpa/base/externals.py",
line 249, in __check_pylab
import pylab as P
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pylab.py",
line 1, in 
from matplotlib.pylab import *
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/pylab.py",
line 206, in 
from matplotlib import mpl  # pulls in most modules
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/mpl.py",
line 2, in 
from matplotlib import axis
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/axis.py",
line 10, in 
import matplotlib.font_manager as font_manager
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/font_manager.py",
line 1297, in 
_rebuild()
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/font_manager.py",
line 1288, in _rebuild
fontManager = FontManager()
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/font_manager.py",
line 980, in __init__
self.ttffiles = findSystemFonts(paths) + findSystemFonts()
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/font_manager.py",
line 337, in findSystemFonts
for f in get_fontconfig_fonts(fontext):
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/font_manager.py",
line 298, in get_fontconfig_fonts
pipe = subprocess.Popen(['fc-list', '', 'file'], stdout=subprocess.PIPE)
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py",
line 621, in __init__
errread, errwrite)
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py",
line 1126, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

-

I am not sure what the problem is (I do not know enough to pinpoint it
at a glance) and I would appreciate any feedback.

And I do once again apologize for asking stupid questions.


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


Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5

2010-02-18 Thread Robert Berman
> -Original Message-
> From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor-
> bounces+bermanrl=cfl.rr@python.org] On Behalf Of Wayne Watson
> Sent: Wednesday, February 17, 2010 10:07 PM
> To: tutor@python.org
> Subject: Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!],
> Py2.5
> 
> I'm following the tutorial and ran into a snag. Here  is the console
> output.( Can I do  this  from IDLE?)
> 
> C:\Sandia_Meteors\Sentinel_Development\Learn_Python>c:\python25\pyth
> on
> setup.py
> Traceback (most recent call last):
>File "setup.py", line 2, in 
>  import py2exe
> ImportError: No module named py2exe
> 
> Note the need to back pedal to c:\python25\
> Perhaps I need some path variable set?
> 
> --
Wayne,

When you install py2exe it should insure all the required modules are
available to your standard python path. For example, I am running python 2.64
and the py2exe module is in the python path.

I don't use IDLE. I use Ipython however I cannot see why IDLE would not work.
To tell if you are OK do import py2exe. You should have no problem loading it.
If you do, reinstall it.

Robert


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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Alan Gauld


"C.T. Matsumoto"  wrote

Can someone point me to any resources that can teach me about algorithms 
in python?


algorithm books tend to be fairly language neutral.

Search for books by Donald Knuth, his books tend to be considered
the canonical references on algorithms in computing.


I'm interested in learning how to analyze and make an algorithm.

Oh, I have no background in math, but a sturdy knowledge of python


In this instance a knowledge of math trumps knowledge of Python 
unfortunately.

Translating math into Python is relatively easy but you probably need the
math to develop efficient algorithms.

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] Python and algorithms

2010-02-18 Thread Alan Gauld


"C.T. Matsumoto"  wrote


I'd say sharpening my problem solving skills. I thought that was often
tied to building an algorithm. The example Walter Prins provided I
thought fit what I was looking for.


"To keep this simple and practical, as a suggestion, consider the problem 
of sorting a list (a pack of cards, or a list of names or whatever you 
want) into order."


Yes, there are many built-ins that wrap good algorithms, so I guess I'm 
leaning more toward problem solving. The above example must be solved 
without using sorted() or list.sort().


OK, having read more I think Knuth will be too deep.

Try Programming Pearls by Jon Bentley.
It covers more than just algoprithm development and is full of useful
generic advice about writing programs (eg dangers of over optimisation
etc) but includes quite a lot on algorithm development. And its easy and
fun to read too.

You can probably get the earlier 2 volumes (try the first as a taster - I
see vol2 on Amazon.com for less than $5 and vol1 on Amazon.co.uk
for less than £1)  secondhand or the more recent combined single
volume.

Alan G 



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


Re: [Tutor] Python and algorithms

2010-02-18 Thread Shashwat Anand
Solving problems on ACM UVA , SPOJ,
Codechef  helps too plus it is fun.

On Thu, Feb 18, 2010 at 11:48 PM, Alan Gauld wrote:

>
> "C.T. Matsumoto"  wrote
>
>  I'd say sharpening my problem solving skills. I thought that was often
 tied to building an algorithm. The example Walter Prins provided I
 thought fit what I was looking for.

>>>
>  "To keep this simple and practical, as a suggestion, consider the problem
>> of sorting a list (a pack of cards, or a list of names or whatever you want)
>> into order."
>>
>> Yes, there are many built-ins that wrap good algorithms, so I guess I'm
>> leaning more toward problem solving. The above example must be solved
>> without using sorted() or list.sort().
>>
>
> OK, having read more I think Knuth will be too deep.
>
> Try Programming Pearls by Jon Bentley.
> It covers more than just algoprithm development and is full of useful
> generic advice about writing programs (eg dangers of over optimisation
> etc) but includes quite a lot on algorithm development. And its easy and
> fun to read too.
>
> You can probably get the earlier 2 volumes (try the first as a taster - I
> see vol2 on Amazon.com for less than $5 and vol1 on Amazon.co.uk
> for less than £1)  secondhand or the more recent combined single
> volume.
>
> Alan G
>
> ___
> 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


Re: [Tutor] "Sounding" Off, IDLE (Win7)

2010-02-18 Thread Wayne Watson
Nothing to do with Ctrl-G. Cmd Prompt not open. So if you have a syntax 
error, no bell rings? I don't want to disable all sounds.


On 2/17/2010 2:48 AM, Michael M Mason wrote:

Wayne Watson wrote on 16 February 2010 at 17:58:-

   

In Win7 IDLE, when I type in something with a syntax
problem, a bell rings. How do I stop that? I've looked
at Control Panel Sounds, but don't see anything of
apparent use.
 

I don't get this on my Win7 machine. But anyway, the sound is
probably the same sound you get if you type CTRL-G at a command
prompt in a DOS box, in which case it isn't one of the sounds
you set in Control Panel.

You can disable it using Device Manager. It's called 'System
Speaker' and it's under 'System devices'.  Right-click and
choose 'Disable'.

   


--
"There is nothing so annoying as to have two people
 talking when you're busy interrupting." -- Mark Twain

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


Re: [Tutor] "Sounding" Off, IDLE (Win7)

2010-02-18 Thread Luke Paireepinart
System Speaker is just the driver for the builtin speaker in your computer
(but it will redirect sound to your main speakers if you have some).  You'll
get sounds out of your regular speakers, you  just won't get system beeps
anymore, if you disable System Speaker.

On Thu, Feb 18, 2010 at 6:09 PM, Wayne Watson
wrote:

> Nothing to do with Ctrl-G. Cmd Prompt not open. So if you have a syntax
> error, no bell rings? I don't want to disable all sounds.
>
>
> On 2/17/2010 2:48 AM, Michael M Mason wrote:
>
>> Wayne Watson wrote on 16 February 2010 at 17:58:-
>>
>>
>>
>>> In Win7 IDLE, when I type in something with a syntax
>>> problem, a bell rings. How do I stop that? I've looked
>>> at Control Panel Sounds, but don't see anything of
>>> apparent use.
>>>
>>>
>> I don't get this on my Win7 machine. But anyway, the sound is
>> probably the same sound you get if you type CTRL-G at a command
>> prompt in a DOS box, in which case it isn't one of the sounds
>> you set in Control Panel.
>>
>> You can disable it using Device Manager. It's called 'System
>> Speaker' and it's under 'System devices'.  Right-click and
>> choose 'Disable'.
>>
>>
>>
>
> --
>"There is nothing so annoying as to have two people
> talking when you're busy interrupting." -- Mark Twain
>
>
> ___
> 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


Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5

2010-02-18 Thread Wayne Watson
It imported setup fine from the IDLE cmd prompt. Win Cmd prompt is fine 
to operate it. Just curious about IDLE. I looked in setup.py and don't 
see what the complaint is. It sure thinks py2exe is not available.


I'm now in IDLE's path browser. I see pkgs in ...\lib\site-packages like 
dateutil, numdisplay, numpy, but no py2exe. Doesn't seem right, since I 
can import it. I'm pretty sure that during the install that py2exe was 
headed to site


On 2/18/2010 8:25 AM, Robert Berman wrote:

-Original Message-
From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor-
bounces+bermanrl=cfl.rr@python.org] On Behalf Of Wayne Watson
Sent: Wednesday, February 17, 2010 10:07 PM
To: tutor@python.org
Subject: Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!],
Py2.5

I'm following the tutorial and ran into a snag. Here  is the console
output.( Can I do  this  from IDLE?)

C:\Sandia_Meteors\Sentinel_Development\Learn_Python>c:\python25\pyth
on
setup.py
Traceback (most recent call last):
File "setup.py", line 2, in
  import py2exe
ImportError: No module named py2exe

Note the need to back pedal to c:\python25\
Perhaps I need some path variable set?

--
 

Wayne,

When you install py2exe it should insure all the required modules are
available to your standard python path. For example, I am running python 2.64
and the py2exe module is in the python path.

I don't use IDLE. I use Ipython however I cannot see why IDLE would not work.
To tell if you are OK do import py2exe. You should have no problem loading it.
If you do, reinstall it.

Robert



   


--
"There is nothing so annoying as to have two people
 talking when you're busy interrupting." -- Mark Twain

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


Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5

2010-02-18 Thread Wayne Watson
Got it. Fooled myself. I'm converting to  Win7 and have my XP keyboard 
and monitor side by side with the same for XP. I did the world program 
in XP and py2exe module in W7!!


world compiled and ran successfully. Now for a bigger program with 
matplotlib and tkinter. Maybe I'll just settle for a small matplotlib 
program for the moment. VBG


Thanks very much.

On 2/18/2010 4:30 PM, Wayne Watson wrote:
It imported setup fine from the IDLE cmd prompt. Win Cmd prompt is 
fine to operate it. Just curious about IDLE. I looked in setup.py and 
don't see what the complaint is. It sure thinks py2exe is not available.


I'm now in IDLE's path browser. I see pkgs in ...\lib\site-packages 
like dateutil, numdisplay, numpy, but no py2exe. Doesn't seem right, 
since I can import it. I'm pretty sure that during the install that 
py2exe was headed to site


On 2/18/2010 8:25 AM, Robert Berman wrote:

-Original Message-
From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor-
bounces+bermanrl=cfl.rr@python.org] On Behalf Of Wayne Watson
Sent: Wednesday, February 17, 2010 10:07 PM
To: tutor@python.org
Subject: Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!],
Py2.5

I'm following the tutorial and ran into a snag. Here  is the console
output.( Can I do  this  from IDLE?)

C:\Sandia_Meteors\Sentinel_Development\Learn_Python>c:\python25\pyth
on
setup.py
Traceback (most recent call last):
File "setup.py", line 2, in
  import py2exe
ImportError: No module named py2exe

Note the need to back pedal to c:\python25\
Perhaps I need some path variable set?

--

Wayne,

When you install py2exe it should insure all the required modules are
available to your standard python path. For example, I am running 
python 2.64

and the py2exe module is in the python path.

I don't use IDLE. I use Ipython however I cannot see why IDLE would 
not work.
To tell if you are OK do import py2exe. You should have no problem 
loading it.

If you do, reinstall it.

Robert







--
"There is nothing so annoying as to have two people
 talking when you're busy interrupting." -- Mark Twain

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


Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5

2010-02-18 Thread Wayne Watson
There's a bit of an anomaly. I've compiled 3 small programs now, and in 
cmd prompt a Dir does not find the file. It finds the py file, but not 
the completed file. Nevertheless, if I type in the prefix, the desired 
program executes.


On 2/18/2010 4:48 PM, Wayne Watson wrote:
Got it. Fooled myself. I'm converting to  Win7 and have my XP keyboard 
and monitor side by side with the same for XP. I did the world program 
in XP and py2exe module in W7!!


world compiled and ran successfully. Now for a bigger program with 
matplotlib and tkinter. Maybe I'll just settle for a small matplotlib 
program for the moment. VBG


Thanks very much.



--
"There is nothing so annoying as to have two people
 talking when you're busy interrupting." -- Mark Twain

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


Re: [Tutor] Python and algorithms

2010-02-18 Thread C.T. Matsumoto

Shashwat Anand wrote:
Solving problems on ACM UVA , SPOJ 
, Codechef  helps too 
plus it is fun.


On Thu, Feb 18, 2010 at 11:48 PM, Alan Gauld 
mailto:alan.ga...@btinternet.com>> wrote:



"C.T. Matsumoto" mailto:c.t.matsum...@gmail.com>> wrote

I'd say sharpening my problem solving skills. I
thought that was often
tied to building an algorithm. The example Walter
Prins provided I
thought fit what I was looking for.


"To keep this simple and practical, as a suggestion, consider
the problem of sorting a list (a pack of cards, or a list of
names or whatever you want) into order."

Yes, there are many built-ins that wrap good algorithms, so I
guess I'm leaning more toward problem solving. The above
example must be solved without using sorted() or list.sort().


OK, having read more I think Knuth will be too deep.

Try Programming Pearls by Jon Bentley.
It covers more than just algoprithm development and is full of useful
generic advice about writing programs (eg dangers of over optimisation
etc) but includes quite a lot on algorithm development. And its
easy and
fun to read too.

You can probably get the earlier 2 volumes (try the first as a
taster - I
see vol2 on Amazon.com for less than $5 and vol1 on Amazon.co.uk

for less than £1)  secondhand or the more recent combined single
volume.

Alan G

___
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
  

Okay I'll look into them all.

Cheers,

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