bob gailer | 9 Feb 23:49
Picon

Re: Character Buffer Object Error OOPS

On 2/8/2012 11:04 PM, Michael Lewis wrote:
> Thanks Bob,

My Bad - I did not examine all your code - I broke my own rule.

Your code is still a little buggy.

> The below code is what I came up with without using your suggestion. 
> On a scale, how novice is mine compared to what you offered? I am 
> curious because I want to know if I should have come up with your 
> solution instead of what I did come up with.

Since you are studying Python you do well to apply what you learn. The 
first objective is to get something that works.

As you learn more you then apply new techniques.

When you have learned conditional expressions, list comprehensions and 
generator expressions then you will be able to write the code I did.

That code is just an alternative to what you wrote, "flattening it" as 
it were.
[snip]

--

-- 
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
(Continue reading)

bob gailer | 9 Feb 23:43
Picon

Re: Character Buffer Object Error

Always reply-all so a copy goes to the tutor list.

On 2/8/2012 11:04 PM, Michael Lewis wrote:
Thanks Bob,
Thanks for what if you did not follow my suggestions?

Your code is still pretty buggy.

Please test it by running it, seeing that the result is not correct, then try the desk checking.

The below code is what I came up with without using your suggestion. On a scale, how novice is mine compared to what you offered? I am curious because I want to know if I should have come up with your solution instead of what I did come up with.

def AlterInput(user_input):
    '''search for nums in str and increment/append, return new string'''
    new_output = []
    for num in user_input:
        if not num.isdigit():
            new_output.append(num)
        else:
            num.isdigit()
            new_num = int(num)
            new_num += 1
            new_output.append(str(new_num))           
    return ' '.join(new_output)

def GetUserInput():
    '''Get a string from the user and pass it'''
    user_input = '''I * got 432 when I counted, but Jim got 433 which
is a lot for only 6 cats, or were there 12 cats?'''
    return AlterInput(user_input.split())


Your code:

return ' '.join(str(int(num)+1) if num.isdigit() else num for num in user_input)

On Wed, Feb 8, 2012 at 6:04 AM, bob gailer <bgailer <at> gmail.com> wrote:
On 2/8/2012 12:56 AM, Michael Lewis wrote:
I want to find all digits in a string and then increment those digits by 1 and then return the same string with the incremented digits.
[snip]

You got lots of good advice from others and some not-so-good advice.


Michael said:
2. The following line appears wrong.
            new_output = ' '.join(user_input)
He did not say why!


I add:

Master the art of "Desk Checking".

Take pencil & paper.

Write down the input and output of each statement as though you were the computer.


Also learn to read the & understand the Python Manuals:

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Notice "return a copy". It does NOT say that this will convert old in place.

No one else caught this problem!

Since this is homework we we probably should not be offering alternative solutions.

However I can't resist offering the one-line solution:

''.join(str(int(x)+1) if x.isdigit() else x for x in 'user_input)

--
Bob Gailer
919-636-4239
Chapel Hill NC




-- Bob Gailer 919-636-4239 Chapel Hill NC
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
bob gailer | 9 Feb 23:32
Picon

Ongoing problems with Pam's new computer

Today is the third time Pam (connecting to the web site for event 
publication) has run into an inexplicable Access error.
Error 430
Class does not support Automation or does not support expected interface.
H have googled this error. None of the explanations I found relate in 
any way to what I see in the code.

Twice now I have "repaired the program" (by removing perfectly valid 
code that runs fine on other computers).

I won't do that again. I deem there is a problem with her computer or 
the Access installation. Therefore I bring this to Sarai's attention.

I recommend Pam use another computer to publish events.

--

-- 
Bob Gailer
919-636-4239
Chapel Hill NC

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

Jose Amoreira | 9 Feb 16:40
Picon
Gravatar

two-dimensional color map

I'm so, so sorry! When editing my script before posting it, I accidently deleted a line activating matplotlib interactive mode. The correct listing is the one below.

 

Hello

I'm trying to plot a false-color map of a two dimensional function. My main problem is that the function I want to plot depends also on time, and I wish to show the plot as the function evolves, in real time. My first (and only, so far) idea on how to achieve this was to iterate the computation and the plot of the values of the function on a 2-D mesh for different times.

 

Regarding the plot, I tried some options and the one that came closer to what I pretend is based on pylab (numpy and matplotlib, I guess), using the matshow() method. However, I found that the display of successive frames takes longer and longer. The following simple script shows this behaviour:

--------------------

from matplotlib.pylab import *

 

ion()

# Display a random matrix with a specified figure number

for i in range(20):

mymatrix = rand(864,864)

fig = matshow(mymatrix,fignum=0)

draw()

quit = raw_input() #wait for user input before closing graphics window

---------------------

 

So my questions are

1. Is matshow() appropriate for what I have in mind?

2. Am I using it correctly?

3. Are there better tools for plotting time-dependent 2D data using python?

 

Thank you very much

Ze Amoreira

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Jose Amoreira | 9 Feb 16:36
Picon
Gravatar

two-dimensional color map

Hello

I'm trying to plot a false-color map of a two dimensional function. My main problem is that the function I want to plot depends also on time, and I wish to show the plot as the function evolves, in real time. My first (and only, so far) idea on how to achieve this was to iterate the computation and the plot of the values of the function on a 2-D mesh for different times.

 

Regarding the plot, I tried some options and the one that came closer to what I pretend is based on pylab (numpy and matplotlib, I guess), using the matshow() method. However, I found that the display of successive frames takes longer and longer. The following simple script shows this behaviour:

--------------------

from matplotlib.pylab import *

 

# Display a random matrix with a specified figure number

for i in range(20):

mymatrix = rand(864,864)

fig = matshow(mymatrix,fignum=0)

draw()

quit = raw_input() #wait for user input before closing graphics window

---------------------

 

So my questions are

1. Is matshow() appropriate for what I have in mind?

2. Am I using it correctly?

3. Are there better tools for plotting time-dependent 2D data using python?

 

Thank you very much

Ze Amoreira

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
David Craig | 9 Feb 12:31
Picon

How to make def where arguments are either stated when called or entered via raw_input

Hi,
I'm trying to write a function that will either take arguments when the 
function is called, such as myFunc(x,y,z) or if the user does not enter 
any arguments, myFunc() the raw_input function will ask for them. But I 
dont know how to check how many arguments have been entered. My code is 
below. Anyone know how??
Thanks
D

def NoiseCorr(file1,file2,500,0.25,0.35):

#######################################################################################
### Check number of arguments
     ??????????????????

#######################################################################################
### User inputs.
     if numArgs == 0:
        file1 = raw_input('Path to Station 1: ')
        file2 = raw_input('Path to Station 2: ')
        shift_length = raw_input('Length of Correlation: ')
        freqMin = raw_input('Min. Frequency: ')
        freqMax = raw_input('Max. Frequency: ')
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Christian Witts | 9 Feb 09:34
Picon

Re: 回复: Re: What the difference between the two RE?

On 2012/02/09 10:17 AM, daedae11 wrote:
Email Signature BLOCKQUOTE { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em } OL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } UL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } DIV.FoxDiv20120209161338888588 { LINE-HEIGHT: 1.5; FONT-FAMILY: 微软雅黑; COLOR: #000080; FONT-SIZE: 10.5pt } BODY { LINE-HEIGHT: 1.5; FONT-FAMILY: 微软雅黑; COLOR: #000080; FONT-SIZE: 10.5pt }
So doesn't it means the follow two sentences can achieve the same goal?
re.match("^hello", "hello")
re.match("hello", "hello")
 
daedae11
 
发件人: Christian Witts
发送时间: 2012-02-09 16:05
主题: Re: [Tutor] What the difference between the two RE?
On 2012/02/09 09:44 AM, daedae11 wrote:
BLOCKQUOTE { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em } OL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } UL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } DIV.FoxDiv20120209154220318942 { LINE-HEIGHT: 1.5; FONT-FAMILY: &amp; COLOR: #000000; FONT-SIZE: 10.5pt; 24494: ; amp: ; 36719: ; 38597: ; 40657: } However, re.match("hello", "And they said hello" ) will also return None. So "And they said hello" also won't be matched by  the regex pattern "hello".
 
 
daedae11
 
Date: 2012-02-09 15:16
To: daedae11
Subject: Re: [Tutor] What the difference between the two RE?
On 2012/02/09 08:15 AM, daedae11 wrote:
BLOCKQUOTE { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em } OL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } UL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } P { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px }
import re
re.match("^hello", "hello")
re.match("hello", "hello")
 
Please give a string that matches RE "^hello" but does not match RE "hello", or matches RE "hello" but does not match RE "^hello".
 
daedae11


_______________________________________________ Tutor maillist - Tutor <at> python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
The caret ^ means "At the beginning of the line" so 'And they said hello' will not be matched by the regex pattern '^hello'.

The docs are pretty good on the module
http://docs.python.org/library/re.html
http://docs.python.org/howto/regex.html
--
P { FONT-FAMILY: Arial, Helvetica, sans-serif; COLOR: #000; FONT-SIZE: 8.5pt } .subscribe { FONT-STYLE: italic; FONT-WEIGHT: bold } .compuscan { TEXT-TRANSFORM: uppercase; LETTER-SPACING: 2px; COLOR: #c60c30; FONT-WEIGHT: bold } .green { COLOR: #093 }

Christian Witts
Python Developer

--
From the docs http://docs.python.org/library/re.html#re.match
Note: If you want to locate a match anywhere in string, use search() instead.
--
P { FONT-FAMILY: Arial, Helvetica, sans-serif; COLOR: #000; FONT-SIZE: 8.5pt } .subscribe { FONT-STYLE: italic; FONT-WEIGHT: bold } .compuscan { TEXT-TRANSFORM: uppercase; LETTER-SPACING: 2px; COLOR: #c60c30; FONT-WEIGHT: bold } .green { COLOR: #093 }

Christian Witts
Python Developer

If your use case is that simple the caret is not needed as .match only searches from the start of the string to see if the pattern is there.  If you need to look anywhere in your string for your pattern you should be using .search instead which if you do you can put the caret in if you just want to match if it's at the start.

>>> import re
>>> re.match('hello', 'hello there')
<_sre.SRE_Match object at 0x02324528>
>>> re.match('^hello', 'hello there')
<_sre.SRE_Match object at 0x023243D8>
>>> re.match('^hello', 'hi, hello there')
>>> re.match('hello', 'hi, hello there')
>>> re.search('hello', 'hi, hello there')
<_sre.SRE_Match object at 0x02324528>
>>> re.search('^hello', 'hi, hello there')
>>>
--
Email Signature p { font-size:8.5pt; font-family: Arial, Helvetica, sans-serif; color: #000;} .subscribe {font-weight:bold; font-style:italic;} .compuscan {color: #c60c30; letter-spacing:2px; text-transform:uppercase; font-weight:bold} .green {color:#093;}

Christian Witts
Python Developer

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
ken brockman | 9 Feb 09:07
Picon
Favicon
Gravatar

general simply question

Without sight of code it's only a guess but are you creating
the file with the 'wb' mode - ie. write binary? :-

myPickleFile = open("somefilename.dat", "wb")


oops. Not as in object orientated programming, just as in oops, i messed up. pasted wrong bit of code. Here is the relative bit.
def General_info():
file4 = open("Genfacts.p", "rb")
Ginfo = pickle.load(file4)
file4.close()
print('General Information: ')
GinfoKey = input('CatKey: ')
GinfoFact = input('Info: ')
Ginfo[GinfoKey] = GinfoFact # add new key:fact
file4 = open("Genfacts.p", "wb")
pickle.dump(Ginfo, file4)
file4.close()
return(Ginfo)
Ken
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Christian Witts | 9 Feb 09:05
Picon

Re: What the difference between the two RE?

On 2012/02/09 09:44 AM, daedae11 wrote:
Email Signature BLOCKQUOTE { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em } OL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } UL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } DIV.FoxDiv20120209154220318942 { LINE-HEIGHT: 1.5; FONT-FAMILY: &amp; COLOR: #000000; FONT-SIZE: 10.5pt; 24494: ; amp: ; 36719: ; 38597: ; 40657: } BODY { LINE-HEIGHT: 1.5; FONT-FAMILY: 微软雅黑; COLOR: #000080; FONT-SIZE: 10.5pt } However, re.match("hello", "And they said hello" ) will also return None. So "And they said hello" also won't be matched by  the regex pattern "hello".
 
 
daedae11
 
Date: 2012-02-09 15:16
To: daedae11
Subject: Re: [Tutor] What the difference between the two RE?
On 2012/02/09 08:15 AM, daedae11 wrote:
BLOCKQUOTE { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em } OL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } UL { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px } P { MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px }
import re
re.match("^hello", "hello")
re.match("hello", "hello")
 
Please give a string that matches RE "^hello" but does not match RE "hello", or matches RE "hello" but does not match RE "^hello".
 
daedae11


_______________________________________________ Tutor maillist - Tutor <at> python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
The caret ^ means "At the beginning of the line" so 'And they said hello' will not be matched by the regex pattern '^hello'.

The docs are pretty good on the module
http://docs.python.org/library/re.html
http://docs.python.org/howto/regex.html
--
P { FONT-FAMILY: Arial, Helvetica, sans-serif; COLOR: #000; FONT-SIZE: 8.5pt } .subscribe { FONT-STYLE: italic; FONT-WEIGHT: bold } .compuscan { TEXT-TRANSFORM: uppercase; LETTER-SPACING: 2px; COLOR: #c60c30; FONT-WEIGHT: bold } .green { COLOR: #093 }

Christian Witts
Python Developer

--
From the docs http://docs.python.org/library/re.html#re.match
Note: If you want to locate a match anywhere in string, use search() instead.
--
Email Signature p { font-size:8.5pt; font-family: Arial, Helvetica, sans-serif; color: #000;} .subscribe {font-weight:bold; font-style:italic;} .compuscan {color: #c60c30; letter-spacing:2px; text-transform:uppercase; font-weight:bold} .green {color:#093;}

Christian Witts
Python Developer

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
daedae11 | 9 Feb 07:15
Favicon

What the difference between the two RE?

import re
re.match("^hello", "hello")
re.match("hello", "hello")
 
Please give a string that matches RE "^hello" but does not match RE "hello", or matches RE "hello" but does not match RE "^hello".
 
daedae11
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Garry Willgoose | 9 Feb 02:46
Picon
Picon
Gravatar

bogus characters in a windows file

I'm reading a file output by the system utility WMIC in windows (so I can track CPU usage by process ID) and the
text file WMIC outputs seems to have extra characters in I've not seen before.

I use os.system('WMIC /OUTPUT:c:\cpu.txt PROCESS GET ProcessId') to output the file and parse file c:\cpu.txt

The first few lines of the file look like this in notepad

ProcessId  
0         
4          
568        
624        
648        

I input the data with the lines

infile = open('c:\cpu.txt','r')
infile.readline()
infile.readline()
infile.readline()

the readline()s yield the following output

'\xff\xfeP\x00r\x00o\x00c\x00e\x00s\x00s\x00I\x00d\x00 \x00 \x00\r\x00\n'
'\x000\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00\r\x00\n'
'\x004\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00\r\x00\n'

Now for the first line the title 'ProcessId' is in this string but the individual characters are separated
by '\x00' and at least for the first line of the file there is an extra '\xff\xfe'. For subsequent its just
'\x00. Now I can just replace the '\x**' with '' but that seems a bit inelegant. I've tried various options
on the open 'rU' and 'rb' but no effect. 

Does anybody know what the rubbish characters are and what has caused the. I'm using the latest Enthought
python if that matters.

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


Gmane