Re: [Tutor] Problem Stripping

2012-04-02 Thread Steven D'Aprano
Please keep your reply on the list, unless you have something private to say. 
That way others can help, or learn from your questions. I've taken the liberty 
of putting this back into the list.



Leam Hall wrote:

On 04/01/2012 09:40 PM, Steven D'Aprano wrote:

leam hall wrote:

Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
but it doesn't seem to work like I thought.


res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()


For future reference, you should identify the shortest possible amount
of code that demonstrates the problem. See also http://sscce.org/

You are asking a question about string.strip(). Where the string comes
from is irrelevant -- there's no need to use an example as complicated
as the above, when a simple string constant will do the job perfectly.
So the shortest possible amount of code to demonstrate your problem is a
single method call:

 >>> 'spam ham 1:2:3 eggs'.strip(':')
'spam ham 1:2:3 eggs'


All the stuff with subprocess.Popen and reading from stout and whatnot
doesn't have anything to do with the issue at hand. It cannot shed any
light on the problem; at best it must be ignored, at worst it may
confuse the issue.

As others have already explained, strip() does not remove characters
from anywhere in the string, it strips them from the ends only.

There is also a lstrip() and rstrip() for times you only want to remove
them from the left or right side.


Steve,

How does one minimize the example code when one doesn't know the 
problem? The short example seemed to work, the long one didn't. So the 
options seemed to be that the long code changed the expected behavior, 
the long code was transcribed incorrectly, or I misunderstood something. 
At the point of asking the question I didn't know which of those was the 
issue.


That's a good question. Being able to diagnose errors or unexpected behaviour 
is an absolutely critical skill for a programmer. So how could you have 
diagnosed this (apparent) error?


(Apart from reading the Fine Manual, of course.)

You started off by reading some data with the subprocess module. So the first 
thing to do is to replace the calls


res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()

with a single assignment using the exact same string:

uname = 'Linux ando 2.6.18-53.el5 #1 SMP Mon Nov 12 02:22:48 EST 2007 i686 
i686 i386 GNU/Linux'


(The above should be a single line; my mail client wraps it over two lines.)

Does the problem still exist? Yes:

>>> uname.strip(':')  # I expect the colons to disappear.
'Linux ando 2.6.18-53.el5 #1 SMP Mon Nov 12 02:22:48 EST 2007 i686 i686 i386 
GNU/Linux'


And lo, the colons don't disappear. The "problem" (as you saw it) continues. 
Clearly the problem has nothing to do with the *source* of the string. (It 
would be a bizarre and strange situation if it did!)


Step two: Simplify simplify simplify. Why use 85 characters when fewer than a 
dozen will do? Cut out all the irrelevant bits of the string:


>>> uname = '02:22:48'
>>> uname.strip(':')
'02:22:48'

No change in the mysterious behaviour.

At this point you have your short, simple example:

>>> '02:22:48'.strip(':')  # I expect '022248'
'02:22:48'


and you can ask for help. Or if you are keen to solve this yourself, you could 
try different strings and see if you can work out what's going on:


>>> '0222:48'.strip(':')  # maybe it works with only 1 colon?
'0222:48'

>>> '022248:'.strip(':')  # or perhaps if I move the colon somewhere else?
'022248'

>>> ':02:22:48:'.strip(':')  # which colons will be removed?
'02:22:48'




--
Steven

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


[Tutor] New to Python programing

2012-04-02 Thread Walter Luna
Hi everybody, my name is Walter I am a new beginner with no programing 
experience. I am using OSX Lion, and successfully installed Python version 3. I 
am looking for a programing guide for  beginners with no programing experience. 
Can you please suggest me a good one to use. Thank you.

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


Re: [Tutor] New to Python programing

2012-04-02 Thread Shane Keene
I don't currently use Python 3 and don't recommend that you use it to
learn with, mostly because the bulk of the docs and learning resources
are Python 2.x focused and the two are not compatible. That said, here
are some resources that you may find useful (particularly if you choose
to learn using 2.x):

Learn Python the Hard Way: http://learnpythonthehardway.org/

How to Think Like a Computer Scientist:
http://openbookproject.net/thinkcs/python/english2e/

and http://python.org/doc has a wealth of info as does
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

hope that's helpful.

On Mon, 2012-04-02 at 17:03 -0700, Walter Luna wrote:
> Hi everybody, my name is Walter I am a new beginner with no programing 
> experience. I am using OSX Lion, and successfully installed Python version 3. 
> I am looking for a programing guide for  beginners with no programing 
> experience. Can you please suggest me a good one to use. Thank you.
> 
> Walter 
> ___
> 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] group txt files by month

2012-04-02 Thread questions anon
I think what I am trying to do is relatively easy but can't get my head
around how to do it.
I have a list of txt files that contain daily rainfall for many years. I
would like to produce a list that contains the year-month and the max, min
and mean of rainfall for each month.
My main question at this stage is how to group the files by each month for
each year?
They are set out like:
r20110101.txt
r20110102.txt
r20110103.txt
r20110104.txt
r20110105.txt
r20110106.txt
r20110107.txt
r20110108.txt
r20110109.txt
r20110110.txt
r20110111.txt
r20110112.txt
r20110113.txt
r20110114.txt
r20110115.txt
r20110116.txt
r20110117.txt
r20110118.txt

and so on for each day for many years.

so far I am able to open each file and calculate the max, min and mean for
each file (see below) but not sure about grouping to monthly for each year.

MainFolder=r"E:/Rainfalldata/"
outputFolder=r"E:/test/"
for (path, dirs, files) in os.walk(MainFolder):
path=path+'/'
for fname in files:
if fname.endswith('.txt'):
filename=path+fname
f=np.genfromtxt(filename, skip_header=6)
print f.max(), f.min(), f.mean()

the ideal output would be something like:
year-month  max   min   mean
2010-12 100  0  50
2011-01  200 0  100
2011-02  50   0  25


any feedback will be greatly appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] group txt files by month

2012-04-02 Thread Asokan Pichai
On Tue, Apr 3, 2012 at 9:29 AM, questions anon  wrote:
> I think what I am trying to do is relatively easy but can't get my head
> around how to do it.
> I have a list of txt files that contain daily rainfall for many years. I
> would like to produce a list that contains the year-month and the max, min
> and mean of rainfall for each month.
> My main question at this stage is how to group the files by each month for
> each year?
> They are set out like:
> r20110101.txt
> r20110102.txt
> r20110103.txt
> r20110104.txt
> r20110105.txt
> r20110106.txt
> r20110107.txt
> r20110108.txt
> r20110109.txt
> r20110110.txt
> r20110111.txt
> r20110112.txt
> r20110113.txt
> r20110114.txt
> r20110115.txt
> r20110116.txt
> r20110117.txt
> r20110118.txt
>
> and so on for each day for many years.
>
> so far I am able to open each file and calculate the max, min and mean for
> each file (see below) but not sure about grouping to monthly for each year.

# -
Monthwise = {}
# --

> MainFolder=r"E:/Rainfalldata/"
> outputFolder=r"E:/test/"
> for (path, dirs, files) in os.walk(MainFolder):
>     path=path+'/'
>     for fname in files:
>     if fname.endswith('.txt'):
>     filename=path+fname
>     f=np.genfromtxt(filename, skip_header=6)
>     print f.max(), f.min(), f.mean()

Replace the last two lines with
# --
   Monthwise[fname[1:7]] = .np.genfromtxt(filename, skip_header=6)
# -

Now at the end you have a dictionary whose keys are the strings of type '201012'
and the values are the f.

You can now iterate over the sorted keys of Monthwise
and print appropriately

[Ideal output etc SNIPPED]

HTH

Asokan Pichai

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python programing

2012-04-02 Thread wesley chun
greetings walter, and welcome to the Python family!

it looks like you've got your machine all set up. hopefully installing
Python 3 wasn't too difficult -- users constantly have issues with
their own installs clash with the Python that's pre-installed by
Apple.

as far as learning Python for beginners goes, you have to decide what
version to learn -- since you have both Python 2 & 3 on your system,
you have a choice. if you have existing code that's written in Python
2.x, you should learn that first. if you have "no baggage," then
Python 3.x is the way to go as it is the future.

regardless of which you pick, you should realize: 1) once you learn
one, you will learn the other as there are only seemingly minor (but
backwards-incompatible differences), 2) most books and online
materials are still in Python 2 although more and more Python 3
materials are becoming available.

as far as books go, the best way to learn Python is by writing games.
this is an approach that works both with children as well as adults.
there are several excellent books that can help you with this regard:

- Hello World! Computer Programming for Kids and Other Beginners by
Warren Sande and Carter Sande, Manning,
- Invent your Own Computer Games with Python second edition by Al Sweigart
- How to Think Like a Computer Scientist by Allen B. Downey, Jeff
Elkner and Chris Meyers, Green Tea Press
- Python Programming for the Absolute Beginner by Michael Dawson,
Course Technology

i go into a bit more detail on these as well as the books shane
recommends in my "Python Reading List" article (which actually
describes 3 separate reading lists):
http://www.informit.com/articles/article.aspx?p=1849069

as far as online resources go, try these:

- How to Think Like a Computer Scientist (Downey, Elkner, Meyers)
http://www.openbookproject.net/thinkcs/
- Learning to Program (Gauld)
http://www.alan-g.me.uk/l2p
- LiveWires Python
http://www.livewires.org.uk/python/home
http://pythongames.weebly.com/livewires.html
- Snake Wrangling for Kids (Briggs)
http://www.briggs.net.nz/snake-wrangling-for-kids.html
http://code.google.com/p/swfk/
- Computer Programming is Fun! (Handy)
http://www.handysoftware.com/cpif/
- Karel the Robot clone: Guido van Robot
http://gvr.sf.net
http://en.wikipedia.org/wiki/Guido_van_Robot
- Karel the Robot clones: RUR-PLE
http://rur-ple.sf.net
http://en.wikipedia.org/wiki/RUR-PLE
- A Byte of Python (Swaroop)
http://www.swaroopch.com/notes/Python
- Instant Hacking: Learning to Program with Python (Hetland)
http://hetland.org/writing/instant-hacking.html

hope this all helps, and again, welcome to Python!!
--wesley


On Mon, Apr 2, 2012 at 5:03 PM, Walter Luna  wrote:
> Hi everybody, my name is Walter I am a new beginner with no programing 
> experience. I am using OSX Lion, and successfully installed Python version 3. 
> I am looking for a programing guide for  beginners with no programing 
> experience. Can you please suggest me a good one to use. Thank you.
>
> Walter


-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    wesley chun : wescpy at gmail : @wescpy/+wescpy
    Python training & consulting : CyberwebConsulting.com
    "Core Python" books : CorePython.com
    Python blog: wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor