Re: [Tutor] Python Question

2013-01-09 Thread Steve Willoughby

On 2013-1月-7, at 下午3:31, Dylan Kaufman wrote:

> Greetings,
> 
> I take Computer Science in school and for a Python program, I have:
> 
> from winsound import Beep
> 

The first thing I notice is "from winsound import …"  Could that be a WINDOWS 
library which might not work on a Macintosh?

> Beep(196, 1500)#G
> Beep(262, 270)#C
> Beep(196, 200)#G
> 
> Traceback (most recent call last):
>   File "/Volumes/PICKLES/School/MyClass/Comp. Sci/Chapter 02 
> Stuff/program2_cep.py", line 5, in 
> from winsound import Beep
> ImportError: No module named 'winsound'
> 

Looking at this, /Volumes/PICKLES/School/... that looks like a Mac filesystem 
mount point name to me.  Looks like you're using something that works on 
Windows at school, then trying to use it on a non-Windows system at home.

Try looking for a sound library which works on all platforms.  Failing that, 
look for something that works on a mac.

As a side note, you probably want to replace all those Beep() calls with some 
code which reads in a set of note values as data.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using 'and ' and 'or' with integers

2013-01-09 Thread Brian van den Broek
On 9 January 2013 01:56, ken brockman  wrote:
> I was looking through some lab material from a computer course offered at UC
> Berkeley and came across some examples in the form of questions on a test
> about python.
> 1 and 2 and 3
>  answer 3
> I've goggled it till i was red in the fingers, but to no avail.. Could
> someone be kind enuff to direct me to some docs that explain this?? I've no
> clue what the hell is going on here..
> Thanks much for any help you may supply.
>


Ken,

I don't have a link, but I'll take a stab.

Any non-zero integer evaluates to True:

>>> if 42: print "!"

!

Python's boolean operators use short-circuit evaluation---they return
a value as soon as they have seen enough to know what truth-value the
compound evaluates to. And, the value they return isn't always True or
False. Rather, they return the object in virtue of which they had seen
enough to know whether the evaluated value is True or False:

>>> True or 4
True
>>> 4 or True
4

Since 4 evaluates as True, and `something True or anything at all'
will evaluate to True, in the second case 4 is returned.

Likewise, in your case:

>>> 1 and 2 and 3
3
>>> (1 and 2) and 3
3
>>> 1 and (2 and 3)
3
>>>

(I put the two bracketting ones in as I cannot recall if python
associates to the left or to the right. I'm pretty sure left, but it
doesn't matter here.) Consider the last version. Since 1 evals as
True, python has to look at the left conjunct. 2 does, too, so it has
to look at 3. Since 3 does, and python now knows the whole evals as
True, it returns 3.

HTH,

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


Re: [Tutor] using 'and ' and 'or' with integers

2013-01-09 Thread Alan Gauld

On 09/01/13 06:56, ken brockman wrote:


1 and 2 and 3
  answer 3
I've goggled it till i was red in the fingers, but to no avail.. Could
someone be kind enuff to direct me to some docs that explain this?? I've
no clue what the hell is going on here..


Its to do with the way Python evaluates booleans using short circuit 
evaluation.


There is a short section in my tutorial under the "Functional 
Propgramming" topic, in the 'other contructs heading about half way down.


HTH,

--
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


[Tutor] pexports python27.dll > python27.def (pygraphviz 1.1 package )

2013-01-09 Thread somnath chakrabarti
 I have mingw and python 2.7 in a Windows 7 box and trying to install
PyGraphViz-1.1 using the following CLI utility

python setup.py install build --compiler=mingw32

However, it ends up compiling error with undefined references as follows:

...
build\temp.win-amd64-2.7\Release\pygraphviz\graphviz_wrap.o:graphviz_wrap.c:(.text+0x5a73):
undefined reference to '_imp__PyInt_FromLong'
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

I checked in the link (see
here)
which suggests exporting definitions from C:\Windows\System32\python27.dll
to python27.def and then using dlltool to create libpython27.a and finally
placing the libpython.a file under C:\Python27\libs of the Python
distribution for MinGW to interpret Python libraries.

I have the C:\MinGW\bin added to my system path and been trying to do the
export using

pexports C:\Windows\System32\python27.dll > C:\Windows\System32\python27.def

but each time I am receiving Access is Denied Message.

I did some searching and found that MS Visual Studio users can avail
another export option with DUMPBIN but since I don't have MSVS installed, I
would like to get some alternative to get rid of the problem and need to
use the PyGraphViz-1.1 package. Any suggestions will be very helpful


Somnath Chakrabarti
MS Student
CSEE Department
University of Maryland Baltimore County
1000 Hilltop Circle
Baltimore, MD 21250
M: 443-812-5609
mail: chak...@umbc.edu
gmail: chakrabarti.somn...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Parsing and evaluating user input

2013-01-09 Thread Timo
I'm having trouble finding a safe way to parse and evaluate user input 
in my program.


In my app, I'm using a calculation like this:
  (a / b) * 100
The user should be able to override this and their preference is stored 
in a configuration file for later use. So I now have a string with the 
user defined calculation template, for example:


>>> config.get('custom-calc')
'(a * b) / 10'

I could tell the user the values should be entered like %(a)s and %(b)s 
which makes parsing easier I think, because I can do this:

>>> custom_calc = config.get('custom-calc')
>>> custom_calc
'(%(a)s * %(b)s) / 10'
>>> calc_str = custom_calc % {'a': get_value_a(), 'b': get_value_b()}

I should parse this, fill in the values which the program has at that 
point and calculate the outcome. What is the safest way?


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


Re: [Tutor] Parsing and evaluating user input

2013-01-09 Thread Oscar Benjamin
On 9 January 2013 11:20, Timo  wrote:
> I'm having trouble finding a safe way to parse and evaluate user input in my
> program.
>
> In my app, I'm using a calculation like this:
>   (a / b) * 100
> The user should be able to override this and their preference is stored in a
> configuration file for later use. So I now have a string with the user
> defined calculation template, for example:
>
 config.get('custom-calc')
> '(a * b) / 10'
>
> I could tell the user the values should be entered like %(a)s and %(b)s
> which makes parsing easier I think, because I can do this:
 custom_calc = config.get('custom-calc')
 custom_calc
> '(%(a)s * %(b)s) / 10'
 calc_str = custom_calc % {'a': get_value_a(), 'b': get_value_b()}

I don't think this '%(x)' part is really necessary. If you can parse
the arithmetic expression and compute the result, then it's easy
enough to subsitute variable names for values after parsing the
string. However, this is a big if.

>
> I should parse this, fill in the values which the program has at that point
> and calculate the outcome. What is the safest way?

A very similar question was recently asked on the python-list mailing list:
https://mail.python.org/pipermail/python-list/2013-January/637869.html

Currently there is no really safe way to do it using the standard
library but there are a number of suggestions in that thread.

My own suggestion in that thread was to use the third-party numexpr module:

>>> import numexpr
>>> int(numexpr.evaluate('(a * b) / 10', {'a':20, 'b':30}))
60

http://code.google.com/p/numexpr/


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


Re: [Tutor] writing effective unittests

2013-01-09 Thread Wayne Werner

On Thu, 3 Jan 2013, Japhy Bartlett wrote:


The general idea is to write tests that use your code in realistic ways and 
check the results.  So if you have a function that takes an input and returns
a result, you write a test that passes that function an input checks the 
result.  If some inputs should make it error, you write a test that checks that
it errors.
In web programming, a common thing is to make a web request with different 
variations of GET / PUT params, then check that it returns the right status
code, or that the result is valid JSON, etc.  Basically, try to simulate all 
the things a real world user would be able to use, and test that your code
does what you intend for it to do.

TDD is a good principle but usually seems a little too pedantic for real world 
programming.  Where tests (in my experience) get really useful is in
making sure that a new change hasn't unexpectedly broken something already 
written.


That's funny, I find it exactly the opposite - unless I'm writing some 
prototype throw-away code, doing TDD has actually exposed problems with 
the underlying program structure precisely because it *is* pedantic.


Granted, if I needed to get a fix in place in production code that was 
costing buckets of cash every minute it wasn't fixed, I'd write the code, 
push that out, and *then* write the tests... but when I've been practicing 
TDD I've not had that problem.


My experience is that it has a tendency to bundle all the development 
costs right up front - instead of a little here, a little there.



But... that's just my experience ;)
-Wayne___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pexports python27.dll > python27.def (pygraphviz 1.1 package )

2013-01-09 Thread Oscar Benjamin
Please post in plain text on this mailing list (not html).

On 9 January 2013 09:38, somnath chakrabarti
 wrote:
>
>  I have mingw and python 2.7 in a Windows 7 box and trying to install
> PyGraphViz-1.1 using the following CLI utility
>
> python setup.py install build --compiler=mingw32
>
> However, it ends up compiling error with undefined references as follows:
>
> ...
> build\temp.win-amd64-2.7\Release\pygraphviz\graphviz_wrap.o:graphviz_wrap.c:(.text+0x5a73):
> undefined reference to '_imp__PyInt_FromLong'
> collect2: ld returned 1 exit status
> error: command 'gcc' failed with exit status 1
>
> I checked in the link (see here) which suggests exporting definitions from
> C:\Windows\System32\python27.dll to python27.def and then using dlltool to
> create libpython27.a and finally placing the libpython.a file under
> C:\Python27\libs of the Python distribution for MinGW to interpret Python
> libraries.
>
> I have the C:\MinGW\bin added to my system path and been trying to do the
> export using
>
> pexports C:\Windows\System32\python27.dll > C:\Windows\System32\python27.def
>
> but each time I am receiving Access is Denied Message.

That's a system folder. You'll probably need to run that command as an
administrator or something (not really sure how that works on
Windows).

>
> I did some searching and found that MS Visual Studio users can avail another
> export option with DUMPBIN but since I don't have MSVS installed, I would
> like to get some alternative to get rid of the problem and need to use the
> PyGraphViz-1.1 package. Any suggestions will be very helpful

My suggestion is to ask somewhere else. This mailing list is for
beginners looking for help with general aspects of Python. Your
question is very specific and is about distutils, PyGraphViz and
mingw32. I suggest that you contact either the author(s) of PyGraphViz
or the distutils-sig mailing list:
https://mail.python.org/mailman/listinfo/distutils-sig


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


Re: [Tutor] pexports python27.dll > python27.def (pygraphviz 1.1 package )

2013-01-09 Thread somnath chakrabarti
Actually I embedded a link to the refer site from where I have been
following the procedure. I am new to Python too and have been following the
book "Mining the Social Web" by Matthew Russell and thought Tutor mailing
list would be the right place to post the question. Anyways will try to
post in text and more python generic questions next time. Thanks for
pointing out though!
:)
-Somnath



On Wed, Jan 9, 2013 at 6:58 AM, Oscar Benjamin
wrote:

> Please post in plain text on this mailing list (not html).
>
> On 9 January 2013 09:38, somnath chakrabarti
>  wrote:
> >
> >  I have mingw and python 2.7 in a Windows 7 box and trying to install
> > PyGraphViz-1.1 using the following CLI utility
> >
> > python setup.py install build --compiler=mingw32
> >
> > However, it ends up compiling error with undefined references as follows:
> >
> > ...
> >
> build\temp.win-amd64-2.7\Release\pygraphviz\graphviz_wrap.o:graphviz_wrap.c:(.text+0x5a73):
> > undefined reference to '_imp__PyInt_FromLong'
> > collect2: ld returned 1 exit status
> > error: command 'gcc' failed with exit status 1
> >
> > I checked in the link (see here) which suggests exporting definitions
> from
> > C:\Windows\System32\python27.dll to python27.def and then using dlltool
> to
> > create libpython27.a and finally placing the libpython.a file under
> > C:\Python27\libs of the Python distribution for MinGW to interpret Python
> > libraries.
> >
> > I have the C:\MinGW\bin added to my system path and been trying to do the
> > export using
> >
> > pexports C:\Windows\System32\python27.dll >
> C:\Windows\System32\python27.def
> >
> > but each time I am receiving Access is Denied Message.
>
> That's a system folder. You'll probably need to run that command as an
> administrator or something (not really sure how that works on
> Windows).
>
> >
> > I did some searching and found that MS Visual Studio users can avail
> another
> > export option with DUMPBIN but since I don't have MSVS installed, I would
> > like to get some alternative to get rid of the problem and need to use
> the
> > PyGraphViz-1.1 package. Any suggestions will be very helpful
>
> My suggestion is to ask somewhere else. This mailing list is for
> beginners looking for help with general aspects of Python. Your
> question is very specific and is about distutils, PyGraphViz and
> mingw32. I suggest that you contact either the author(s) of PyGraphViz
> or the distutils-sig mailing list:
> https://mail.python.org/mailman/listinfo/distutils-sig
>
>
> Oscar
>



-- 

Somnath Chakrabarti
MS Student
CSEE Department
University of Maryland Baltimore County
1000 Hilltop Circle
Baltimore, MD 21250
M: 443-812-5609
mail: chak...@umbc.edu
gmail: chakrabarti.somn...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2013-01-09 Thread eryksun
On Mon, Jan 7, 2013 at 6:31 PM, Dylan Kaufman  wrote:
>
> from winsound import Beep
>
> Beep(196, 1500)#G

winsound.Beep wraps the Windows (win32) system call of the same name.
Instead, consider using a cross-platform library such as PortAudio or
PortMidi:

http://sourceforge.net/apps/trac/portmedia

One option is to synthesize PCM audio and play it with PyAudio, which
wraps PortAudio. But you'll probably have more fun with MIDI (Musical
Instrument Digital Interface).

Installing pyPortMidi involves compiling libportmidi and the
Cython/Pyrex extension module. If you run into trouble there,
hopefully an OS X user on the list can help.

Alternatively, pygame includes pyPortMidi as pygame.midi:

pygame installers:
http://www.pygame.org/download.shtml

Demo:

import and initialize:

>>> from time import sleep
>>> from pygame import midi
>>> midi.init()

list devices:

>>> for i in range(midi.get_count()):
...   print i, midi.get_device_info(i)
...
0 ('ALSA', 'Midi Through Port-0', 0, 1, 0)
1 ('ALSA', 'Midi Through Port-0', 1, 0, 0)
2 ('ALSA', 'TiMidity port 0', 0, 1, 0)
3 ('ALSA', 'TiMidity port 1', 0, 1, 0)
4 ('ALSA', 'TiMidity port 2', 0, 1, 0)
5 ('ALSA', 'TiMidity port 3', 0, 1, 0)

open output for MIDI device 2:

>>> device = 2
>>> latency = 0
>>> out = midi.Output(device, latency)

set the channel 0 instrument to GM (General MIDI) steel drum:

>>> instrument = STEEL_DRUM = 114
>>> channel = 0
>>> out.set_instrument(instrument, channel)

play some notes:

>>> MAX_VOL = 127
>>> MID_C = 60
>>> for i in range(12):
...   out.note_on(MID_C + i, MAX_VOL, channel)
...   sleep(0.5)
...   out.note_off(MID_C + i, MAX_VOL, channel)

You can also write MIDI messages directly (0x90 + n is "note on" for channel n):

>>> out.write_short(0x90 + channel, MID_C, MAX_VOL)


General Links

MIDI and Music Synthesis
http://www.midi.org/aboutmidi/tut_midimusicsynth.php

Message Protocol
http://www.midi.org/techspecs/midimessages.php

GM Sound Set
http://www.midi.org/techspecs/gm1sound.php

Note Numbers:
http://www.midimountain.com/midi/midi_note_numbers.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Robot Radio program

2013-01-09 Thread Steven D'Aprano

On 09/01/13 12:07, Kirk Bailey wrote:

Simulates an old time radio station. Place program files in the numbered
 folders, station identifications in the ID folder, commercials in the com
folder. run from the command line.

[...]

Did you have a question, or where you just sharing the code with us?



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


Re: [Tutor] Documentation

2013-01-09 Thread Steven D'Aprano

On 07/01/13 14:48, Ed Owens wrote:

[...]

parser = HTMLParser(formatter.AbstractFormatter(
formatter.DumbWriter(cStringIO.StringIO(



HTMLParser is from htmllib.

I'm having trouble finding clear documentation for what the functions
that are on the 'parser =' line do and return. The three modules
(htmllib, formatter, & cStringIO are all imported, but I can't seem to
find much info on how they work and what they do. What this actually
does and what it produces is completely obscure to me.

Any help would be appreciated. Any links to clear documentation and
examples?


I'll start with the easiest: cStringIO, and it's slower cousin StringIO,
are modules for creating fake in-memory file-like objects. Basically they
create an object that holds a string in memory but behaves as if it were
a file object.

http://docs.python.org/2/library/stringio.html

The formatter module is used to produce an object that you can use for
creating formatted text. It's quite abstract, and to be honest I have
never used it and don't know how it works.

http://docs.python.org/2/library/formatter.html

The htmllib module is a library for parsing HTML code. Essentially, you
use it to read the content of webpages (.html files).

http://docs.python.org/2/library/htmllib.html

Unfortunately, there is not a lot of documentation for the
htmllib.HTMLParser object, so I can't help you with that.



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


[Tutor] .strip question

2013-01-09 Thread richard kappler
I have a sort of a dictionary resulting from psutil.disk_usage('/') that
tells me info about my hard drive, specifically:

usage(total=147491323904, used=62555189248, free=77443956736, percent=42.4)

I'm having a bit of a brain fudge here and can't remember how to strip out
what I want. All I want to end up with is the number percent (in this case
42.4)  I started playing with .strip but the usage term before the parens
gets in the way. If it weren't for that I could convert this into a dict
and just pull the number by the key, right? So how do I strip out the
'usage' string? Once I do that, I think I know what I'm doing but here's my
proposed code to look at if you would.

import psutil as ps

disk = ps.disk_usage('/')

# whatever I need to do to strip usage out

d = {}
for item in disk.split(','):
item = item.strip()
key, value = item.split(':')
key = key.strip()
value = value.strip()
d[key] = float(value)
return d

Mind you, this is as of yet untested code, so before you ask for
tracebacks, I can't give any until I figure out how to get rid of the
preceding 'usage'.

regards, Richard


-- 

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


Re: [Tutor] .strip question

2013-01-09 Thread Mitya Sirenef

On Wed 09 Jan 2013 01:27:26 PM EST, richard kappler wrote:

I have a sort of a dictionary resulting from psutil.disk_usage('/')
that tells me info about my hard drive, specifically:

usage(total=147491323904, used=62555189248, free=77443956736,
percent=42.4)

I'm having a bit of a brain fudge here and can't remember how to strip
out what I want. All I want to end up with is the number percent (in
this case 42.4)  I started playing with .strip but the usage term
before the parens gets in the way. If it weren't for that I could
convert this into a dict and just pull the number by the key, right?
So how do I strip out the 'usage' string? Once I do that, I think I
know what I'm doing but here's my proposed code to look at if you would.

import psutil as ps

disk = ps.disk_usage('/')

# whatever I need to do to strip usage out

d = {}
for item in disk.split(','):
item = item.strip()
key, value = item.split(':')
key = key.strip()
value = value.strip()
d[key] = float(value)
return d

Mind you, this is as of yet untested code, so before you ask for
tracebacks, I can't give any until I figure out how to get rid of the
preceding 'usage'.

regards, Richard


--

quando omni flunkus moritati



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



Is it actually a string, though? Can you do disk.percent or 
disk['percent'] ?


If it IS a string, you can get percent value like so:

# split by equal sign, strip parens from last value
print(disk.split('=')[-1].strip(')'))


- mitya


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] .strip question

2013-01-09 Thread Peter Otten
richard kappler wrote:

> I have a sort of a dictionary resulting from psutil.disk_usage('/') that
> tells me info about my hard drive, specifically:
> 
> usage(total=147491323904, used=62555189248, free=77443956736,
> percent=42.4)
> 
> I'm having a bit of a brain fudge here and can't remember how to strip out
> what I want. All I want to end up with is the number percent (in this case
> 42.4)  I started playing with .strip but the usage term before the parens
> gets in the way. If it weren't for that I could convert this into a dict
> and just pull the number by the key, right? So how do I strip out the
> 'usage' string? Once I do that, I think I know what I'm doing but here's
> my proposed code to look at if you would.
> 
> import psutil as ps
> 
> disk = ps.disk_usage('/')
> 
> # whatever I need to do to strip usage out
> 
> d = {}
> for item in disk.split(','):
> item = item.strip()
> key, value = item.split(':')
> key = key.strip()
> value = value.strip()
> d[key] = float(value)
> return d
> 
> Mind you, this is as of yet untested code, so before you ask for
> tracebacks, I can't give any until I figure out how to get rid of the
> preceding 'usage'.

Problems like this are easily attacked in the interactive interpreter. With 
dir() you can find out an object's attributes:

>>> import psutil
>>> du = psutil.disk_usage("/")
>>> du
usage(total=244020019200, used=72860221440, free=158764216320, percent=29.9)
>>> dir(du)
['__add__', '__class__', '__contains__', '__delattr__', '__dict__', 
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', 
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', 
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', 
'__subclasshook__', '_asdict', '_fields', '_make', '_replace', 'count', 
'free', 'index', 'percent', 'total', 'used']

As you can see one of du's attributes is percent, so let's have a look at 
its contents:

>>> du.percent
29.9

So that was easy, wasn't it? No need for string manipulation.

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


[Tutor] run perl script files and capture results

2013-01-09 Thread T. Girowall
New to programing and need some help. 
 
I have multiple perl script files that I would like to automate using python. 
Currently each one is ran individually and the output is manually examined. The 
perl script is ran form the command promp with arguments as follows: 
 
c:\scripts\perl>perl plscript.pl -cmnd1 -cmnd2
 
cmnd1 and cmnd2 are ran on files that reside within "perl" directory. The 
output is displayed in DOS window. 
 
My objective: 
1. Run the perl script using python
2. Capture the results from the DOS window and save it to python. Output 
consists of text names and corresponding numbers. 
 
I'm using python 2.7
 
Thank you!
Tim___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Flow charts

2013-01-09 Thread Ed Owens
I'm working my way through Chun's book "Core Python Applications 
Programming" and can't get one of the examples to actually work.  In 
trying to analyze the problem (good learning approach) I had troubles 
understanding the interactions between the two classes of objects.  As 
an old FORTRAN programmer, I picked up my pencil to flowchart the code, 
then realized I didn't know how to flowchart an OOP.


Google led me to UML (Unified Modeling Language) and OMG (apparently Oh 
My God!!!).  Looks more complicated than the code I'm trying to understand.


It there a technique that people use to figure out how do design OOP 
models, objects, and the information flow?


Thanks for helping a newby.
Ed
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flow charts

2013-01-09 Thread Mitya Sirenef

On Wed 09 Jan 2013 09:26:15 PM EST, Ed Owens wrote:

I'm working my way through Chun's book "Core Python Applications
Programming" and can't get one of the examples to actually work.  In
trying to analyze the problem (good learning approach) I had troubles
understanding the interactions between the two classes of objects.  As
an old FORTRAN programmer, I picked up my pencil to flowchart the
code, then realized I didn't know how to flowchart an OOP.

Google led me to UML (Unified Modeling Language) and OMG (apparently
Oh My God!!!).  Looks more complicated than the code I'm trying to
understand.

It there a technique that people use to figure out how do design OOP
models, objects, and the information flow?

Thanks for helping a newby.
Ed
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




Here's what I do sometimes:

1. write down the steps that need to be done
2. write pseudo-code for the steps
3. revise
4. make one single class with many methods. If you are familiar with
 programming with just functions, this is the same but instead of
 functions you have methods.
5. note which methods should logically be split up into separate
 classes & refactor


Once you do this with a few programs, you'll be able to start
with several classes right away, without the need for intermediary
'main' class.

I haven't used diagrams to design in a long time so I won't comment
on that.

HTH, - m




--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flow charts

2013-01-09 Thread Dave Angel
On 01/09/2013 09:26 PM, Ed Owens wrote:
> I'm working my way through Chun's book "Core Python Applications
> Programming" and can't get one of the examples to actually work.  In
> trying to analyze the problem (good learning approach) I had troubles
> understanding the interactions between the two classes of objects.  As
> an old FORTRAN programmer, I picked up my pencil to flowchart the
> code, then realized I didn't know how to flowchart an OOP.
>
> Google led me to UML (Unified Modeling Language) and OMG (apparently
> Oh My God!!!).  Looks more complicated than the code I'm trying to
> understand.
>
> It there a technique that people use to figure out how do design OOP
> models, objects, and the information flow?
>
> Thanks for helping a newby.
> Ed
> 

You're probably making it more complicated than it needs to be.  A class
is simply an organized way to associate a collection of data, with
functions (methods) that operate on that data.  You instantiate the
class one or more times to create one or more objects. And when you
invoke the method on a particular object, it has access to all the data
in that particular object.

Many times the class represents a bunch of data that is analogous to
real life information.  So a class might name a natural collection, like
a person, and the data members of that class are attributes that exist
for all persons, but have different values.  So you might have an
attribute for hair color, value brown, and an attribute for height,
value 68 inches.  A separate instance describing a different person
would have the same attributes, but different values for them.

A method on such a class is something that affects one or more
attributes, or that returns information derived from one or more
attributes.  For example, the die() method might change the hair color
attribute.

Many times the class is more abstract than that, but the attributes
still should be related to each other in some useful way.  So a file
class represents a file on disk, and contains attributes like filename,
seek-position, mode, and buffer, and has methods like read, write, seek.

HTH.



-- 

DaveA

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


[Tutor] How to run multiline shell command within python

2013-01-09 Thread Karim



Hello all,

I want to run multiline shell command within python without using a 
command file but directly execute several lines of shell.
I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but 
I want to know if it is posssible to avoid making file and execute

shell lines of code directly.

Example:

cat< myfile
echo "foo"
echo "bar"
...
EOF

Regards

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


Re: [Tutor] run perl script files and capture results

2013-01-09 Thread eryksun
On Wed, Jan 9, 2013 at 8:14 PM, T. Girowall  wrote:
>
>
> c:\scripts\perl>perl plscript.pl -cmnd1 -cmnd2
>
> cmnd1 and cmnd2 are ran on files that reside within "perl" directory.
>
> My objective:
> 1. Run the perl script using python
> 2. Capture the results from the DOS window and save it to python. Output
> consists of text names and corresponding numbers.
>
> I'm using python 2.7

subprocess.check_output returns piped stdout (optionally including
stderr) from a process

http://docs.python.org/2/library/subprocess#subprocess.check_output

universal_newlines=True replaces the Windows CRLF ('\r\n') line
endings with LF ('\n').

To set the working directory use cwd='c:/scripts/perl' (see [1]).

On Windows you can pass args as a string if you need non-standard
quoting. If you use a list instead, subprocess builds a string quoted
according to Microsoft's rules:

http://docs.python.org/2/library/subprocess#converting-argument-sequence

If the call fails check_output raises subprocess.CalledProcessError:

http://docs.python.org/2/library/subprocess#subprocess.CalledProcessError

For example:

import subprocess

output = subprocess.check_output(
['perl.exe', 'plscript.pl', '-cmnd1', '-cmnd2'],
universal_newlines=True,
cwd='c:/scripts/perl',
)

Windows CreateProcess will search for 'perl.exe' on the system PATH.
For increased security you can use an absolute path such as
'C:/perl/bin/perl.exe'.

1. Using a forward slash in paths is OK for DOS/Windows system calls
(e.g. opening a file or setting the cwd of a new process), dating back
to the file system calls in MS-DOS 2.0 (1983). Otherwise a backslash
is usually required (e.g. shell commands and paths in commandline
arguments, where forward slash is typically used for options). In this
case use a raw string or os.path.join. For a raw string, note that a
trailing backslash is not allowed, e.g. r'C:\Python27\'. Instead you
could use r'C:\Python27' '\\', among other options:

http://docs.python.org/2/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor