[Tutor] Next steps...

2014-07-08 Thread Ni Hung
ok, I am now able to write scripts in python. I can read/modify scripts
written by others (true to some level).  I understand the basics of
libraries (how are they different from modules or are the same thing with
two names?)  like urllib2, json, sys, os etc. and have used them in some
scripts.

What should I do next to advance my knowledge of python? Should I study/use
libraries/modules?  Which ones?  Any other suggestions?

Thanks and Regards
Nii
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SyntaxError Message

2014-07-08 Thread Danny Yoo
 I have no programming experience and am trying to  teaching myself python.
 Am trying to replicate the code below but I get the error message below
 highlighted in yellow:


We need a little more information.  Where does this code come from?
What text book or instructional material are you using?


I ask because the code you're doing is unusual looking, and appears to
be out-of-context.  To be more specific, the following part of your
program:

return input (1)

doesn't make too much sense in isolation.

The errors you're seeing are due to the fish-out-of-waterness of the
code: the return statement only makes sense if you're writing a
function definition, but if you're just getting started, you probably
haven't learned about how to write function definitions yet.

If you can point us to the instructional material you're looking at,
we might give better suggestions as to what's missing.

But alternatively, have you tried the following tutorial instead?

http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3

It may be a gentler introduction to the language than what you're
looking at now.


Good luck!  If you have more questions, please feel free to ask.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SyntaxError Message

2014-07-08 Thread Danny Yoo
Following up.  I can guess that you're looking at:

http://www.sthurlow.com/python/lesson05/

But I would like not to guess unless I have no choice. In the future,
if you're asking for help, provide context.  The reason why it's very
useful to say where you're learning from is because we can then look
at previous things you have seen before, and try to relate those past
things with what you're doing now.

In this case, now that I know the context more, I can look at the
material myself and see if it makes sense.  Sometimes tutorials can be
confusingly written.

Ah.  In particular, the Code Example 9 in the link above is not
presented well.  I say this because it is not even a complete program!
 And yet the author is notating it as if it were a complete program by
putting it in its own figure.  If you tried to do Code Example 9, it
would raise the exact same error messages you're seeing now.

Hmm... I have other reservations about this tutorial.  It's presumably
trying to teach functions, but it is not really using the functions as
things that return useful values.  add(), sub(), mul(), and div() in
Code Example 14 are not great examples of functions.


I think you may want to switch to a different tutorial and see if it
is easier to approach.  Since you're using Python 2, I have to
backtrack on the tutorial I linked in a previous message: I needed to
have referred to a tutorial that used Python 2, not Python 3.  My
apologies!

Here is a link to a good tutorial:
http://openbookproject.net/thinkcs/python/english2e/

Please try that one instead and see if it makes more sense.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Next steps...

2014-07-08 Thread Danny Yoo
On Mon, Jul 7, 2014 at 8:51 PM, Ni Hung niih...@gmail.com wrote:
 ok, I am now able to write scripts in python. I can read/modify scripts
 written by others (true to some level).  I understand the basics of
 libraries (how are they different from modules or are the same thing with
 two names?)  like urllib2, json, sys, os etc. and have used them in some
 scripts.

 What should I do next to advance my knowledge of python? Should I study/use
 libraries/modules?  Which ones?  Any other suggestions?


This is hard to say since it depends on your learning style.

Some folks tend to really integrate what they've learned by doing a
project.  Do you have any interesting ideas of something you'd like to
do?  Any hobbies?

For example, I like playing Go (aka Baduk), so sometimes I look into
writing programs that are related to Go in some way.  I'm planning to
write a program to parse the file format used to record Go games, the
Smart Game Format, or SGF format.  Such programs already exist, of
course!  But reinvention is fine when we're learning.  And rather than
write it in Python, I'm thinking of writing it in the Go programming
language, for maximal confusion.  :P


So that's one possibility.  Another possibility is doing something
like The Python Challenge.  Have you seen the following?

http://www.pythonchallenge.com/

For some people, the Python Challenge web site helps them cement their
programming a little more.


The others on this list will have more suggestions; I'm sure of it.
:P  Good luck.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SyntaxError Message

2014-07-08 Thread Dave Angel
Pamela Wightley pamela.wight...@morgij.com.au Wrote in message:
 
 

This is a text mailing list.  So leaving an html message blocks
 some of us from seeing what you intended.  Color and other
 formatting vanishes for many.  Please tell your email program to
 post in text mode.

The problem with your fragment is that the return statement only
 makes sense inside a function,  but you don't begin with a def
 statement. 

-- 
DaveA

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


Re: [Tutor] SyntaxError Message

2014-07-08 Thread Alan Gauld

On 08/07/14 02:41, Pamela Wightley wrote:

Hi All,

I have no programming experience and am trying to  teaching myself
python. Am trying to replicate the code below but I get the error
message below highlighted in yellow:


Unfortunately I can't see anything in yellow, probably due to enmail 
losing the formatting.


But the code below has several errors and looks like its part
of a bigger program. It also looks completely unrelated to the
errors you show us further down.



choice = input(Choose your option: )


You seem to be using Python 2 in which case using input() is
frowned upon. Its better to use raw_input() and convert the
resultant string to a number using int() or float() as needed.
[ In Python 3 input() has been rwe oved and raw_input renamed to input()...]



 if choice == 1:


indentation(spacing) is very importanbt in Python.
You should only indent the code inside a code block that follows a 
structural statement such as if/for/while/def/class etc.


Indenting the if statement after the input() will raise an error.


 add1 = input(Add this: )



But this should e indented because its part of the if block.



 add2 = input(to this: )

 print add1, +, add2, =, add1 + add2

 elif choice == 2:

 sub2 = input(Subtract this: )

 sub1 = input(from this: )

 print sub1, -, sub2, =, sub1 - sub2

 elif choice == 3:

 mul1 = input(Multiply this: )

 mul2 = input(with this: )

 print mul1, *, mul2, =, mul1 * mul2

 elif choice == 4:

 div1 = input(Divide this: )

 div2 = input(by this: )

 print div1, /, div2, =, div1 / div2

 elif choice == 5:

 loop = 0

Any assistance appreciated.

ERROR MESSAGE


return input (1)


   File stdin, line 1

SyntaxError: 'return' outside function



This seems completely unrelated to the choice code?
You can only use return inside a function (ie a block
starting def)


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Next steps...

2014-07-08 Thread Alan Gauld

On 08/07/14 04:51, Ni Hung wrote:


libraries (how are they different from modules or are the same thing
with two names?)


In Python libraries and modules are nearly the same. Libraries are a 
concept in Python which are realized using modules (or packages).


In other languages libraries are the reality and modules are the logical 
concept.


And in still other languages you have modules and libraries where 
libraries are (usually binary) files containing several code modules.


Some day computing science will get around to making its language 
consistent!



What should I do next to advance my knowledge of python? Should I
study/use libraries/modules?  Which ones?  Any other suggestions?


Danny suggested a project or the Python challenge.
I'd agree with those two suggestions, and personally I'd
favour a project. It doesn't matter what it is - don't be too
ambitious, but something bigger than a single file of code.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


[Tutor] Tkinter resizable menu??

2014-07-08 Thread Albert-Jan Roskam
Hi,

I created a small menu with Tkinter. It has two listboxes: a source and a 
destination box. The user can select files from the source box and 'move' them 
to the destination box. It now does what I want, except for one thing: when I 
resize it, the listboxes will not (*will* not, grrr) increase in size.

I followed these instructions: 
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/root-resize.html


I added background colors to the various frames to see what's happening. I 
pasted the code here because it is a bit much (sorry): 
http://pastebin.com/waeACc4B. Any idea what I am doing wrong in the function 
createListbox (line 80)? I am using sticky=NESW in the grid() calls. 

Thank you in advance!


Regards,

Albert-Jan




~~

All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 

fresh water system, and public health, what have the Romans ever done for us?

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


Re: [Tutor] Tkinter resizable menu??

2014-07-08 Thread Albert-Jan Roskam


 

 I added background colors to the various frames to see what's happening. I 
 pasted the code here because it is a bit much (sorry): 
 http://pastebin.com/waeACc4B. Any idea what I am doing wrong in the function 
 createListbox (line 80)? I am using sticky=NESW in the grid() calls. 

PS: The trailing dot was not part of the URL: http://pastebin.com/waeACc4B
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter resizable menu??

2014-07-08 Thread Peter Otten
Albert-Jan Roskam wrote:

 I created a small menu with Tkinter. It has two listboxes: a source and a
 destination box. The user can select files from the source box and 'move'
 them to the destination box. It now does what I want, except for one
 thing: when I resize it, the listboxes will not (*will* not, grrr)
 increase in size.
 
 I followed these instructions:
 http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/root-resize.html
 
 
 I added background colors to the various frames to see what's happening. I
 pasted the code here because it is a bit much (sorry):
 http://pastebin.com/waeACc4B. Any idea what I am doing wrong in the
 function createListbox (line 80)? I am using sticky=NESW in the grid()
 calls.

