Re: [Tutor] Importing and creation on the fly

2007-06-28 Thread Tino Dai

On 6/26/07, Tino Dai <[EMAIL PROTECTED]> wrote:


On 6/26/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote:

> On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:
> > Hi there,
> >
> > I've been banging my head on this for about two weeks, and I can't
> > figure out a solution to this. I'm wondering if you could assist me on
> this
> > pesky problem.
> >
> > I'm reading in an xml file that has the name of class, location,
> and
> > the filename into a dictionary. I want to import these classes and
> create
> > instances of them.  The code in question is as follows:
> >
> > 36   for xmlKey in self.dictXML.keys():
> > 37 if not self.dictXML[xmlKey]['location'] in sys.path and
> \
> > 38 not self.dictXML[xmlKey]['location'] == os.getcwd():
> > 39 sys.path.append(self.dictXML[xmlKey]['location'])
> > 40 try:
> > 41 if os.stat(self.dictXML[xmlKey]['location'] + \
> > 42 self.dictXML[xmlKey]['filename']):
> > 43 eval('import ' + self.dictXML[xmlKey]["class"])
> > <-- syntax error here
> > 44 actionStmt=self.dictXML [xmlKey]["class"] + '.'
> +
> > self.dictXML[xmlKey]["class"] + '()' 45
> > 45  self.objList.append(eval(actionStmt))
> > 46 except:
> > 47 pass
> >
> >
> > I have also tried: __import__(self.dictXML[xmlKey]["class"]), which
> gave me
> > an error when I did the eval(actionStmt). Could anybody shed some
> light on
> > this? Thanks in advance.
>
> For the task of importing, look at the "imp" module:
>
> http://docs.python.org/lib/module-imp.html
>
> Also, the "inspect" module may be of help:
>
> http://docs.python.org/lib/module-inspect.html
>
> In particular, look at the the inspect.getmembers() and
> inspect.isclass() methods.
>
> Dave


 Thanks guys. I will look into that this afternoon

-Tino

Hi Everybody,


 This is the solution that I came up with. Using Dave's advice:

 fp, pathname, description = imp.find_module(self.dictXML
[xmlKey]["class"])
 tarMod = imp.load_module('apacheLimiter',fp,pathname,description)
 tarClass = getattr(tarMod,self.dictXML[xmlKey]["class"])
 tarObj=tarClass()
 if hasattr(tarObj,'do'):
   self.objList.append(tarObj)

  So, I used the find_module to find the particular module that I
wanted, returning the filename, path to the module, and the description (see
the docs.python.org for more info). Next, I loaded the module from the info
provided to me. After loading the module, I "extracted" the class that I
wanted using the getattr function. After I had the class loaded, I created
an instance from it.

I also checked out the exec function Kent. And one of my colleagues
recommended execfile. I felt like that was "hack" to get around a problem
(IHMO). Thanks for everything.

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


Re: [Tutor] Removing a file from a tar

2007-06-28 Thread Jacob S.
>* Adam A. Zajac <[EMAIL PROTECTED]> [2007-06-27 11:26]:
>> I was reading over the documentation for the tarfile module and it
>> occurred to me that there didn't seem to be a way to remove an
>> individual file from the tar.
>>
>> For example, suppose I did this:
>>
>> import tarfile
>> tar = tarfile.open("sample.tar", "w")
>> tar.add("unwanted")
>> tar.add("wanted")
>> tar.close()
>>
>> At this point, how could I come back and remove "unwanted" from the tar?
>
> Wel, it looks like tar's --remove-files is not supported yet, you would
> probably have to reopen the tarfile, write it to a new one file-by-file,
> excluding the ones you don't want.  Messy :-(

Disclaimer: I was not alive during the described era. This information may 
be off or just plain wrong. Perhaps someone who was alive then will correct 
any errors(?).

Remove is not supported because of the way tar s were designed. They were 
designed for tape drive backups, that is 'tar' Tape ARchive. Because they 
were designed as a backup scheme for tape drives, there was no need to have 
a remove function.

|  File1 | File2 | File3 |
Remove File2
| File1 |   | File3|

Moving file3 up would mean that the tape would have to rock back and forth, 
something that tapes just weren't supposed to do. Not moving file3 up would 
very nearly defeat the purpose of deleting file2 in the first place: to save 
space. (Which was important then - and should be now... tsk, tsk Microsoft)

Anyway, there was no need to have a remove function because people just did 
what Adam said - write a whole new tape. Now that tar is no longer widely 
used for tapes, there are other formats (like RAR) that *are* designed for 
modern storage equipment.

HTH,
Tiger12506 

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


[Tutor] Money matters

2007-06-28 Thread Terry
I am going to need to be handling money calculations and was wondering
about the float problem 
in my calculations.

Should I simply run the results of all calculations through something
like this:

from __future__ import division
...
...
s=(int(round(s, 2)*100))/100

Or should I be using Decimal on all money calculations?

Or, is there another more preferred approach?







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


Re: [Tutor] Money matters

2007-06-28 Thread Brian van den Broek
Terry said unto the world upon 06/28/2007 02:55 PM:
> I am going to need to be handling money calculations and was wondering
> about the float problem 
> in my calculations.
> 
> Should I simply run the results of all calculations through something
> like this:
> 
> from __future__ import division
> ...
> ...
> s=(int(round(s, 2)*100))/100
> 
> Or should I be using Decimal on all money calculations?
> 
> Or, is there another more preferred approach?


Hi Terry,

I'd say definitely use Decimal. Money calculations were a primary use 
case for the Decimal module. (Though a bit more needs to be done to 
get the right decimal precision. See 
.)

Best,

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


Re: [Tutor] Money matters

2007-06-28 Thread Tiger12506
> Should I simply run the results of all calculations through something
> like this:
>
> from __future__ import division
> ...
> ...
> s=(int(round(s, 2)*100))/100
>
> Or should I be using Decimal on all money calculations?

Firstly - that does not magically fix the imprecisions in floating point
numbers. If that would work, it would be hardcoded into the interpreter, no?

I think that Decimal is the way to go here, but you do have another option.
Whenever you put in a number, remove the decimal point and store it as an
integer. Do all of you calculations with integers. Every time you have to
display a total, convert it then (but don't overwrite the variable! Convert
a temp)

Obviously this is a tough way to go in an easy language like python. That is
a solution I am considering using C. (I might just make it too..) That's why
I encouraged Decimal.

If you're interested in the integer representation of floats, like this
particular efficiency & precision demon (me!), then you will have to work
out ways to multiply and divide using pseudo floats... Not too difficult.
Say you want to multiply an integer against say 5.5 %. Multiply the total by
ten, 55, then divide by a 100. In that order. Of course you will still have
problems. For example do it over and over and you will overflow your
integer, but nevermind. Am I rambling? Ooops.

HTH,
tiger12506

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


[Tutor] Iterating through two lists at the same time with manipulation..

2007-06-28 Thread Iyer
I have 2 lists:

List 1 has lists in it, such as

list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]

There is another list2 such as

list2 = [[1,'AA'],[3,'CC'], [4,'DD']]

For eg,

I wish to iterate over both the lists and produce the output 

a = [[1,'A'],[1,'AA']]
b = [[2,'B']]
c = [[3,'C'],[3,'CC']]
d = [[4,'D'],[4,'DD']]

Or [a,b,c,d] where a,b,c,d are defined above

What would be the best and quickest way to carry this out ? This list 
comprehension doesn't seem to get the desired result:

[[x,y] for x in list1 for y in list2 if x[0]==y[0]]

the sub-list [2,'B'] in list1 is left out. i.e, the output is 
[[[1, 'A'], [1, 'AA']], [[3, 'C'], [3, 'CC']], [[4, 'D'], [4, 'DD']]]


   
-
Moody friends. Drama queens. Your life? Nope! - their life, your story.
 Play Sims Stories at Yahoo! Games. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Money matters

2007-06-28 Thread Terry


Thanks! I am playing with Decimal suggested recipe now and it is just
way cool! Perfect!  In fact, more than perfect!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating through two lists at the same time with manipulation..

2007-06-28 Thread John Fouhy
On 29/06/07, Iyer <[EMAIL PROTECTED]> wrote:
> I have 2 lists:
>
> List 1 has lists in it, such as
>
> list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
>
> There is another list2 such as
>
> list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
>
> For eg,
>
> I wish to iterate over both the lists and produce the output
>
> a = [[1,'A'],[1,'AA']]
> b = [[2,'B']]
> c = [[3,'C'],[3,'CC']]
> d = [[4,'D'],[4,'DD']]

Your best choice is probably just to do it "by hand":

i1 = 0
i2 = 0
output = []
while True:
# compare list1[i] and list2[i2]
# produce appropriate output
# advance i1 or i2

Alternatively, you could shove the data into a sqlite in-memory
database, then use SQL to pull it back out.

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


Re: [Tutor] Iterating through two lists at the same time with manipulation..

2007-06-28 Thread Kent Johnson
Iyer wrote:
> I have 2 lists:
> 
> List 1 has lists in it, such as
> 
> list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
> 
> There is another list2 such as
> 
> list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
> 
> For eg,
> 
> I wish to iterate over both the lists and produce the output
> 
> a = [[1,'A'],[1,'AA']]
> b = [[2,'B']]
> c = [[3,'C'],[3,'CC']]
> d = [[4,'D'],[4,'DD']]
> 
> Or [a,b,c,d] where a,b,c,d are defined above
> 
> What would be the best and quickest way to carry this out ?

If you want the result ordered by  the first element, you can combine 
the two lists, sort the combined list, and use itertools.groupby() to 
collect the groups:

In [1]: list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
In [2]: list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
In [3]: import itertools, operator
In [4]: l=list1+list2
In [5]: l.sort()
In [7]: [ list(g) for k, g in itertools.groupby(l, 
key=operator.itemgetter(0))]
Out[7]:
[[[1, 'A'], [1, 'AA']],
  [[2, 'B']],
  [[3, 'C'], [3, 'CC']],
  [[4, 'D'], [4, 'DD']]]


If you don't care about order, you can use a dict to collect like items:
In [8]: d={}
In [9]: for i in l:
...: d.setdefault(i[0], []).append(i)
In [11]: d.values()
Out[11]:
[[[1, 'A'], [1, 'AA']],
  [[2, 'B']],
  [[3, 'C'], [3, 'CC']],
  [[4, 'D'], [4, 'DD']]]

These are in order but in general they will not be.

If you want to preserve the original order then I think you will have to 
  do it "by hand" as John suggests.

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


Re: [Tutor] Iterating through two lists at the same time withmanipulation..

2007-06-28 Thread David Heiser

A dictionary would work well here. Read each element of the lists into
the dictionary using the integer as the key with a list of strings as
the values.

{1: ['A', 'AA'], 2: ['B'], 3: ['C', 'CC'], 4: ['D', 'DD']}

Then output the contents in the required format.

This may take more lines of code than other solutions, but it's simple
and flexible. If you can guarantee that this is not a class assignment,
I will pass on the code. It starts like this:

list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
dictionary = {}
for i, j in list1+list2:


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of John Fouhy
Sent: Thursday, June 28, 2007 7:24 PM
To: Iyer
Cc: tutor@python.org
Subject: Re: [Tutor] Iterating through two lists at the same time
withmanipulation..

On 29/06/07, Iyer <[EMAIL PROTECTED]> wrote:
> I have 2 lists:
>
> List 1 has lists in it, such as
>
> list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
>
> There is another list2 such as
>
> list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
>
> For eg,
>
> I wish to iterate over both the lists and produce the output
>
> a = [[1,'A'],[1,'AA']]
> b = [[2,'B']]
> c = [[3,'C'],[3,'CC']]
> d = [[4,'D'],[4,'DD']]

Your best choice is probably just to do it "by hand":

i1 = 0
i2 = 0
output = []
while True:
# compare list1[i] and list2[i2]
# produce appropriate output
# advance i1 or i2

Alternatively, you could shove the data into a sqlite in-memory
database, then use SQL to pull it back out.

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


[Tutor] Python & cgi on win98--tinyweb problems, etc

2007-06-28 Thread Kirk Bailey
RE: TinyWeb running python in windows

PROBLEM: getting python scripts to execute.
SOLUTION: Insure the script ends in the name extension .py.

Windows associates all such files with the pythonw.exe interpreter program, 
and will use it to interpret them. IT IGNORES THE SHEBANG (the first line in 
a script which in the lovely world of un*x points to the interpreter 
program). Some servers have config files to tell the server what to use. 
TinyWeb does not, and relies on windows file associations to direct it to 
the proper interpreter.

Also note that it is possible for windows to conceal name extensions in some 
configurations, and also to create name extensions it does not display, 
resulting in some interesting hair pulling evenings chasing bugs.

Also note that tinyweb checks for the existence of a default page to use if 
none is specified in an incoming request. IF YOU CHANGE THE FILE NAME AFTER 
TINY LOADS IT WILL BARK LIKE A DOG. For instance, index.htm or index.html 
are equally acceptable. You had .htm. then you decided to change it to 
.html- and the server started woofing. It thinks the file index.htm still is 
there someplace and is looking for it! If you change the file name, restart 
the server.

I used tinyweb in supporting the development of windows wiki, and in all 
that crazy alpha stage flakiness, it NOT ONCE blew out. It is BULLETPROOF.

But it is it's own strange beast, and has it's own peculiarities.

-- 
Salute!
-Kirk Bailey
   Think
  +-+
  | BOX |
  +-+
   knihT

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


[Tutor] get pixel color form canvas , or draw text on image (Tkinter lib).

2007-06-28 Thread Yang
What I am trying to do is get the dot matrix
of chinese char, and then make  a font lib for embed system. 
I had thought of print the char on a tk canvas,
and get dot color one bye one, but I found no mathod to get 
data form canvas.
As for image, I can put and get dots on it, but I don't 
konw how to put a char on it!
So, any ideas?  or should I consider using something like
gtk!


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