Re: [Tutor] keeping track of the present line in a file

2010-01-21 Thread Alan Gauld


"sudhir prasad"  wrote 


is there any other way to keep track of line number in a file other than
fileinput.filelineno()


You could always do it manually by incrementing a counter every 
time you read a line. But what's the problem with filelineno()?


If you tell us what your problem is we might be able to offer 
a suggestion.



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Still searching in html files

2010-01-21 Thread Paul Melvin
Hi,

 

I am still looking for information in these files and have a sort of
'clunky' solution that I would like feedback on please.

 

The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like
some advice on is how to get the sorted data and where to put it.

 

Currently I use a temporary list to store the data before writing it to
another, is this OK? (lines 45 onwards)

 

I want to expand the program to give some output which the user can interact
with, e.g. 'which of this information do you want me to get?' and the user
would choose.  Does anyone have any tips on this? Can I just use things like
raw_input and lists?

 

Thanks

 

paul

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


Re: [Tutor] Still searching in html files

2010-01-21 Thread Stefan Behnel

Paul Melvin, 21.01.2010 10:03:
> I am still looking for information in these files and have a sort of
> 'clunky' solution that I would like feedback on please.
> 
> The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like
> some advice on is how to get the sorted data and where to put it.

Looks like you already didn't take the good advice to use an HTML parser.


> I want to expand the program to give some output which the user can interact
> with, e.g. 'which of this information do you want me to get?' and the user
> would choose.  Does anyone have any tips on this? Can I just use things like
> raw_input and lists?

I'd advise against that, although this obviously depends on the type of
users you expect. Likely, you'd rather want a) a command line interface so
that you can also use it from other scripts (look at the optparse module
for that), and b) a suitable module API so that you can use the code from
other Python modules. Try to make your code reusable by splitting it into
separate functions and classes, and move it out of the top module level.

Stefan

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


[Tutor] Need help, tutoring Python

2010-01-21 Thread Neal Mendelsohn
I am looking for someone who knows WordPress to spend an hour with me on the
phone and with a shared screen to coach me on how WordPress can be
configured using Python files.

Can you refer me?

Thanks,

NM


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


[Tutor] combinatorics problem: assembling array

2010-01-21 Thread markus kossner

Dear Pythonics,
I have a rather algorithmic problem that obviously made a knot in my brain:

Assume we have to build up all the arrays that are possible if we have a 
nested array
containing an array of integers that are allowed for each single 
position. For example

the nested array
(
(1,2,3,89),
(3,5,8),
(19,30,7,100,210,1,44)
)
would define all the arrays of length  3  that can be  enumerated  using 
the

array of  possible numbers  for each position.
Beforehand I will have no Idea of the length of the arrays. How do I 
enumerate all the possible

arrays with python?

Thanks for getting the knot out of my brain,
Markus  
___

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


Re: [Tutor] keeping track of the present line in a file

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 1:57 AM, sudhir prasad  wrote:
> hi,
> is there any other way to keep track of line number in a file other than
> fileinput.filelineno()

With enumerate():

for line_number, line in enumerate(open('myfile.txt')):
  # etc

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


Re: [Tutor] Still searching in html files

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 4:03 AM, Paul Melvin
 wrote:

> The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like
> some advice on is how to get the sorted data and where to put it.

Generally the code seems a bit disorganized, consider breaking it into
functions. There is a fair amount of deadwood you could delete. Also
it will be more readable if you move the initialization of variables
closer to where they are used.

> Currently I use a temporary list to store the data before writing it to
> another, is this OK? (lines 45 onwards)

for chunk in u.split(searchText):
temp.append(chunk)

u.split() already returns a sequence, there is no need for this loop.
You could just write
temp = u.split(searchText)
though you might think of a more descriptive name.

for item in temp[1:]:
temp2 = []
temp2.append(findLink(item))
temp2.append(findTitle(item))
temp2.append(findSize(item))
cleaned.append(temp2)

This could be written more concisely without temp2:
  cleaned.append( [ findLink(item), findTitle(item), findSize(item) ] )

or even put the whole thing into a single list comprehension (though
this is perhaps too dense):
cleaned = [ [ findLink(item), findTitle(item), findSize(item) ] for
item in u.split(searchText)[1:] ]

I think I would write it as
items = u.split(searchText)[1:]
cleaned = [ [ findLink(item), findTitle(item), findSize(item) ] for
item in items ]

> I want to expand the program to give some output which the user can interact
> with, e.g. ‘which of this information do you want me to get?’ and the user
> would choose.  Does anyone have any tips on this? Can I just use things like
> raw_input and lists?

That's fine, just separate out the data acquisition from the user
input and processing.

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


Re: [Tutor] combinatorics problem: assembling array

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 4:06 AM, markus kossner  wrote:
> Dear Pythonics,
> I have a rather algorithmic problem that obviously made a knot in my brain:
>
> Assume we have to build up all the arrays that are possible if we have a
> nested array
> containing an array of integers that are allowed for each single position.
> For example
> the nested array
> (
> (1,2,3,89),
> (3,5,8),
> (19,30,7,100,210,1,44)
> )
> would define all the arrays of length  3  that can be  enumerated  using the
> array of  possible numbers  for each position.

See itertools.product():
http://docs.python.org/library/itertools.html#itertools.product

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


Re: [Tutor] combinatorics problem: assembling array

2010-01-21 Thread spir
On Thu, 21 Jan 2010 10:06:40 +0100
markus kossner  wrote:

> Dear Pythonics,
> I have a rather algorithmic problem that obviously made a knot in my brain:
> 
> Assume we have to build up all the arrays that are possible if we have a 
> nested array
> containing an array of integers that are allowed for each single 
> position. For example
> the nested array
> (
> (1,2,3,89),
> (3,5,8),
> (19,30,7,100,210,1,44)
> )
> would define all the arrays of length  3  that can be  enumerated  using 
> the
> array of  possible numbers  for each position.
> Beforehand I will have no Idea of the length of the arrays. How do I 
> enumerate all the possible
> arrays with python?
> 
> Thanks for getting the knot out of my brain,
> Markus  

A loop over first set of numbers, with an inner loop over second set of 
numbers, with an inner loop over third set of numbers. Deep inside the loops, 
just feed a list of combinations.

Denis




la vita e estrany

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


Re: [Tutor] combinatorics problem: assembling array

2010-01-21 Thread Kent Johnson
On Thu, Jan 21, 2010 at 8:07 AM, Kent Johnson  wrote:
> On Thu, Jan 21, 2010 at 4:06 AM, markus kossner  wrote:
>> Dear Pythonics,
>> I have a rather algorithmic problem that obviously made a knot in my brain:
>>
>> Assume we have to build up all the arrays that are possible if we have a
>> nested array
>> containing an array of integers that are allowed for each single position.
>> For example
>> the nested array
>> (
>> (1,2,3,89),
>> (3,5,8),
>> (19,30,7,100,210,1,44)
>> )
>> would define all the arrays of length  3  that can be  enumerated  using the
>> array of  possible numbers  for each position.
>
> See itertools.product():
> http://docs.python.org/library/itertools.html#itertools.product

Here is an example:
In [1]: values = (
   ...: (1,2,3,89),
   ...: (3,5,8),
   ...: (19,30,7,100,210,1,44)
   ...: )

In [2]: from itertools import product

In [3]: list(product(*values))
Out[3]:
[(1, 3, 19),
 (1, 3, 30),
 (1, 3, 7),
...
 (2, 3, 19),
 (2, 3, 30),
...
 (89, 8, 210),
 (89, 8, 1),
 (89, 8, 44)]

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


[Tutor] Hello

2010-01-21 Thread Samuel de Champlain
This is my first message to this mailing list.
I want to create a project with glade and pygtk on fedora.
Can you suggest a good IDE?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello

2010-01-21 Thread Shashwat Anand
The choice of IDE is quite objective. Many of the people use vim/emacs
exclusively for all the work. Eclipse with PyDev plugin too is a good
choice.

On Thu, Jan 21, 2010 at 11:03 PM, Samuel de Champlain <
samueldechampl...@gmail.com> wrote:

> This is my first message to this mailing list.
> I want to create a project with glade and pygtk on fedora.
> Can you suggest a good IDE?
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] need help in python

2010-01-21 Thread invincible patriot

hi
I am a student and i need soe help regarding my assignmentif some one can help 
me il be glad.

