Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-01 Thread Dan Sommers via Python-list
On 2024-10-01 at 23:03:01 +0200,
Left Right  wrote:

> > If I recognize the first digit, then I *can* hand that over to an
> > external function to accumulate the digits that follow.
> 
> And what is that external function going to do with this information?
> The point is you didn't parse anything if you just sent the digit.
> You just delegated the parsing further. Parsing is only meaningful if
> you extracted some information, but your idea is, essentially "what if
> I do nothing?".

If the parser detects the first digit of a number, then the parser can
read digits one at a time (i.e., "streaming"), assimilate and accumulate
the value of the number being parsed, and successfully finish parsing
the number it reads a non-digit.  Whether the function that accumulates
the value during the process is internal or external isn't relevant; the
point is that it is possible to parse integers from most significant
digit to least significant digit under a streaming model (and if you're
sufficiently clever, you can even write partial results to external
storage and/or another transmission protocol, thus allowing for numbers
bigger (as measured by JSON or your internal representation) than your
RAM).

At most, the parser has to remember the non-digit character it read so
that it (the parser) can begin to parse whatever comes after the number.
Does that break your notion of "streaming"?

Why do I have to start with the least significant digit?

> > Under that constraint, I'm not sure I can parse anything.  How can I
> > parse a string (and hand it over to an external function) until I've
> > found the closing quote?
> 
> Nobody says that parsing a number is the only pathological case.  You,
> however, exaggerate by saying you cannot parse _anything_. You can
> parse booleans or null, for example.  There's no problem there.

My intent was only to repeat what you implied:  that any parser that
reads its input until it has parsed a value is not streaming.

So how much information can the parser keep before you consider it not
to be "streaming"?

[...]

> In principle, any language that has infinite words will have the same
> problem with streaming [...]

So what magic allows anyone to stream any JSON file over SCSI or IP?
Let alone some kind of "live stream" that by definition is indefinite,
even if it only lasts a few tenths of a second?

> [...] If you ever pondered h/w or low-level
> protocols s.a. SCSI or IP [...]

I spent a good deal of my career designing and implementing all manner
of communicaations protocols, from transmitting and receiving single
bits over a wire all the way up to what are now known as session and
presentation layers.  Some imposed maximum lengths in certain places;
some allowed for indefinite amounts of data to be transferred from one
end to the other without stopping, resetting, or overflowing.  And yet
somehow, the universe never collapsed.

If you believe that some implementation of fsync fails to meet a
specification, or fails to work correctly on files containign JSON, then
file a bug report.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-01 Thread Dan Sommers via Python-list
On 2024-09-30 at 21:34:07 +0200,
Regarding "Re: Help with Streaming and Chunk Processing for Large JSON Data (60 
GB) from Kenna API,"
Left Right via Python-list  wrote:

> > What am I missing?  Handwavingly, start with the first digit, and as
> > long as the next character is a digit, multipliy the accumulated result
> > by 10 (or the appropriate base) and add the next value.  Oh, and handle
> > scientific notation as a special case, and perhaps fail spectacularly
> > instead of recovering gracefully in certain edge cases.  And in the
> > pathological case of a single number with 60 billion digits, run out of
> > memory (and complain loudly to the person who claimed that the file
> > contained a "dataset").  But why do I need to start with the least
> > significant digit?
> 
> You probably forgot that it has to be _streaming_. Suppose you parse
> the first digit: can you hand this information over to an external
> function to process the parsed data? -- No! because you don't know the
> magnitude yet.  What about two digits? -- Same thing.  You cannot
> leave the parser code until you know the magnitude (otherwise the
> information is useless to the external code).

If I recognize the first digit, then I *can* hand that over to an
external function to accumulate the digits that follow.

> So, even if you have enough memory and don't care about special cases
> like scientific notation: yes, you will be able to parse it, but it
> won't be a streaming parser.

Under that constraint, I'm not sure I can parse anything.  How can I
parse a string (and hand it over to an external function) until I've
found the closing quote?

How much state can a parser maintain (before it invokes an external
function) and still be considered streaming?  I fear that we may be
getting hung up on terminology rather than solving the problem at hand.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-10-01 Thread Dan Sommers via Python-list
On 2024-09-30 at 18:48:02 -0700,
Keith Thompson via Python-list  wrote:

> 2qdxy4rzwzuui...@potatochowder.com writes:
> [...]
> > In Common Lisp, you can write integers as #nnR[digits], where nn is the
> > decimal representation of the base (possibly without a leading zero),
> > the # and the R are literal characters, and the digits are written in
> > the intended base.  So the input #16f is read as the integer 65535.
> 
> Typo: You meant #16R, not #16f.

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


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-09-30 Thread Dan Sommers via Python-list
On 2024-10-01 at 09:09:07 +1000,
Chris Angelico via Python-list  wrote:

> On Tue, 1 Oct 2024 at 08:56, Grant Edwards via Python-list
>  wrote:
> >
> > On 2024-09-30, Dan Sommers via Python-list  wrote:
> >
> > > In Common Lisp, integers can be written in any integer base from two
> > > to thirty six, inclusive.  So knowing the last digit doesn't tell
> > > you whether an integer is even or odd until you know the base
> > > anyway.
> >
> > I had to think about that for an embarassingly long time before it
> > clicked.
> 
> The only part I'm not clear on is what identifies the base. If you're
> going to write numbers little-endian, it's not that hard to also write
> them with a base indicator before the digits [...]

In Common Lisp, you can write integers as #nnR[digits], where nn is the
decimal representation of the base (possibly without a leading zero),
the # and the R are literal characters, and the digits are written in
the intended base.  So the input #16f is read as the integer 65535.

You can also set or bind the global variable *read-base* (yes, the
asterisks are part of the name) to an integer between 2 and 36, and then
anything that looks like an integer in that base is interpreted as such
(including literals in programs).  The literals I described above are
still handled correctly no matter the current value of *read-base*.  So
if the value of *read-base* is 16, then the input  is read as the
integer 65535 (as is the input #16r).

(Pedants may point our details I omitted.  I admit to omitting them.)

IIRC, certain [old 8080 and Z-80?] assemblers used to put the base
indicator at the end.  So 10 meant, well, 10, but 10H meant 16 and 10b
meant 2 (IDK; the capital H and the lower case b both look right to me).

I don't recall numbers written from least significant digit to most
significant digit (big and little endian *storage*, yes, but not the
digits when presented to or read from a human).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-09-30 Thread Dan Sommers via Python-list
On 2024-10-01 at 04:46:35 +1000,
Chris Angelico via Python-list  wrote:

> On Tue, 1 Oct 2024 at 04:30, Dan Sommers via Python-list
>  wrote:
> >
> > But why do I need to start with the least
> > significant digit?
> 
> If you start from the most significant, you don't know anything about
> the number until you finish parsing it. There's almost nothing you can
> say about a number given that it starts with a particular sequence
> (since you don't know how MANY digits there are). However, if you know
> the LAST digits, you can make certain statements about it (trivial
> examples being whether it's odd or even).

But that wasn't the question.  Sure, under certain circumstances and for
specific use cases and/or requirements, there might be arguments to read
potential numbers as strings and possibly not have to parse them
completely before accepting or rejecting them.

And if I start with the least significant digit and the number happens
to be written in scientific notation and/or has a decimal point, then I
can't tell whether it's odd or even until I further process the whole
thing anyway.

> It's not very, well, significant. But there's something to it. And it
> extends nicely to p-adic numbers, which can have an infinite number of
> nonzero digits to the left of the decimal:
> 
> https://en.wikipedia.org/wiki/P-adic_number

In Common Lisp, integers can be written in any integer base from two to
thirty six, inclusive.  So knowing the last digit doesn't tell you
whether an integer is even or odd until you know the base anyway.

Curiously, we agree:  if you move the goal posts arbitrarily, then
some algorithms that parse JSON numbers will fail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with Streaming and Chunk Processing for Large JSON Data (60 GB) from Kenna API

2024-09-30 Thread Dan Sommers via Python-list
On 2024-09-30 at 11:44:50 -0400,
Grant Edwards via Python-list  wrote:

> On 2024-09-30, Left Right via Python-list  wrote:
> > Whether and to what degree you can stream JSON depends on JSON
> > structure. In general, however, JSON cannot be streamed (but commonly
> > it can be).
> >
> > Imagine a pathological case of this shape: 1... <60GB of digits>. This
> > is still a valid JSON (it doesn't have any limits on how many digits a
> > number can have). And you cannot parse this number in a streaming way
> > because in order to do that, you need to start with the least
> > significant digit.
> 
> Which is how arabic numbers were originally parsed, but when
> westerners adopted them from a R->L written language, thet didn't flip
> them around to match the L->R written language into which they were
> being adopted.

Interesting.

> So now long numbers can't be parsed as a stream in software. They
> should have anticipated this problem back in the 13th century and
> flipped the numbers around.

What am I missing?  Handwavingly, start with the first digit, and as
long as the next character is a digit, multipliy the accumulated result
by 10 (or the appropriate base) and add the next value.  Oh, and handle
scientific notation as a special case, and perhaps fail spectacularly
instead of recovering gracefully in certain edge cases.  And in the
pathological case of a single number with 60 billion digits, run out of
memory (and complain loudly to the person who claimed that the file
contained a "dataset").  But why do I need to start with the least
significant digit?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Common objects for CLI commands with Typer

2024-09-23 Thread Dan Sommers via Python-list
On 2024-09-23 at 19:00:10 +0100,
Barry Scott  wrote:

> > On 21 Sep 2024, at 11:40, Dan Sommers via Python-list 
> >  wrote:

> But once your code gets big the disciple of using classes helps
> maintenance. Code with lots of globals is problematic.

Even before your code gets big, discipline helps maintenance.  :-)

Every level of your program has globals.  An application with too many
classes is no better (or worse) than a class with too many methods, or a
module with too many functions.  Insert your own definitions of (and
tolerances for) "too many," which will vary in flexibility.

(And as was alluded to elsewhere in this thread, you could probably
deduce the original and/or preferred programming languages of people
with certain such definitions.  But I digress.)

$ python -m this|grep Namespaces
Namespaces are one honking great idea -- let's do more of those!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Common objects for CLI commands with Typer

2024-09-21 Thread Dan Sommers via Python-list
On 2024-09-21 at 06:38:05 +0100,
Barry via Python-list  wrote:

> > On 20 Sep 2024, at 21:01, Loris Bennett via Python-list 
> >  wrote:
> > 
> > Hi,
> > 
> > Apologies if the following description is to brief - I can expand if no
> > one knows what I'm on about, but maybe a short description is enough.
> > 
> > I am developing a command line application using Typer.  Most commands
> > need to do something in a database and also do LDAP stuff.  Currently
> > each command creates its own Database and LDAP objects, since each
> > command forms an entry point to the program.
> > 
> > With Typer, is there a way I can define the equivalent of class
> > attributes at a single point which are then available to all commands?
> 
> I do not know typer. But the general solution is to create an instance of 
> your class
> and tell typer to call member function of the instance.
> 
> app = Application()
> …
> typer.set_callback(app.my_handler)

Despite the fact that "everything is an object" in Python, you don't
have to put data or functions inside classes or objects.  I also know
nothing about Typer, but there's nothing wrong with functions in a
module.

There's also nothing wrong with writing a function that creates and
returns the database and LDAP connections (perhas as instances of
application-level classes), amd calling that function from within each
command.

DRY.  Yeah, yeah, yeah.  :-/ So there's one line at the top of each
comamnd that initializes things, and possibly a line at the bottom to
close those things down.  Turn those lines into a context manager, which
is actually a sub-framework inside Typer.  Don't convolute/compilicate
your design to eliminate one line at the top of each command.

Go ahead, accuse me of writing FORTRAN (all caps, no numbers or
qualifiers, as $deity intended) in Python.  But neither optimize
prematurely nor invoke the Inner Platform Effect to save one or two
lines in your not-yet-written commands, either.

Sorry for the rant.  :-)

Simple is better than complex.
Complex is better than complicated.

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


Re: Is there a better way? [combining f-string, thousands separator, right align]

2024-08-26 Thread Dan Sommers via Python-list
On 2024-08-26 at 20:42:32 +1200,
dn via Python-list  wrote:

> and if we really want to go over-board:
> 
> >>> RIGHT_JUSTIFIED = ">"
> >>> THOUSANDS_SEPARATOR = ","
> >>> s_format = F"{RIGHT_JUSTIFIED}{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
> 
> or (better) because right-justification is the default for numbers:
> 
> >>> s_format = F"{S_FIELD_WIDTH}{THOUSANDS_SEPARATOR}"
> 
> 
> To the extreme that if your user keeps fiddling with presentations (none
> ever do, do they?), all settings to do with s_format could be added to a
> config/environment file, and thus be even further separated from
> program-logic!

And then you'll need a parser, many of whose Unique Challenges™ aren't
even apparent until you start parsing files from actual users, and
you'll still need some sort of fallback in the code anyway for the case
that s_format can't be parsed (for whatever reason).

Isn't a config file what just caused the global CrowdStrike outage?  ;-)

That said, I understand that report generators are a thing, not to
mention RPG (https://en.wikipedia.org/wiki/IBM_RPG).

Okay, sorry; I'll just crawl back into the hole from whence I came.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: new here

2024-08-21 Thread Dan Sommers via Python-list
On 2024-08-20 at 23:16:48 -0400,
AVI GROSS via Python-list  wrote:

> I do wonder if the people at python.org want multiple forums. There is
> also one that sort of tutors people that obviously has an overlapping
> but different audience.

$ python -m this
The Zen of Python, by Tim Peters
[...]
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
[...]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best use of "open" context manager

2024-07-06 Thread Dan Sommers via Python-list
On 2024-07-06 at 11:49:06 +0100,
Rob Cliffe via Python-list  wrote:

> Is there a better / more Pythonic solution?

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

At least this attempts to abstract the problem of iterating over a file
(or multiple files) into a library routine.  I've used it a little, but
I don't know the full depths of your use case and/or requirements.

HTH,
Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Any marginally usable programming language approaches an ill defined barely usable re-implementation of half of Common-Lisp

2024-05-29 Thread Dan Sommers via Python-list
On 2024-05-29 at 11:39:14 -0700,
HenHanna via Python-list  wrote:

> On 5/27/2024 1:59 PM, 2qdxy4rzwzuui...@potatochowder.com wrote:

> > https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule

[...]

> Are  the Rules 1--9  by  Greenspun   good too?

I don't know; let me look it up.  Oh, there it is:

https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule says that
Greenspun said he "was just trying to give the rule a memorable name."

Sadly, the citation link is failing for me right now.
-- 
https://mail.python.org/mailman/listinfo/python-list


Formatted Output and Argument Parsing (was: Re: Flubbed it in the second interation through the string: range error... HOW?)

2024-05-29 Thread Dan Sommers via Python-list
On 2024-05-29 at 17:14:51 +1000,
Chris Angelico via Python-list  wrote:

> I wouldn't replace str.format() everywhere, nor would I replace
> percent encoding everywhere - but in this case, I think Thomas is
> correct. Not because it's 2024 (f-strings were brought in back in
> 2015, so they're hardly chronologically special), but because most of
> this looks like debugging output that can take advantage of this
> feature:
> 
> print(f"if block {name[index]=} {index=}")

defsnark:

After years of getopt (and even more, if you include non-Python
experience), I'm glad I decided to wait a decade before chugging the
optparse koolaid.

(For the history-impaired, getopt existed long before Python and will
likely exist long after it, but getopt's "replacement" optparse lasted
only from 2003 until 2011.)

That said, I agree that the = thing makes f-strings eminently useful for
debugging, and I wholeheartedly agree with not fixing things that aren't
broken.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Any marginally usable programming language approaches an ill defined barely usable re-implementation of half of Common-Lisp

2024-05-27 Thread Dan Sommers via Python-list
On 2024-05-27 at 12:37:01 -0700,
HenHanna via Python-list  wrote:

> 
> On 5/27/2024 7:18 AM, Cor wrote:
> > Some entity, AKA "B. Pym" ,
> > wrote this mindboggling stuff:
> > (selectively-snipped-or-not-p)
> > 
> > > On 12/16/2023, c...@clsnet.nl wrote:
> > > 
> > > > Any marginally usable programming language approaches an ill
> > > > defined barely usable re-implementation of half of common-lisp
> > > 
> > > The good news is, it's not Lisp that sucks, but Common Lisp.
> > >   --- Paul Graham
> > 
> > Just to set the record straight;
> > This is not My line.
> > I quoted it but don't know who the originator of that remark is.

https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Terminal Emulator (Posting On Python-List Prohibited)

2024-05-19 Thread Dan Sommers via Python-list
On 2024-05-19 at 18:13:23 +,
Gilmeh Serda via Python-list  wrote:


> Was there a reason they chose the name Pip?

Package Installer for Python

https://pip.pypa.io/en/stable/index.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Terminal Emulator (Posting On Python-List Prohibited)

2024-05-19 Thread Dan Sommers via Python-list
On 2024-05-19 at 18:13:23 +,
Gilmeh Serda via Python-list  wrote:

> Was there a reason they chose the name Pip?

Package Installer for Python

https://pip.pypa.io/en/stable/index.html

Every time I see PIP, I think Peripheral Interchange Program, but I'm
old.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to discover what values produced an exception?

2024-05-04 Thread Dan Sommers via Python-list
On 2024-05-03 at 10:56:39 -0300,
Johanne Fairchild via Python-list  wrote:

> How to discover what values produced an exception?  Or perhaps---why
> doesn't the Python traceback show the values involved in the TypeError?
> For instance:
> 
> --8<>8---
> >>> (0,0) < 4
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: '<' not supported between instances of 'tuple' and 'int'
> --8<>8---
> 
> It could have said something like: 
> 
> --8<>8---
> TypeError: '<' not supported between instances of 'tuple' and 'int'
>   in (0,0) < 4.
> --8<>8---
> 
> We would know which were the values that caused the problem, which would
> be very helpful.

I'm not disagreeing that knowing the values could be useful in many
cases.  In the general case, though, it's not practical.  Consider a
function like this:

def f(x, y):
return g(x) < h(y)

The printed values of x, y, g(x), and h(y) could all be millions of (or
more) glyphs.  Worse, one or more of those values could contain circular
lists or similar structures.  And h or g could have changed x or y.  In
summary, printing run-time values isn't always safe or useful.  At least
printing the types is safe.  In the face of ambiguity, refuse to guess.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Configuring an object via a dictionary

2024-03-20 Thread Dan Sommers via Python-list
On 2024-03-20 at 09:49:54 +0100,
Roel Schroeven via Python-list  wrote:

> You haven't only checked for None! You have rejected *every* falsish value,
> even though they may very well be acceptable values.

OTOH, only you can answer these questions about your situations.

Every application, every item of configuration data, is going to be a
little bit different.

What, exactly, does "missing" mean?  That there's no entry in a config
file?  That there's some sort of degenerate entry with "missing"
semantics (e.g. a line in a text file that contains the name of the
value and an equals sign, but no value)?  An empty string or list?  Are
you making your program easier for users to use, easier for testers to
test, easier for authors to write and to maintain, or something else?
What is your program allowed and not allowed to do in the face of
"missing" configuration data?

Once you've nailed down the semantics of the configuration data, then
the code usually falls out pretty quickly.  But arguing about corner
cases and failure modes without specifications is a losing battle.
Every piece of code is suspect unless you know what the inputs mean, and
what the application "should" do if the don't look like that.

Python's flexibiliry and expressiveness are double edge swords.  Use
them wisely.  :-)

Sorry for the rant.

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


Re: Configuring an object via a dictionary

2024-03-15 Thread Dan Sommers via Python-list
On 2024-03-15 at 15:48:17 -0400,
Thomas Passin via Python-list  wrote:

> [...] And I suppose there is always the possibility that sometime in
> the future an "or" clause like that will be changed to return a
> Boolean, which one would expect anyway.

Not only is the current value is way more useful, but changing it would
be a compatibility and maintenance nightmare.

If I want Java, I know where to find it.  :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extract lines from file, add to new files

2024-01-13 Thread Dan Sommers via Python-list
On 2024-01-13 at 11:34:29 +0100,
Left Right  wrote:

> > The Python term, at least colloquially, is "tuple unpacking."

That quote is from me.  Please do preserve attributions.

> Well, why use colloquialism if there's a language specification? Also,
> there weren't any tuples used in my example, at least not explicitly
> (i could've been a tuple, but that wasn't specified).

According to the language specification,⁰ it's a "target list," and
there can be more than one target in that list.

The unpacking isn't really called anything, it's just the way Python
assignment works, all the way back to its earliest stages.¹

⁰ https://docs.python.org/3/reference/simple_stmts.html#assignment-statements,
¹ https://docs.python.org/release/1.4/ref/ref6.html#HDR2
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extract lines from file, add to new files

2024-01-12 Thread Dan Sommers via Python-list
On 2024-01-13 at 02:02:39 +0100,
Left Right via Python-list  wrote:

> Actually, after some Web search.  I think, based on this:
> https://docs.python.org/3/reference/simple_stmts.html#grammar-token-python-grammar-augtarget
> that in Python you call this "augmented assignment target". The term
> isn't in the glossary, but so are many others.

The Python term, at least colloquially, is "tuple unpacking."

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


Re: How/where to store calibration values - written by program A, read by program B

2023-12-06 Thread Dan Sommers via Python-list
On 2023-12-06 at 09:32:02 +,
Chris Green via Python-list  wrote:

> Thomas Passin  wrote:

[...]

> > Just go with an .ini file. Simple, well-supported by the standard 
> > library. And it gives you key/value pairs.
> > 
> My requirement is *slightly* more complex than just key value pairs,
> it has one level of hierarchy, e.g.:-
> 
> KEY1:
>   a: v1
>   c: v3
>   d: v4
> KEY2:
>   a: v7
>   b: v5
>   d: v6
> 
> Different numbers of value pairs under each KEY.

INI files have sections.

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


Re: on writing a number as 2^s * q, where q is odd

2023-11-29 Thread Dan Sommers via Python-list
On 2023-11-29 at 21:44:01 -0300,
Julieta Shem via Python-list  wrote:

> How would you write this procedure?
> 
> --8<---cut here---start->8---
> def powers_of_2_in(n):
>   s = 0
>   while "I still find factors of 2 in n...":
> q, r = divmod(n, 2)
> if r == 0:
>   s = s + 1
>   n = n // 2
> else:
>   return s, n
> --8<---cut here---end--->8---

What's wrong with what you have?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Beep on WIndows 11

2023-11-12 Thread Dan Sommers via Python-list
On 2023-11-11 at 23:44:19 +,
Y Y via Python-list  wrote:

> I am curious and humble to ask: What is the purpose of a BEEP?

It's a simple way for a terminal-based program to alert (hence '\a') a
user or an operator that their attention is requested or required.

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


Re: Why doc call `__init__` as a method rather than function?

2023-09-15 Thread Dan Sommers via Python-list
On 2023-09-15 at 10:49:10 +,
scruel tao via Python-list  wrote:

> ```python
> >>> class A:
> ...   def __init__(self):
> ... pass
> ...
> >>> A.__init__
> 
> >>> a = A()
> >>> a.__init__
> >
> ```
> 
> On many books and even the official documents, it seems that many authors 
> prefer to call `__init__` as a "method" rather than a "function".
> The book PYTHON CRASH COURSE  mentioned that "A function that’s part of a 
> class is a method.", however, ` A.__init__` tells that `__init__` is a 
> function...

I always call __init__ "the initializer."  YMMV.

> I wonder how can I call `__init__` as? Consider the output above.
> Maybe both are OK? If you prefer or think that we must use one of the two, 
> please explain the why, I really want to know, thanks!

Usually, you don't call (or even refer to) __init__ from your
application.  One __init__ can call another one in the case of
initializing superclasses.

When you evaluate A(), Python calls __init__ for you.  You can see this
if you add something "visible" to __init__, like a print statement.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: A new version (0.5.1) of python-gnupg has been released.

2023-07-22 Thread Dan Sommers via Python-list
On 2023-07-22 at 11:04:35 +,
Vinay Sajip via Python-list  wrote:

> What Changed?
> =

What changed, indeed.

Maybe I'm old, and curmudgeonly, but it would be nice if the body of
these annoucement emails (not just this one) contained the name of the
program and a one-line summary of what the program does, preferably
right at the top.

(Admittedly, in this case, once I found the name of the program in the
subject and the footnotes, I was able to figure out what it does.  Not
all software is named that usefully.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to design a class that will listen on stdin?

2020-05-23 Thread Dan Sommers


On Saturday, May 23, 2020, at 07:24 -0400, zljubi...@gmail.com wrote:

> I have to talk to outer system by stdin/stdout.
> Each line that comes to stdin should be processed and its result returned 
> back to stdout. Logging should go to stderr.
> 
> How to design a class that will listed to stdin and call required methods in 
> order to process the data?

I wouldn't put it into a class, but the core of it looks something like
this:

for line in sys.stdin:
result = process(line)
print(result)
if some_condition():
break

The details may be different, and there's likely more error handling in
production code, but that's the general idea.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exiting a while loop

2020-05-22 Thread Dan Sommers

On Friday, May 22, 2020, at  7:49, John Yeadon via Python-list wrote:

> Am I unreasonable in expecting this code to exit when required?

Yes.  :-)

> # Add up the powers of 2 starting with 2**0 until 2 million is met.
> n = 1
> target = 200
> sum = 0
>
> while True:
>  x = 2 ** (n - 1)
>  sum += x
>  print(n, sum)
>  if sum >= target:
>  print("Target met.")
>  exit

Try break instead of exit.  See
https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
for more information.

>  n += 1
>
> print("\n", n, "terms are required.")

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why no camelCase in PEP 8?

2020-05-18 Thread Dan Sommers
On Tue, 19 May 2020 09:55:04 +1200
Juergen Brendel  wrote:

> ... he prefers snake-case.

That's not snake_case.  That's kebab-case.¹

¹ https://wiki.c2.com/?KebabCase
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: =+ for strings

2020-05-03 Thread Dan Sommers
On Sun, 3 May 2020 19:38:06 -0700 (PDT)
James Smith  wrote:

> I tried:
> dt=+"{:02d}".format(day)
> but I got:
> dt=+"{:02d}".format(day)
> TypeError: bad operand type for unary +: 'str'
> 
> This works:
> dt=dt+"{:02d}".format(day)
> 
> Why can't I do the shortcut on strings?

ITYM:

dt += "{:02d}".format(day)

Note += rather than =+ (which is parsed as an assignment operator
followed by a unary plus operator).

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What user-defined request error levels are recommended?

2020-04-30 Thread Dan Sommers
On Thu, 30 Apr 2020 13:14:42 -0700 (PDT)
Dan Campbell  wrote:

> Hi, what range of error codes are recommended, if we wanted to return
> a user-defined code?

> Obviously, we don't want to use a code in the 200+ range, or the 400+
> range, e.g.

> I want to throw, or just return, a code that represents that the size
> of a web page (len(response.content)) is less than the expected size.

It sounds like you're building an HTTP server; if not, then please give
more details.

My question is:  how "strong" is that expectation?  If there's some
calculation that says that the response should be X octets, and then the
logic comes up with Y octets instead, isn't that a logic bug?  Shouldn't
that be a 5XX response?

Did the client request X octets, but the server knows better and returns
a correct response that's Y octets long?  Then use a 2XX code.  Did the
client request X octets, but the server has more information and
disagrees?  Then use a 401 (Bad Request) code.

Most of these codes allow the server to pass additional information back
to the client, either in standard headers, X- headers, or the body of
the response.

What's the actual use case?

(Also, this isn't really a Python question; it's either a plain HTTP
question, or perhaps one specific to your web server.)

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Floating point problem

2020-04-18 Thread Dan Sommers
On Sun, 19 Apr 2020 01:25:00 +1200
DL Neil via Python-list  wrote:

> On 19/04/20 1:07 AM, Souvik Dutta wrote:
> > I have one question here. On using print(f"{c:.32f}") where c= 2/5 instead
> > of getting 32 zeroes I got some random numbers. The exact thing is
> > 0.40002220446049250313
> > Why do I get this and not 32 zeroes?
> 
> Approximating decimal numbers as binary values.
> 
> Do NOT try this at home! How many lines will the following code display 
> on-screen?
> 
>  >>> v = 0.1
>  >>> while v != 1.0:
> ... print(v)
> ... v += 0.1
> 
> As an exercise, try dividing 1.0 by 10.0 and then adding the result to 
> itself ten times.
> 
> Back in the ?good, old days, a Computer Science course would almost 
> certainly involve some "Numerical Analysis", when such issues would be 
> discussed. Not sure that many institutions offer such, these days...

These days, I continue to recommend _What Every Computer Scientist
Should Know About Floating-Point Arithmetic_ by David Goldberg.  The
first link that pops up in my search bubble is
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html; YMMV.

Yes, it's highly technical, but well worth the effort.

Or an extended session at the feet of the timbot, which is much harder
to come by.

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python script to give a list of prime no.

2020-04-05 Thread Dan Sommers
On Sun, 05 Apr 2020 19:46:00 +0200
Pieter van Oostrum  wrote:

> Sathvik Babu Veligatla  writes:
> 
> > hi,
> > I am new to python, and i am trying to output the prime numbers
> > beginning from 3 and i cannot get the required output.  It stops
> > after giving the output "7" and that's it.
> > CODE:
> > a = 3
> > l = []
> > while True:
> > for i in range(2,a):
> > if a%i == 0:
> > l.append(1)
> > else:
> > l.append(0)
> >
> > if l.count(1) == 0:
> > print(a)
> > a = a + 2
> > elif l.count(1) >> 0:
> > a = a + 2

[intervening optimizations omitted to conserve electrons]

> And you effectively use the counter as a boolean, so replace is by a boolean.
> 
> a = 3
> while True:
> is_prime = True
> for i in range(2,a):
> if a%i == 0:
> is_prime = False
> # once we found a divisor, it is no use to continue
> break
> if is_prime:
> print(a)
> a = a + 2
> 
> Further optimizations are possible, for example use range(2,a/2) or
> even range (2, sqrt(a)).

Now if only there were a way to get rid of that extraneous flag and
build some sort of "if the loop ran to completion" construct

a = 3
while True:
for i in range(2,a):
if a%i == 0:
# once we found a divisor, it is no use to continue
break
else:   # magic here!
print(a)
a = a + 2

The Time Machine Strikes Again,
Dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Confusing textwrap parameters, and request for RE help

2020-03-27 Thread Dan Sommers
On Fri, 27 Mar 2020 15:46:54 -0600
Michael Torrie  wrote:

> On 3/27/20 3:28 PM, Dan Stromberg wrote:

> > Back when I was a kid, and wordprocessors were exemplified by
> > WordStar, I heard about a study the conclusion of which was that
> > aligned right edges were harder to read - that it was better to
> > align on the left and leave the right ragged.

I remember WordStar, and its text editor cousin WordMaster.

> > But one study doesn't establish truth.

> I've read poorly-typeset, self-published books that were really hard
> to read due to the way the full justification worked out.  Not sure if
> that's just due to the poor typesetting job (a word processor probably
> did it), or the font, or what.  There must be some tricks that
> publishers use to both justify and keep the text looking good and
> readable ...

Ask Donald Knuth why he invented TeX, and why math is enclosed in
literal $ (U+0024) characters.  ;-)

Also, there are word processors and there are word processors.  From an
aesthetics perspective, TeX has been producing beautiful printed pages
(and pairs and sequences of pages) for decades, Word still didn't the
last time I looked (maybe 8 or 10 years ago?), and there are dozens or
more in between.

> ... At one point in the print world, the majority of what I read is
> fully justified, yet still quite readable.  Even now I think most
> published books I've seen recently are fully justified. These days,
> seems like more and more is ragged-right, such as magazines, which is
> probably due to the prevalence of digital media where it's probably
> easier to generate and easier to read on small screens.

If content producers did their job well, a 2540 pixel per inch printed
page, a desktop 72 or 96 pixel per inch display screen, and a handheld
320 pixel per inch screen would all be optimized separately (through one
sort of style sheet mechanism or another).  But these days, they spend
more time and money on ads and SEO than typesetting.

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: queue versus list

2020-03-20 Thread Dan Sommers
On Fri, 20 Mar 2020 06:50:29 -0700
Dan Stromberg  wrote:

> On Thu, Mar 19, 2020 at 6:11 PM Cameron Simpson  wrote:
> 
> > >> 4. If it doesn't need to be thread-safe, why not try deques instead?
> > >
> > >Bingo. Performance is indistinguishable from that of the list.
> >
> > A deque is implement using a list.
> >
> 
> Actually, I think a deque is a doubly linked list of python lists.

According to the source¹:

Data for deque objects is stored in a doubly-linked list of fixed
length blocks.

[...]

Textbook implementations of doubly-linked lists store one datum per
link, but that gives them a 200% memory overhead (a prev and next
link for each datum) and it costs one malloc() call per data
element.  By using fixed-length blocks, the link to data ratio is
significantly improved and there are proportionally fewer calls to
malloc() and free().  The data blocks of consecutive pointers also
improve cache locality.

¹ 
https://github.com/python/cpython/blob/3.8/Modules/_collectionsmodule.c#L19-L78

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Suggestions on mechanism or existing code - maintain persistence of file download history

2020-01-30 Thread Dan Sommers
On Thu, 30 Jan 2020 23:34:59 +1100
Chris Angelico  wrote:

> ... I wasn't advocating for the use of a database; my first and
> strongest recommendation was, and still is, a stateless system wherein
> the files themselves are the entire indication of which documents have
> been downloaded.

Yes, I like stateless systems, too, but that system isn't stateless.  As
I understand the problem of a "crudely persistem download manager,"
there's a collection of to-be-downloaded URLs (which may be empty) and
some data that's been downloaded (which may also be empty).  You can
certainly encode a lot of that directly in the file system, but it's
still state.

Using a database instead solves a lot of the tricky bits that the bare
file system doesn't (which is what ChrisA said in what I snipped).  It's
just Greenspun's Tenth Rule with the words C, Fortran, and Common Lisp
crossed out and Python and ACID Database written in crayon.

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Was: Dynamic Data type assignment

2020-01-30 Thread Dan Sommers
On Thu, 30 Jan 2020 20:40:50 +1300
DL Neil via Python-list  wrote:

> Why do we have [argparse] at the cmdLN and yet not have something
> similar for input?

Because argparse works on input, too?

Many examples on https://docs.python.org/3/library/argparse.html¹
explicitly pass a list of strings to parser.parse_args instead of
letting that function default to the list of command line arguments.

¹ e.g., https://docs.python.org/3/library/argparse.html#parents

Dan


-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Suggestions on mechanism or existing code - maintain persistence of file download history

2020-01-29 Thread Dan Sommers
On Thu, 30 Jan 2020 07:26:36 +1100
Chris Angelico  wrote:

> On Thu, Jan 30, 2020 at 7:06 AM jkn  wrote:

> > The situation is this - I have a long list of file URLs and want to
> > download these as a 'background task'. I want this to process to be
> > 'crudely persistent' - you can CTRL-C out, and next time you run
> > things it will pick up where it left off.

> A decent project. I've done this before but in restricted ways.

> > The download part is not difficult. Is is the persistence bit I am
> > thinking about.  It is not easy to tell the name of the downloaded
> > file from the URL.

Where do the names of the downloaded files come from now, and why can't
that same algorithm be used later to determine the existence of the
file?  How much control do you have over this algorithm (which leads to
what ChrisA suggested)?

> > I could have a file with all the URLs listed and work through each
> > line in turn.  But then I would have to rewrite the file (say, with
> > the previously-successful lines commented out) as I go.

Files have that problem.  Other solutions, e.g., a sqlite3 database,
don't.  Also, a database might give you a place to store other
information about the URL, such as the name of the associated file.

> Hmm. The easiest way would be to have something from the URL in the
> file name. For instance, you could hash the URL and put the first few
> digits of the hash in the file name, so
> http://some.domain.example/some/path/filename.html might get saved
> into "a39321604c - filename.html". That way, if you want to know if
> it's been downloaded already, you just hash the URL and see if any
> file begins with those digits.

> Would that kind of idea work?

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Threading

2020-01-24 Thread Dan Sommers
On Sat, 25 Jan 2020 07:43:49 +1100
Chris Angelico  wrote:

> On Sat, Jan 25, 2020 at 7:35 AM Matt  wrote:
> >
> > I am using this example for threading in Python:
> >
> > from threading import Thread
> >
> > def start_test( address, port ):
> > print address, port
> > sleep(1)
> >
> > for line in big_list:
> > t = Thread(target=start_test, args=(line, 80))
> > t.start()
> >
> > But say big_list has thousands of items and I only want to have a
> > maximum of 10 threads open.  How do work my way through the big_list
> > with only 10 threads for example?
> 
> First off, it is high time you move to Python 3, as the older versions
> of Python have reached end-of-life.
> 
> The best way is to create your ten threads, and have each one request
> "jobs" (for whatever definition of job you have) from a queue. Once
> the queue is exhausted, the threads terminate cleanly, and then you
> can join() each thread to wait for the entire queue to be completed.

Or use a thread pool:

https://docs.python.org/3/library/concurrent.futures.html

Dan

-- 
“Atoms are not things.” – Werner Heisenberg
Dan Sommers, http://www.tombstonezero.net/dan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Friday Finking: Source code organisation

2019-12-28 Thread Dan Sommers

On 12/28/19 6:52 PM, Greg Ewing wrote:

> On 29/12/19 11:49 am, Chris Angelico wrote:

>> "Define before use" is a broad principle that I try to follow, even
>> when the code itself doesn't mandate this.

> But strangely, I tend to do the opposite for methods of a class. I
> don't really know why. My instinctive idea of the "right" ordering
> just seems to flip over somehow between modules and classes.

Me, too.  :-/  Could be one of two reasons:

(1) putting the public API at the top and pushing the internal/helper
functions towards the bottom (whether program, library, or class); or

(2) writing too much FORTRAN and/or BASIC early on, where execution
begins at the top, whether you want it to or not

Who puts a class's __init__ function anywhere except the very top of a
class definition (doc strings and class attributes that aren't "def"ed
notwithstanding), even when it calls some other method in the class?

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


Re: Grepping words for match in a file

2019-12-28 Thread Dan Sommers

On 12/28/19 12:29 AM, Mahmood Naderan via Python-list wrote:

Hi
I have some lines in a text file like
ADD R1, R2
ADD3 R4, R5, R6
ADD.MOV R1, R2, [0x10]
If I grep words with this code
for line in fp:
 if my_word in line:
Then if my_word is "ADD", I get 3 matches. However, if I grep word with this 
code
for line in fp:
 for word in line.split():
 if my_word == word:
Then I get only one match which is ADD R1. R2.
Actually I want to get 2 matches. ADD R1, R2 and ADD.MOV R1, R2, [0x10] because these two lines are 
actually "ADD" instructions. However, "ADD3" is something else.
How can I fix the code for that purpose?

(1) word.startswith() won't solve your problem.  Expliticly
checking the character after the second "D" would.  You'll
have to determine which characters are or aren't part of
the instruction.  A complete solution probably depends on
what else you are or will look for in the future.

(2) That looks like a programming language (88000? POWER?).
Watch out for comments containing the word ADD, too.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Grepping words for match in a file

2019-12-28 Thread Dan Sommers

On 12/28/19 12:29 AM, Mahmood Naderan via Python-list wrote:

Hi
I have some lines in a text file like
ADD R1, R2
ADD3 R4, R5, R6
ADD.MOV R1, R2, [0x10]
If I grep words with this code
for line in fp:
 if my_word in line:
Then if my_word is "ADD", I get 3 matches. However, if I grep word with this 
code
for line in fp:
 for word in line.split():
 if my_word == word:
Then I get only one match which is ADD R1. R2.
Actually I want to get 2 matches. ADD R1, R2 and ADD.MOV R1, R2, [0x10] because these two lines are 
actually "ADD" instructions. However, "ADD3" is something else.
How can I fix the code for that purpose?


Grep's "words" are different from what line.split() returns:

>>> 'ADD.MOV R1, R2, [0x10]'.split()
['ADD.MOV', 'R1,', 'R2,', '[0x10]']

If you want to match "ADD" instructions, you'll have to use
word.startswith or some other method to separate the "ADD"
from the ".MOV."

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


Re: urllib unqoute providing string mismatch between string found using os.walk (Python3)

2019-12-21 Thread Dan Sommers

On 12/21/19 4:46 PM, Ben Hearn wrote:


import difflib
print('\n'.join(difflib.ndiff([a], [b])))
- /Users/macbookpro/Music/tracks_new/_NS_2018/J.Staaf - ¡Móchate! 
_PromoMix_.wav
?   
   ^^
+ /Users/macbookpro/Music/tracks_new/_NS_2018/J.Staaf - ¡Móchate! _PromoMix_.wav
?   
   ^


What am I missing when it comes to unquoting the string, or should I do some 
other fancy operation on the drive string?


I'm going to guess that the trailing characters are newline
(U+0010) and/or carriage return (U+001D) characters.  Use
str.strip() to remove them before comparing:

a = a.strip()

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


Re: IOError: cannot open resource

2019-12-07 Thread Dan Sommers

On 12/7/19 9:43 AM, RobH wrote:

When I run a python project with an oled display on a rasperry pi zero,
it calls for the Minecraftia.ttf font. I have the said file in
home/pi/.fonts/


Do you mean /home/pi/.fonts (with a leading slash, an absolute path
rather than a relative one)?

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


Re: low-level csv

2019-11-17 Thread Dan Sommers

On 11/17/19 7:18 AM, Antoon Pardon wrote:

This is python 2.6->2.7 and 3.5->3.7

I need to convert a string that is a csv line to a list and vice versa.
I thought to find functions doing this in the csv module but that doesn't
seem to be the case.

I can of course write special classes that would allow for this using
a csv.reader and csv.writer but that would be some convoluted code.

Anyone some suggestions?


Wrap your string in a list; csv.reader takes an iterator:

s = "some, string, with, commas"
r = csv.reader([s])
for x in r:
print x

Dan

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


Re: pathlib

2019-10-03 Thread Dan Sommers

On 10/2/19 6:58 PM, DL Neil via Python-list wrote:
> On 3/10/19 12:42 AM, Dan Sommers wrote:

> Certainly, although some may have quietly given-up talking to a
> non-OOP native - and one so 'slow', I am appreciative of all
> help-given!

I also speak OO as a second language (usually kicking, screaming, and
spouting expletives, but that's my personal perspective and a separate
issue).  Hold this thought.

>> Maybe you could implement one of the proposed changes in a private
>> library function as a workaround?

> In my mind, I'm wondering if it will come to that (having 'got past'
> the original observation/issue, I'm concerned by .rename()'s silent
> errors, for example). However, that 'outside' research, eg
> StackOverflow, shows that sub-classing pathlib is problematic, and
> quite possibly not part of the design (this is repeating 'gossip' -
> I'm not going to try to justify the comment or the claim) ...

So don't subclass anything.  :-)

One non-OO option would be to write a function that takes a Path
instance and a new name, calls Path.rename, and returns a new Path
instance with the new name, something like this (untested):

def path_rename(path, target):
new_path = pathlib.Path(target)
path.rename(new_path)
return new_path

Because it returns a new Path instance rather than mutating an existing
one, you may have to re-think parts of your application that depend on a
specific Path instance being mutated.

The usual OO option that doesn't involve subclassing is Delegation, with
a capital D.  See <https://en.wikipedia.org/wiki/Delegation_pattern>,
and see Mhttps://softwareengineering.stackexchange.com/questions/381140>
for python-specific objections.

> ... That said, last night my code sub-classing Path() seemed to work
> quite happily (albeit only tested on a 'Posix' box). The yawning
> chasm/gaping jaws below, however, are that I've probably made yet
> another 'assumption' about how things 'should' work.  Run for the
> hills!

Your caution regarding an assumption is a Good Thing.  Tests, tests, and
more tests.  Document (in large, friendly lettering) that you haven't
tested in a non-Posix environment.

> This was supposed to be a simple, down-time task; a
> learning-opportunity re-factoring code to use a new (er, um, as of
> v3.4) library...

The best laid plans  :-)
--
https://mail.python.org/mailman/listinfo/python-list


Re: pathlib

2019-10-02 Thread Dan Sommers

On 10/2/19 4:14 AM, DL Neil via Python-list wrote:


In the case that sparked this enquiry, and in most others, there is no
need for a path that doesn't actually lead somewhere. The paths that are
used, identify files, open them, rename them, create directories, etc.
The idea of a path that just 'exists' (sorry!), without being part of
some purpose, seems odd.


Think about preferences.  Users can put preferences in (e.g.)
~/.config/prefs, admins might provide "local" preferences in
/etc/prefs, and the developers might provide fallback preferences
in /etc/prefs.

When the application starts up, it could have a list of paths to files
that may or may not exist.

Or think about the shell in that failed "cat" command.  It's
possible that cat created a path from what the user typed and
then tried to open it.  For that brief moment, cat had a path
to a file that didn't exist, however un-useful it may have been.


At this time (and assuming that after two (separate) incidents dragging
me away to solve other people's problems, I intend to stick with trying
to get my head around pathlib - even if I have to sub-class it (which my
reading shows is another 'can of worms'). So, 'reading' is about all
I've accomplished since the original post. Sadly, the majority of posts
seem to have come from other confused-minds - many of whom seemed to be
giving-up in disgust. If true, such represents TWO failures! I'm sure
that the designer(s) had a clear vision (having watched previous
attempts rise-and-fall), but per earlier in this discussion, maybe the
explanation and 'vision' could be better communicated to us simple-boys?


I don't think anyone gave up in disgust.  Yes, there was some
disagreement, and now the discussion has slowed or stopped, but I
think your original question was answered:  Path objects,
apparently by an arguably questionable design, fail to meet your
expecation, and some simple changes end up breaking backwards
compatibility.  Maybe a documentation change could prevent others
from experiencing the same expectation failure.

Maybe you could implement one of the proposed changes in a private
library function as a workaround?
--
https://mail.python.org/mailman/listinfo/python-list


Re: pathlib

2019-09-30 Thread Dan Sommers

On 9/30/19 3:56 PM, Barry Scott wrote:



On 30 Sep 2019, at 16:49, Dan Sommers 
<2qdxy4rzwzuui...@potatochowder.com 
<mailto:2qdxy4rzwzuui...@potatochowder.com>> wrote:



In the totality of a Path object that claims to represent paths
to files,


It represents string that *should* in most cases work in the APIs
that work on files. Not the same idea.


I'm not sure the documentation supports your view.  Components
of paths are strings, but IMO the Path object represents a file.


including the arguably troublesome read_bytes and
write_bytes methods, and a rename method, however, it's not
unreasonable expect the object to track *itself* when I use *its*
own rename method (backwards compatibility restraints
notwithstanding).


"the object" did track the changes its just that "the object" is not
the Path object, it's in the operating system and/or the file system.
For the rename it's the directory that the name is recorded in.


So which is it?  Does the Path object represent a string, the
name of a file (whether that file exists or not), or the file
itself?  A moment ago, you claimed that a Path object represents
a string that should work in the APIs that work on files.  Now,
you're claiming that the Path object is a proxy for something in
the filesystem.  I don't mean to be combative or confrontational,
but I think that this fuzziness/inconsistency is at or near the
root of the differing expectations.


There was an interest talk at this years PYCON UK about the
the errors that people new to python make. Misunderstand syntax
is about 1/3 of the problems, but 2/3 was having the wrong model.

This discussion seems to fall into the "wrong model" world that
leads to a expectation failure.


On this (that there's something about the model of Path objects
that leads to expectation failures) we agree.  :-)


Have we moved on to how we can improve the situation?


Absolutely.

Documenting the fact that calling rename on a Path object does
not update that object, and at least an acknowledgement of the
backwards compatibility issues, would be a good start.  Does the
same sort of thing happen with Path.chmod as well?

Clarifying what a Path object represents is also in order.  Again,
I note that the top of https://docs.python.org/3/library/pathlib.html
talks about filesystem paths, but a lot of the method descriptions
(e.g., rename) use the phrase "this file," as if Path objects
represent (or are actually proxies for) actual files or an actual
filesystem.
--
https://mail.python.org/mailman/listinfo/python-list


Re: pathlib

2019-09-30 Thread Dan Sommers

On 9/30/19 12:51 PM, Chris Angelico wrote:

On Tue, Oct 1, 2019 at 1:51 AM Dan Sommers
<2qdxy4rzwzuui...@potatochowder.com> wrote:

In the totality of a Path object that claims to represent paths
to files, including the arguably troublesome read_bytes and
write_bytes methods, and a rename method, however, it's not
unreasonable expect the object to track *itself* when I use *its*
own rename method (backwards compatibility restraints
notwithstanding).



What if the path is formed from another path, and the other one
renames? Create a Path("/path/to/some/dir") and then file = dir /
"some_file", then rename the directory - should the file insta-reflect
that? Even if you DO have them link magically so that it updates, you
would then have to contend with the fact that, when you rename
directories, you often *want* to slide onto the new dir - that's one
way to replace an entire set of files atomically (which also involves
open_at).


Mu.

The filesystem is its own ultimate arbiter.  It handles (or not)
race conditions, failures, multiple processes, etc. in some
manner, which might not even be reasonable or consistent from one
moment to the next (who among us hasn't debugged a problem that
came down the patch level of NFS running on some remote system?).
The level to which some random object in a Python heap reflects
any given filesystem is, well, arbitrary.

All I'm doing is defending the OP, who was surprised that
renaming a file *using a Path instance* didn't reflect that
operation *in that Path instance*.  I believe that such a
surprise is reasonable; others apparently don't.

Yes, there are a lot of reasons that it is the way it is, and a
lot of reasons not to change it now.  I get that.  If the OP is
still here, then I'd like to think that the OP also gets that.
--
https://mail.python.org/mailman/listinfo/python-list


Re: pathlib

2019-09-30 Thread Dan Sommers

On 9/30/19 10:33 AM, Barry Scott wrote:



On 30 Sep 2019, at 14:20, Dan Sommers 
<2qdxy4rzwzuui...@potatochowder.com 
<mailto:2qdxy4rzwzuui...@potatochowder.com>> wrote:


That's the wording I read.  I inferred that "path-handling operations
which don't actually access a filesystem" meant an object that didn't
necessarily represent an actual file, and that "provide methods to do
system calls on path objects" did indicate an actual file.  From the
existence of Path.read_bytes, I inferred that at least some Path objects
represent (and operate on) actual existing files.  I've been doing this
for a long time, and I may have read my expecations into those words.


pathlib.Path() adds the semantics of operating system file rules to strings.


pathlib.Path() adds the semantics of OS pathnames to strings


The addition of open(), read_bytes() etc is a convenience to the programmer.
Some people consider this a mistake other a great feature.


Regardless of your opinion of those methods, they exist, and
evidently, they lead to arguably unfounded expectations.

Do they rise to the level of a wart that should be deprecated?

I don't know.

Are they practical in a number of use cases?

Probably.

That was my original point:  a Path object backed by OS- and
filesystem- objects and operations is a great idea, but it's
imperfect and it leads some programmers astray.


You can write this:

pathlib.Path("name").open()

Which is the same as:

open(pathlib.Path("name"))

Which is the same as:

open("name")

You would not expect str to track the file, why expect Path() to?


That's an interesting question.  If you phrase the question like
that, then you're right:  expecting a string to track the content
of a file is a mistake.

In the totality of a Path object that claims to represent paths
to files, including the arguably troublesome read_bytes and
write_bytes methods, and a rename method, however, it's not
unreasonable expect the object to track *itself* when I use *its*
own rename method (backwards compatibility restraints
notwithstanding).

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


Re: pathlib

2019-09-30 Thread Dan Sommers



On 9/30/19 8:40 AM, Barry Scott wrote:
 >
 >
 >> On 30 Sep 2019, at 12:51, Dan Sommers
<2qdxy4rzwzuui...@potatochowder.com> wrote:
 >>
 >> On 9/30/19 4:28 AM, Barry Scott wrote:
 >>>> On 30 Sep 2019, at 05:40, DL Neil via Python-list
 wrote:
 >>>> Should pathlib reflect changes it has made to the file-system?
 >>> I think it should not.
 >>> A Path() is the name of a file it is not the file itself. Why 
should it

 >>> track changes in the file system for the name?
 >>
 >> I would have said the same thing, but the docs⁰ disagree:  a
 >> PurePath represents the name of (or the path to) a file, but a
 >> Path represents the actual file.
 >
 > I'm not seeing that wording in the python 3.7 pathlib documentation.
 > Can you quote the exact wording please?
 >
 > I do see this:
 >
 > "Pure path objects provide path-handling operations which don’t
actually access a filesystem."
 >
 > And:
 >
 > "Concrete paths are subclasses of the pure path classes. In addition
to operations provided
 > by the latter, they also provide methods to do system calls on path
objects."

That's the wording I read.  I inferred that "path-handling operations
which don't actually access a filesystem" meant an object that didn't
necessarily represent an actual file, and that "provide methods to do
system calls on path objects" did indicate an actual file.  From the
existence of Path.read_bytes, I inferred that at least some Path objects
represent (and operate on) actual existing files.  I've been doing this
for a long time, and I may have read my expecations into those words.

 > There is no requirement that a Path() names a file that exists even.

Agreed.

 >> That said, why doesn't your argument apply to read and write?  I
 >> would certainly expect that writing to a path and then reading
 >> from that same path would return the newly written data.  If I
 >> squint funny, the Path object is tracking the operations on the
 >> file system.
 >
 > I do not expect that. Consider the time line:
 >
 > 1. with p.open('w') write data
 > 2. external process changes file on disk
 > 3. with p.open('r') read data
 >
 > How would (3) get the data written at (1) guaranteed?
 > It will lead to bugs to assume that.

I didn't say anything about a guarantee, or about an external processes.
If I have a single process that writes data to a file and then reads
from that file, I would expect to read what I just wrote.  See the
documentation of Path.read_bytes and Path.write_bytes.  If I throw an
external process, or a networked file system, or multiple layers of
buffering and/or caching into the mix, then all such bets are off.

I think you're making my point about expectations.  :-)

 > The path object is allowing system calls that need a file's path to
be called,
 > that is all. Beyond that there is no relationship between the
pathlib.Path()
 > objects and files.

The documentation of Path.read_bytes and Path.write_bytes say otherwise.

 >> I think I'm actually arguing against some long since made (and
 >> forgotten?) design decisions that can't be changed (dare I say
 >> fixed?) because changing them would break backwards
 >> compatibility.
 >>
 >> Yuck.  :-)  And I can absolutely see all sorts of different
 >> expecations not being met and having to be explained by saying
 >> "well, that's the way it works."
 >
 > I'd suggest that the design is reasonable and If there is
misunderstanding that its
 > something that docs could address.

I'm not disagreeing.  I suspect that we've both worked on enough
different systems to know that not all OSes, file systems, libraries,
and versions and combinations thereof work the same way under all
circumstances (multiple threads, multiple processes, caching, buffering,
etc.).  It's the epitome of YMMV.

Rename is a particularly thorny case because renaming a file, at least
on a POSIX system, is an operation on the directory containing the file
rather than the file itself.
--
https://mail.python.org/mailman/listinfo/python-list


Re: pathlib

2019-09-30 Thread Dan Sommers

On 9/30/19 4:28 AM, Barry Scott wrote:




On 30 Sep 2019, at 05:40, DL Neil via Python-list  
wrote:

Should pathlib reflect changes it has made to the file-system?


I think it should not.

A Path() is the name of a file it is not the file itself. Why should it
track changes in the file system for the name?


I would have said the same thing, but the docs⁰ disagree:  a
PurePath represents the name of (or the path to) a file, but a
Path represents the actual file.

That said, why doesn't your argument apply to read and write?  I
would certainly expect that writing to a path and then reading
from that same path would return the newly written data.  If I
squint funny, the Path object is tracking the operations on the
file system.

I think I'm actually arguing against some long since made (and
forgotten?) design decisions that can't be changed (dare I say
fixed?) because changing them would break backwards
compatibility.

Yuck.  :-)  And I can absolutely see all sorts of different
expecations not being met and having to be explained by saying
"well, that's the way it works."

⁰ https://docs.python.org/3/library/pathlib.html
--
https://mail.python.org/mailman/listinfo/python-list


Re: Recursive method in class

2019-09-27 Thread Dan Sommers

On 9/27/19 7:54 AM, ast wrote:

Hello

Is it feasible to define a recursive method in a class ?
(I don't need it, it's just a trial)

Here are failing codes:


class Test:
  def fib(self, n):
  if n < 2: return n
  return fib(self, n-2) + fib(self, n-1)


return self.fib(n - 2) + self.fib(n - 1)

(1) This is a horribly ineffective way to compute Fibonacci
numbers, but that's not immediately relevant.

(2) Recursive instance methods only make sense when there's
some sort of state being maintained, which in this case there
is not.  But that's also not immediately relevant.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Spread a statement over various lines

2019-09-17 Thread Dan Sommers

On 9/17/19 2:59 PM, Manfred Lotz wrote:

> def regex_from_filepat(fpat):
>  rfpat = fpat.replace('.', '\\.') \
>.replace('%', '.')  \
>.replace('*', '.*')
>
>  return '^' + rfpat + '$'
>
> As I don't want to have the replace() functions in one line my
> question is if it is ok to spread the statement over various lines as
> shown above, or if there is a better way?

Is that way okay?  Sure.  Are there other ways?  Sure.

To isolate each replace() function on its own line, and to eliminate the
clutter of the backslashes, consider this:

rfpat = (fpat
  .replace('.', '\\.')
  .replace('%', '.')
  .replace('*', '.*')
)

"Better" is going to depend on your initial reason for not wanting all
those function calls on one line.  What is that reason?
--
https://mail.python.org/mailman/listinfo/python-list


Re: fileinput module not yielding expected results

2019-09-07 Thread Dan Sommers

On 9/7/19 11:12 AM, Jason Friedman wrote:


$ grep "File number" ~/result | sort | uniq
File number: 3

I expected that last grep to yield:
File number: 1
File number: 2
File number: 3
File number: 4
File number: 5
File number: 6


As per https://docs.python.org/3/library/fileinput.html#fileinput.fileno,
fileno is the underlying file descriptor of the file, and not at
all what you're looking for.


My ultimate goal is as follows. I have multiple CSV files, each with the
same header line. I want to read the header line from the first file and
ignore it for subsequent files.


If you're certain that the headers are the same in each file,
then there's no harm and much simplicity in reading them each
time they come up.

with fileinput ...:
for line in f:
if fileinput.isfirstline():
headers = extract_headers(line)
else:
pass # process a non-header line here

Yes, the program will take slightly longer to run.  No, you won't
notice it.
--
https://mail.python.org/mailman/listinfo/python-list


Re: itertools cycle() docs question

2019-08-21 Thread Dan Sommers

On 8/21/19 2:32 PM, Calvin Spealman wrote:

The point is to demonstrate the effect, not the specific implementation.


Once you've gone through the iterable once, it's falsey,
which means that the while loop will end.  But if you copy
all the elements to a real list, then the while loop is
infinite.

Dan


On Wed, Aug 21, 2019 at 2:30 PM Tobiah  wrote:


In the docs for itertools.cycle() there is
a bit of equivalent code given:

 def cycle(iterable):
 # cycle('ABCD') --> A B C D A B C D A B C D ...
 saved = []
 for element in iterable:
 yield element
 saved.append(element)
 while saved:
 for element in saved:
 yield element


Is that really how it works?  Why make
the copy of the elements?  This seems
to be equivalent:


 def cycle(iterable):
 while iterable:
 for thing in iterable:
 yield thing
--
https://mail.python.org/mailman/listinfo/python-list






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


Re: String slices

2019-08-09 Thread Dan Sommers

On 8/9/19 10:13 AM, Paul St George wrote:

In the code (below) I want a new line like this:

Plane rotation X: 0.0
Plane rotation Y: 0.0
Plane rotation Z: 0.0

But not like this:

Plane rotation X:
0.0
Plane rotation Y:
0.0
Plane rotation Z:
0.0

Is it possible?
(I am using Python 3.5 within Blender.)

#
import os

outstream = open(os.path.splitext(bpy.data.filepath)[0] + ".txt",'w')

print(

"Plane rotation X:",bpy.data.objects["Plane"].rotation_euler[0],

"Plane rotation Y:",bpy.data.objects["Plane"].rotation_euler[1],

"Plane rotation Z:",bpy.data.objects["Plane"].rotation_euler[2],

file=outstream, sep="\n"

)

outstream.close()



Use separate calls to print:

with open(...) as outstream:
plane = bpy.data.objects["Plane"]
print("X:", plane.rotation_euler[0])
print("Y:", plane.rotation_euler[1])
# print Z here

And please (a) use "with" instead of "open" and "close," and
(b) use an email client that preserves indentation.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python help needed

2019-08-08 Thread Dan Sommers

On 8/8/19 12:26 PM, Paolo G. Cantore wrote:

> I think the special case treatment could be avoided.
>
> First: Join all items with ' and '
> Second: Replace all ' and ' with ', ' except the last

That works great, until one of the elements of the original list is
"spam and eggs":

>>> spam = ['apples', 'spam and eggs', 'bananas', 'tofu', 'cats']
>>> s = " and ".join(spam)
>>> s.replace(" and ", ", ", len(spam) - 2)  # except the last
'apples, spam, eggs, bananas and tofu and cats'

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


Re: if bytes != str:

2019-08-04 Thread Dan Sommers

On 8/4/19 10:56 AM, Hongyi Zhao wrote:

Hi,

I read and learn the the following code now:

https://github.com/shadowsocksr-backup/shadowsocksr-libev/blob/master/src/
ssrlink.py

In this script, there are the following two customized functions:


--
def to_bytes(s):
if bytes != str:
if type(s) == str:
return s.encode('utf-8')
return s

def to_str(s):
if bytes != str:
if type(s) == bytes:
return s.decode('utf-8')
return s
--

I've the following confusion on the above code:

Why should use `if bytes != str:' here?  I mean, this will always return
True, IMO.


Not in Python 2:

Python 2.7.16 (default, Mar 11 2019, 18:59:25)
[GCC 8.2.1 20181127] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> bytes != str
False


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


Re: Boolean comparison & PEP8

2019-07-29 Thread Dan Sommers

On 7/29/19 10:02 AM, David Raymond wrote:

> I think the other part of the discussion to be had here is: how do you
> name your booleans?

Yep.

> ... To me the name of a boolean variable should be obvious that it's a
> boolean ...

Well, yeah, maybe.  If it's really only a boolean, and its value is
always either True or False, then I agree.

> if shell:

> #wait, "shell" is not really a statement or an instruction. "If
> shell"... what? "I need to shell"? Is this whether we want to use a
> shell, or if we discovered that we're already in one? Or is "shell"
> not really a boolean, and is holding which shell we want, and we're
> just using the "truthyness" to make sure it's not None? What's going
> on here? Dangit, ok, where's the documentation?  (subprocess is common
> enough that people know this, but imagine a much less frequently used
> module/function)

> Do I have a point or am I just smoking crack? Does anyone else have
> examples?

Consider an algorithm like this to determine which shell to use:

shell = (get_shell_from_command_line()
 or get_shell_from_project_preferences()
 or get_shell_from_personal_preferences()
 or get_shell_from_system_preferences())

where each get_shell_from_XXX() function returns a /path/to/shell string
or None.  At least to me, Python's "or" operator and its notion of
truthiness make this algorithm natural.  Perhaps a better name is
path_to_shell, but I'd still end up with:

if path_to_shell:
execute_program(path_to_shell, ...)
else:
raise NoShellException()

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


Re: Proper shebang for python3

2019-07-25 Thread Dan Sommers

On 7/24/19 10:24 PM, Michael Torrie wrote:

> ... In more recent times, binaries that are mostly applicable to the
> super user go there.  I don't see why you would want to merge those.
> A normal user rarely has need of much in /sbin.  Already /bin has way
> too much stuff in it (although I don't see any other way to
> practically do it without ridiculous PATHs searching all over the
> disk).

On ancient file systems (SysV?), ISTR something like 1400 files in a
directory being some sort of limit, or perhaps a major performance
issue.  Separate directories (including /usr/X11/bin) mitigated that,
too.

> Having said that, I note that on my CentOS 7 workstation, sbin seems
> to be in the path by default. So that negates my argument I suppose.
> Although I might have made that change myself.

My .shrc file has all sorts of leftovers from the old days, but my
current Linux PATH is just $HOME/bin, $HOME/local/bin, and /usr/bin.

Get Off My Lawn,
Dan
--
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Dan Sommers




On 7/13/19 5:54 AM, Abdur-Rahmaan Janhangeer wrote:
> > Greetings,
> >
> > Given this snippet
> >
> > from itertools import *
> > import operator
> >
> >
> > x = [1, 2, 3] # [0, 1, 2, 3, ..., 10]
> >
> > y = accumulate(x, operator.mul)
> >
> > print(list(y))
> >
> > why does x = list(range(5)) produces only zeros?

I see two things going on here.

(1) Don't type snippets of code and results from memory, or
in bits and pieces from an interactive session.  Copy and
paste exactly the code that ran and the output that it
produced.

(2) What is range(5)?  Okay, what is list(range(5))?  What
do you (the person) get when you multiply those five
integers together?

HTH,
Dan
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function to determine list max without itertools

2019-04-19 Thread Dan Sommers

On 4/19/19 4:01 AM, Sayth Renshaw wrote:


Now, what happens when the code is tested with various (different) sets 
of test-data?

(remember the last question from my previous msg!?)


It fails on any list entry that isn't a float or an int, giving a TypeError.


What if the *first* entry isn't a float or an int?  For
instance, what if the list is ['a', 'b', 'c']?  What about
['a', 'b', 3]?

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


Re: How call method from a method in same class?

2019-04-01 Thread Dan Sommers

On 4/1/19 10:02 PM, Dave wrote:


  def validScale(self, scaleName):
  if scaleName.upper == 'F' or 'C' or 'K':
  return True
  else:
  return False

  def convertTemp(self):
  """ Converts temperature scale if scales valid."""
  if validScale(self.scale):
  scaleValid = True
  if validScale(self.newScale):
  newScaleValid = True
  if scaleValid and newScaleValid:
  print('Scale converted')
  else:
  msg = "There was and error with the scales entered.\n"
  msg = msg + "You entered: " + self.scale
  msg = msg + ' ' 'and' + self.newScale
  print(msg)


Note that validscale has a self parameter.  Call it just
like you would from outside the class:

if self.validScale(self.newScale):
newScaleValid = True

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


Re: Library for parsing binary structures

2019-03-29 Thread Dan Sommers

On 3/29/19 12:13 PM, Peter J. Holzer wrote:


Obviously you need some way to describe the specific binary format you
want to parse - in other words, a grammar. The library could then use
the grammar to parse the input - either by interpreting it directly, or
by generating (Python) code from it. The latter has the advantage that
it has to be done only once, not every time you want to parse a file.

If that sounds familiar, it's what yacc does. Except that it does it for
text files, not binary files. I am not aware of any generic binary
parser generator for Python. I have read research papers about such
generators for (I think) C and Java, but I don't remember the names and
I'm not sure if the generators got beyond the proof of concept stage.


It's been a while since I've used those tools, but if you
create a lexer (the yylex() function) that can tokenize a
binary stream, then yacc won't know the difference.
--
https://mail.python.org/mailman/listinfo/python-list


Re: What does "TypeError: unsupported operand type(s) for +: 'function' and 'int'" mean?

2019-03-25 Thread Dan Sommers

On 3/25/19 2:30 PM, CrazyVideoGamez wrote:

I have no idea what "TypeError: unsupported operand type(s) for +: 'function' and 
'int'" means


It means that you're trying to add an int to a function.

> ... and I don't know how to fix it. Help!

Don't do that?

It's possible that with the correct context (copied and
pasted, not just retyped from memory), someone can help
you appropriately.

What version of Python are you using?  What program are
you running?  Is this homework?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Can my python program send me a text message?

2019-03-19 Thread Dan Sommers

On 3/19/19 2:35 PM, Chris Angelico wrote:

On Wed, Mar 20, 2019 at 6:31 AM Steve  wrote:


I have a program that triggers a reminder timer.  When that timer is done, I 
would like to receive a text message on my phone to tell me that it is time to 
reset the experiment.

Can this be done using Python?



Yes! There are APIs that will help you with that. Search the web for
"text message api" and see what you can find. (Be aware that some of
them will ask you to pay money.) Then pick one of them and see if you
can access it from Python.


Some telephone carriers provide cost-free email-to-SMS
gateways.


Most of them will be HTTP-based APIs, so you will do well to use the
"requests" module.


Send email over SMTP; use Python's standard smtplib module.

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


Re: subprocess svn checkout password issue

2019-03-16 Thread Dan Sommers

On 3/16/19 1:50 AM, Martin De Kauwe wrote:

On Saturday, 16 March 2019 16:50:23 UTC+11, dieter  wrote:

Martin De Kauwe  writes:

> I'm trying to write a script that will make a checkout from a svn repo and build the result for the user. However, when I attempt to interface with the shell it asks the user for their filename and I don't know how to capture this with my implementation. 


[...]


That is non-trivial.

Read the "svn" documentation. You might be able to pass in the
required information by other means, maybe an option, maybe
an envvar, maybe via a configuration file.

Otherwise, you must monitor what it written to the subprocess'
"stdout" and "stderr", recognized the interaction request
perform the interaction with the user and send the result
to the subprocess' stdin.


Thanks, I think this solution will work.

import subprocess
import getpass

user = "XXX578"
root="https://trac.nci.org.au/svn/cable";
repo_name = "CMIP6-MOSRS"

pswd = "'" + getpass.getpass('Password:') + "'"
cmd = "svn checkout %s/branches/Users/%s/%s --password %s" %\
  (root, user, repo_name, pswd)


I'm not questioning whether or not that will work, but I will
point out that the password will be exposed to anyone doing a
process listing when that process is running (e.g., "ps -ef"
on a posix-like system).  This may or may not be an issue for
your use case.


error = subprocess.call(cmd, shell=True)
if error is 1:
 raise("Error checking out repo")



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


Re: Not Defined error in basic code

2019-03-14 Thread Dan Sommers

On 3/14/19 9:05 AM, Jack Dangler wrote:

Just getting started with tutorials and such, and don't understand this -



class weapon:
      weaponId
      manufacturerName


The previous two lines attempt to refer to existing names,
but neither name exists.


      def printWeaponInfo(self):
      infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
self.manufacturerName >       return infoString



import class_weapon

MyWeapon=weapon()
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

executing 'python3 weaponTrack.py' results in this bailing on the first
element in the class with "not defined". I've been staring at templates
of this exact structure for about an hour trying to figure out why this
isn't running at all. Is it simply because it isn't all in one file?
Thanks for any guidance. Really appreciate the help.


I'm curious about the tutorial you're following.  Depending
on how faithfully you're following along, it seems to
violate some Python conventions, such as CamelCasing class
names and snake_casing (or lowercasing) other names.

Also, when you encounter an error, please copy and paste
the traceback instead of saying "this bailed" so that we
have something to go on rather that having to study your
example from top to bottom to try to figure out what's
wrong.
--
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: Creating GUI Applications with wxPython

2019-02-27 Thread Dan Sommers

On 2/27/19 9:37 AM, Dave wrote:


I have two Python 3 (3.6) apps that will get the full GUI treatment very
soon.  I'm in the process of choosing a GUI, and that may be where
you/your book can help.  Seems this is not a trivial effort (wishing
that Python was like VB6 from the 90's).

Anyway, here is what I am looking for - hopefully it helps guide you.




   A network widget may also be good.


What is a "network widget" in this context?  Application
users don't usually interact with "the network" directly,
and networks are usually on the opposite end of applications
from the GUI.  What would your hypothetical network widget
do?  What cross-platform differences would it abstract/hide?
What do your applications do to/with/against "the network"?
--
https://mail.python.org/mailman/listinfo/python-list


Re: revisiting the "What am I running on?" question

2019-02-17 Thread Dan Sommers

On 2/17/19 8:46 PM, songbird wrote:


   simply put.  if i'm running on a computer and i
don't easily know why kind of computer how can i
answer this in a quick way without getting overly
complicated that also will cover most of the easy
cases?

   i came up with this:

   comments?  additions?  clarifications?


[...]


(env) me@ant(39)~/src/salsa/bits/sysprobe$ python3 sysprobe.py
Temp directory : -->/tmp<--

Result : -->/tmp<--

posix


Python 3.7.2 (default, Jan 10 2019, 23:51:51)
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.platform
'linux'

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


Re: Why float('Nan') == float('Nan') is False

2019-02-13 Thread Dan Sommers

On 2/13/19 1:53 PM, Grant Edwards wrote:


Floating point is sort of the quantum mechanics of computer science.
At first glance, it seems sort of weird.  But after you work with it a
while, it gets even worse.


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


Re: Why float('Nan') == float('Nan') is False

2019-02-13 Thread Dan Sommers

On 2/13/19 7:21 AM, ast wrote:

Hello

  >>> float('Nan') == float('Nan')
False

Why ?

Because the IEEE-754 Standard demands it, and people smarter
than I worked on the IEEE-754 Standard.

 is a quick starting
point for a deeper dive.
--
https://mail.python.org/mailman/listinfo/python-list


Re: exit 2 levels of if/else and execute common code

2019-02-11 Thread Dan Sommers

On 2/11/19 9:25 AM, Neal Becker wrote:

I have code with structure:
```
if cond1:
   [some code]
   if cond2: #where cond2 depends on the above [some code]
 [ more code]

   else:
 [ do xxyy ]
