Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread Roel Schroeven

Op 15/08/2021 om 7:01 schreef Chris Angelico:

On Sun, Aug 15, 2021 at 1:02 PM John O'Hagan  wrote:
>
> > On 2021-08-13 17:17, Chris Angelico wrote:
> > > Is it really? In my experience, no human ear can distinguish 277Hz
> > > from 277.1826Hz when it's played on a one-bit PC speaker, which the
> > > Beep function will be using.
>
> Rounding to integer frequencies will produce disastrously out-of-tune
> notes in a musical context! Particularly for low notes, where a whole
> semitone is only a couple of Hz difference. Even for higher notes, when
> they're played together any inaccuracies are much more apparent.

But before you advocate that too hard, check to see the *real*
capabilities of a one-bit PC speaker.
We're not necessarily talking about the PC speaker here: (almost) all 
computers these days have sound cards (mostly integrated on the 
motherboard) that are much more capable than those one-bit PC speakers. 
Beep uses that sound card, when available. I don't know how accurate the 
generated sound is though.


--
"Now, the invention of the scientific method and science is, I'm sure we'll all
agree, the most powerful intellectual idea, the most powerful framework for
thinking and investigating and understanding and challenging the world around
us that there is, and that it rests on the premise that any idea is there to be
attacked and if it withstands the attack then it lives to fight another day and
if it doesn't withstand the attack then down it goes. Religion doesn't seem to
work like that; it has certain ideas at the heart of it which we call sacred or
holy or whatever. That's an idea we're so familiar with, whether we subscribe
to it or not, that it's kind of odd to think what it actually means, because
really what it means is 'Here is an idea or a notion that you're not allowed to
say anything bad about; you're just not. Why not? - because you're not!"
-- Douglas Adams

--
https://mail.python.org/mailman/listinfo/python-list


Cyclic imports

2021-08-16 Thread Dan Stromberg
Hi folks.

I'm working on a large codebase that has at least one cyclic import.

In case I end up needing to eliminate the cyclic imports, is there any sort
of tool that will generate an import graph and output Just the cycles?

I tried pyreverse, but it produced too big a graph to be very useful; it
showed all internal imports, not just the cycles.

Thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on slices, negative indices, which are the equivalent procedures?

2021-08-16 Thread Jack Brandom
Dennis Lee Bieber  writes:

> On Wed, 11 Aug 2021 11:59:11 -0300, Jack Brandom 
> declaimed the following:
>
>>
>>Where are these production rules coming from?  They're not at 
>>
>>  https://docs.python.org/3/reference/grammar.html
>>
>>The word ``stride'' doesn't appear in this grammar.
>
>   Possibly from older versions of the grammar, before simplification to
> just . Originally, the "stride" term was added in response to
> requests from the numerical library developers (NumPy seems to be the
> survivor of that cluster). At the time, it wasn't envisioned to be usable
> with regular Python objects.
>
> https://en.wikipedia.org/wiki/Array_slicing#1991:_Python
> """
> The stride syntax (nums[1:5:2]) was introduced in the second half of the
> 1990s, as a result of requests put forward by scientific users in the
> Python "matrix-SIG" (special interest group).
> """

Always great to know the history with good references.  Thanks very much.

>   As for the grammar... Would you prefer that from 2.7?
>
> trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
> subscriptlist: subscript (',' subscript)* [',']
> subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
> sliceop: ':' [test]
>
> test: or_test ['if' or_test 'else' test] | lambdef
> or_test: and_test ('or' and_test)*
> and_test: not_test ('and' not_test)*
> not_test: 'not' not_test | comparison

This is looking less readable, so, no, I prefer that previous, which was
much clearer about slices.  I can't even quite make the slices out in
these rules.  (But thanks for the opportunity to try.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Hope Rouselle
Dennis Lee Bieber  writes:

> On Thu, 12 Aug 2021 06:15:28 +1000, Chris Angelico 
> declaimed the following:
>
>>The default command interpreter and shell on OS/2 was fairly primitive
>>by today's standards, and was highly compatible with the MS-DOS one,
>>but it also had the ability to run REXX scripts. REXX was *way* ahead
>>of its time. It's a shell language but remarkably well suited to
>>building GUIs and other tools (seriously, can you imagine designing a
>>GUI entirely in a bash script??). It had features that we'd consider
>>fairly normal or even primitive by Python's standards, but back then,
>>Python was extremely new and didn't really have very much mindshare.
>>REXX offered arbitrary-precision arithmetic, good databasing support,
>>a solid C API that made it easy to extend, integrations with a variety
>>of other systems... this was good stuff for its day. (REXX is still
>>around, but these days, I'd rather use Python.)
>>
>   I was spoiled by the Amiga variant of REXX. Most current
> implementations (well, Regina is the only one I've looked at) can just pass
> command to the default shell. The Amiga version took advantage of Intuition
> Message Ports (OS supported IPC). That allowed it to "address
> " any application that defined an ARexx port, allowing ARexx
> to be used as a scripting language for that application (and with multiple
> applications, one could easily fetch data from app1 and feed it to app2).
> ARexx did not, to my memory, implement arbitrary precision math.
>
>   Any statement in a REXX script that was not parsable as REXX would be
> passed on the currently "addressed" command host.
>
>   Granted, the fact that the Amiga used a shared common address space for
> all running applications made IPC quite easy -- one looked up the
> application message port, then added a small packet to the linked list
> associated with the port. That small packet basically held the address of
> the message port for returning data, and the address of the data being
> passed. The closet thing I've seen to that capability on systems with
> process-isolated virtual memory is (Open)VMS "mailbox" structures. The
> difference being that the entire data had to be written (QIO) to the
> mailbox, and the receiver had to read (another QIO call) the message --
> this allowed the address space to change.
>
>   I've not seen anything equivalent in my light perusal of the Win32 API
> (the various guide books aren't layed out in any way to be a reference),
> and Linux seems to use UNIX sockets for IPC... No way to search for a
> connection point by name...

I don't know anything about Amiga, REXX et cetera, so I might be totall
off here. But since you spoke of your perusal of the Win32 API, let me
add a tiny bit.  I gave myself a quick tour through the Win32 API using
Pavel Yosifovich's book ``Windows 10 System Programming''.  It's a two
volume work.  The thing that impressed me the most was the many ways to
do the IPC.  The purpose the work is clearly to show what is available
and it it probably does the job well.  (I ignored Windows for most of my
life and now I decided to take a look at it.  I don't feel it has much
of the elegance of UNIX.  It's what it is.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Hope Rouselle
Chris Angelico  writes:

> On Thu, Aug 12, 2021 at 9:23 AM Dennis Lee Bieber  
> wrote:

[...]

>> I was spoiled by the Amiga variant of REXX. Most current
>> implementations (well, Regina is the only one I've looked at) can just pass
>> command to the default shell. The Amiga version took advantage of Intuition
>> Message Ports (OS supported IPC). That allowed it to "address
>> " any application that defined an ARexx port, allowing ARexx
>> to be used as a scripting language for that application (and with multiple
>> applications, one could easily fetch data from app1 and feed it to app2).
>> ARexx did not, to my memory, implement arbitrary precision math.
>
> The same functionality was available in OS/2, but not heavily used.
> You could 'address cmd commandname' to force something to be
> interpreted as a shell command, but that was about it. However, I
> built a MUD that used REXX as its scripting language, and the default
> destination was sending text back to the person who sent the command;
> and you could, of course, still 'address cmd' to run a shell command.
>
>> I've not seen anything equivalent in my light perusal of the Win32 
>> API
>> (the various guide books aren't layed out in any way to be a reference),
>> and Linux seems to use UNIX sockets for IPC... No way to search for a
>> connection point by name...
>
> Win32 doesn't really have it. Unix sockets are kinda there but you
> identify something by a path to the socket, not the name of the
> application. But I think dbus is probably the closest to what you're
> thinking of.

Win32 offers some APIs that sound like it could be what you guys are
talking about here that I'm not too clear what it is.  There are some
shared memory you can reference by name, so it's as easy to use: both
applications invoke a procedure with a string-argument and exchange
information that way.  (And Win32 does support named pipes as well.)  I
mean I'm sure you know it all better than I do.  (Perhaps I'm too
clueless here.)  I could look up the Win32 APIs I'm thinking of if that
would clarify things.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Hope Rouselle
Dennis Lee Bieber  writes:

> On Wed, 11 Aug 2021 09:27:38 -0300, Hope Rouselle 
> declaimed the following:
>>
>>I wouldn't.  This is all Python-stuff.  The course chooses a language
>>like Python, but it is not trying to teach Python --- it is trying to
>>teach computer programming, that is, strategies in high-precision.
>
>   Then I would posit you are teaching the wrong level... What you
> describe is what would be found a "data structures" and/or "algorithms"
> course -- which is a second year, if not third year course in my (ancient)
> history. [...] Slicing with a stride isn't really useful to most
> algorithm development -- it's more of trick in normal Python.

I didn't express myself very well.  And it's hard to make sense of the
course.  I say it's interested in teaching computer programming because
it is not allowing the students to dive into the Python language.  It's
very restrictive.  Students will finish the course after learning to use
the loop contructs while and for.  The course calls them ``structures
for repetition''.  I think it's safe to say that the course is designed
by people concerned with education, but not well-versed in programming
languages, so not even the terminology seems quite correct.

So if they're not letting students embrace the whole of the language,
they must be concerned with other things?  They sloppily emphasize
documentation and put in some concerns for procedure signatures.  I have
some trouble defending it.  I see the good intentions, though, and the
job is not easy.

But this course is definitely not teaching any data structures or any
algorithms.  Zero.  Students are using tuples, lists and dictionaries,
strings and numbers, but they're not studying any strategies whatsoever.
The exercises make students use a few basic facts of mathematics, but
there is pretty much no investigation into the strategies behind some of
these facts.  It's not very intelligible.

I *kinda* like that it's restrictive because it allows us to show
students at least a few idioms: otherwise there's so much syntax to
digest that there is no time to show a few useful applications of them.
But then I read your next paragraph and it brings me to a very important
point, which I think it's at the back of your mind too.  Let's see.

>   So... The first thing I would drop is the RESTRICTION to only use what
> you've covered in prior sessions... Let the students read the language
> reference manual, peruse the library reference manual, and allow them to
> use whatever "advanced" techniques they are able to understand on their
> own. 

Precisely.  We kill the spirit of investigation by saying --- don't do
anything I don't allow you to.  This is pretty dangerous.  I have a much
better idea, I think.

You have to give people a tool and say --- use however you want.  If we
don't want them to use things not introduced in previous lessons, we
should give them a precisely-limited tool.  But that's not done.

The much better idea is to use such a limited language.  There are such
languages, made precisely for teaching.  It comes with very little
syntax and lesson after lesson it gets immensely powerful with very
little syntax.

> Granted you may have to restrict some features if [...]

To let students use the entire language feels a bit weird in the sense
that the group goes in so many different directions.  It definitely put
teachers in a position they have to be --- I don't know the answer.  It
is not my case.  But I do think that holding a class with wildly
different backgrounds, each going about in their own weird ways is kinda
odd.  It doesn't seem to bring people together --- on average.

The better-idea brings people together by leveling everyone out.
Unpopular languages give us this feature.  Students hardly ever master
them.  It's a game nobody played.  The rules are very simple.  The
implications are far-fetching.  Sometimes even the programmer-expert in
class realizes he is less skilled than the total-novice that never
handled a compiler: his tricks don't work in the new game.  (It's not
that we don't allow him to use them.  They're not there.  They don't
compile.)  (We take Ghess players, Backgammon and Checkers players [the
students], we teach them a new game, say, Go, and suddenly everyone is
learning together.  Assume Go is unpopular.  It's very simple.  Everyone
learns the rules quickly and we spend the semester looking into
strategies.  Much better idea.)

>   For my Algorithm/Data Structure course (circa 1978), the instructor
> allowed us to use any language /he/ could understand (so no SNOBOL). At the
> time I'd gone through intro/advanced FORTRAN-4, intro/advanced COBOL, Sigma
> Assembly, UCSD Pascal (not on the campus main computer, just a pair of
> LSI-11s), and BASIC. The assignment was a "Hashed Head, Multiply-Linked
> List". I chose to do that assignment using BASIC! In nearly 45 years, I've
> only seen ONE real-world implementation of the HHMLL -- The file system
> 

Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread jak

Il 13/08/2021 18:17, Chris Angelico ha scritto:

On Sat, Aug 14, 2021 at 2:11 AM Terry Reedy  wrote:


On 8/13/2021 6:53 AM, Umang Goswami wrote:

Hi There, Hope you find this mail in good health.

I am Umang Goswami, a Python developer and student working on a huge
project for automation of music instruments. I am producing the musical
notes using the Beep function of Winsound Module(
https://docs.python.org/3/library/winsound.html) by passing frequency as a
argument to the function.

Now whenever i provide frequency of any note in decimal(for example
277.1826 for C4 note) it shows following error:
Traceback (most recent call last):
File "C:\Users\Umang Goswami\Desktop\Umang  Goswami\test.py", line 2, in

  winsound.Beep(111.11,11)
TypeError: integer argument expected, got float

Now I have  to round up the frequencies. This is hurting the quality,
accuracy ,authenticity and future of the project. Almost all the notes have
the frequencies  in decimal parts. Rounding up means changing semitones and
quatertones thus whole note itself. This problem is technically making my
program useless.



Is it really? In my experience, no human ear can distinguish 277Hz
from 277.1826Hz when it's played on a one-bit PC speaker, which the
Beep function will be using.

ChrisA



Hi,
you could use a trick to get a better approximation:
277 * 4 = 1108
278 * 1 = 278
1108 + 278 = 1386
1386/5 = 277.2
now, respecting the duration of the tone, in accordance with its simple
rate, the buffer to be played will become:
277, 277, 277, 277, 278, 277,
277, 277, 277, 278, 277, 277,
... and so on.
--
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Hope Rouselle
Chris Angelico  writes:

> On Fri, Aug 13, 2021 at 2:15 AM Hope Rouselle  wrote:
>>
>> Chris Angelico  writes:
>>
>> > History lesson!
>> >
>> > Once upon a time, IBM and Microsoft looked at what Intel was
>> > producing, and went, hey, we need to design an operating system that
>> > can take advantage of the fancy features of this 80286 thing. So they
>> > collaborate on this plan to make a 16-bit protected mode OS.
>> > Unfortunately, things didn't work out too well, partly because this
>> > was when Microsoft was at its most monopolistic, and they ended up
>> > parting company. IBM continued to make OS/2, but Microsoft took their
>> > part of the code and made Windows NT out of it.
>>
>> How is it possible that Microsoft would take part of the code of OS/2?
>> Did IBM just hand it to them?
>
> I presume both companies had all of the code. It was a matter of
> licensing, though. There were a few components that were saddled with
> awkward restrictions due to the dual ownership (for instance, HPFS386
> was Microsoft-controlled, but vanilla HPFS was fine - kinda like the
> difference between LZW and LZ77).
>
>> > (Aside: Windows NT's 16-bit applications and OS/2's 16-bit
>> > applications were actually identical and compatible. Unfortunately,
>> > Win32 introduced a very new API, so as soon as everyone moved to
>> > 32-bit everything, the schism became problematic. But it was actually
>> > possible to package up a single .EXE file with a 16-bit MS-DOS loader,
>> > a Win32 loader, and an OS/2 32-bit loader, all happily coexisting.
>>
>> Beautiful. :-) So if all libraries were around in each system, they had
>> perfect compatibility?
>
> The 16-bit loaders were fine, but the 32-bit loaders were different,
> so this trick basically meant having three different copies of the
> code wrapped up in a single executable.
>
>> > Plus, it had this fancy
>> > concept of "extended attributes"; on older systems (like MS-DOS's
>> > "FAT" family), a file might be Read-Only, Hidden, a System file, or
>> > needing to be Archived, and that was it - but on HPFS, you could
>> > attach arbitrary data like "File type: DeScribe Word Processor" or
>> > "Double click action: Run CASMake.cmd". This allowed the GUI to store
>> > all kinds of information *on the file itself* instead of needing
>> > hidden files (or, in Windows' case, the registry) to track that kind
>> > of thing.
>>
>> Yeah, that's kinda nice.  Isn't that a UNIX design?  A file is a
>> sequence of bytes?  Users decide what to put in them?  So OS/2 was
>> taking advantage of that to integrate it well with the system.  Windows
>> was doing the same, but integrating the system with files in odd ways
>> --- such as a registry record to inform the system which programs open
>> which files?  (That does sound more messy.)
>
> Something like that, but with a lot more metadata. Modern OSes don't
> seem to work that way any more.
>
>> UNIX's execve() is able to read the first line of an executable and
>> invoke its interpreter.  I guess OS/2 was doing precisely that in a
>> different way?
>
> Kinda, but instead of having the choice of interpreter be inside the
> file contents itself, the choice was in the file's metadata. Still
> part of the file, but if you open and read the file, it isn't any
> different.

Speaking of which, NTFS supports something called file streams.  That's
essentially a way to have files inside files.  Streams are not shown by
tools such as file explorer.  Applications could definitely use that as
metadata.  (End of tangent.)

I think UNIX people would say and I'd agree --- we have that already.
But instead of hiding the stream, which is obscurantism, we let the user
see.  (Just create two files in a directory, one with the extension
.metadata.  And, oh, if you don't like that extension, you can change it
to whatever you like.  Lol.)  I like that.

>> > The default command interpreter and shell on OS/2 was fairly primitive
>> > by today's standards, and was highly compatible with the MS-DOS one,
>> > but it also had the ability to run REXX scripts. REXX was *way* ahead
>> > of its time. It's a shell language but remarkably well suited to
>> > building GUIs and other tools (seriously, can you imagine designing a
>> > GUI entirely in a bash script??).
>>
>> I cannot imagine.  I always wondered what REXX was about --- I saw
>> programs sometimes written in some website whose name is something like
>> Rosetta Code.  REXX looked so weird.  (``Who would program in that?'')
>> But I see now there is a context to it.
>
> Yeah. It was a strange choice by today's standards, but back then,
> most of my GUI programs were written in REXX.
>
> https://en.wikipedia.org/wiki/VX-REXX
> http://www.edm2.com/0206/vrexx.html
>
> (There were other tools too - VisPro REXX, VREXX, DrDialog, and
> various others - but VX-REXX was where most of my dev work happened.)

I like that kind of simpler GUI interface.  Cleaner.  Fast!  You know,
the so-called ``modern world'' is totally 

Re: some problems for an introductory python test

2021-08-16 Thread Hope Rouselle
Hope Rouselle  writes:

[...]

>> Granted you may have to restrict some features if [...]
>
> To let students use the entire language feels a bit weird in the sense
> that the group goes in so many different directions.  It definitely put
> teachers in a position they have to be --- I don't know the answer.
  ^^
Sorry.  I meant ``they [hate] to be [in] --- I don't know the answer.''

And, alas, I also find many more typos, grammar and cosmetic
incoherences below, which I fix with brackets.  (Not that they were
needed.  Only this bit above seemed to lose the meaning of the author,
Your Truly.)

> It is not my case.  But I do think that holding a class with wildly
> different backgrounds, each going about in their own weird ways[,] is
> kinda odd.  It doesn't seem to bring people together --- on average.
>
> The better-idea brings people together by leveling everyone out.
> Unpopular languages give us this feature.  Students hardly ever master
> them.  It's a game nobody played.  The rules are very simple.  The
> implications are far-fetching.  Sometimes even the programmer-expert in
> class realizes he is less skilled than the total-novice that never
> handled a compiler: his tricks don't work in the new game.  (It's not
> that we don't allow him to use them.  They're not there.  They don't
> compile.)  (We take [Chess] players, Backgammon and Checkers players [the
> students], we teach them a new game, say, Go, and suddenly everyone is
> learning together.  Assume Go is unpopular.  It's very simple.  Everyone
> learns the rules quickly and we spend the semester looking into
> strategies.  Much better idea.)
>
>>  For my Algorithm/Data Structure course (circa 1978), the instructor
>> allowed us to use any language /he/ could understand (so no SNOBOL). At the
>> time I'd gone through intro/advanced FORTRAN-4, intro/advanced COBOL, Sigma
>> Assembly, UCSD Pascal (not on the campus main computer, just a pair of
>> LSI-11s), and BASIC. The assignment was a "Hashed Head, Multiply-Linked
>> List". I chose to do that assignment using BASIC! In nearly 45 years, I've
>> only seen ONE real-world implementation of the HHMLL -- The file system
>> used by the Commodore Amiga. (Hash the first component of the path/name,
>> that maps to one of 64 entries in the root directory block; each entry
>> points the start of a linked list, follow the list until you reach the
>> block with the component name; if it is a directory block, hash the next
>> component and repeat; if it is a file block, the "entries" point to data
>> blocks instead of lists)
>
> Repeating my criticism with one more illustration.  When ``there's
> always more than one way to do it'', students can't even count on their
> classmates to help each other --- because each one is doing a different
> thing.  This is good in science, but what I like the most in schooling
> is working together and too much freedom like seems not very helpful in
> this direction.
>
> But both worlds [are] possible.  Use a limited tool (which is not
> computationally limited, quite the contrary) and tell them --- you can
> do anything you want with this.  [...] [Although] everyone is more or
> less original, the different solutions are never too far apart, so all
> the exchange is quite possible.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Hope Rouselle
Grant Edwards  writes:

> On 2021-08-12, Hope Rouselle  wrote:
>
>>> OS/2 had all kinds of amazing features (for its time). [...] Plus,
>>> it had this fancy concept of "extended attributes"; on older
>>> systems (like MS-DOS's "FAT" family), a file might be Read-Only,
>>> Hidden, a System file, or needing to be Archived, and that was it -
>>> but on HPFS, you could attach arbitrary data like "File type:
>>> DeScribe Word Processor" or "Double click action: Run
>>> CASMake.cmd". This allowed the GUI to store all kinds of
>>> information *on the file itself* instead of needing hidden files
>>> (or, in Windows' case, the registry) to track that kind of thing.
>>
>> Yeah, that's kinda nice.  Isn't that a UNIX design?  A file is a
>> sequence of bytes?  Users decide what to put in them?
>
> I think what he's talking about is allowing the user to attach
> arbitrary _metadata_ to the file -- metadata that exists separately
> and independently from the normal data that's just a "sequence of
> bytes". IOW, something similar to the "resource fork" that MacOS used
> to have. https://en.wikipedia.org/wiki/Resource_fork

Got ya.

>> So OS/2 was taking advantage of that to integrate it well with the
>> system.  Windows was doing the same, but integrating the system with
>> files in odd ways --- such as a registry record to inform the system
>> which programs open which files?  (That does sound more messy.)
>
> Windows never had filesystems that supported metadata like OS/2 and
> MacOS did. The registry was an ugly hack that attempted (very poorly)
> to make up for that lack of metadata.

Take a look at NTFS streams.  Maybe you would consider that as a way to
store metadata in Windows.  Pavel Yosifovich talks about them briefly in
chapter 11, page 555, part 1.  Windows 10 System Programming.  Here's
the first paragraph:

--8<---cut here---start->8---
The NTFS filesystem supports /file streams/, which are essentially files
within a file.  Normally, we use the default data stream, but others can
be created and used. These are essentially hidden from normal view and
don’t show up in standard tools such as Windows Explorer.
--8<---cut here---end--->8---

Pavel Yosifovich wrote a tool to let us see these streams:

https://github.com/zodiacon/NtfsStreams

>> UNIX's execve() is able to read the first line of an executable and
>> invoke its interpreter.  I guess OS/2 was doing precisely that in a
>> different way?
>
> No, that's not at all the same thing. The #! line is part of the
> normal file data. It's part of the 'sequence of bytes'. Metadata
> maintained by Unix filesystems comprises a very limited and
> pre-defined set of attributes present in the inode.

Well said.
-- 
https://mail.python.org/mailman/listinfo/python-list


Recoding Categorical to Numerical

2021-08-16 Thread John Griner
thanks


Virus-free.
www.avast.com

<#m_4567368322047287300_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-- 
https://mail.python.org/mailman/listinfo/python-list


on perhaps unloading modules?

2021-08-16 Thread Hope Rouselle
(*) Introduction

By the way, I'm aware that what I'm doing here is totally unsafe and I
could get my system destroyed.  I'm not planning on using this --- thank
you for your concern.  I'm just interested in understanding more about
modules.

(*) The problem

I got myself into a mess with loading modules at runtime.  The reason
I'm loading modules at runtime is because I'm writing a script to grade
students' work.  (Each student test is a file.py.)

Here's the phenomenon.  Notice how student m0 (who really scored a zero)
first gets his grade right, but if I invoke it again, then it gets 50.0.

>>> grade_student(m0)
{'grade': 0.0, ...}
>>> grade_student(m0)
{'grade': 50.0, ...}

(*) Where's the problem?

The variable m0 is a module and modules in Python are effectively
singletons --- that is, they are meant to be loaded only once, no matter
how many times you ask the system to.  That's my understanding.

(*) How did I get into this mess?

When m0 is graded first, that is the first student's module ever loaded,
so everything turns out correct.  This module m0 didn't do any work ---
didn't write any procedures, so it gets grade = 0 ---, so when I check
the first procedure, it doesn't exist --- zero on question 1.  However,
question 2 depends on question 1.  So I use the test's key (which I
wrote, which is perfectly correct) and I augment the student's module
with the key's procedures that are prerequisites to question 2 and then
I test question 2.  How do I do that?

  I do m.question1 = key.question1

where ``key.question1'' is a correct procedure for getting all points of
question1.  (That's kind to the student: I'm allowing them to get a zero
on question 1 while perhaps getting question 2 right.)  However, once I
augment the student's code, I can' t find a way to properly restore it
to the original --- so on a second execution, m0 gets many more points.

That's not the whole problem.  For reasons I don't understand, new
modules I load --- that is, different students --- get mixed with these
modifications in m0 that I made at some point in my code.

Of course, you want to see the code.  I need to work on producing a
small example.  Perhaps I will even answer my own question when I do.

For now, let me just ignite your imagination.  Feel free to ignore all
of this and wait for a nice example of the problem.

(*) The code

How do I load a student's file?

--8<---cut here---start->8---
from importlib import *
def get_student_module(fname): 
  # Here fname := p1_hello.py. But we need to remove the extension.
  # Let's call it basename then.
  mod_name = basename(fname)
  try:
student = import_module(mod_name)
  except Exception as e:
return False, str(e)
  return True, student
--8<---cut here---end--->8---

Now let d be a path to a directory.  How do use I this procedure?

--8<---cut here---start->8---
  for f in get_all_tests(d):
okay, student = get_student_module(f)
report = grade_student(student)
[...]
--8<---cut here---end--->8---

What does grade_student(student_module) do?  It passes student_module to
procedures to check every little thing the test requires.  Let's take a
look at question4().

Question 4 requires a previous procedure called procedure_x.  So I
overwrite the student's module with the key's procedure.

def question4(m):
  # We first provide students with all the prerequisites of the
  # question, so they don't necessarily get this one wrong by getting
  # prerequisites wrong.
  m.procedure_x = key.procedure_x
  
  # Now I make all my verifications about, say, m.procedure_y and I
  # compute an integer called losses, which I return.

  return losses

This strategy must be so unwise that it totally breaks my naïve dream of
automatic grading, showing how unfit I am for teaching students how to
program.  Such is life.

(*) If it were easy to unload modules...

I could just unload it and load it again.  That should restore the
student's module back to its original source code.
-- 
https://mail.python.org/mailman/listinfo/python-list


ANN: Dogelog Runtime, Prolog to the Moon (2021)

2021-08-16 Thread Mostowski Collapse

Yesterday we went into a little programming binge, despite there
was a free parade in Zurich. We could now already implement a transpiler
that targets Python. We simply took the transpiler main.p that targets

JavaScript and moded it into a new transpiler mainpy.p that targets
Python. The code is already on GitHub and we present it also here
as the Python code mainpy.p. We were also busy

on machine.py and special.py. The progress is now:

++   cross  +-+
| loader.p   |   compile| loader.py   | 100%
| compiler.p | ---> | compiler.py | 100%
++  +-+
| machine.py  |  66%
| special.py  |  33%
+-+

See also:

Python Version of Dogelog Runtime special
https://twitter.com/dogelogch/status/1426884473988292617

Python Version of Dogelog Runtime special
https://www.facebook.com/groups/dogelog
--
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-16 Thread Greg Ewing

On 16/08/21 1:50 am, Hope Rouselle wrote:

By the way, I'm aware that what I'm doing here is totally unsafe and I
could get my system destroyed.  I'm not planning on using this --- thank
you for your concern.  I'm just interested in understanding more about
modules.


Okay, I'll assume all the security issues have been taken are of, e.g.
by running all of this in a virtual machine...


Notice how student m0 (who really scored a zero)
first gets his grade right, but if I invoke it again, then it gets 50.0.


The best way to do this would be to run each student's file in
a separate process, so you know you're getting a completely fresh
start each time.

The second best way would be to not use import_module, but to
exec() the student's code. That way you don't create an entry in
sys.modules and don't have to worry about somehow unloading the
module.

Something like

code = read_student_file(student_name)
env = {} # A dict to hold the student's module-level definitions
exec(code, env)
grade_question1(env)
env['procedure_x'] = key.procedure_x
grade_question2(env)
...etc...


That's not the whole problem.  For reasons I don't understand, new
modules I load --- that is, different students --- get mixed with these
modifications in m0 that I made at some point in my code.


I would have to see a specific example of that. One thing to keep
in mind is that if key.procedure_x modifies any globals in the
key module, it will still modify globals in the key module -- not
the student's module -- after being transplanted there.

More generally, there are countless ways that a student's code
could modify something outside of its own module and affect the
behaviour of other student's code. This is why it would be
vastly preferable to run each test in a fresh process.


(*) If it were easy to unload modules...


It's sometimes possible to unload and reimport a module, but
only if the module's effects are completely self-contained.
That depends not only on what the module itself does, but
what other modules do with it. If any other module has imported
it, that module will still contain references to the old
module; if there are instances of a class defined in it still
existing, they will still be instances of the old version of
the class; etc.

99.999% of the time it's easier to just start again with a
fresh Python process.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-16 Thread Hope Rouselle
Hope Rouselle  writes:

[...]

> Of course, you want to see the code.  I need to work on producing a
> small example.  Perhaps I will even answer my own question when I do.

[...]

Here's a small-enough case.  We have two students here.  One is called
student.py and the other is called other.py.  They both get question 1
wrong, but they --- by definition --- get question 2 right.  Each
question is worth 10 points, so they both should get losses = 10.

(*) Student student.py

--8<---cut here---start->8---
def question1(t): # right answer is t[2]
  return t[1] # lack of attention, wrong answer
--8<---cut here---end--->8---

(*) Student other.py

--8<---cut here---start->8---
def question1(t): # right answer is t[2]
  return t[0] # also lack of attention, wrong answer
--8<---cut here---end--->8---

(*) Grading

All is good on first run.

Python 3.5.2 [...] on win32
[...]
>>> reproducible_problem()
student.py, total losses 10
other.py, total losses 10

The the problem:

>>> reproducible_problem()
student.py, total losses 0
other.py, total losses 0

They lose nothing because both modules are now permanently modified.

(*) The code of grading.py

--8<---cut here---start->8---
# -*- mode: python; python-indent-offset: 2 -*-
def key_question1(t): 
  # Pretty simple.  Student must just return index 2 of a tuple.
  return t[2]

def reproducible_problem(): # grade all students
  okay, m = get_student_module("student.py")
  r = grade_student(m)
  print("student.py, total losses", r) # should be 10
  okay, m = get_student_module("other.py")
  r = grade_student(m)
  print("other.py, total losses", r) # should be 10

def grade_student(m): # grades a single student
  losses  = question1_verifier(m)
  losses += question2_verifier(m)
  return losses

def question1_verifier(m):
  losses = 0
  if m.question1( (0, 1, 2, 3) ) != 2: # wrong answer
losses = 10
  return losses

def question2_verifier(m):
  m.question1 = key_question1
  # To grade question 2, we overwrite the student's module by giving
  # it the key_question1 procedure.  This way we are able to let the
  # student get question 2 even if s/he got question 1 incorrect.
  losses = 0
  return losses 

def get_student_module(fname): 
  from importlib import import_module
  mod_name = basename(fname)
  try:
student = import_module(mod_name)
  except Exception as e:
return False, str(e)
  return True, student

def basename(fname): # drop the the .py extension
  return "".join(fname.split(".")[ : -1])
--8<---cut here---end--->8---
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-16 Thread Hope Rouselle
Greg Ewing  writes:

> On 16/08/21 1:50 am, Hope Rouselle wrote:
>> By the way, I'm aware that what I'm doing here is totally unsafe and I
>> could get my system destroyed.  I'm not planning on using this --- thank
>> you for your concern.  I'm just interested in understanding more about
>> modules.
>
> Okay, I'll assume all the security issues have been taken are of, e.g.
> by running all of this in a virtual machine...

Oh, good idea. :-D

>> Notice how student m0 (who really scored a zero)
>> first gets his grade right, but if I invoke it again, then it gets 50.0.
>
> The best way to do this would be to run each student's file in
> a separate process, so you know you're getting a completely fresh
> start each time.

Yes, that would be much better indeed, but I'd be left with IPC
mechanisms of exchanging data.  My first idea is to just print out a
JSON of the final report to be read by the grader-process.  (That is
essentially what I'm already doing, except that I don't need to
serialize things in a string.)

> The second best way would be to not use import_module, but to
> exec() the student's code. That way you don't create an entry in
> sys.modules and don't have to worry about somehow unloading the
> module.
>
> Something like
>
> code = read_student_file(student_name)
> env = {} # A dict to hold the student's module-level definitions
> exec(code, env)
> grade_question1(env)
> env['procedure_x'] = key.procedure_x
> grade_question2(env)
> ...etc...

That seems a lot easier to implement.

>> That's not the whole problem.  For reasons I don't understand, new
>> modules I load --- that is, different students --- get mixed with these
>> modifications in m0 that I made at some point in my code.
>
> I would have to see a specific example of that. 

I'm sorry.  I don't think I was right in that observation.  I tried to
produce one such small program to post here and failed.  (Let's forget
about that.)

> One thing to keep in mind is that if key.procedure_x modifies any
> globals in the key module, it will still modify globals in the key
> module -- not the student's module -- after being transplanted there.
>
> More generally, there are countless ways that a student's code
> could modify something outside of its own module and affect the
> behaviour of other student's code. This is why it would be
> vastly preferable to run each test in a fresh process.

I'm totally convinced.  I'll do everything in a fresh process.  Having
guarantees makes the job much more pleasant to do.

>> (*) If it were easy to unload modules...
>
> It's sometimes possible to unload and reimport a module, but
> only if the module's effects are completely self-contained.
> That depends not only on what the module itself does, but
> what other modules do with it. If any other module has imported
> it, that module will still contain references to the old
> module; if there are instances of a class defined in it still
> existing, they will still be instances of the old version of
> the class; etc.
>
> 99.999% of the time it's easier to just start again with a
> fresh Python process.

I'm totally convinced.  Thank you so much!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-16 Thread Hope Rouselle
Hope Rouselle  writes:

> Hope Rouselle  writes:
>
> [...]
>
>> Of course, you want to see the code.  I need to work on producing a
>> small example.  Perhaps I will even answer my own question when I do.
>
> [...]
>
> Here's a small-enough case.  We have two students here.  One is called
> student.py and the other is called other.py.  They both get question 1
> wrong, but they --- by definition --- get question 2 right.  Each
> question is worth 10 points, so they both should get losses = 10.
>
> (*) Student student.py
>
> def question1(t): # right answer is t[2]
>   return t[1] # lack of attention, wrong answer
>
>
> (*) Student other.py
>
> def question1(t): # right answer is t[2]
>   return t[0] # also lack of attention, wrong answer
>
>
> (*) Grading
>
> All is good on first run.
>
> Python 3.5.2 [...] on win32
> [...]
 reproducible_problem()
> student.py, total losses 10
> other.py, total losses 10
>
> The the problem:
>
 reproducible_problem()
> student.py, total losses 0
> other.py, total losses 0
>
> They lose nothing because both modules are now permanently modified.
>
> (*) The code of grading.py
>
> # -*- mode: python; python-indent-offset: 2 -*-
> def key_question1(t): 
>   # Pretty simple.  Student must just return index 2 of a tuple.
>   return t[2]
>
> def reproducible_problem(): # grade all students
>   okay, m = get_student_module("student.py")
>   r = grade_student(m)
>   print("student.py, total losses", r) # should be 10
>   okay, m = get_student_module("other.py")
>   r = grade_student(m)
>   print("other.py, total losses", r) # should be 10
>
> def grade_student(m): # grades a single student
>   losses  = question1_verifier(m)
>   losses += question2_verifier(m)
>   return losses
>
> def question1_verifier(m):
>   losses = 0
>   if m.question1( (0, 1, 2, 3) ) != 2: # wrong answer
> losses = 10
>   return losses
>
> def question2_verifier(m):
>   m.question1 = key_question1
>   # To grade question 2, we overwrite the student's module by giving
>   # it the key_question1 procedure.  This way we are able to let the
>   # student get question 2 even if s/he got question 1 incorrect.
>   losses = 0
>   return losses 

My solution is to painfully save each original procedure from student in
a variable, replace them with the key's, then restore them at the end.
I couldn't overwrite m.__dict__ because it is readonly.  (Didn't find a
neater's way out.)

def question2_verifier(m):
  question1_original = m.question1
  m.question1 = key_question1
  # apply verifications... 
  m.question1 = question1_original
  return losses
-- 
https://mail.python.org/mailman/listinfo/python-list


post.py

2021-08-16 Thread Testing
Just got done writing this nice little posting script. Its my first python 
script ecer
so be nice lol.

#!/usr/bin/python3
import nntplib
import sys

NAME = 'testbunny'
EMAIL = 'te...@test.com'
SIG = 'Testbunny'
ORG = 'Python Pirates'
SERVER = 'nntp.aioe.org'
GROUPS = 'alt.test'

SUBJECT = input("What is the subject? ")

print("Enter/Paste your content. Ctrl-D or Ctrl-Z ( windows ) to save it.")
contents = []
while True:
try:
BODY = input()
contents.append(BODY)
except EOFError:
break

text = '\n'.join(contents)

open('article.txt', 'w').close()

with open('article.txt', 'a') as f:
f.write('From: ' + NAME + " <" + EMAIL + ">")
f.write("\n")
f.write('Subject: ' + SUBJECT)
f.write("\n")
f.write('Newsgroups: ' + GROUPS)
f.write("\n")
f.write('Organization: ' + ORG)
f.write("\n")
f.write('User-Agent: Python3')
f.write("\n")
f.write("\n")
f.writelines(text)
f.write("\n")
f.write("\n")
f.write('--')
f.write("\n")
f.write(SIG)
f.write("\n")
f.write('.')
print("Sending article")

s = nntplib.NNTP(SERVER)
f = open('article.txt', 'rb')
s.post(f)
print("done")
s.quit()

--
Python Test Script
.
-- 
https://mail.python.org/mailman/listinfo/python-list


fixed the from fiield

2021-08-16 Thread testing
heh

--
Python Test Script
.
-- 
https://mail.python.org/mailman/listinfo/python-list


please help

2021-08-16 Thread vitalis
   I keep getting this error while trying to install pyqt5 designer

   Fatal error in launcher: Unable to create process using '"c:\program
   files\python39\python.exe"  "C:\Program Files\Python39\Scripts\pip.exe"
   install PyQt5Designer': The system cannot find the file specified.

   please what do I do about this





   Sent from [1]Mail for Windows



References

   Visible links
   1. https://go.microsoft.com/fwlink/?LinkId=550986
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Chris Angelico
On Tue, Aug 17, 2021 at 3:51 AM Hope Rouselle  wrote:
>
> Chris Angelico  writes:
> >> Wow, I kinda feel the same as you here.  I think this justifies perhaps
> >> using a hardware solution.  (Crazy idea?! Lol.)
> >
> > uhhh Yes. Very crazy idea. Can't imagine why anyone would ever
> > think about doing that.
>
> Lol.  Really?  I mean a certain panic button.  You know the GNU Emacs.
> It has this queue with the implications you mentioned --- as much as it
> can.  (It must of course get the messages from the system, otherwise it
> can't do anything about it.)  And it has the panic button C-g.  The
> keyboard has one the highest precedences in hardware interrupts, doesn't
> it not?  A certain very important system could have a panic button that
> invokes a certain debugger, say, for a crisis-moment.
>
> But then this could be a lousy engineering strategy.  I am not an expert
> at all in any of this.  But I'm surprised with your quick dismissal. :-)
>
> > Certainly nobody in his right mind would have WatchCat listening on
> > the serial port's Ring Indicator interrupt, and then grab a paperclip
> > to bridge the DTR and RI pins on an otherwise-unoccupied serial port
> > on the back of the PC. (The DTR pin was kept high by the PC, and could
> > therefore be used as an open power pin to bring the RI high.)
>
> Why not?  Misuse of hardware?  Too precious of a resource?
>
> > If you're curious, it's pins 4 and 9 - diagonally up and in from the
> > short
> > corner. http://www.usconverters.com/index.php?main_page=page&id=61&chapter=0
>
> You know your pins!  That's impressive.  I thought the OS itself could
> use something like that.  The fact that they never do... Says something,
> doesn't it?  But it's not too obvious to me.
>
> > And of COURSE nobody would ever take an old serial mouse, take the
> > ball out of it, and turn it into a foot-controlled signal... although
> > that wasn't for WatchCat, that was for clipboard management between my
> > app and a Windows accounting package that we used. But that's a
> > separate story.
>
> Lol.  I feel you're saying you would. :-)

This was all a figure of speech, and the denials were all tongue in
cheek. Not only am I saying we would, but we *did*. All of the above.
The Ring Indicator trick was one of the best, since we had very little
other use for serial ports, and it didn't significantly impact the
system during good times, but was always reliable when things went
wrong.

(And when I posted it, I could visualize the port and knew which pins
to bridge, but had to go look up a pinout to be able to say their pin
numbers and descriptions.)

> I heard of Python for the first time in the 90s.  I worked at an ISP.
> Only one guy was really programming there, Allaire ColdFusion.  But, odd
> enough, we used to say we would ``write a script in Python'' when we
> meant to say we were going out for a smoke.  I think that was precisely
> because nobody knew that ``Python'' really was.  I never expected it to
> be a great language.  I imagined it was something like Tcl.  (Lol, no
> offense at all towards Tcl.)

Haha, that's a weird idiom!

Funny you should mention Tcl.

https://docs.python.org/3/library/tkinter.html

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on perhaps unloading modules?

2021-08-16 Thread Chris Angelico
On Tue, Aug 17, 2021 at 4:02 AM Greg Ewing  wrote:
> The second best way would be to not use import_module, but to
> exec() the student's code. That way you don't create an entry in
> sys.modules and don't have to worry about somehow unloading the
> module.

I would agree with this. If you need to mess around with modules and
you don't want them to be cached, avoid the normal "import" mechanism,
and just exec yourself a module's worth of code.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: please help

2021-08-16 Thread MRAB

On 2021-08-16 02:19, vitalis wrote:

I keep getting this error while trying to install pyqt5 designer

Fatal error in launcher: Unable to create process using '"c:\program
files\python39\python.exe"  "C:\Program Files\Python39\Scripts\pip.exe"
install PyQt5Designer': The system cannot find the file specified.

please what do I do about this


Try using the Python Launcher and the pip module:

py -3.9 -m install PyQt5Designer
--
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Abhiram R
Hello,
One of my absolute favorite places to go to practice Python questions from
is https://projecteuler.net . It's just Maths based questions that cannot
be solved by hand without consuming a Ton of time because of the high
limits.
This is how I learnt Python. By solving problems from the site in Python!
Happy to provide more tips wrt this if required.

Regards
Abhi R 

On Tue, Aug 10, 2021 at 2:56 AM Hope Rouselle 
wrote:

> I'm looking for questions to put on a test for students who never had
> any experience with programming, but have learned to use Python's
> procedures, default arguments, if-else, strings, tuples, lists and
> dictionaries.  (There's no OOP at all in this course.  Students don't
> even write ls.append(...).  They write list.append(ls, ...)).
>
> I'd like to put questions that they would have to write procedures that
> would would be real-world type of stuff, without error checking,
> exceptions and such.  So if you think of something more or less cool
> that uses loops, we can sometimes simplify it by assuming the input has
> a certain fixed size.
>
> I came up with the following question.  Using strings of length 5
> (always), write a procedure histogram(s) that consumes a string and
> produces a dictionary whose keys are each substrings (of the string) of
> length 1 and their corresponding values are the number of times each
> such substrings appear.  For example, histogram("a") = {"a": 5}.
> Students can "loop through" the string by writing out s[0], s[1], s[2],
> s[3], s[4].
>
> I'd like even better questions.  I'd like questions that would tell them
> to write procedures that would also have inverses, so that one could
> check the other of the other.  (A second question would ask for the
> inverse, but hopefully real world stuff.  One such question could be
> parsing a line separate by fields such as "root:0:0:mypass:Super User"
> and another that gives them ["root", 0, 0, ...] and asks them to write
> "root:0:0:mypass:..."  You get the idea.)
>
> Students know how to use str().  But they don't know how to use type(),
> so they can't really check for the type of the input.  I probably
> couldn't ask them to write a prototype of a tiny subset of pickle, say.
>
> I think you get the idea.  I hope you can provide me with creativity.  I
> have been looking at books, but every one I look at they introduce loops
> very quickly and off they go.  Thank you!
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
-Abhiram R
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on slices, negative indices, which are the equivalent procedures?

2021-08-16 Thread Dennis Lee Bieber
On Sun, 15 Aug 2021 00:05:29 -0300, Jack Brandom 
declaimed the following:

>Dennis Lee Bieber  writes:
>

>> subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
>> sliceop: ':' [test]

>
>This is looking less readable, so, no, I prefer that previous, which was
>much clearer about slices.  I can't even quite make the slices out in
>these rules.  (But thanks for the opportunity to try.)

The last part of "subscript" has the main "slice" notation (stripping
the quotes)

[test] : [test]

and the "stride" component is [sliceop] being

: [test]



-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: please help

2021-08-16 Thread Dennis Lee Bieber
On Mon, 16 Aug 2021 20:24:17 +0100, MRAB 
declaimed the following:

>On 2021-08-16 02:19, vitalis wrote:
>> I keep getting this error while trying to install pyqt5 designer
>> 
>> Fatal error in launcher: Unable to create process using '"c:\program
>> files\python39\python.exe"  "C:\Program Files\Python39\Scripts\pip.exe"
>> install PyQt5Designer': The system cannot find the file specified.
>> 
>> please what do I do about this
>> 
>Try using the Python Launcher and the pip module:
>
> py -3.9 -m install PyQt5Designer

py-3.9 -m pip install PyQt5Designer...

However I would want to see the exact command line (not whatever
reformatting took place in the error message). Literal reading of that
message implies the OP is trying to pass a pip EXECUTABLE as the program to
be run by Python, rather than a pip .py file.


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: some problems for an introductory python test

2021-08-16 Thread Dennis Lee Bieber
On Sun, 15 Aug 2021 00:15:58 -0300, Hope Rouselle 
declaimed the following:

Giganews seems to have just vomited up three days worth of traffic...

>Dennis Lee Bieber  writes:
>
>>
>>  Granted, the fact that the Amiga used a shared common address space for
>> all running applications made IPC quite easy -- one looked up the
>> application message port, then added a small packet to the linked list
>> associated with the port. That small packet basically held the address of
>> the message port for returning data, and the address of the data being
>> passed. The closet thing I've seen to that capability on systems with
>> process-isolated virtual memory is (Open)VMS "mailbox" structures. The
>> difference being that the entire data had to be written (QIO) to the
>> mailbox, and the receiver had to read (another QIO call) the message --
>> this allowed the address space to change.
>>
>>  I've not seen anything equivalent in my light perusal of the Win32 API
>> (the various guide books aren't layed out in any way to be a reference),
>> and Linux seems to use UNIX sockets for IPC... No way to search for a
>> connection point by name...
>
>I don't know anything about Amiga, REXX et cetera, so I might be totall
>off here. But since you spoke of your perusal of the Win32 API, let me
>add a tiny bit.  I gave myself a quick tour through the Win32 API using
>Pavel Yosifovich's book ``Windows 10 System Programming''.  It's a two
>volume work.  The thing that impressed me the most was the many ways to
>do the IPC.  The purpose the work is clearly to show what is available
>and it it probably does the job well.  (I ignored Windows for most of my
>life and now I decided to take a look at it.  I don't feel it has much
>of the elegance of UNIX.  It's what it is.)

For a book that was published only a year ago -- it seems to be hard to
find... Amazon has v1, but not v2.

I have the "Windows Internals 6th" both volumes.

The closet those get to IPC is something called ALPC -- and that is
declared to be internal only, not available to third party programmers.

"Programming Windows 6th" is focused on using C# (which, for the most
part, means using the "managed" API, not the Win32 API directly).

"Mailslots" (I'm using Google to find Windows IPC options) initially
sound close to Amiga message ports... Except there is no status return
inherent to the system; Amiga messages contain a return address so a
program can wait for the status of processing by the destination address.
They also don't qualify as VMS mailboxes as there can only be one reader
(the creator of the mailslot). VMS mailboxes allow multiple writers and/or
multiple readers.

"File mapping" doesn't provide any form of queuing -- multiple
"writers" would require manually implementing some protocol to track "next
available space" in the mapped file. It would allow bidirectional message
passing, but again that requires some rather nasty overhead information
(when is the return status valid -- it is presumed the client wanting the
return status would get it from the same memory space as the message it
sent, and that space can't be reused until the client explicitly frees it
-- again by some overhead protocol changing headers of free space.

"Data Copy" also sounds close to Amiga message ports (closer than
"Mailslots"), but the online documentation doesn't indicate if it queues
messages. The documentation doesn't illustrate HOW to return TRUE or FALSE
(processed/rejected). It advices that the receiver should treat the data as
read-only -- though I have to wonder if it /could/ write a return status
into the same message structure before "returning TRUE".

Named Pipes and Sockets both have the "tied to endpoints" problem, and
in the case of Pipes, may only allow one sender to connect at a time
(sockets are also one connection, but normally the receivers spawns a
thread for each active connection and the connections are made using
different allocated sockets).



-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread Eryk Sun
On 8/16/21, Roel Schroeven  wrote:
>
> We're not necessarily talking about the PC speaker here: (almost) all
> computers these days have sound cards (mostly integrated on the
> motherboard) that are much more capable than those one-bit PC speakers.

Yes, the PC speaker beep does not get used in Windows 7+. The beep
device object is retained for compatibility, but it redirects the
request to a task in the user's session (which could be a remote
desktop session) that generates a WAV buffer in memory and plays it
via PlaySound(). You can do this yourself if you need a more precise
frequency. For example:

import io
import math
import wave
import winsound

def beep(frequency, duration):
'''Play a beep at frequency (hertz) for duration (milliseconds).'''
fs = 48000
f = 2 * math.pi * frequency / fs
n = round(duration * fs / 1000)
b = io.BytesIO()
w = wave.Wave_write(b)
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(fs)
w.writeframes(b''.join(round(32767 * math.sin(f*i)).to_bytes(
2, 'little', signed=True) for i in range(n)))
w.close()
winsound.PlaySound(b.getbuffer(), winsound.SND_MEMORY)


Play a beep at 277.1826 Hz for one second:

beep(277.1826, 1000)

I'm tone deaf, I suppose, since I need a difference of about 3 cycles
to perceive it, let alone a fraction of a cycle.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread Chris Angelico
On Tue, Aug 17, 2021 at 11:44 AM Eryk Sun  wrote:
>
> On 8/16/21, Roel Schroeven  wrote:
> >
> > We're not necessarily talking about the PC speaker here: (almost) all
> > computers these days have sound cards (mostly integrated on the
> > motherboard) that are much more capable than those one-bit PC speakers.
>
> Yes, the PC speaker beep does not get used in Windows 7+. The beep
> device object is retained for compatibility, but it redirects the
> request to a task in the user's session (which could be a remote
> desktop session) that generates a WAV buffer in memory and plays it
> via PlaySound().

That seems a bizarre way to handle it. What happens if you have
multiple sound cards? Or if a program needs to get the user's
attention despite headphones being connected to the sound card? That's
what the PC speaker is for, and I would be quite surprised if Windows
just says "nahh that doesn't matter".

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread Eryk Sun
On 8/16/21, Chris Angelico  wrote:
> On Tue, Aug 17, 2021 at 11:44 AM Eryk Sun  wrote:
>
>> Yes, the PC speaker beep does not get used in Windows 7+. The beep
>> device object is retained for compatibility, but it redirects the
>> request to a task in the user's session (which could be a remote
>> desktop session) that generates a WAV buffer in memory and plays it
>> via PlaySound().
>
> That seems a bizarre way to handle it.

Check the documentation [1]:

In Windows 7, Beep was rewritten to pass the beep to the default sound
device for the session. This is normally the sound card, except when
run under Terminal Services, in which case the beep is rendered on the
client.

I didn't just take their word for it, however. I checked the code for
"\Device\Beep" in a kernel debugger. It gets the session ID to find
and complete the I/O Request Packet (IRP) that was queued up by the
per-session multimedia task (in Windows device drivers, this is called
an inverted call [2]). I attached a debugger to the taskhostw.exe
process that's hosting the multimedia task -- in particular the
PlaySoundSrv.dll module -- and followed what it did to play a beep.
The implementation is in a CBeepRedirector class, in particular its
WorkThread() method handles the completed IOCTL from the beep device
by calling the DoPlaySound() method, which calls
Generate16bitMonoTone() to create a sine wave in WAV format, and plays
it via PlaySound() with the SND_MEMORY flag.

> multiple sound cards?

PlaySound() and system sounds use the default audio output device. If
it's an RDP session, the audio is redirected to the client-side device
stack.

> attention despite headphones being connected to the sound card?

An application that wants the user's attention can also flash the
window caption and/or taskbar button via FlashWindowEx().

---

[1] 
https://docs.microsoft.com/en-us/windows/win32/api/utilapiset/nf-utilapiset-beep
[2] https://www.osr.com/nt-insider/2013-issue1/inverted-call-model-kmdf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency

2021-08-16 Thread Chris Angelico
On Tue, Aug 17, 2021 at 1:50 PM Eryk Sun  wrote:
>
> On 8/16/21, Chris Angelico  wrote:
> > On Tue, Aug 17, 2021 at 11:44 AM Eryk Sun  wrote:
> >
> >> Yes, the PC speaker beep does not get used in Windows 7+. The beep
> >> device object is retained for compatibility, but it redirects the
> >> request to a task in the user's session (which could be a remote
> >> desktop session) that generates a WAV buffer in memory and plays it
> >> via PlaySound().
> >
> > That seems a bizarre way to handle it.
>
> Check the documentation [1]:
>
> In Windows 7, Beep was rewritten to pass the beep to the default sound
> device for the session. This is normally the sound card, except when
> run under Terminal Services, in which case the beep is rendered on the
> client.
>

Huh. Okay. Then I withdraw the concern from this list, and instead lay
it at Microsoft's feet. That is, I maintain, a bizarre choice. Surely
there are better ways to trigger audio on the sound card?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list