Re: [Tutor] Installing Python v3 on a laptop Windows 10

2019-06-15 Thread eryk sun
On 6/15/19, Alan Gauld via Tutor  wrote:
> On 15/06/2019 22:23, Ken Green wrote:
>
>> I understood there is a preferable method
>> of installing Python into Windows. I pray
>> tell on how about to do it, gentlemen.
>
> It depends a bit on which python distribution you use,
> there are several.
>
> Personally for Windows I always recommend the ActiveState free
> version. It bundles several useful extra Windows tools and
> puts the docs in Windows help format for you.

The compiled HTML (python*.chm) documentation is also included in the
official PSF (python.org) distribution. It's in the "Doc" folder. The
installer should create a shortcut to it in the start menu.

The current release of Windows 10 includes a `python` command that
installs the 3.7 app bundle from the Microsoft Store. This is a simple
one-click install method that should be easy for anyone. Unfortunately
the above-mentioned python37.chm file is not included in this
distribution.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Case Insensitive Globing

2019-05-19 Thread eryk sun
On 5/19/19, Alan Gauld via Tutor  wrote:
>
> Hmm, odd. My NTFS filesystems on Windows all appear to be case
> sensitive. For example I have a photo editor that saves its files
> with a jpg extension but the files from my camera all end in JPG.
> So I always end up with two copies - the original file and the
> edited version.
>
> I'm not aware of having done anything to cause that.
> More investigation required I think...

Maybe you have Explorer configured to hide file extensions, and you
have one file named "filename.JPG" and another named
"filename.jpg.JPG".

On a related note, the new support for case-sensitive directories in
Windows 10 can lead to an issue when running commands. Shells use the
PATHEXT environment variable to supply a list of default extensions
when searching PATH for a command. These are usually upper case, so if
we run "script" and it searches for "script.PY", it won't find
"script.py" in a case-sensitive directory. The same applies to
Python's shutil.which().

Regarding this topic, glob.py and fnmatch.py are unreliable in Windows
for case-sensitive NTFS directories. Sometimes they rely on regular
expressions and os.path.normcase (for ntpath.normcase, this replaces
slash with backslash and converts to lowercase), and sometimes they
rely on stat-based functions such as os.path.lexists, which will be
case sensitive.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How arguments to the super() function works?

2019-05-19 Thread eryk sun
On 5/19/19, Arup Rakshit  wrote:
>
> class Dad:
> def can_i_take_your_car(self):
> print("No...")
>
> class Mom(Dad):
> def can_i_take_your_car(self):
> print("Asking your dad...")
>
> class Victor(Mom, Dad):
> def can_i_take_your_car(self):
> print("Asking mom...")
>
> class Pinki(Mom, Dad):
> def can_i_take_your_car(self):
> print("I need it today..")
>
> class Debu(Pinki, Victor):
> def can_i_take_your_car(self):
> super(Victor, self).can_i_take_your_car()
>
> In this piece of code:
>
> print(Debu().can_i_take_your_car())
>
> Why the above call prints "Asking your dad…” but not " print("Asking mom…”)”
> although can_i_take_your_car is defined inside the class Victor ?

Victor comes after Pinki in the MRO:

>>> super(Debu, Debu()).can_i_take_your_car()
I need it today..
>>> super(Pinki, Debu()).can_i_take_your_car()
Asking mom...
>>> super(Victor, Debu()).can_i_take_your_car()
Asking your dad...
>>> super(Mom, Debu()).can_i_take_your_car()
No...

> Last question: Why duplicate PEPs for the same thing
> https://www.python.org/dev/peps/pep-0367/ and
> https://www.python.org/dev/peps/pep-3135/#specification ? Which one to read
> when such duplicate exists?

See https://www.python.org/dev/peps/pep-0367/#numbering-note and
https://www.python.org/dev/peps/pep-3135/#numbering-note.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Case Insensitive Globing

2019-05-19 Thread eryk sun
On 5/18/19, Steven D'Aprano  wrote:
>
> That means that, like Windows file systems FAT and NTFS, file names are
> case-insensitive: files "Foo", "foo" and "FOO" are all considered the
> same. But unlike Windows, the file system preserves the case of the file
> as you created it, so if you created it as "foO" that's how it will be
> recorded on the disk rather than normalizied to "foo".

NTFS and FAT32 are case preserving.

> Fun fact: even NTFS supports a case-sensitive mode! But again, hardly
> anyone uses it.

There's nothing inherent in the design of NTFS that prevents
case-sensitive names in a directory. It's strictly a function of the
OS and filesystem driver. On non-Windows platforms, an NTFS driver can
create names that differ only in case. An example is the Linux ntfs-3g
driver, which allows this unless the "windows_names" option is set.

In a Windows system, the NT object namespace is case sensitive, and
overriding a create or open to be case insensitive requires the flag
OBJ_CASE_INSENSITIVE. The Windows API uses this flag by default in the
file and registry APIs. However, beginning with Windows XP, the kernel
takes it a step further. Regardless of the user-mode subsytem, it
forces this flag for object types that are defined as case
insensitive, such as Object Manager "Directory" and "SymbolicLink"
objects (used in the root object namespace, akin to the root
filesystem in Unix), Configuration Manager "Key" objects (registry),
and I/O manager "Device" and "File" objects.

Prior to Windows XP, it was possible to force a CreateFile or
FindFirstFileEx call to be case sensitive, respectively via the flags
FILE_FLAG_POSIX_SEMANTICS and FIND_FIRST_EX_CASE_SENSITIVE.
Internally, this is implemented by *omitting* the OBJ_CASE_INSENSITIVE
flag that the Windows API usually includes for device and file
operations. This didn't change in XP, but, as mentioned above, it's
impotent now since the kernel itself adds the flag for
case-insensitive object types. The only way to change this is to
restart the system after zeroing the "obcaseinsensitive" value in the
"Session Manager\kernel" registry key. That's not tenable in practice,
so we have to just accept that device and file names are case
insensitive.

That said, in Windows 10, the Windows Subsystem for Linux requires a
way to change the filesystem default to case sensitive for individual
directories. If a directory is case sensitive, then create, open, and
control operations in or on it always ignore OBJ_CASE_INSENSITIVE.
This directory attribute can be queried and set with a new file
information class in the NT API named "FileCaseSensitiveInformation",
or on the command line via `fsutil file queryCaseSensitiveInfo `
and `fsutil file setCaseSensitiveInfo  [enable|disable]`.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is this code doing? What is it?

2019-05-12 Thread eryk sun
On 5/12/19, Alan Gauld via Tutor  wrote:
>
> They are both very powerful ways of constructing output strings with
> data inserted. {} and format() has a few extra tricks once you get
> into advanced uses, but % style does most of the same things (and
> has the advantage of being used in other languages too so you only
> need to remember one style!).

IMHO, given the choice of learning only one, learn the newer
curly-brace string formatting system. It's extensible since it's based
on a __format__ special method that a type (i.e. class) can override.

Take the Decimal type, for instance. By default it supports 28 digits
of precision. For example:

>>> from decimal import Decimal
>>> n = Decimal('0.12340567891234056789123405669') * 10
>>> n
Decimal('1.234056789123405678912340567')

Decimal implements the __format__ special method with its own
implementation of the "f" format specifier, which faithfully renders
the value as a string.

>>> '{:0.27f}'.format(n)
'1.234056789123405678912340567'

We can also use this directly with the built-in format() function in Python 3:

>>> format(n, '0.27f')
'1.234056789123405678912340567'

On the other hand, if we use a string's percent formatting, its "f"
format specifier has a fixed implementation that first converts a
number to a Python float, as opposed to delegating the string
conversion to the object itself.

>>> float(n)
1.2340567891234058
>>> '%0.27f' % n
'1.234056789123405772912178691'

As you can see in the formatted output, everything after
"1.234056789123405" is wrong. This is because a CPython float is
implemented as a C double-precision value, which only has about 16
decimal digits of precision.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exception not working as expected?

2019-02-28 Thread eryk sun
On 2/28/19, Chip Wachob  wrote:
>
> Python 2.7 & Windows and also Linux are the platforms being used.  Running
> the code from the command line / terminal as   python except.py.  Note that
> it does work properly in Linux.  So I'm guessing I need to test for a
> different exception along with the KeyboardInterrupt??

When reading from the console, we depend on a particular error being
set in order to distinguish an interrupted read from end-of-file
(EOF), but this error doesn't get set in Windows 8+ due to a bug in
the Windows API. I created an issue with Microsoft's console team, but
thus far they've ignored it.

Due to this bug, Python 2 code that calls raw_input() or input() also
needs to handle EOFError, which should probably be handled anyway.
However, it's not enough to separately handle KeyboardInterrupt and
EOFError, since the KeyboardInterrupt may get raised while handling
EOFError. This can happen because Windows calls the console control
handler asynchronously in a new thread. We need a bit of a kludge that
checks for a delayed KeyboardInterrupt. For example, the following
shows a case that uses a common handler for EOF and Ctrl+C:

import os
import sys
import time

def main():
try:
os.system('cls' if os.name == 'nt' else 'clear')
print "\n\n\n\n Welcome to Test.\n"
time.sleep(DISPLAY_TIME)
start_menu()
except (KeyboardInterrupt, EOFError):
try:
time.sleep(0.1)
except KeyboardInterrupt:
pass
# provides a clean exit
print "\n\n Keyboard Interrupt or EOF - Exiting \n\n"
time.sleep(5)
print "End of Script\n"
return 0

if __name__ == "__main__":
sys.exit(main())

> # trap no choice, just Enter key cases
> except ValueError:
> # handles non-entry selections like 'Enter'
> print "Invalid selection please try again"
> time.sleep(DISPLAY_TIME / 2)
> start_menu()

ValueError should be handled near the int() conversion that raises it,
and the exception handler should `continue` the loop instead of
recursively calling start_menu().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread eryk sun
On 2/27/19, Alan Gauld via Tutor  wrote:
> On 27/02/2019 18:28, Chip Wachob wrote:
>> Windows 7 vintage machine this morning.
>
> Caveat: I have no direct experience on Windows 7 - I jumped
> from XP to Windows 8... Also I haven't used ANSIO codes in
> Windows since the heady days of Windows 98! But...
>
>> run in the command window in Windows, from the terminal in Linux...
>
> Be aware that ANSI codes in DOS don't correspond directly
> with the VT100 control codes commonly used in Linux etc.
> You might get some things that either don;t work or work
> slightly differently.
>
>> In the code I had included the ANSI escape characters so I could invert
>> the
>> text and change the color.
>
> Again remember that old ANSI terminals didn't have colors.
> (They were either green on black, white on black, or if you
> were really fancy, amber on black).
>
> Color came later so some terminal emulators won't do anything
> with those codes.
>
>> As I have now learned, Windows 7 does not support this functionality.
>> What do the experts out there suggest as the path of least pain and
>> suffering?
>
> No idea if this works on Windows 7 but what we used to have
> to do was load the ANSI.SYS driver in the CONFIG.SYS file
> located in the root directory of the boot drive.

Windows NT systems use the Windows console API, which talks to a
console that's hosted in a system process. In Windows 7+, each console
is hosted in an instance of conhost.exe. In older versions of NT,
consoles are hosted by threads in the session server process,
csrss.exe.

In 32-bit builds of NT, there's integration between the console and
the virtual DOS machine (ntvdm.exe). This allows running old 16-bit
DOS programs/drivers such as COMMAND.COM and ANSI.SYS in an emulated
DOS environment. 64-bit builds no longer support NTVDM. Anyway, it
wouldn't apply to Windows console applications such as python.exe,
which have nothing to do with DOS.

The only practical option is to implement VT sequences using the
console API, such as is done by the colorama package. Other options
require installing an alternate console (e.g. ConEmu) or a program
that hacks the console API in each process via DLL injection (e.g.
ANSICON).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread eryk sun
On 2/27/19, Chip Wachob  wrote:
>
> I've been spending the morning looking at different solutions.  Many of
> them seem to involve either including yet aother module, or, worse (IMHO) a
> C library that will 'emulate' the ANSI escape sequences, API calls, etc.

To support virtual-terminal sequences prior to Windows 10, you'll need
a package such as colorama [1] that implements VT sequences in terms
of the Windows console API -- unless you're willing to require an
alternate console such as ConEmu or an API hack such as ANSICON.

[1]: https://pypi.org/project/colorama
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the best way for a program suite to know where it is installed?

2018-10-22 Thread eryk sun
On 10/22/18, boB Stepp  wrote:
>
> Importing the various program modules/module contents is
> no issue.  Where I believe I need to know the paths to things are to
> get to data folders, config files, and occasionally utility programs
> that I have written that are on my hard drive, but not copied to my
> current program suite.

It's common to use __file__ for a portable application, except you
should use sys.executable if it's a frozen executable, i.e. if
sys.frozen exists.

For an installed application, limit this approach to the application's
immutable resources. Don't use the installation directory to store
modifiable data. You may not have write access (e.g. a system
installation and the user lacks root/admin access, or it's an
immutable directory or read-only disk mount).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what does the forward slash mean in this function signature?

2018-10-15 Thread eryk sun
On Mon, Oct 15, 2018 at 6:00 AM Albert-Jan Roskam
 wrote:
>
> In Python 3.6 (Windows) I often see a forward slash in a function signature, 
> see below.
> What does it mean? I vaguely remember reading something about new 
> possbilities in
> python 3, something like "def foo(x, *, y)". Perhaps it's related to that?
>
> >>> help({}.fromkeys)
> Help on built-in function fromkeys:
>
> fromkeys(iterable, value=None, /) method of builtins.type instance
> Returns a new dict with keys from iterable and values equal to value.

This syntax is for position-only function parameters. It's used by
Argument Clinic [1], a preprocessor used internally to develop
CPython. Position-only parameters have also been proposed for the
language grammar, first in PEP 457 and again in PEP 570 [2].

[1]: https://docs.python.org/3/howto/clinic.html#converting-your-first-function
[2]: https://www.python.org/dev/peps/pep-0570
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling printf() in a C language DLL using ctypes.CDLL

2018-05-08 Thread eryk sun
On Wed, May 9, 2018 at 1:30 AM, Brad M  wrote:
>
> If you want to know where your program went when something went wrong or
> when it triggers a if condition, how do you do it?

Look into use the logging module [1] and debuggers [2], either
dedicated like Winpdb or in an IDE such as PyCharm.

[1]: https://docs.python.org/3/howto/logging.html#logging-basic-tutorial
[2]: https://wiki.python.org/moin/PythonDebuggingTools

For debugging a DLL, learn to use a native debugger, such as
Microsoft's WinDbg, cdb, or the Visual Studio debugger.
OutputDebugString [3] writes a string to an attached debugger.

[3]: https://msdn.microsoft.com/en-us/library/aa363362
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling printf() in a C language DLL using ctypes.CDLL

2018-05-08 Thread eryk sun
On Tue, May 8, 2018 at 9:39 AM, Brad M  wrote:
>
> I compile this by typing this in the command line:
> cl /LD /I C:\python\include helloworld.c C:\python\libs\python36.lib

You're not using Python's C API, so you only need `cl /LD helloworld.c`.

> However, this doesn't print anything on the python window.
> What I would like is to do is to be able to use printf() in my .dll
> by having the c code pop up a console window to print or
> to have something that can print() in the python window somehow.

By Python window, do you mean the IDLE GUI? If the library is loaded
in a GUI program in which stdout is invalid, it will have to manually
allocate a console via `AllocConsole` and open the screen buffer using
the reserved filename "CONOUT$". Then it can print to the opened FILE
stream using fprintf(). But I'll reiterate Alan here that this would
be unusual behavior for a shared library, unless it's specifically
intended as a UI library.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python C extension - which method?

2018-05-07 Thread eryk sun
On Mon, May 7, 2018 at 9:57 AM, Michael C
 wrote:
> Sorry I don't understand your suggestion.
>
> Use "ctypes.CDLL" and "ctypes.WinDLL"
>
> this works,
> mydll = cdll.LoadLibrary('test.dll')
> print(mydll.sum(3,2))
>
> and this doens't
> mydll = cdll('test.dll')
> print(mydll.sum(3,2))
>
> What's the syntax of what you suggested?

Loading the library is simply `mydll = ctypes.CDLL('test')`. Note that
CDLL is uppercase. Python is a case-sensitive language.

The base shared library class is CDLL. The constructor takes the
following parameters: name, mode=DEFAULT_MODE, handle=None,
use_errno=False, and use_last_error=False. For the first 3 parameters,
if the shared library `handle` isn't provided, it calls
ctypes._dlopen(mode, name) to load the library.

The CDLL constructor dynamically defines a function-pointer subclass,
with custom _flags_ and _restype_. The _restype_ is c_int (i.e. a
32-bit C integer). The base _flags_  value is _FUNCFLAG_CDECL because
CDLL uses the cdecl calling convention. For the last two constructor
parameters, use_errno includes _FUNCFLAG_USE_ERRNO, and use_last_error
includes _FUNCFLAG_USE_LASTERROR.

The above function pointer class is used by the __getitem__ method of
a CDLL instance, which calls self._FuncPtr((name_or_ordinal, self)) to
return a function pointer instance, e.g. mydll['sum']. (Using a
subscript also allows ordinal look-up, such as mydll[42].) The
function pointer is not cached, so it has to be instantiated ever time
it's accessed this way. On the other hand, the __getattr__ method of
CDLL calls __getitem__ and caches the function pointer via
setattr(self, name, func). For example, with mydll.sum, the `sum`
function pointer is cached.

WinDLL subclasses CDLL to change the default function pointer _flags_
value to _FUNCFLAG_STDCALL because the Windows API uses the stdcall
calling convention, as do many shared libraries on Windows.

ctypes also defines a LibraryLoader class, of which ctypes.cdll and
ctypes.windll are instances, i.e. cdll = LibraryLoader(CDLL). The
constructor simply sets the shared library class as the attribute
self._dlltype. In turn, the LoadLibrary method returns
self._dlltype(name). This is silly because it's extra typing, an extra
call, and prevents passing constructor arguments (e.g.
use_last_error).

Where this goes from silly to smelly is the __getattr__ method of
LibraryLoader. This method makes the mistake of caching loaded
libraries. For example, ctypes.windll.kernel32 caches a
WinDLL('kernel32') instance on ctypes.windll. Remember that
__getattr__ is a fallback method called by __getattribute__. Thus
subsequent references to ctypes.windll.kernel32 will use the cached
library instance. This is bad because multiple packages will end up
sharing the same library instance.

Recall that the __getattr__ method of CDLL caches function pointers.
That's a good thing because function pointer look-up and instantiation
is relatively expensive. I can understand the good intentions of
carrying this over to caching entire library instances across multiple
packages. But the savings is not worth the ensuing chaos of
super-global cached state, especially for commonly used Windows
libraries such as kernel32.dll. The problem is that every function
pointer defines a prototype that consists of its errcheck, restype,
and argtypes properties, but not every package or script will define
these exactly the same, for various reasons. Thus the last one to
define the prototype wins. All other users of the library in the
process will be broken.

In short, avoid ctypes.[cdll | windll].LoadLibrary because it's silly,
and avoid ctypes.[cdll | windll].[library name] because it's smelly.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] passing values and C pointers

2018-05-06 Thread eryk sun
On Sun, May 6, 2018 at 2:17 AM, Brad M  wrote:
>
> Say I have an array of values, say addresses or int produced by a c module/
> c function that's in a DLL , how do I pass that array back to
> the python code?

C arrays are passed and returned automatically as pointers to the
first element. The array length has to be passed separately, unless
there's a known sentinel value.

A simple pattern is to let the caller allocate the array and pass a
pointer and the length. This gives the caller explicit control over
the lifetime of the array, which is especially simple for ctypes since
it uses reference-counted objects.

Say you have a function in C such as the following:

int
DLLAPI
get_data(int *data, size_t length)
{
size_t i;
for (i=0, i < length; i++) {
if (do_something(i, &data[i]) == -1) {
return -1; /* failure */
}
}
return 0; /* success */
}

In Python, set up and call this function as follows:

import ctypes

mydll = ctypes.CDLL('mydll')

# setup
mydll.get_data.argtypes = (
ctypes.POINTER(ctypes.c_int), # data
ctypes.c_size_t)  # length

# call
data = (ctypes.c_int * 10)()
status = mydll.get_data(data, len(data))
if status == -1:
raise MyDllException('get_data: ...')

for i, value in enumerate(data):
result = do_something_else(i, value)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python C extension - which method?

2018-05-06 Thread eryk sun
On Sun, May 6, 2018 at 12:49 AM, Brad M  wrote:
> If I may ask, what's the difference between these two?
>
> 1)
> import ctypes
> hello = ctypes.WinDLL('hello', use_last_error=True)
>
> 2)
> from ctypes import cdll
> hello = cdll.LoadLibrary('hello.dll')

Use ctypes.CDLL and ctypes.WinDLL instead of cdll.LoadLibrary and
windll.LoadLibrary. The latter is more typing for no benefit and
prevents using the constructor arguments: handle, mode (POSIX),
use_errno, and use_last_error (Windows). You need the latter two
options if the library requires C errno or Windows GetLastError(), in
which case you should use ctypes.get_errno() or
ctypes.get_last_error() to get the error values after a C function
call.

> Both of them can return "1980" from  this:
>
> hello.c
>
> #include 
>
> __declspec(dllexport) int say_something()
> {
> return 1980;
> }

CDLL is the cdecl calling convention, and WinDLL is stdcall. There is
no difference in 64-bit Windows (x64 ABI). In 32-bit Windows (x86
ABI), cdecl has the caller clean the stack (i.e. pop arguments), and
stdcall has the callee (the called function) clean the stack. cdecl
allows functions with a variable number of arguments, such as the CRT
printf function. In MSVC, cdecl is the default convention if you don't
declare a function as __stdcall. A library can export functions with
varying calling conventions, so in general you may need to mix CDLL
and WinDLL.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running a .py file from the comand line

2018-04-02 Thread eryk sun
On Mon, Apr 2, 2018 at 8:53 AM, Alan Gauld via Tutor  wrote:
>
> Try
>
> python c:\Users\Rex\"ascii keys.py"
>
> Note the quotes to cater for the space.
>
>> python:  can't open file 'Ascii':  [errno2] no such file or directory
>
> The space confuses windows CMD, so it thinks you have
> two files called 'Ascii' and 'keys.py'

Unlike Unix, this is not due to the shell in Windows. A process is
started with a raw command line string. For a C/C++ application, the
default process entry point is provided by the C runtime and does the
setup work to call the application entry point (e.g. [w]main). This
includes parsing the command line into an argv array according to
documented rules [1]. An application can also call GetCommandLineW [2]
and CommandLineToArgvW [3].

[1]: https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments
[2]: https://msdn.microsoft.com/en-us/library/ms683156
[3]: https://msdn.microsoft.com/en-us/library/bb776391

CPython is written in C and uses the standard Windows C/C++ wmain and
wWinMain application entry points. If you run "python
C:\Users\Rex\Ascii Keys.py", the C runtime parses this into an argv
array with 3 items: "python", "C:\Users\Rex\Ascii", and "Keys.py".
Thus Python tries to open a script named "C:\Users\Rex\Ascii".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Virtual environment question

2018-03-12 Thread eryk sun
On Tue, Mar 13, 2018 at 1:31 AM, Jim  wrote:
> On 03/12/2018 04:04 AM, eryk sun wrote:
>>
>> On Mon, Mar 12, 2018 at 12:44 AM, Jim  wrote:
>>>
>>> home = /usr/bin
>>> include-system-site-packages = false
>>
>> [...]
>>>
>>>  resp = opener.open(request, timeout=self._timeout)
>>>File "/usr/lib/python3.5/urllib/request.py", line 466, in open
>>
>> This is normal. Virtual environments are not isolated from the standard
>> library.
>
> Interesting. All I know about virtual environments is what I read on the
> net. I always see them recommended as a way to keep from messing up the
> default python, so I thought isolation was their purpose.

You're thinking of isolating packages that are installed in
site-packages, not the standard library. There's no point in copying
and recompiling the entire standard library in every virtual
environment.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Virtual environment question

2018-03-12 Thread eryk sun
On Mon, Mar 12, 2018 at 12:44 AM, Jim  wrote:
>
> home = /usr/bin
> include-system-site-packages = false
[...]
> resp = opener.open(request, timeout=self._timeout)
>   File "/usr/lib/python3.5/urllib/request.py", line 466, in open

This is normal. Virtual environments are not isolated from the standard library.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is and isn't "__file__" set?

2018-01-12 Thread eryk sun
On Fri, Jan 12, 2018 at 5:22 PM, Albert-Jan Roskam
 wrote:
> On Jan 11, 2018 03:47, Steven D'Aprano  wrote:
>>
>> Modules which are loaded from a .dll or .so binary file also should have
>> __file__ set.
>
> And .pyd? I would hope so

A .pyd is a Windows DLL (i.e. PE/COFF shared library). The .pyd type
is associated with the "Python.Extension" program identifier, which
sets a custom descriptive name and icon in Explorer. Sometimes a
Python extension module uses the .dll file extension instead of .pyd.
For example, from PyWin32:

>>> print(pywintypes.__file__)
C:\Program Files\Python36\pywintypes36.dll
>>> print(pythoncom.__file__)
C:\Program Files\Python36\pythoncom36.dll
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does os.path.realpath('test_main.py') give different results for unittest than for testing statement in interpreter?

2018-01-10 Thread eryk sun
On Wed, Jan 10, 2018 at 12:59 PM, Albert-Jan Roskam
 wrote:
>
> I tried:
 from os.path import _getfullpathname
 _getfullpathname(r"H:")
> 'h:\\path\\to\\folder'
 import os
 os.getcwd()
> 'h:\\path\\to\\folder'
>
> I expected h:\ to be \\server\share\foo.

You called _getfullpathname (WinAPI GetFullPathName), not
_getfinalpathname (WinAPI GetFinalPathNameByHandle). GetFullPathName
works on the path as a string without touching the filesystem.
GetFinalPathNameByHandle reconstructs a path when given a handle to a
file or directory.

> The fact that the current working directory was returned was even more 
> unexpected.

"H:" or "H:relative/path" is relative to the working directory on
drive H:. The process only has one working directory, but
GetFullPathName also checks for environment variables such as "=H:".
The C runtime's _chdir function sets these magic variables, as does
Python's os.chdir function (we don't call C _chdir). WinAPI
SetCurrentDirectory does not set them. For example:

>>> os.chdir('Z:/Temp')
>>> win32api.GetEnvironmentVariable('=Z:')
'Z:\\Temp'
>>> os.path._getfullpathname('Z:relative')
'Z:\\Temp\\relative'

> Why would anybody *want* the drive letters? They are only useful because (a) 
> they save on
> keystrokes (b) they bypass the annoying limitation of the cd command on 
> windows, ie. it
> does not work with UNC paths.

Windows itself has no problem using a UNC path as the working
directory. That's a limit of the CMD shell.

SUBST drives can be used to access long paths. Since the substitution
occurs in the kernel, it avoids the MAX_PATH 260-character limit. Of
course, you can also use junctions and symlinks to access long paths,
or in Windows 10 simply enable long-path support.

> I know net use, pushd, subst. I use 'net use' for more or less permanent 
> drives and
> pushd/popd to get a temporary drive, available letter (cd nuisance).

`net.exe use` and CMD's PUSHD command (with a UNC path) both call
WinAPI WNetAddConnection2 to create a mapped network drive. The
difference is that net.exe can supply alternate credentials and create
a persistent mapping, while PUSHD uses the current user's credentials
and creates a non-persistent mapping.

If your account gets logged on with a UAC split token, the standard
and elevated tokens actually have separate logon sessions with
separate local-device mappings. You can enable a policy to link the
two logon sessions. Set a DWORD value of 1 named
"EnableLinkedConnections" in the key
"HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System", and
reboot.

subst.exe creates substitute paths using WinAPI DefineDosDevice.
Unlike the WNet API, this function doesn't use MPR (multiple provider
router) to create a direct link for the network provider (e.g.
\Device\LanmanRedirectory); doesn't create a linked connection when
EnableLinkedConnections is defined; and can't create a persistent
drive with stored credentials (though you can use an account logon
script for this). On the plus side, a drive mapped via subst.exe can
target any path.

> Interesting code! I have used the following, which uses SHGetFolderPath, ie. 
> without 'Known'.
> from win32com.shell import shell, shellcon
> desktop = shell.SHGetFolderPath(0, shellcon.CSIDL_DESKTOP, 0, 0)

SHGetFolderPath is usually fine, but still, it's outdated and
deprecated. win32com.shell doesn't wrap SHGetKnownFolderPath for some
reason, but you can still use the new known-folder API without ctypes.
Just create a KnownFolderManager instance. For example:

import pythoncom
from win32com.shell import shell

kfmgr = pythoncom.CoCreateInstance(shell.CLSID_KnownFolderManager, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IKnownFolderManager)

desktop_path = kfmgr.GetFolder(shell.FOLDERID_Desktop).GetPath()

This doesn't work as conveniently for getting known folders of other
users. While the high-level SHGetKnownFolderPath function takes care
of loading the user profile and impersonating, we have to do this
ourselves when using a KnownFolderManager instance.

That said, to correct my previous post, you have to be logged on with
SeTcbPrivilege access (e.g. a SYSTEM service) to get and set other
users' known folders without their password. (If you have the password
you can use a regular logon instead of an S4U logon, and that works
fine.)

> Working with ctypes.wintypes is quite complex!

I wouldn't say ctypes is complex in general. But calling LsaLogonUser
is complex due to all of the structs that include variable-sized
buffers. And working with COM via ctypes is also complex, which is why
comtypes exists.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does os.path.realpath('test_main.py') give different results for unittest than for testing statement in interpreter?

2018-01-09 Thread eryk sun
On Tue, Jan 9, 2018 at 2:48 PM, Albert-Jan Roskam
 wrote:
>
> I think that it would be a great enhancement if os.realpath would return the 
> UNC path if
> given a mapped drive in Windows, if needed as extended path (prefixed with 
> "\\?\UNC\").
> That's something I use all the time, unlike symlinks, in Windows.

pathlib can do this for you, or call os.path._getfinalpathname. I
recently helped someone that wanted the reverse, to map the resolved
UNC path back to a logical drive:

https://bugs.python.org/issue32442

> And I would also welcome a convenience function in the os.path module that 
> does
> expanduser, expandvars, normpath, realpath, and maybe more.

pathlib implements expanduser, but it's still lame on Windows for a
user other than the current user. It assumes all user profiles are in
the same directory instead of directly getting the user's profile
path. Anyway, expanduser is of limited use.

We can't assume in general that a user's special folders (e.g.
Desktop, Documents) are in the default location relative to the
profile directory. Almost all of them are relocatable. There are shell
APIs to look up the current locations, such as SHGetKnownFolderPath.
This can be called with ctypes [1]. For users other than the current
user, it requires logging on and impersonating the user, which
requires administrator access.

[1]: https://stackoverflow.com/a/33181421/205580

You can log a user on without a password by calling LsaLogonUser to
request an MSV1 S4U (service for user) logon. The access token will
only be identification level, unless the script is running as a SYSTEM
service. But identification level is enough to query the location of a
user's known folders. I don't recommended calling LsaLogonUser via
ctypes if you have the option to write an extension module in C/C++,
but I did manage to get it working with ctypes [2]. For example:

>>> from security_lsa import logon_msv1_s4u
>>> from knownfolders import FOLDERID, get_known_folder_path

>>> logon_info = logon_msv1_s4u('Administrator')
>>> get_known_folder_path(FOLDERID.Desktop, logon_info.Token)
'C:\\Users\\Administrator\\Desktop'

[2]: https://stackoverflow.com/a/4322/205580

Also, please don't use expanduser to enable applications to spam the
profile directory with configuration files and directories. Use the
local and roaming application data directories.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why does os.path.realpath('test_main.py') give different results for unittest than for testing statement in interpreter?

2018-01-08 Thread eryk sun
On Sun, Jan 7, 2018 at 10:51 AM, Albert-Jan Roskam
 wrote:
>
> On Jan 7, 2018 09:08, Steven D'Aprano  wrote:
>>
>> realpath() returns the canonical path of the given filename. It doesn't
>> try to locate some actual existing file.
>
> I always thought that os.path.realpath is the Python equivalent of Linux
> realpath/readlink

In POSIX, os.path.realpath resolves a path that contains symlinks by
calling os.readlink in a loop until all links are resolved.

In Windows, os.path.realpath is an alias for os.path.abspath. This
decision predates NTFS support for junctions (2000) and symbolic links
(Vista). It's simply the case that no one has gotten around to
implementing realpath for Windows.

In Windows, os.path.abspath calls os.path._getfullpathname (WinAPI
GetFullPathName). Other than resolving the working directory, this is
a string transformation to return a canonical, fully-qualified path.
It doesn't touch the filesystem. Note that in the case of classic DOS
devices, the canonical path is a local-device path (i.e. "\\.\"
prefix). For example:

>>> print(os.path.abspath('C:/path/to/nul'))
\\.\nul

> maybe also when it's a hard link - in this case the function actually also 
> does
> something in Windows

A hard link shouldn't be resolved to another path by os.path.realpath.
It's already a real path.

Cross-platform Python 3 code that needs to resolve symbolic links (or
junctions) can use pathlib.Path.resolve(). For example:

>>> pathlib.Path('C:/Documents and Settings').resolve()
WindowsPath('C:/Users')

On Windows this relies on os.path._getfinalpathname (WinAPI
GetFinalPathNameByHandle). This function returns the final opened
filename, so the OS does all of the work to resolve the final path. In
non-strict mode pathlib uses a loop to reduce the path when it can't
be resolved to an existing file. (There are open issues for edge cases
in this code, but it should work as intended with regular files in
accessible directories.)

For those who are curious, WinAPI GetFinalPathNameByHandle is called
with a handle for a File object that's opened via CreateFile. It gets
the NT name of the volume (e.g, "\Device\HarddiskVolume1") and the
filesystem path (e.g. "\Users"). Then it asks the mount-point manager
to map this NT name back to a DOS name such as drive "C:"; or an NTFS
volume mountpoint such as "C:\Mount\VolumeName"; or a "Volume{GUID}"
name. (In the latter case, Python's _getfinalpathname and pathlib have
bugs and need improvement.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-06 Thread eryk sun
On Fri, Oct 6, 2017 at 11:05 PM, Michael C
 wrote:
> For this read process memory, if I am trying compose a LPCVOID
> lpBaseAddress, am I not making a variable that equals to  mbi.BaseAddress,
> and then making a pointer pointing to it?
>
> start_address = mbi.BaseAddress
>  LPCVOID = ctypes.byref(start_address)

LPCVOID is a pointer type; don't use it as a variable name because
it's confusing to someone who's reading your code.

The `BaseAddress` field is an LPVOID, which is an alias for
ctypes.c_void_p. Simple C types such as c_void_p are automatically
converted to Python native types such as int, bytes, and str. It's
fine that mbi.BaseAddress is a Python int. With argtypes defined for
ReadProcessMemory, ctypes will convert the int back to a void pointer
for you automatically.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-06 Thread eryk sun
On Fri, Oct 6, 2017 at 10:26 PM, Michael C
 wrote:
>
> base = mbi.BaseAddress
> buffer = ctypes.c_int32()
> buffer_pointer = ctypes.byref(buffer)
> ReadProcessMemory = Kernel32.ReadProcessMemory
>
> if ReadProcessMemory(Process, base, buffer_pointer, mbi.RegionSize, None):
> print('buffer is: ',buffer)
> else:
> raise ctypes.WinError(ctypes.get_last_error())

If you need to read RegionSize bytes, then you have to allocate a
buffer that's RegionSize bytes:

buffer = ctypes.create_string_buffer(mbi.RegionSize)

Or use a smaller buffer and loop until the total number of bytes read
is RegionSize.

Also, remember to check that the state is MEM_COMMIT. You cannot read
an address range that's free or reserved. It must be committed, i.e.
backed by physical storage.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-06 Thread eryk sun
On Fri, Oct 6, 2017 at 10:06 PM, Michael C
 wrote:
> like this?
>
> buffer = ctypes.byref(ctypes.create_string_buffer(4))

No, the buffer is the array created by create_string_buffer, which you
pass byref(). In the following example I create a `test` buffer that
contains "spam", and I use the pseudo-handle from GetCurrentProcess
with ReadProcessMemory to read this buffer into a target `buffer`.
It's silly to do this in the current process, but it's just an
example.

import ctypes
from ctypes.wintypes import HANDLE, LPVOID

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

SIZE_T = ctypes.c_size_t
LPSIZE_T = ctypes.POINTER(SIZE_T)

kernel32.GetCurrentProcess.restype = HANDLE
kernel32.ReadProcessMemory.argtypes = (HANDLE, LPVOID,
LPVOID, SIZE_T, LPSIZE_T)

hProcess = kernel32.GetCurrentProcess()
test = ctypes.create_string_buffer(b'spam')
address = ctypes.addressof(test)
buffer = ctypes.create_string_buffer(4)
nread = SIZE_T()

success = kernel32.ReadProcessMemory(hProcess, address,
ctypes.byref(buffer), ctypes.sizeof(buffer),
ctypes.byref(nread))

if not success:
raise ctypes.WinError(ctypes.get_last_error())

print(buffer[:])
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-06 Thread eryk sun
On Fri, Oct 6, 2017 at 9:12 PM, Michael C
 wrote:
>
> How do I create a buffer, or rather, is a buffer just a variable?

A buffer is a block of memory for an I/O operation. For example, if
you need to read a 4-byte (32-bit) integer at an address in another
process, the 'buffer' could be ctypes.c_int32(). In general, to read
an arbitrary-sized block of memory, use ctypes.create_string_buffer()
to create a char array.

> How do I create a pointer to it?

Pass it byref().

> print('mbi.State: ',mbi.State)

Check whether mbi.State is MEM_COMMIT before trying to read it. If
it's MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail.

> buffer = ctypes.create_string_buffer(4)
> bufferSize = (ctypes.sizeof(buffer))
>
> ReadProcessMemory = Kernel32.ReadProcessMemory
>
> if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, None):
> print('buffer is: ',buffer)
> else:
> print('something is wrong')

Don't print "something is wrong". You're capturing the thread's last
error value, so use it to raise an informative exception. For example:

if not success:
raise ctypes.WinError(ctypes.get_last_error())
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-06 Thread eryk sun
On Fri, Oct 6, 2017 at 7:43 PM, Michael C
 wrote:
> Sorry but I dont understand this line:
>
> mbi = MEMORY_BASIC_INFORMATION()
>
> This creates a instance of the class?

Yes, and this allocates sizeof(MEMORY_BASIC_INFORMATION) bytes at
addressof(mbi), which you pass to a function by reference via
byref(mbi).

> Also, I thought with VirtualQueryEx, what you need for it
> is a handle, which I acquire from this
> Process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
> False, PID)

My example called VirtualQuery, not VirtualQueryEx. Internally
VirtualQuery calls VirtualQueryEx using the pseudo handle
(HANDLE)(-1), which refers to the current process.

> and then feed it to the function like so:
>
> VirtualQuery(Process, ctypes.byref(mbi), ctypes.sizeof(mbi))
>
> I know it doesn't work. But what are these lines for? They don't look like
> handle to me:
>
> VirtualQuery = kernel32.VirtualQuery
> VirtualQuery.restype = SIZE_T
> VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T)

In the above, I'm setting the function pointer's argtypes attribute to
the types of the 3 parameters that VirtualQuery takes: the target
address (i.e. LPVOID), a pointer to the buffer (i.e.
PMEMORY_BASIC_INFORMATION), and the size of the buffer (SIZE_T). This
is to allow ctypes to correctly check and convert arguments passed to
the function.

VirtualQueryEx has four parameters, starting with the handle to the
target process, hProcess. The remaining 3 are the same as
VirtualQuery.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-06 Thread eryk sun
On Fri, Oct 6, 2017 at 7:26 PM, Michael C
 wrote:
>
> I started out with what you gave me:
>
[...]
>
> I am trying to acquire "lpMinimumApplicationAddress" and
> "lpMaximumApplicationAddress" from system_info, so I did this,
>
>>code
> Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
> Kernel32.GetSystemInfo(LPSYSTEM_INFO)
> print(LPLPSYSTEM_INFO.lpMinimumApplicationAddress)

It's the same pattern as before. Create a SYSTEM_INFO instance, which
allocates the block of memory for the information, and pass
GetSystemInfo a pointer. For example:

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
kernel32.GetSystemInfo.restype = None
kernel32.GetSystemInfo.argtypes = (LPSYSTEM_INFO,)

sysinfo = SYSTEM_INFO()
kernel32.GetSystemInfo(ctypes.byref(sysinfo))

Here are the minimum and maximum addresses for a 64-bit process,
formatted in hexadecimal:

>>> hex(sysinfo.lpMinimumApplicationAddress)
'0x1'
>>> hex(sysinfo.lpMaximumApplicationAddress)
'0x7ffe'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-05 Thread eryk sun
On Thu, Oct 5, 2017 at 8:27 PM, Michael C
 wrote:
>
> How do I see the values of each field? This doesn't work.
>
> print(PMEMORY_BASIC_INFORMATION.Protect)

Create an instance of MEMORY_BASIC_INFORMATION and pass a pointer to
it via byref(). For example, the following queries the region of
memory of the VirtualQuery function itself.

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

MEM_COMMIT = 0x1000
PAGE_EXECUTE_READ = 0x20
PAGE_EXECUTE_WRITECOPY = 0x80

VirtualQuery = kernel32.VirtualQuery
VirtualQuery.restype = SIZE_T
VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T)

mbi = MEMORY_BASIC_INFORMATION()
VirtualQuery(VirtualQuery, ctypes.byref(mbi), ctypes.sizeof(mbi))

>>> mbi.AllocationBase == kernel32._handle
True
>>> mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY
True
>>> mbi.BaseAddress
140703181352960
>>> mbi.RegionSize
364544
>>> mbi.State == MEM_COMMIT
True
>>> mbi.Protect ==  PAGE_EXECUTE_READ
True
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ctypes wintypes

2017-10-05 Thread eryk sun
On Tue, Oct 3, 2017 at 10:30 PM, Michael C
 wrote:
>
> I am trying to create SYSTEM_INFO structure  and MEMORY_BASIC_INFORMATION
> structure

First, avoid relying on constants, enumerations, and structures
published on MSDN. It's not always right. Get the SDK and use the
header files instead. MEMORY_BASIC_INFORMATION is defined in winnt.h,
and SYSTEM_INFO is defined in sysinfoapi.h.

MEMORY_BASIC_INFORMATION is simple. Don't worry about the
MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions.
Those are meant for a debugger that's reading this structure directly
from the memory of another process.

SYSTEM_INFO is a bit tricky, given the anonymous struct and union. I
prefer to nest the definitions, but you could flatten it as separate
definitions if you like. Refer to the docs for how to use _anonymous_:

https://docs.python.org/3/library/ctypes#ctypes.Structure._anonymous_

Here are the definitions. Please don't mindlessly copy and paste.
Recreate them on your own and use this example as a reference.

import ctypes
from ctypes.wintypes import WORD, DWORD, LPVOID

PVOID = LPVOID
SIZE_T = ctypes.c_size_t

# https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR
if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulonglong):
DWORD_PTR = ctypes.c_ulonglong
elif ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong):
DWORD_PTR = ctypes.c_ulong

class MEMORY_BASIC_INFORMATION(ctypes.Structure):
"""https://msdn.microsoft.com/en-us/library/aa366775""";
_fields_ = (('BaseAddress', PVOID),
('AllocationBase',PVOID),
('AllocationProtect', DWORD),
('RegionSize', SIZE_T),
('State',   DWORD),
('Protect', DWORD),
('Type',DWORD))

PMEMORY_BASIC_INFORMATION = ctypes.POINTER(MEMORY_BASIC_INFORMATION)

class SYSTEM_INFO(ctypes.Structure):
"""https://msdn.microsoft.com/en-us/library/ms724958""";
class _U(ctypes.Union):
class _S(ctypes.Structure):
_fields_ = (('wProcessorArchitecture', WORD),
('wReserved', WORD))
_fields_ = (('dwOemId', DWORD), # obsolete
('_s', _S))
_anonymous_ = ('_s',)
_fields_ = (('_u', _U),
('dwPageSize', DWORD),
('lpMinimumApplicationAddress', LPVOID),
('lpMaximumApplicationAddress', LPVOID),
('dwActiveProcessorMask',   DWORD_PTR),
('dwNumberOfProcessors',DWORD),
('dwProcessorType', DWORD),
('dwAllocationGranularity', DWORD),
('wProcessorLevel',WORD),
('wProcessorRevision', WORD))
_anonymous_ = ('_u',)

LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] logging to cmd.exe

2017-09-26 Thread eryk sun
On Tue, Sep 26, 2017 at 7:35 AM, Mats Wichmann  wrote:
> On 09/26/2017 05:22 AM, Albert-Jan Roskam wrote:
>
>> Rather than change your code can you change the codepage with the chcp
>> command?
>
> the way chcp takes effect is problematic for this:
>
> "Programs that you start after you assign a new code page use the new
> code page, however, programs (except Cmd.exe) that you started before
> assigning the new code page use the original code page. "

Some console applications only check the codepage at startup. If you
change it while the program is running, they'll encode/decode text for
the original codepage, but the console will decode/encode it for its
current codepage. That's called mojibake.

Prior to 3.6, at startup Python uses the input codepage for sys.stdin,
and the output codepage for sys.stdout and sys.stderr. You can of
course rebind sys.std* if you change the codepage via chcp.com or
SetConsoleCP() and SetConsoleOutputCP(). If you do change the
codepage, it's considerate to remember the previous value and restore
it in an atexit function.

> I think there's also a module you can use for pre-3.6, sorry too lazy to
> do a search.

It's win_unicode_console [1].

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


Re: [Tutor] logging to cmd.exe

2017-09-26 Thread eryk sun
> cmd.exe can use cp65001 aka utf8???

CMD is a Unicode application that for the most part uses WinAPI
wide-character functions, including the console API functions (as does
Python 3.6+). There are a few exceptions. CMD uses the console
codepage when decoding batch files (line by line, so you can change
the codepage in the middle of a batch script), when writing output
from its internal commands (e.g. dir) to pipes and files (the /u
option overrides this), and when reading output from programs in a
`FOR /F` loop.

> Why does cmd.exe still use cp850?

In the above cases CMD uses the active console input or output
codepage, which defaults to the system locale's OEM codepage. If it's
not attached to a console (i.e. when run as a DETACHED_PROCESS), CMD
uses the ANSI codepage in these cases.

Anyway, you appear to be talking about the Windows console, which
people often confuse with CMD. Programs that use command-line
interfaces (CLIs) and text user interfaces (TUIs), such as classic
system shells, are clients of a given console or terminal interface. A
TUI application typically is tightly integrated with the console or
terminal interface (e.g. a curses application), while a CLI
application typically just uses standard I/O (stdin, stdout, stderr).
Both cmd.exe and python.exe are Windows console clients. There's
nothing special about cmd.exe in this regard.

Now, there are a couple of significant problems with using codepage
65001 in the Windows console.

Prior to Windows 8, WriteFile and WriteConsoleA return the number of
decoded wide characters written to the console, which is a bug because
they're supposed to return the number of bytes written. It's not a
problem so long as there's a one-to-mapping between bytes and
characters in the console's output codepage. But UTF-8 can have up to
4 bytes per character. This misleads buffered writers such as C FILE
streams and Python 3's io module, which in turn causes gibberish to be
printed after every write of a string that includes non-ASCII
characters.

Prior to Windows 10, with codepage 65001, reading input from the
console via ReadConsole or ReadConsoleA fails if the input has
non-ASCII characters. It gets reported as a successful read of zero
bytes. This causes Python to think it's at EOF, so the REPL quits (as
if Ctrl+Z had been entered) and input() raises EOFError.

Even in Windows 10, while the entire read doesn't fail, it's not much
better. It replaces non-ASCII characters with NUL bytes. For example,
in Windows 10.0.15063:

>>> os.read(0, 100)
abcαβγdef
b'abc\x00\x00\x00def\r\n'

Microsoft is gradually working on fixing UTF-8 support in the console
(well, two developers are working on it). They appear to have fixed it
at least for the private console APIs used by the new Linux subsystem
in Windows 10:

Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> s = os.read(0, 100)
abcαβγdef
>>> s
b'abc\xce\xb1\xce\xb2\xce\xb3def\n'
>>> s.decode()
'abcαβγdef\n'

Maybe it's fixed in the Windows API in an upcoming update. But still,
there are a lot of Windows 7 and 8 systems out there, for which
codepage 65001 in the console will remain broken.

> I always thought 65001 was not a 'real' codepage, even though some locales 
> (e.g. Georgia) use it [1].

Codepage 65001 isn't used by any system locale as the legacy ANSI or
OEM codepage. The console allows it probably because no one thought to
prevent using it in the late 1990s. It has been buggy for two decades.

Moodle seems to have special support for using UTF-8 with Georgian.
But as far as Windows is concerned, there is no legacy codepage for
Georgian. For example:

import ctypes
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

LD_ACP = LOCALE_IDEFAULTANSICODEPAGE = 0x1004
acp = (ctypes.c_wchar * 6)()

>>> kernel32.GetLocaleInfoEx('ka-GE', LD_ACP, acp, 6)
2
>>> acp.value
'0'

A value of zero here means no ANSI codepage is defined [1]:

If no ANSI code page is available, only Unicode can be used for
the locale. In this case, the value is CP_ACP (0). Such a locale
cannot be set as the system locale. Applications that do not
support Unicode do not work correctly with locales marked as
"Unicode only".

Georgian (ka-GE) is a Unicode-only locale [2] that cannot be set as
the system locale.

[1]: https://msdn.microsoft.com/en-us/library/dd373761
[2]: https://msdn.microsoft.com/en-us/library/ms930130.aspx
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Percentage of installations without setuptools (Was if __name__=='__main__' ...)

2017-08-11 Thread eryk sun
On Fri, Aug 11, 2017 at 6:27 PM, Alan Gauld via Tutor  wrote:
> On 11/08/17 19:13, Chris Warrick wrote:
>
>> False since Python 3.4/2.7.9. ensurepip installs Python on every new
>> Python install.
>
> Sorry Chris, that's not making sense? Do you mean ensurepip
> installs setuptools on every install? How does it do that if
> I don't have the internet connected? Does it wait for
> a connection then automatically do a download?

The Windows installer defaults to running ensurepip, which bundles
wheels for pip and setuptools. They may not be the latest versions.

> How would I tell if it is installed? Where do I look and
> for what? Because its not where I thought it would be
> - (in the libs)...

On Windows, look in "Lib\site-packages". In general you can `import
pip` and check pip.__file__. You should also have pip and pip3
commands. Run `pip3 show pip` to check the location.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-10 Thread eryk sun
On Fri, Aug 11, 2017 at 2:34 AM, Cameron Simpson  wrote:
>
> In files however, the default encoding for text files is 'utf-8': Python
> will read the file's bytes as UTF-8 data and will write Python string
> characters in UTF-8 encoding when writing.

The default encoding for source files is UTF-8. The default encoding
for text files is the locale's preferred encoding, i.e.
locale.getpreferredencoding(False). On Windows this is the locale's
ANSI codepage (e.g. 1252 in Western Europe). Also, the default newline
string in text files is os.linesep. On Windows this is "\r\n".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setup.py "script" vs "console_scripts" Was: if __name__=='main' vs entry points: What to teach new comers?

2017-08-09 Thread eryk sun
On Wed, Aug 9, 2017 at 11:32 PM, Alan Gauld via Tutor  wrote:
> On 09/08/17 22:15, Steven D'Aprano wrote:
>>
>> This is the critical factor. How can you use *by default* something that
>> is *NOT* supplied by default?
>
> I have to agree with Steven here. Any mature language should
> ship with all the tools needed to create and distribute a
> finished program. It is to Python's shame that it currently
> fails that test because the recommended distribution framework
> is not part of the standard language package. (and the irony
> is that the tool for installing the recommended format (pip)
> is in the standard library. You can download other peoples
> stuff but you can't make your own in the same format.
> That's bad.

The standard library has ensurepip [1] to bootstrap bundled wheels for
pip and setuptools, which in turn vendor other packages such as
distlib. The wheels are already there, unless the distro or IT
department has removed them. (They're distributed with the official
Windows installers, at least.) If you want the legal details, you
could ask on the pypa-dev list [2]. I'm sure Nick Coghlan or Donald
Stufft would be happy to clarify the situation.

[1]: https://github.com/python/cpython/tree/v3.6.2/Lib/ensurepip
[2]: https://groups.google.com/forum/#!forum/pypa-dev
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How does len() compute length of a string in UTF-8, 16, and 32?

2017-08-07 Thread eryk sun
On Tue, Aug 8, 2017 at 3:20 AM, Cameron Simpson  wrote:
>
> As you note, the 16 and 32 forms are (6 + 1) times 2 or 4 respectively. This
> is because each encoding has a leading byte order marker to indicate the big
> endianness or little endianness. For big endian data that is \xff\xfe; for
> little endian data it would be \xfe\xff.

To avoid encoding a byte order mark (BOM), use an "le" or "be" suffix, e.g.

>>> 'Hello!'.encode('utf-16le')
b'H\x00e\x00l\x00l\x00o\x00!\x00'

Sometimes a data format includes the byte order, which makes using a
BOM redundant. For example, strings in the Windows registry use
UTF-16LE, without a BOM.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference(s) betweenPython 3 static methods with and without @staticmethod?

2017-08-06 Thread eryk sun
On Sun, Aug 6, 2017 at 11:35 PM, boB Stepp  wrote:
>
> I see no difference in result, whether I use the @staticmethod decorator or 
> not.

While a staticmethod and a function are both descriptors [1], a
staticmethod is basically a no-op descriptor. Its __get__ method
always returns its unbound callable (i.e. its __func__ attribute)
instead of returning a computed property or bound method.

Examples

>>> descr = vars(MyOtherClass)['my_other_method']
>>> descr


>>> descr.__func__


When accessed as an attribute of a class (i.e. the instance argument
passed to __get__ is None), a staticmethod returns just its __func__.

>>> descr.__get__(None, MyOtherClass)


That's effectively no different from a Python 3 function. But they
diverge when accessed as an attribute of an instance. In this case the
__get__ method of a staticmethod also simply returns its __func__,

>>> descr.__get__(MyOtherClass(), MyOtherClass)


whereas the __get__ method of a function returns a method that's bound
to the instance.

>>> func = vars(MyClass)['my_method']
>>> func


>>> func.__get__(MyClass(), MyClass)
>

[1]: https://docs.python.org/3/reference/datamodel.html#implementing-descriptors
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setup.py "script" vs "console_scripts" Was: if __name__=='main' vs entry points: What to teach new comers?

2017-08-04 Thread eryk sun
On Fri, Aug 4, 2017 at 10:15 AM, Thomas Güttler
 wrote:
> Am 04.08.2017 um 02:50 schrieb Ben Finney:
>
>> Because Distutils implements only ‘scripts’, and that's not capable
>> enough for what people need so Setuptools implements entry points.
>>
>> In other words: One of them is in the standard library and does
>> something; the other is third-party and can do more.
>>
>> That answers why there are two. But maybe you wanted to ask some
>> underlying question?
>
> The underlaying question is: Imangine you are a newcomer. And there
> are two more choices. You need a guide like 'if unsure do x'. With
> other words: What is the sane default choice?

A newcomer should simply check for __name__ == "__main__". Learning
about packaging comes later.

> Chris wrote "Simple: `scripts` are legacy."
>
> You say it is the standard, and console_scripts is from a third party.

console_scripts and gui_scripts are installed by setuptools and pip,
which are developed under the umbrella of the Python Packaging
Authority [1]. The standard library also has the ensurepip [2] module
to install pip and setuptools. As third-party packages go, they're as
close to being 'standard' as you can get.

> For me "legacy" sound like "don't go this old way".

Legacy scripts aren't automatically created as console or GUI
executables when installed on Windows. Often Windows users associate
.py scripts with an editor, in which case legacy scripts aren't
executable from PATH, i.e. they have to be run as `python
legacy_script.py`, for example.

[1]: https://www.pypa.io
[2]: https://docs.python.org/3/library/ensurepip
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setup.py "script" vs "console_scripts" Was: if __name__=='main' vs entry points: What to teach new comers?

2017-08-03 Thread eryk sun
On Thu, Aug 3, 2017 at 4:22 PM, Chris Warrick  wrote:
>
> Simple: `scripts` are legacy. `entry_points` are the new thing.
> There’s also a third approach: gui_scripts entry_points, which work
> the same way on Linux/*nix, but on Windows, it means that running your
> script by opening the created .exe files does not show a console
> window. Note that stdout/stderr do not work in that mode under
> Windows, which can lead to spurious application crashes.  (GUI-only
> processes cannot use stdout/stderr because they don’t have a console
> attached)

A Windows GUI executable doesn't automatically inherit or create a
console for standard I/O. (It has to manually call AttachConsole or
AllocConsole.) But it does automatically inherit the standard handle
values from the parent process (just the integer values, not the
handles themselves). If these handles are inheritable and the child is
created with handle inheritance, then standard I/O will work. For
example:

C:\Temp>echo spam | pythonw -c print(input()) >out.txt 2>&1
C:\Temp>type out.txt
spam

In the above example pythonw.exe is a GUI executable. The example
reads "spam" from stdin and prints it to stdout, which is redirected
to a file named "out.txt". For those who don't know, `>out.txt` is
shell syntax to redirect stdout to "out.txt", and `2>&1` redirects
stderr (2) to stdout (1).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if __name__=='main' vs entry points: What to teach new comers?

2017-08-02 Thread eryk sun
On Wed, Aug 2, 2017 at 4:06 PM, Wolfgang Maier
 wrote:
> On 08/02/2017 04:57 PM, Steven D'Aprano wrote:
>
>> I have a module with a main() function and an "if __name__ == ..."
>> guard. Under what circumstances is that not sufficient, and I would want
>> console_scripts?
>
> If you install things using pip/setuptools and have defined a
> console_scripts entry point for it, then the corresponding wrapper
> script will be installed in whatever is considered the scripts directory
> at install time on that machine. With a bit of luck the entry point will
> thus be executable directly without any end-user intervention (like
> adding folders to $PATH and chmodding files).
> Personally, I always found it straightforward to write the wrapper
> script myself, then define this as a 'scripts' file in the package
> layout of my setup.py, but people's MMV.

For Windows, using frozen executables is preferred because
CreateProcess doesn't support shebangs. setuptools freezes entry-point
scripts with one of the following stub executables: console_scripts
(cli-32.exe, cli-64.exe) and gui_scripts (gui-32.exe, gui-64.exe).
Actually, it's better to create and install a wheel package, for which
pip uses the newer stubs from distlib: console_scripts (t32.exe,
t64.exe) and gui_scripts (w32.exe, w64.exe). Most Python 3
installations on Windows have at least two entry-point scripts:
pip.exe and easy_install.exe.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is meaning of "/" in "pow(x, y, z=None, /)"?

2017-08-02 Thread eryk sun
On Wed, Aug 2, 2017 at 1:06 AM, boB Stepp  wrote:
> I had typed help(pow) in the interpreter and got:
>
> 
> py3: help(pow)
> Help on built-in function pow in module builtins:
>
> pow(x, y, z=None, /)
> Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
>
> Some types, such as ints, are able to use a more efficient algorithm when
> invoked using the three argument form.
> 
>
> A quick scan of some of my Python books does not turn up the use of
> "/" as a function argument.  I have a nagging feeling I've read about
> this somewhere previously, but I cannot bring it to mind, and I have
> yet to stumble on a search that brings up an answer (Yet.).

We discussed this syntax several months ago:

https://mail.python.org/pipermail/tutor/2017-February/thread.html#110344
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pythonic ascii decoding!

2017-07-31 Thread eryk sun
On Mon, Jul 31, 2017 at 3:39 PM, bruce  wrote:
>
> So, is there a quick/dirty approach I can use to simply strip out the
> "non-ascii" chars. I know, this might not be the "best/pythonic" way,
> and that it might result in loss of some data/chars, but I can live
> with it for now.

Ignore or replace the non-ASCII characters. For example:

>>> print 's\xffp\xffa\xffm'.decode('ascii', 'ignore')
spam
>>> print 's\xffp\xffa\xffm'.decode('ascii', 'replace')
s�p�a�m
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python __del__ method

2017-07-11 Thread eryk sun
On Wed, Jul 12, 2017 at 12:07 AM, eryk sun  wrote:
> On Tue, Jul 11, 2017 at 2:47 PM, Jia Yue Kee  
> wrote:
>>
>> Case 2: If I were to run the code in "Interactive Mode", the following 
>> output will be obtained:
>>
>>>>> x = Robot("Tik-Tok")
>> Tik-Tok has been created!
>>>>> y = Robot("Jenkins")
>> Jenkins has been created!
>>>>> z = x
>>>>> z
>> <__main__.Robot object at 0x02D7E910>
>>>>> x
>> <__main__.Robot object at 0x02D7E910>
>>>>> del x
>>>>> del z
>>>>> del y
>> Robot has been destroyed
>>
>> My question being why is that "Robot has been destroyed" is only printed 
>> once in Case 2
>> (interactive mode) while it is printed out twice in Case 1 (script mode)?
>
> The REPL (interactive mode) keeps a reference to the last non-None
> result as builtins `_`, for convenient access to the last result. In
> your case, it results from evaluating `x`. Thus you have a hidden
> reference that's keeping that object alive. Enter anything except None
> or _ to clear that reference.

Well, not just any statement. It has to evaluate to another object
except None or the Robot object that _ currently references. For
example, enter 42.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python __del__ method

2017-07-11 Thread eryk sun
On Tue, Jul 11, 2017 at 2:47 PM, Jia Yue Kee  wrote:
>
> Case 2: If I were to run the code in "Interactive Mode", the following output 
> will be obtained:
>
 x = Robot("Tik-Tok")
> Tik-Tok has been created!
 y = Robot("Jenkins")
> Jenkins has been created!
 z = x
 z
> <__main__.Robot object at 0x02D7E910>
 x
> <__main__.Robot object at 0x02D7E910>
 del x
 del z
 del y
> Robot has been destroyed
>
> My question being why is that "Robot has been destroyed" is only printed once 
> in Case 2
> (interactive mode) while it is printed out twice in Case 1 (script mode)?

The REPL (interactive mode) keeps a reference to the last non-None
result as builtins `_`, for convenient access to the last result. In
your case, it results from evaluating `x`. Thus you have a hidden
reference that's keeping that object alive. Enter anything except None
or _ to clear that reference.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Probably a silly question: how to install lxml?

2017-07-05 Thread eryk sun
On Thu, Jul 6, 2017 at 1:51 AM, Steven D'Aprano  wrote:
>
> I dunno, to me it sounds like it *didn't* work, not if it is printing
> red error messages at the end. What do they say?

lxml should install from a wheel (e.g.
lxml-3.8.0-cp36-cp36m-win_amd64.whl). There's nothing to build, so the
most likely reason for failing would be getting denied access to
modify site-packages.

> My guess is that you need Administrator privileges to install the
> packages. Or perhaps to tell pip to install as site packages in your
> home directory, but I'm not sure how to do that.

If you've installed Python for all users in the default location in
%ProgramFiles% or %ProgramFiles(x86), then you will need to use an
elevated command prompt to install to the system site-packages. A
standard user or UAC restricted administrator does not have the right
to modify this directory.

Alternatively you can install a package just for the current user with
pip's "--user" option.

You can also install packages in a virtual environment created with
the venv module, which is convenient for development.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] call key on_press event multiple times when key is held down

2017-07-04 Thread eryk sun
On Tue, Jul 4, 2017 at 8:50 AM, Carlton Banks  wrote:
> I am using pynput  for keyboard events

You could use an event that enables a recording loop. The on_press and
on_release callbacks of the Listener [1] would set() and clear() this
event, respectively. For example:

import threading
from pynput import keyboard

def main():
do_record = threading.Event()

def on_press(key):
if key == keyboard.Key.cmd_l:
do_record.set()

def on_release(key):
if key == keyboard.Key.cmd_l:
do_record.clear()

with keyboard.Listener(on_press=on_press,
   on_release=on_release) as listener:

do_record.wait()

frames = []

while do_record.is_set():
print('Started recording')
# record and append audio frame

print('Stopped recording')
listener.join()

[1]: http://pythonhosted.org/pynput/keyboard.html#pynput.keyboard.Listener
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Image Library

2017-05-17 Thread eryk sun
On Thu, May 18, 2017 at 1:58 AM, Michael C
 wrote:
> when I run this, while it's called test.pyw, this pops up
>
> from PIL import Image
>
> im = Image.open('1.bmp')
> im.show()

Ok, I stand corrected. It's something weird, and most likely due to
Windows support here being an afterthought throw in just for coverage,
instead of something anyone actually cared about. The code does indeed
use os.system to show the file, which is just about the worst thing
one can do here for a Windows viewer.

>>> print(inspect.getsource(ImageShow.WindowsViewer))
class WindowsViewer(Viewer):
format = "BMP"

def get_command(self, file, **options):
return ('start "Pillow" /WAIT "%s" '
'&& ping -n 2 127.0.0.1 >NUL '
'&& del /f "%s"' % (file, file))

>>> print(inspect.getsource(ImageShow.Viewer.show_file))
def show_file(self, file, **options):
"""Display given file"""
os.system(self.get_command(file, **options))
return 1

The WindowsViewer class preferrably should override show_file to  call
ShellExecuteExW directly via ctypes, and wait (if possible) to delete
the temporary file. Or at least use subprocess.call with shell=True,
which will hide the console window. Note that being able to wait on
the image viewer is not guaranteed. For example, the image viewer in
Windows 10 is an app that cannot be waited on.

Since you already have a file on disk, you could skip PIL completely.
Just use os.startfile('1.bmp').
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Image Library

2017-05-17 Thread eryk sun
On Wed, May 17, 2017 at 10:33 PM, Michael C
 wrote:
> On Wed, May 17, 2017 at 3:30 PM, eryk sun  wrote:
>
>> You're probably running a .py script that's associated with py.exe or
>> python.exe. These executables create a new console (i.e. an instance
>> of the console host process, conhost.exe), if they don't inherit one
>> from their parent process. The new console defaults to creating a
>> visible window. Change the file extension to .pyw. This file extension
>> should be associated with pyw.exe or pythonw.exe, which will not
>> create a console.
>>
>> FYI, the cmd shell is unrelated to that console window. Windows users
>> often confuse the cmd shell with the console window that it uses. I
>> suppose it's less confusing on Linux. Like cmd, bash will inherit the
>> parent's terminal/console; however, it doesn't automatically spawn a
>> terminal on Linux if the parent doesn't have one. (Getting that
>> behavior on Windows requires the DETACHED_PROCESS creation flag.)
>> Since Windows users typically run cmd.exe to get a command-line shell
>> for running programs, they associate cmd.exe with the console window.
>> It isn't obvious that other programs create consoles without any
>> associated instance of cmd.exe.
>
> Actually, that is the whole script! I didn't get used to have the cmd.exe
> window pop up at all, could it be something I did?

Changing the script's extension to .pyw didn't prevent the console
from appearing? Or did you not try it?

Also, to reiterate, the cmd shell doesn't create or own any window,
and unless something really weird is going on, there's no cmd.exe
instance associated with the console window that you're seeing. cmd
can use a console via standard I/O File handles, and usually does, but
not always. It's no different from python.exe, powershell.exe, or any
other console application. Really, there is no such thing as a "cmd
window", "python window", or "PowerShell window". Because these are
long-running shell processes (e.g. Python's REPL), people are inclined
to think in those terms, and that's fine, but would you call it a
"where.exe window", "chcp.com window", "doskey.exe window",
"whoami.exe window", "findstr.exe window"? I think not. It's clear
that these programs simply use the console; they don't own it. Neither
do shells, but in the case of shells and other long-running console
processes, we're loose with language for convenience -- as long as it
doesn't confuse our understanding of how things really work.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Image Library

2017-05-17 Thread eryk sun
On Wed, May 17, 2017 at 8:24 PM, Michael C
 wrote:
> from PIL import Image
>
> im = Image.open('pic.bmp')
> im.show()
>
> I ran this code and it not only opened the picture in paint, which is what
> I want, but it also opens a CMD.exe console window! how do I prevent that from
> happening?

You're probably running a .py script that's associated with py.exe or
python.exe. These executables create a new console (i.e. an instance
of the console host process, conhost.exe), if they don't inherit one
from their parent process. The new console defaults to creating a
visible window. Change the file extension to .pyw. This file extension
should be associated with pyw.exe or pythonw.exe, which will not
create a console.

FYI, the cmd shell is unrelated to that console window. Windows users
often confuse the cmd shell with the console window that it uses. I
suppose it's less confusing on Linux. Like cmd, bash will inherit the
parent's terminal/console; however, it doesn't automatically spawn a
terminal on Linux if the parent doesn't have one. (Getting that
behavior on Windows requires the DETACHED_PROCESS creation flag.)
Since Windows users typically run cmd.exe to get a command-line shell
for running programs, they associate cmd.exe with the console window.
It isn't obvious that other programs create consoles without any
associated instance of cmd.exe.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Subtract a 1D Array from a 2D Array

2017-05-13 Thread eryk sun
On Sat, May 13, 2017 at 1:33 PM, Stephen P. Molnar
 wrote:
> I am using Python 3.6 in Anaconda3 Spyder IDE.
>
> I have two arrays:
>
> X   Y  Z
> a
> 0   0  0
>  2.059801   0  0
> -1.2031263.402953  0
> -1.108639   -1.5678532.715601
> -0.938564   -1.32733-2.299003
>a_c
> 0.42833750.91755 0.208299
>
> and want to subtract the value of the value of each column or the array a_c
> from the each value in the corresponding column of array c.
>
> Trying a_mm = a - a_c where a_mm is the new array results in the error
> message:
>
> ValueError: operands could not be broadcast together with shapes (3,5) (1,3)
>
> My search for a solution has not been successful.

You've presented array `a` as if it's 5x3, but the error says it's
3x5. If it were actually 5x3, there would be no problem:

import numpy as np

a = np.array([[0, 0, 0],
  [2.059801, 0, 0],
  [-1.203126, 3.402953, 0],
  [-1.108639, -1.567853, 2.715601],
  [-0.938564, -1.32733, -2.299003]])

a_c = np.array([[0.4283375, 0.91755, 0.208299]])

>>> a.shape
(5, 3)
>>> a_c.shape
(1, 3)
>>> a - a_c
array([[-0.4283375, -0.91755  , -0.208299 ],
   [ 1.6314635, -0.91755  , -0.208299 ],
   [-1.6314635,  2.485403 , -0.208299 ],
   [-1.5369765, -2.485403 ,  2.507302 ],
   [-1.3669015, -2.24488  , -2.507302 ]])

You can use the transpose of either array. For example, use `a.T` to
get the same number of columns, or use `a_c.T` to get the same number
of rows.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi all: How do I save a file in a designated folder?

2017-05-02 Thread eryk sun
On Tue, May 2, 2017 at 6:09 PM, Michael C
 wrote:
> screenshot.save("\test\missed.png")

You probably know that "\t" represents a tab in a string literal, but
there's something about working with a path that causes people to
overlook this. Windows won't overlook it. Control characters, i.e.
characters with an ordinal value below 32, are forbidden in Windows
file names.

Also, you're depending on whatever drive or UNC path is the current
directory. For example:

>>> os.chdir('C:\\')
>>> os.path.abspath('/test/missed.png')
'C:\\test\\missed.png'

>>> os.chdir('localhost\C$')
>>> os.path.abspath('/test/missed.png')
'localhost\\C$\\test\\missed.png'

A fully-qualified path must start with the drive or UNC share. For example:

>>> os.path.abspath('C:/test/missed.png')
'C:\\test\\missed.png'

>>> os.path.abspath('//localhost/C$/test/missed.png')
'localhost\\C$\\test\\missed.png'

In this case I'm using abspath() to normalize the path, which first
has Windows itself normalize the path via GetFullPathName(). This
shows what Windows will actually try to open or create, given that it
implements legacy DOS rules. For example, it ignores trailing spaces
and dots in the final path component:

>>> os.path.abspath('C:/test/missed.png ... ')
'C:\\test\\missed.png'

and DOS devices are virtually present in every directory:

>>> os.path.abspath('C:/test/nul.txt')
'.\\nul'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [ctypes-users] Press ESC to exit()

2017-05-01 Thread eryk sun
On Tue, May 2, 2017 at 3:03 AM, Michael C
 wrote:
> holy cow

The code for a global keyboard hook is a bit complex - mostly because
I had to use ctypes (properly instead of an unreliable hack). Normally
an application has one or more windows and a message loop, in which
case there's no need for such an awkward approach.

But you're asking to do this from a console application, which doesn't
own a window. The window procedure that gets keyboard input from the
system is in the console host process (conhost.exe). It processes
input as a sequence of events that it stores in its input buffer. Thus
you could use the input buffer instead of a global keyboard hook, but
that comes with its own set of problems. You're no longer getting
notified as soon as the character is typed in the window. Instead, the
escape-key monitor has to share the input buffer with the main
application.

Events can be read without removing them via PeekConsoleInput, or read
and removed via ReadConsoleInput. They can also be flushed (discarded)
via FlushConsoleInputBuffer. If your script doesn't read from the
console and the escape-key monitory only peeks the input buffer, it
will grow without bound. However, it shouldn't read or flush input
events that the main task needs, and the main task shouldn't read
events that causes the escape-key monitoring thread to miss the user
pressing escape. It needs an integrated design.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [ctypes-users] Press ESC to exit()

2017-05-01 Thread eryk sun
On Mon, May 1, 2017 at 6:28 PM, Michael C
 wrote:
> Hi all, I found out that one way to press ESC to kill the script was to use
> my previous
> script language, AutoHotKey and this is how it works:
>
> AutoHotKey code
>  ## function that kills the window with title '*Python 3.6.1 Shell*'
>
> kill()
> {
> WinKill, *Python 3.6.1 Shell*
> }
>
> ## When ESC is pressed, runs the function 'kill'
> Esc::kill()
>
> Yes, that's the entire script.
> Is there a way to write it in Python on windows?

Instead of using a hotkey, you can set a global low-level keyboard
hook. Then optionally you can check for a particular foreground window
before handling the key. For example:

import ctypes
import win32con
import win32gui

from ctypes import wintypes

user32 = ctypes.WinDLL('user32', use_last_error=True)

VK_ESCAPE = 0x1B

LLKHF_EXTENDED  = 0x0001
LLKHF_LOWER_IL_INJECTED = 0x0002
LLKHF_INJECTED  = 0x0010
LLKHF_ALTDOWN   = 0x0020
LLKHF_UP= 0x0080

ULONG_PTR = wintypes.WPARAM
class KBDLLHOOKSTRUCT(ctypes.Structure):
_fields_ = (('vkCode',  wintypes.DWORD),
('scanCode',wintypes.DWORD),
('flags',   wintypes.DWORD),
('time',wintypes.DWORD),
('dwExtraInfo', ULONG_PTR))

HOOKPROC = ctypes.WINFUNCTYPE(wintypes.LPARAM, ctypes.c_int,
wintypes.WPARAM, wintypes.LPARAM)

user32.SetWindowsHookExW.restype = wintypes.HHOOK
user32.SetWindowsHookExW.argtypes = (
ctypes.c_int,   # _In_ idHook
HOOKPROC,   # _In_ lpfn
wintypes.HINSTANCE, # _In_ hMod
wintypes.DWORD) # _In_ dwThreadId

user32.CallNextHookEx.restype = wintypes.LPARAM
user32.CallNextHookEx.argtypes = (
wintypes.HHOOK,  # _In_opt_ hhk
ctypes.c_int,# _In_ nCode
wintypes.WPARAM, # _In_ wParam
wintypes.LPARAM) # _In_ lParam

def keyboard_hook(handler, hwnd=None):
@HOOKPROC
def hookfunc(nCode, wParam, lParam):
event = KBDLLHOOKSTRUCT.from_address(lParam)
if hwnd is not None and win32gui.GetForegroundWindow() == hwnd:
handler(event)
return user32.CallNextHookEx(hHook, nCode, wParam, lParam)

hHook = user32.SetWindowsHookExW(win32con.WH_KEYBOARD_LL, hookfunc,
None, 0)
if not hHook:
raise ctypes.WinError(ctypes.get_last_error())

win32gui.PumpMessages()

if __name__ == '__main__':
import msvcrt
import threading
import win32console

print('Press escape to quit')

def escape_handler(event):
if event.vkCode == VK_ESCAPE:
# kill the hook thread
win32gui.PostQuitMessage(0)
elif not (event.flags & LLKHF_UP):
print('Virtual Key Code: {:#04x} [{}]'.format(event.vkCode,
event.time))

t = threading.Thread(target=keyboard_hook, args=(escape_handler,
win32console.GetConsoleWindow()), daemon=True)
t.start()

# consume keyboard input
while t.is_alive():
if msvcrt.kbhit():
msvcrt.getwch()

The __main__ demo should be run as a console script. The keyboard hook
filters on the console window handle from GetConsoleWindow().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between %f and %F string formatting?

2017-04-26 Thread eryk sun
On Thu, Apr 27, 2017 at 2:19 AM, Tim Peters  wrote:
> [boB Stepp ]
>
>>  I cannot find any discernible
>> difference between '%f' %  and '%F' %
>> .  Is there any or do they duplicate
>> functionality?  If the latter, why are there two ways of doing the
>> same thing?
>
> They differ only in the capitalization of the strings produced for
> NaNs and infinities (math.nan and math.inf in Python 3).
>
 "%f" % math.nan
> nan'
 "%F" % math.nan
> NAN'
 "%f" % math.inf
> inf'
 "%F" % math.inf
> INF'

In context this can be inferred from the docs, but I think f/F should
indicate the casing difference just like e/E, g/G, and x/X do.

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sets question

2017-04-26 Thread eryk sun
On Thu, Apr 27, 2017 at 1:34 AM, Phil  wrote:
> I did try {int(num)} but that resulted in an error that said something along
> the lines of int not being iterable. I'll have another look at that idea.

That exception indicates you probably used set(int(num)) instead of
either {int(num)} or set([int(num)]). The set() constructor needs an
iterable container such as a sequence (e.g. range, list, tuple, str).
An integer isn't iterable.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sets question

2017-04-26 Thread eryk sun
On Thu, Apr 27, 2017 at 12:33 AM, Phil  wrote:
> Another question I'm afraid.
>
> If I want to remove 1 from a set then this is the answer:
>
> set([1,2,3]) - set([1])

You can also use set literals here, with the caveat that {} is
ambiguous, and Python chooses to make it an empty dict instead of a
set.

>>> {1, 2, 3} - {1}
{2, 3}

related operations:

>>> s = {1, 2, 3}
>>> s -= {1}
>>> s
{2, 3}
>>> s.remove(2)
>>> s
{3}

> Ideally, I would like {'1'} to become {1}. Try as I may, I have not
> discovered how to remove the '' marks. How do I achieve that?

The quotation marks are the representation of a string object (i.e.
str). If you want an integer object (i.e. int), you have to convert
the value of the string to an integer.

>>> x = '1'
>>> {1, 2, 3} - {int(x)}
{2, 3}
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] classproperty for Python 2.7 (read-only enough)

2017-04-19 Thread eryk sun
On Wed, Apr 19, 2017 at 10:19 AM, Peter Otten <__pete...@web.de> wrote:
> Steven D'Aprano wrote:
>
>> As I said, I haven't had a chance to try Peter's code, so it's possible
>> that he's solved all these problems. I'm judging by previous
>
> No, my simple code only "works" for read-only properties and only as long as
> you don't overwrite the property by assigning to the attribute. To disallow
> writing you can define a
>
> def __set__(*args):
> raise AttributeError
>
> method, but that would only be invoked for instances of the class, not the
> class itself. For the same reason the setter of a read/write class property
> following that design would only be invoked in an instance, not in the
> class.
>
> The other simple solution, defining a normal property in the metaclass,
> works for the class (as expected, remember that the class is an instance of
> the metaclass), but is invisible to the instance:
>
 class T(type):
> ... @property
> ... def foo(self): return 42
> ...
 class Foo:
> ... __metaclass__ = T
> ...
 Foo.foo
> 42
 Foo.foo = "bar"
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: can't set attribute
 Foo().foo
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'Foo' object has no attribute 'foo'

This is a bit awkward, but you can take advantage of property() being
a data descriptor. When the attribute is looked up on the class, the
metaclass __getattribute__ or __setattr__ first searches the metaclass
MRO. If it finds a data descriptor, then it uses it unconditionally.
This means you can add a foo property to the class as well and have it
chain to the metaclass property. For example:

class T(type):
@property
def foo(self):
return 42

class C(object):
__metaclass__ = T
@property
def foo(self):
return type(self).foo

>>> C.foo
42
>>> C.foo = 0
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute
>>> o = C()
>>> o.foo
42
>>> o.foo = 0
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: can't set attribute
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp  wrote:
>
> I have not used the decimal module (until tonight).  I just now played
> around with it some, but cannot get it to do an exact conversion of
> the number under discussion to a string using str().

Pass a string to the constructor:

>>> d = decimal.Decimal('3.14159265358979323846264338327950288419716939')
>>> str(d)
'3.14159265358979323846264338327950288419716939'

When formatting for printing, note that classic string interpolation
has to first convert the Decimal to a float, which only has 15 digits
of precision (15.95 rounded down).

>>> '%.44f' % d
'3.14159265358979311599796346854418516159057617'
>>> '%.44f' % float(d)
'3.14159265358979311599796346854418516159057617'

The result is more accurate using Python's newer string formatting
system, which allows types to define a custom __format__ method.

>>> '{:.44f}'.format(d)
'3.14159265358979323846264338327950288419716939'
>>> format(d, '.44f')
'3.14159265358979323846264338327950288419716939'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 3:40 AM, boB Stepp  wrote:
>
> I have to say I am surprised by this as well as the OP.  I knew that
> str() in general makes a nice printable representation

The single-argument str() constructor calls the object's __str__
method (or __repr__ if __str__ isn't defined). In Python 3,
float.__str__ and float.__repr__ behave the same. They use as many
digits as is required to round trip back to the float value exactly.
That's up to 17 digits.

>>> str(1.2345678901234567)
'1.2345678901234567'
>>> str(1.)
'1.0'

In Python 2, float.__str__ uses up to 12 digits:

>>> str(1.2345678901234567)
'1.23456789012'

> realize that using str() on an arbitrarily typed in "float-like" value
> would convert the typed in value to a normal float's max precision.

For a literal floating-point value in source code, the compiler
(component of the interpreter) first parses a NUMBER node:

>>> e = parser.expr('3.14159265358979323846264338327950288419716939')
>>> p = parser.st2list(e)
>>> while p[0] != token.NUMBER:
... p = p[1]
...
>>> p
[2, '3.14159265358979323846264338327950288419716939']

Next it transforms this concrete syntax tree into a abstract syntax tree:

>>> a = ast.parse('3.14159265358979323846264338327950288419716939',
...   mode='eval')
>>> ast.dump(a)
'Expression(body=Num(n=3.141592653589793))'

>>> a.body.n
3.141592653589793
>>> type(a.body.n)


You see above that the AST transforms the NUMBER node into a Num node
that references a float object. The value of the float is the closest
possible approximation of the source code literal as a
double-precision binary float.

If the compiler instead used Decimal objects, then it could retain all
of the literal's precision. Double-precision binary floats are fast
and efficient by virtue of hardware support, but that's not a
compelling reason in Python. Maybe some future version of Python will
switch to using Decimals for floating-point literals.

The final step is to compile this AST into bytecode and create a code
object. The float value is referenced in the code object's co_consts
attribute:

>>> c = compile(a, '', 'eval')
>>> c.co_consts
(3.141592653589793,)

The code in this case is simple; just load and return the constant value:

>>> dis.dis(c)
  1   0 LOAD_CONST   0 (3.141592653589793)
  3 RETURN_VALUE

>>> eval(c)
3.141592653589793
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading files in Python 3

2017-03-31 Thread eryk sun
On Thu, Mar 30, 2017 at 8:45 PM, Mats Wichmann  wrote:
> Yeah, fun.  You need to escape the \ that the idiot MS-DOS people chose
> for the file path separator. Because \ is treated as an escape character.

The COMMAND.COM shell inherited command-line switches (options) that
use slash from TOPS-10 by way of CP/M, so using backslash in paths was
less ambiguous for the shell (e.g. dir/w could be intended to run "w"
in the "dir" subdirectory, or it could mean to run "dir" with the
option "/w"). The DOS kernel did support both slash and backslash in
file-system paths.

Also, C wasn't a common language on the PC back then. BASIC was.
Instead of using escapes in string literals, BASIC used addition at
runtime with the CHR$ function or predefined constants. Support for
hierarchical paths (DOS 2.0) came around at about the same time that C
was rising in popularity, so the pain came on slowly like boiling a
lobster.

The system designers who really have no excuse are the NT kernel
developers circa 1988-93. They were working in C on a system that
already required converting paths from the DOS namespace to a native
object namespace. They could have easily implemented the native object
system to use slash instead of backslash.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading files in Python 3

2017-03-30 Thread eryk sun
On Thu, Mar 30, 2017 at 8:47 PM, Zachary Ware
 wrote:
> In this case, the problem is the bogus Unicode escape that you
> inadvertently included in your path: `\Us...`.  To fix it, either use a
> 'raw' string (`r"C:\Users\..."`) or use forward slashes rather than
> backslashes, which Windows is happy to accept.

Forward slash is acceptable when all you need to support is DOS-style
paths that are limited to DOS semantics and MAX_PATH. A raw string
works, except not for paths ending in backslash and not for unicode
strings in Python 2. A generic solution is to use a pathlib.Path() in
3.x, which normalizes the path to use backslash, or os.path.normpath()
in 2.x.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen / proc.communicate issue

2017-03-30 Thread eryk sun
On Thu, Mar 30, 2017 at 10:51 PM, Cameron Simpson  wrote:
> This suggests that .communicate uses Threads to send and to gather data
> independently, and that therefore the deadlock situation may not arise.

For Unix, communicate() uses select or poll. It uses threads on
Windows. Either way it avoids deadlocking.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exponential function

2017-02-14 Thread eryk sun
On Tue, Feb 14, 2017 at 11:01 PM, Alan Gauld via Tutor  wrote:
> To compute it if you don't know x in advance then yes,
> use something like
>
> value = 10**x
>
> But if you know the value in advance you can write it in
> a more compact form as:
>
> value = 1e5  # or 3e7 or whatever...

10**5 is an int and 1e5 is a float. This could lead to a significant
loss of precision depending on the calculation. For example:

>>> x = 10**5 * 1234567890123456789
>>> y = 1e5 * 1234567890123456789
>>> x - y
16777216.0

Replacing 10**5 with 10 is a compile-time optimization (constant
expression folding), so you needn't worry about manually optimizing
it. For example:

>>> compile('10**5', '', 'exec').co_consts
(10, 5, None, 10)

The compiled bytecode refers to the pre-computed constant:

>>> dis.dis(compile('10**5', '', 'exec'))
  1   0 LOAD_CONST   3 (10)
  3 POP_TOP
  4 LOAD_CONST   2 (None)
  7 RETURN_VALUE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python-list thread: int vs. float

2017-02-12 Thread eryk sun
On Sat, Feb 11, 2017 at 8:53 PM, boB Stepp  wrote:
>
> I suspect Eryk had set a normal 's' as an identifier for the character
> code sequence that produces the non-ASCII output, but forgot to show
> us that step.  But I could be mistaken.

Sorry, I forgot to show the assignment of the string "௧꘢୩" to the variable s.

> Today I am working in Windows 7, not Linux Mint.  Of course when I
> attempted to copy and paste the non-ASCII sequence from Gmail into
> cmd.exe I got 3 rectangular boxes on the paste line, indicating
> cmd.exe could not translate those characters.

If you're running python.exe, it either inherits or allocates a
console, which is hosted by an instance of conhost.exe. At most you're
inheriting a console from cmd.exe. That's the extent of its
involvement. Think of conhost.exe as a terminal window, like GNOME
Terminal, etc. cmd.exe and powershell.exe are shells that use standard
I/O, much like Python's REPL -- or like running bash in a Unix
terminal.

The console window can handle characters in the basic multilingual
plane (BMP). Python 3.6 uses the console's wide-character (Unicode)
API, so it should have no problem reading the string "௧꘢୩" if typed or
pasted into the console. For example, if it's assigned to `s`, then
ascii(s) should show the correct \u literals:

>>> ascii(s)
"'\\u0be7\\ua622\\u0b69'"

Unfortunately, for rendering text the Windows console has limited font
support. It requires a strictly monospace font, among other criteria.
It won't switch automatically to other fonts when the current font
doesn't have a glyph for a character. It just displays an empty box
glyph. In Windows 7 a good font choice for the console window is
Consolas. It has pretty good coverage for Western languages, but not
for any of the characters in "௧꘢୩".

If you copy the empty-box glyphs from the console window to the
clipboard, it's actually copying the Unicode characters, which can be
pasted into a window that has better font support, such as notepad. So
in 3.6 this is just a superficial rendering issue that can be
addressed by using programs such as ConEmu that replace the standard
console window (it hides the console and hooks the API to write to its
own window).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python-list thread: int vs. float

2017-02-11 Thread eryk sun
On Sat, Feb 11, 2017 at 8:06 AM, Steven D'Aprano  wrote:
> Valid digits for integers include 0 through 9 in decimal

Note that Python 3 uses the Unicode database to determine the decimal
value of characters, if any. It's not limited to the ASCII decimal
digits 0-9. For example:

>>> s
'௧꘢୩'
>>> int(s)
123
>>> print(*(unicodedata.name(c) for c in s), sep='\n')
TAMIL DIGIT ONE
VAI DIGIT TWO
ORIYA DIGIT THREE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-11 Thread eryk sun
On Sat, Feb 11, 2017 at 7:35 AM, boB Stepp  wrote:
> Has this PEP been implemented yet?  I am running Python 3.5.2 and it
> appears not to work.  Also, in "What's New In Python 3.6"
> (https://docs.python.org/3/whatsnew/3.6.html) I did not see a mention
> of it.

You can see in the document header that PEP 457 is an Informational
document at the draft stage, as opposed to a Standards Track document
that's finalized. It's the best I could find as provisional
documentation of CPython's usage of '/' to mark positional-only
arguments of built-in functions. FYI, the PEP's author, Larry
Hastings, is also the author of Argument Clinic.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 6:22 AM, boB Stepp  wrote:
> On Fri, Feb 10, 2017 at 11:49 PM, eryk sun  wrote:
>
>> It's from the function's __text_signature__.
>>
>> >>> repr.__text_signature__
>> '($module, obj, /)'
>>
>> It means "obj" is a positional-only argument that cannot be passed as a 
>> keyword.
>
> [snip]
>
>> Look at open() for comparison.
>>
>> >>> open.__text_signature__
>> "($module, /, file, mode='r', buffering=-1, encoding=None,\n
>> errors=None, newline=None, closefd=True, opener=None)"
>
> In your comparison example, a forward slash appears again, but this
> time after "$module" in the location where "obj" previously appeared
> in the repr example.  Is it indicating a similar use here?

All parameters listed before the slash are position-only. Everything
after the slash can be passed as a keyword argument.

> Can you provide a link to "__text_signature__" in the docs?  I tried
> searching for it, but came up empty.

No, I can't. It was added in issue 19674.

http://bugs.python.org/issue19674

The '/' syntax for positional-only arguments is documented in PEP 457.

https://www.python.org/dev/peps/pep-0457

Also see the how-to for CPython's "Argument Clinic" preprocessor,
since (for now) positional-only arguments are only a concern for
built-in functions.

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


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 3:22 AM, boB Stepp  wrote:
>
> py3: help(repr)
> Help on built-in function repr in module builtins:
>
> repr(obj, /)
> Return the canonical string representation of the object.
>
> For many object types, including most builtins, eval(repr(obj)) == obj.
>
> Question:  What does the forward slash represent in this context?

It's from the function's __text_signature__.

>>> repr.__text_signature__
'($module, obj, /)'

It means "obj" is a positional-only argument that cannot be passed as a keyword.

>>> s = inspect.signature(repr)
>>> s.parameters['obj'].kind
<_ParameterKind.POSITIONAL_ONLY: 0>

CPython has a lot of built-in functions that don't allow keyword arguments.

>>> repr(obj=42)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: repr() takes no keyword arguments

Look at open() for comparison.

>>> open.__text_signature__
"($module, /, file, mode='r', buffering=-1, encoding=None,\n
errors=None, newline=None, closefd=True, opener=None)"

All of its parameters can be passed as keyword arguments.

>>> s = inspect.signature(open)
>>> s.parameters['file'].kind
<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>
>>> s.parameters['mode'].kind
<_ParameterKind.POSITIONAL_OR_KEYWORD: 1>

For example:

>>> open(file='spam', mode='w')
<_io.TextIOWrapper name='spam' mode='w' encoding='UTF-8'>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 4:32 AM, boB Stepp  wrote:
>
> This bit got me experimenting.  Since the integer "5" is an integer
> object instance, I am wondering why I can't do:
>
> py3: 5.__repr__()
>   File "", line 1
> 5.__repr__()
>  ^
> SyntaxError: invalid syntax
>
> , but I can do:
>
> py3: x = 5
> py3: x.__repr__()
> '5'

The parser sees "5." as a floating point number. You can use
parentheses to force it to parse 5 as an integer:

>>> (5).__repr__()
'5'

> But in class examples I've seen posted, I do not recall __repr__ ever
> being defined.  So I infer that most of the time I should not or need not
> do so, but under what circumstances should I do so?

Classes inherit a default __repr__ from `object`. For example:

>>> object.__repr__(5)
''

Define a custom __repr__ when it's useful for debugging to include
additional information. It also gets called for str() if you don't
implement a custom __str__.

> Another curiosity question.  If I type:
>
> py3: repr(int)
> ""
>
> I get what I expect, but if I do the same with my custom class, "boB",
> I instead get:
>
> py3: repr(boB)
> ""
>
> Why the difference between the Python class "int" and my custom class
> "boB"?  Did I not define __repr__() correctly?

The __repr__ defined by your class is used for instances of the class,
not for the class itself. In the above you're seeing the return value
from the metaclass __repr__ that's defined by `type`:

In CPython, type.__repr__ is implemented in C as type_repr in
Objects/typeobject.c. If it can determine the module and qualified
name of a class, then it uses the template "".
Otherwise it uses the template "" with the basic char
*tp_name from the PyTypeObject.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test for type(object) == ???

2017-02-10 Thread eryk sun
On Sat, Feb 11, 2017 at 1:34 AM, boB Stepp  wrote:
> I was playing around with type() tonight.  If I type (pun intended), I get:
>
> py3: type(5)
> 

`type` is a metaclass that either creates a new class (given 3
arguments: name, bases, and dict) or returns a reference to the class
of an existing object. type(5) is the latter case, and it returns a
reference to the class `int`. What you see printed in the REPL is
repr(int), a string representation of the class object:

 >>> repr(int)
 ""

Speaking of classes and metaclasses, note that you can't call
int.__repr__(int) to get this representation, because the __repr__
special method of int is meant for instances of int such as int(5).
Instead you have to explicitly use __repr__ from the metaclass, i.e.
`type`:

>>> type(int)

>>> type.__repr__(int)
""

But just use built-in repr().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sort() method and non-ASCII

2017-02-05 Thread eryk sun
On Sun, Feb 5, 2017 at 10:30 PM, boB Stepp  wrote:
> I was looking at http://unicode.org/charts/  Because they called them
> charts, so did I.  I'm assuming that despite this organization into
> charts, each and every character in each chart has its own unique
> hexadecimal code to designate each character.

Those are PDF charts (i.e. tables) for Unicode blocks:

https://en.wikipedia.org/wiki/Unicode_block

A Unicode block always has a multiple of 16 codepoints, so it's
convenient to represent the ordinal values in hexadecimal.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sort() method and non-ASCII

2017-02-04 Thread eryk sun
On Sun, Feb 5, 2017 at 3:52 AM, boB Stepp  wrote:
> Does the list sort() method (and other sort methods in Python) just go
> by the hex value assigned to each symbol to determine sort order in
> whichever Unicode encoding chart is being implemented?

list.sort uses a less-than comparison. What you really want to know is
how Python compares strings. They're compared by ordinal at
corresponding indexes, i.e. ord(s1[i]) vs ord(s2[i]) for i less than
min(len(s1), len(s2)).

This gets a bit interesting when you're comparing characters that have
composed and decomposed Unicode forms, i.e. a single code vs multiple
combining codes. For example:

>>> s1 = '\xc7'
>>> s2 = 'C' + '\u0327'
>>> print(s1, s2)
Ç Ç
>>> s2 < s1
True

where U+0327 is a combining cedilla. As characters, s1 and s2 are the
same. However, codewise s2 is less than s1 because 0x43 ("C") is less
than 0xc7 ("Ç"). In this case you can first normalize the strings to
either composed or decomposed form [1]. For example:

>>> strings = ['\xc7', 'C\u0327', 'D']
>>> sorted(strings)
['Ç', 'D', 'Ç']

>>> norm_nfc = functools.partial(unicodedata.normalize, 'NFC')
>>> sorted(strings, key=norm_nfc)
['D', 'Ç', 'Ç']

[1]: https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Python 3 module to create simple text editor in Windows command prompt?

2017-02-02 Thread eryk sun
On Fri, Feb 3, 2017 at 2:22 AM, boB Stepp  wrote:
> What would be the best Python 3 module to best assist me in controlling
> the command prompt window display, font colors, positioning the cursor
> dynamically, etc.?

Try using curses [1]. Christoph Gohlke has a port for Windows Python
based on the PDCurses library [2].

[1]: https://docs.python.org/3/howto/curses.html
[2]: http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ReadableInt

2016-12-30 Thread eryk sun
On Fri, Dec 30, 2016 at 4:46 PM, Albert-Jan Roskam
 wrote:
>
> Remember I only intend to use it while debugging the given script.

Have you tried hooking sys.displayhook?

> Why does the call to str() below return '1' and not 'one'? Should I implement 
> __new__
> because int is immutable?

You can't reliably replace a built-int type with a late binding. The
concrete C API (in this case PyLong_FromLong) is called directly by
the compiler:

>>> str(1)

Breakpoint 0 hit
python35_d!PyAST_FromNodeObject:
`5ea92ad0 4c894c2420  mov qword ptr [rsp+20h],r9
  ss:0021`b39ef2d8=
  {python35_d!_PyParser_Grammar
   (`5ed84ad8)}

0:000> bp python35_d!PyLong_FromLong
0:000> g
Breakpoint 1 hit
python35_d!PyLong_FromLong:
`5e95fee0 894c2408mov dword ptr [rsp+8],ecx
  ss:0021`b39ee9e0=c19956a8

0:000> kc 0n15
Call Site
python35_d!PyLong_FromLong
python35_d!parsenumber
python35_d!ast_for_atom
python35_d!ast_for_atom_expr
python35_d!ast_for_power
python35_d!ast_for_expr
python35_d!ast_for_call
python35_d!ast_for_trailer
python35_d!ast_for_atom_expr
python35_d!ast_for_power
python35_d!ast_for_expr
python35_d!ast_for_testlist
python35_d!ast_for_expr_stmt
python35_d!ast_for_stmt
python35_d!PyAST_FromNodeObject

0:000> r rcx
rcx=0001

0:000> bd 0-1; g
'1'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Open a libreoffice calc file in Python

2016-12-22 Thread eryk sun
On Thu, Dec 22, 2016 at 4:20 PM, boB Stepp  wrote:
>
> Both you and Eryk seem to be speaking in terms of using
> subprocess.Popen() directly.  So I think I need some clarification.
> At 
> https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module
> it says:
>
> "The recommended approach to invoking subprocesses is to use the run()
> function for all use cases it can handle. For more advanced use cases,
> the underlying Popen interface can be used directly.

The high-level functions such as `run` pass their positional arguments
and Popen keyword arguments to the Popen constructor. Everything
stated about handling args as a list or string and shell=True applies
the same to the high-level API.

> My current understanding as to why the subprocess module is preferred
> to using the older os.system() is to avoid shell injection attacks.
> So my assumption is that even when using "shell=True" with either
> run() or Popen(), this is avoided.  Is this true?

Using shell=True is insecure. The command-line string is passed
directly to the shell, the same as with os.system. Try to avoid using
shell=True as much as possible, especially when the command is based
on user input.

But the subprocess module has more to offer other than just avoiding
the shell. For example, it allows communicating directly with the
child process using standard I/O (stdin, stdout, stderr) pipes;
controlling inheritance of Unix file descriptors or Windows handles;
and creating a new Unix session or Windows process group.

On Windows, Popen also allows passing creationflags [1] (e.g.
CREATE_NEW_CONSOLE, CREATE_NO_WINDOW, DETACHED_PROCESS) and a subset
of the process startupinfo [2], including the standard handles and
wShowWindow.

[1]: https://msdn.microsoft.com/en-us/library/ms684863
[2]: https://docs.python.org/3/library/subprocess.html#subprocess.STARTUPINFO

> Are there subtleties as to when to use run() and when to use Popen()?

The high-level API executes a child process synchronously -- i.e.
write some (optional) input, read the output, close the standard I/O
files, and wait for the process to exit. For example, here's the
implementation of `run` in 3.5:

def run(*popenargs, input=None, timeout=None, check=False, **kwargs):
if input is not None:
if 'stdin' in kwargs:
raise ValueError(
'stdin and input arguments may not both be used.')
kwargs['stdin'] = PIPE
with Popen(*popenargs, **kwargs) as process:
try:
stdout, stderr = process.communicate(
input, timeout=timeout)
except TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
raise TimeoutExpired(process.args, timeout,
 output=stdout, stderr=stderr)
except:
process.kill()
process.wait()
raise
retcode = process.poll()
if check and retcode:
raise CalledProcessError(retcode, process.args,
 output=stdout, stderr=stderr)
return CompletedProcess(process.args, retcode, stdout, stderr)

In most cases the high-level API is all that you'll need. Rarely you
may need to execute a child process asynchronously and interact with a
long-running process. In that case call Popen directly.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Open a libreoffice calc file in Python

2016-12-21 Thread eryk sun
On Thu, Dec 22, 2016 at 4:50 AM,   wrote:
> BTW, the array form is Popen's default mode; sensibly you need to _ask_ to
> use a shell string with shell=True, because that is harder and more fragile.

Without shell=True, args as a string on POSIX is generally an error
because it will look for the entire string as the executable. The
exception is if the string has no command-line arguments (e.g.
Popen('ls')).

On Windows it's always acceptable to pass args as a string. For a
complex command line it may be easier to pass args as a list and let
Popen call list2cmdline to create a string that's properly quoted and
escaped. Here 'properly' assumes the target executable parses its
command line using VC++ rules. It may use custom rules, in which case
you have to pass args as a string.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble Launching Python

2016-12-21 Thread eryk sun
On Tue, Dec 20, 2016 at 7:12 PM, George Fischhof  wrote:
> 2016-12-19 23:38 GMT+01:00 Joseph Olugbohunmi via Tutor :
>
> this is a Microsoft visual c++ redistributable is missing from system.
> I found this stackoverflow link which seems to describing a solution
>
> http://stackoverflow.com/questions/33265663/
> api-ms-win-crt-runtime-l1-1-0-dll-is-missing-when-opening-microsoft-office-file

There is no reason in this case to install the VC++ redistributable
package. Python is written in C, so it only needs the C runtime, which
is now an OS component. I provided a link for the updated download for
the Universal C Runtime, KB3118401. The above Stack Overflow answer
mentions KB2999226, which is the old, outdated update from well over a
year ago.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trouble Launching Python

2016-12-20 Thread eryk sun
On Mon, Dec 19, 2016 at 10:38 PM, Joseph Olugbohunmi via Tutor
 wrote:
> Hello,Good day, I installed Python 3.5.2 on my Windows 8.1 PC and then I 
> tried launching
> IDLE as well as the Interpreter but I got a message that 
> api-ms-win-crt-runtime-l1-1-0.dll was
> missing. I downloaded and installed that after which I got another message 
> that
> api-ms-win-crt-math-l1-1-0.dll was also missing, I got that, and then another 
> dll was missing
> and it goes on and on. Please what can I do?

The implementation of Python that you're using is written in C and
relies on the C runtime library to provide a measure of cross-platform
portability. The latest C runtime from Microsoft is called the
"Universal CRT". It's an operating system component. As such, you
should already have ucrtbase.dll and all of the API set DLLs, such as
api-ms-win-crt-runtime-l1-1-0.dll, assuming your system is up to date
via Windows Update. If for some reason your system doesn't have this
update, you can manually download and install it from the following
link:

https://www.microsoft.com/en-us/download/details.aspx?id=50410

Install either "Windows8.1-KB3118401-x64.msu" (64-bit) or
"Windows8.1-KB3118401-x86.msu" (32-bit), depending on whether your
version of Windows is 64-bit or 32-bit.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Q regarding external program calling

2016-11-07 Thread eryk sun
On Sun, Nov 6, 2016 at 9:09 AM, Alan Gauld via Tutor  wrote:
> On 06/11/16 01:44, Clayton Kirkwood wrote:
>> Looked all over, but haven't found the answer. If I have a (windows) program
>> which I wish to start, even shell scripts, and possibly capture the output
>> from, how do I do that?
>
> Others have already pointed you to the subprocess module.
> The documentation there includes several examples.
>
> On Windows it sometimes helps to start a program via the
> "start" command.

`start` is a cmd shell built-in command, so it requires passing
shell=True to subprocess.Popen. It allows more control over how a
process is started -- such as whether to use the current or a new
console, the window state, the process working directory, priority,
and CPU affinity -- and whether the shell should wait on the process.
Popen directly supports most of what `start` does, and otherwise you
can use os.startfile, which is generally safer than using the shell.

> start notepad.exe

Using `start` like this is typical in a batch file, to avoid waiting
for the command to exit. In contrast, Popen defaults to asynchronous
mode, and waiting requires the wait() or communicate() methods.

> start myfile.txt

`start` isn't generally necessary here. Both with and without `start`,
cmd tries to locate and execute (open) the file.

If the target is a batch file (i.e. .bat or .cmd), then cmd either
executes it in place or, if the `start` command is used, executes it
in a new shell via `cmd.exe /k`. Otherwise cmd first tries to execute
the target via CreateProcess(). If the latter fails, cmd tries
ShellExecuteEx(), using the default file association.

For example, the .py extension is usually associated with the ProgId
"Python.File". If the py launcher is installed, then the default
action for this ProgId is typically the template command
'C:\Windows\py.exe "%1" %*'. ShellExecuteEx substitutes the target
file and command-line arguments for the %1 and %* template parameters
and then calls CreateProcess.

The `start` command expands the set of possible targets to whatever
ShellExecuteEx supports, including directories and virtual targets.
For example:

* Opening directories in Explorer
* Opening `shell:` folders such as shell:UserProgramFiles
* Executing "App Paths" commands registered under
  [HKCU|HKLM]\Software\Microsoft\Windows\CurrentVersion\App Paths

> If your program has a GUI however, accessing the output
> becomes much more difficult and may not even be
> possible. In that case you may need to resort
> to driving it programmatically via its API using
> a tool like pywin32 or ctypes.

The best-case scenario is an app that exports a COM IDispatch
interface for scripting, which you can access via PyWin32's win32com
module.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 3.5 installation beside Python 2.3 on Windows 7

2016-10-22 Thread eryk sun
On Sun, Oct 23, 2016 at 1:39 AM, Pierre-Michel Averseng
 wrote:
> Le 22/10/2016 à 20:00, tutor-requ...@python.org a écrit :
>
>> Do I have to completely uninstall Python 2.7.1 and then install Python
>> 3.5.2? Or can I still install Python 3.5.2 keeping Python 2.7.1 untouched
>> on my laptop?
>
> For Windows (including XP, Vista, 7, and 8), Python comes as a
> self-installer MSI program file—simply double-click on its file icon, and 
> answer
> Yes or Next at every prompt to perform a default install. The default install
> includes Python’s documentation set and support for tkinter (Tkinter in Python
> 2.X) GUIs, shelve databases, and the IDLE development GUI. Python 3.3 and
> 2.7 are normally installed in the directories C:\Python33 and C:\Python27
> though this can be changed at install time.

3.5 doesn't support Windows XP. It also switched to using executable
installers that are built using the WiX toolset. The web installer
downloads up to 22 MSI packages on demand. Even the 'offline'
installer has to download the MSIs for the debug binaries, since
they're not commonly installed and including them would make the
offline installer substantially larger.

Also, the default installation targets have changed to
"%ProgramFiles[(x86)]%\Python35[-32]" for all-users installs and
"%LocalAppData%\Programs\Python\Python35[-32]" for per-user installs.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using python shell program on windows

2016-10-14 Thread eryk sun
On Fri, Oct 14, 2016 at 4:52 PM, Peter Otten <__pete...@web.de> wrote:
>
> python -m pip install timesheet
>
> on the commandline should take care of the details. On my (Linux) machine
> this also installed a script that I can invoke on the command line with
>
> $ timesheet start foo bar
> Started task:
> Subject foo
> Title   bar
> Start   2016-10-14 18:49
> End
> Duration00:00
> $
>
> If that does not work on Windows there's probably a Windows user who knows
> what to try instead.

I haven't used this package before, but I can attest that the wheel
and its dependencies do install without error on Windows, and it
appears to be working.

It says it supports Python 3, but I discovered it has a raw_input call
in the implementation of its start command. So I suggest using Python
2 instead.

The installation creates a timesheet.exe script wrapper in Python's
"Scripts" folder. Unless you're using a virtual environment, you'll
have to add this directory to the PATH environment variable to run
timesheet from the command line.

Running timesheet warns about a missing configuration file,
"%LOCALAPPDATA%\timesheetrc" (expanded for your %LOCALAPPDATA%
directory). I created this as an empty file to avoid the warning.

If you need to back up or delete the timesheet database, it's located
at "%LOCALAPPDATA%\timesheet\timesheet.sqlite".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with Numpy

2016-09-21 Thread eryk sun
On Wed, Sep 21, 2016 at 10:35 PM, Oscar Benjamin
 wrote:
> I would have given the same advice a year or so ago. It's worth noting
> though that the situation with pip, wheel etc has improved significantly
> recently. It's now straightforward to pip install Numpy, scipy, matplotlib
> and many others on Windows, OSX and Linux. Try it!

There are NumPy and Matplotlib wheels for Windows on PyPI, but not for
SciPy. Building SciPy from source on Windows is not straightforward.
If you try to build with just VS 2015 installed, it fails because the
required LAPACK/BLAS libs aren't available, and there's no Fortran
compiler (Intel MKL is not free). For Windows users, I recommend
downloading the wheels for NumPy and SciPy from Gohlke's site -- or
just use a batteries-included distro as Alan recommended.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python IDLE for Windows 10, 64-bit

2016-09-21 Thread eryk sun
On Wed, Sep 21, 2016 at 9:21 PM, Alan Gauld via Tutor  wrote:
> On Windows it is usually installed by default at:
>
> %PYTHONDIR%/Lib/idelib/idle.bat

With Python 3, you can run the package as a script:

pythonw -m idlelib

Python 2:

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


Re: [Tutor] help with Numpy

2016-09-21 Thread eryk sun
On Wed, Sep 21, 2016 at 2:53 PM, Paul Dentinger  wrote:
>
> I need Numpy and Scipy and may need matplotlib.  So I tried to call Numpy and 
> it
> can't be found.  So I go online and find Numpy and download it.

Read the following docs on Python packaging and pip:

https://pip.pypa.io
https://packaging.python.org

I recommend Christoph Gohlke's MKL builds of NumPy and SciPy:

http://www.lfd.uci.edu/~gohlke/pythonlibs
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python IDLE for Windows 10, 64-bit

2016-09-21 Thread eryk sun
On Wed, Sep 21, 2016 at 3:14 PM, Aaron Rose  wrote:
> I'm looking for a link for Python IDLE for Windows 10, 64-bit.  Can't seem to 
> find
> the right link on python.org.  Could someone kindly point me in the right 
> direction?

Python's installer has an option to install IDLE. Here's the link for
the latest stable version of the web-based installer for x64:

https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64-webinstall.exe
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] @property for old style classes vs new style classes

2016-09-15 Thread eryk sun
On Thu, Sep 15, 2016 at 9:48 PM, monik...@netzero.net
 wrote:
> But why var is in class __dict__?
> Does @property make it a class attribute?

An instance of `property` is a data descriptor, i.e. it implements the
__get__, __set__, and __delete__ methods of the descriptor protocol. A
non-data descriptor only implements __get__. Descriptors are
fundamental to the way attribute lookup works in Python. See the
descriptor howto guide:

https://docs.python.org/3/howto/descriptor.html

The above document paints a picture of attribute access that's not
really accurate at times. Also, the example code that attempts to
demonstrate type.__getattribute__ in terms of object.__getattribute__
is wrong. (It fails to search the __mro__ of the class, only the
metaclass. Also, what it's trying to do is fundamentally impossible.
You can't know whether object.__getattribute___ already evaluated a
descriptor that it found in the metaclass.)

Here's an overview of how object.__getattribute__ works:

* If a data descriptor is found in the class, call its __get__
  method, passing it the instance and the class, and return the
  result.
* Else if the instance has a dict and the name is found in it,
  return the value from the instance dict.
* Else if a non-data descriptor is found in the class, call its
  __get__ method, passing it the instance and the class, and
  return the result.
* Else if a non-descriptor is found in the class, return it.
* Else raise AttributeError.

A class lookup searches the dicts of each class in the __mro__,
stopping on the first hit. Also, a type can define a __getattr__
fallback method, in which case the AttributeError raised by
__getattribute__ is ignored.

For type.__getattribute__, the class object in this case is handled as
an instance of the metaclass. The main difference compared to
object.__getattribute__ is in the second step, since the instance in
this case is a class. Instead of searching just the class dict, it
searches the dicts of each class in the __mro__. Also, if it finds a
descriptor in the class __mro__, it calls its __get__ method, passing
None for the instance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] @property for old style classes vs new style classes

2016-09-15 Thread eryk sun
On Thu, Sep 15, 2016 at 4:40 AM, monik...@netzero.net
 wrote:
> class GetSet():
>
> def __init__(self, value):
> self.attrval = value
>
> @property
> def var(self):
> print "getting the var attribute"
> return self.attrval
> @var.setter
> def var(self,value):
> print "setting the var attribute"
> self.attrval = value
>
> @var.deleter
> def var(self):
> print "deleting the var attribute"
> self.attrval = None
>
> me = GetSet(5)
> me.var = 1000

A classic instance has both a type (`instance`) and a __class__ (e.g.
GetSet). The type defines how getting, setting, and deleting
attributes works, and the hard-coded classic behavior for calling the
class __getattr__, __setattr__, and __delattr__ methods.

If the class doesn't define __setattr__ and __delattr__, the instance
type sets and deletes attributes directly in the instance dict. Unlike
new-style classes, a data descriptor defined by the class gets ignored
here.

Getting attributes also prefers the instance dict. However, to support
bound methods (e.g. __init__), it falls back on a class lookup and
calls the descriptor __get__ method if defined. Unintentionally, it
happens that this partially supports the property descriptor. But
since there's no data descriptor support, it's only useful for
read-only properties.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __init__

2016-08-30 Thread eryk sun
On Tue, Aug 30, 2016 at 9:09 AM, Alan Gauld via Tutor  wrote:
> new() that sets up the memory then calls init(). So init is
> only used to initialise the object after it has been
> constructed.

__new__ and __init__ are called by the metaclass __call__ method.
__init_ is called if __new__ returns an instance of the class.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where is win32print in Windows 10 Pro

2016-08-12 Thread eryk sun
On Thu, Aug 11, 2016 at 2:44 PM, Joaquin Alzola
 wrote:
>
>>import win32print
>>ImportError: No module named win32print
>
> That module doesn't exist on your python path
>
> 'pywin32' is its canonical name.
>
> http://sourceforge.net/projects/pywin32/

I'm not certain what's meant in the above, but to be clear, PyWin32
installs most of its modules as top-level imports. So `import
win32print` is correct.

PyWin32 can be pip installed using the "pypiwin32" package:

https://pypi.python.org/pypi/pypiwin32

The difference compared to the executable installer is that the wheel
package doesn't automatically run the post-install script that copies
some DLLs to the System32 directory. Running the post-install script
isn't necessary if you just need to call Windows and COM APIs.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] command to determine of python 32 or 64 bit?

2016-08-10 Thread eryk sun
On Wed, Aug 10, 2016 at 4:27 AM, Garry Willgoose
 wrote:
> I have a number of binary libraries that are dependent on whether the 
> precompiled python
> distribution (eg. Enthought, ActiveState, etc)  in which they are installed 
> are compiled with
> 32 or 64 bit. Is there any reliable way to determine at run time whether a 
> python distribution
> is 32 or 64 bit? If I can determine at runtime then I can set the path for 
> loading of my libraries
> to something different depending on whether the python is 32 or 64 bit.

Use platform.architecture()[0]. Currently the value will be either
"32bit" or "64bit".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2016-07-25 Thread eryk sun
On Tue, Jul 26, 2016 at 4:39 AM, DiliupG  wrote:
> I am reading in a list of file names with the following code.
>
> def get_filelist(self):
> ""
> app = QtGui.QApplication(sys.argv)
> a = QtGui.QFileDialog.getOpenFileNames()
>
> filelist = []
> if a:
> for name in a:
> a = unicode(name)
>
> filelist.append(a)
>
> return filelist
>
> mainprg.filelist = mainprg.get_filelist()
>
> filelist = mainprg.filelist
>
> for name in filelist:
> print name <  THIS IS WHERE THE PROBLEM IS

This is an output problem, which is unrelated to the IDLE input error
that you initially reported. But IDLE isn't (at least shouldn't be)
used for deployed applications. It's a development environment.

Generally if you're using Qt then you're not creating a console
application that prints to stdout, but instead the program outputs
text to a Qt widget such as a QTextEdit. That said, if you need to print
Unicode text to the Windows console, for whatever reason (maybe
debugging), then pip install and enable the win_unicode_console
module.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2016-07-25 Thread eryk sun
On Mon, Jul 25, 2016 at 10:28 AM, Steven D'Aprano  wrote:
> I know that Linux and Mac OS X both use UTF-8 for filenames, and so support 
> all
> of Unicode. But Windows, I'm not sure. It might be localised to only use 
> Latin-1
> (Western European) or similar.

Windows filesystems (e.g. NTFS, ReFS, UDF, exFAT, FAT32) use Unicode
[1], i.e. UTF-16, as does the Windows wide-character API. Using 16-bit
wchar_t strings is a problem for C standard functions such as fopen,
which require 8-bit null-terminated strings, so the Windows C runtime
also supports wide-character alternatives such as _wfopen.

Python's os and io functions use Windows wide-character APIs for
unicode arguments, even in 2.x. Unfortunately some 2.x modules such as
subprocess use only the 8-bit API (e.g. 2.x Popen calls CreateProcessA
instead of CreateProcessW).

[1]: https://msdn.microsoft.com/en-us/library/ee681827#limits
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2016-07-25 Thread eryk sun
On Fri, Jul 22, 2016 at 7:38 AM, DiliupG  wrote:
> I am using Python 2.7.12 on Windows 10
>
> filename = u"මේක තියෙන්නේ සිංහලෙන්.txt"
> Unsupported characters in input

That error message is from IDLE. I'm not an expert with IDLE, so I
don't know what the following hack potentially breaks, but it does
allow entering the above Unicode filename in the interactive
interpreter.

Edit "Python27\Lib\idlelib\IOBinding.py". Look for the following
section on line 34:

if sys.platform == 'win32':
# On Windows, we could use "mbcs". However, to give the user
# a portable encoding name, we need to find the code page
try:
encoding = locale.getdefaultlocale()[1]
codecs.lookup(encoding)
except LookupError:
pass

Replace the encoding value with "utf-8" as follows:

# encoding = locale.getdefaultlocale()[1]
encoding = "utf-8"

When you restart IDLE, you should see that sys.stdin.encoding is now "utf-8".

IOBinding.encoding is used by ModifiedInterpreter.runsource in
PyShell.py. When the encoding is UTF-8, it passes the Unicode source
string directly to InteractiveInterpreter.runsource, where it gets
compiled using the built-in compile() function.

Note that IDLE uses the Tk GUI toolkit, which -- at least with Python
Tkinter on Windows -- is limited to the first 65,536 Unicode
characters, i.e the Basic Multilingual Plane. The BMP includes
Sinhalese, so your filename string is fine.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quote and double quotes opens up File Explorer in Windows 10

2016-07-09 Thread eryk sun
On Sat, Jul 9, 2016 at 2:22 PM, Ken G.  wrote:
> Hi: In gradually moving over my Python programs (2.7.6) to Windows 10 and
> using Geany 1.27 to modify or write up a program there, it is noted that
> whenever I typed in a quote (') or double quote ("), Windows 10 File
> Explorer opens up. I end up closing it by typing ALT-F4 to resume typing as
> before. Is there a way to prevent the File Explorer from opening up whenever
> I use a quote or double quote? Is there another key(s) that does weird thing
> in Windows 10? Thanks.

The single/double quote key isn't a standard hotkey in any version of
Windows. That would be insane. If this is just happening in Geany,
check its keybindings preferences. Otherwise, check that you're using
the right keyboard layout and that you don't have any programs (or
malware) that's registering a global hotkey.

For example, here's a simple script that registers the single quote
(US keyboard layout) as a global hotkey to open the desktop in
Explorer:

#! /usr/bin/python3
import os
import ctypes
from ctypes import wintypes

user32 = ctypes.WinDLL('user32')

WM_HOTKEY = 0x0312
MOD_NOREPEAT = 0x4000
VK_OEM_7 = 0xDE # US layout, single/double quote

hotkey_id = 1
if user32.RegisterHotKey(None, hotkey_id, MOD_NOREPEAT, VK_OEM_7):
print('Hotkey "\'" registered.')
msg = wintypes.MSG()
count = 0
while (count < 3 and
   user32.GetMessageW(ctypes.byref(msg), None, 0, 0)):
if msg.message == WM_HOTKEY and msg.wParam == hotkey_id:
count += 1
print('Hotkey received.')
os.startfile('shell:Desktop')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterators

2016-07-04 Thread eryk sun
On Mon, Jul 4, 2016 at 9:56 PM, Danny Yoo  wrote:
> So the extra trailing comma in a 1-tuple parenthesized expression is
> just there to make it different looking, to disambiguate it from the
> use of parentheses for expression grouping.

The comma is the distinguishing element of non-empty tuple literals.
It's always required in order to create a non-empty tuple, with or
without brackets. For example:

>>> 27,
(27,)

The parentheses are optional for a non-empty tuple, but they're often
required because a comma has low precedence. For example, with the
expression `x in y, z`, Python first evaluates `x in y`. If the goal
is to check whether x is in the the tuple `y, z`, then it has to be
written `x in (y, z)`.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why are expressions not allowed as parameters in function definition statements?

2016-06-18 Thread eryk sun
On Sun, Jun 19, 2016 at 4:04 AM, Danny Yoo  wrote:
>
>> def f((x,y), z):
> ...  print x, y, z
> ...
>>   f([1, 2], 3)
> 1 2 3

Note that Python 3 doesn't support tuple parameter unpacking. See PEP 3113:

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


Re: [Tutor] capture output from a subprocess while it is being produced

2016-06-14 Thread eryk sun
On Tue, Jun 14, 2016 at 8:03 PM, Albert-Jan Roskam
 wrote:
>
> proc = Popen("ls -lF", cwd=cwd, shell=True, stdout=PIPE, stderr=PIPE)

Don't use shell=True if you can avoid it. "ls" isn't a shell command.
Use ['ls', '-lF'].

The child process probably buffers its output when stdout isn't a
terminal. A typical standard I/O buffer size is 4 KiB. Reading from
the pipe will block until the child flushes the buffer to the pipe. It
might fflush() at checkpoints, but otherwise this happens
automatically when the buffer is full or when the child exits
normally.

On Linux you can use stdbuf to try forcing a program's stdout to use
either no buffering (-o0) or line buffering (-oL) -- e.g. `stdbuf -o0
ls -lF`. That said, "ls" should use full buffering in a pipe. You need
to test using a long-running process with intermittent output. You
could use a Python script that sleeps for a random interval between
writes to stdout and stderr.

On Windows there's no program like stdbuf to control standard I/O
buffering. If the target is a Python script, you can force interactive
mode using the "-i" option or disable buffering using "-u" (but -u
doesn't work for the 2.x REPL on Windows, since it switches to binary
mode).

> #output, errors = proc.communicate() # "Wait for process to 
> terminate."
> output = proc.stdout.read()  # docs: "Use communicate() rather 
> than .stdin.write, .stdout.read or .stderr.read"
> errors = proc.stderr.read()

Start a dedicated thread to read from each file. Otherwise you can end
up with a deadlock. For example, the child could block while writing
to a full stderr pipe, while the parent blocks while reading from an
empty stdout pipe. Also, you should read by lines using readline(), or
a byte at a time using read(1).

> if errors:
> raise RuntimeError(str(errors))

Data on stderr doesn't necessarily indicate an error. It could simply
be logging, warnings, or debug output. An error is indicated by a
non-zero exit code. Use proc.wait().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Howto display progress as cmd is being executed via subprocess ?

2016-06-14 Thread eryk sun
On Mon, Jun 13, 2016 at 4:50 PM, Ramanathan Muthaiah
 wrote:
>
> subprocess.check_output([cmd], stderr=subprocess.STDOUT, shell=True)
> ...
> how to combine the progressbar and subprocess code snippets to show the
> progress as the cmd is being executed.

check_output waits for the process to exit, so you can't use it with a
progress bar. However, if the program writes its progress to stdout,
then you can use `p = subprocess.Popen(args, stdout=PIPE)`, and create
a thread to read p.stdout, parse the output, and update the progress
bar.

Many programs detect when a standard stream isn't an interactive TTY
and switch to full buffering. You need to avoid this if you're
tracking progress or otherwise need to interact with a child process.
If you're on a Unix system you can use the stdbuf utility program to
try to force the child to disable buffering or use line buffering. If
that doesn't work, or you're on Windows, maybe the program has a
command-line option or environment variable setting that forces
interactive mode, such as Python's `-i` option. If all else fails, try
pexpect or winpexpect instead of subprocess. This works by making a
program think it's connected to a terminal/console.

FYI, with shell=True, you should use a command line instead of an args list.

The POSIX implementation does the following:

if isinstance(args, (str, bytes)):
args = [args]
else:
args = list(args)

if shell:
args = ["/bin/sh", "-c"] + args

Using just [cmd] doesn't cause a problem, but [cmd, arg1, arg2] is
probably a mistake. This passes the arguments to the shell, which sets
them as $N parameters. For example:

>>> subprocess.call(['echo $0 $1', 'arg1', 'arg2'], shell=True)
arg1 arg2

The Windows implementation does the following:

if not isinstance(args, str):
args = list2cmdline(args)

if shell:
startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = _winapi.SW_HIDE
comspec = os.environ.get("COMSPEC", "cmd.exe")
args = '{} /c "{}"'.format (comspec, args)

Thus on Windows even [cmd] is potentially wrong with shell=True.
list2cmdline doesn't know how to escape special characters for
cmd.exe.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] int(1.99...99) = 1 and can = 2

2016-05-01 Thread eryk sun
On Sun, May 1, 2016 at 1:02 AM, boB Stepp  wrote:
>
> py3: 1.
> 2.0
> py3: 1.999
> 1.999
...
> It has been many years since I did problems in converting decimal to
> binary representation (Shades of two's-complement!), but I am under
> the (apparently mistaken!) impression that in these 0.999...999
> situations that the floating point representation should not go "up"
> in value to the next integer representation.

https://en.wikipedia.org/wiki/Double-precision_floating-point_format

A binary64 float has 52 signifcand bits, with an implicit integer
value of 1, so it's effectively 53 bits. That leaves 11 bits for the
exponent, and 1 bit for the sign.

The 11-bit exponent value is biased by 1023, i.e. 2**0 is stored as
1023. The minimum binary exponent is (1-1023) == -1022, and the
maximum binary exponent is (2046-1023) == 1023. A biased exponent of 0
signifies either signed 0 (mantissa is zero) or a subnormal number
(mantissa is nonzero). A biased exponent of 2047 signifies either
signed infinity (mantissa is zero) or a non-number, i.e. NaN
(mantissia is nonzero).

The largest finite value has all 53 bits set:

>>> sys.float_info.max
1.7976931348623157e+308

>>> sum(Decimal(2**-n) for n in range(53)) * 2**1023
Decimal('1.797693134862315708145274237E+308')

The smallest finite value has the 52 fractional bits unset:

>>> sys.float_info.min
2.2250738585072014e-308

>>> Decimal(2)**-1022
Decimal('2.225073858507201383090232717E-308')

The machine epsilon value is 2**-52:

>>> sys.float_info.epsilon
2.220446049250313e-16

>>> Decimal(2)**-52
Decimal('2.220446049250313080847263336E-16')

Your number is just shy of 2, i.e. implicit 1 plus a 52-bit fractional
value and a binary exponent of 0.

>>> sum(Decimal(2**-n) for n in range(53))
Decimal('1.999777955395075')

The next increment by epsilon jumps to 2.0. The 52-bit mantissa rolls
over to all 0s, and the exponent increments by 1, i.e. (1 + 0.0) *
2**1. Python's float type has a hex() method to let you inspect this:

>>> (1.9998).hex()
'0x1.fp+0'
>>> (2.0).hex()
'0x1.0p+1'

where the integer part is 0x1; the 52-bit mantissa is 13 hexadecimal
digits; and the binary exponent comes after 'p'. You can also parse a
float hex string using float.fromhex():

>>> float.fromhex('0x1.0p-1022')
2.2250738585072014e-308

>>> float.fromhex('0x1.fp+1023')
1.7976931348623157e+308
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Newbie trying to get pip run on windows 7

2016-04-20 Thread eryk sun
On Tue, Apr 19, 2016 at 11:26 PM, Gustavo Davis via Tutor
 wrote:
>  But for some reason after I made the changes and saved them they wont
>  run. I mean once I go to the file and right click on them and click run
>  the cmd prompt pops up for a moment and then it just closes down and the
>  pip module never runs in python.

When you run a .py script from Explorer, Windows creates a new console
for the script's standard input and output, but this console closes as
soon as the script exits (i.e. when the python.exe process exits).
Instead you can run the script from an existing cmd shell to inherit
the shell's console.

> ;C:\Python34\Scripts\pip

There's no "Scripts\pip" directory.

Only add fully qualified directories to PATH, separated by a
semicolon, without quotes, and with no spaces between entries. You can
reference another environment variable in PATH, but only if the
variable doesn't reference other environment variables.

Add ;.PY to PATHEXT to allow running "command" instead of "command.py".

> >>> import pyperclip
> Traceback (most recent call last):
>   File "", line 1, in 
> import pyperclip
> ImportError: No module named 'pyperclip'

With PATH set up correctly, you can install this module from a cmd
shell using "pip install pyperclip".

pip searches for packages on pypi.python.org. This should work fine
for pure-Python packages and those with a WHL or EGG that's built for
your version of Python. Some source-only packages require building
extension modules. If your system isn't configured to build
extensions, look for an unofficial WHL on Christoph Gohlke's site:

http://www.lfd.uci.edu/~gohlke/pythonlibs
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


  1   2   >