else:
   [ do the same xxyy as above ]
```

So what's the best style to handle this?  As coded, it violates DRY.
Try/except could be used with a custom exception, but that seems a bit heavy
handed.  Suggestions?


Put some code, cond2, more code, and xxyy into separate
functions, and leave it the way it is.  We all have our own
levels of acceptable inelegance, and this one fits well
within mine.  Optionally, instead of separate calls to xxyy,
set a flag and  only call xxyy at the end if the flag is set.

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


Re: Switch function

2019-02-03 Thread Dan Sommers

On 2/3/19 9:03 PM, Avi Gross wrote:

> The example I show above could in many cases be done as you describe
> but what are you gaining?
>
> I mean if I subtract the integer representation of a keyboard
> alphabetic letter (ASCII for the example) from letter 'a' or 'A' then
> A maps to 0 and B maps to 1 and ...  Z maps to 26. So, you could make
> a list of 26 functions (some may be absent) and your entire switch
> statement looks like:
>
>funcs=[zeroeth,first,other,...,last] # list of function handles
>var=input("Enter a Command: ")
>if ord('A') <= ord(var[0]) <= ord('Z'):
>result = funcs[ord(var[0]) - ord('A')]()
>
> Is that short enough?

It's not a matter of shortness.  Your code encapsulates the idea of
dispatching to a 0-airity function based on an input.  In Python,
though, I'd still use a dictionary, which would be more flexible (what
if I start to use digits or other characters as commands?)  and less
error prone.

> Your comment about object polymorphism is interesting. I am picturing
> how each choice somehow delivers an object that automatically is set
> up to do the right thing.

Disclaimer:  I speak OO as a second language, and only when there is an
obvious and compelling advantage over some other paradigm.  That said:

If the mapping from input to function is more complex than A -> func1, B
-> func2, etc., then a factory function that builds an object with an
execute method is a good way of isolating the mapping and keeping the
main code clean and clear.

> Again, see how can you write a COMPLICATED try command that captures
> many things including real errors?

Don't do that.  Why are you writing COMPLICATED try commands?

Did you have a Python question?  ;-)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Switch function

2019-02-03 Thread Dan Sommers

On 2/3/19 5:40 PM, Avi Gross wrote:


Bottom line, does anyone bother using anything like this? It is actually a
bunch of hidden IF statements matched in order but may meet many needs.


I sure don't.  In the rare case that I might use a switch
statement in another language, I just use a series of elifs,
or maybe a dictionary I build once and use over and over (a
habit left over from the old days of extremely constrained
systems, when medium-to-large lookup tables and simple logic
was better than many kinds of runtime computation).

If it gets unweidly, I think harder about how to isolate the
"switching" from the results, and usually end up with a
collection of smaller functions to do the work, or high order
functions, or maybe some sort of object oriented polymorphism
as a last resort.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Instance of 'dict' has no 'replace' member

2019-02-01 Thread Dan Sommers

On 2/1/19 7:15 AM, sinless...@gmail.com wrote:

Hello guys can you help me to solve problem when i compile proram got error like this 
"Instance of 'dict' has no 'replace' member[no member](67;14)".


Python dicts don't have a replace method.

It looks like you're trying to replace strings inside particular
dict entries; perhaps you meant something like this:

querystring["key"] =
querystring["key"].replace("placeholder", "value")

HTH,
Dan
--
https://mail.python.org/mailman/listinfo/python-list


Re: Exercize to understand from three numbers which is more high

2019-01-29 Thread Dan Sommers

On 1/29/19 9:27 AM, Jack Dangler wrote:

wow. Seems like a lot going on. You have 3 ints and need to determine 
the max? Doesn't this work?


N1, N2, N3

if N1>N2
   if N1>N3
     MaxNum = N1
elif N2>N3
   MaxNum = N2
elif N1

No.  Assuing that you meant to include colons where I think
you did, what if (N1, N2, N3) == (5, 4, 6)?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Are all items in list the same?

2019-01-08 Thread Dan Sommers

On 1/8/19 2:31 PM, Tobiah wrote:> On 1/8/19 9:20 AM, Alister wrote:
>> On Tue, 08 Jan 2019 17:15:17 +, Alister wrote:
>>
>>> On Tue, 08 Jan 2019 16:48:58 +0200, Serhiy Storchaka wrote:
>>>
 08.01.19 11:07, Peter Otten пише:
> Bob van der Poel wrote:
>
>> I need to see if all the items in my list are the same. I was using
>> set()
>> for this, but that doesn't work if items are themselves lists. So,
>> assuming that a is a list of some things, the best I've been able to
>> come up with it:
>>
>>   if a.count( targ ) == len(a):
>>
>> I'm somewhat afraid that this won't scale all that well. Am I 
missing

>> something?
>
> a[1:] == a[:-1]
>
> :)
>
>
 Very clever! It is definitely the shortest solution.
>>>
>>> would that still not return true if the list was a palindrome?
>> ignore me, just tried & ok
>>
>
> You were right the first time.  The above comparison should have been
>
>  a == a[::-1]
>
> A palindrome will pass.

Let's find out (vertical space added for clarity):

Python 3.7.2 (default, Dec 29 2018, 21:15:15)
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> a = [1, 1, 1, 1, 1]
>>> a[1:] == a[:-1]
True
>>> a == a[::-1]
True

>>> a = [1, 2, 3, 4, 3, 2, 1]
>>> a[1:] == a[:-1]
False
>>> a == a[::-1]
True

Looks like Peter's pretty clever after all.  :-)

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


Re: ValueError vs IndexError, unpacking arguments with string.split

2018-11-30 Thread Dan Sommers

On 11/30/18 12:00 PM, Morten W. Petersen wrote:

> I guess syntax could be added, so that
>
> a, b, @c = some sequence
>
> would initialize a and b, and leave anything remaining in c.  We could
> then call this @ syntax "teh snek".

Close.  ;-)  Try this:

a, b, *c = [4, 5, 6, 7]
--
https://mail.python.org/mailman/listinfo/python-list


Re: ValueError vs IndexError, unpacking arguments with string.split

2018-11-30 Thread Dan Sommers

On 11/30/18 10:57 AM, Morten W. Petersen wrote:
> On Fri, Nov 30, 2018 at 4:25 PM Dan Sommers 
<2qdxy4rzwzuui...@potatochowder.com> wrote:


>> Python validates that the right hand side contains exactly the right
>> number of elements before beginning to unpack, presumably to ensure a
>> successful operation before failing part way through.

> Hm.  Well I like the simplicity and abstraction of Python, it makes it
> a very productive language.

Agreed, and hold that thought.

> But since you mention it, why is it necessary to ensure a successful
> operation?
>
> Is it so that when
>
> a,b,c = [1,2]
>
> fails, none of the variables a,b,c have been assigned to, and because
> of that, one avoids "rolling back" any assignment that would have been
> done without checking the right-hand argument first ?

That's what I was getting at, but apparently failed to express clearly.

Yes, there are use cases for a short iterator just not assigning the
rest of the variables, and for a long iterator using only what it needs,
but also explicit is better than implicit, and errors should never pass
silently, and other bits of wisdom learned from horrible debugging
experiences.
--
https://mail.python.org/mailman/listinfo/python-list


Re: ValueError vs IndexError, unpacking arguments with string.split

2018-11-30 Thread Dan Sommers

On 11/30/18 7:35 AM, Morten W. Petersen wrote:


... but isn't it logical that the
string is parsed and split, and then later the unpacking operation fails
with an IndexError?


With slightly simpler code and the full text of the exception,
the details becomes more apparent:

>>> x, y = [4]
Traceback (most recent call last):
  File "", line 1, in 
ValueError: not enough values to unpack (expected 2, got 1)

Python validates that the right hand side contains exactly the
right number of elements before beginning to unpack, presumably
to ensure a successful operation before failing part way through.

Effectively, Python is saying that you can't unpack a one-element
array into two pieces *before* it gets to the indexing logic.

HTH,
Dan
--
https://mail.python.org/mailman/listinfo/python-list


Re: Odd truth result with in and ==

2018-11-21 Thread Dan Sommers

On 11/21/18 7:09 PM, Chris Angelico wrote:
> On Thu, Nov 22, 2018 at 11:04 AM Dan Sommers
> <2qdxy4rzwzuui...@potatochowder.com> wrote:
>> But the second one has to do an expensive subset operation.  If I think
>> "is elem in both sets," then I'd never write:
>>
>>   (elem in set1) and (set1 <= set2)
>
> Yes, but that doesn't mean "is elem in both sets". It means "is elem
> in set 1, which needs to be a subset of set 2" ...

Then I was right about not mapping "is elem in both sets" to that
expression!  ;-)

But I did make the leap from Serhiy's original expression to "is elem in
both sets," which *may* mean that Serhiy's original expression is
confusing, but it's probably just further evidence that I'm not actually
as sharp as I think I am.

> ... I'm not sure where that would come up though.

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


Re: Odd truth result with in and ==

2018-11-21 Thread Dan Sommers

On 11/21/18 6:45 PM, Ian Kelly wrote:
> On Wed, Nov 21, 2018 at 2:53 PM Serhiy Storchaka 
 wrote:

>>
>> 21.11.18 22:17, Cameron Simpson пише:
>>> Can someone show me a real world, or failing that - sane looking,
>>> chained comparison using "in"?
>>
>>   s[0] == s[-1] in '\'"'
>>
>> Tests that string s starts and ends with a single or double quote.
>
> Clever, but too esoteric to use in practice. I'd rather make the
> chaining explicit.
>
>> It can be also used with sets:
>>
>>   elem in set1 <= set2
>
> I like this one better.

LOL:  I had exactly the opposite reaction.

The first one expresses exactly the idea that the first and last
elements of the string match and are quotation characters, as if I'd
written:

(s[0] == s[-1]) and (s[0] in '\'"')

(Arguably too esoteric to be used in practice, *unless* profiling
indicates a necessary (before) and noticable (after) speed improvement.)

But the second one has to do an expensive subset operation.  If I think
"is elem in both sets," then I'd never write:

(elem in set1) and (set1 <= set2)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Question about implementing immutability

2018-11-21 Thread Dan Sommers

On 11/21/18 11:45 AM, Iwo Herka wrote:

Hello,

Let's say I want to implement immutability for user-defined class.
More precisely, a class that can be modified only in its (or its
super-class') __init__ method. My initial idea was to do it the
following fashion:

 def __setattr__(self, *args, **kwargs):
 if sys._getframe(1).f_code.co_name == '__init__':
 return super().__setattr__(*args, **kwargs)
 raise AttributeError()

What do you think of this approach? Is there a better one?
Thanks.

That depends on your definition of immutability.

If an instance of your class contains a list, and you change one
of the elements of that list, then the instance's __setattr__
never comes into play:

x = AllegedlyImmutableObject(list('a', 'b', 'c'))
x.some_list[4] = 0
--
https://mail.python.org/mailman/listinfo/python-list


Re: on the prng behind random.random()

2018-11-19 Thread Dan Sommers

On 11/19/18 6:49 PM, Robert Girault wrote:

> I think I disagree with your take here.  With mt19937, given ANY seed,
> I can eventually predict all the sequence without having to query the
> oracle any further.

Even if that's true, and I use mt19937 inside my program, you don't
[usually|necessarily] have access to the raw output from it.

> If you're just writing a toy software, even K&R PRNG works just fine.
> If you're writing a weather simulation, I suppose you need real
> random-like properties and still need your generator to be reproducible.
> If you're using random Quicksort, you do need unpredictability and
> reproducibility.  If you're writing a crypto application, then you need
> something way stronger.  We need all of them ...

Agreed.  Mostly.  IIRC, though, your question was about *replacing*
mt19937, not adding a new RNG.

Please use the right tool for the job at hand.

> ... But mt19937 is now useful only in toy software.

It has "real random-like" properties (for certain definitions of "real"
and "random-like") and it's reproducible.  Therefore, it's good for
weather simulations, too.
--
https://mail.python.org/mailman/listinfo/python-list


Re: bottledaemon stop/start doesn't work if killed elsewhere

2018-11-18 Thread Dan Sommers

On 11/18/18 1:21 PM, MRAB wrote:> On 2018-11-18 17:50, Adam Funk wrote:
>> Hi,
>>
>> I'm using bottledaemon to run a little REST service on a Pi that takes
>> input from other machines on the LAN and stores stuff in a database.
>> I have a cron job to call 'stop' and 'start' on it daily, just in case
>> of problems.
>>
>> Occasionally the oom-killer runs overnight and kills the process using
>> bottledaemon; when this happens (unlike properly stopping the daemon),
>> the pidfile and its lockfile are left on the filesystem, so the 'stop'
>> does nothing and the 'start' gets refusedq because the old pidfile and
>> lockfile are present.  At the moment, I eventually notice something
>> wrong with the output data, ssh into the Pi, and rm the two files then
>> call 'start' on the daemon again.
>>
>> Is there a recommended or good way to handle this situation
>> automatically?
>>
> Could you write a watchdog daemon that checks whether bottledaemon is
> running, and deletes those files if it isn't (or hasn't been for a 
while)?


What if the oom-killer kills the watchdog?

Whatever runs in response to the start command has to be smarter:  if
the pid and lock files exist, then check whether they refer to a
currently running bottledaemon.  If so, then all is well, and refuse to
start a redundant daemon.  If not, then remove the pid and lock files
and start the daemon.
--
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Dan Sommers

On 9/28/18 2:00 PM, Chris Green wrote:

I have a list created by:-

 fld = shlex.split(ln)

It may contain 3, 4 or 5 entries according to data read into ln.
What's the neatest way of setting the fourth and fifth entries to an
empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
clumsy somehow.


Do you care whether there are more than 5 entries in the list?  If not,
then just add two empty strings to the end of the list:

fld.extend(["", ""])

If fld already contained 5 entries, then the extra two empty strings may
or may not affect the subsequent logic.  If possible extra entries
bother you, then truncate the list:

fld = (fld + ["", ""])[:5]

Or add empty strings as long as the list contains 5 entries:

while len(fld) < 5:
fld.append("")

Which one is "better" or "best"?  Your call, depending on what your
criteria are.  I think the last one expresses the intent the most
clearly, but YMMV.
--
https://mail.python.org/mailman/listinfo/python-list


Re: "glob.glob('weirdness')" Any thoughts?

2018-09-11 Thread Dan Sommers

On 9/11/18 6:24 PM, Gilmeh Serda wrote:


I think this is no different than RegEx matching, so the routine really
shouldn't be called glob() but the_regex_variations_opus_five().


Maybe your version should be called crippled_glob.

Globbing has a long history, longer than many of the people who use it.

See .


If there really is a need for other stuff with brackets and what not,
this could go into a subclass of glob, say glob.brain_hemorrhage() or
something, for those so inclined to use it. Or maybe a keyword
parameter finicky=True/False.


In their simplest form, the brackets match alternatives.  For example,
file[1234].text matches file1.text, file2.text, file3.text, and
file4.text, but not file_something_else.text.

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


Re: Help Needed : script weird result.

2018-09-01 Thread Dan Sommers

On 9/1/18 1:11 PM, moha...@gmail.com wrote:

All,

I m trying to run this small script to find the lowest of the given array of 
numbers. The script works fine for various combination of inputs but fails in a 
weird way for a particular set of inputs, can anyone point the mistake in the 
script and the behavior.

Script

x = input ("Enter the numbers separated by space and press ENTER :")


Think about what x is here, and what x.split does.


x = x.split(" ")


Now think about what x is again, and recall that Python is strongly typed.

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


Re: Pylint false positives

2018-08-20 Thread Dan Sommers
On Mon, 20 Aug 2018 22:55:26 +0300, Marko Rauhamaa wrote:

> Dan Sommers :
> 
>> On Mon, 20 Aug 2018 14:39:38 +, Steven D'Aprano wrote:
>>> I have often wished Python had proper namespaces, so I didn't have to
>>> abuse classes as containers in this way :-(
>>> 
>>> (Not that I do this using "inner classes", but I do often want to use
>>> a class as a container for functions, without caring about "self" or
>>> wrapping everything in staticmethod.)
>>
>> Isn't that what modules are for?  (I suspect that I'm missing something,
>> because I also suspect that you knew/know that.)
> 
> What's the syntax for creating an inner module...?

Why does it have to be an inner anything?  An ordinary, top-level,
"outer" module is a perfectly good "container for functions, without
caring about "self.""  The Python "math" module, for example.  As a long
time, non-native speaker of Object Oriented, I must admit that I still
don't understand the obsession with classes (a certain level of
usefulness and arguably appropriateness for some kinds of problems,
sure, but not the obsession).

Dan

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


Re: Pylint false positives

2018-08-20 Thread Dan Sommers
On Mon, 20 Aug 2018 14:39:38 +, Steven D'Aprano wrote:

> If a class' methods don't use self, it probably shouldn't be a class.

Agreed.

> I have often wished Python had proper namespaces, so I didn't have to
> abuse classes as containers in this way :-(
> 
> (Not that I do this using "inner classes", but I do often want to use
> a class as a container for functions, without caring about "self" or
> wrapping everything in staticmethod.)

Isn't that what modules are for?  (I suspect that I'm missing something,
because I also suspect that you knew/know that.)

Dan

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


Re: Pylint false positives

2018-08-17 Thread Dan Sommers
On Fri, 17 Aug 2018 09:46:01 +0200, Frank Millman wrote:

> It is just a slight annoyance (to me) that pylint complains about the
> subclass methods when they are called from the Field class. I don't
> mind adding 10 stub methods to the Field class to keep it happy, but I
> do not get the feeling that it is improving my code. I do grant,
> however, that it may be of benefit to someone reading my code for the
> first time.

What about the next subclass, the one that doesn't exist today?  If you
write it, then you know to create those ten methods.  If I write it,
though, then those ten stubs tell me that I have to override them, and
their docstrings tell me how.  That is a huge improvement in long term
maintainability.

Dan

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


Re: pattern block expression matching

2018-07-21 Thread Dan Sommers
On Sat, 21 Jul 2018 17:37:00 +0100, MRAB wrote:

> On 2018-07-21 15:20, aldi.kr...@gmail.com wrote:
>> Hi,
>> I have a long text, which tells me which files from a database were 
>> downloaded and which ones failed. The pattern is as follows (at the end of 
>> this post). Wrote a tiny program, but still is raw. I want to find term 
>> "ERROR" and go 5 lines above and get the name with suffix XPT, in this first 
>> case DRXIFF_F.XPT, but it changes in other cases to some other name with 
>> suffix XPT. Thanks, Aldi
>> 
>> # reading errors from a file txt
>> import re
>> with open('nohup.out', 'r') as fh:
>>lines = fh.readlines()
>>for line in lines:
>>m1 = re.search("XPT", line)
>>m2 = re.search('ERROR', line)
>>if m1:
>>  print(line)
>>if m2:
>>  print(line)
>> 
> Firstly, you don't need regex for something has simple has checking for 
> the presence of a string.
> 
> Secondly, I think it's 4 lines above, not 5.
> 
> 'enumerate' comes in useful here:
> 
> with open('nohup.out', 'r') as fh:
>  lines = fh.readlines()
>  for i, line in enumerate(lines):
>  if 'ERROR' in line:
>  print(line)
>  print(lines[i - 4])

Where's awk when you need it?

import fileinput
for line in fileinput.fileinput('nohump.out'):
if 'XPT' in line:
line_containing_filename = line
if 'ERROR' in line:
print(line_containing_filename)

I think Aldi's original approach is pretty good.

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


Re: Cult-like behaviour [was Re: Kindness]

2018-07-16 Thread Dan Sommers
On Tue, 17 Jul 2018 08:48:55 +1000, Chris Angelico wrote:

> That said, though, the fact that indexing a byte string yields an int
> instead of a one-byte string is basically unable to be changed now ...

Agreed.

> ... and IMO it'd be better to be consistent with text strings than
> with bytearray ...

Disagreed.  Given an arbitrary byte string, you can't know whether it's
semantically text or semantically an array of bytes.  (Sometimes a good
byte array is just a byte array?)

In the past, I've done plenty of work with "strings" (in the generic
sense) of octets to/from wire-level protocols.  It would have been much
easier had Python *not* tried to pretend they were text, and *not*
rendered some of the bytes as their ASCII equivalent and some of the
bytes as hex escapes (especially in the cases that some of the bytes
happened to be 0x58, 0x78, 0x5c, or in range(0x30, 0x3a)).

> ... I'm not sure how many of the core devs agree that b'spam'[1] ought
> to be b'p' rather than 112, but I'd say they all agree that it's too
> late to change it.

Curmudgeonly C programmer that I am, b'p' *is* 112.  ;-)

Quick:  how long is the byte array that displays as '\xff'?  Too easy?
What about '\0xff' and '0\xff'?

FWIW, Erlang, a language all but designed to read/write wire level
protocols, prints any array of integers less than 256 as a(n ASCII) text
string.  It never *mixes* integers and characters, but often picks the
wrong one.

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


Re: Unicode [was Re: Cult-like behaviour]

2018-07-16 Thread Dan Sommers
On Mon, 16 Jul 2018 10:39:49 +, Steven D'Aprano wrote:

> ... people who think that if ISO-8859-7 was good enough for Jesus ...

It may have been good enough for his disciples, but Jesus spoke Aramaic.

Also, ISO-8859-7 doesn't cover ancient polytonic Greek; it only covers
modern monotonic Greek.

See also the Unicode Greek FAQ (https://www.unicode.org/faq/greek.html).

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


Re: Python gotcha of the day

2018-03-13 Thread Dan Sommers
On Wed, 14 Mar 2018 04:08:30 +, Steven D'Aprano wrote:

> Explain the difference between these two triple-quoted strings:

> But remove the spaces, and two of the quotation marks disappear:
> 
> py> """\""
> '"'

That's (a) a triple quoted string containing a single escaped quote,
followed by (b) a quoted empty string.  The two consecutive strings are
concatenated into a single string.

(Too many years of shell programming to miss this one!)

Dan

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


Re: Which part of the loop is it going through in this class frame?

2018-03-07 Thread Dan Sommers
On Wed, 07 Mar 2018 16:57:51 -0500, C W wrote:

> class Clock(object):
> def __init__(self, time):
> self.time = time
> def print_time(self):
> time = '6:30'
> print(self.time)
> 
> clock = Clock('5:30')
> clock.print_time()
> 5:30
> 
> I set time to 6:30, but it's coming out to 5:30. I guess it's because I
> passed in 5:30, so, it's replaced?

Hint:  in Python, self.time and time are not the same (unless you've
played some sort of weird trick somewhere).

Dan


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


Re: Which part of the loop is it going through in this class frame?

2018-03-07 Thread Dan Sommers
On Wed, 07 Mar 2018 16:57:51 -0500, C W wrote:

> Hello,
> 
> I am new to OOP. I'm a bit confused about the following code.
> 
> class Clock(object):
> def __init__(self, time):
> self.time = time
> def print_time(self):
> time = '6:30'
> print(self.time)
> 
> clock = Clock('5:30')
> clock.print_time()
> 5:30
> 
> I set time to 6:30, but it's coming out to 5:30. I guess it's because I
> passed in 5:30, so, it's replaced?

Here's a hint:  within a method, time and self.time are not the same (at
least not in Python; other languages may (and do) work differently).

Dan

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


Re: Where has the practice of sending screen shots as source code come from?

2018-01-28 Thread Dan Sommers
On Mon, 29 Jan 2018 00:27:07 +, Steven D'Aprano wrote:

> On Mon, 29 Jan 2018 08:55:54 +1100, Tim Delaney wrote:
> 
>> I got back a Word document containing about 10 screenshots where they'd
>> apparently taken a screenshot, moved the horizontal scrollbar one
>> screen, taken another screenshot, etc.
> 
> You're lucky they didn't just take a single screen shot, thinking that 
> you can scroll past the edges to see what is off-screen.

No!  Not another pass by reference vs. pass by value thread!

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


  1   2   3   4   5   >