[Tutor] What algorithm suits here

2011-07-13 Thread Amit Sethi
I have a list of dictionaries in this form.

{ message : xyz
  parent : 23
  id : 25
}
 or
{ message : abc
parent : None
id : 25
}

{ message : cde
parent : 28
id : 32
}

{ message : cde
parent : 23
id : 35
}

I want to make seperate the lists such that messages in same thread(
The parent message and its child messages } come together . What is
the best algorithm here



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


Re: [Tutor] What algorithm suits here

2011-07-13 Thread Steven D'Aprano

Amit Sethi wrote:

I have a list of dictionaries in this form.

{ message : xyz
  parent : 23
  id : 25
}
 or
{ message : abc
parent : None
id : 25
}

{ message : cde
parent : 28
id : 32
}

{ message : cde
parent : 23
id : 35
}

I want to make seperate the lists such that messages in same thread(


Separate the *lists* plural? Earlier, you said you have *one* list. 
Please explain what you mean, showing an example.




The parent message and its child messages } come together . What is
the best algorithm here



Define "best" -- easiest to write, simplest to understand, uses least 
amount of memory, something else?





--
Steven

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


Re: [Tutor] compare and arrange file

2011-07-13 Thread Edgar Almonte
On Tue, Jul 12, 2011 at 10:32 PM, Emile van Sebille  wrote:
> On 7/12/2011 4:01 PM Edgar Almonte said...
>>
>> On Tue, Jul 12, 2011 at 5:44 AM, Peter Otten<__pete...@web.de>  wrote:
>
> 
>>>
>>> import csv
>
> imports the comma separated values (csv) file handler utilities module
>
>>>
>>> def sortkey(row):
>>>    if float(row[1]):
>>>        return row[1], True
>>>    else:
>>>        return row[2], False
>
> ... sortkey defines a function that accepts a cvs.reader data row, and
> returns either row[1] and True or row[2] and False based on which of row[1]
> and row[2] has a non-zero value
>
>>>
>>> with open("infile.txt", "rb") as instream:
>>>    rows = sorted(csv.reader(instream, delimiter="|"), key=sortkey)
>
> rows becomes the sortkey sorted result of the lines of infile.txt
>
>>>
>>> with open("outfile.txt", "wb") as outstream:
>>>    csv.writer(outstream, delimiter="|").writerows(rows)
>
> ... and this writes those results to outfile.txt
>
> you might also try the shortened:
>
> from csv import reader,writer
>
> def sortkey(row): return max(row[1],row[2]),row[1]>row[2]
>
> writer(open("outfile.txt", "wb"), delimiter="|").writerows(
> sorted(reader(open("infile.txt", "rb"), delimiter="|"),key=sortkey))
>
>
> Emile
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

fist time i saw the statement "with" , is part of the module csv ? ,
that make a loop through the file ? is not the sortkey function
waiting for a paramenter ( the row ) ? i don't see how is get pass ,
the "key" is a parameter of sorted function ? , reader is a function
of csv module ? if "with..." is a loop  that go through the file is
the second with inside of that loop ? ( i dont see how the write of
the output file catch the line readed

Thanks again for the helping of my understand
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] GUI selection help

2011-07-13 Thread Shwinn Ricci
Hey all,

I am browsing through the large list of apps for creating GUIs from python
on http://wiki.python.org/moin/GuiProgramming but unfortunately don't know
which one is the best for my project, which involves mapping a point on a
2-Dimensional surface to a 3-Dimensional structure by having users move
their mouse over the 2-D surface to light up a respective point on the 3-D
surface. The GUI should also allow me to implement rotated camera angles for
the 3-D structure. Does the GUI I select matter at all? Any pointers would
be appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] compare and arrange file

2011-07-13 Thread Peter Otten
Edgar Almonte wrote:

> fist time i saw the statement "with" , is part of the module csv ? ,
> that make a loop through the file ? is not the sortkey function
> waiting for a paramenter ( the row ) ? i don't see how is get pass ,
> the "key" is a parameter of sorted function ? , reader is a function
> of csv module ? if "with..." is a loop  that go through the file is
> the second with inside of that loop ? ( i dont see how the write of
> the output file catch the line readed

with open(filename) as fileobj:
   do_something_with(fileobj)

is a shortcut for

fileobj = open(filename)
do_something_with(fileobj)
fileobj.close()

But it is not only shorter; it also guarantees that the file will be closed 
even if an error occurs while it is being processed.

In my code the file object is wrapped into a csv.reader.

for row in csv.reader(fileobj, delimiter="|"):
# do something with row

walks through the file one row at a time where the row is a list of the 
columns. To be able to sort these rows you have to read them all into 
memory. You typically do that with

rows_iter = csv.reader(fileobj, delimiter="|")
rows = list(rows_iter)

and can then sort the rows with

rows.sort()

Because this two-step process is so common there is a builtin sorted() that 
converts an iterable (the rows here) into a sorted list. Now consider the 
following infile:

$ cat infile.txt
a | 0.00| 1.11|
b | 0.00| 1.11|
X | 0.00| 88115.39|
X | 90453.29| 0.00|
X | 0.00| 90443.29|
c | 1.11| 0.00|
X | 88115.39| 0.00|
X | 0.00| 88335.39|
X | 90453.29| 0.00|
X | 88335.39| 0.00|
X | 90443.29| 0.00|
d | 1.11| 0.00|

If we read it and sort it we get the following:

>>> import csv
>>> with open("infile.txt") as fileobj:
... rows = sorted(csv.reader(fileobj, delimiter="|"))
...
>>> from pprint import pprint
>>> pprint(rows)
[['X ', ' 0.00', ' 88115.39', ''],
 ['X ', ' 0.00', ' 88335.39', ''],
 ['X ', ' 0.00', ' 90443.29', ''],
 ['X ', ' 88115.39', ' 0.00', ''],
 ['X ', ' 88335.39', ' 0.00', ''],
 ['X ', ' 90443.29', ' 0.00', ''],
 ['X ', ' 90453.29', ' 0.00', ''],
 ['X ', ' 90453.29', ' 0.00', ''],
 ['a ', ' 0.00', ' 1.11', ''],
 ['b ', ' 0.00', ' 1.11', ''],
 ['c ', ' 1.11', ' 0.00', ''],
 ['d ', ' 1.11', ' 0.00', '']]

Can you infer the sort order of the list of lists above? The rows are sorted 
by the first item in the list, then rows whose first item compares equal are 
sorted by the second and so on. This sort order is not something built into 
the sort() method, but rather the objects that are compared. sort() uses the 
usual operators like < and == internally. Now what would you do if you 
wanted to sort your data by the third column, say? Here the key parameter 
comes into play. You can use it to provide a function that takes an item in 
the list to be sorted and returns something that is used instead of the 
items to compare them to each other:

>> def extract_third_column(row):
... return row[2]
...
>>> rows.sort(key=extract_third_column)
>>> pprint(rows)
[['X ', ' 88115.39', ' 0.00', ''],
 ['X ', ' 88335.39', ' 0.00', ''],
 ['X ', ' 90443.29', ' 0.00', ''],
 ['X ', ' 90453.29', ' 0.00', ''],
 ['X ', ' 90453.29', ' 0.00', ''],
 ['c ', ' 1.11', ' 0.00', ''],
 ['d ', ' 1.11', ' 0.00', ''],
 ['a ', ' 0.00', ' 1.11', ''],
 ['b ', ' 0.00', ' 1.11', ''],
 ['X ', ' 0.00', ' 88115.39', ''],
 ['X ', ' 0.00', ' 88335.39', ''],
 ['X ', ' 0.00', ' 90443.29', '']]

The key function you actually need is a bit more sophisticated. You want 
rows with equal nonzero values to end close together, no matter whether the 
interesting value is in the second or third column. Let's try:

>>> def extract_nonzero_column(row):
... if row[1] == ' 0.00':
... return row[2]
... else:
... return row[1]
...

Here's a preview of the keys:

>

Re: [Tutor] GUI selection help

2011-07-13 Thread Wayne Werner
On Wed, Jul 13, 2011 at 9:41 AM, Shwinn Ricci  wrote:

> Hey all,
>
> I am browsing through the large list of apps for creating GUIs from python
> on http://wiki.python.org/moin/GuiProgramming but unfortunately don't know
> which one is the best for my project, which involves mapping a point on a
> 2-Dimensional surface to a 3-Dimensional structure by having users move
> their mouse over the 2-D surface to light up a respective point on the 3-D
> surface. The GUI should also allow me to implement rotated camera angles for
> the 3-D structure. Does the GUI I select matter at all? Any pointers would
> be appreciated.
>

Do you have any experience with 3d programming? If you've already familiar
with OpenGL, you can use the pyglet framework that gives you OpenGL
bindings.

Of course, if you already have a way to do the 3d part, then your GUI
framework really doesn't matter - Tkinter is probably the easiest one to
use, wxPython give you native-looking widgets (if you're using Windows, your
apps will look like other Windows apps), PyGTK+ is great if you plan to use
the Gnome window manager under Linux, and PyQT is good for KDE and contains
everything but the kitchen sink. Also you'll be using your left pinky /all/
the time because everything you use has "q" in it.

If all you need to do is display an image and track where the mouse
click/drags are happening, I'd probably use Tkinter.

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


[Tutor] Get file last user

2011-07-13 Thread Susana Iraiis Delgado Rodriguez
Hello list!!!

I want to get the last user who accessed to a file, I already have the way
to know who owns the file, but I really need to get this information.
To get file user I'm using: os.environ.get("USERNAME") and to get the
machine host: socket.gethostname()
Is there a way to find out who used the file for the last time?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get file last user

2011-07-13 Thread Steven D'Aprano

Susana Iraiis Delgado Rodriguez wrote:

Hello list!!!

I want to get the last user who accessed to a file, I already have the way
to know who owns the file, but I really need to get this information.
To get file user I'm using: os.environ.get("USERNAME") and to get the
machine host: socket.gethostname()
Is there a way to find out who used the file for the last time?



I don't believe so. As far as I know, that information simply isn't 
recorded anywhere.



--
Steven

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


[Tutor] copy and paste excel worksheet using win32

2011-07-13 Thread Pirritano, Matthew
Pythonistas,

I have a nicely formatted report in excel that is designed to be filled
in by an excel macro. But first I need to get the report worksheet into
52 separate excel workbooks.

Here's what I've tried so far. I'm snipping the code a bit. I have a wx
dialog that gets a directory where the excel files live that will have
the report worksheet added to them. The variable 'infile' is really a
directory, and comes from the results of the wx dialog. Then I step
through the directory and create a list of the files. Next I start excel
and open the file with the report, then I'm trying to copy that report
into each of the files in the directory.  Below is the error I get and
the syntax.

I'm also sure each time before I run it that there is no excel process
still running.

Any help is much appreciated! Thanks,
Matt

Here's the error:

Traceback (most recent call last):
  File "C:\Projects\Copy_Worksheets_20110713.py", line 50, in 
reportWs(2).Copy(None, wbWorksheets(1))
  File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line
172, in __call__
return
self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.def
aultDispatchName,None)
com_error: (-2147352573, 'Member not found.', None, None)


Here's the code:

fileList = ()
for infile in glob.iglob( os.path.join(infile, '*.xls') ):
print "current file is: " + infile
print ''

fileList = fileList + (infile,)   


excel = win32com.client.Dispatch("Excel.Application")
reportWb =
excel.Workbooks.open("D:\\Data\\Excel\\BHS_Report_Format_20110713.xls")
reportWs = reportWb.Worksheets(1)

for f in fileList:
print "processing file: " + f
wb = excel.Workbooks.Open(f)
wbWorksheets = wb.Worksheets(1)
wbWorksheets.Activate()
reportWs(2).Copy(None, wbWorksheets(1))
wb.Close(True)

reportWb.Close(True)
exce.Quit()






Matthew Pirritano, Ph.D.
Research Analyst IV
Medical Services Initiative (MSI)
Orange County Health Care Agency
(714) 568-5648
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Get file last user

2011-07-13 Thread Alan Gauld

Susana Iraiis Delgado Rodriguez wrote:

Hello list!!!

I want to get the last user who accessed to a file, ...
Is there a way to find out who used the file for the last time?


You don't say wgich OS you are using, which bis alklimportant.l Heavy 
duty industrial OS like OS.390,Pick and VAX VMS do record an audit 
trail, if the admin has switched that on. But mid range OS like Windows 
and Unix do not, so far as I know, record who made a change, just when 
the change was made. But that also probably depends on the filesystem in 
Linux, some of the more exotic ones may support audit trails.


If you really need to know who made changes you need to use a version 
control system like RCS, CVS, SVN, etc. Thats what they are designed to 
do, amongst other things...


HTH,

Alan G.
(From my Netbook because my PC broke! Boohoo... :-(

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


Re: [Tutor] Get file last user

2011-07-13 Thread Alan Gauld

Susana Iraiis Delgado Rodriguez wrote:

Hello list!!!

I want to get the last user who accessed to a file, ...
Is there a way to find out who used the file for the last time?


You don't say which OS you are using, which is allimportant. Heavy duty 
industrial OS like OS.390,Pick and VAX VMS do record an audit trail, if 
the admin has switched that on. But mid range OS like Windows and Unix 
do not, so far as I know, record who made a change, just when the change 
was made. But that also probably depends on the filesystem in Linux, 
some of the more exotic ones may support audit trails.


If you really need to know who made changes you need to use a version 
control system like RCS, CVS, SVN, etc. Thats what they are designed to 
do, amongst other things...


HTH,

Alan G.
(From my Netbook because my PC broke! Boohoo... :-(

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


Re: [Tutor] Get file last user

2011-07-13 Thread Alan Gauld

Susana Iraiis Delgado Rodriguez wrote:

Hello list!!!

I want to get the last user who accessed to a file, ...
Is there a way to find out who used the file for the last time?


You don't say wgich OS you are using, which bis alklimportant.l Heavy
duty industrial OS like OS.390,Pick and VAX VMS do record an audit
trail, if the admin has swirtchedv that on. But mid range OS like
Windows and Unix do not, so fgar as I know, record who made a change,
just when the change was made. But that also probably depends on the
filesystem in Linux, some of the more exotic ones may support audit trails.

If you really need to know who made changes you need to use a version
control system like RCS, CVS, SVN, etc. Thats what they are designed to
do, amongst other things...

HTH,




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


Re: [Tutor] Get file last user

2011-07-13 Thread Alan Gauld

Susana Iraiis Delgado Rodriguez wrote:

Hello list!!!

I want to get the last user who accessed to a file, ...
Is there a way to find out who used the file for the last time?


You don't say which OS you are using, which bis alklimportant.l Heavy
duty industrial OS like OS.390,Pick and VAX VMS do record an audit
trail, if the admin has swirtchedv that on. But mid range OS like
Windows and Unix do not, so fgar as I know, record who made a change,
just when the change was made. But that also probably depends on the
filesystem in Linux, some of the more exotic ones may support audit trails.

If you really need to know who made changes you need to use a version
control system like RCS, CVS, SVN, etc. Thats what they are designed to
do, amongst other things...

HTH,

Alan G


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