Re: some problems for an introductory python test

2021-08-09 Thread Chris Angelico
On Tue, Aug 10, 2021 at 1:41 PM Mats Wichmann  wrote:
>
>
> On 8/9/21 6:34 PM, Chris Angelico wrote:
>
> > If you want to highlight the OOP nature of Python, rather than looking
> > at magic methods, I'd first look at polymorphism. You can add a pair
> > of integers; you can add a pair of tuples; you can add a pair of
> > strings. Each one logically adds two things together and gives a
> > result, and they're all spelled the exact same way. Dunder methods are
> > a way for custom classes to slot into that same polymorphism, but the
> > polymorphism exists first and the dunders come later.
> >
> > ChrisA
> >
>
> not disagreeing... and yeah I could have thought deeper about the
> answer, but I still think "notthing has been OOP" -> "yes it has, they
> just didn't realize it"  was worth mentioning

Oh yes, absolutely agree.

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


Re: some problems for an introductory python test

2021-08-09 Thread Mats Wichmann



On 8/9/21 6:34 PM, Chris Angelico wrote:


If you want to highlight the OOP nature of Python, rather than looking
at magic methods, I'd first look at polymorphism. You can add a pair
of integers; you can add a pair of tuples; you can add a pair of
strings. Each one logically adds two things together and gives a
result, and they're all spelled the exact same way. Dunder methods are
a way for custom classes to slot into that same polymorphism, but the
polymorphism exists first and the dunders come later.

ChrisA



not disagreeing... and yeah I could have thought deeper about the 
answer, but I still think "notthing has been OOP" -> "yes it has, they 
just didn't realize it"  was worth mentioning

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


Re: some problems for an introductory python test

2021-08-09 Thread Chris Angelico
On Tue, Aug 10, 2021 at 8:19 AM Mats Wichmann  wrote:
> Even if you do
>
> x = 2 + 3
>
> you're actually creating an integer object with a value of 2, and
> calling its add method to add the integer object with the value of 3 to
> it. The syntax hides it, but in a way it's just convenience that it does
> so...
>
>  >>> 2 + 3
> 5
>  >>> x = 2
>  >>> x.__add__(3)
> 5
>
>
> sorry for nitpicking :)  But... don't be afraid of letting them know
> it's OOP, and it''s not huge and complex and scary!
>

Since we're nitpicking already, "2 + 3" isn't the same as
"(2).__add__(3)"; among other things, it's able to call
(3).__radd__(2) instead. Plus there's technicalities about type slots
and such.

If you want to highlight the OOP nature of Python, rather than looking
at magic methods, I'd first look at polymorphism. You can add a pair
of integers; you can add a pair of tuples; you can add a pair of
strings. Each one logically adds two things together and gives a
result, and they're all spelled the exact same way. Dunder methods are
a way for custom classes to slot into that same polymorphism, but the
polymorphism exists first and the dunders come later.

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


Re: some problems for an introductory python test

2021-08-09 Thread Mats Wichmann

On 8/9/21 3:07 PM, Hope Rouselle wrote:

I'm looking for questions to put on a test for students who never had
any experience with programming, but have learned to use Python's
procedures, default arguments, if-else, strings, tuples, lists and
dictionaries.  (There's no OOP at all in this course.  Students don't
even write ls.append(...).  They write list.append(ls, ...)).


Nitpickery... there *is* OOP in the course, they just don't know it.

Long long ago (over 20 yrs now) I developed a Python course for a 
commercial training provider, and in it I claimed one of the great 
things about Python was it supported all kinds of object oriented 
programming techniques, but you could also use it without doing anything 
object oriented. If I wrote a course now, I'd never make that claim, 
because everything you do in Python is pretty much object oriented.


>>> x = list()
>>> type(x)

>>> dir(x)
['__add__', '__class__', '__class_getitem__', '__contains__', 
'__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', 
'__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '
__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']


list is a class and it has methods... it's "object-oriented"!


Even if you do

x = 2 + 3

you're actually creating an integer object with a value of 2, and 
calling its add method to add the integer object with the value of 3 to 
it. The syntax hides it, but in a way it's just convenience that it does 
so...


>>> 2 + 3
5
>>> x = 2
>>> x.__add__(3)
5


sorry for nitpicking :)  But... don't be afraid of letting them know 
it's OOP, and it''s not huge and complex and scary!






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


Re: some problems for an introductory python test

2021-08-09 Thread Chris Angelico
On Tue, Aug 10, 2021 at 7:25 AM Hope Rouselle  wrote:
> I came up with the following question.  Using strings of length 5
> (always), write a procedure histogram(s) that consumes a string and
> produces a dictionary whose keys are each substrings (of the string) of
> length 1 and their corresponding values are the number of times each
> such substrings appear.  For example, histogram("a") = {"a": 5}.
> Students can "loop through" the string by writing out s[0], s[1], s[2],
> s[3], s[4].

