Re: [Tutor] String Processing Query

2011-05-17 Thread spawgi
This snipped also works - may be there is a better solution -

', '*']
>>> s = ''
>>> for line in lines:
line = line.rstrip('\n')
 if line.startswith('>from header'):
continue
elif line.startswith('*'):
 s = s + '\n'
else:
s = s + line

>>> s
'abcdefghijklmnopqrstuvwxyz\npoiuytrewqlkjhgfdsamnbvcxz\n'
>>> print s
abcdefghijklmnopqrstuvwxyz
poiuytrewqlkjhgfdsamnbvcxz


Thanks and Regards,
Sumod

On Mon, May 16, 2011 at 7:35 PM, James Reynolds  wrote:

> concatenate the entire thing together, including the "*".
>
> Once you have that as a single string, use string.split('*') and you will
> have your two strings.
>
> On Mon, May 16, 2011 at 9:51 AM, Spyros Charonis wrote:
>
>> I have a file with the following contents:
>>
>> >from header1
>> abcdefghijkl
>> mnopqrs
>> tuvwxyz
>> *
>> >from header2
>> poiuytrewq
>>  lkjhgfdsa
>> mnbvcxz
>> *
>>
>> My string processing code goes as follows:
>>
>> file1=open('/myfolder/testfile.txt')
>> scan = file1.readlines()
>>
>> string1 = ' '
>> for line in scan:
>> if line.startswith('>from'):
>> continue
>> if line.startswith('*'):
>> continue
>> string1.join(line.rstrip('\n'))
>>
>> This code produces the following output:
>>
>> 'abcdefghijkl'
>> 'mnopqrs'
>> 'tuvwxyz'
>> 'poiuytrewq'
>> 'lkjhgfdsa'
>> 'mnbvcxz'
>>
>> I would like to know if there is a way to get the following
>> output instead:
>>
>> 'abcdefghijklmnopqrstuvwxyz'
>>
>> 'poiuytrewqlkjhgfdsamnbvcxz'
>>
>> I'm basically trying to concatenate the strings
>> in order to produce 2 separate lines
>>
>>
>>
>> ___
>> 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
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Indexing a List of Strings

2011-05-18 Thread spawgi
Agreed that your original sequences are 1000 char long. But it helps to
understand the problem better if you can give examples with smaller strings.
Please can you post smaller examples? This will also help you test your code
on your own inputs.

On Wed, May 18, 2011 at 5:40 AM, Spyros Charonis wrote:

> Greetings Python List,
>
> I have a motif sequence (a list of characters e.g. 'EAWLGHEYLHAMKGLLC')
> whose index I would like to return.
> The list contains 20 strings, each of which is close to 1000 characters
> long making it far too cumbersome to display an example.
> I would like to know if there is a way to return a pair of indices, one
> index where my sequence begins (at 'E' in the above case) and
> one index where my sequence ends (at 'C' in the above case). In short, if
> 'EAWLGHEYLHAMKGLLC' spans 17 characters is it possible
> to get something like 100 117, assuming it begins at 100th position and
> goes up until 117th character of my string. My loop goes as
> follows:
>
> for item in finalmotifs:
> for line in my_list:
> if item in line:
> print line.index(item)
>
> But this only returns a single number (e.g 119), which is the index at
> which my sequence begins.
>
> Is it possible to get a pair of indices that indicate beginning and end of
> substring?
>
> Many thanks
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sequencing

2011-05-18 Thread spawgi
I don't think we can solve your homework :). But regarding hint, try to
think in terms of each number as part of list and then process the list.
Also, consider the option if the numbers will be given as a string or as
integers or float etc.

Thanks.

On Wed, May 18, 2011 at 2:49 PM, Cindy Lee  wrote:

> Hi Pyton Tutors thanks for adding me,
>
> I am new to Python and missed one of my classes and am not sure of my
> homework. We are currently on sequencing and are being asked to make a
> function that receives text as an argument and returns the same text, but
> with 1 added to each number. So far I have:
>
>
> def ReceiveAndReturn():
>
> sentence=raw_input("Give me a sentence with variables in it: ")
>
> print ReceiveAndReturn
>
>
>
> could anyone give me any hints on what else needs to be added?
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xrange() with start or stop > sys.maxint?

2011-05-18 Thread spawgi
What is the version of python you are using?

>From the documentation of python 2.71.
http://docs.python.org/library/functions.html#xrange

CPython implementation detail: xrange() is intended to be simple and fast.
Implementations may impose restrictions to achieve this. The C
implementation of Python restricts all arguments to native C longs (“short”
Python integers), and also requires that the number of elements fit in a
native C long. If a larger range is needed, an alternate version can be
crafted using the itertools module: islice(count(start, step),
(stop-start+step-1)//step).

Hope this helps.

Thanks and Regards,
Sumod


On Thu, May 19, 2011 at 6:47 AM, Terry Carroll  wrote:

> Is there any way to use xrange with a start or stop value that exceeds
> sys.maxint?
>
>  import sys
 print sys.maxint

>>> 2147483647
>
>> start = sys.maxint-1
 for i in xrange(start, start+1):

>>> ...   pass
> ...
>
>> start = sys.maxint
 for i in xrange(start, start+1):

>>> ...   pass
> ...
> Traceback (most recent call last):
>  File "", line 1, in 
> OverflowError: Python int too large to convert to C long
>
>>

> Works okay with range, though:
>
>  start = sys.maxint
 for i in range(start, start+1):

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



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is `if __name__ == "__main__"` for?

2011-05-20 Thread spawgi
If the module is run as a program, then the __name__ is assigned the value
__main__ .
If the module is imported, then the value is not assigned.

Please see here -
http://stackoverflow.com/questions/419163/what-does-if-name-main-do


On Fri, May 20, 2011 at 4:39 PM, Ganesh Kumar  wrote:

> Hi Gurus,
>
> I am new python programming.. I see many programs
> if __name__ == '__main__':
>  when I check __name__ always eq __main__.
> what purpose use these structure.. please guide me..
>
> -Ganesh
>
> --
> Did I learn something today? If not, I wasted it.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] STRING PROC

2011-05-20 Thread spawgi
You can also try this

>>> a = '>> a
'>> a.partition('<')
('', '<', 'NAME')
>>> splitstring = a.partition('<')
>>> splitstring
('', '<', 'NAME')
>>> splitstring[0]
''
>>> splitstring[1]
'<'
>>> splitstring[2]
'NAME'

I guess you can use this tuple.

Hope this helps.

Thanks and Regards,
Sumod

On Fri, May 20, 2011 at 6:42 PM, Spyros Charonis wrote:

> Hello List,
>
> A quick string processing query. If I have an entry in a list such as
> ['>NAME\n'],
> is there a way to split it into two separate lines:
>
> >
> NAME
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and the web

2011-05-22 Thread spawgi
Please see the Django framework and Google App Engine using Python.
I particularly recommend the GAE using Python to create a basic data driven
application. It will give answers to quite a few of your questions.

Regards
SWP

On Sun, May 22, 2011 at 11:39 PM, michael scott wrote:

> I want to start getting into web site development. I already know basic
> html and css, which will create a basic webpage. But my question is what
> exactly does python bring to the web?
>
> Are forums, blogs, flash sites, etc the results of web programming or can
> they all be achieved with standard html / css? What exactly can I do
> differently with python than with html and css? Or should I say, how can I
> use python WITH html and css to create something better?
>
>
>
> 
> What is it about you... that intrigues me so?
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Non programmer wanting to become programmer

2011-05-26 Thread spawgi
Hello amt,

Please find my responses below -

On Fri, May 27, 2011 at 1:06 AM, amt <0101...@gmail.com> wrote:

> First of all, hello!  I want to start learning programming. I'm looking
> into becoming more than a hobbyist programmer. I searched a lot on Google on
> what programming language should I learn first and I see a lot of good words
> about Python so I decided to go for it but have some questions:
>
> 1)What book should I start with?  ( I have checked Python for non
> programmers but there are a lot of titles there, what should I pick first?I
> was thinking about Invent your own computer games with Python.)
> >> I think Learning Python and Core Python Programming are good books.
> Please also have a look at Learn Python the Hard Way.
>
> 2)Version 2 or version 3? What should I go for as a beginner and why? ( I
> ask because some books in the Python for non programmers section are for
> python 2, Invent your own computer games with Python is version 3.)
> >> I would start with version 2 first as I think there are more resources
> available on that.
>
> 3)Algorithms, memory management, data structures, when is the right time to
> learn them?
>
>> :) . The two things go hand in hand. It is like pieces of a jigsaw. Now
is the right time in my opinion. But you should be able to map the data
structures with the programming concepts. For some data structures C/C++ may
be better options to consider.

I consider myself as a student only and this is my personal approach. May be
there is a better way.

Regards,
Sumod

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


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating a dictionary

2011-05-27 Thread spawgi
The first approach does not really give you any variable (in easy form) to
operate upon.
The second approach returns a dict object that you can later reuse in easier
form. So one advantage is certainly that ease of object reuse and also
object modification.
If you want to add, remove, update anything in the dict object, the second
syntax is easier. You can also make it part of a class or module. You can
pass it to a function as well.

Regards
SWP

2011/5/27 Válas Péter 

> Hi,
> I think I am new to here, as far as I remember. :-)
>
> http://docs.python.org/dev/library/stdtypes.html#dict says:
> we can create a dictionary with
>
>- dict({'one': 1, 'two': 2})
>
> What is the adventage of this form to simply writing d = {'one': 1, 'two': 2}?
> Is there any difference?
>
> Thanks
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating a dictionary

2011-05-27 Thread spawgi
I think the way - d = dict({'one': 1, 'two': 2})  can be used to created
dictionary using list comprehension.
e.g.
>>> d = dict((i,i**2) for i in range(10))
>>> d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

I do not think the second approach can be used in this fashion.

>From python documentation -
http://docs.python.org/tutorial/datastructures.html
The dict() constructor builds dictionaries directly from lists of key-value
pairs stored as tuples. When the pairs form a pattern, list comprehensions
can compactly specify the key-value list.

I am not an expert. So may be someone can explain it better.

Regards
SWP
2011/5/27 Válas Péter 

> Sorry, I am afraid, I was not clear enough. So what is the difference
> between
>   d = dict({'one': 1, 'two': 2})
> and
>   d = {'one': 1, 'two': 2}
> ?
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating a dictionary

2011-05-27 Thread spawgi
Thanks for the detailed explanation Steve. That was very helpful.

On Fri, May 27, 2011 at 8:04 PM, Steven D'Aprano wrote:

> Válas Péter wrote:
>
>> Hi,
>> I think I am new to here, as far as I remember. :-)
>>
>> http://docs.python.org/dev/library/stdtypes.html#dict says:
>> we can create a dictionary with
>>
>>   - dict({'one': 1, 'two': 2})
>>
>> What is the adventage of this form to simply writing d = {'one': 1, 'two':
>> 2}?
>> Is there any difference?
>>
>
> dict() is a function (technically, a type) that creates new dictionaries
> from whatever argument you pass to it. { ... } is syntax for creating
> literal dictionaries. Think of this as similar to the difference between a
> mathematical expression:
>
> x = 2*9-1
>
> and a number literal:
>
> x = 17
>
>
> HOWEVER, in the above example with dict(), the form shown is redundant.
> dict({'one': 1, 'two': 2}) does these four steps:
>
> (a) Python creates a dictionary using the "dict literal" syntax
>{'one': 1, 'two': 2}
> (b) That dictionary is then passed to the dict() function
> (c) The dict() function makes a copy of that dictionary and returns it
> (d) Python's garbage collector deletes the original dictionary.
>
> Never put a lone dict literal {...} inside a call to dict(), that's just a
> waste of time. Just use the literal on its own.
>
> dict() *can* be very useful, just not in the example shown. You can use it
> to make copies of other dicts:
>
>
> first_dict = {'one': 1, 'two': 2}
> second_dict = dict(first_dict)
>
>
> That's not very interesting, as you can easily make a copy with
> first_dict.copy() instead. But it gets more interesting if you want to add
> new items to the dictionary:
>
>
> third_dict = dict(first_dict, three=3, four=4)
>
>
> You can even leave out the original dict:
>
>
> fourth_dict = dict(a=1, b=2, c=3)
>
>
> or instead use a list of (key, value) pairs:
>
>
> items = [('a', 1), ('b', 2), ('c', 3)]
> fifth_dict = dict(items, d=4)
>
>
>
> --
> Steven
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File parsing

2011-06-16 Thread spawgi
Hello Neha,

I think this script will do what you want and will also fix some of the
issues in your original code -

  1 #! /usr/bin/python
  2
  3 import curses.ascii
  4 import string
  5 import sys
  6
  7 with open(sys.argv[1],'r') as input:
  8   for eachline in input:
  9 eachline = eachline.strip('\n')
 10 if curses.ascii.isalpha(eachline[0]):
 11   first_char = ''
 12 else:
 13   first_char = eachline[0]
 14 last_char = eachline[len(eachline)-1]
 15 eachline = eachline.lstrip(first_char)
 16 eachline = eachline.rstrip(last_char)
 17 words = eachline.split(' ')
 18 outline = first_char
 19 for word in reversed(words):
 20   outline = outline + str(word) + ' '
 21 outline = outline.rstrip() + last_char + '\n'
 22 print outline
---

It is much more explicit. It considers some special cases and gives the
output in the format desired by you.

Regards,
SWP

On Fri, Jun 17, 2011 at 12:21 AM, Neha P  wrote:

> Thanks James
>
> I guess i have to use the same code for text in yellow... seems like ther's
> no other way...
>
> Regards,
> Neha
> --
> *From:* James Reynolds 
> *To:* Neha P 
> *Cc:* "tutor@python.org" 
> *Sent:* Thursday, June 16, 2011 2:43 PM
> *Subject:* Re: [Tutor] File parsing
>
> use split on the list to split it up. search each element for something
> like:
>
> if '"' == element[:-1]:
>
> if that evaluation is True, I would remove the quote mark from the word on
> the right side, and place a new one on the left side using something like
> '"' + element.
>
> I would do the same thing for the other side in the same for loop, instead
> the evaluation would be:
>
> if '"' == element[:1]:
>
>
>
>
> On Thu, Jun 16, 2011 at 1:03 PM, Neha P  wrote:
>
> Hi all,
> I know below query may sound silly, but can somebody suggest any better way
> of doing this:
> It would be helpful.
>
> I need to read a file line by line and print each line starting from the
> last word first:
>
> C:\Python26>type file_reversing_program.txt
> import sys
> import string
>
> f_obj=open(sys.argv[1],"r")
>
> for eachline in f_obj:
> eachline=eachline[ :-1] # to eliminate the trailing "\n"
> list_words=eachline.split(" ")
> list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1 is
> printed, line 2 should start on a new line
> list_words.reverse()
> for every_word in list_words:
>  print every_word, #  'comma' helps in printing words on same
> line,hence for last word we append "\n"
>
> f_obj.close()
>
> C:\Python26>type input_file.txt
> "Hi ther, how are you?"
> I are doing fine, thank you.
>
> C:\Python26>file_reversing_program.py input_file.txt
> you?" are how ther, "Hi
> you. thank fine, doing are I
>
> Is there a better way of doing the above program, mainly the text
> highlighted in yellow,
> Also if that is settled can there be a logic for getting the ouput more
> properly formatted (for text in blue) ,
> say giving an output like  :
> "you? are how ther, Hi"
> you. thank fine, doing are I
>
>
> Thanks,
> Neha
>
>
> ___
> 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
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File parsing

2011-06-16 Thread spawgi
Output on your sample input -

$./file_reversing_program.py input.txt
"you? are how there, Hi"

you thank fine, doing am I.


On Fri, Jun 17, 2011 at 1:26 AM,  wrote:

> Hello Neha,
>
> I think this script will do what you want and will also fix some of the
> issues in your original code -
> 
>   1 #! /usr/bin/python
>   2
>   3 import curses.ascii
>   4 import string
>   5 import sys
>   6
>   7 with open(sys.argv[1],'r') as input:
>   8   for eachline in input:
>   9 eachline = eachline.strip('\n')
>  10 if curses.ascii.isalpha(eachline[0]):
>  11   first_char = ''
>  12 else:
>  13   first_char = eachline[0]
>  14 last_char = eachline[len(eachline)-1]
>  15 eachline = eachline.lstrip(first_char)
>  16 eachline = eachline.rstrip(last_char)
>  17 words = eachline.split(' ')
>  18 outline = first_char
>  19 for word in reversed(words):
>  20   outline = outline + str(word) + ' '
>  21 outline = outline.rstrip() + last_char + '\n'
>  22 print outline
>
> ---
>
> It is much more explicit. It considers some special cases and gives the
> output in the format desired by you.
>
> Regards,
> SWP
>
> On Fri, Jun 17, 2011 at 12:21 AM, Neha P  wrote:
>
>> Thanks James
>>
>> I guess i have to use the same code for text in yellow... seems like
>> ther's no other way...
>>
>> Regards,
>> Neha
>> --
>> *From:* James Reynolds 
>> *To:* Neha P 
>> *Cc:* "tutor@python.org" 
>> *Sent:* Thursday, June 16, 2011 2:43 PM
>> *Subject:* Re: [Tutor] File parsing
>>
>> use split on the list to split it up. search each element for something
>> like:
>>
>> if '"' == element[:-1]:
>>
>> if that evaluation is True, I would remove the quote mark from the word on
>> the right side, and place a new one on the left side using something like
>> '"' + element.
>>
>> I would do the same thing for the other side in the same for loop, instead
>> the evaluation would be:
>>
>> if '"' == element[:1]:
>>
>>
>>
>>
>> On Thu, Jun 16, 2011 at 1:03 PM, Neha P  wrote:
>>
>> Hi all,
>> I know below query may sound silly, but can somebody suggest any better
>> way of doing this:
>> It would be helpful.
>>
>> I need to read a file line by line and print each line starting from the
>> last word first:
>>
>> C:\Python26>type file_reversing_program.txt
>> import sys
>> import string
>>
>> f_obj=open(sys.argv[1],"r")
>>
>> for eachline in f_obj:
>> eachline=eachline[ :-1] # to eliminate the trailing "\n"
>> list_words=eachline.split(" ")
>> list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1
>> is printed, line 2 should start on a new line
>> list_words.reverse()
>> for every_word in list_words:
>>  print every_word, #  'comma' helps in printing words on same
>> line,hence for last word we append "\n"
>>
>> f_obj.close()
>>
>> C:\Python26>type input_file.txt
>> "Hi ther, how are you?"
>> I are doing fine, thank you.
>>
>> C:\Python26>file_reversing_program.py input_file.txt
>> you?" are how ther, "Hi
>> you. thank fine, doing are I
>>
>> Is there a better way of doing the above program, mainly the text
>> highlighted in yellow,
>> Also if that is settled can there be a logic for getting the ouput more
>> properly formatted (for text in blue) ,
>> say giving an output like  :
>> "you? are how ther, Hi"
>> you. thank fine, doing are I
>>
>>
>> Thanks,
>> Neha
>>
>>
>> ___
>> 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
>>
>>
>
>
> --
> http://spawgi.wordpress.com
> We can do it and do it better.
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get last 7 days string in a log file

2011-08-01 Thread spawgi
Hi,

Let me understand the problem
- Do you want to count the number of times the application was restarted
between today and 7 days back?
=> If yes, then you will need to do some string manipulation to get the
required dates. Then you will need to apply both the conditions to the line
i.e. if the date is the required date and if the string contains what you
want.
i.e. if "2009-03-6" in line and :
... count += 1
You can apply this logic to each date and create a complex statement using
OR to get the count.
OR you can use the datetime object with proper formatting and check the
existence of the condition.

Do I understand your problem correctly?

Regards
Sumod


2011/8/1 Bjørn-Roar Eriksen 

> Hi
>
> I'm new to Python and I'm trying to write a script that's count restart of
> an application. The log file contains:
> 2009-03-06 18:20:26,423  User operation - start nanny
> 2009-03-06 18:20:26,423  User operation - start all services
> And 2009-03-06 18:20:26,423  User operation - start all services tells me
> that the application has been restarted.
>
> My script do count it, but I cant figure out how to code so it's only get
> from to day and 7 day's back.
> #Counts restart of an application
> count = 0
> with open("c:/test/bre.log")as f:
> for line in f:
> if "User operation - start all services" in line:
> count += 1
> #print line.strip()
> print 'Server restart: %s' % count
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] time zone conversion

2011-08-01 Thread spawgi
Quick question - Is any daylight saving taken into consideration for this?

On Tue, Aug 2, 2011 at 1:35 AM, ian douglas wrote:

> Hi all,
>
> Been trying to wrap my head around some datetime vs time stuff with regards
> to parsing a string as a date plus time with a timezone offset.
>
> This is the string I'm given:
>
> 2010-01-22T00:14:33.000Z
>
> And I can use time.strptime to parse out its individual elements, but then
> I need to adjust that time, in UTC/Zulu to my local time zone.
>
> Here's what I'm currently trying:
>
>old_launch_time = '2010-01-22T00:14:33.000Z'
>os.environ['TZ'] = 'UTC'
>time.tzset()
>launch_time = time.strptime(old_launch_time,
> '%Y-%m-%dT%H:%M:%S.000Z')
>os.environ['TZ'] = 'US/Pacific'
>time.tzset()
>print 'old time: ' + old_launch_time
>print 'new time: ' + time.strftime("%Y-%m-%d %H:%M:%S", launch_time)
>
> output:
>
> old time: 2010-01-22T00:14:33.000Z
> new time: 2010-01-22 00:14:33
>
> But the different tzset() calls are not adjusting based on the 7 or 8 hour
> difference.
>
> I know that all incoming dates/times are coming to me in UTC. I just can't
> seem to get a good handle on how to properly convert it to my own time zone.
>
> Thanks,
> Ian
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python scripts into executable Windows programss

2011-08-03 Thread spawgi
For a seamless application development experience, you may want to consider
these steps.
1. Use Eclipse-pydev to write your scripts
2. Use py2exe to convert into executable programs
3. Use Inno setup to create installers

There is support for 2.6 and 2.7 with Python. Please see the news page -
http://www.py2exe.org/index.cgi/News

Hope this helps.

Thanks and Regards,
Sumod


On Wed, Aug 3, 2011 at 9:55 AM, Emeka  wrote:

> Hello All,
>
> I would want to convert  Python scripts into executable Windows programs.
> I have already checked out py2exe, it seems like they support only Python
> 2.5. Mine is Python 2.7.7. Could anyone here help me out on this issue?
> ?
>
> Regards,
> Emeka --
> *Satajanus  Nig. Ltd
>
>
> *
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamically linking python into my vc project - help required

2011-08-03 Thread spawgi
+1 to Alan on this.

Also, you may want to try stackoverflow.com for this question.

Thanks and Regards,
Sumod

On Wed, Aug 3, 2011 at 1:41 PM, Alan Gauld wrote:

> On 03/08/11 07:52, mrinal...@edss.co.in wrote:
>
>> Hi,
>>
>> I am trying to embed python into my MFC application. I have done this
>> before by statically linking to the python lib. But I want to change
>> this now.
>>
>
> Hi, this mailing list is really for people trying to learn Python.
> This question is way more advanced than that! While you might
> get a reply here you will probably have more success on the main Python
> mailing list or even on the Python Windows list.
>
>
> --
> Alan G
> 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
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calendar

2011-09-12 Thread spawgi
If you want to create a calendar for a webpage, I think you can use many
ready made JS packages for this. May be jquery also has something.

Is using Python mandatory for this task? And why?

On Tue, Sep 13, 2011 at 12:45 AM, nitin chandra wrote:

> Hi All,
>
> I need to create a calendar with python & CSS for the web page, in the
> following format
>
>  Dec , 2011
>
> -
> sun  |   mon  |   tue  |  wed thu fri sat sun mon tue  wed  thu  fri  sat
>  sun
>
> ---|---|--|--
> 1 | 2 |   3 | 45   6   789 10   11
> 12   13  14   15
>
> ---|---|--|---
> img1 |  img2 |  img3 | 
>
>  Jan , 2012
>
> -
> sun  |   mon  |   tue  |  wed thu fri sat sun mon tue  wed  thu  fri  sat
>  sun
>
> ---|---|--|--
> 1 | 2 |   3 | 45   6   789 10   11
> 12   13  14   15
>
> ---|---|--|---
> img1 |  img2 |  img3 | 
>
>  Feb , 2012
>
> -
> sun  |   mon  |   tue  |  wed thu fri sat sun mon tue  wed  thu  fri  sat
>  sun
>
> ---|---|--|--
> 1 | 2 |   3 | 45   6   789 10   11
> 12   13  14   15
>
> ---|---|--|---
> img1 |  img2 |  img3 | 
>
>
> Can some one please show me how to do the above ?
>
> Thanks
>
> Nitin
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-01 Thread spawgi
Shouldn't this be treated as a bug then? As a user I should be allowed to
uninstall the software I want to.
Or you uninstalled other things by mistake?

On Wed, Nov 2, 2011 at 6:18 AM, Joel Montes de Oca
wrote:

> On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:
>
>> Heh, yeah.  It's usually a bad idea to do stuff like that (I know a guy
>> (Windows) who deleted his OS of his system).
>>
>> On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:
>>
>>  I just discovered that it is a bad idea to complete uninstall Python 2.7
>>> on Ubuntu 11.10. If you do, expect a lot of things not to work, mainly your
>>> system. haha
>>>
>>> I just reinstalled Python 2.7 and I hope things are not so bad now when
>>> I reboot.
>>>
>>> --
>>> -Joel M.
>>>
>>> __**_
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/**mailman/listinfo/tutor
>>>
>>
>>
> Yea, It wiped out GNOME and UNITY along with a few other applications. It
> wasn't a big deal tho, I just reinstalled ubuntu-desktop threw apt-get. :)
>
>
>
> --
> -Joel M.
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread spawgi
I use OS X / Windows and I have not noticed any dependency issues. So I was
not aware about the same with respect to Ubuntu. I am glad that I learnt
something from this discussion.

Regards,
Sumod

On Wed, Nov 2, 2011 at 7:40 PM, Joel Montes de Oca
wrote:

> On 11/02/2011 02:26 AM, spa...@gmail.com wrote:
>
>> Shouldn't this be treated as a bug then? As a user I should be allowed to
>> uninstall the software I want to.
>> Or you uninstalled other things by mistake?
>>
>> On Wed, Nov 2, 2011 at 6:18 AM, Joel Montes de Oca <
>> joelmonte...@gmail.com <mailto:joelmonte...@gmail.com**>> wrote:
>>
>>On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:
>>
>>Heh, yeah.  It's usually a bad idea to do stuff like that (I
>>know a guy (Windows) who deleted his OS of his system).
>>
>>On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:
>>
>>I just discovered that it is a bad idea to complete
>>uninstall Python 2.7 on Ubuntu 11.10. If you do, expect a
>>lot of things not to work, mainly your system. haha
>>
>>I just reinstalled Python 2.7 and I hope things are not so
>>bad now when I reboot.
>>
>>-- -Joel M.
>>
>>__**_
>>Tutor maillist  - Tutor@python.org <mailto:Tutor@python.org>
>>
>>To unsubscribe or change subscription options:
>>
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>>
>>
>>Yea, It wiped out GNOME and UNITY along with a few other
>>applications. It wasn't a big deal tho, I just reinstalled
>>ubuntu-desktop threw apt-get. :)
>>
>>
>>
>>-- -Joel M.
>>__**_
>>Tutor maillist  - Tutor@python.org <mailto:Tutor@python.org>
>>
>>To unsubscribe or change subscription options:
>>
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>>
>>
>>
>> --
>> http://spawgi.wordpress.com
>> We can do it and do it better.
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>
> Spawgi,
>
> Like a few people have mentioned before, this isn't a bug. Unity and GNOME
> rely on Python 2.7 to work, which is fine. When I uninstalled Python2.7
> GNOME and Unity didn't have a way to run their py files, which results in a
> broken environment. The package manager was smart enough to list everything
> that was going to be uninstalled since they rely on Python. So instead of
> leaving broken systems on my computer, it uninstalled all the packages that
> would had been broken, including Unity and GNOME.
>
> I just want to add one thing that hasn't been mentioned yet. The only bug
> was the one called Joel M, which failed to read the uninstall list until
> later when I noticed my environments were being uninstalled.  haha!
>
> Synaptic, the package manager I was using, listed everything that was
> going to be uninstalled but I didn't read it. I didn't realize how many
> system tools used python until later. Live and learn.
>
> It wasn't a big deal. All I had to do was reinstall Python2.7 and reboot.
> Of course, after the reboot, I didn't have an environment to log into. So I
> pressed Ctr + Alt + F1 to get into tty1 and then ran sudo apt-get install
> ubuntu-desktop. That took care of the missing environment. Then I restarted
> with sudo shutdown -r now. I didn't really need to do that since I think
> restarting X was enough but I restarted anyhow.
>
> As far as the comments about Ubuntu 11.10 being buggy. I don't think it's
> Ubuntu 11.10 that is buggy, I think it's Unity that is a bit buggy. I use
> Unity and sometimes applications do crash or fail to open when you click on
> their icons... Like I said, I think it's an issue with Unity but I can't
> say for sure since I haven't used another environment since I installed
> Ubuntu 11.10.
>
>
> --
> -Joel M.
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to remove the coming duplication

2011-11-09 Thread spawgi
Hello,

list.remove will remove the first occurrence of the value from the list. So
the output is expected as far as Python is concerned.
May be you should think about using pop function.
Please take a look at the code below


>>> def RemoveComingDuplicates(a):
for i in range(len(a)-1):
 print i,i+1
if a[i+1] == a[i]:
a.pop(i+1)
 print a

>>> a = ['2','5','7','5','5']
>>> RemoveComingDuplicates(a)
0 1
['2', '5', '7', '5', '5']
1 2
['2', '5', '7', '5', '5']
2 3
['2', '5', '7', '5', '5']
3 4
['2', '5', '7', '5']
>>>

Hope this helps.

Thanks and Regards,
Sumod
On Thu, Nov 10, 2011 at 12:54 PM, lina  wrote:

> >>> for i in range(len(a)):
>if i == 0:
>b.append(a[i])
>if a[i]!=b[-1]:
>b.append(a[i])
>
> This one seems work, but looks clumsy,
>
> Thanks for any suggestions that helps to improve.
>
> Best regards,
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pythonic way of concatenation of elements in an array

2012-01-26 Thread spawgi
Hello,

My code is -

l = len(m)
item = str(m[1])
for i in range(2,l):
item = item + "-" + str(m[i])

This code is part of a bigger function. It works fine. But I am not happy
with the way I have written it. I think there is a better (Pythonic) way to
rewrite it.
If anyone knows how to improve this snippet, I would be very thankful.

Thanks and Regards,
Sumod


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythonic way of concatenation of elements in an array

2012-01-26 Thread spawgi
Hello Steven,

Thanks a lot for the detailed answer. I will implement your suggestions.
Really appreciate it.

Thanks and Regards,
Sumod

On Fri, Jan 27, 2012 at 4:34 AM, Steven D'Aprano wrote:

> spa...@gmail.com wrote:
>
>> Hello,
>>
>> My code is -
>>
>> l = len(m)
>> item = str(m[1])
>> for i in range(2,l):
>>item = item + "-" + str(m[i])
>>
>> This code is part of a bigger function. It works fine. But I am not happy
>> with the way I have written it. I think there is a better (Pythonic) way
>> to
>> rewrite it.
>> If anyone knows how to improve this snippet, I would be very thankful.
>>
>
> (1) Never use "l" as a variable name, as it looks too much like 1.
>
>
> (2) Use meaningful names. When posting snippets for help, you should show
> example data.
>
>
> (3) You almost never need to iterate over an index by hand. Instead
> iterate over the items themselves. That is, instead of this:
>
> for i in len(sequence):
>do_something_with( sequence[i] )
>
> do this instead:
>
> for item in sequence:
>do_something_with( item )
>
>
> and similar variations.
>
>
> (4) When you want only part of a sequence, use slicing to extract just the
> parts you care about.
>
>
> (5) When assembling strings from substrings, never use repeated
> concatenation using + as that can be EXTREMELY slow. Use str.join to build
> the string in one assignment, instead of multiple assignments.
>
> Your code shown above is *very* inefficient and will be PAINFULLY slow if
> m is very large. To understand why, you should read this article:
>
> http://www.joelonsoftware.com/**articles/fog000319.html
>
> In this case, you can replace your snippet with this:
>
> result = '-'.join(str(item) for item in m[1:])
>
>
>
> For example:
>
> py> m = [1, 2, "hello world", (23, 42), 5]
> py> '-'.join(str(item) for item in m[1:])
> '2-hello world-(23, 42)-5'
>
>
>
> --
> Steven
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: help

2012-04-30 Thread spawgi
What does your input look like?
Please provide that information.

Regards,
- SWP

On Mon, Apr 30, 2012 at 12:30 PM, viral shah  wrote:

>
>
> I want to print below matrix.
>
> can any one suggest me the method for the same
>
> 1 2   3
> 4 5   6
> 7 8   9
>
> Thanks
>
> --
> Viral Shah
> IT Department,
> E-mail : shahviral...@gmail.com
> Mobile : (+91) 9722312220
>
>
>
>
>
>
> --
> Viral Shah
> IT Department,
> E-mail : shahviral...@gmail.com
> Mobile : (+91) 9722312220
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there space a between "#!" and "/usr/bin/env python" ?

2012-05-02 Thread spawgi
I have observed that either ways, works fine.

On Wed, May 2, 2012 at 2:15 PM, Santosh Kumar  wrote:

> Now its enough info. Thanks all for clearing my doubt.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why list is not thread-safe, but deque and queue are thread-safe?

2012-05-02 Thread spawgi
http://stackoverflow.com/questions/6319207/are-lists-thread-safe



On Wed, May 2, 2012 at 7:58 PM, Lion Chen  wrote:

> Hi, All,
> i can not understand why list is not thread-safe, but deque and queue
> are thread-safe?
>
> that's all. thanks.
>
> Lion Chen
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to exit this loop in the interpreter

2012-05-03 Thread spawgi
Hello all,

I have encountered the following scenario.
Here is the code - on IDLE on Windows XP.

*>>> while True:
try:
number = raw_input("enter number - ")
print number * number
except ValueError:
print "invalid number"
except:
print "unspecified exception"
else:
print "other conditions"


enter number - 3
unspecified exception
*
What I noticed is that no matter, what input I give, I cannot exit this
loop. I have tried control-C, control-D etc. all the keys. So how can I
exit from this loop?

Thanks and Regards,
Sumod

-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to exit this loop in the interpreter

2012-05-03 Thread spawgi
Thanks Jerry!

By the way, I know I can change the way the loops works and bypass this
issue :)
I just wanted to mention this behavior which I thought interesting and
present some corner case (I have worked extensively in testing).
And I agree, probably I should not have used color. Bad on my part.

Cheers!!
Sumod

On Thu, May 3, 2012 at 7:48 PM, Jerry Hill  wrote:

> On Thu, May 3, 2012 at 9:57 AM,   wrote:
> > Hello all,
> >
> > I have encountered the following scenario.
> > Here is the code - on IDLE on Windows XP.
> >
>  while True:
> > try:
> > number = raw_input("enter number - ")
> > print number * number
> > except ValueError:
> > print "invalid number"
> > except:
> > print "unspecified exception"
> > else:
> > print "other conditions"
> >
> >
> > enter number - 3
> > unspecified exception
> >
> > What I noticed is that no matter, what input I give, I cannot exit this
> > loop. I have tried control-C, control-D etc. all the keys. So how can I
> exit
> > from this loop?
>
> You can't, because you've painted yourself into a corner.  The bare
> "except:" line will catch any exception at all.  Including the
> KeyboardInterrupt exception that is raised when you hit control-c.  If
> you must catch unknown exceptions, but still want to allow the
> KeyboardInterrupt exception from pressing control-c to exit your
> program, then use "except Exception:"
>  instead of a bare "except:" statement.  Since KeyboardInterrupt (and
> SystemExit) are not subclasses of Exception, they won't be caught.
>
> KeyboardInterrupt and SystemExit are subclasses of a class called
> BaseException, instead of Exception, for this exact purpose.  See more
> about python's exception hierarchy here:
> http://docs.python.org/library/exceptions.html
>
> --
> Jerry
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Query - Where to put in global variables, if needed, as a good programming practice

2012-06-15 Thread spawgi
Hello,

The point of good-bad-ness of global variables aside, if I needed to use
them, which is a better place to put them.
1. In the __init__ function of a class? So they are available at the time
an object is initialized or
2. In the actual function of the class where the variables are needed?
Pros and Cons of either approach?

Thanks and Regards,
Sumod

-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Unsubscribe

2016-10-01 Thread spawgi
On Sat, Oct 1, 2016 at 8:12 AM, boB Stepp  wrote:

> On Sat, Oct 1, 2016 at 2:02 AM, Alan Gauld via Tutor 
> wrote:
> > On 01/10/16 05:24, boB Stepp wrote:
> >
> >> 
> ===
> >> '''Exerise 3.1 from "Think Python 2" by Allen Downey.
> >>
> >> This module will take a string and right justify it so that the last
> character
> >> of the line will fall in column 70 of the display.  The results will be
> >> printed to stdout.'''
> >>
> >> def right_justify(a_string):
> >  
> >> def print_msgs(*msgs):
> >> '''Prints messages to stdout.'''
> >>
> >> for msg in msgs:
> >> print(msg)
> >>
> >> def main(input_strings):
> >> '''Run main program.'''
> >>
> >> print('0123456789' * 7)# Print a number guide to check length
> of line.
> >> for input_string in input_strings:
> >> print_msgs(*right_justify(input_string))
> >
> > Do you need print_msgs()?
> > Won't it work the same with
> >
> >print(right_justify(input_string))
> >
> > You are only feeding one line at a time into the print msgs.
>
> [snip]
>
> > But I think I'd just leave it as you have it but
> > without the print_msgs()...
>
> I would still need to unpack the arguments returned by right_justify()
> that get fed to print():
>
> print(*right_justify(input_string))
>
> And I would have to add an additional "\n" to msg in the else clause
> of right_justify():
>
>  msg = ("The string has too many characters (> 70)!\n" +
> "Only a partial, 70 character line will be returned.\n")
>
> so that the result formats the same as the original intent.  This
> gives me main() now as:
>
> def main(input_strings):
> '''Run main program.'''
>
> print('0123456789' * 7)# Print a number guide to check length of
> line.
> for input_string in input_strings:
> print(*right_justify(input_string))
>
> Doing this did not occur to me because I was blind to treating
> print(*args) like any other function in regards to argument unpacking.
>
> One of the many wonderful things about Python is its consistency!
>
> Thanks, Alan!
>
>
>
> --
> boB
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HELP-Regarding python

2013-01-29 Thread spawgi
A safer approach would be -
with open(,  as :
//operation with the file.

This way, you do not have to remember to close the file explicitly.


On Wed, Jan 30, 2013 at 12:27 PM, Dave Angel  wrote:

> On 01/30/2013 01:51 AM, Gayathri S wrote:
>
>> Hi All!
>>   I don't know how to read text file in python. If the
>> data
>> values are stored in a text file format, for example(1,30,60,90,120...200)
>> means what i would do for reading it in python. could you just explain it.
>>
>>
>>
> infile = open("filename", "rt")will open a text file
>
> line = infile.readline()will read one line from it, as a str
>
> After that, you can parse it anyway you like.
>
>
>
>
> --
> DaveA
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HELP-Regarding python

2013-01-29 Thread spawgi
missed a parenthesis, it should look like -
with open(, ) as :
//operation with the file.


On Wed, Jan 30, 2013 at 12:31 PM,  wrote:

> A safer approach would be -
> with open(,  as :
> //operation with the file.
>
> This way, you do not have to remember to close the file explicitly.
>
>
> On Wed, Jan 30, 2013 at 12:27 PM, Dave Angel  wrote:
>
>> On 01/30/2013 01:51 AM, Gayathri S wrote:
>>
>>> Hi All!
>>>   I don't know how to read text file in python. If the
>>> data
>>> values are stored in a text file format, for
>>> example(1,30,60,90,120...200)
>>> means what i would do for reading it in python. could you just explain
>>> it.
>>>
>>>
>>>
>> infile = open("filename", "rt")will open a text file
>>
>> line = infile.readline()will read one line from it, as a str
>>
>> After that, you can parse it anyway you like.
>>
>>
>>
>>
>> --
>> DaveA
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>
>
>
> --
> http://spawgi.wordpress.com
> We can do it and do it better.
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor