How would you go about creating a heap of shortcuts to a batch file?

2006-09-17 Thread googleboy
I need to create about 2000 shortcuts to a batch file.  The shortcuts
need to have:

Name: %parameter%.lnk

Target: n:\Path\To\batchfile.bat %parameter%
Start In: n:\Path\To\


Where the parameter is filled in by reading in a file with all the
shortcut names.


Extra bonus points for being able to apply the icon for an application
at n:\path\to\application\app.exe

At the moment I am looking like having to do this with VB,  but I am
trying hard to leave that behind for evermore.

TIA

googleboy

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


A question about searching with multiple strings

2005-10-21 Thread googleboy

Hi there.

I have defined a class called Item with several (about 30 I think)
different attributes (is that the right word in this context?).  An
abbreviated example of the code for this is:

class Item(object):

def __init__(self, height, length, function):
params = locals()
del params['self']
self.__dict__.update(params)
def __repr__(self):

all_items = self.__dict__.items()
return '%s,%s,%s' % (self.height, self.length, self.function)



I have a csv file that I use to store and retrieve all the info about
each Item, one item per line.

I have written a little peice of python that lets me search through all
Items (after reading them into a variable called all_items) and will
return matching results:



for item in all_items:

strItem = str(item)

m = re.search(p[i], strItem, flags = re.I)
if m:
height = getattr(item, height)
length = getattr(item, length)
function = getattr(item, function)
print height is %s, length is %s and function is %s % height,
length, function



This has the limitation of only working over a single search item.  I
want to be able to search over an uncontrollable number of search
strings because I will have people wanting to search over 2, 3 or even
(maybe) as many as 5 different things.

I was thinking that I would try to write a function that created a
sublist of Items if it matched and then run subsequent searches over
the subsequent search strings using this sublist.

