Re: [Tutor] myown.getfilesystemencoding()

2013-08-31 Thread Oscar Benjamin
On 30 August 2013 17:39, eryksun eryk...@gmail.com wrote:
 On Fri, Aug 30, 2013 at 11:04 AM, Albert-Jan Roskam fo...@yahoo.com wrote:

 the function returns 850 (codepage 850) when I run it via the command prompt,
 but 1252 (cp1252) when I run it in my IDE (Spyder).

 Maybe Spyder communicates with python.exe as a subprocess in a hidden
 console, with the console's codepage set to 1252. You can use ctypes
 to check windll.kernel32.GetConsoleCP(). If a console is attached,
 this will return a nonzero value.

Spyder has both an internal interpreter and an external interpreter.
One is the same interpreter process that runs the Spyder GUI. The
other is run in a subprocess which keeps the GUI safe but reduces your
ability to inspect the workspace data via the GUI. So presumable
Albert means the external interpreter here. Also Spyder has the
option to use ipython as the shell for (I think) either interpreter
and ipython does a lot of weirdness to stdin/stdout etc. (according to
the complaints of the Spyder author when users asked for ipython
support).


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


[Tutor] A mergesort

2013-08-31 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I have been searching for mergesort implimentations in python and came
across
this.

def merge(a, b):
if len(a)*len(b) == 0:
return a+b

v = (a[0]  b[0] and a or b).pop(0)
return [v] + merge(a, b)

def mergesort(lst):
if len(lst)  2:
return lst

m = len(lst)/2
return merge(mergesort(lst[:m]), mergesort(lst[m:]))

mlst = [10, 9, 8, 4, 5, 6, 7, 3, 2, 1]
sorted = mergesort(mlst)
print sorted

Besides using recursion in merge function also, it has somethings
intresting.

Especially the statement

v = (a[0]  b[0] and a or b).pop(0)

gives a.pop(0), if a[0]  b[0] otherwise b.pop(0).

We have to look at the statement as

v = ((a[0]  b[0] and a) or b).pop(0)


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


Re: [Tutor] A mergesort

2013-08-31 Thread Chris Down
On 2013-08-31 22:00, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
 def merge(a, b):
 if len(a)*len(b) == 0:
 return a+b

Indentation in Python matters; if you're going to post code, you should
probably keep it.

 We have to look at the statement as

 v = ((a[0]  b[0] and a) or b).pop(0)

This is short circuit evaluation, which is fairly common in programming
languages.[0]

0: https://en.wikipedia.org/wiki/Short-circuit_evaluation


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


Re: [Tutor] A mergesort

2013-08-31 Thread Stefan Behnel
D.V.N.Sarma డి.వి.ఎన్.శర్మ, 31.08.2013 18:30:
 I have been searching for mergesort implimentations in python and came
 across this.

In case this isn't just for education and you actually want to use it, the
built-in sorting algorithm in Python (used by list.sort() and sorted()) is
a very fast mergesort variant. Anything you could write in Python code is
bound to be slower.

Stefan


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


[Tutor] how to save variables after a user quits in python

2013-08-31 Thread Jack Little
I am coding a game and I want the player to be able to quit the game and 
immediately take off right from where they started from.

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


Re: [Tutor] how to save variables after a user quits in python

2013-08-31 Thread Chris Down
On 2013-08-31 14:30, Jack Little wrote:
 I am coding a game and I want the player to be able to quit the game and
 immediately take off right from where they started from.

If you're asking how to store variables between sessions, look at the pickle
module.


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


Re: [Tutor] how to save variables after a user quits in python

2013-08-31 Thread Steven D'Aprano

On 01/09/13 07:30, Jack Little wrote:

I am coding a game and I want the player to be able to quit the game and 
immediately take off right from where they started from.



That is trickier than it sounds. You have to save the internal state of the 
game, which means you first have to *identify* the internal state of the game. 
That means:

* everything about the user: name, health, score, ammunition, treasure, etc.;

* everything about every enemy: whether they are alive or dead, health, 
ammunition, etc.;

* everything about the internal state of the game: which parts of the game have 
already been visited, which parts haven't been;

* position of the user;

* anything else I have forgotten.

Once you have identified all of those things, then and only then can you start 
thinking about the best way to save that information to disk. Depending on what 
you need to save, you can then decide what module is best suited to that type 
of data. There are many choices:

http://docs.python.org/2/library/persistence.html
http://docs.python.org/2/library/fileformats.html
http://docs.python.org/2/library/json.html
http://docs.python.org/2/library/xml.html

It is difficult to tell what would be best without knowing what you need to 
save. Possibly as little as a Windows-style INI file would be enough:

[settings]
key: value


possibly you will need a custom solution that dumps the entire state of the 
Python interpreter to disk, then later restores it.




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


Re: [Tutor] myown.getfilesystemencoding()

2013-08-31 Thread eryksun
On Sat, Aug 31, 2013 at 9:16 AM, Oscar Benjamin
oscar.j.benja...@gmail.com wrote:
 Spyder has both an internal interpreter and an external interpreter.
 One is the same interpreter process that runs the Spyder GUI. The
 other is run in a subprocess which keeps the GUI safe but reduces your
 ability to inspect the workspace data via the GUI. So presumable
 Albert means the external interpreter here.

I installed Spyder on Windows to look into this. It's using Qt
QProcess to run the external interpreter in a child process.
sys.stdin.isatty() confirms it's not a tty, and Process Explorer
confirms that all 3 standard I/O handles (from msvcrt.get_osfhandle())
are pipes.

The file encoding is None for piped standard I/O, so printing unicode
falls back to the default encoding. Normally this is ASCII in 2.x, but
Spyder uses sitecustomize to set the default encoding based on the
default locale. It also sets the hidden console's codepage:

if os.name == 'nt': # Windows platforms

# Setting console encoding (otherwise Python does not
# recognize encoding)
try:
import locale, ctypes
_t, _cp = locale.getdefaultlocale('LANG')
try:
_cp = int(_cp[2:])
ctypes.windll.kernel32.SetConsoleCP(_cp)
ctypes.windll.kernel32.SetConsoleOutputCP(_cp)
except (ValueError, TypeError):
# Code page number in locale is not valid
pass
except ImportError:
pass

http://code.google.com/p/spyderlib/source/browse/spyderlib/
widgets/externalshell/sitecustomize.py?name=v2.2.0#74

Probably this was added for a good reason, but I don't grok the point.
Python isn't interested in the hidden console window at this stage,
and the standard handles are all pipes. I didn't notice any difference
with these lines commented out, running with Python 2.7.5. YMMV

There's a design flaw here since sys.stdin.encoding is used by the
parser in single-input mode. With it set to None, Unicode literals
entered in the REPL will be incorrectly parsed if they use non-ASCII
byte values. For example, given the input is Windows 1252, then u'€'
will be parsed as u'\x80' (i.e. PAD, a C1 Control code).

Here's an alternative to messing with the default encoding -- at least
for the new version of Spyder that doesn't have to support 2.5. Python
2.6+ checks for the PYTHONIOENCODING environment variable. This
overrides the encoding/errors values in Py_InitializeEx():

http://hg.python.org/cpython/file/70274d53c1dd/Python/pythonrun.c#l265

You can test setting PYTHONIOENCODING without restarting Spyder. Just
bring up Spyder's Internal Console and set
os.environ['PYTHONIOENCODING']. The change applies to new interpreters
started from the Interpreters menu. Spyder could set this itself in
the environment that gets passed to the QProcess object.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor