It's been several years since I've needed to write a python script so I'm asking for advice to get me started with a brief script to separate names
and email addresses in one file into two separate files: salutation.txt and emails.txt.
An example of the input file:
Calvin
calvin@example.com
Hobbs
hobbs@some.com
Nancy
nancy@herown.com
Sluggo
sluggo@another.com
Having extracted salutations and addresses I'll write a bash script using
sed and mailx to associate a message file with each name and email address.
It's been several years since I've needed to write a python script so I'm asking for advice to get me started with a brief script to separate names
and email addresses in one file into two separate files: salutation.txt and emails.txt.
An example of the input file:
Calvin
calvin@example.com
Hobbs
hobbs@some.com
Nancy
nancy@herown.com
Sluggo
sluggo@another.com
Having extracted salutations and addresses I'll write a bash script using
sed and mailx to associate a message file with each name and email address.
I'm unsure where to start given my lack of recent experience.
separate names and email addresses
From the look of it:
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
On 2024-01-11 18:08, Rich Shepard via Python-list wrote:I'm
It's been several years since I've needed to write a python script so =
mesasking for advice to get me started with a brief script to separate na=
ingand email addresses in one file into two separate files:=20
salutation.txt and
emails.txt.
An example of the input file:
Calvin
calvin@example.com
Hobbs
hobbs@some.com
Nancy
nancy@herown.com
Sluggo
sluggo@another.com
Having extracted salutations and addresses I'll write a bash script us=
sed and mailx to associate a message file with each name and email=20From the look of it:
address.
I'm unsure where to start given my lack of recent experience.
=20
1. If the line is empty, ignore it.
=20
2. If the line contains "@", it's an email address.
=20
3. Otherwise, it's a name.
=20
4. Don't assume it's going to be "plain text" if the email info is
harvested from external sources (like incoming emails) - you'll end up stumbling over a $BC/$+$N%f!<%6!<(J from somewhere. Process as bytes, or be really careful about which encodings you allow - which for email "names"
is something you can't actually control.
Why not to use bash script for all?
On Thu, 11 Jan 2024, Piergiorgio Sartor via Python-list wrote:
Why not to use bash script for all?
Piergiorgio,
That's certainly a possibility, and may well be better than python
for this
task.
Thank you,
Rich
On 2024-01-11 18:08, Rich Shepard via Python-list wrote:
It's been several years since I've needed to write a python script so I'mFrom the look of it:
asking for advice to get me started with a brief script to separate names
and email addresses in one file into two separate files:
salutation.txt and
emails.txt.
An example of the input file:
Calvin
calvin@example.com
Hobbs
hobbs@some.com
Nancy
nancy@herown.com
Sluggo
sluggo@another.com
Having extracted salutations and addresses I'll write a bash script using
sed and mailx to associate a message file with each name and email
address.
I'm unsure where to start given my lack of recent experience.
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
From which I figured why not something like this:
On 1/11/2024 1:27 PM, MRAB via Python-list wrote:I'm
On 2024-01-11 18:08, Rich Shepard via Python-list wrote:
It's been several years since I've needed to write a python script so =
mesasking for advice to get me started with a brief script to separate na=
ingand email addresses in one file into two separate files:
salutation.txt and
emails.txt.
An example of the input file:
Calvin
calvin@example.com
Hobbs
hobbs@some.com
Nancy
nancy@herown.com
Sluggo
sluggo@another.com
Having extracted salutations and addresses I'll write a bash script us=
sed and mailx to associate a message file with each name and emailFrom the look of it:
address.
I'm unsure where to start given my lack of recent experience.
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
You could think about a single Python script that looks through your
input file and constructs all the message files without ever writing
separate salutation and address files at all. Then you wouldn't need to write the sed and mailx scripts. It shouldn't be much harder than
peeling out the names and addresses into separate files.
If you haven't written any Python for some years, the preferred way to
read and write files is using a "with" statement, like this:
with open('email_file.txt', encoding =3D 'utf-8') as f:
lines =3D f.readlines()
for line in lines:
if not line.strip(): # Skip blank lines
continue
# Do something with this line
You don't need to close the file because when the "with" block ends the
file will be closed for you.
If the encoding is not utf-8 and you know what it will be, use that
encoding instead.
--
https://mail.python.org/mailman/listinfo/python-list
By the way, in an attempt to golf this problem, I discovered this,o I'm
which seems like a parser problem:
This is what Python tells me about its grammar:
with_stmt:
| 'with' '(' ','.with_item+ ','? ')' ':' block
| 'with' ','.with_item+ ':' [TYPE_COMMENT] block
| ASYNC 'with' '(' ','.with_item+ ','? ')' ':' block
| ASYNC 'with' ','.with_item+ ':' [TYPE_COMMENT] block
with_item:
| expression 'as' star_target &(',' | ')' | ':')
| expression
From which I figured why not something like this:
with (open('example.txt', 'r'), open('emails.txt', 'w'), open('salutations.txt', 'w')) as e, m, s:
for line in e:
if line.strip():
(m if '@' in line else s).write(line)
Which, surprise, parsers! But it seems like it's parse is wrong,
because running this I get:
=E2=9D=AF python ./split_emails.py
Traceback (most recent call last):
File "/home/?/doodles/python/./split_emails.py", line 1, in <module>
with (open('example.txt', 'r'), open('emails.txt', 'w'), open('salutations.txt', 'w')) as e, m, s:
TypeError: 'tuple' object does not support the context manager protocol
It seems to me it shouldn't have been parsed as a tuple. The
parenthesis should've been interpreted just as a decoration.
NB. I'm using 3.11.6.
On Thu, Jan 11, 2024 at 10:20=E2=80=AFPM Thomas Passin via Python-list <python-list@python.org> wrote:
On 1/11/2024 1:27 PM, MRAB via Python-list wrote:
On 2024-01-11 18:08, Rich Shepard via Python-list wrote:
It's been several years since I've needed to write a python script s=
namesasking for advice to get me started with a brief script to separate =
usingand email addresses in one file into two separate files:
salutation.txt and
emails.txt.
An example of the input file:
Calvin
calvin@example.com
Hobbs
hobbs@some.com
Nancy
nancy@herown.com
Sluggo
sluggo@another.com
Having extracted salutations and addresses I'll write a bash script =
osed and mailx to associate a message file with each name and emailFrom the look of it:
address.
I'm unsure where to start given my lack of recent experience.
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
You could think about a single Python script that looks through your
input file and constructs all the message files without ever writing separate salutation and address files at all. Then you wouldn't need t=
write the sed and mailx scripts. It shouldn't be much harder than
peeling out the names and addresses into separate files.
If you haven't written any Python for some years, the preferred way to
read and write files is using a "with" statement, like this:
with open('email_file.txt', encoding =3D 'utf-8') as f:
lines =3D f.readlines()
for line in lines:
if not line.strip(): # Skip blank lines
continue
# Do something with this line
You don't need to close the file because when the "with" block ends the file will be closed for you.
If the encoding is not utf-8 and you know what it will be, use that encoding instead.
--
https://mail.python.org/mailman/listinfo/python-list
By the way, in an attempt to golf this problem, I discovered this,A series of comma-separated items will be parsed as a tuple (some people
which seems like a parser problem:
This is what Python tells me about its grammar:
with_stmt:
| 'with' '(' ','.with_item+ ','? ')' ':' block
| 'with' ','.with_item+ ':' [TYPE_COMMENT] block
| ASYNC 'with' '(' ','.with_item+ ','? ')' ':' block
| ASYNC 'with' ','.with_item+ ':' [TYPE_COMMENT] block
with_item:
| expression 'as' star_target &(',' | ')' | ':')
| expression
From which I figured why not something like this:
with (open('example.txt', 'r'), open('emails.txt', 'w'), open('salutations.txt', 'w')) as e, m, s:
for line in e:
if line.strip():
(m if '@' in line else s).write(line)
Which, surprise, parsers! But it seems like it's parse is wrong,
because running this I get:
❯ python ./split_emails.py
Traceback (most recent call last):
File "/home/?/doodles/python/./split_emails.py", line 1, in <module>
with (open('example.txt', 'r'), open('emails.txt', 'w'), open('salutations.txt', 'w')) as e, m, s:
TypeError: 'tuple' object does not support the context manager protocol
It seems to me it shouldn't have been parsed as a tuple. The
parenthesis should've been interpreted just as a decoration.
NB. I'm using 3.11.6.
By the way, in an attempt to golf this problem, I discovered this,
which seems like a parser problem:
On Fri, 12 Jan 2024 at 08:56, Left Right via Python-list <python-list@python.org> wrote:
By the way, in an attempt to golf this problem, I discovered this,
which seems like a parser problem:
When you jump immediately to "this is a bug", all you do is make
yourself look like an idiot. Unsurprisingly, this is NOT a bug, this
is simply that you didn't understand what was going on. The grammar
isn't easy to read, and it's usually better to read the documentation instead.
(Plus, golfing isn't really a goal in Python, and you didn't shorten
the code by much at all. Good job.)
On Thu, 11 Jan 2024, MRAB via Python-list wrote:
From the look of it:
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
On Thu, 11 Jan 2024, MRAB via Python-list wrote:
From the look of it:
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
But is the solution a good one for some purpose? The two output files may
end up being out of sync for all kinds of reasons. One of many "errors"
can happen if multiple lines in a row do not have an "@" or a person's
name does, for example. What if someone supplied more than one email
address with a comma separator? This may not be expected but could cause problems.
Thursday, January 11, 2024 at 10:44, Rich Shepard via Python-list wrote:
Re: Extract lines from file, add to (at least in part)
On Thu, 11 Jan 2024, MRAB via Python-list wrote:
From the look of it:
1. If the line is empty, ignore it.
2. If the line contains "@", it's an email address.
3. Otherwise, it's a name.
If that is it all? a simple Grep would do (and save on the blank line)
--
https://mail.python.org/mailman/listinfo/python-list
x = [...]
for x[i] in x: print(i)
....x = [1, 2, 3]
for x[i] in x: print(i)
surprising for me:
There's no destructuring going on here
On 13/01/24 12:11 am, Left Right wrote:
x =3D [...]
for x[i] in x: print(i)
I suspect you've misremembered something, because this doesn't
do anything surprising for me:
...x =3D [1, 2, 3]
for x[i] in x: print(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined
There's no destructuring going on here, just assignment to a
sequence item.
--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
surprising for me:
Surprise is subjective, it's based on personal experience. Very few
languages allow arbitrary complex expressions in the same place they
allow variable introduction. The fact that "i" is not defined is
irrelevant to this example. Most programmers who haven't memorized
Python grammar by heart, but expect the language to behave similar to
the languages in the same category would be surprised this code is
valid (i.e. can be parsed), whether it results in error or not is of
no consequence.
There's no destructuring going on here
I use the term "destructuring" in the same way Hyperspec uses it.
It's not a Python term. I don't know what you call the same thing in
Python. I'm not sure what you understand from it.
On Sat, Jan 13, 2024 at 12:37=E2=80=AFAM Greg Ewing via Python-list <python-list@python.org> wrote:
On 13/01/24 12:11 am, Left Right wrote:
x =3D [...]
for x[i] in x: print(i)
I suspect you've misremembered something, because this doesn't
do anything surprising for me:
...x =3D [1, 2, 3]
for x[i] in x: print(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined
There's no destructuring going on here, just assignment to a
sequence item.
--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Very few
languages allow arbitrary complex expressions in the same place they
allow variable introduction.
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."
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).
I use the term "destructuring" in the same way Hyperspec uses it.
It's not a Python term. I don't know what you call the same thing in
Python. I'm not sure what you understand from it.
On Sat, 13 Jan 2024 at 13:11, Left Right via Python-list <python-list@python.org> wrote:
Very few
languages allow arbitrary complex expressions in the same place they
allow variable introduction.
What do you mean by this? Most languages I've worked with allow
variables to be initialized with arbitrary expressions, and a lot of languages allow narrowly-scoped variables.
On Thu, 11 Jan 2024, Piergiorgio Sartor via Python-list wrote:
Why not to use bash script for all?
Piergiorgio,
That's certainly a possibility, and may well be better than python for this task.
To people discussing BNF:
The grammar language Python uses is *very* far from BNF. It's more
similar to PEG, but even then it's still quite far. Python's grammar
is just its own thing, which makes it harder to read, if you are
already familiar with other more popular formats.
I've also found bugs in Python parser before, so had this turned out
to be a real issue, this wouldn't have been the first time. There are
plenty of weird corners in Python grammar that allow unexpected
programs to parse (and sometimes even run!), and these are very often connected to assignments, because, in general, assignments in Python
are very elaborate and hard to describe / conceptualize about. The
most popular example I've even seen used in coding interviews (which I
think is a silly gimmick, but that's kind of the whole point of a lot
of these interviews...) is:
x = [...]
for x[i] in x: print(i)
Which is not an assignment by itself, but the "weirdness" results from
the loop syntax sharing definitions with the "destructuring bind"
style of assignment (i.e. where the left-hand side can be an arbitrary complex expression).
I was surprised, for example, to learn that "as" in "with_stmt" isn't
shared with "as" in "except_block" (so, from the grammar perspective,
these are two different keywords), and that asterisk in "except_block"
isn't shared with "star_target" (also weird, since you'd think these
should be the same thing). In general, and by and large, if you look
at Python's grammar there are many "weird" choices that it makes to
describe the language which seem counterintuitive to the programmer
who tries to learn the language from examples (i.e. context-depending
meaning of parenthesis, of asterisk, of period etc.) Having been
exposed to this, you'd start to expect that some of this weirdness
will eventually result in bugs, or at least in unexpected behavior.
----
Anyways. To the OP: I'm sorry to hijack your question. Below is the
complete program:
with (
open('example.txt', 'r') as e,
open('emails.txt', 'w') as m,
open('salutations.txt', 'w') as s,
):
for line in e:
if line.strip():
(m if '@' in line else s).write(line)
it turned out to be not quite the golfing material I was hoping for.
But, perhaps a somewhat interesting aspect of this program you don't
see used a lot in the wild is the parenthesis in the "with" head. So,
it's not a total write-off from the learning perspective. I.e. w/o
looking at the grammar, and had I have this code in a coding interview question, I wouldn't be quite sure whether this code would work or
not: one way to interpret what's going on here is to think that the expression inside parentheses is a tuple, and since tuples aren't
context managers, it wouldn't have worked (or maybe not even parsed as
"as" wouldn't be allowed inside tuple definition since there's no
"universal as-expression" in Python it's hard to tell what the rules
are). But, it turns out there's a form of "with" that has parentheses
for decoration purposes, and that's why it parses and works to the
desired effect.
Since it looks like you are doing this for educational reasons, I
think there's a tiny bit of value to my effort.
Similarly, whilst we could write:
a, b, c = 1, 2, 3
Having worked with a bunch of different grammar languages, the one
used for Python isn't a recognizable BNF derivative.
For example, you may say "functions in Python are
objects", but you cannot put a function definition in the head of the
for loop clause.
What do you mean?
for x in lambda: ...:
...
Perfectly grammatical.
1. You put the lambda definition in the wrong place (it should be in
the left-hand side, or as Python calls it "star_targets", but you put
it into "star_expressions", which would be where the right-hand side
is drawn from).
2. You used what Python calls "lambdadef" in place of what Python
calls "function_def". I.e. lambda definition and function definition
are two different things, at least as far as grammar is considered.
So, you solved a different problem.
Having worked with a bunch of different grammar languages, the one
used for Python isn't a recognizable BNF derivative.
For example, you may say "functions in Python are
objects", but you cannot put a function definition in the head of the
for loop clause.
What do you mean?
for x in lambda: ...:
...
Perfectly grammatical.
1. You put the lambda definition in the wrong place (it should be in
the left-hand side, or as Python calls it "star_targets", but you put
it into "star_expressions", which would be where the right-hand side
is drawn from).
2. You used what Python calls "lambdadef" in place of what Python
calls "function_def". I.e. lambda definition and function definition
are two different things, at least as far as grammar is considered.
So, you solved a different problem.
Python grammar rules prevent function definition from
appearing in left-hand side of the head of the for loop. However, a
variable declaration, which is also a statement, is allowed there.
Second time to ameliorate wording-dispute in this thread! The original
phrase was: "[modified] BNF". Some of us have worked with various forms
and evolutions of BNF since back in the days of COBOL-60 proposals, and
know it when we see it!
Yes it is hard to read - and even harder to learn-from;
In Python, everything is an object. As long as the LHS is a legal-object which makes sense for the situation, it can be used.
What do you mean?
for x in lambda: ...:
...
Perfectly grammatical.
You said function. I made a function. You said "head of a for loop
clause". I put it there. Problem was underspecified.
But if you're trying to tell me that a def statement should be a valid assignment target,
You provided a way to create an anonymous function and that was not enough.
I wonder if you could throw in the new := walrus operator to similarly make
a named lambda function in a similar way.
Python grew and there was regular pressure to add keywords which might break existing programs. So, yes, sometimes, a keyword was re-used in a different context.
How often do you really think anyone out there NEEDS to define a function in the context mentioned?
What do you mean by this? Most languages I've worked with allow
variables to be initialized with arbitrary expressions, and a lot of languages allow narrowly-scoped variables.
The Python term, at least colloquially, is "tuple unpacking."
On Mon, 15 Jan 2024 at 08:15, Left Right <olegsivokon@gmail.com> wrote:
Python grammar rules prevent function definition from
appearing in left-hand side of the head of the for loop. However, a
variable declaration, which is also a statement, is allowed there.
What is a "variable declaration" in Python? Please elaborate.
On Sun, 14 Jan 2024 at 14:43, dn via Python-list <python-list@python.org> wrote:
Similarly, whilst we could write:
a, b, c = 1, 2, 3
I would only do this when it aligns particularly well with the
algorithm being implemented. For example, you could start a Fibonacci evaluator with "a, b = 0, 1". Otherwise, there's not all that much
reason to unpack three constants in this way.
(Though I am much more likely to use multiple initialization to set a
bunch of things to the SAME value, lilke "a = b = c = 0".)
You said function. I made a function. You said "head of a for loop
clause". I put it there. Problem was underspecified.
But if you're trying to tell me that a def statement should be a valid assignment target,
You provided a way to create an anonymous function and that was not =enough.
I wonder if you could throw in the new :=3D walrus operator to =similarly make
a named lambda function in a similar way.
Python grew and there was regular pressure to add keywords which might =break
existing programs. So, yes, sometimes, a keyword was re-used in a =different
context.
How often do you really think anyone out there NEEDS to define a =function in
the context mentioned?
The basic challenge came from my earlier (and blas=C3=A9) repetition of t=he
Python refrain "everything in Python is an object". Which led to:
<<<
For example, you may say "functions in Python are
objects", but you cannot put a function definition in the head of the
for loop clause.
Which is logical - to some degree, and in-isolation.
for def a_function( etc )... in iterable/iterator:
does not make sense. The 'head' (a more generic name, where Python says "target_list", that refines down to 'something which can identify the generated-value'.
So, no, there's an "everything" which (might be) an object but which
cannot be used in that scenario.
That said, does anyone think that something like:
for a_function( etc ) in iterable/iterator:
is acceptable?
- see both Python definition and (full-)quotation.
I've not come-across a language which does allow such - YMMV/mea culpa;
and am struggling to see how it could possibly be useful.
* Looking at the correspondent's email-address (cf 'handle') - and as an unfair stereotype, raises the question of issues related to (English) language-skills - which, arrogantly implies/assumes that native English-speakers are all highly-capable. (?) A negative-interpretation
is to note his apparent intelligence, but wonder if failing to represent others' comments fairly is deliberate, or carelessness. Is there an
irony in behaving/failing in such, whilst attempting to hold Python's structure to some golden-ideal?
From one perspective, python comments have even less meaning than =whitespace
Second time to ameliorate wording-dispute in this thread! The originalforms
phrase was: "[modified] BNF". Some of us have worked with various =
and evolutions of BNF since back in the days of COBOL-60 proposals, =and
know it when we see it!
Yes it is hard to read - and even harder to learn-from;
In Python, everything is an object. As long as the LHS is a =legal-object
which makes sense for the situation, it can be used.
Second time to ameliorate wording-dispute in this thread! The original
phrase was: "[modified] BNF". Some of us have worked with various forms
and evolutions of BNF since back in the days of COBOL-60 proposals, and
know it when we see it!
OK, here are the conceptual differences between what Python grammar
language does and what you'd expect from anything that's based on BNF, modified or not:
Python isn't a context-free language, so the grammar that is used to
describe it doesn't actually describe the language... so, it's a
"pretend grammar" that ignores indentation. BNF is supposed to be
used to describe the language, it's not a "pretend" or "pseudo"
grammar, in a way we have at least two established grammar for
pseudo-code.
BNF and derivatives don't have an inherent mechanism for tiebreaks.
The mechanism is necessary because BNF rules can be tried in any
order. Some grammar languages derived from BNF declare ambiguous
grammars invalid, some allow ambiguity, but say that the longest
prefix wins, and if there's still ambiguity after that, then such
grammar is invalid, some have special constructs to define "priority"
etc. My reading of Python grammar is that it works like PEG, where
rules are tried in the order they are defined. This makes it less expressive, but easier to work with. This is, probably, the most
fundamental difference between the BNF family and the PEG family.
BNF and family languages rarely incorporate elements of Perl-like
regular expression parsing in the language (i.e. things like
lookaheads, lookbehinds etc.) This is more typical of the PEG family.
On top of this, the Python grammar language has a bunch of
"inventions" that are unique to it (I've never seen any other grammar language use '.' in the same way Python uses it). So, there's that
too.
Having worked with a bunch of different grammar languages, the one
used for Python isn't a recognizable BNF derivative. I think the
authors used this as a description in the same way as today a lot of programmers would use the word "IDE" to describe any text editor or
"REST" to describe any kind of server-client protocol over HTTP and so
on. Or, how we'd use "Xerox" to name a copier machine, even if that
company didn't manufacture it, and even if the tech used for copying
is completely different. And that's why I wrote that the grammar is
actually more like PEG, adding that it's neither, but seems to fall
more into that later category.
Yes it is hard to read - and even harder to learn-from;
This wasn't my point. My point is that it's hard to learn languages
that are "one off" in the group languages that all share a similar set
of rules. The difficulty comes from the surprise caused by the unique
use, not because there's something inherently difficult about reading
grammar languages. In fact, however you look at Python's grammar
language, in a sense, it's a lot easier to read than Python itself
because it has significantly fewer rules. Of course, the number of
rules doesn't entirely capture the difficulty, but it's a useful
metric.
In Python, everything is an object. As long as the LHS is a legal-object
which makes sense for the situation, it can be used.
This is a very interesting statement... I don't think you are fully
aware of what it might mean :) Here are just a few questions for you
to ponder:
* What is Python? Is it only Python 3.12? Is Python 3.11 not Python?
How far back do you go to draw the line?
* What makes something an "object"? Is it the ability to dispatch on?
Is it the inheritance from "object" type?
* What elements of the language do you consider part of the language
that can be included in your "all" set. Do types belong in that set?
Do expressions belong in that set? What about comments?
Depending on how you answer these questions, you'd have some further
problems to deal with. For example, historically, Python had plenty
of things that didn't inherit from "object" but acted similar to one.
I believe "module" objects were among the last to transition into
object inheritance lane, which might have happened some time around
Python 3.5. Of course, there are plenty of things that are "in
Python", at least due to its grammar, that are hard to describe as
objects (eg. comments). So, you'd have to make a special subset of
Python language that (eg. excludes comments) to claim that everything
is an object.
Most importantly, however, regardless of what you understand to be an
object, or how you decide to answer any of those questions: what value
does such a claim possibly have? Especially, given the context...
I wonder if you could throw in the new := walrus operator to similarly make
a named lambda function in a similar way.
On Mon, 15 Jan 2024 at 09:40, dn via Python-list <python-list@python.org> wrote:....
The basic challenge came from my earlier (and blas) repetition of the
Python refrain "everything in Python is an object". Which led to:
So, no, there's an "everything" which (might be) an object but which
cannot be used in that scenario.
More accurately, every VALUE in Python is an object. This does not
mean that syntax is an object. Very few languages would say that every
single grammatical element is a value.
Yes, it's sloppy to say "everything" is an object, but it's also
rather nonintuitive to claim that, therefore, syntax elements are all objects. It's like claiming that everything that this dealership sells
is a car (or "everything in this dealership is a car"), and therefore
the dealership's name must itself be a car.
That said, does anyone think that something like:
for a_function( etc ) in iterable/iterator:
is acceptable?
- see both Python definition and (full-)quotation.
I've not come-across a language which does allow such - YMMV/mea culpa;
and am struggling to see how it could possibly be useful.
You could do something close to that:
for a_function(etc)[0] in iterable: ...
because an assignment target can contain an arbitrary expression
followed by the subscript.
* Looking at the correspondent's email-address (cf 'handle') - and as an
unfair stereotype, raises the question of issues related to (English)
language-skills - which, arrogantly implies/assumes that native
English-speakers are all highly-capable. (?) A negative-interpretation
is to note his apparent intelligence, but wonder if failing to represent
others' comments fairly is deliberate, or carelessness. Is there an
irony in behaving/failing in such, whilst attempting to hold Python's
structure to some golden-ideal?
Seems likely.
Here's another witticism I'll often toss at trainees (in many languages,
and especially in UX): just because we can do it, doesn't make it a good idea!
On Mon, 15 Jan 2024 at 12:12, dn via Python-list <python-list@python.org> wrote:
Here's another witticism I'll often toss at trainees (in many languages,
and especially in UX): just because we can do it, doesn't make it a good
idea!
Programming. We were so busy with whether we COULD that we didn't stop
to think if we SHOULD.
I think that's basically my whole life.
On 15/01/24 14:33, Chris Angelico via Python-list wrote:
On Mon, 15 Jan 2024 at 12:12, dn via Python-list <python-list@python.org> wrote:
Here's another witticism I'll often toss at trainees (in many languages, >> and especially in UX): just because we can do it, doesn't make it a good >> idea!
Programming. We were so busy with whether we COULD that we didn't stop
to think if we SHOULD.
I think that's basically my whole life.
Don't be too hard on yourself.
My life-advice has always been "don't do anything I wouldn't do"
- which pretty-much gives you carte blanche.
On Mon, 15 Jan 2024 at 12:42, dn via Python-list <python-list@python.org> wrote:
On 15/01/24 14:33, Chris Angelico via Python-list wrote:
On Mon, 15 Jan 2024 at 12:12, dn via Python-list <python-list@python.org> wrote:
Here's another witticism I'll often toss at trainees (in many languages, >>>> and especially in UX): just because we can do it, doesn't make it a good >>>> idea!
Programming. We were so busy with whether we COULD that we didn't stop
to think if we SHOULD.
I think that's basically my whole life.
Don't be too hard on yourself.
My life-advice has always been "don't do anything I wouldn't do"
- which pretty-much gives you carte blanche.
I would NEVER give that advice to anyone. They would leave the dishes unwashed, and a number of other problems :)
To make this
shorter, Python allows:
for <statement> in ... : ...
Python isn't a context-free language, so the grammar that is used to
describe it doesn't actually describe the language
... so, it's a "pretend grammar" that ignores indentation.
On 15/01/24 1:28 am, Left Right wrote:
Python isn't a context-free language, so the grammar that is used to describe it doesn't actually describe the language
Very few languages have a formal grammar that *fully* describes
the set of strings that constitute valid programs, including all
the rules about things having to be declared, types matching up,
etc. The only one I know of which attempted that is Algol 68,
and it seems to be regarded as a technical success but a practical
failure.
... so, it's a "pretend grammar" that ignores indentation.
Indentation isn't ignored, it appears in the grammar by means of
INDENT and DEDENT lexical tokens.
It's true that the meaning of these tokens is described informally
elsewhere, but that's true of all the lexical features.
Soon after, Wirth simplified
rather than expanded, and developed Pascal.
When I first saw this I was
stunned, then attracted to its simplicity, but then steered-away once realised that it needed 'more' to cope with 'the outside world'.
The grammar *can't* specify everything. If it did, it would have to
have rules for combining individual letters into a NAME and individual characters into a string literal.
On 15/01/24 9:07 pm, Chris Angelico wrote:
The grammar *can't* specify everything. If it did, it would have to
have rules for combining individual letters into a NAME and individual characters into a string literal.
The lexical level of a grammar can be, and often is, described
formally using regular expressions.
Although some might consider that this doesn't contradict
your statement about readability. :-)
On 15/01/24 1:54 pm, dn wrote:
Soon after, Wirth simplified rather than expanded, and developed Pascal.
Before Pascal there was Algol-W, which Wirth invented as a rebellion
against how complicated Algol 68 was becoming.
When I first saw this I was stunned, then attracted to its simplicity,
but then steered-away once realised that it needed 'more' to cope with
'the outside world'.
Pascal was intended as a teaching language, and as such it was lacking
in practicality in a few spots. But it didn't need much tweaking to
make it a very useful language. UCSD Pascal, Turbo Pascal, Delphi, etc. enjoyed a lot of popularity. A variant of UCSD was the main language
for Macintosh application development for a number of years.
Python isn't a context-free language, so the grammar that is used to
describe it doesn't actually describe the language... so, it's a
"pretend grammar" that ignores indentation.
No it doesn't. Here is the definition of a block, it clearly mentions >indentation:
For my use 1) the salutation and email address (always with an '@') are sequential and 2) I'm developing the script to extract both from the same file.
Have you read "https://docs.python.org/3/library/io.html#module-io"?
...
If this explanation is not sufficiently clear I'll re-write it. :-)
No, I hadn't ... but I am reading it now.
On Mon, 29 Jan 2024, Rich Shepard via Python-list wrote:
No, I hadn't ... but I am reading it now.
Perhaps I missed the answer to my question when reading the io module. It explains how to open/write/read files of text and binary data, not passing
a variable's value from one file to a place-keeper in another file.
No, I hadn't ... but I am reading it now.
There are several general solutions that may apply. Some involve reading
in both files into data structures and perhaps linking them together in
some way such as a data.frame or binary tree. You can then process
individual request in memory/
On Fri, 12 Jan 2024, Rich Shepard via Python-list wrote:
For my use 1) the salutation and email address (always with an '@') are
sequential and 2) I'm developing the script to extract both from the same
file.
I've looked at my Python books "Python Crash Course," "Effective Python,"
and "Python Tricks The Book" as well as web pages in my searches without finding the answer to what may be a simple question: how to specify a variable in one file that has its values in another file.
Specifically, how to I designate the salutation holder in the message file and pass it the name value from the name/email address file?
If this explanation is not sufficiently clear I'll re-write it. :-)
On Mon, 29 Jan 2024, Rich Shepard via Python-list wrote:
No, I hadn't ... but I am reading it now.
Perhaps I missed the answer to my question when reading the io module. It explains how to open/write/read files of text and binary data, not passing
a variable's value from one file to a place-keeper in another file.
For my use 1) the salutation and email address (always with an '@') are
sequential and 2) I'm developing the script to extract both from the same
file.
I've looked at my Python books "Python Crash Course," "Effective Python,"
and "Python Tricks The Book" as well as web pages in my searches without finding the answer to what may be a simple question: how to specify a variable in one file that has its values in another file.
Specifically, how to I designate the salutation holder in the message file and pass it the name value from the name/email address file?
If this explanation is not sufficiently clear I'll re-write it. :-)
TIA,
Rich
On Mon, 29 Jan 2024, Rich Shepard via Python-list wrote:
No, I hadn't ... but I am reading it now.
Perhaps I missed the answer to my question when reading the io module. It explains how to open/write/read files of text and binary data, not passing
a variable's value from one file to a place-keeper in another file.
If you aren't going to use one or another existing template system,
perhaps the easiest is to use unique strings in the message file. For example:
Dear __##so-and-so##__:
Please don't write this message off as mere spam.
Respectfully, Rich
Then you just do a replace of the unique string by the salutation. Don't change the original (i.e., template), make the changes to a copy that you will output.
On Mon, 29 Jan 2024, Thomas Passin via Python-list wrote:
If you aren't going to use one or another existing template system,
perhaps the easiest is to use unique strings in the message file. For
example:
Dear __##so-and-so##__:
Please don't write this message off as mere spam.
Respectfully, Rich
Then you just do a replace of the unique string by the salutation. Don't
change the original (i.e., template), make the changes to a copy that you
will output.
My script is not a web application, but an emailer that allows me to contact clients and prospective clients. From the command line on a linux host.
Using the python smtplib and mail modules.
It can be quite frustrating figuring out what someone wants, Grant, especially when they just change it.
It is worse when instead of starting a new thread with an appropriate
subject line, it continues and old one that was also frustrating to understand.
On Mon, 29 Jan 2024, Thomas Passin via Python-list wrote:
If you aren't going to use one or another existing template system,
perhaps the easiest is to use unique strings in the message file. For
example:
Dear __##so-and-so##__:
Please don't write this message off as mere spam.
Respectfully, Rich
Then you just do a replace of the unique string by the salutation. Don't
change the original (i.e., template), make the changes to a copy that you
will output.
My script is not a web application, but an emailer that allows me to
contact
clients and prospective clients. From the command line on a linux host.
Using the python smtplib and mail modules.
Rich
Fine, my toy example will still be applicable. But, you know, you haven't told us enough to give you help. Do you want to replace text from values
in a file? That's been covered. Do you want to send the messages using
those libraries? You haven't said what you don't know how to do. Something else? What is it that you want to do that you don't know how?
But seriously, the OP, AKA Rich, is making clear that he is making a tool
for his own use. It sounds like he wants to maintain a data repository of
his own with some info about his clients and then have the ability to
specify a name and pop up an email directed to them, or something along
those lines.
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:
Fine, my toy example will still be applicable. But, you know, you haven't
told us enough to give you help. Do you want to replace text from values
in a file? That's been covered. Do you want to send the messages using
those libraries? You haven't said what you don't know how to do.
Something
else? What is it that you want to do that you don't know how?
Thomas,
For 30 years I've used a bash script using mailx to send messages to a list of recipients. They have no salutation to personalize each one. Since I
want
to add that personalized salutation I decided to write a python script to replace the bash script.
I have collected 11 docs explaining the smtplib and email modules and providing example scripts to apply them to send multiple individual
messages
with salutations and attachments.
Today I'm going to be reading these. They each recommend using .csv input files for names and addresses. My first search is learning whether I can write a single .csv file such as:If the file contents are going to be people's names and email addresses,
"name1","address1"
"mane2","address2"
which I believe will work; and by inserting at the top of the message block Hi, {yourname}
the name in the .csv file will replace the bracketed place holder
Still much to learn and the batch of downloaded PDF files should educate
me.
Regards,
Rich
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:list
Fine, my toy example will still be applicable. But, you know, you haven't
told us enough to give you help. Do you want to replace text from values
in a file? That's been covered. Do you want to send the messages using
those libraries? You haven't said what you don't know how to do.
Something
else? What is it that you want to do that you don't know how?
Thomas,
For 30 years I've used a bash script using mailx to send messages to a
of recipients. They have no salutation to personalize each one. Since I
want
to add that personalized salutation I decided to write a python script to replace the bash script.
I have collected 11 docs explaining the smtplib and email modules and providing example scripts to apply them to send multiple individual
messages
with salutations and attachments.
Today I'm going to be reading these. They each recommend using .csv input files for names and addresses. My first search is learning whether I can write a single .csv file such as:block
"name1","address1"
"mane2","address2"
which I believe will work; and by inserting at the top of the message
Hi, {yourname}If the file contents are going to be people's names and email addresses,
the name in the .csv file will replace the bracketed place holder
Still much to learn and the batch of downloaded PDF files should educate
me.
Regards,
Rich
Thomas, on some points we may see it differently.
Some formats can be done simply but are maybe better done in somewhat standard ways.
Some of what the OP has is already tables in a database and that can trivially be exported into a CSV file or other formats like your TSV file
and more. They can also import from there. As I mentioned, many spreadsheets and all kinds of statistical programs tend to support some formats making it quite flexible.
Python has all kinds of functionality, such as in the pandas module, to read in a CSV or write it out. And once you have the data structure in memory, al kinds of queries and changes can be made fairly straightforwardly. As one example, Rich has mentioned wanting finer control in selecting who gets some version of the email based on concepts like market segmentation. He already may have info like the STATE (as in Arizona) in his database. He might at some point enlarge his schema so each entry is placed in one or more categories and thus his CSV, once imported, can do the usual tasks of selecting various rows and columns or doing joins or whatever.
Mind you, another architecture could place quite a bit of work completely on the back end and he could send SQL queries to the database from python and get back his results into python which would then make the email messages
and pass them on to other functionality to deliver. This would remove any need for files and just rely on the DB.
There as as usual, too many choices and not necessarily one best answer. Of course if this was a major product that would be heavily used, sure, you could tweak and optimize. As it is, Rich is getting a chance to improve his python skills no matter which way he goes.
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Thomas Passin via Python-list
Sent: Tuesday, January 30, 2024 10:37 PM
To: python-list@python.org
Subject: Re: Extract lines from file, add to new files
On 1/30/2024 12:21 PM, Rich Shepard via Python-list wrote:
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:list
Fine, my toy example will still be applicable. But, you know, you haven't >>> told us enough to give you help. Do you want to replace text from values >>> in a file? That's been covered. Do you want to send the messages using
those libraries? You haven't said what you don't know how to do.
Something
else? What is it that you want to do that you don't know how?
Thomas,
For 30 years I've used a bash script using mailx to send messages to a
of recipients. They have no salutation to personalize each one. Since I
want
to add that personalized salutation I decided to write a python script to
replace the bash script.
I have collected 11 docs explaining the smtplib and email modules and
providing example scripts to apply them to send multiple individual
messages
with salutations and attachments.
If I had a script that's been working for 30 years, I'd probably just
use Python to do the personalizing and let the rest of the bash script
do the rest, like it always has. The Python program would pipe or send
the personalized messages to the rest of the bash program. Something in
that ballpark, anyway.
Today I'm going to be reading these. They each recommend using .csv inputblock
files for names and addresses. My first search is learning whether I can
write a single .csv file such as:
"name1","address1"
"mane2","address2"
which I believe will work; and by inserting at the top of the message
Hi, {yourname}If the file contents are going to be people's names and email addresses,
the name in the .csv file will replace the bracketed place holder
I would just tab separate them and split each line on the tab. Names
aren't going to include tabs so that would be safe. Email addresses
might theoretically include a tab inside a quoted name but that would be extremely obscure and unlikely. No need for CSV, it would just add complexity.
data = f.readlines()
for d in data:
name, addr = line.split('\t') if line.strip() else ('', '')
Still much to learn and the batch of downloaded PDF files should educate
me.
Regards,
Rich
If I had a script that's been working for 30 years, I'd probably just use Python to do the personalizing and let the rest of the bash script do the rest, like it always has. The Python program would pipe or send the personalized messages to the rest of the bash program. Something in that ballpark, anyway.
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:
If I had a script that's been working for 30 years, I'd probably just use
Python to do the personalizing and let the rest of the bash script do the
rest, like it always has. The Python program would pipe or send the
personalized messages to the rest of the bash program. Something in that
ballpark, anyway.
Thomas,
A bash shell script looks easier for me and more promising. Using a while loop (one for the name file the other for the address file), and sed for putting the name at the head of the message replacing a generic placeholder should work with the existing for loop script.
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:placeholder
If I had a script that's been working for 30 years, I'd probably just use
Python to do the personalizing and let the rest of the bash script do the
rest, like it always has. The Python program would pipe or send the
personalized messages to the rest of the bash program. Something in that
ballpark, anyway.
Thomas,
A bash shell script looks easier for me and more promising. Using a while loop (one for the name file the other for the address file), and sed for putting the name at the head of the message replacing a generic
should work with the existing for loop script.
Thomas, on some points we may see it differently.
Some formats can be done simply but are maybe better done in somewhat standard ways.spreadsheets
Some of what the OP has is already tables in a database and that can trivially be exported into a CSV file or other formats like your TSV file
and more. They can also import from there. As I mentioned, many
and all kinds of statistical programs tend to support some formats makingit
quite flexible.read
Python has all kinds of functionality, such as in the pandas module, to
in a CSV or write it out. And once you have the data structure in memory,al
kinds of queries and changes can be made fairly straightforwardly. As one example, Rich has mentioned wanting finer control in selecting who getssome
version of the email based on concepts like market segmentation. Healready
may have info like the STATE (as in Arizona) in his database. He might at some point enlarge his schema so each entry is placed in one or more categories and thus his CSV, once imported, can do the usual tasks of selecting various rows and columns or doing joins or whatever.on
Mind you, another architecture could place quite a bit of work completely
the back end and he could send SQL queries to the database from python and get back his results into python which would then make the email messagesOf
and pass them on to other functionality to deliver. This would remove any need for files and just rely on the DB.
There as as usual, too many choices and not necessarily one best answer.
course if this was a major product that would be heavily used, sure, you could tweak and optimize. As it is, Rich is getting a chance to improvehis
python skills no matter which way he goes.
-----Original Message-----On
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org>
Behalf Of Thomas Passin via Python-listhaven't
Sent: Tuesday, January 30, 2024 10:37 PM
To: python-list@python.org
Subject: Re: Extract lines from file, add to new files
On 1/30/2024 12:21 PM, Rich Shepard via Python-list wrote:
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:
Fine, my toy example will still be applicable. But, you know, you
listtold us enough to give you help. Do you want to replace text from values >>> in a file? That's been covered. Do you want to send the messages using
those libraries? You haven't said what you don't know how to do.
Something
else? What is it that you want to do that you don't know how?
Thomas,
For 30 years I've used a bash script using mailx to send messages to a
of recipients. They have no salutation to personalize each one. Since I
want
to add that personalized salutation I decided to write a python script to
replace the bash script.
I have collected 11 docs explaining the smtplib and email modules and
providing example scripts to apply them to send multiple individual
messages
with salutations and attachments.
If I had a script that's been working for 30 years, I'd probably just
use Python to do the personalizing and let the rest of the bash script
do the rest, like it always has. The Python program would pipe or send
the personalized messages to the rest of the bash program. Something in
that ballpark, anyway.
Today I'm going to be reading these. They each recommend using .csv inputblock
files for names and addresses. My first search is learning whether I can
write a single .csv file such as:
"name1","address1"
"mane2","address2"
which I believe will work; and by inserting at the top of the message
Hi, {yourname}If the file contents are going to be people's names and email addresses,
the name in the .csv file will replace the bracketed place holder
I would just tab separate them and split each line on the tab. Names
aren't going to include tabs so that would be safe. Email addresses
might theoretically include a tab inside a quoted name but that would be extremely obscure and unlikely. No need for CSV, it would just add complexity.
data = f.readlines()
for d in data:
name, addr = line.split('\t') if line.strip() else ('', '')
Still much to learn and the batch of downloaded PDF files should educate
me.
Regards,
Rich
Thomas,
I have been thinking about the concept of being stingy with information as this is a fairly common occurrence when people ask for help. They often ask for what they think they want while people like us keep asking why they want that and perhaps offer guidance on how to get closer to what they NEED or a better way.
In retrospect, Rich did give all the info he thought he needed. It boiled down to saying that he wants to distribute data into two files in such a way that finding an item in file A then lets him find the corresponding item in file B. He was not worried about how to make the files or what to do with
the info afterward. He had those covered and was missing what he considered
a central piece. And, it seems he programs in multiple languages and environments as needed and is not exactly a newbie. He just wanted a way to implement his overall design.
We threw many solutions and ideas at him but some of us (like me) also got frustrated as some ideas were not received due to one objection or another that had not been mentioned earlier when it was not seen as important.
I particularly notice a disconnect some of us had. Was this supposed to be a search that read only as much as needed to find something and stopped reading, or a sort of filter that returned zero or more matches and went to the end, or perhaps something that read entire files and swallowed them into data structures in memory and then searched and found corresponding entries, or maybe something else?
All the above approaches could work but some designs not so much. For example, some files are too large. We, as programmers, often consciously or unconsciously look at many factors to try to zoom in on what approaches me might use. To be given minimal amounts of info can be frustrating. We worry about making a silly design. But the OP may want something minimal and not worry as long as it is fairly easy to program and works.
We could have suggested something very simple like:
Open both files A and B
In a loop get a line from each. If the line from A is a match, do something with the current line from B.
If you are getting only one, exit the loop.
Or, if willing, we could have suggested any other file format, such as a
CSV, in which the algorithm is similar but different as in:
Open file A
Read a line in a loop
Split it in parts
If the party of the first part matches something, use the party of the
second part
Or, of course, suggest they read the entire file, into a list of lines or a data.frame and use some tools that search all of it and produce results.
I find I personally now often lean toward the latter approach but ages ago when memory and CPU were considerations and maybe garbage collection was not automatic, ...
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Thomas Passin via Python-list
Sent: Wednesday, January 31, 2024 7:25 AM
To: python-list@python.org
Subject: Re: Extract lines from file, add to new files
On 1/30/2024 11:25 PM, avi.e.gross@gmail.com wrote:
Thomas, on some points we may see it differently.
I'm mostly going by what the OP originally asked for back on Jan 11.
He's been too stingy with information since then to be worth spending
much time on, IMHO.
Some formats can be done simply but are maybe better done in somewhatspreadsheets
standard ways.
Some of what the OP has is already tables in a database and that can
trivially be exported into a CSV file or other formats like your TSV file
and more. They can also import from there. As I mentioned, many
and all kinds of statistical programs tend to support some formats makingit
quite flexible.read
Python has all kinds of functionality, such as in the pandas module, to
in a CSV or write it out. And once you have the data structure in memory,al
kinds of queries and changes can be made fairly straightforwardly. As onesome
example, Rich has mentioned wanting finer control in selecting who gets
version of the email based on concepts like market segmentation. Healready
may have info like the STATE (as in Arizona) in his database. He might aton
some point enlarge his schema so each entry is placed in one or more
categories and thus his CSV, once imported, can do the usual tasks of
selecting various rows and columns or doing joins or whatever.
Mind you, another architecture could place quite a bit of work completely
the back end and he could send SQL queries to the database from python and >> get back his results into python which would then make the email messagesOf
and pass them on to other functionality to deliver. This would remove any
need for files and just rely on the DB.
There as as usual, too many choices and not necessarily one best answer.
course if this was a major product that would be heavily used, sure, youhis
could tweak and optimize. As it is, Rich is getting a chance to improve
python skills no matter which way he goes.On
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org>
Behalf Of Thomas Passin via Python-listhaven't
Sent: Tuesday, January 30, 2024 10:37 PM
To: python-list@python.org
Subject: Re: Extract lines from file, add to new files
On 1/30/2024 12:21 PM, Rich Shepard via Python-list wrote:
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:
Fine, my toy example will still be applicable. But, you know, you
listtold us enough to give you help. Do you want to replace text from values >>>> in a file? That's been covered. Do you want to send the messages using >>>> those libraries? You haven't said what you don't know how to do.
Something
else? What is it that you want to do that you don't know how?
Thomas,
For 30 years I've used a bash script using mailx to send messages to a
of recipients. They have no salutation to personalize each one. Since I
want
to add that personalized salutation I decided to write a python script to >>> replace the bash script.
I have collected 11 docs explaining the smtplib and email modules and
providing example scripts to apply them to send multiple individual
messages
with salutations and attachments.
If I had a script that's been working for 30 years, I'd probably just
use Python to do the personalizing and let the rest of the bash script
do the rest, like it always has. The Python program would pipe or send
the personalized messages to the rest of the bash program. Something in
that ballpark, anyway.
Today I'm going to be reading these. They each recommend using .csv input >>> files for names and addresses. My first search is learning whether I can >>> write a single .csv file such as:block
"name1","address1"
"mane2","address2"
which I believe will work; and by inserting at the top of the message
Hi, {yourname}If the file contents are going to be people's names and email addresses,
the name in the .csv file will replace the bracketed place holder
I would just tab separate them and split each line on the tab. Names
aren't going to include tabs so that would be safe. Email addresses
might theoretically include a tab inside a quoted name but that would be
extremely obscure and unlikely. No need for CSV, it would just add
complexity.
data = f.readlines()
for d in data:
name, addr = line.split('\t') if line.strip() else ('', '')
Still much to learn and the batch of downloaded PDF files should educate >>> me.
Regards,
Rich
In my view this whole thread became murky and complicated because the O=P=20
did not write down the requirements for the program.=C2=A0 Requirements=are=20
needed to communicate with other people.=C2=A0 An individual may not ne=ed to=20
actually write down the requirements - depending on their complexity -=20e=20
but they always exist even if only vaguely in a person's mind.=C2=A0 Th=
requirements may include what tools or languages the person wants to us=e=20
and why.=20
=20
If you are asking for help, you need to communicate the requirements to=
the people you are asking for help from.=20
=20
The OP may have thought the original post(s) contained enough of the=20 requirements but as we know by now, they didn't.
=20
The person asking for help may not realize they don't know enough to=20
write down all the requirements; an effort to do so may bring that lack=
to visibility.=20
=20
Mailing lists like these have a drawback that it's hard to impossible=20
for someone not involved in a thread to learn anything general from it.=
We can write over and over again to please state clearly what you want=20ple=20
to do and where the sticking points are, but newcomers post new=20
questions without ever reading these pleas.=C2=A0 Then good-hearted peo=
who want to be helpful end up spending a lot of time trying to guess=20ution=20
what is actually being asked for, and maybe never find out with enough=20 clarity.=C2=A0 Others take a guess and then spend time working up a sol=
that may or may not be on target.
=20
So please! before posting a request for help, write down the=20
requirements as best you can figure them out, and then make sure that=20
they are expressed such that the readers can understand.
Thomas,ask
I have been thinking about the concept of being stingy with information as this is a fairly common occurrence when people ask for help. They often
for what they think they want while people like us keep asking why theywant
that and perhaps offer guidance on how to get closer to what they NEED ora
better way.way
In retrospect, Rich did give all the info he thought he needed. It boiled down to saying that he wants to distribute data into two files in such a
that finding an item in file A then lets him find the corresponding itemin
file B. He was not worried about how to make the files or what to do withconsidered
the info afterward. He had those covered and was missing what he
a central piece. And, it seems he programs in multiple languages and environments as needed and is not exactly a newbie. He just wanted a wayto
implement his overall design.a
We threw many solutions and ideas at him but some of us (like me) also got frustrated as some ideas were not received due to one objection or another that had not been mentioned earlier when it was not seen as important.
I particularly notice a disconnect some of us had. Was this supposed to be
search that read only as much as needed to find something and stopped reading, or a sort of filter that returned zero or more matches and wentto
the end, or perhaps something that read entire files and swallowed theminto
data structures in memory and then searched and found correspondingentries,
or maybe something else?or
All the above approaches could work but some designs not so much. For example, some files are too large. We, as programmers, often consciously
unconsciously look at many factors to try to zoom in on what approaches me might use. To be given minimal amounts of info can be frustrating. Weworry
about making a silly design. But the OP may want something minimal and not worry as long as it is fairly easy to program and works.something
We could have suggested something very simple like:
Open both files A and B
In a loop get a line from each. If the line from A is a match, do
with the current line from B.a
If you are getting only one, exit the loop.
Or, if willing, we could have suggested any other file format, such as a
CSV, in which the algorithm is similar but different as in:
Open file A
Read a line in a loop
Split it in parts
If the party of the first part matches something, use the party of the
second part
Or, of course, suggest they read the entire file, into a list of lines or
data.frame and use some tools that search all of it and produce results.not
I find I personally now often lean toward the latter approach but ages ago when memory and CPU were considerations and maybe garbage collection was
automatic, ...
-----Original Message-----On
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org>
Behalf Of Thomas Passin via Python-listand
Sent: Wednesday, January 31, 2024 7:25 AM
To: python-list@python.org
Subject: Re: Extract lines from file, add to new files
On 1/30/2024 11:25 PM, avi.e.gross@gmail.com wrote:
Thomas, on some points we may see it differently.
I'm mostly going by what the OP originally asked for back on Jan 11.
He's been too stingy with information since then to be worth spending
much time on, IMHO.
Some formats can be done simply but are maybe better done in somewhatspreadsheets
standard ways.
Some of what the OP has is already tables in a database and that can
trivially be exported into a CSV file or other formats like your TSV file
and more. They can also import from there. As I mentioned, many
and all kinds of statistical programs tend to support some formats makingit
quite flexible.read
Python has all kinds of functionality, such as in the pandas module, to
in a CSV or write it out. And once you have the data structure in memory,al
kinds of queries and changes can be made fairly straightforwardly. As onesome
example, Rich has mentioned wanting finer control in selecting who gets
version of the email based on concepts like market segmentation. Healready
may have info like the STATE (as in Arizona) in his database. He might aton
some point enlarge his schema so each entry is placed in one or more
categories and thus his CSV, once imported, can do the usual tasks of
selecting various rows and columns or doing joins or whatever.
Mind you, another architecture could place quite a bit of work completely
the back end and he could send SQL queries to the database from python
valuesget back his results into python which would then make the email messagesOf
and pass them on to other functionality to deliver. This would remove any
need for files and just rely on the DB.
There as as usual, too many choices and not necessarily one best answer.
course if this was a major product that would be heavily used, sure, youhis
could tweak and optimize. As it is, Rich is getting a chance to improve
python skills no matter which way he goes.On
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org>
Behalf Of Thomas Passin via Python-listhaven't
Sent: Tuesday, January 30, 2024 10:37 PM
To: python-list@python.org
Subject: Re: Extract lines from file, add to new files
On 1/30/2024 12:21 PM, Rich Shepard via Python-list wrote:
On Tue, 30 Jan 2024, Thomas Passin via Python-list wrote:
Fine, my toy example will still be applicable. But, you know, you
told us enough to give you help. Do you want to replace text from
tolistin a file? That's been covered. Do you want to send the messages using >>>> those libraries? You haven't said what you don't know how to do.
Something
else? What is it that you want to do that you don't know how?
Thomas,
For 30 years I've used a bash script using mailx to send messages to a
of recipients. They have no salutation to personalize each one. Since I
want
to add that personalized salutation I decided to write a python script
inputreplace the bash script.
I have collected 11 docs explaining the smtplib and email modules and
providing example scripts to apply them to send multiple individual
messages
with salutations and attachments.
If I had a script that's been working for 30 years, I'd probably just
use Python to do the personalizing and let the rest of the bash script
do the rest, like it always has. The Python program would pipe or send
the personalized messages to the rest of the bash program. Something in
that ballpark, anyway.
Today I'm going to be reading these. They each recommend using .csv
files for names and addresses. My first search is learning whether I can >>> write a single .csv file such as:block
"name1","address1"
"mane2","address2"
which I believe will work; and by inserting at the top of the message
Hi, {yourname}If the file contents are going to be people's names and email addresses,
the name in the .csv file will replace the bracketed place holder
I would just tab separate them and split each line on the tab. Names
aren't going to include tabs so that would be safe. Email addresses
might theoretically include a tab inside a quoted name but that would be
extremely obscure and unlikely. No need for CSV, it would just add
complexity.
data = f.readlines()
for d in data:
name, addr = line.split('\t') if line.strip() else ('', '')
Still much to learn and the batch of downloaded PDF files should educate >>> me.
Regards,
Rich
In my view this whole thread became murky and complicated because the OP
did not write down the requirements for the program. Requirements are needed to communicate with other people. An individual may not need to actually write down the requirements - depending on their complexity -
but they always exist even if only vaguely in a person's mind. The requirements may include what tools or languages the person wants to use
and why.
If you are asking for help, you need to communicate the requirements to
the people you are asking for help from.
The OP may have thought the original post(s) contained enough of the requirements but as we know by now, they didn't.
The person asking for help may not realize they don't know enough to
write down all the requirements; an effort to do so may bring that lack
to visibility.
Mailing lists like these have a drawback that it's hard to impossible
for someone not involved in a thread to learn anything general from it.
We can write over and over again to please state clearly what you want
to do and where the sticking points are, but newcomers post new
questions without ever reading these pleas. Then good-hearted people
who want to be helpful end up spending a lot of time trying to guess
what is actually being asked for, and maybe never find out with enough clarity. Others take a guess and then spend time working up a solution
that may or may not be on target.
So please! before posting a request for help, write down the
requirements as best you can figure them out, and then make sure that
they are expressed such that the readers can understand.
Every trainer, in any field, has to deal with these problems - all the
time, and over-and-over.
On 4/02/24 06:58, Thomas Passin via Python-list wrote:
In my view this whole thread became murky and complicated because the
OP did not write down the requirements for the program. Requirements
are needed to communicate with other people. An individual may not
need to actually write down the requirements - depending on their
complexity - but they always exist even if only vaguely in a person's
mind. The requirements may include what tools or languages the person
wants to use and why.
If you are asking for help, you need to communicate the requirements
to the people you are asking for help from.
The OP may have thought the original post(s) contained enough of the
requirements but as we know by now, they didn't.
There is another possible interpretation in such situations (not
necessarily this one): that the person is fixated on a particular
solution (and unable/unwilling to adjust his/her thinking to consider
more widely).
Thus, the question is not: 'here's an entire problem, how can it be
solved', but more: 'I have a solution, and want help to implement it
(and only it) just-so'.
The latter is an interesting psychology:
1
an experienced person who is trying to translate from one tool to
another (Python), but discovers that a word-for-word solution is
difficult because of the artificial-constraints they've placed on the situation.
2
a beginner who doesn't know what (s)he doesn't know and comes-up with an idea, but fails to appreciate that there is likely more than one path to
the goal.
The person asking for help may not realize they don't know enough to
write down all the requirements; an effort to do so may bring that
lack to visibility.
In the case of 'Beginners' this should probably be taken-as-read!
Which is why we will always find ourselves asking questions or 'please
give more information'...
However, there are other reasons, eg corporate concerns or personality;
why people don't want to give more information. The former is reasonable (have suffered from same myself). The latter may reveal that the person
is 'difficult to deal with'...
Mailing lists like these have a drawback that it's hard to impossible
for someone not involved in a thread to learn anything general from
it. We can write over and over again to please state clearly what you
want to do and where the sticking points are, but newcomers post new
questions without ever reading these pleas. Then good-hearted people
who want to be helpful end up spending a lot of time trying to guess
what is actually being asked for, and maybe never find out with enough
clarity. Others take a guess and then spend time working up a
solution that may or may not be on target.
So please! before posting a request for help, write down the
requirements as best you can figure them out, and then make sure that
they are expressed such that the readers can understand.
Unfortunately, if the person doesn't understand the problem (leave-aside
any ideas of solution), then (s)he will not be able to clearly
communicate same to us, in any way, shape, or form...
Which brings one to the question: if a person cannot express the problem clearly and completely, is (s)he suited to development work? If the
problem is not understood, could 'the solution' ever be more than an exercise in hope?
(prototyping and experimentation aside)
Yes, it is frustrating to invest time and effort in helping someone,
only for same to disappear 'into a black hole'. The lack of response
seems to indicate a lack of respect or appreciation. Is this perhaps
part of today's "consumer" life-style, where so few are contributors or creators?
On the other side of that coin: do the people who make assumptions and (kindly) blaze-ahead with 'a solution', actually help the conversation?
If the assumptions are correct, yes! What if they are not?
...and don't get me started on folk who want us to do their training-assignments or build some application, for them!
As a slight aside: on one training-course DiscussionList/BulletinBoard set-up, if a trainee asked a question without a descriptive title/SubjectLine, eg "Python not working" or "Urgent: please help"; I
asked them to re-post with a title that would help others in a similar situation find the topic - and closed the original thread.
Some found it "brutal" - probably skewing towards those who felt
"Urgent" because they'd left things too close to deadline. Others joined
the (later) thread because they could identify the topic and realise
their interest in learning or contributing to the conversation...
Time pressures lead to a multitude of evils!
There's a quotation that goes something like "the poor will be with your always"?
(?possibly Biblical)
Whether we (here) are talking about 'poor' manners, 'poor'
understanding, 'poor' communication skills, 'poor' Python knowledge, or whatever; isn't such one of the rationales for this DiscussionList?
That said, we're all volunteering our (valuable) time!
In my view this whole thread became murky and complicated because the =OP=20
did not write down the requirements for the program. Requirements are =to=20
needed to communicate with other people. An individual may not need =
actually write down the requirements - depending on their complexity - =use=20
but they always exist even if only vaguely in a person's mind. The=20 requirements may include what tools or languages the person wants to =
and why.to=20
=20
If you are asking for help, you need to communicate the requirements =
the people you are asking for help from.
=20
The OP may have thought the original post(s) contained enough of the=20 requirements but as we know by now, they didn't.
The person asking for help may not realize they don't know enough to=20lack=20
write down all the requirements; an effort to do so may bring that =
to visibility.
Mailing lists like these have a drawback that it's hard to impossible=20it.=20
for someone not involved in a thread to learn anything general from =
We can write over and over again to please state clearly what you want =solution=20
to do and where the sticking points are, but newcomers post new=20
questions without ever reading these pleas. Then good-hearted people=20
who want to be helpful end up spending a lot of time trying to guess=20
what is actually being asked for, and maybe never find out with enough =
clarity. Others take a guess and then spend time working up a =
that may or may not be on target.
=20
So please! before posting a request for help, write down the=20
requirements as best you can figure them out, and then make sure that=20
they are expressed such that the readers can understand.
Dave,
You and I have had some experience in teaching or tutoring others and I think it fair to say our motivation is closer to teaching someone how they can fish for themselves rather than just handing them a fully-cooked fish.
My favorite kinds of questions, thus, include someone who explains what they are trying to do and shows some code along with indications of what it produced (including error messages) and what it should produce. Then the question should not be a request to just FIX THIS or WRITE IT FOR ME but asking if someone can show what they did wrong with some idea where it went wrong.
This may not be so common but it allows faster and easier help.
I will end with a comment. I have heard of interview techniques for a job where they deliberately supply a problem in which the goal is not so much to be easy to solve in front of them in real time but to watch how the person looking for a job responds to the uncertainties and asks follow-up questions or verbalizes things like, if it is like this, I might use this technique but if you also need that then ...
So, I shudder to think what happens if someone being interviewed turns around and asks us and further confuses things with changes to make it harder to recognize they are asking for outside help. The answer expected may well be to NOT use say the older versions of PASCAL to do something but switch to something better suited (and for that matter available.) I would not want to program the DES encryption/decryption method in Pascal again! And these days, it seems much better to just find a module or package that meets such needs.
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 6 |
Nodes: | 8 (0 / 8) |
Uptime: | 44:27:28 |
Calls: | 45 |
Files: | 21,492 |
Messages: | 63,459 |