You must explicitly tell the row/column to grab the extra space:

def createListbox(self, row, column, name, contents):
...
self.listboxframe.grid(row=row, column=column, sticky=self.sticky)
self.listboxframe.columnconfigure(column, weight=1)
self.listboxframe.rowconfigure(row, weight=1)
...

See http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html

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


[Tutor] subprocess.call not formatting date

2014-07-08 Thread Bob Williams
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

I'm using Python 2.7.6 on an openSUSE linux system.

I'm trying to convert a shell (bash) script to a python script, and 
everything's worked OK except this. The following line in the shell script

btrfs subvolume snapshot /home/bob/A3/documents /home/bob/A3/docsnaps/`date 
+%y-%m-%d_%H-%M`

creates a folder of the form

/home/bob/A3/docsnaps/14-07-08_17-32

ie. the name is a formatted date string, which is what I want.

In my python script, this

subprocess.call('btrfs', 'subvolume', 'snapshot', '/home/bob/A3/documents', 
'/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`')

creates a folder

/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`

In other words, it does not format the date, but takes the stuff between the 
backticks (`) as a string.

I tried adding shell=True, but got the following error:

Traceback (most recent call last):
  File /home/bob/bin/btrfs-backup.py, line 55, in module
subprocess.call('btrfs', 'subvolume', 'snapshot', '/home/bob/A3/documents', 
'/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`', shell=True)
  File /usr/lib64/python2.7/subprocess.py, line 522, in call
return Popen(*popenargs, **kwargs).wait()
  File /usr/lib64/python2.7/subprocess.py, line 658, in __init__
raise TypeError(bufsize must be an integer)
TypeError: bufsize must be an integer

Any suggestions, please?
- -- 
Bob Williams
System:  Linux 3.11.10-17-desktop
Distro:  openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.2
Uptime:  06:00am up 2 days 9:43, 5 users, load average: 0.00, 0.02, 0.05
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlO8H9gACgkQ0Sr7eZJrmU7fdACgkBqVXT+Ozb+XqmEFwhPBdmeX
NcgAnjY6YrbXcmUTAvgLPblk4rOWFAdH
=vfIY
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.call not formatting date

2014-07-08 Thread Peter Otten
Bob Williams wrote:

 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1
 
 I'm using Python 2.7.6 on an openSUSE linux system.
 
 I'm trying to convert a shell (bash) script to a python script, and
 everything's worked OK except this. The following line in the shell script
 
 btrfs subvolume snapshot /home/bob/A3/documents
 /home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`
 
 creates a folder of the form
 
 /home/bob/A3/docsnaps/14-07-08_17-32
 
 ie. the name is a formatted date string, which is what I want.
 
 In my python script, this
 
 subprocess.call('btrfs', 'subvolume', 'snapshot',
 '/home/bob/A3/documents', '/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`')
 
 creates a folder
 
 /home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`
 
 In other words, it does not format the date, but takes the stuff between
 the backticks (`) as a string.
 
 I tried adding shell=True, but got the following error:
 
 Traceback (most recent call last):
   File /home/bob/bin/btrfs-backup.py, line 55, in module
 subprocess.call('btrfs', 'subvolume', 'snapshot',
 '/home/bob/A3/documents', '/home/bob/A3/docsnaps/`date
 +%y-%m-%d_%H-%M`', shell=True)
   File /usr/lib64/python2.7/subprocess.py, line 522, in call
 return Popen(*popenargs, **kwargs).wait()
   File /usr/lib64/python2.7/subprocess.py, line 658, in __init__
 raise TypeError(bufsize must be an integer)
 TypeError: bufsize must be an integer

That's because the second argument to call() is bufsize, and you are passing
it the string subvolume. While I suppose that

# untested
subprocess.call(btrfs subvolume snapshot /home/bob/A3/documents 
/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`, shell=True)

would work I suggest that you calculate the folder name in Python instead:

# untested
name = datetime.datetime.now().strftime(%y-%m-%d_%H-%M)
destpath = os.path.join(/home/bob/A3/docsnaps, name)
subprocess.call(
[btrfs, subvolume, snapshot, /home/bob/A3/documents, destpath])


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


Re: [Tutor] subprocess.call not formatting date

2014-07-08 Thread Emile van Sebille

On 7/8/2014 9:44 AM, Bob Williams wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

I'm using Python 2.7.6 on an openSUSE linux system.

I'm trying to convert a shell (bash) script to a python script, and 
everything's worked OK except this. The following line in the shell script

btrfs subvolume snapshot /home/bob/A3/documents /home/bob/A3/docsnaps/`date 
+%y-%m-%d_%H-%M`

creates a folder of the form

/home/bob/A3/docsnaps/14-07-08_17-32

ie. the name is a formatted date string, which is what I want.

In my python script, this

subprocess.call('btrfs', 'subvolume', 'snapshot', '/home/bob/A3/documents', 
'/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`')

creates a folder

/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`

In other words, it does not format the date, but takes the stuff between the 
backticks (`) as a string.

I tried adding shell=True, but got the following error:

Traceback (most recent call last):
   File /home/bob/bin/btrfs-backup.py, line 55, in module
 subprocess.call('btrfs', 'subvolume', 'snapshot', 
'/home/bob/A3/documents', '/home/bob/A3/docsnaps/`date +%y-%m-%d_%H-%M`', 
shell=True)
   File /usr/lib64/python2.7/subprocess.py, line 522, in call
 return Popen(*popenargs, **kwargs).wait()
   File /usr/lib64/python2.7/subprocess.py, line 658, in __init__
 raise TypeError(bufsize must be an integer)
TypeError: bufsize must be an integer

Any suggestions, please?


Pass in the formatted string using the python datetime module.

datetime.datetime.today().strftime(%y-%m-%d_%H-%M)

see https://docs.python.org/2/library/datetime.html for more details.

Emile



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


Re: [Tutor] subprocess.call not formatting date

2014-07-08 Thread Bob Williams
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 08/07/14 18:12, Peter Otten wrote:
 I suggest that you calculate the folder name in Python instead:
 
 # untested name =
 datetime.datetime.now().strftime(%y-%m-%d_%H-%M) destpath =
 os.path.join(/home/bob/A3/docsnaps, name) subprocess.call( 
 [btrfs, subvolume, snapshot, /home/bob/A3/documents,
 destpath])

Thank you. That worked perfectly. Once it's pointed out it is obvious
to use the python solution, but my brain is becoming less agile as I
collect more birthdays!

Bob
- -- 
Bob Williams
System:  Linux 3.11.10-17-desktop
Distro:  openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.2
Uptime:  06:00am up 2 days 9:43, 5 users, load average: 0.00, 0.02, 0.05
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlO8Nw8ACgkQ0Sr7eZJrmU6HVwCaAkT+nqxn818s1Di8mgqc9U1a
qksAni7exn27xTGgDV2O6vSNtg8FbgMK
=9I7G
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter resizable menu??

2014-07-08 Thread Alan Gauld

On 08/07/14 16:46, Albert-Jan Roskam wrote:

I pasted the code here because it is a bit much (sorry):


Too much for me, I gave up without spotting the problem.

One thing that did strike me though was that you spend quite
a lot of code setting up scrollbars etc on your list boxes.

The Tix module has a scrollable listbox widget which is
quite easy to use and does all that stuff for you.

Simply replace

import Tkinter as tk

with

import Tix as tk

and it should just work as is.
Then replace the list box and scrollers with the
ScrolledListBox widget.

Much less work.
Tix is not very well documented but the ScrolledListBox
(and ScrolledText) are two of the better examples.


Also all that messing around with lambdas etc outside the
widget creation would actually be easier to read if you
just used the lambda inside the command=argument and
referenced the actual method. Having two levels of
redirection simply makes the reader have to search
your code to find out what it really is...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


[Tutor] How can I open and use gnome-terminal from a Python script?

2014-07-08 Thread Jim Byrnes

I would like to automate running virtualenv with a python script by:

opening gnome-terminal
cd to proper directory
run source /bin/activate

I found some examples of using os.system() to get gnome-terminal to open 
but I can't figure out how then cd to the proper directory in the new 
terminal.


My biggest frustration right now is that I can't find any documentation 
on how to use os.system().  Looking at the docs on the Python site I 
don't see system() under the os module.  Googling hasn't helped.


Thanks,  Jim

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


Re: [Tutor] How can I open and use gnome-terminal from a Python script?

2014-07-08 Thread Danny Yoo
You might look at:


http://askubuntu.com/questions/351582/open-terminal-window-and-execute-python-script-on-startup

http://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu

both which appears relevant to your question.

If I understand your question correctly, you're trying to open up a
new terminal with gnome-terminal, and within that new terminal,
execute some additional set of instructions in the context of that
terminal.  If so, then you'll probably have to open up the
gnome-terminal in such a way that it knows what to do next by itself,
without further interaction with your original Python program.  So
when you say:

opening gnome-terminal
cd to proper directory
run source /bin/activate

then I see a secondary terminal_script.py in charge of doing:

cd to proper directory
run source /bin/activate

and your primary script will be doing the initial call to do

gnome-terminal -e python terminal_script.py

to finish the rest of the computation you're planning.


As a side note, consider using the subprocess module instead of
os.system().  
https://docs.python.org/2/library/subprocess.html#module-subprocess.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Next steps... :p:

2014-07-08 Thread Paradox

On 07/08/2014 11:51 AM, Ni Hung wrote:
 What should I do next to advance my knowledge of python? Should I 
study/use libraries/modules?  Which ones?  Any other suggestions?


 Thanks and Regards
 Nii

Oops, replied to sender and not to the list ...

Sounds like you are ready to start solving real problems.  Pick 
something that interests you and dig in.  If you don't have any pressing 
problems to solve then look for the myriad of problems sites to get some 
training problems.  Here are a few of my favorites:


1. pythonchallenge.com (good fun)
2. checkio.org (my current favorite)
3. projecteuler.net (good if you are interested in math oriented puzzles)
4. https://github.com/karan/Projects/ (long list of puzzles to solve)
5. https://github.com/gregmalcolm/python_koans/wiki - You are probably 
passed this but I like koans for some easy fun and to keep sharp when I 
have had an interruption in learning
6. 
http://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/ 
- there is a github repo for solutions for these as well


There are many such puzzle collections out there, these are just the 
ones with which I have experience.


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


Re: [Tutor] How can I open and use gnome-terminal from a Python script?

2014-07-08 Thread Alan Gauld

On 08/07/14 21:45, Jim Byrnes wrote:

I would like to automate running virtualenv with a python script by:

opening gnome-terminal
cd to proper directory
run source /bin/activate


Thats almost certainly the wrong approach.
Instead of trying to automate what the user does in the terminal replace 
the terminal with Python.


Run the cd command from within Python (os.chdir())
Run Activate from within Python (this is where os.system()
could be used, but subprocess.call() is considered better
practice.


I found some examples of using os.system() to get gnome-terminal to open
but I can't figure out how then cd to the proper directory in the new
terminal.


You can't. os.system() just runs a command it has no way to interaxct5 
with the command, you do that manually. Its perfectly fine for 
displaying a directory listing (although os.listdir() would be better) 
or sending a file to a printer, or even opening a terminal/editor
for the user to interact with. But its no good for your program 
interacting with it. Thats what subprocess is for.




My biggest frustration right now is that I can't find any documentation
on how to use os.system().  Looking at the docs on the Python site I
don't see system() under the os module.


You should, although they don;t have a lot to say...

--
os.system(command)

Execute the command (a string) in a subshell. This is implemented by 
calling the Standard C function system(), and has the same limitations. 
Changes to sys.stdin, etc. are not reflected in the environment of the 
executed command. If command generates any output, it will be sent to 
the interpreter standard output stream.


On Unix, the return value is the exit status of the process encoded in 
the format specified for wait(). Note that POSIX does not specify the 
meaning of the return value of the C system() function, so the return 
value of the Python function is system-dependent.


On Windows, the return value is that returned by the system shell after 
running command. The shell is given by the Windows environment variable 
COMSPEC: it is usually cmd.exe, which returns the exit status of the 
command run; on systems using a non-native shell, consult your shell 
documentation.


The subprocess module provides more powerful facilities for spawning new 
processes and retrieving their results; using that module is preferable 
to using this function. See the Replacing Older Functions with the 
subprocess Module section in the subprocess documentation for some 
helpful recipes.


Availability: Unix, Windows.
---


 Googling hasn't helped.


When you know the module use the browser search tools on the page itself 
- that's how I found it. I searched for os.system and it

was the 3rd occurence.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] How can I open and use gnome-terminal from a Python script?

2014-07-08 Thread Cameron Simpson

On 08Jul2014 15:45, Jim Byrnes jf_byr...@comcast.net wrote:

I would like to automate running virtualenv with a python script by:

opening gnome-terminal
cd to proper directory
run source /bin/activate

I found some examples of using os.system() to get gnome-terminal to 
open but I can't figure out how then cd to the proper directory in the 
new terminal.


You basicly can't do that, in that specific order, easily via system(); it runs 
a shell command.  It also waits for it to complete; you don't say if that is 
required.


But like Alan, I think you're doing it in the wrong order. I would do the cd 
and source activate, then start the gnome-terminal.


You can do that as one shell command like this:

  cd /the/proper/directory; . /path/to/bin/activate; gnome-terminal

Such a string you can hand to os.system().

There are some obvious things to consider here:

  - proper quoting of the directory path and the path to activate; they may 
have almost anything in them and care is needed

  - do you intent to wait for the terminal to exit before proceeding?

Regarding the former item: for this reason it would be more robust and safer to 
do the cd (os.chdir) and activation in python. But please get the shell version 
working first to ensure it does what you want it to do.


My biggest frustration right now is that I can't find any documentation on how 
to use os.system().  Looking at the docs on the Python site I don't see 
system() under the os module.  Googling hasn't helped.


os.system should be documented in the os module.

But it just takes a shell command, so aside for handling the fork/exec-of-sh 
-c your_command-wait, there is little to document.


Cheers,
Cameron Simpson c...@zip.com.au
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I open and use gnome-terminal from a Python script?

2014-07-08 Thread Steven D'Aprano
On Tue, Jul 08, 2014 at 03:45:42PM -0500, Jim Byrnes wrote:
 I would like to automate running virtualenv with a python script by:
 
 opening gnome-terminal
 cd to proper directory
 run source /bin/activate

Why not just run /bin/activate directly from Python?

os.chdir(the/proper/directory)
os.system(/bin/activate)

 I found some examples of using os.system() to get gnome-terminal to open 
 but I can't figure out how then cd to the proper directory in the new 
 terminal.
 
 My biggest frustration right now is that I can't find any documentation 
 on how to use os.system().  Looking at the docs on the Python site I 
 don't see system() under the os module.  Googling hasn't helped.

https://docs.python.org/2/library/os.html#os.system

I found it by going to the os module, then using my browser's Find In 
This Page (not google!) function to look for os.system, then clicking 
the very first link that came up.

Basically, if you can type a command at the shell prompt, you can run 
the same thing using os.system. There are a few complications due to the 
fact that each call to os.system operates in isolation from any other 
call to os.system e.g. this won't touch the file /tmp/foo:

os.system(cd /tmp)
os.system(touch foo)

but this will:

os.system(cd /tmp; touch foo)

but otherwise it is as simple as it gets.



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


[Tutor] Unpacking lists

2014-07-08 Thread Robert Nanney
Hello All,

I have the following code.  The idea is to have one list that contains
all of the items from the different iterable types and maintain the
order of the items.

Python 2.7.6 |Anaconda 2.0.0 (x86_64)| (default, May 27 2014, 14:58:54)

#!/usr/bin/python
#list_test2.py

list1 = [1, 8, 15]
list2 = [2, 9, 16]
list3 = [[3, 4, 5, 6], [10, 11, 12, 13], [17, 18, 19, 20]]
list4 = [7, 14, 21]
one_list = zip(list1, list2, list3, list4)
print Start: {}.format(one_list)
first_round = [one_list[x][y] for x in range(len(list3)) for y in range(4)]
print First Round: {}.format(first_round)
second_round = []
for i in first_round:
if not isinstance(i, list):
second_round.append(i)
else:
for x in range(len(i)):
second_round.append(i[x])
print Second round: {}.format(second_round)

While this seems to do the trick, I feel there is probably a
better/more pythonic way to accomplish the same thing.

One thing to keep in mind is that the number of items in each list
will always be the same, ie... if list1, list2, list4 have 4 items
each, there will be 4 lists in list3.

Any advice would be greatly appreciated!

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


Re: [Tutor] How can I open and use gnome-terminal from a Python script?

2014-07-08 Thread Cameron Simpson

On 09Jul2014 11:42, Steven D'Aprano st...@pearwood.info wrote:

On Tue, Jul 08, 2014 at 03:45:42PM -0500, Jim Byrnes wrote:

I would like to automate running virtualenv with a python script by:

opening gnome-terminal
cd to proper directory
run source /bin/activate


Why not just run /bin/activate directly from Python?

os.chdir(the/proper/directory)
os.system(/bin/activate)


Because activate needs to be sourced by the shell. It hacks the environment.

Cheers,
Cameron Simpson c...@zip.com.au

If at first you don't succeed, your sky-diving days are over.
- Paul Blumstein, pa...@harley.tti.com, DoD #36
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor