Re: What's The Best Editor for python

2006-03-26 Thread Charles Krug
On 2006-03-26, Andrew Gwozdziewycz [EMAIL PROTECTED] wrote:
 If you want something that won't get in your way, you should really  
 use /bin/ed. It's probably simpler to use then searching the archives.
 /bin/ed will also run in cygwin for windows.

 Can one of you say to me what's the best editor for
 editing the python programs( for linux or windows )


The best editor is the one you like best.

I'm a vim user with taglist--I'm not fully happy with how ctags does
Python, but it's more than Good Enough.

But editors are religious, and not worth arguing about, generally.
-- 
http://mail.python.org/mailman/listinfo/python-list


Want to re-pack() a Frame displaying a collection

2006-03-19 Thread Charles Krug
List,

I'd like to do the following with Tkinter's Frame() object:

1. Display a collection of pack()-able objects.  Easy.  Done.  I hold
the objects in a dictionary, mostly so that the owning program can refer
to them uniformly.

2. Whenever the collection is added to or deleted from, re-pack() the
owning object.

Will this happen automatically simply whenever I add or delete from the
object's __dict__, or do I need to add to __setattr__ and __delattr__ to
detect when the user has added or removed an object?

Thanks

Not Quite Related:

I'm doing a lot of displaying of (Label, Entry) pairs so I've another
subclass of Frame() that simplifies that action for me.  These are items
from a collection that's created at runtime where the labels aren't
known in advance.

Right now, I'm raising an exception if grid() or place() is called on
the object.  Eventually, what I'd like to do is flesh out the grid() and
place() methods so that the Label and Entry are placed in what I think
are sensible default positions reletive to the desired placement.

With grid(), how do I determine my current column and row in the case
when grid() is invoked without arguments?

With place() . .pretty much the same song.  I need to determine my
position so that I can Do The Right Thing (or at least the expected
thing).

Thanks




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


Re: is there a better way?