I am not entirely sure how to store this subset of Items in such a way
that I can make searches over it.  I guess I have to initialize a
variable of type Item, which I can use to add matching Item's to,  but
I have no idea how to do that(If it was just a list I could say
sublist = [],  what do I use for self defined classes?  I Am also
usure how to go about creating a function that will accept any number
of parameters.

Any assistance with these two questions will be greatly appreciated!

Thanks!

googleboy

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


list.join()... re.join()...? Do they exist? (newbie questions...)

2005-10-01 Thread googleboy
Hi.

In some google posts I searched suggested that there was a list.join()
thing that I assume works like string.join [which I notice is now
deprecated in favour of S.join()]

It seems that I have been misled.

I start with a text file that I split up to run some formatting over
the various sections with re.split.  I want to join it back together
again with a | delimeter and write it out to a new file.  I looked for
a re.join, but it seems that doesn't exist.   I looked for a list.join
after reading the aforementioned posts, but it doesn't seem to exist
either.

To get it to work I did this:


List[0] = list0
List[1] = list1
List[2] = list2
List[3] = list3
cat_list = list0 + '|' + flatblurb + '|' + flatcontents + '|' + flates
+ '\n'
file.write(concat_list)


But it seems to me that there is probably something more pythonic than
having to go about it in such a laborious fashion

Would someone be so kind as to fill me in?

TIA!

googleboy

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


Newbie regular expression and whitespace question

2005-09-22 Thread googleboy
Hi.

I am trying to collapse an html table into a single line.  Basically,
anytime I seewith nothing but whitespace between them,  I'd
like to remove all the whitespace, including newlines. I've read the
how-to and I have tried a bunch of things,  but nothing seems to work
for me:

--

table = open(r'D:\path\to\tabletest.txt', 'rb')
strTable = table.read()

#Below find the different sort of things I have tried, one at a time:

strTable = strTable.replace(\s, ) #I got this from the module
docs

strTable = strTable.replace(., )

strTable = \s+.join(strTable)

strTable = \s.join(strTable)

print strTable

--

The table in question looks like this:

table width=80%  border=0
  tr
tdnbsp;/td
td colspan=2Introduction/td
tddiv align=right3/div/td
  /tr
  tr
tdnbsp;/td
  /tr
  tr
tdiONE/i/td
td colspan=2Childraising for Parrots/td
tddiv align=right11/div/td
  /tr
/table



For extra kudos (and I confess I have been so stuck on the above
problem I haven't put much thought into how to do this one) I'd like to
be able to measure the number of characters between the p  /p
tags, and then insert a newline character at the end of the next word
after an arbitrary number of characters.   I am reading in to a
script a bunch of paragraphs formatted for a webpage, but they're all
on one big long line and I would like to split them for readability.

TIA

Googleboy

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


Re: Newbie regular expression and whitespace question

2005-09-22 Thread googleboy
Thanks for the great positive responses.  I was close with what I was
trying,  I guess,  but close only counts in horseshoes and um..
something else that close counts in.

:-)

googleboy

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


Python CSV writer confusion.

2005-09-15 Thread googleboy
Hi.  I am trying to write out a csv file with | instead of comma,
because I have a field that may have many commas in it.  I read in a
csv file, sort it,  and want to write it out again.

I read the example that says:

import csv
writer = csv.writer(open(some.csv, wb))
writer.writerows(someiterable)

The someiterable is what is confusing me.



class Image(object):
def __init__(self, title, date, genre, data, value, filename):
params = locals()
del params['self']
self.__dict__.update(params)
def __repr__(self):
all_items = self.__dict__.items()
return '%s,%s,%s,%s,%s, %s' % (self.title, self.date,
self.genre, self.data, self.value, self.filename)

def read_images(filename):
csv_file = open(filename, rb)
reader = csv.reader(csv_file, dialect='excel', delimiter='|')
images = [Image(*[field.strip() for field in row]) for row in
reader]
csv_file.close()
return books

def sort_images(filename, *attr_names):
csv_file = open(filename, rb)
reader = csv.reader(csv_file, dialect='excel', delimiter='|')


if __name__ == '__main__':
images = read_images(rD:\path\to\imagespipe.csv)

def get_key(*attr_names):
def key(image):
return [getattr(image, name) for name in attr_names]
return key

images.sort(key = get_key(filename))

t = open(r'D:\path\to\filename_sort1.csv', 'w')

for image in images:
print book
#t.write('%s\n' % book) %Before I needed | delimited, this
worked
#csv.writer(t, dialect='excel', delimiter='|')
output = csv.writer(t, dialect='excel', delimiter='|')
output.writerows()
#output.writerows(image)
#output.writerow(image)

t.close()



This returns an error that says Error: sequence expected

My understanding of this is that I am creating a list of lists and I am
iterating over it (for image in images),  and that image is a list, and
is therefore iterable...?

I am a bit new at this, and would greatly appreciate any assistance.

TIA

googleboy

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


Confused newbie needs help with __init__() takes exactly 11 arguments (1 given)

2005-08-18 Thread googleboy
I've written a little script to parse a csv file then use seach/replace
over a template to create a file for each line in the file.  It pikes
out when I call the function that parses the csv (read_revs).  If I
have inadvertantly left an extra comma or two in the comma field, it
gives an error that says:

Traceback (most recent call last):
  File
C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py,
line 310, in RunScript
exec codeObject in __main__.__dict__
  File d:\dev\python\projects\books\revgen3.py, line 177, in ?
gen_html()
  File d:\dev\python\projects\books\revgen3.py, line 92, in gen_html
revlist =
read_revs(r'D:\library\work\websites\gobooks\cgi\reviews.csv')
  File d:\dev\python\projects\books\revgen3.py, line 38, in read_revs
reviews = [Review(*[field.strip() for field in row]) for row in
reader]
TypeError: __init__() takes exactly 11 arguments (13 given)

Which I understand totally, because I can see the two extra fields in
the csv.  Problem is, if I correct that problem it gives me confusing
output that is identical except for the final line which says:

TypeError: __init__() takes exactly 11 arguments (1 given)

I don't get how it goes from 13 arguments to 1!  I now have 10 fields
and can understand that it is passing self as the 11th argument.

Particularly confusing to me was that when I translated this from a
windows box running Python 2.4 to an OpenBSD box running python 2.3, it
just worked on the unix box.

Find below an the code and an example of the csv file in question.

TIA

googleboy




-
param1,param2,param3,param4,param5,param6,param7,param8,param9,comments
data1,data2,data3,data4,data5,data6,data7,data8,data9,Comments on the
item reviewed go here. Sometime, but not all the time, there might be
extra commas in this text.
data1,data2,data3,data4,data5,data6,data7,data8,data9,Comments on the
item reviewed go here.



--

import string, os, re, sys, operator, csv

class Review(object):
def __init__(self, param1, param2, param3, param4, param5, param6,
param7, param8, param9, comments):
params = locals()
del params['self']
self.__dict__.update(params)
def __repr__(self):
all_items = self.__dict__.items()
return '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % (self.param1,
self.param2, self.param3, self.param4, self.param5, self.param6,
self.param7, self.param8, self.param9, self.comments)