In other words, recreate collections.Counter? Seems decent, but you'll
need to decide whether you want them to use defaultdict, use
__missing__, or do it all manually.

> I think you get the idea.  I hope you can provide me with creativity.  I
> have been looking at books, but every one I look at they introduce loops
> very quickly and off they go.  Thank you!

Probably because loops are kinda important? :)

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


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

2021-08-09 Thread Chris Angelico
On Tue, Aug 10, 2021 at 7:24 AM Jack Brandom  wrote:
>
> Greg Ewing  writes:
>
> > On 6/08/21 12:00 pm, Jack Brandom wrote:
> >> It seems
> >> that I'd begin at position 3 (that's "k" which I save somewhere), then I
> >> subtract 1 from 3, getting 2 (that's "c", which I save somewhere), then
> >> I subtract 1 from 2, getting 1 (that's "a", ...), then I subtract 1 from
> >> 1, getting 0 (that's J, ...), so I got "kcaJ" but my counter is 0 not
> >> -13, which was my stopping point.
> >
> > You need to first replace any negative or missing indices with
> > equivalent indices measured from the start of the string.
> >
> > When you do that in this example, you end up iterating backwards from 3
> > and stopping at -1.
>
> Yeah, that makes sense now.  But it sucks that the rule for replacing
> negative indices is sometimes missing index and sometimes positive
> index.  (That is, we can't always use positive indices.  Sometimes we
> must use no index at all.  I mean that's how it looks to my eyes.)

Sometimes, the index  you need to use is the value None. You cannot
use a positive number to indicate the position to the left of zero -
at least, not if you consider numbers to be on a number line.

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


some problems for an introductory python test

2021-08-09 Thread Hope Rouselle
I'm looking for questions to put on a test for students who never had
any experience with programming, but have learned to use Python's
procedures, default arguments, if-else, strings, tuples, lists and
dictionaries.  (There's no OOP at all in this course.  Students don't
even write ls.append(...).  They write list.append(ls, ...)).

I'd like to put questions that they would have to write procedures that
would would be real-world type of stuff, without error checking,
exceptions and such.  So if you think of something more or less cool
that uses loops, we can sometimes simplify it by assuming the input has
a certain fixed size.

I came up with the following question.  Using strings of length 5
(always), write a procedure histogram(s) that consumes a string and
produces a dictionary whose keys are each substrings (of the string) of
length 1 and their corresponding values are the number of times each
such substrings appear.  For example, histogram("a") = {"a": 5}.
Students can "loop through" the string by writing out s[0], s[1], s[2],
s[3], s[4].

I'd like even better questions.  I'd like questions that would tell them
to write procedures that would also have inverses, so that one could
check the other of the other.  (A second question would ask for the
inverse, but hopefully real world stuff.  One such question could be
parsing a line separate by fields such as "root:0:0:mypass:Super User"
and another that gives them ["root", 0, 0, ...] and asks them to write
"root:0:0:mypass:..."  You get the idea.)

Students know how to use str().  But they don't know how to use type(),
so they can't really check for the type of the input.  I probably
couldn't ask them to write a prototype of a tiny subset of pickle, say.

I think you get the idea.  I hope you can provide me with creativity.  I
have been looking at books, but every one I look at they introduce loops
very quickly and off they go.  Thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2021-08-09 Thread Jack Brandom
Greg Ewing  writes:

> On 6/08/21 12:00 pm, Jack Brandom wrote:
>> It seems
>> that I'd begin at position 3 (that's "k" which I save somewhere), then I
>> subtract 1 from 3, getting 2 (that's "c", which I save somewhere), then
>> I subtract 1 from 2, getting 1 (that's "a", ...), then I subtract 1 from
>> 1, getting 0 (that's J, ...), so I got "kcaJ" but my counter is 0 not
>> -13, which was my stopping point.
>
> You need to first replace any negative or missing indices with
> equivalent indices measured from the start of the string.
>
> When you do that in this example, you end up iterating backwards from 3
> and stopping at -1.

Yeah, that makes sense now.  But it sucks that the rule for replacing
negative indices is sometimes missing index and sometimes positive
index.  (That is, we can't always use positive indices.  Sometimes we
must use no index at all.  I mean that's how it looks to my eyes.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: CODING PAGE ACCESS

2021-08-09 Thread Jack Dangler

Or open a terminal and type 'python3' ...

Or open an editor, type in a py program, save it as "myfirstcode.py" and 
then at the console type 'python3 ./myfirstcode.py' and hit return...


There are a number of choices to get there - that's the tenet of Linux. 
It's all about choice.


On 8/7/21 3:31 PM, MRAB wrote:

On 2021-08-07 04:34, MICHAEL J W SMITH via Python-list wrote:

I downloaded python. I selected it from the start menu. I clicked on:-
Python 3-9New
I got:-
IDLE (Python 3.9 64-bit)
Python 3.9 (64-bit)
Python 3.9 Manuals (64-bit)
Python 3.9 Module Docs (64-bit)
I wish to access the page where I do coding.I would appreciate help, 
either directly, or with information as to where I can get help.Thank 
you so much!



The Manual contains a tutorial.

You can use IDLE to use Python interactively or edit programs.

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


Re: Errors and some bugs

2021-08-09 Thread dn via Python-list
On 09/08/2021 15.12, Himanshu Gupta wrote:
> While running some pip command to install spacy package for ver3.9 I always
> face trouble can you provide a solution

The short answer is "yes", but what is the problem?

Please copy-paste the install command and the (full) error message, from
the terminal-session into an email response, and we'll try to help...
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


annotations cause dataclass fields type side effects

2021-08-09 Thread Lukas Lösche
I'm on Python 3.9.6 and trying to make sense of the following behaviour:

>>> from dataclasses import dataclass, fields
>>> @dataclass
... class Foobar:
... name: str
...
>>> fields(Foobar)[0].type

>>> type(fields(Foobar)[0].type)




>>> from __future__ import annotations
>>> from dataclasses import dataclass, fields
>>>
>>> @dataclass
... class Foobar:
... name: str
...
>>> fields(Foobar)[0].type
'str'
>>> type(fields(Foobar)[0].type)


I have a validation function that checks if the types of all fields in
a dataclass are what they are supposed to be. But as soon as I import
annotations from __future__ this validation breaks as the arg that I'm
passing to isinstance() is no longer a type class but a string.

What am I doing wrong?

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


Errors and some bugs

2021-08-09 Thread Himanshu Gupta
While running some pip command to install spacy package for ver3.9 I always
face trouble can you provide a solution
-- 
https://mail.python.org/mailman/listinfo/python-list


John Griner Python Tutor Available

2021-08-09 Thread John Griner
   To whom it may concern,

I am not sure if this is appropriate, but I would like to somehow
   `advertise' my classes.  I have a free workshop: Intro to Python: Data
   Wrangling, Visualization, List and Tuples and a series that consist of 3
   parts, (1) Data Wrangling (2) Data Visualization and (3) List and Tuples.
   The free workshop is a broad overview of the series that consist of 3
   classes. It was designed for the beginner, but someone with intermediate
   to advanced knowledge of Python may also benefit.  I also have 2 other
   more advanced courses, and after sometime, I will offer other, more
   advanced courses.



   If you would like the link to join, I can provide the link, and if you ae
   unsure of whether or not you want to take the courses, just take the free
   workshop.



   Best, John Griner

   [1][IMG] Virus-free. [2]www.avast.com

References

   Visible links
   1. 
https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient&utm_term=icon
   2. 
https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient&utm_term=link
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Flask – how to write csv file & save using prefilled value of the filename (response.headers["Content-Disposition"]="attachment; filename=xxx")

2021-08-09 Thread Roland Mueller via Python-list
pe 6. elok. 2021 klo 19.15 MRAB (pyt...@mrabarnett.plus.com) kirjoitti:

> On 2021-08-06 16:50, Suretha Weweje wrote:
> > I am trying to upload a CSV file with flask, read and process one line
> at a
> > time while iterating through all rows of the file and write the results
> > back to a new CSV file.  My python script produces the correct results on
> > its own, but I am not able to get the same results when using Flask. I am
> > new to flask.
> >
> [snip]
>  >
> >  csv_reader = csv.reader(new)
> >
> [snip]
> >
> >  *mywriter = csv.DictWriter('results.csv', fieldnames=headers_new)*
> [snip]
>
> The problem has nothing to do with Flask.
>
> 'csv.reader' and 'csv.DictWriter' expect a file object, but you're
> passing them strings.
> --
> https://mail.python.org/mailman/listinfo/python-list


May be you should use io.StringIO to create a file/stream from an input
string.

from io import StringIO

s = "\n".join(["a","b","c"]) + "\n"

for line in StringIO(s):
print(f"\t{line}")

BR,
Roland
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tracing in a Flask application

2021-08-09 Thread Albert-Jan Roskam
   Hi,
   logging.basicConfig(level="DEBUG")
   ..in e.g  __init__.py
   AJ
   On 4 Aug 2021 23:26, Javi D R  wrote:

 Hi

 I would like to do some tracing in a flask. I have been able to trace
 request in plain python requests using sys.settrace(), but this doesnt
 work
 with Flask.

 Moreover, what i want to trace is in a flask application, when an
 endpoint
 is called, what was the request, which parts of the code was executed (i
 think i can still do it with settrace) and what is the response sent by
 the
 application

 Can you help me to understand which services i can use to do this?

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