2006-02-11 Thread Charles Krug
On 2006-02-11, Alex Martelli [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 Problem:
 
 You have a list of unknown length, such as this: list =
 [X,X,X,O,O,O,O].  You want to extract all and only the X's.  You know
 the X's are all up front and you know that the item after the last X is
 an O, or that the list ends with an X.  There are never O's between
 X's.

 If the list is incredibly long, you should use a bisection approach.
 Standard module bisect in the Python library could help, but mostly as
 an _example_, since it unfortunately relies on normal alphabetic order,
 and alphabetically speaking X should come _after_ O, not _before_.


Isn't every call to list.index() an O(n) operation?  We certainly want
to avoid multiple calls there if we can.

What happens if your split occurs in the middle of your block of Xs?
Then the split before/after fails --the stated algorithm says, If the
split is an X, choose the front half, so perhaps the statement was
inprecise?

The only way you'll know if you have an X in a particular block is using
a linear search method, either in Python or with list.index()

If (as the OP seemed to state) we KNOW that there's only one block of
X's in the list:

1. Find the index of the first X
2. Find the index of the last X.
3. Delete the block we've found.

That relies on the underlying language, which means we're working in
Linear Time in C, more or less.

If we make no such guarantee, then I can do the operation in linear
Python Time by scanning the list once, finding each instance and
calling list.del() as I find each block, keeping track of my current
position so I don't have to start over again.

Judging from way the OP worded the question, I'd advise making something
that works and that you understand how it works.

After that, s/he can worry about whether or not its performance is
suboptimal.

How large must the list be before logarithmic Python algorithm is
faster than linear C algorithm?  I've never measured, but it may be a
question worth exploring if one has a huge pile of data to chew on--like
US Census or UN budget-sized.




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


Re: Is there any books such as 'effective python' or else about the performance?

2006-02-11 Thread Charles Krug
On 2006-02-11, Kenneth Xie [EMAIL PROTECTED] wrote:
 att, thx.

A lot of the ideas discussed in Effective C++ et al are things that
Python does for us already.  C++ works at a much lower layer of
abstraction and you need to deal explicitly with freestore for any
nontrivial class.

EC++ is mostly about how NOT to crash your programs.

The Python Cookbook is a more pythonic version of the same
idea--proven techniques appropriate to the abstraction level of the
language.

Design Patterns is worth reading as well, but be careful with it.  Many
of the ideas that are difficult to do in C++, Java, Smalltalk, etc
(singletons come to mind) are trivially easy to do in Python because of
how our object model works.

Bruce Eckle's Thinking in Python gives a pattern introduction
explaining how things work in Python that can give you a sprinboard to
adapting other patterns from C++ et al.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another try at Python's selfishness

2006-02-08 Thread Charles Krug
On 2006-02-08, Ben Wilson [EMAIL PROTECTED] wrote:
 But the point is, the current situation is not newbie-friendly (I can
 tell, I am a newbie)

 I will agree to that, as I consider myself still new. _But_, it's a
 stumbling stone only briefly. Get enough nagging error messages, and
 you learn and move on. I agree with the grandparent poster that it is a
 perfect self-documenting thing, as the use of 'self' is pretty obvious.
 For a language that one can learn in a short time, this is a tempest in
 a teacup.


This old C hound finds it much more sensible than C++ or Java, where the
self parameter (called this) is implicit rather than explicit and
you just sorta kinda hafta know it's there and the correct syntax to
use to reference it.

Then there's all the places where you need a Secret Decoder Ring--in
Java you have to define the equivalents of stdout and stdin as they
aren't provided.  In c++ you can't overload the  operator in your
class, you have to use a friend function and you have to return an
ostream--the Rule of Three for constructors, and just generally lots
of small knotty issues to bite beginners.

9 times out of 10, Python Just Works the first time and things do what
your mind says they should without having to learn a seventeen special
cases to everything.

IMO, YMMV, Not Valid in Vermont, Happy Fun Ball may accellerate to
dangerous speeds.  Do NOT taunt Happy Fun Ball.


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


Re: breadth first search

2006-02-08 Thread Charles Krug
On 2006-02-08, News [EMAIL PROTECTED] wrote:
 I am new in using Python

 Anyone know how to implement breadth first search using Python?  Can Python
 create list dynamically, I want to implement a program which will read data
 from a file and store each line into a list, is this possible?

 Please send mail to me at [EMAIL PROTECTED] or reply this mail

 Thanks a lot!


Yes.  List has methods that support a stack, in case you find it useful
in this context.

Yes.  List has methods that allow dynamic creation, such as might be
useful when implementing a stack, in case you find it useful in this
context.

And Yes.  File has methods that will populate a list from a file.
Examples are in the tutorials.

You're welcome.

You can find numerous examples of the breadth-first algorithm on the
web.  You can then take the individual steps and translate them into
Python.  You'll likely find one or two sticking points, but the
implementation is straightforward from pseudocode or from a GOOD
statement of the algorithm.

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


Re: Having Trouble with Scoping Rules

2006-01-31 Thread Charles Krug
On 2006-01-31, Fredrik Lundh [EMAIL PROTECTED] wrote:

 def ExpensiveObject():
 global _expensiveObject
 if _expensiveObject is None:
 _expensiveObject = A VERY Expensive object
 print CREATED VERY EXPENSIVE OBJECT
 return _expensiveObject

 if __name__ == __main__:
 print _expensiveObject
 print ExpensiveObject()
 print ExpensiveObject()
 print ExpensiveObject()

 which prints

 None
 CREATED VERY EXPENSIVE OBJECT
 A VERY Expensive object
 A VERY Expensive object
 A VERY Expensive object

 and works perfectly fine if you import it from another module:


Ah, I had another error that broke it on import.  Testing with a
dictionary showed me the creates another object error.  Using a string
constant for that was just a between the ears problem that would have
shown me the error much quicker.

Thanks.

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


Re: Costly object creation (was : Having Trouble with Scoping Rules)

2006-01-31 Thread Charles Krug
On 2006-01-31, bruno at modulix [EMAIL PROTECTED] wrote:
 See other answers in this thread for how to solve the UnboundLocalError
 problem.

 Now about your *real* problem - which is nothing new -, you may want to
 read about some known solutions:

 http://en.wikipedia.org/wiki/Singleton_pattern
 http://en.wikipedia.org/wiki/Proxy_pattern
 http://en.wikipedia.org/wiki/Lazy_initialization
 http://en.wikipedia.org/wiki/Memoization


No, I'm already quite certain that this is a Singleton--pythonically a
module-level instance of Something--I'm not thinking of it as lazy
instantiated 'cuz it's initialized at startup, but I suppose it
qualifies.

I considered using a Borg for this, but discarded the idea as too
complicated for my needs--this will be easier to implement . . . 

Which I suppose it would be if I knew how correctly to use the global
keyword.

I don't need to restrict the actual class to One Instance so much as I'd
prefer that to be the default behavior.  I can't think of a reason why
I'd want to subclass this, so I can't say whether it makes sense to
Borg/Highlander/Singleton it or not.  For now, I'll Do The Simplest
Thing That Might Work and figure YAGNI (or IAGNI, I guess).

Thanks for the help.

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


Having Trouble with Scoping Rules

2006-01-30 Thread Charles Krug
List:

I've a module that's not doing what I expect.  My guess is that I don't
quite understand the scoping rules the way I should.

I have an object that's costly to create.  My thought was to create it
at the module level like this:

# expensive Object Module

_expensiveObject = None
def ExpensiveObject():

if not(_expensiveObject):
_expensiveObject = A VERY Expensive object

return _expensiveObject

if __name__ == '__main__':
print _expensiveObject
print ExpensiveObject()

When I run this module, I get the expected None but then I get
UnboundLocalError from the function call when _expensiveObject is
accessed:

Traceback (most recent call last):
  File
C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py,
line 310, in RunScript
exec codeObject in __main__.__dict__
  File Expensive.py, line 13, in ?
print ExpensiveObject()
  File Expensive.py, line 6, in ExpensiveObject
if not(_expensiveObject):
UnboundLocalError: local variable '_expensiveObject' referenced before
assignment

I obviously missed some part of the scoping rules.

What's the correct way to do this?

Thanx


Charles

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


Re: Having Trouble with Scoping Rules

2006-01-30 Thread Charles Krug
On 2006-01-31, Farshid Lashkari [EMAIL PROTECTED] wrote:
 You need to declare _expensiveObject as global inside your function. 
 Whenever you assign something to a variable that resides in the global 
 scope inside a function, you need to declare it as global at the 
 beginning of the function. So your function should look like this

 def ExpensiveObject():
  global _expensiveObject
  if not(_expensiveObject):
  _expensiveObject = A VERY Expensive object

  return _expensiveObject

 The documentation will no doubtedly explain it better than I have


Not really.  If I'd understood the docs, I wouldn't need to ask here.

Okay, that works in the module where I define the function.  But if I
import the module:

# expensive Object User
import Expensive

print Expensive.ExpensiveObject()

I get the same exception.  That approach most likely isn't going to work
for me, as I need to be able to reuse the costly (to create) object.


Okay THIS seems to be working for me:

# expensive Object Module

_expensiveObject = None
def ExpensiveObject():
try:
retval = _expensiveObject
except UnboundLocalError:
_expensiveObject = A VERY Expensive object
retval = _expensiveObject

return retval

if __name__ == '__main__':
print _expensiveObject
print ExpensiveObject()



Which gives me:

 import Expensive
 a = Expensive.ExpensiveObject()
 b = Expensive.ExpensiveObject()
 a == b
True
 a is b
True
 

I'll try it with my actual class instance to verify.  Anyone see
anything I'm missing?


Thanx



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


Re: How do I tell if I'm running in the PyWin interpreter?

2006-01-28 Thread Charles Krug
On 2006-01-28, Peter Otten [EMAIL PROTECTED] wrote:
 Charles Krug wrote:

 Is there a way to detect that I'm running the the PyWin interpreter so
 that I can bypass its raw_input behavior?

 You could test

 if pywin_specific_module in sys.modules:
# use workaraound

 Or maybe you can get away with always using sys.stdin.readline() instead of
 raw_input()? Look into cmd.py for an example.

 Peter


cmd.py is the battery included I was thinking of last night.
Unfortunately it uses something that PyWin replaces.

However I did note that PyWin's version raises KeyboardInterrupt out of
its dialog box.

That's not ideal, but at least it gives me an idea what I need to trap
to exit.

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


Re: How do I tell if I'm running in the PyWin interpreter?

2006-01-28 Thread Charles Krug
On 2006-01-28, Charles Krug [EMAIL PROTECTED] wrote:
 On 2006-01-28, Peter Otten [EMAIL PROTECTED] wrote:
 Charles Krug wrote:

 Is there a way to detect that I'm running the the PyWin interpreter so
 that I can bypass its raw_input behavior?

 You could test

 if pywin_specific_module in sys.modules:
# use workaraound

 Or maybe you can get away with always using sys.stdin.readline() instead of
 raw_input()? Look into cmd.py for an example.

 Peter


 cmd.py is the battery included I was thinking of last night.
 Unfortunately it uses something that PyWin replaces.

 However I did note that PyWin's version raises KeyboardInterrupt out of
 its dialog box.

 That's not ideal, but at least it gives me an idea what I need to trap
 to exit.


Okay, I poked around a bit more and found the initialization code that
does this:

sys.modules['__builtin__'].raw_input=Win32RawInput

Which is the substituted function.

Is there a way to access the original function that I want to use, or do
I need to come up with some other way to do console input?

Thanks

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


How do I tell if I'm running in the PyWin interpreter?

2006-01-27 Thread Charles Krug
Here's the deal:

I've a dead-simple command-line program I'm using to test things that I
can't (for various reasons) test in the IDE.

Here's a do-nothing subset that shows the idea:

# insanely simply command interpreter
import Commands
import sys

myPrompt = '$ '

# Raw Input doesn't QUITE do what I want in Python Win.
while True:
try:
args = raw_input(myPrompt).strip().split()
except EOFError:
break

cmd = args[0]
print '%s' % cmd
print args

As the comment says, when I run this under Python Win, I get an (pretty
sure) Tkinter interface, not a command line, and I don't get my
EOFError when I expect to.

This is something I occasionally need in my Swiss Army Knife.  Not
often, but when I need something like this, I need something like THIS
pretty badly, and sometimes I need to run it under PyWin (and under
Linux, Unix, Solaris, and anything else you might name and a few things
I bet you couldn't).

Is there a way to detect that I'm running the the PyWin interpreter so
that I can bypass its raw_input behavior?

Is there a simpler way to do this?

I recall some sample code that did something very much like this (define
a small set of callbacks and execute them from a command-like interface)
but I can't seem to lay my hands on the example.

Thanx


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


Re: How do I tell if I'm running in the PyWin interpreter?

2006-01-27 Thread Charles Krug
On 2006-01-28, Steven D'Aprano [EMAIL PROTECTED] wrote:
 
 As the comment says, when I run this under Python Win, I get an (pretty
 sure) Tkinter interface, not a command line, and I don't get my
 EOFError when I expect to.

 When do you expect to get an EOFError? The only way I get an EOFError is
 if I explicitly hit Ctrl-D while raw_input is running. When do you expect
 to get it? Have you tried Ctrl-Z under Windows?


That's exactly how I use it everywhere else.  Type until you're done
then hit Ctrl-D

The problem is only when running under the PyWin IDE . . I'd been using
this for months under Idle and every place else I needed it.

The problem is that PyWin doesn't give you a raw command line with in
response to raw_input, but gives you a text entry box and a nice
OK-Cancel yada yada interface that silently eats my EOF.

I'd like to have a single tool I can use everywhere.  So far as I can
tell, that means I have to detect the PyWin IDE and handle it
separately on initialization so I get a real raw input and not the
redefined Tkinter version.
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to generate a list of the subclasses of C

2006-01-18 Thread Charles Krug
On 2006-01-16, Alex Martelli [EMAIL PROTECTED] wrote:
 Charles Krug [EMAIL PROTECTED] wrote:
...
 I'm trying to create a list of all of C's subclasses:

 There's a class method for that very purpose:

 class C(object): pass
 ... 
 class D(C): pass
 ... 
 class E(C): pass
 ... 
 C.__subclasses__()
 [class '__main__.D', class '__main__.E']
 


 Alex

Exactly what I was looking for, thanks.

It stuck in my brain that there was a way to do this, but I couldn't lay
my mouse on it.

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


Trying to generate a list of the subclasses of C

2006-01-16 Thread Charles Krug
List:

I have this:

# classC.py

class C(object): pass

class D(C): pass

class E(C): pass

def CSubclasses():
for name in dir(): pass

I'm trying to create a list of all of C's subclasses:

import classC

print C
aList = []
for name in dir(classC):
print name,
try:
if issubclass(classC.__dict__[name], C):
print classC.__dict__[name]
else:
print
except TypeError:
print

Which gives me this:

class '__main__.C'
C
CSubclasses
D
E
__builtins__
__doc__
__file__
__name__

However when I do this from the command line:

 issubclass(D,C)
True

but

 issubclass(classC.__dict__['D'], C)
False

So my approach is flawed.

The end result I'm after is an automatically generated dictionary
containing instaces of the subclasses keyed by the subclass names:

{'D':D(), 'E':E(), . . . }

I can see the information I need in the module's __dict__ and by using
the dir() method, but I'm not having much success extracting it.



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


Re: File Paramaterized Abstract Factory

2006-01-11 Thread Charles Krug
On 2006-01-11, Fredrik Lundh [EMAIL PROTECTED] wrote:
 Charles Krug wrote:

 What I'd like is to do something like this:

 factoryFile = sys.argv[1] # we assume that argv[1] implements a
   # correct ThingMaker interface.

 sys.argv[1] is a string, so I assume that you meant to say that
 the module named by argv[1] implements the correct interface.

Yes.


 Anywho, my problem is with the whole Miracle thing.

 I've tried using __import__:

 a = 'HerFactory'
 __import(a)

 Which returns:

 module 'HerFactory' from 'HerFactory.py'

 try:

 factoryObject = __import__(factoryFile)
 print dir(factoryObject)


Ah Ha!

That's what comes of working from memory instead of letting the computer
remember for me.

 factoryFile = 'ThingMaker'
 factoryObject = __import__(factoryFile)
 print dir(factoryObject)
['MakeOtherThing', 'MakeThing', '__builtins__', '__doc__', '__file__', 
'__name__']
 factoryObject.MakeThing()
I made a Thing!

Wonderful!  Exactly what I'm after, thanks.

 this should give you a list of the functions in the given module,
 or an ImportError if it doesn't exist.


Yes, that's what I was getting.  Should have pasted that as well, but I
was feeling grumpy and impatient.

Thanx


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


File Paramaterized Abstract Factory

2006-01-10 Thread Charles Krug
List:

I have an idea for an abstract factory that parameterizes the factory
with an input module.

The general ideom can be expressed thus:

class ThingFactory(object):
def CreateThisThing() : return ThisThing()
def CreateThatThing() : return ThatThing()
def CreateTheOtherThing() : return TheOtherThing()

With subclasses:

class HerThingFactory(ThingFactory):
def CreateThisThing() : return HerThisThing()
# etc

You'd invoke it in the usual way:

ThingClient(Factory=ThingFactory)

And Thing client would refer to:

ThingFactory.CreateThisThing()

Now, Pythonically speaking, as the envisioned application uses only one
ThingFactory at a time, I'd ordinarily want to just load a module
(HisFactory, HerFactory, KidsFactory, YadaYadaYadaFactory) that
implements the expected interface--in this case, supplying the CreateX
methods.

What I'd like is to do something like this:

factoryFile = sys.argv[1] # we assume that argv[1] implements a
  # correct ThingMaker interface.

# A Miracle Occurs Here

UseFactory(factoryObject)

Where UseFactory looks like:

def UseFactory(factoryObject):

factoryObject.CreateThisThing() # returns the kind of thing


Rather a lot of preamble to learn what I'm trying to do.

Anywho, my problem is with the whole Miracle thing.

I've tried using __import__:

a = 'HerFactory'
__import(a)

Which returns:

module 'HerFactory' from 'HerFactory.py'

But after that I can't access the members.

Clearly I'm missing a step or seventeen.

What's the best way to do this?

Thanks


Charles


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


Re: Python's Performance

2005-10-11 Thread Charles Krug
On Mon, 10 Oct 2005 11:21:18 -0700, Donn Cave [EMAIL PROTECTED]
wrote:
 Iron-
 Python).  is it still an interpreter if it generates machine code?
 
 Is what an interpreter?
 
 I am not very well acquainted with these technologies, but it sounds
 like variations on the implementation of an interpreter, with no
 really compelling distinction between them.  

An important point made by Tannenbaum is this:

Once you have a machine that executes instructions (what he called
hardware or Level Zero Machine, then you can create higher level
machines that execute code written in the language of that machine.

For the purposes of writing C, for example, we pretend that we have this
magical machine that runs C code, or more typically, the machine that
runs (insert OS name) C code.

When the Level n machine isn't fast enough, we go to the level n-1
machine . . . C or Java for us, or assembly, but the model remains valid
until the point where you bump up against the underlying logic gates.

While we don't talk about the Virtual Machine the way Java folks do,
Tannenbaum's model is still useful for thinking about such things.

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


Re: When someone from Britain speaks, Americans hear a British accent...

2005-10-11 Thread Charles Krug
On Mon, 10 Oct 2005 15:46:34 -0500, Terry Hancock
[EMAIL PROTECTED] wrote:
 On Saturday 08 October 2005 04:35 am, Steve Holden wrote:
 I must have been working at NASA at the time; they are well known for 
 embiggening prices.
 
 Not nearly as much as the DoD, from what I hear.
 
 Truthfully, I think those stories are bit exaggerated -- I think the
 real problem is somebody making a bad make/buy decision. They decide
 to make something that they could easily have bought at the hardware
 store.
 

Typically, it was a $30 hammer with $270 worth of paperwork attached.

The famous $10k Toilet Seat is actually a bit of an interesting tale.

The part in question is the toilet from a C5A transport . . not
something you can purchase at the local Home Depot.

Being an aircraft toilet, it's crammed into a tiny space and has to be
as light as possible and all the things you associate with aircraft
toilets.

When they were speccing the project, the airframe manufacturer included
some number of spare toilet seats in the bid, given the expected life of
the airframe.  Some faceless bureaucrat decided that they didn't NEED
any spare toilet seats and cancelled that line item.

Lo and Behold, they eventually needed spare toilet seats.  But because
of Another Good Regulation (tm) the tooling had been recycled.

Recreating the tooling to make the spares was, amortized over the number
ordered, around $10k/seat.  Compared to the tooling costs, subsequent
orders of the same seat are pretty much free . . . at least until some
bozo in Ring A decides to toss the tooling again.

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


Re: Creating a list of Mondays for a year

2005-09-19 Thread Charles Krug
On Mon, 19 Sep 2005 12:10:04 GMT, Chris [EMAIL PROTECTED] wrote:
 Thanks to everyone for your help!
 
 That fit the need perfectly.
 
 In article [EMAIL PROTECTED], 
 [EMAIL PROTECTED] says...
 Is there a way to make python create a list of Mondays for a given year?
 
 For example,
 
 mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
 '1/31/2005','2/7/2005',   ... ]
 

You can also calculate it using the Julian Day Number.  Google on
astronomical calculations, or read the introduction to one of the
Numerical Recipies. . . books for a discussion and algorithm.

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


Re: How to program efficient pattern searches in a list of float numbers?

2005-09-19 Thread Charles Krug
On 19 Sep 2005 00:02:34 -0700, malv [EMAIL PROTECTED] wrote:
 Simple case:
 In this list, how to find all occurences of intervals of n adjacent
 indexes having at least one list-member with a value between given
 limits.
 Visualizing the list as a two-dimensional curve, this is like
 horizontally dragging a given rectangle over the curve and finding the
 x coordinates where the curve passes through the rectangle.(Define such
 a x-index coordinate as the left corner of the rectangle.)
 
 More complicated case:
 Given a pair of rectangles spaced relatively to each other in a fixed
 manner. Drag this rectangle pair horizontally over the above
 two-dimensional curve and list the indexes of the occurences where the
 curve passes simultaneously through both rectangles.
 (Define such a x-index coordinate as the leftmost corner of the
 rectangle pair).
 
 These problems can be solved by programming a naive search advancing
 index by index. It seems obvious that due to the localized properties
 searched for, much more efficient searches should be possible.
 After having found the occurence-indexes for one particular rectangle
 set, how to find the pattern occurences after changing one or more
 rectangle parameters?
 

Make a picture of the problem.  From your description, I'm not certain
anything more complicated than a linear search is called for.

Is this the phrasing of the original problem presentation?  Seems to me
the word superimpose ought to occur here.

Another thought:  What is the end result you're after?  Often we start
thinking of a solution but lose sight of the end result.  Then another
solution will pop into our mind that makes it Oh So Simple.

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


Re: Having trouble importing against PP2E files

2005-07-09 Thread Charles Krug
On Fri, 08 Jul 2005 22:43:55 +0300, Elmo Mäntynen [EMAIL PROTECTED]
wrote:
 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1
 
 Import Error: no module named PP2E.launchmodes
 
 However if I copy launchmodes.py into my work directory, it imports
 successfully.
 
 Both Examples above and Examples\PP2E contain the __init__.py file.
 Are both Examples and PP2E packages? 

They appear to be, yes.

 In python if a folder is meant to represent a package it should iclude
 the above mentioned file __init__.py and by saying the above your
 suggesting that PP2E is a package inside the package Examples.

That appears to be the case, yes.

 If the above is correct, you should append the pythonpath with
 c:\Python24\ and refer to the wanted .py with Examples.PP2E.launchmodes.
 As such the import statement obviously should be from
 Examples.PP2E.launchmodes import PortableLauncher. If the above isn't
 the case and there is still something unclear about this, reply with a
 more detailed post about the situation.
 

The registry value is this:

C:\Python24\Lib;C:\Python24\DLLs;C:\Python24\Lib\lib-tk;
C:\Python24\Examples\PP2E

I'm not realy sure what other details are relavant.  I've installed from
the Windows .msi package, and appended the directory I want to
PythonPath in the registry, and that doesn't do what I need.

This is WinXP Pro

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


Having trouble importing against PP2E files

2005-07-08 Thread Charles Krug
List:

I'm trying to use the example files from Programming Python, 2nd Ed.

I've copied them into c:\Python24\Examples\PP2E.

Launching any of the examples programs by themselves seems to work
spiffily.

Using regedit, I appended c:\Python24\Examples\PP2E to Pythonpath

from the immediate window, executing the line:

from PP2E.launchmodes import PortableLauncher

Raises the exception:

Import Error: no module named PP2E.launchmodes

However if I copy launchmodes.py into my work directory, it imports
successfully.

Both Examples above and Examples\PP2E contain the __init__.py file.

Obviously, I'm missing a setup step here.

What magic do I need to perform to get Python to find modules in the
Examples heirarchy?

Is there any way to check from the immediate window where Python will
search for modules?  Pythonpath appears to be correct, but the file
isn't importing unless I copy it to the current directory.

Thanx


Charles

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


Re: Excellent Site for Developers

2005-06-25 Thread Charles Krug
On Sat, 25 Jun 2005 14:50:48 GMT, Brian [EMAIL PROTECTED] wrote:
 Do Re Mi chel La Si Do wrote:
 rather... super troll
 
 100% Agreed.
 
 Can anyone say, This looks like spam... Feels like spam... and is about 
 as useful here in the Python forums as spam -- therfore my conclusion is 
 that his VB message probably IS SPAM.  :-D
 
 Brian
 ---

H . . .tough call.

After all, Spam is nearly ALWAYS ON topic for a Python group.

*enjoys his spam bacon eggs spam spam and spam*

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


Re: Python choice of database

2005-06-21 Thread Charles Krug
On Mon, 20 Jun 2005 23:42:21 -0800, EP [EMAIL PROTECTED] wrote:
 Oren suggested:
 
 How about using the filesystem as a database? For the number of records
 you describe it may work surprisingly well. A bonus is that the
 database is easy to manage manually.
 
 I tried this for one application under the Windows OS and it worked fine...
 
 until my records (text - maybe 50KB average) unexpectedly blossomed
 into the 10,000-1,000,000 ranges.  If I or someone else (who
 innocently doesn't know better) opens up one of the directories with
 ~150,000 files in it, the machine's personality gets a little ugly (it
 seems buggy but is just very busy; no crashing).  Under 10,000 files
 per directory seems to work just fine, though.
 
 For less expansive (and more structured) data, cPickle is a favorite.
 

Related question:

What if I need to create/modify MS-Access or SQL Server dbs?

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


Re: Loop until condition is true

2005-06-21 Thread Charles Krug
On Tue, 21 Jun 2005 12:05:25 +0200, Magnus Lycka [EMAIL PROTECTED] wrote:
 Remi Villatel wrote:
 while True:
 some(code)
 if final_condition is True:
 break
 #
 #
 
 What I don't find so nice is to have to build an infinite loop only to 
 break it.
 
 This is a common Python idiom. I think you will get used to it.
 
 
 Is there a better recipe?
 
 final_condition = False
 while not final_condition:
  some(code)

To the OP, don't get hung up on the syntax we use to implement a loop.

I took my first programming class long enough ago that we had to do
things like this (roughly translating the FORTRAN IV I remember into
p-code)

To do:

while TrueCondition
(loop body)

we'd have to write:

TopOfLoop:
if not TrueConditional goto loopEnd
   (loop body)
   goto TopOfLoop

loopEnd:

We were even required to write our source twice:

The first pass was structured and had to be proven correct.

The second pass was translated into something our compiler supported.

We still called it a while loop even though the syntax was icky.

When C came out with its built in looping, it was an unbelievable
luxury.

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


Re: Is there something similar to ?: operator (C/C++) in Python?

2005-06-20 Thread Charles Krug
On Mon, 20 Jun 2005 06:36:42 GMT, Ron Adam [EMAIL PROTECTED] wrote:
 Ron Adam wrote:
 
 You might be able to use a dictionary of tuples.
 
 call_obj = {(type_obj1,0):obj1a,
 (type_obj1,0):obj1b,
 (type_boj2,1):obj2a,
 (type_obj2,1):obj2b,
 etc... }
 call_obj[(type_of_obj,order)]()
 
 
 Regards, Ron
 
 This won't work like I was thinking it would.
 
 But to get back to your is there a ? operator question...
 
 Try this.
 
 def foo():
 return foo
 
 def boo():
 return boo
 
 print (foo, boo)[10]()# prints boo
 print (foo, boo)[10]()# prints foo
 
 Regards,
 Ron

Another thought:

Often complicated conditional logic is a flag that we need to refactor.
An accounting package I built has an official list of approved vendors,
but allows users to provisionally add a new vendor, which is corrected
later.

The bulk of this system only understands, This document has-a vendor
with a vendor factory that returns the appropriate type of vendor.

All of the logic specific to the specific subclass is internal to the
subclasses themselves.

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


Couple functions I need, assuming they exist?

2005-06-20 Thread Charles Krug
List:

First, I'm reading that aString.split() is depreciated.  What's the
current best practice for this?

Or am I mistaking that:

myWords = split(aString, aChar) 

is depreciated but

myWords = aString.split(aChgar)

is not?

Second question, I've written a script that generates a LaTeX source
containing randomly generated arithmetic problems of various types.

The target of the problems (my daughter) would prefer that the thousands
be delimited.  Is there a string function that does this?

Thanks


Charles

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


Re: Couple functions I need, assuming they exist?

2005-06-20 Thread Charles Krug
On 20 Jun 2005 15:51:07 GMT, Duncan Booth [EMAIL PROTECTED] wrote:
 Peter Hansen wrote:
 
 The target of the problems (my daughter) would prefer that the thousands
 be delimited.  Is there a string function that does this?
 
 You refer to something like putting a comma between groups of three 
 digits, as in 1,000?  This is locale-specific, and there's a locale 
 module that should have what you need.
 
 import locale
 locale.setlocale(locale.LC_ALL, '')
 'English_United Kingdom.1252'
 print locale.format(%d, 100, True)
 1,000,000

Perfect!

Thanks.

Sometimes hard part is figuring out which package already does the
thing I need done.


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


Re: performance of Nested for loops

2005-05-20 Thread Charles Krug
On 20 May 2005 15:35:10 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED]
wrote:
 Is there a better way to code nested for loops as far as performance is
 concerned.
 
 what better way can we write to improve the speed.
 for example:
 N=1
 for i in range(N):
for j in range(N):
do_job1
for j in range(N):
do_job2
 

What do you see when you profile the code?  

Premature Optimization is the root of all manner of evil and all that
good stuff.

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


Re: How to detect a double's significant digits

2005-05-05 Thread Charles Krug
On 5 May 2005 10:37:00 -0700, mrstephengross [EMAIL PROTECTED] wrote:
 Hi all... How can I find out the number of significant digits (to the
 right of the decimal place, that is) in a double? At least, I *think*
 that's what I'm asking for. For instance:
 
 0.103 -- 3
 0.0103 -- 4
 0.00103 -- 5
 0.000103 -- 6
 0.103 -- 7
 
 Thanks in advance!
 --Steve ([EMAIL PROTECTED])
 

I would say that each of these examples has three signficant figures.
Each of them can be expressed as:

1.03e+n

For any integer n.

The fact that you've only shown the cases where n \in {-1, -2, -3, -4,
-5 . . } doesn't change the generality of the answer.

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


Has anyone built a file reader for NIST IHEad?

2005-05-04 Thread Charles Krug
List:

I'm playing with some image algorithms and one of the examples discusses
fingerprint comparison.  

The NIST has fingerprint sample files for download, in NIST IHead
format.  Has anyone built a reader for that format?

Thanks


Charles

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


Re: OOP

2005-04-28 Thread Charles Krug
On 28 Apr 2005 10:34:44 -0700, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
 Hey yall,
 I'm new to Python and I love it. Now I can get most of the topics
 covered with the Python tutorials I've read but the one thats just
 stumping me is Object Orientation. I can't get the grasp of it. Does
 anyone know of a good resource that could possibly put things in focus
 for me? Thanks.
 

Learning Python (Lutz/Ascher) has a good discussion of the basics.

Unfortunately, most of the OOP writings I've read fall into two
catagories:  Trivial examples where you say, But why Bother?? and
examples that you don't understand until you've some OO design under
your belt and can understand what it's all good for.

Objects are, at the end of the day, data and the accompanying methods.
Once you've read the various tutorials take a stab at converting a
problem you know well into objects.

You'll get it wrong at first.  Most everyone does.  Don't sweat it.
Eventually, you'll just get it.

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


Is there a better interactive plotter then pylab?

2005-04-27 Thread Charles Krug
List:

I'm trying to us pylab to see what I'm doing with some DSP algorithms,
in case my posts about convolution and ffts weren't giving it away.

I've been using pylab's plot function, but I'm finding it a bit
cumbersome.

It works, but if I switch from the interactive window to the plot window
and back, the plot window gets trashed.

Is there a better alternative for interactive use?

Thanks


Charles

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


Re: Is there a better interactive plotter then pylab?

2005-04-27 Thread Charles Krug
On Wed, 27 Apr 2005 20:56:07 -0500, John Hunter
[EMAIL PROTECTED] wrote:
 Charles == Charles Krug [EMAIL PROTECTED] writes:
 
Charles List: I'm trying to us pylab to see what I'm doing with
Charles some DSP algorithms, in case my posts about convolution
Charles and ffts weren't giving it away.
 
Charles I've been using pylab's plot function, but I'm finding it
Charles a bit cumbersome.
 
Charles It works, but if I switch from the interactive window to
Charles the plot window and back, the plot window gets trashed.
 
Charles Is there a better alternative for interactive use?
 
 You are probably not using pylab interactive mode properly.
 matplotlib has several GUI backends (gtk, tk, qt, etc...).  Most GUIs
 take control with their mainloop and prevent further interaction.
From what you describe, I'm pretty sure you haven't setup your
 configuration properly for interactive use.  Fortunately, there are a
 couple of solutions.
 
 For the standard python shell, you need to use the TkAgg backend.
 Tkinter is the only python GUI that plays nicely with the standard
 python shell.  You will need to set backend : TkAgg and 
 interactive : True in the matplotlib rc file.  See
 http://matplotlib.sf.net/interactive.html for details and
 http://matplotlib.sf.net/.matplotlibrc for information on the
 configuration file.
 

Those are already set.  I'm taking a look at ipython now.

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


Pythonic way to do static local variables?

2005-04-25 Thread Charles Krug
I've a function that needs to maintain an ordered sequence between
calls.

In C or C++, I'd declare the pointer (or collection object) static at
the function scope.

What's the Pythonic way to do this?

Is there a better solution than putting the sequence at module scope?

Thanks.



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


Re: Pythonic way to do static local variables?

2005-04-25 Thread Charles Krug
On Mon, 25 Apr 2005 21:30:18 -0500, Jaime Wyant
[EMAIL PROTECTED] wrote:
 Well, if you're a c++ programmer, 

Well, my forte is embedded systems and device controls . . .

 then you've probably ran into
 `functors' at one time or another.  You can emulate it by making a
 python object that is `callable'.
 
 class functor:
   def __init__(self):
   self.ordered_sequence = [1, 2, 3, 4, 5]
   def __call__(self, arg1, arg2):
   self.ordered_sequence.extend((arg1,arg2))
   self.ordered_sequence.sort()
   

ordered in this case doesn't mean sorted. . . .
8-)

It's the set of filter coefficients and cumulative remainders for an
overlap add convolution.  Sorting would be . . . bad.  Like crossing the
streams bad.

Both of these techniques look promising here.


Thanks


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