i wil be waiting for the reply

thanks



Date: Thu, 21 Jan 2010 12:33:40 -0500
From: samueldechampl...@gmail.com
To: tutor@python.org
Subject: [Tutor] Hello

This is my first message to this mailing list.
I want to create a project with glade and pygtk on fedora.
Can you suggest a good IDE?
  
_
Hotmail: Trusted email with powerful SPAM protection.
http://clk.atdmt.com/GBL/go/196390707/direct/01/___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help in python

2010-01-21 Thread Andreas Kostyrka
Am Donnerstag, 21. Januar 2010 18:48:36 schrieb invincible patriot:
> hi
> I am a student and i need soe help regarding my assignmentif some one can
>  help me il be glad.

You do realize that assignments are things to be done by yourself?

This is a mailing list, so just post your questions, but do not expect us to 
do the assignment for you. OTOH specific questions, that suggest that you've 
at least tried to solve the problem yourself, will probably be answered. 
(Consider that answering and explaining it to you will probably take more time 
than just creating the your assignment, but that's beside the point.)

Andreas

> 
> i wil be waiting for the reply
> 
> thanks
> 
> 
> 
> Date: Thu, 21 Jan 2010 12:33:40 -0500
> From: samueldechampl...@gmail.com
> To: tutor@python.org
> Subject: [Tutor] Hello
> 
> This is my first message to this mailing list.
> I want to create a project with glade and pygtk on fedora.
> Can you suggest a good IDE?
> 
> _
> Hotmail: Trusted email with powerful SPAM protection.
> http://clk.atdmt.com/GBL/go/196390707/direct/01/
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help in python

2010-01-21 Thread Luke Paireepinart
Don't post messages to the list in reply to other messages, it messes up
threading.
Other than that, you'll have to tell us more about your assignment if you
want help.
-Luke

On Thu, Jan 21, 2010 at 11:48 AM, invincible patriot <
invincible_patr...@hotmail.com> wrote:

>  hi
> I am a student and i need soe help regarding my assignmentif some one can
> help me il be glad.
>
> i wil be waiting for the reply
>
> thanks
>
>
>
> --
> Date: Thu, 21 Jan 2010 12:33:40 -0500
> From: samueldechampl...@gmail.com
> To: tutor@python.org
> Subject: [Tutor] Hello
>
> This is my first message to this mailing list.
> I want to create a project with glade and pygtk on fedora.
> Can you suggest a good IDE?
>
> --
> Hotmail: Trusted email with powerful SPAM protection. Sign up 
> now.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help in python

2010-01-21 Thread Robert
And your question is ?


On Thu, Jan 21, 2010 at 12:48 PM, invincible patriot
 wrote:
> hi
> I am a student and i need soe help regarding my assignmentif some one can
> help me il be glad.
>
> i wil be waiting for the reply
>
> thanks
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello

2010-01-21 Thread Robert
Check out wingware IDE and Geany.


On Thu, Jan 21, 2010 at 12:33 PM, Samuel de Champlain
 wrote:
> This is my first message to this mailing list.
> I want to create a project with glade and pygtk on fedora.
> Can you suggest a good IDE?
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello

2010-01-21 Thread vishwajeet singh
>
> On Thu, Jan 21, 2010 at 11:03 PM, Samuel de Champlain <
> samueldechampl...@gmail.com> wrote:
>
>> This is my first message to this mailing list.
>> I want to create a project with glade and pygtk on fedora.
>> Can you suggest a good IDE?
>>
>

   netbeans have worked quite well for me but its heavy weight if you are
looking for something light check out komodo.



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


-- 
Vishwajeet Singh
+91-9657702154 | dextrou...@gmail.com | http://singhvishwajeet.com
Twitter: http://twitter.com/vishwajeets | LinkedIn:
http://www.linkedin.com/in/singhvishwajeet
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Replacing the string in a file

2010-01-21 Thread vanam
Hi all,

I have been trying to write a script where in it has to replace a
particular string in a file and write back to the file without intact
of the contents

I have tried below mentioned steps, but of no avail:

1. Created a file by name data.txt and included some text (For
instance,


Then, I started searching for a good book on Python. I couldn't find
any! I did find some O'Reilly books
but they were either too expensive or were more like a reference
manual than a guide. So, I settled for
the documentation that came with Python. However, it was too brief and
small. It did give a good idea
about Python but was not complete. I managed with it since I had
previous programming experience, but
it was unsuitable for newbies.


2.Read the file contents (For this opened the file in read mode -- log
= open('data.txt','r')

3.Iterated through the text using for loop for reading the contents in
the file -- for x in log:

4.Replaced the word  Python with PYTHON as indicated --- x  =
x.replace('Python','PYTHON')

6.printed the full contents -- It had printed the full contents by
replacing the word Python to PYTHON

Python string did not get changed to PYTHON for the reasons that i did
not open the file in write mode and written the string

[Query]: How to write Python string to PYTHON to that file without
intact of the full contents

I was successful in routing the contents (with changed string to
PYTHON) to a different file.

And i am aware that opening the file in write mode will wipe out the
previous contents.

I have tried appending the content which it does but that is not my goal.


7. Closed the files outside for loop -- log.close()
-- 
Raghavendra  Vanam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Shashwat Anand
You can try it by reading all data as string, replacing the string and then
write it to target file via opeing it in +w mode.

f = open('data.txt', 'rw').read().replace('Python', 'PYTHON')
f1 = open('data.txt',
'w')

f1.write(f)

HTH


On Fri, Jan 22, 2010 at 12:14 PM, vanam wrote:

> Hi all,
>
> I have been trying to write a script where in it has to replace a
> particular string in a file and write back to the file without intact
> of the contents
>
> I have tried below mentioned steps, but of no avail:
>
> 1. Created a file by name data.txt and included some text (For
> instance,
>
> 
> Then, I started searching for a good book on Python. I couldn't find
> any! I did find some O'Reilly books
> but they were either too expensive or were more like a reference
> manual than a guide. So, I settled for
> the documentation that came with Python. However, it was too brief and
> small. It did give a good idea
> about Python but was not complete. I managed with it since I had
> previous programming experience, but
> it was unsuitable for newbies.
> 
>
> 2.Read the file contents (For this opened the file in read mode -- log
> = open('data.txt','r')
>
> 3.Iterated through the text using for loop for reading the contents in
> the file -- for x in log:
>
> 4.Replaced the word  Python with PYTHON as indicated --- x  =
> x.replace('Python','PYTHON')
>
> 6.printed the full contents -- It had printed the full contents by
> replacing the word Python to PYTHON
>
> Python string did not get changed to PYTHON for the reasons that i did
> not open the file in write mode and written the string
>
> [Query]: How to write Python string to PYTHON to that file without
> intact of the full contents
>
> I was successful in routing the contents (with changed string to
> PYTHON) to a different file.
>
> And i am aware that opening the file in write mode will wipe out the
> previous contents.
>
> I have tried appending the content which it does but that is not my goal.
>
>
> 7. Closed the files outside for loop -- log.close()
> --
> Raghavendra  Vanam
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Stefan Behnel
vanam, 22.01.2010 07:44:
> [Query]: How to write Python string to PYTHON to that file without
> intact of the full contents

What one would normally do is: read the file (line by line if that's ok in
your case), replace the word by the new one, write the new line to a new
temporary file. When done, move the new file over the old one. That also
makes sure that your changes are 'atomic' (as good as it gets), i.e. your
original file is still there when you encounter an error during the
replacement run.

Stefan

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


Re: [Tutor] Replacing the string in a file

2010-01-21 Thread Shashwat Anand
Editing original file can be risky at times. If @vanam wants to edit the
same fie, he can open the original file, read it, rename it to say
'data.txt.bak' and then write the modified content to the file named as
'data.txt'. This way he gets the data in the file he wants and also have a
backup of original file.

On Fri, Jan 22, 2010 at 1:07 PM, Stefan Behnel  wrote:

> vanam, 22.01.2010 07:44:
> > [Query]: How to write Python string to PYTHON to that file without
> > intact of the full contents
>
> What one would normally do is: read the file (line by line if that's ok in
> your case), replace the word by the new one, write the new line to a new
> temporary file. When done, move the new file over the old one. That also
> makes sure that your changes are 'atomic' (as good as it gets), i.e. your
> original file is still there when you encounter an error during the
> replacement run.
>
> Stefan
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor