Re: mapLast, mapFirst, and just general iterator questions

2022-06-14 Thread Cameron Simpson
On 15Jun2022 05:49, Chris Angelico  wrote:
>On Wed, 15 Jun 2022 at 05:45, Roel Schroeven  wrote:
>> Not (necessarily) a main function, but these days the general
>> recommendation seems to be to use the "if __name__ == '__main__':"
>> construct, so that the file can be used as a module as well as as a
>> script. Even for short simple things that can be helpful when doing
>> things like running tests or extracting docstrings.
>
>If it does need to be used as a module as well as a script, sure. But
>(a) not everything does, and (b) even then, you don't need a main()
>function; what you need is the name-is-main check. The main function
>is only necessary when you need to be able to invoke your main entry
>point externally, AND this main entry point doesn't have a better
>name. That's fairly rare in my experience.

While I will lazily not-use-a-function in dev, using a function has the 
benefit of avoiding accidental global variable use, because assignments 
within the function will always make local variables. That is a big plus 
for me all on its own. I've used this practice as far back as Pascal, 
which also let you write outside-a-function code, and consider it a 
great avoider of a common potential bug situation.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: mapLast, mapFirst, and just general iterator questions

2022-06-14 Thread Greg Ewing

On 15/06/22 7:49 am, Chris Angelico wrote:

If it does need to be used as a module as well as a script, sure. But
(a) not everything does, and (b) even then, you don't need a main()


I think this is very much a matter of taste. Personally I find it tidier
to put the top level code in a function, because it ties it together
visually and lets me have locals that are properly local.

If the file is only ever used as a script, I just put an unconditional
call to the main function at the bottom.

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


Re: mapLast, mapFirst, and just general iterator questions

2022-06-14 Thread Chris Angelico
On Wed, 15 Jun 2022 at 05:45, Roel Schroeven  wrote:
>
> Chris Angelico schreef op 14/06/2022 om 20:47:
> > > def main():
> > > for each in (iterEmpty, iter1, iter2, iterMany):
> > > baseIterator = each()
> > > chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
> > > andCapLast = mapLast(chopFirst, lambda x: x.upper())
> > > print(repr(" ".join(andCapLast)))
> >
> > Don't bother with a main() function unless you actually need to be
> > able to use it as a function. Most of the time, it's simplest to just
> > have the code you want, right there in the file. :) Python isn't C or
> > Java, and code doesn't have to get wrapped up in functions in order to
> > exist.
> Not (necessarily) a main function, but these days the general
> recommendation seems to be to use the "if __name__ == '__main__':"
> construct, so that the file can be used as a module as well as as a
> script. Even for short simple things that can be helpful when doing
> things like running tests or extracting docstrings.

If it does need to be used as a module as well as a script, sure. But
(a) not everything does, and (b) even then, you don't need a main()
function; what you need is the name-is-main check. The main function
is only necessary when you need to be able to invoke your main entry
point externally, AND this main entry point doesn't have a better
name. That's fairly rare in my experience.

My recommendation is to write the code you need, and only add
boilerplate when you actually need it. Don't just start every script
with an if-name-is-main block at the bottom just for the sake of doing
it.

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


Re: mapLast, mapFirst, and just general iterator questions

2022-06-14 Thread Roel Schroeven

Chris Angelico schreef op 14/06/2022 om 20:47:

> def main():
> for each in (iterEmpty, iter1, iter2, iterMany):
> baseIterator = each()
> chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
> andCapLast = mapLast(chopFirst, lambda x: x.upper())
> print(repr(" ".join(andCapLast)))

Don't bother with a main() function unless you actually need to be
able to use it as a function. Most of the time, it's simplest to just
have the code you want, right there in the file. :) Python isn't C or
Java, and code doesn't have to get wrapped up in functions in order to
exist.
Not (necessarily) a main function, but these days the general 
recommendation seems to be to use the "if __name__ == '__main__':" 
construct, so that the file can be used as a module as well as as a 
script. Even for short simple things that can be helpful when doing 
things like running tests or extracting docstrings.


--
"This planet has - or rather had - a problem, which was this: most of the
people living on it were unhappy for pretty much of the time. Many solutions
were suggested for this problem, but most of these were largely concerned with
the movement of small green pieces of paper, which was odd because on the whole
it wasn't the small green pieces of paper that were unhappy."
-- Douglas Adams

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


Re: mapLast, mapFirst, and just general iterator questions

2022-06-14 Thread Chris Angelico
On Wed, 15 Jun 2022 at 04:07, Travis Griggs  wrote:
> def mapFirst(stream, transform):
> try:
> first = next(stream)
> except StopIteration:
> return
> yield transform(first)
> yield from stream

Small suggestion: Begin with this:

stream = iter(stream)

That way, you don't need to worry about whether you're given an
iterator or some other iterable (for instance, you can't call next()
on a list, but it would make good sense to be able to use your
function on a list).

(BTW, Python's convention would be to call this "map_first" rather
than "mapFirst". But that's up to you.)

> def mapLast(stream, transform):
> try:
> previous = next(stream)
> except StopIteration:
> return
> for item in stream:
> yield previous
> previous = item
> yield transform(previous)

Hmm. This might be a place to use multiple assignment, but what you
have is probably fine too.

> def main():
> for each in (iterEmpty, iter1, iter2, iterMany):
> baseIterator = each()
> chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
> andCapLast = mapLast(chopFirst, lambda x: x.upper())
> print(repr(" ".join(andCapLast)))

Don't bother with a main() function unless you actually need to be
able to use it as a function. Most of the time, it's simplest to just
have the code you want, right there in the file. :) Python isn't C or
Java, and code doesn't have to get wrapped up in functions in order to
exist.

> Is this idiomatic? Especially my implementations of mapFirst and mapList 
> there in the middle? Or is there some way to pull this off that is more 
> elegant?
>

Broadly so. Even with the comments I've made above, I wouldn't say
there's anything particularly *wrong* with your code. There are, of
course, many ways to do things, and what's "best" depends on what your
code is doing, whether it makes sense in context.

> I've been doing more with iterators and stacking them (probably because I've 
> been playing with Elixir elsewhere), I am generally curious what the 
> performance tradeoffs of heavy use of iterators and yield functions in python 
> is. I know the argument for avoiding big list copies when moving between 
> stages. Is it one of those things where there's also some overhead with them, 
> where for small stuff, you'd just be better list-ifying the first iterator 
> and then working with lists (where, for example, I could do the first/last 
> clamp operation with just indexing operations).
>

That's mostly right, but more importantly: Don't worry about
performance. Worry instead about whether the code is expressing your
intent. If that means using a list instead of an iterator, go for it!
If that means using an iterator instead of a list, go for it! Python
won't judge you. :)

But if you really want to know which one is faster, figure out a
reasonable benchmark, and then start playing around with the timeit
module. Just remember, it's very very easy to spend hours trying to
make the benchmark numbers look better, only to discover that it has
negligible impact on your code's actual performance - or, in some
cases, it's *worse* than before (because the benchmark wasn't truly
representative). So if you want to spend some enjoyable time exploring
different options, go for it! And we'd be happy to help out. Just
don't force yourself to write bad code "because it's faster".

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


mapLast, mapFirst, and just general iterator questions

2022-06-14 Thread Travis Griggs
I want to be able to apply different transformations to the first and last 
elements of an arbitrary sized finite iterator in python3. It's a custom 
iterator so does not have _reversed_. If the first and last elements are the 
same (e.g. size 1), it should apply both transforms to the same element. I'm 
doing this because I have an iterator of time span tuples, and I want to clamp 
the first and last elements, but know any/all of the middle values are 
inherently in range.

A silly example might be a process that given an iterator of strings, chops the 
the outer characters off of the value, and uppercases the final value. For 
example:


def iterEmpty():
return iter([])

def iter1():
yield "howdy"

def iter2():
yield "howdy"
yield "byebye"

def iterMany():
yield "howdy"
yield "hope"
yield "your"
yield "day"
yield "is"
yield "swell"
yield "byebye"

def mapFirst(stream, transform):
try:
first = next(stream)
except StopIteration:
return
yield transform(first)
yield from stream

def mapLast(stream, transform):
try:
previous = next(stream)
except StopIteration:
return
for item in stream:
yield previous
previous = item
yield transform(previous)

def main():
for each in (iterEmpty, iter1, iter2, iterMany):
baseIterator = each()
chopFirst = mapFirst(baseIterator, lambda x: x[1:-1])
andCapLast = mapLast(chopFirst, lambda x: x.upper())
print(repr(" ".join(andCapLast)))


This outputs:

''
'OWD'
'owd BYEBYE'
'owd hope your day is swell BYEBYE'

Is this idiomatic? Especially my implementations of mapFirst and mapList there 
in the middle? Or is there some way to pull this off that is more elegant?

I've been doing more with iterators and stacking them (probably because I've 
been playing with Elixir elsewhere), I am generally curious what the 
performance tradeoffs of heavy use of iterators and yield functions in python 
is. I know the argument for avoiding big list copies when moving between 
stages. Is it one of those things where there's also some overhead with them, 
where for small stuff, you'd just be better list-ifying the first iterator and 
then working with lists (where, for example, I could do the first/last clamp 
operation with just indexing operations).
-- 
https://mail.python.org/mailman/listinfo/python-list


[Python-announce] python-oracledb 1.0.1

2022-06-14 Thread Anthony Tuininga
What is python-oracledb?

python-oracledb is a Python extension module that enables access to Oracle
Database for Python and conforms to the Python database API 2.0
specifications with a number of enhancements. This module is intended to
eventually replace cx_Oracle.

Where do I get it?

https://pypi.org/project/oracledb/1.0.1/

The easiest method to install/upgrade python-oracledb is via pip as in

python -m pip install oracledb --upgrade

What's new?

This release addresses a number of issues reported after the release of
1.0.0.

See the full release notes for all of the details:
https://python-oracledb.readthedocs.io/en/latest/release_notes.html#oracledb-1-0-1-june-2022

Please provide any feedback via GitHub issues: https://github.com/oracle/
python-oracledb/issues or discussions: https://github.com/oracle/python-
oracledb/discussions
___
Python-announce-list mailing list -- python-announce-list@python.org
To unsubscribe send an email to python-announce-list-le...@python.org
https://mail.python.org/mailman3/lists/python-announce-list.python.org/
Member address: arch...@mail-archive.com


Re: fill out bulletins

2022-06-14 Thread Avi Gross via Python-list
I wish this discussion was simplified.
It sounds to me like what is wanted is a way to PRINT a filled-out form using 
some dynamic text that fits over designated slots in the data. It is not that 
different from many other tasks where you overlay some graphics with text.
You need a decent version of the graphics with enough pixels and the right size 
using something as simple as a scanner at higher resolution OR getting someone 
to give you a template in a form you can use.
Using a ruler or other tools, map out the rectangles you are going to be 
printing text in.
Use your own algorithms to take each snippet of text you want to print and do 
wrapping and resizing of the font or whatever it takes to make it fit.
Finally, overlay the text snippets on the image.
Then PRINT it on a decent printer.
From what I hear, you seem not to need the original text anymore, but nothing 
stops your program with storing bits you will use again later and reusing them, 
or having a log of the original text used and so on.

Plan B might be to COPY your image on a ream of paper and then replace the 
paper in the printer so the next print writes on top of it. Create a template 
with just text that when printed will happen to write over the same spots.
As to what tools you can use, there are many to choose from. You asked on a 
Python list so you may want some of the Python Graphics utilities. In R, I 
might use ggplot which lets me set a background layer then draw objects above 
it at various offsets, as one of many things. 
I know many businesses do things like this all the time such as having printers 
loaded with checks to be printed, or inserting an envelope into a slot on the 
printer to have the name and address printedin the right places.

-Original Message-
From: Peter Pearson 
To: python-list@python.org
Sent: Tue, Jun 14, 2022 9:28 am
Subject: Re: fill out bulletins

On Tue, 14 Jun 2022 00:41:07 +0200, jak  wrote:
[snip]
>
> If you are interested in seeing what I called "post office bulletin"
> (English is not my language and I don't know the name, sorry), you can
> find a sample pdf (fillable) but it works badly here:
>
> https://www.guardiacostiera.gov.it/venezia/Documents/Bollettino%20MOD.%20TD123.pdf


Are these "post office bulletins" always PDFs?


-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: fill out bulletins

2022-06-14 Thread Dennis Lee Bieber
On Tue, 14 Jun 2022 00:41:07 +0200, jak  declaimed the
following:

>If you are interested in seeing what I called "post office bulletin"
>(English is not my language and I don't know the name, sorry), you can
>find a sample pdf (fillable) but it works badly here:
>
>https://www.guardiacostiera.gov.it/venezia/Documents/Bollettino%20MOD.%20TD123.pdf

While I can't read Italian... But that looks suspiciously like what I'd
call a Return Receipt (one side to be filled with "your" address for
returning, and the other to be filled/signed by the person accepting the
article)
https://faq.usps.com/s/article/Return-Receipt-The-Basics#Green_Card

Or an archaic Telegram form (NOT the cloud app) -- or even an ARRL
Message form https://www.ncarrl.org/nets/mes_form.html

In either case -- they should be manageable spending some time laying
out the "form" in any decent word processor, and developing the suitable
mail-merge definitions and database. Or even a decent report-writer with a
relational database.


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: fill out bulletins

2022-06-14 Thread jak

Il 14/06/2022 15:28, Peter Pearson ha scritto:

On Tue, 14 Jun 2022 00:41:07 +0200, jak  wrote:
[snip]


If you are interested in seeing what I called "post office bulletin"
(English is not my language and I don't know the name, sorry), you can
find a sample pdf (fillable) but it works badly here:

https://www.guardiacostiera.gov.it/venezia/Documents/Bollettino%20MOD.%20TD123.pdf



Are these "post office bulletins" always PDFs?




Oh no, sadly no. these forms can be found almost exclusively in post
offices in paper form and you would need to fill them in by hand (only
with black or blue ballpoint pen). I struggled to find a pdf version of
them on the web that wasn't pre-compiled. I also tried to get a graphic
file by scanning (or photo) but I couldn't get a decent image, probably
because I don't have precise enough tools. Ask them for a copy of the
document from which they print them, I don't try because I think it's
wasted breath.
--
https://mail.python.org/mailman/listinfo/python-list


Re: fill out bulletins

2022-06-14 Thread Peter Pearson
On Tue, 14 Jun 2022 00:41:07 +0200, jak  wrote:
[snip]
>
> If you are interested in seeing what I called "post office bulletin"
> (English is not my language and I don't know the name, sorry), you can
> find a sample pdf (fillable) but it works badly here:
>
> https://www.guardiacostiera.gov.it/venezia/Documents/Bollettino%20MOD.%20TD123.pdf


Are these "post office bulletins" always PDFs?


-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list