Is there a package with convolution and related methods?

2005-04-21 Thread Charles Krug
List:

Is there a Python package with Convolution and related methods?

I'm working on modeling some DSP processes in Python.  I've rolled one
up, but don't feel much like reinventing the wheel, especially if
there's already something like Insanely Efficient FFT for Python
already.

Thanks


Charles

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


How can I verify that a passed argument is an interible collection?

2005-04-21 Thread Charles Krug
List:

I'm working on some methods that operate on (mathematical) vectors as
in:

def Convolution(x, y)
Returns a list containing the convolution of vectors x and y

Is there any way to determine at runtime that x and y are iterible
collections?

Do I *coughs* simply *coughs* trap the exception created by:

for v in x:

when v is a scaler quantity?

Thanks


Charles

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


Re: Is there a package with convolution and related methods?

2005-04-21 Thread Charles Krug
On Thu, 21 Apr 2005 13:13:17 -0400, David M. Cooke
[EMAIL PROTECTED] wrote:
 Charles Krug [EMAIL PROTECTED] writes:
 
 List:

 Is there a Python package with Convolution and related methods?

 I'm working on modeling some DSP processes in Python.  I've rolled one
 up, but don't feel much like reinventing the wheel, especially if
 there's already something like Insanely Efficient FFT for Python
 already.

 Thanks
 
 You most certainly want to look at the numerical python packages
 Numeric and numarray (http://numeric.scipy.org/) for array
 manipulations, and scipy (http://scipy.org) has wraps for FFTW (Fast
 Fourier Transform in the West).
 

Great, thanks.

Figured someone had done it.

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