[Tutor] Frozen Binaries

2005-05-30 Thread Chuck Allison
Hello tutor,

  Could someone please give me the quick lowdown on creating frozen
  binaries? Thanks.

-- 
Best regards,
 -- Chuck Allison

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


[Tutor] File Input of "Objects"

2005-06-01 Thread Chuck Allison
Hello tutor,

  I notice that with 'print >> f' one can print the string
  representation of objects to a file. Is there a corresponding input
  facility that parses through a file creating objects? I can't find
  one. In the meantime, I did this:

>>> f = open('out.dat','w')
>>> print >> f, 1, 2
>>> f.close()
>>> s = open('out.dat').read()
>>> x,y = tuple(map(eval,s.split()[:2]))
>>> x,y
(1, 2)  

This is just what came to mind without thought. Is there a Pythonic
way to read formatted objects? Thanks.

-- 
Best regards,
 -- Chuck Allison

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


[Tutor] Word COM interface

2005-06-09 Thread Chuck Allison
Hello,

Does anyone know how to get the Microsoft Word 11.0 Object library
working in ActiveState Python 2.4.1? When I go into PythonWin and try
to load it under makePy, I get the following error:

>>> Generating to 
>>> C:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x3.py
Failed to execute command:
from win32com.client import makepy;makepy.main()
Traceback (most recent call last):
  File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\toolmenu.py", 
line 103, in HandleToolCommand
exec "%s\n" % pyCmd
  File "", line 1, in ?
  File "C:\Python24\Lib\site-packages\win32com\client\makepy.py", line 362, in 
main
GenerateFromTypeLibSpec(arg, f, verboseLevel = verboseLevel, bForDemand = 
bForDemand, bBuildHidden = hiddenSpec)
  File "C:\Python24\Lib\site-packages\win32com\client\makepy.py", line 273, in 
GenerateFromTypeLibSpec
gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor)
  File "C:\Python24\Lib\site-packages\win32com\client\gencache.py", line 550, 
in AddModuleToCache
mod = _GetModule(fname)
  File "C:\Python24\Lib\site-packages\win32com\client\gencache.py", line 629, 
in _GetModule
mod = __import__(mod_name)
  File 
"C:\Python24\lib\site-packages\win32com\gen_py\00020905---C000-0046x0x8x3\__init__.py",
 line 2831
 '{00020960---C000-0046}' : 'Pane',
'{00020961---C000-0046}' : 'Windows',
   ^
 SyntaxError: invalid syntax

Any clue?

-- 
Best regards,
 Chuck

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


Re: [Tutor] Python Books

2005-06-12 Thread Chuck Allison
Hello Terry,

Practical Python is really good too, although I chose Learning Python
over it to teach my class this month.

Sunday, June 12, 2005, 9:00:34 AM, you wrote:

TC> On Sun, 12 Jun 2005, Kristiano Ang wrote:

>>  So, I'm prepared to invest in a Python book of some sort that will
>> help me master the language. Does anyone have any recommendations? Or
>> perhaps tutorials? I have limited programming experience (a little
>> dabbling in C++).

TC> For a how-to-learn Python book, it's hard to beat "Learning Python"

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



-- 
Best regards,
 Chuck

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


Re: [Tutor] Popen4 and Paths Containing Spaces

2005-06-15 Thread Chuck Allison
Hello Hugo,

Wednesday, June 15, 2005, 12:20:37 PM, you wrote:

HGM> This problem is explained there. Also, why are you using those forward 
HGM> slashes in your Windows paths Meaning:  d:/program 
HGM> files/winzip/wzzip.exe

Windows doesn't care. The only place you can't use forward slashes in
path names is in a command prompt. It's been that way since DOS 2.0 in
the 80s. I prefer using the forward slashes in file name strings since
they're fairly portable.

-- 
Best regards,
 Chuck

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


Re: [Tutor] A newbie question about running python scripts

2005-06-15 Thread Chuck Allison
Dear Tutors,

Here is a query from a student from a corporate class I'm teaching. Do 
you know of any such modules of the top of your head? Thanks!

===
Quick question since the next class isn't until Monday.  Do you know of 
any modules in Python that can convert a bitmap from one format to 
another, specifically .BMPs to .JPGs?

We have a need to do this conversion.  Our developers have grown fond of 
screen shots as they give much information that may not be written up in 
a bug.  However, our most common tool for getting a screen shot is a 
screen capture in VMWare which only does .BMP format files.  They are 
about 2.3 Mb apiece.   Where as a .JPG of the same thing is only about 
34 - 175 Kb.  The space savings is significant.   So, we need a way of 
efficiently convert these screenshots.  This class presents me an 
opportunity to create a utility to do this for us.  Hence, the question.
===

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


[Tutor] Every Other

2005-06-19 Thread Chuck Allison
Hello Tutors,

What would be the most Pythonic way of printing (or extracting) every 
other element of a list? Thanks in advance.

-- Chuck Allison

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


[Tutor] Class vs. Static Methods

2005-06-21 Thread Chuck Allison
Sorry for the elementary question: I was wondering if someone could
explain the difference to me between class and static methods. Coming
from other languages, I'm used to static methods, but not "class
methods". Thanks.

-- 
Best regards,
 Chuck

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


Re: [Tutor] Class vs. Static Methods

2005-06-21 Thread Chuck Allison
Hello Chinook,

So is the main motivation for class methods so that you can have the
class object available? It seems you can have that anyway in a static
method by just asking. I'm sure there's a good reason for this, but I
haven't yet gotten to the point of mastery where I can see a need for
class methods (even after reading Martelli's Nutshell). I understand
the syntax issues - I just don't see the need yet.

Tuesday, June 21, 2005, 3:28:48 PM, you wrote:

C> On Tue, 21 Jun 2005 16:52:09 -0400, Chuck Allison wrote
C> (in article <[EMAIL PROTECTED]>):

>> Sorry for the elementary question: I was wondering if someone could
>> explain the difference to me between class and static methods. Coming
>> from other languages, I'm used to static methods, but not "class
>> methods". Thanks.
>> 
>> 

C> Does this help (from the QR)?

C> Static methods : Use staticmethod(f) to make method f(x) static (unbound).
C> Class methods: like a static but takes the Class as 1st argument => Use f = 
C> classmethod(f) to make method f(theClass, x) a class method.

C> The decorators @staticmethod and @classmethod replace more elegantly the 
C> equivalent declarations f = staticmethod(f) and f = classmethod(f).

C> Lee C


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



-- 
Best regards,
 Chuck

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


Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Chuck Allison
This is a neat trick. But can't this also be done with a static method 
that accesses a static data attribute the same way?

Alan G wrote:

>>class Shape(object):
>>  _count = 0
>>  
>>  @classmethod
>>  def count(cls):
>>try:
>>  cls._count += 1
>>except AttributeError:
>>  cls._count = 1
>>
>>
>
>Ah, clever. This is where I thought I'd need an if/elif 
>chain, adding a new clause for each subclass. i never thought of 
>using a try/except to add an attribute to subclasses based on cls.
>
>I like it.
>
>Thanks again Kent.
>
>Alan G.
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Class vs. Static Methods

2005-06-22 Thread Chuck Allison
Hello Kent,

This is the killer example I've been looking for. Now I understand.
Sorry I've been so dense. This is way cool. Thanks.

Wednesday, June 22, 2005, 4:39:38 AM, you wrote:

KJ> Not sure why you think you have to write a new classmethod for
KJ> each shape. Suppose you want to maintain creation counts for each
KJ> class. Here is one way to do it using classmethods:

KJ> class Shape(object):
KJ>   _count = 0# Default for classes with no instances (cls.count() never 
called)
  
KJ>   @classmethod
KJ>   def count(cls):
KJ> try:
KJ>   cls._count += 1
KJ> except AttributeError:
KJ>   cls._count = 1
  
KJ>   @classmethod
KJ>   def showCount(cls):
KJ> print 'Class %s has count = %s' % (cls.__name__, cls._count)

KJ>   def __init__(self):
KJ> self.count()

KJ> class Point(Shape): pass

KJ> class Line(Shape): pass

KJ> p, p2, p = Point(), Point(), Point()
KJ> Point.showCount()

KJ> Line.showCount()
KJ> l = Line()
KJ> Line.showCount()


KJ> ### prints
KJ> Class Point has count = 3
KJ> Class Line has count = 0
KJ> Class Line has count = 1


KJ> Kent

-- 
Best regards,
 Chuck

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


Re: [Tutor] Interesting problem

2005-06-28 Thread Chuck Allison
I may be missing something, but isn't this what __dict__ does? Just 
return self.__dict__. This is an old message, so this may have mentioned 
already. Sorry if that's the case. I'm a little behind.

Kent Johnson wrote:

>Smith, Jeff wrote:
>  
>
>>Here would be the usage:
>>
>>myinst = MyClass()
>>print myinst.getprops_as_dict()
>>
>>would print
>>
>>{'var1': 1, 'var2': 2, 'var3': 3}
>>
>>Needless to say I want the instance values which might be different for
>>each instance.  I know that I could code it brute force, but I want to
>>be able to add properties without having to remember to update
>>getprops_as_dict().
>>
>>
>
>OK, so will a variation on my last recipe work? This looks for property 
>attributes of the class and gets the corresponding property on the instance:
>  def getprops_as_dict(self):
>return dict(pname, getattr(self, pname) 
>  for pname in dir(self.__class__) 
>if isinstance(getattr(self.__class__, pname), property))
>)
>
>Kent
>
>  
>
>>For those who are interested, the dictionary created by
>>getprops_as_dict() will be fed to string.Template.substitute
>>
>>Jeff
>>
>>
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String to List and back?

2005-07-02 Thread Chuck Allison
Hello Chinook,

How about join(), as in

''.join(strlist)

?


Saturday, July 2, 2005, 9:45:28 PM, you wrote:

C> I'm missing something simple again.  The simplified issue is:

C> Python 2.4.1 (#2, Mar 31 2005, 00:05:10)

C>  >>> mystr = 'abc'

C>   # I can create a list of the string characters
C>   #  with list comprehension
C>  >>> [c for c in mystr]
C> ['a', 'b', 'c']

C>   # Or just a simple builtin conversion function
C>  >>> list(mystr)
C> ['a', 'b', 'c']

C>   # But a conversion back to a string simply makes the
C>   #  the list a string (I know there would have to be
C>   #  special handling, but I noted it for illustration)
C>  >>> str(['a', 'b', 'c'])
C> "['a', 'b', 'c']"

C>   # I know I could get the original string back by
C>   #  rebuilding it with "for" loop
C>  >>> bts = ''
C>  >>> for c in strlist:
C> ...   bts += c
C> ...
C>  >>> bts
C> 'abc'

C>   # or even a function or class method which implemented
C>   #  the "for" loop
C>   # I could also build a new string directly from the
C>   #  original with a "for" loop

C> Is there a one-liner like a builtin conversion function or list 
C> comprehension technique for the reverse (i.e. a list of strings back to 
C> a single string)?

C> Thank you,

C> Lee C

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



-- 
Best regards,
 Chuck

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


Re: [Tutor] OT python Licences

2005-07-12 Thread Chuck Allison
Hello Sandip,

Tuesday, July 12, 2005, 10:38:09 AM, you wrote:

SB> Dave S wrote:
>> This is a bit OT but here goes.
>> 
>> My work wants me to write a fairly large python script to analyze some
>> technical ASCII data files. Python and its libraries are GPL.
>> 
>> That being the case am I right in thinking that my script would also
>> have to be GPL and I would have to inform my employer as I hand it over ?
>> 
>> Secondly it would have to run in Windows, The results could pop up on a
>> DOS window. However I was looking at QT until I read the Windows
>> license. Are there any widget libraries that would allow me to program
>> for windows commercially without any license or fees ?
>> 

What about wxWindows, for which we have wxPython?

-- 
Best regards,
 Chuck

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


Re: [Tutor] [Edu-sig] nice()

2006-02-13 Thread Chuck Allison





There is a reliable way to compute the exact number of floating-point "intervals" (one less than the number of FP numbers) between any two FP numbers. It is a long-ago solved problem. I have attached a C++ version. You can't define closeness by a "distance" in a FP system - you should use this measure instead (called "ulps" - units in the last place). The distance between large FP numbers may always be greater than the tolerance you prescribe. The spacing between adjacent FP numbers at the top of the scale for IEEE double precision numbers is 2^(972) (approx. 10^(293))! I doubt you're gping to make your tolerance this big. I don't believe newbies can grasp this, but they can be taught to get a "feel" for floating-point number systems. You can't write reliable FP code without this understanding. See http://uvsc.freshsources.com/decimals.pdf.

Sunday, February 12, 2006, 11:44:51 AM, you wrote:




>


I've been thinking about a function that was recently proposed at python-dev named 'areclose'. It is a function that is meant to tell whether two (or possible more) numbers are close to each other. It is a function similar to one that exists in Numeric. One such implementation is
 
def areclose(x,y,abs_tol=1e-8,rel_tol=1e-5):
    diff = abs(x-y)
    return diff <= ans_tol or diff <= rel_tol*max(abs(x),abs(y))
 
(This is the form given by Scott Daniels on python-dev.)
 
Anyway, one of the rationales for including such a function was: 
 
When teaching some programming to total newbies, a common frustration
is how to explain why a==b is False when a and b are floats computed
by different routes which ``should'' give the same results (if
arithmetic had infinite precision).  Decimals can help, but another
approach I've found useful is embodied in Numeric.allclose(a,b) --
which returns True if all items of the arrays are ``close'' (equal to
within certain absolute and relative tolerances)
The problem with the above function, however, is that it *itself* has a comparison between floats and it will give undesired result for something like the following test:
 
###
>>> print areclose(2, 2.1, .1, 0) #see if 2 and 2.1 are within 0.1 of each other
False
>>>
###
 
Here is an alternative that might be a nice companion to the repr() and round() functions: nice(). It is a combination of Tim Peter's delightful 'case closed' presentation in the thread, "Rounding to n significant digits?" [1] and the hidden magic of "prints" simplification of floating point numbers when being asked to show them. 
 
It's default behavior is to return a number in the form that the number would have when being printed. An optional argument, however, allows the user to specify the number of digits to round the number to as counted from the most significant digit. (An alternative name, then, could be 'lround' but I think there is less baggage for the new user to think about if the name is something like nice()--a function that makes the floating point numbers "play nice." And I also think the name...sounds nice.) 
 
Here it is in action:
 
###
>>> 3*1.1==3.3
False
>>> nice(3*1.1)==nice(3.3)
True
>>> x=3.21/0.65; print x
4.93846153846
>>> print nice(x,2)
4.9
>>> x=x*1e5; print nice(x,2)
49.0
###
 
Here's the function: 
###
def nice(x,leadingDigits=0):
 """Return x either as 'print' would show it (the default) or rounded to the
 specified digit as counted from the leftmost non-zero digit of the number,
 
 e.g. nice(0.00326,2) --> 0.0033"""
 assert leadingDigits>=0
 if leadingDigits==0:
  return float(str(x)) #just give it back like 'print' would give it
 leadingDigits=int(leadingDigits)
 return float('%.*e' % (leadingDigits,x)) #give it back as rounded by the %e format

###
 
Might something like this be useful? For new users, no arguments are needed other than x and floating points suddenly seem to behave in tests made using nice() values. It's also useful for those computing who want to show a physically meaningful value that has been rounded to the appropriate digit as counted from the most significant digit rather than from the decimal point. 
 
Some time back I had worked on the significant digit problem and had several math calls to figure out what the exponent was. The beauty of Tim's solution is that you just use built in string formatting to do the work. Nice.
 
/c
 
[1] http://mail.python.org/pipermail/tutor/2004-July/030324.html








-- 
Best regards,
 Chuck

// ulps.h: Counts the FP intervals between two FP numbers
#include 
#include 
#include 
#include 
#include 

template
int sign(const FType& x) {
return x < FType(0) ? -1 : x > FType(0) ? 1 : 0;
}

template
int lowerexp(FType z, FType& glb) {
int base = std::numeric_limits::radix;
// Find largest exponent of base <= z ("glb" = "base"^"exp").
// z is bracketed by [glb, lub)

// First, search above 1
FType lub = 1;
int exp = 0;
while (lub <= z) {
   lub *= base;
   ++exp;
}
glb = lub/base;
--exp;

// In case z < 1, search t