Re: [Tutor] lc_ctype and re.LOCALE

2016-01-29 Thread eryk sun
On Thu, Jan 28, 2016 at 2:23 PM, Albert-Jan Roskam
 wrote:
> Out of curiosity, I wrote the throw-away script below to find a character 
> that is classified
> (--> LC_CTYPE) as digit in one locale, but not in another.

The re module is the wrong tool for this. The re.LOCALE flag is only
for byte strings, and in this case only ASCII 0-9 are matched as
decimal digits. It doesn't call the isdigit() ctype function. Using
Unicode with re.LOCALE is wrong. The current locale doesn't affect the
meaning of a Unicode character. Starting with 3.6 doing this will
raise an exception.

The POSIX ctype functions such as isalnum and isdigit are limited to a
single code in the range 0-255 and EOF (-1). For UTF-8, the ctype
functions return 0 in the range 128-255 (i.e. lead bytes and trailing
bytes aren't characters). Even if this range has valid characters in a
given locale, it's meaningless to use a Unicode value from the Latin-1
block, unless the locale uses Latin-1 as its codeset.

Python 2's str uses the locale-aware isdigit() function. However, all
of the locales on my Linux system use UTF-8, so I have to switch to
Windows to demonstrate two locales that differ with respect to
isdigit(). You could use PyWin32 or ctypes to iterate over all the
locales known to Windows, if it mattered that much to you.

The English locale (codepage 1252) includes superscript digits 1, 2, and 3:

>>> locale.setlocale(locale.LC_CTYPE, 'English_United Kingdom')
'English_United Kingdom.1252'
>>> [chr(x) for x in range(256) if chr(x).isdigit()]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\xb2', '\xb3', '\xb9']
>>> unicodedata.name('\xb9'.decode('1252'))
'SUPERSCRIPT ONE'
>>> unicodedata.name('\xb2'.decode('1252'))
'SUPERSCRIPT TWO'
>>> unicodedata.name('\xb3'.decode('1252'))
'SUPERSCRIPT THREE'

Note that using the re.LOCALE flag doesn't match these superscript digits:

>>> re.findall(r'\d', '0123456789\xb2\xb3\xb9', re.LOCALE)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

The Windows Greek locale (codepage 1253) substitutes "Ή" for superscript 1:

>>> locale.setlocale(locale.LC_CTYPE, 'Greek_Greece')
'Greek_Greece.1253'
>>> [chr(x) for x in range(256) if chr(x).isdigit()]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\xb2', '\xb3']

>>> unicodedata.name('\xb9'.decode('1253'))
'GREEK CAPITAL LETTER ETA WITH TONOS'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I test file operations (Such as opening, reading, writing, etc.)?

2016-01-29 Thread Alan Gauld
On 29/01/16 07:09, Ben Finney wrote:

>>   Create a directory and copy your pristine tree into the directory
>>   and make the base directory for the important data files
>>   configurable. Also known as parameterization.
> 
> I consider that a poor option; real files complicate the test setup and
> cleanup, they are always going to be slower, and they can lead to false
> positives and false negatives in the test results.

There are pros and cons to all the options.

File simulation is faster and fine for unit testing functionality,
but not so good for testing the overall system. Simulated files
can lead to false positives precisely because they are faster
than real files. Recognising slow access times - especially
on big data sets - are a vital part of testing any application.

In an ideal world parameterize so that you use file simulation
for basic functional unit testing and then use a dummy real file
system for integration, performance and system testing (and
probably user testing too). Finally, acceptance testing will
likely have to run on the real (actual) filesystem.

Having configurable test environments is pretty much essential.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
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 do I test file operations (Such as opening, reading, writing, etc.)?

2016-01-29 Thread Ben Finney
"Martin A. Brown"  writes:

> Option A (tempfile):
>
>   Create a directory and copy your pristine tree into the directory
>   and make the base directory for the important data files
>   configurable. Also known as parameterization.

I consider that a poor option; real files complicate the test setup and
cleanup, they are always going to be slower, and they can lead to false
positives and false negatives in the test results.

All of those will act as a disincentive to add lots of unit tests for
filesystem-related areas of your code; and I think that's a disincentive
we don't need.

> Option D (use StringIO):
>
>   If you are less concerned about filesystem interactions and really 
>   just want an individual thingy that that behaves like a file, but 
>   can be constructed in memory, use StringIO (or cStringIO).

These are great if you can get away with them. But many programs use not
just file objects, but also interrogate the filesystem: they use
`os.stat`, `os.path.exists`, builtin `open`, etc.

We need to be able to control not only the in-memory file objects, but
the filesystem access for our fake files that don't actually exist on
the real filesystem.

> Option C (use pyfakefs):
>
>   Use pyfakefs, which acts like a filesystem for testing purposes.
>   https://pypi.python.org/pypi/pyfakefs
>   https://github.com/jmcgeheeiv/pyfakefs

I have today published a different approach, which I designed for one
code base ad is to date its only user. I'm looking for more feedback.



See the ‘doc/tutorial.txt’ for a worked example of how it can be useful.
(and I'm still writing more documentation for general use.)

-- 
 \   “I do not believe in immortality of the individual, and I |
  `\consider ethics to be an exclusively human concern with no |
_o__)  superhuman authority behind it.” —Albert Einstein, letter, 1953 |
Ben Finney

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


Re: [Tutor] How do I test file operations (Such as opening, reading, writing, etc.)?

2016-01-29 Thread Alan Gauld
On 29/01/16 04:58, boB Stepp wrote:

> I'll ask you the same question that I asked Danny:  Do you have a
> favorite text which teaches OOP the way you feel it should be taught?
> And if possible, Python-based?

I have three; depending on your level of interest :-)

1) OO Design by Grady Booch.
But it has to be the first edition - try your library.
He teaches it by way of projects, each in a different
language, showing how to adapt the pure OOP ideas to
the target language.(Smalltalk, Object Pascal, C++,
ADA and Lisp) (He also teaches his design notation
which you can almost ignore because UML has replaced it.)

2) OOA by Coad & Yourdon. A lightweight( intro to OO
concepts without much code at all. Purely at the
analysis level. They also did an OOD book which does
feature some code and focuses on how to partition
an app but the OOA book is better.

3) OO Software Construction by B Meyer (2nd edition
this time) The daddy of OOP books. Its massive
(1200+pages), very detailed and at quite an academic
level but covers every facet of OOP you can imagine.
But its in Eiffel - Meyers own language which is
possibly the best (as in clean and complete) software
engineering language ever invented but in practice
hardly ever used.

I'd start with Coad. But borrow from a library rather
than buy (unless you can get it for pennies on Amazon
marketplace etc). But Booch is probably better overall
for practical application of the ideas and discussing
how OOP applies at the code level. Myer is for the
pure theory side of things.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
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] lc_ctype and re.LOCALE

2016-01-29 Thread Oscar Benjamin
On 28 January 2016 at 20:23, Albert-Jan Roskam  wrote:
>
> Out of curiosity, I wrote the throw-away script below to find a character 
> that is classified (--> LC_CTYPE) as digit in one locale, but not in another.
> I ran it with 5000 locale combinations in Python 2 but did not find any 
> (somebody shut down my computer!). I just modified the code so it also
> runs in Python 3. Is this the correct way to find such locale-dependent regex 
> matches?

Eryk already gave you a better explanation of the locale stuff than I
could but I have a separate comment about the algorithmic performance
of your code (since you mentioned that it took a long time).

You're looping over all pairs of locales:

...
> for n, (locale1, locale2) in enumerate(itertools.combinations(locales, 2), >
...
> for i in xrange(sys.maxunicode + 1):   # 1114111
> s = unichr(i)  #.encode("utf8")
> try:
> locale.setlocale(locale.LC_CTYPE, locale1)
> m1 = bool(regex.match(s))
> locale.setlocale(locale.LC_CTYPE, locale2)
> m2 = bool(regex.match(s))
> if m1 ^ m2:  # m1 != m2

Suppose there are N locales and M is sys.maxunicode. The number of
pairs of locales is N*(N-1)/2 which grows like N**2. For each pair you
loop over M characters so the innermost loop body is repeated
something like M*N**2 times.

Assume that f(locale, c) is the function that gets e.g. m1 or m2 in
your code above. We can swap the loops around so that the outer loop
is over unicode characters. Then the inner loop can be over the
locales but we only loop over all N locales once rather than over all
N**2 pairs of locales. This looks like this:

for c in unicode_chacters:
matched = f(locales[0], c) # Check the first locale
for locale in locales:
assert all(f(locale, c) == matched for locale in locales)

This way you call f(locale, c) M*N times which if N is not small
should be a lot faster than M*N**2 times.

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


[Tutor] http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html

2016-01-29 Thread Albert-Jan Roskam


from sqlalchemy import *

db = create_engine('sqlite:///joindemo.db')

db.echo = True

metadata = BoundMetaData(db)

users = Table('users', metadata, autoload=True)
emails = Table('emails', metadata, autoload=True)

# These are the empty classes that will become our data classes
class User(object):
pass
class Email(object):
pass

usermapper = mapper(User, users)
emailmapper = mapper(Email, emails)

session = create_session()

mary = session.query(User).selectfirst(users.c.name=='Mary')
mary.age += 1

session.flush()

fred = User()
fred.name = 'Fred'
fred.age = 37

print "About to flush() without a save()..."
session.flush()  # Will *not* save Fred's data yet

session.save(fred)
print "Just called save(). Now flush() will actually do something."
session.flush()  # Now Fred's data will be saved

session.delete(fred)
session.flush()   
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Noob: nested if-clauses

2016-01-29 Thread Alan Gauld
On 29/01/16 17:41, STF wrote:

> what that gives.  IDLE is indeed easier to use than the "DOS-style" Python
> command-line window.  

One thing I'd say is that you can tweak the DOS CMD window quite
a lot to make it better for IDLE(and other interpreters). But
IDLE is probably still better for most folks.

> How do you call this thing, BTW?  I'm unable to
> understand how to navigate inside this thing.  

I'm not totally clear what you mean here.
You start IDLE from the start menu or desktop shortcut.
That gets you to the interactive shell which is a version
of the python interpreter running inside idle

> I mean, when I open it, in which folder am I in?

You can find out with

>>> import os
>>> print ( os.getcwd() )

But its rarely important because you normally use
File->Open to open a file, or File->New to create a new
one and save it wherever you want).

Once you do that IDLE opens a new editor (as opposed
to shell) window. From there you can use the Run menu
to execute your code with the output appearing in the
original shell window.

> Suppose I have a Python file in
> D:\mycode\abc\myfile.py.  How to run it?

Within IDLE use File->Open to locate and load it.
Then use the Run->Riun Module menu command (shortcut F5)

Outside idle use

C:\SOME\PATH> python D:\mycode\abc\myfile.py

You can of course create a shortcut to do that.

or CD to the folder:

C:\SOME\PATH> CD  D:\mycode\abc
D:
D:\mycode\abc>

then type

D:\mycode\abc> python myfile.py

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
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 do I test file operations (Such as opening, reading, writing, etc.)?

2016-01-29 Thread Ben Finney
Alan Gauld  writes:

> File simulation is faster and fine for unit testing functionality,
> but not so good for testing the overall system.

Yes, I'm only advocating fake files for unit tests which, more than all
other kinds of tests, need to be fast and numerous.

For testing larger parts of the system than a single code unit, real
files are better. But likewise, there should be fewer tests (because
complete-coverage unit tests should be much more numerous) so the slower
performance is not so much a problem.

-- 
 \“I got fired from my job the other day. They said my |
  `\  personality was weird. … That's okay, I have four more.” |
_o__)   —Bug-Eyed Earl, _Red Meat_ |
Ben Finney

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


Re: [Tutor] Noob: nested if-clauses

2016-01-29 Thread John Wong
On Fri, Jan 29, 2016 at 3:13 PM, Alan Gauld 
wrote:

>
> When I first heard that python was white-space sensitive I
> thought, oh no! But now I see it as a huge strength of the
> language. Once you get used to it you will find it helps
> far more than it hinders - just avoid tabs and/or map your
> tab key to spaces.
>
>
Just want to emphasize: use a tool that actually works for you and for the
programming tool. In general, any modern IDE is capable of doing the right
thing. NotePad++ is also capable, but avoid anything like WordPad or
NotePad, if they are still around...
Lastly, most projects I encounter do use 4-space, but some projects (and
notably Google projects internally) would use 2-space, so you almost never
have to worry about mixing 4-space and 2-space in your code. It just happen
that sometimes some people publish code under tab or 2 space and if you
just copy-paste you can run into issues, just FYI for the beginners.

Thanks.

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


Re: [Tutor] Noob: nested if-clauses

2016-01-29 Thread STF
On 25 January 2016 at 21:46, Alan Gauld  wrote:

> On 25/01/16 15:52, STF wrote:
>
> > It's a total fluke.  I put the indentation like this to *visually* help
> > myself understand what I was going to write.
>
> That's one of the good things about Python, if it looks right
> it very often is right.
>

Actually, in the original example code I type on notepad, I was using
tabs.  But since I can't press Tab inside Gmail interface, I pressed spaces
instead.

My incomprehension is partially due to this YouTube video:
https://youtu.be/W1zOj2CI-KQ (@ 7:00) in which the author didn't insist on
"consistency".

Another reason is that, while tab is interpreted as 4 white spaces in
convention, it's shown as 8 white spaces in Notepad.  So when I opened some
source code, I have different numbers of leading white spaces, which lead
to my confusion.

Personally, I don't find this as a "good thing".  It rather recalls the
horrible dreams I have had when I was using Fortran!  In Fortran, we have
to deal with position of first characters to make things work.  IMO, making
a visual format an essential thing in programming is a very bad idea, if
it's not superficial.



> > In the Python tutorial that I was using, the author only told us to use
> > indentation, without emphasizing on the size of it.
>
> Quite right the amount is not important(syntactically at least) provided
> you are consistent.
>
> > As I'm a newbie, I'm mostly using Python IDLE but sometimes I would use
> > Programmer's Notepad.
>
> I don't know PN but IDLE will keep you right most of the time.
>
> > Let me ask an alternative question.  Suppose I have something like this:
> > 
> >
> > if condition_C:
> > instruction_10
> >instruction_11
> >  instruction_12
> > 
> > There are 4 spaces in front of instruction_10, 3 spaces in front of
> > instruction_11 and 5 spaces in front of instruction_12.
> >
> > What would happen to instruction_11 and instruction_12?
>
> One of the best things about Python is the interpreter.
> Just try it and see. It's much faster than posting a question
> here and you can be sure it's the correct answer! If you
> don't understand what you see, then come here.
>
> Just use some print statements or simple assignments
> for example:
>
> >>> if True:
> ...print 'in the if'
> ...   print 'still here'
> ...  y = 5 * 6
> ...
>
> what happens?
>

OK, I have just tried it (instead of just reading source codes) and I see
what that gives.  IDLE is indeed easier to use than the "DOS-style" Python
command-line window.  How do you call this thing, BTW?  I'm unable to
understand how to navigate inside this thing.  I mean, when I open it, in
which folder am I in?  Suppose I have a Python file in
D:\mycode\abc\myfile.py.  How to run it?

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


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-29 Thread Steven D'Aprano
On Fri, Jan 22, 2016 at 11:10:39PM -0600, boB Stepp wrote:

> Am I understanding this correctly?  It appears to me that you have
> successfully added a method to class X from outside of class X!  If
> this is the case, then this clarifies some things I was wondering
> about in my other thread.


If you are looking for a real-world example of why you might add a 
method to an instance, please look at the thread started by Ryan:

"Why define a function inside a function?"


for an example of adding a pseudo-method to a specific instance (not the 
entire class).

For technical reasons, it's not actually a method, it's a regular 
function, but the difference is not important.


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


Re: [Tutor] Why define a function inside a function?

2016-01-29 Thread Ben Finney
Ryan Smith  writes:

> In trying to understand the logic behind the code for my own
> edification, I was wondering what is the reasoning of defining a
> function in side another function (for lack of a better phrase)?

Sometimes a function is so specific to the workings of another function
that it would not make sense outside that context.

Other times, the information needed to define the function – for
example, a default value for one of the parameters – may be dynamically
determined within the enclosing function. The statement defining the
inner function would not work without that context.

Yet other times, the inner function may be simpler to express by making
use of the enclosing scope of the outer function. Names in the enclosing
function can be referenced in the inner function, without needing to
formally pass them as parameters. Defining the function at the point of
its use keeps the code readable, and keeps the function simpler.

Lastly, functions themselves are objects like any other in Python.
Defining one function inside another, and then using the defined
function object as a value to pass on for further processing, is a
paradigm known as “functional programming” which Python has good support
for. This is how it's done.

-- 
 \“Like the creators of sitcoms or junk food or package tours, |
  `\ Java's designers were consciously designing a product for |
_o__)   people not as smart as them.” —Paul Graham |
Ben Finney

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


Re: [Tutor] Why define a function inside a function?

2016-01-29 Thread Steven D'Aprano
On Fri, Jan 29, 2016 at 04:29:14PM -0500, Ryan Smith wrote:
> Hi all,
> 
> I am new to programming and python and had a question. I hope I
> articulate this well enough so here it goes... I was following along
> on a thread on this mailing list discussing how to test file I/O
> operations. In one of the suggested solutions the following code was
> used:
> 
> 
> def _open_as_stringio(self, filename):
> filelike = StringIO(self.filecontents[filename])
> real_close = filelike.close
> def wrapped_close():
> self.filecontents[filename] = filelike.getvalue()
> real_close()
> filelike.close = wrapped_close
> return filelike

This function (actually a method) creates and returns a new object which 
behaves like an actual file. When that fake file's "close" method is 
called, it has to run some code which accesses the instance which 
created it (self). How does this fake file know which instance created 
it? It has to "remember" the value of self, somehow.

There are two usual ways for a function to be given access to a variable 
(in this case, self):

- pass it as a parameter to the function;
- access it as a global variable.

The first can't work, because fake_file.close() takes no arguments.

The second won't work safely, unless you can guarantee that there is 
only ever one fake file at a time. If there are two, they will both try 
to access different values of self in the same global, and bad things 
will happen. ("Global Variables Considered Harmful.")

But there's a third way, and that's called "a closure". And this is 
exactly what happens here. Let's look at the inner function more 
carefully:

def wrapped_close():
self.filecontents[filename] = filelike.getvalue()
real_close()


"wrapped_close" takes no arguments, but it references four variables 
inside the body:

- self
- filename
- filelike
- real_close

Each of those variables are defined in the surrounding function. So when 
Python creates the inner function, it records a reference to those outer 
variables inside the inner function. These are called "closures", and we 
say "the inner function closes over these four variables". (I don't know 
why the word "close" is used.) So the word "closure" can be used either 
for the inner function itself, or the internal hidden links between it 
and the variables.

In other words:

The inner function "wrapped_close" remembers the values of those four 
variables, as they were at the time the inner function was defined.

So by the time you eventually get hold of the fake file, and call its 
close method, that close method will remember which instance created the 
fake file, etc, and use those variables as needed.

Complicated? Well, a bit. Here's a simpler demonstration of a closure:


def factory(num):
"""Factory function that returns a new function that adds 
num to whatever it is given."""
def adder(x):
return x + num


add1 = factory(1)
add2 = factory(2)
add17_25 = factory(17.25)

print(add1(100))
# => prints 101

print(add2(50))
# => prints 52

print(add17_25(5000))
# => prints 5017.25


Each of the "add" functions remember the value that "num" had, *at the 
time of creation*. So as far as add1 is concerned, num=1, even though 
add2 considers that num=2.

(Warning: be careful with mutable values like lists, since they can 
change value if you modify them. If this makes no sense to you, please 
ask, and I'll demonstrate what I mean.)


 
> In trying to understand the logic behind the code for my own
> edification, I was wondering what is the reasoning of defining a
> function in side another function (for lack of a better phrase)?

There are two common reasons for defining inner functions.

The first is a boring and not-very-exciting reason: you want to hide 
that function from anyone else, prevent them from using it, except for 
one specific function. So you put it inside that function:

def example(n):
results = []
def calc(x):
# Pretend that calc is actually more complex.
return x*100
for i in range(1, n+1):
results.append(calc(i))
return results


example(3)
# => returns [100, 200, 300]


That's a fairly boring reason, and people rarely do that. Normally, you 
would just make calc an external function, perhaps named "_calc" so 
people know it is a private function that shouldn't be used.

The second reason is to make the inner function a closure, as shown 
above.



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


Re: [Tutor] Why define a function inside a function?

2016-01-29 Thread Steven D'Aprano
Oops, I screwed up!


On Sat, Jan 30, 2016 at 02:31:09PM +1100, Steven D'Aprano wrote:

> Complicated? Well, a bit. Here's a simpler demonstration of a closure:
> 
> 
> def factory(num):
> """Factory function that returns a new function that adds 
> num to whatever it is given."""
> def adder(x):
> return x + num


Of course that can't work, because I forgot to return the inner 
function.

def factory(num):
def adder(x):
return x + num
return adder


will work better. Sorry for any confusion.



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


[Tutor] Why define a function inside a function?

2016-01-29 Thread Ryan Smith
Hi all,

I am new to programming and python and had a question. I hope I
articulate this well enough so here it goes... I was following along
on a thread on this mailing list discussing how to test file I/O
operations. In one of the suggested solutions the following code was
used:


def _open_as_stringio(self, filename):
filelike = StringIO(self.filecontents[filename])
real_close = filelike.close
def wrapped_close():
self.filecontents[filename] = filelike.getvalue()
real_close()
filelike.close = wrapped_close
return filelike

In trying to understand the logic behind the code for my own
edification, I was wondering what is the reasoning of defining a
function in side another function (for lack of a better phrase)?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Noob: nested if-clauses

2016-01-29 Thread boB Stepp
On Fri, Jan 29, 2016 at 11:41 AM, STF  wrote:

> ...How do you call this thing, BTW?  I'm unable to
> understand how to navigate inside this thing.  I mean, when I open it, in
> which folder am I in?  Suppose I have a Python file in
> D:\mycode\abc\myfile.py.  How to run it?

IDLE presents itself in two ways.  Normally it defaults to opening up
as the Python interpreter where you see the ">>>" and can type in code
interactively.

Its other view can be accessed by going to the "File" menu and either
choosing "Open..." and browsing to the file you wish to open, such as
"D:\mycode\abc\myfile.py" you mention above, or by creating a new file
with the menu option "New File".  This will open a second window which
is an editor window that you can type in and edit Python code, such as
you were doing in Programmer's Notepad.  To run the code you have
either opened using "Open..." or that you have typed in, use the menu
"Run" and select "Run Module".

Having both of these windows open at the same time is quite handy!  As
you type code into the Editor, you may get stuck and not be sure as to
what you want to do next.  You can then go to the Interpreter window
and type in code snippets, exploring Python's behavior for the code
you are contemplating.  Once you are satisfied, back to the Editor and
enter your code.  Of course you can copy and paste between the two as
well.

Notice that the menu bar has differences between the two windows.  You
should explore the various menu options to see what you can do with
both forms of IDLE.  Don't forget to check out "Help > IDLE Help" for
more information on IDLE's options.  Also note that you can get to the
local Python language docs through "Help > Python Docs".

One more thing to note:  IDLE has shortcut keys.  As you explore the
menus you will see some of them listed next to the menu entries.  For
more go to "Options > Configure IDLE" and you will see a small tabbed
window.  If you go to the "Keys" tab, you can see (and edit) the key
bindings IDLE uses as well as available pre-configured key binding
styles.

Hopefully I'm answering the question(s) you are really asking and have
not gone off on a tangent!


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


Re: [Tutor] Noob: nested if-clauses

2016-01-29 Thread Alan Gauld
On 29/01/16 17:41, STF wrote:

> Personally, I don't find this as a "good thing".  It rather recalls the
> horrible dreams I have had when I was using Fortran!  In Fortran, we have
> to deal with position of first characters to make things work.  IMO, making
> a visual format an essential thing in programming is a very bad idea, if
> it's not superficial.

I've never done Fortran but I did do COBOL which similarly is fussy
about spacing. But trust me, Python is much more flexible and friendly
in its  use of whitespace. It essentially just asks you to do what
you should be doing anyways.

And the way it works avoids most of the dangling else errors
you get in languages like C/Java etc (And I had >10 years
experience with those before discovering python.)

When I first heard that python was white-space sensitive I
thought, oh no! But now I see it as a huge strength of the
language. Once you get used to it you will find it helps
far more than it hinders - just avoid tabs and/or map your
tab key to spaces.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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