def read_revs(filename):
csv_file = open(filename, rb)
reader = csv.reader(csv_file)
reviews = [Review(*[field.strip() for field in row]) for row in
reader]
csv_file.close()
return reviews


def gen_revs():

revlist = read_revs(r'd:\dev\python\projects\books\reviews.csv')
revheader = revlist[0]
all_reviews = revlist[1:]

template = open(r'd:\dev\python\projects\books\template.txt', 'r')
sTemplate = template.read()

for review in all_reviews:
param1 = getattr(review, 'param1')
param2 = getattr(review, 'param2')
param3 = getattr(review, 'param3')
param4 = getattr(review, 'param4')
param5 = getattr(review, 'param5')
param6 = getattr(review, 'param6')
param7 = getattr(review, 'param7')
param9 = getattr(review, 'param8')
param9 = getattr(review, 'param9')
comments = getattr(review, 'comments')

output = template % (param1, param2, param3, param4, param5,
param6, param7, param8, param9, comments)

f=open(rd:\dev\python\projects\books\%s.html % param1, 'w')
f.write(output)
f.close()

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


Confused newbie needs help with __init__() takes exactly 11 arguments (1 given)

2005-08-18 Thread googleboy
Mostly I posted to try to find out more about the arguments being
passed to __init__,  and why they'd be 13 one second and 1 the next.

I also was hoping to get a little more background on what is happening
under the hood of the csv function I've plagiarised.  I haev read the
online python doco on it, but I am not really entirely sure in my own
head how it is breaking down that csv entry.

I am actually quite familiar with handling strings and lists in python,
though less so with dictionaries.

I did like your trick with telling splut to stop after 9 times.  I
hadn't seen that before.

Thanks for the response.

:-)

Googleboy

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


cgi form validation problems

2005-08-14 Thread googleboy
Hi.

I am writing up my own web form. I'm a bit of a newb, and read up many
different how-tos and examples and documentaion.  I finally had it
working just great until I decided that I wanted to add some extra
logic because there's one form that submits a particular type of
information.  a little extra validation, and the creation of a list of
the values to be mailed in to the site manager.

The code below is where I am going wrong (edited for brevity):


form=cgi.FieldStorage()

rev_fields = { param1:None, param3:None, param6:None,
param5:None, param8:None, param9:None, param10:None,
param11:None }


# Everything worked until I added the following if statement:

if form.has_key(param8):   #  Only one form has this
param8 = form.getvalue(param8)
if param8 == 0: # 0 is the default value
print Content-type: text/html
debug(You must give the item a rating)
for field in form.keys():
value = form[field].value
if rev_fields.has_key(field):
rev_fields[field] = value
for key in rev_fields:
if rev_fields[key] == None:
print Content-type: text/html
debug(All fields must be filled in. Please check your %s
submission. % key)
else:
#feedback = (%s, %s, %s, %s, %s, %s, %s, %s %
(form[param1].value, form[param3].value, form[param6].
value, form[param5].value, form[param8].value,
form[param9].value, form[param10].value, form[param11].value)
#feedback = (%s, %s, %s, %s, %s, %s, %s, %s %
(form.getvalue(param1), form.getvalue(param3), form.getvalue(
param6), form.getvalue(param5), form.getvalue(param8),
form.getvalue(param9), form.getvalue(param10),
form.getvalue(param11))
feedback = (%s, %s, %s, %s, %s, %s %
(rev_fields[param1], rev_fields[param3], rev_fields[param6],
rev_fields[param5], rev_fields[param8], rev_fields[param9],
rev_fields[param10], rev_fields[param11])


#feedback = form[ score ].value

msg = (From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n Feedback: %s\r\n\r\n
%
(rev_fields[param2], rev_fields[param7], rev_fields[param3],
rev_fields[param6]))


If I comment out the 'else:' logic, it works great.  But then I don't
get a list called feedback containing all teh bits I want,  The error I
get is really strange, too:

[Mon Aug 15 05:54:58 2005] [error] [client 60.224.106.116] Premature
end of script headers: /var/www/users/senta/html/gobooks/cgi/form.py
  File /var/www/users/senta/html/gobooks/cgi/form.py, line 99
msg = (From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n Feedback:
%s\r\n\r\n %
  ^
SyntaxError: invalid syntax

Just a simple assignation  I did think it might have been an
indentation error, but I changed that around and got a message telling
me about an indentation problem, which this doesn't do.

I have tried several different ways to assign the values, as you can
see by the commented out lines.  Tried getting the values directly from
teh form, and also from the validated rev_fields dictionary.  I'd be
extremely grateful to anyone who helps me through this.

TIA

Googleboy

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


Re: Parsing a log file

2005-08-14 Thread googleboy
I am similarly not a programmer but am trying to learn python to do
tasks like this.   I would read through the regular expressions
tutorial.  You could probably easily read in all teh lines of the log
file,  and then split them up by   (spaces)..

If you're right about the lines all being consistent, that should
easily handle each line.

From there you could amost certainly drop off the trailling
milliseconds on the timestamps and do the simple data manipulation
you'd like.

here are a couple of links:

http://www.amk.ca/python/howto/regex/
http://gnosis.cx/publish/programming/regular_expressions.html

HTH

googleboy

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


Some newbie cgi form questions...

2005-08-07 Thread googleboy
Hi there.

I am having a bit of a play with teh cgi module, trying to figure out
how to process a web form.

I have come a little ways from reading up variou sexamples and tutes
that I've found on the net, but they all leave me with a couple of
questions:

My code below has an attempt at validation that doesn't seem to do
anything.  I am expecting that it will report an error if all the
fields haven't been filled in.  Instead, it seems to just ignore an
empty field and carry on.

The second for loop is an attempt at trying to print all the values
that were entered on the for without presenting the hidden values.  I'd
really like to do this, but I can't seem to figure out how to make a
special case for hidden form values, nor can I find an example of how
to do it in any of the many python cgi pages I've visited so far.  If I
removed the if not type=hidden and fix the indentation this works
pretty much as I expect.


TIA

Googleboy



print 'Content-type: text/html\n\n'
print htmlheader

for key in form.keys():
if not form.has_key(key):
print h1 ERROR! h1BR
print Please go back and fill in all fields.  All fields are
required.br
break

print You entered the following:br 
for key in form.keys():
if not type=hidden:
print form[key].value + 'brbr'
print 'br' + timestamp
print 'brbr'
print '/body/html'

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


Re: Some newbie cgi form questions...

2005-08-07 Thread googleboy
for key in form.keys():Yes,  I know which fields are hidden because I
made them that way,  but  I am trying to figure out a way I can iterate
over the fields and do one thing with hidden fields (assign them to
variables that tell the form how to process) and a different thing with
non hidden fields (present them back to the browser with after
emailling them on to the form owner).  I want this script to work fro
several forms on the same webserver, some of which will have several
different and individual names.  I am also hopeful that I may be able
to extrapolate from that ways to differentiate other things from my
form input things, but we'll see how it goes.

My html code looks like this:

  h1Send Feedback/h1

  form action=cgi/formparse.py method=get
input name=subject type=text size=32 maxlength=196Subjectbr
input name=name type=text maxlength=64Your namebr
input name=email1 type=text size=24 maxlength=128Your email
address. br
brbrPlease let me know your thoughts below.brtextarea
name=thoughts cols=64 rows=12/textareabr
input  type=submit value=Submit
input type=hidden name=recipient value=[EMAIL PROTECTED]
input type=hidden name=email value=[EMAIL PROTECTED]
input type=hidden name=mailserver value=localhost
input name=Clear type=button value=Clear/form


In other news, I have tried suggested syntaxes aboce (thanks guys!) but
I get strange errors.  If the following:

for key in form.keys():
if not form.keys():
print h1 ERROR! h1BR
print Please go back and fill in all fields.  All fields are
required.br

I get a traceback in teh error_log file that looks like this:

Traceback (most recent call last):
  File cgi/form.py, line 34, in ?
for key, value in form.items():
  File /usr/local/lib/python2.3/cgi.py, line 533, in __getattr__
raise AttributeError, name
AttributeError: items

if this:

for key in form.keys():
if not form.keys()[key]:
print h1 ERROR! h1BR
print Please go back and fill in all fields.  All fields are
required.br
print '/body/html'

the I get this traceback:

Traceback (most recent call last):
  File /var/www/users/senta/html/gobooks/cgi/form.py, line 35, in ?
if not form.keys()[key]:
TypeError: list indices must be integers

As you can see,  I am using python 2.3 (my web service provider is
responsible for this - I'd use 2.4.1 if I could)

TIA

googleboy

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


Re: Some newbie cgi form questions...

2005-08-07 Thread googleboy

Robert Kern wrote:
 Diez B.Roggisch wrote:
 Traceback (most recent call last):
   File /var/www/users/senta/html/gobooks/cgi/form.py, line 35, in ?
 if not form.keys()[key]:
 TypeError: list indices must be integers
 
 As you can see,  I am using python 2.3 (my web service provider is
 responsible for this - I'd use 2.4.1 if I could)
 
  That code above can't work - you want something like
 
  if not form.keys() in key:

 This thread has to rank somewhere in the top ten threads with respect to
 the density of obviously wrong code samples.

LoL!

Yeah, I am guessing I started out not too far from something that was
doing what I wanted until now, I have gradually moved further and
further away from stuff that looks to me like it should work as python
code.   I've even managed to turn code that was working into code that
is broken somehow...

At the moment this bit of code is about validating that stuff was
entered into the form properly, and about doing something with some
types of form inputs and not others.  I try all sorts of things and
they just give me exception errors.

And I just don't know what use to make of advice that says stuff like
The form is not a dictionary.  :(

I have spent the whole weekend doing searches and reading tutorials on
this before I posted,  I have a form that I get working and doing most
of what I want.  It is just the last bits that I can't figure out, and
the tutes don't seem to cover the stuff I am trying to do.

O well,  I guess I will go bang my head against it or a few more days
and then hopefully an answer will show up in my brain somewhere.

Thanks for your help.

Googleboy.

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


Re: more newbie list questions

2005-07-14 Thread googleboy

Ali wrote:
 It's not really clear what you mean?

Ah.  sorry.  Let me try again.

I start with a csv that looks something like this:

title, author1, author2, publisher, code, ISBN
frogs of canada, andy humber, , springer press, foc2, 111-20345-556
newts of the UK, nigel snodgrass, sarah strauss, cambridge press,
nsss23,32-443567-130
amphibian environments, nigel snodgrass, , cambridge press, nsxx11,
32443567-120

First problem:

I have a seed file which I have placed a set of easily searchable
keywords (%author1%, %publisher% etc.) in.  I want to be able to
replace these keywords with the relevent values from each book, in
order to create an individual text file for each book called code.txt
(where the word code is replaced by the code of each book)

I've read the tutorial and (I think I)know how to use regular
expressions to make the substitions, but I can't figure out how to get
to the individual values of each book.

I have been trying the following:

++
import string, os, re, sys, operator, csv

class Book(object):
def __init__(self, title, author1, author2, publisher, code,ISBN):
params = locals()
del params['self']
self.__dict__.update(params)
def __repr__(self):
all_items = self.__dict__.items()
return str(all_items)

def read_books(filename):
reader = csv.reader(csv_file)
books = [Book(*[field.strip() for field in row]) for row in reader]
csv_file.close()
return books

booklist = read_books(c:\path\to\books.csv)
header = booklist[0]
all_books = booklist[1:]

Template = open(r'c:\path\to\cell.txt', 'r')
sTemplate = Template.read()


author1 = getattr(all_books, 'author1')  # trying to assign the author
value to the variable author1.


sAuth = re.sub('%author%', author1, sTemplate) # replace %author1% with
the value in author1 in the variable sTemplate)

++
The getattr causes an error that says AttributeError: 'list' object
has no attribute 'author1' so I think I am reading the operator
library page on the python site wrong somehow.

once this works,  I move onto the second problem, which is to write out
the string after the substitions to a new file called e.g. foc2.txt
(for the frogs of canada book).

I really am not sure how to go about this.  I was thinking of something
like concatenating three strings together

path = r/'c:\path\to
code = getattr(code, all_books)
extension = '.txt/''
filename = path + code + extension

f = open(filename, 'w')

however I have serious doubts that doing that will work.

I guess what I am needing, if it doesn't, is some sort of method of
substituting a variable's value into the following:

f = open(r'c:\path\to\%subs%.txt, 'w')


The third thing I am hoping to acheive is to append an extra field to
each book called 'filename' which will simply be a concatenation of the
'code' field + '.txt'.   Further thought suggests that I should create
the field first, even if it is empty, so that the books still all
conform to the Book class.  Then it simply becomes a question of how to
I change the value of a field in these lists.  I am sure that the
answer to that was something I found in the tute, or it will be in my
deitel python book somewhere...  The hard bit is part one (assigning
the value of a field to a variable)

Thanks for your help.

Googleboy

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


Re: more newbie list questions

2005-07-14 Thread googleboy
Thanks for this.  It ahs been very helpful.

I realised that my problem using getattr were because I was trying to
use it over the list of lists, instead of each book.  I have written up
a for loop now, and I think I am a lot closer:

for book in all_books:
author1 = getattr(book, 'author1')
sTemplate.replace('author1', author1)
found = sTemplate.find('%author1%')
print '\n.\n.\n'
print author1
print '\n'
print found
print sTemplate


You will notice that I print out various things. I am having some
troubles.

The resulting output of this loop is as follows:

andy humber

47
The title is %title%.  brbr

The author is %author1% %author2% brbr

The Publisher is %publisher1% %publisher2% brbr

The ISBN is %ISBN% brbr

This is obviously correctly assigning the author's name to the author1
variable, which I was my problem.

It is finding my search string at teh 47th character of the sTemplate
string, which is great.

But it doesn't seem to want to replace the search string with the value
of the author1 variable.

Is there something else I am missing here?

Thanks for the advice.

regards,

googleboy

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


more newbie list questions

2005-07-13 Thread googleboy
Hi there.

I am doing a bunch of processing over a list of lists,  and am
interested in doing several things taht don't seem to be working for me
just at the moment.

I have a list of books with several fields (Title, Author1, Author2,
Publisher, ISBN) in a csv.

I have a cell.txt file that looks like this:

++
The title is %title%.  brbr
The author is %author1% %author2% brbr
The Publisher is %publisher1% %publisher2% brbr
The ISBN is %ISBN% brbr
++

I know how to do something like sAuth = re.sub('%author1%', author1,
sTemplate), but I can't figure out to populate variables within python
from the list of fields.

The second thing I am trying to figure out is how to add a field (or
populate an empty field) called filename with the value of ISBN +
'.txt', though I hope this becomes pretty self evident after someone
helps me with the above.

The last thing that is vexing me at the moment is something I am not
sure if it is possible.  Once I have populated this filename field, I
will want to try to do something like:

for book in all_books
open(r'd:\path\to\files\%filename%', 'a')
some_function


Where I replace %filename% with the value of the filename field for
each book.  Is it possible to do some sort of search and replace like
that over a command within python?

Or is there another way entirely different to accomplish such a task,
like maybe assigning the path/filename to a variable... ?

TIA!

Googleboy

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


Re: what is the best way to determine system OS?

2005-05-15 Thread googleboy
I did somethign that really seems far brighter...  approaching it from
a slightly different angle I just search through each line for the
right field title,  and then take that field's value from teh last line
of output.

Doesn't matter what OS, doesn't matter what format now.  It can cope
with it all.

:-)

Thanks for the advice!  I learned a bunch.

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


Re: how can I sort a bunch of lists over multiple fields?

2005-04-28 Thread googleboy
I'd be just such a newbie; I don't understand why it would matter if I
left the book instance referencing itself

However these wonderful responses have gotten me a very long way
towards my goal.  I just have a couple of quick questions.

firstly,  I am trying hard to figure out how to create a new file with
the list rather than print to standard out.  I haev done this:

for book in books:
print book # just to be sure it works as I expect
sort1 = open(r'D:\path to\sort1.csv', 'w+')
print  sort1, book
sort1.close()

and this creates the file as I expect, however it creates it populated
with only the information of the final book in the sorted list. I am
guessing I need to figure out how to append as part of this loop,  but
the only info I have found so far suggests this should append by
default?

Secondly,  I am wondering how I can get a search algorithm that will
search by multiple fields here,  so that I can (as one example) sort
the books out by author and then date,  to present a list of the book
grouped by authors and having each group presented in a chronological
order,   or by author and title, grouping all the books up into authors
presenting each group alphabetically by title.  Or by publisher and
date,  or by publisher and code

I have tried things like

books.sort(key = operator.attrgetter(author), key =
operator.attrgetter(title) and
books.sort(key = operator.attrgetter(author, title)

but they both give errors.

Is this where using cmd functions instead of keys becomes necessary?

Thanks!

googleboy

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


how can I sort a bunch of lists over multiple fields?

2005-04-27 Thread googleboy
I didn't think this would be as difficult as it now seems to me.

I am reading in a csv file that documents a bunch of different info on
about 200 books, such as title, author, publisher, isbn, date and
several other bits of info too.

I can do a simple sort over the first field (title as it turns out),
and that is fine as far as it gets:


import string

bookData = open(r'D:\path\to\books.csv', 'r')
sBookData = bookData.read()
lBooks = string.split(sBookData, '\n')

lBooks.sort()
sorted = string.join(lBooks, '\n')
output = open(r'D:\path\to\output.csv', 'w')
output.close()


I really want to be able to sort the list of books based on other
criterium, and even multiple criteria (such as by author, and then by
date.)

I am using python 2.4 and have found this site:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305304

and so I tried doing things like

lBooks.sort(cmp=cmp5)
Traceback (most recent call last):
  File interactive input, line 1, in ?
NameError: name 'cmp5' is not defined

(I was hoping that cmp5 meant it would use the 5th item in the lists to
sort across)

 lBooks.sort(key=lambda i:i[4])
Traceback (most recent call last):
  File interactive input, line 1, in ?
  File interactive input, line 1, in lambda
IndexError: string index out of range


(I was hoping for similar things)


would you be so kind as to point me in the right direction?

THanks!

googleboy

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


what is the best way to determine system OS?

2005-04-25 Thread googleboy
Hi there.

I am writing a little app tha tI would like to make cross-platform
(debian, RH, Fedora, Solaris, AIX, etc)

Originally I decided to check what uname returned, as I didn't think it
mattered beyond the detail of Linux or SunOS etc.

Recently I have learned that FC3 breaks my script,  so I need to be
able to determine not simply Linux but to know exactly what unix the
script is being run on.

What is the best way to determine this?

Thanks!

googleboy

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


Re: what is the best way to determine system OS?

2005-04-25 Thread googleboy
Thanks for the edifying responses.  I will try out these things and
figure out if I have enough to solve my problem.

What this script does is use take the output of vmstat to report idle
cpu cycles (or memory stuff, etc.) over a period specified by a
parameter,  all the better to be a plugin for a monitoring service.

Basically it parses the output of the command,  runs through a whole
bunch of regex to discard everything not required and then reports the
info.  Everything I had checked out that responded Linux to a uname
command had identical output for vmstat until FC3.   FC3 seems to use
similar output to BSD.   I wrote a simple if-then loop to try to
determine which regex function to use, but it obivously isn't working
under fc3.

I could post the script if you'd like to look at it, but I was a little
bit afraid of looking like a total newbie.  I don't get an excuse to
write code very often so it is probably quite lame.

B)


I will have a look at platform and see what I can come up with.
Delving into libraries seems a little bit much for my current level
(though lots of fun).

Thanks for the responses!

googleboy

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