requests

2019-04-04 Thread Jack Dangler
Hi, all. Just getting started but already have an idea for something to 
save me some grief


we have lists of files that reside on a sharepoint site at work that we 
pick from. These have a variety of data items in them and we need to 
start the process by copying the entire contents into a local work file 
and then use that as a part of a runbook to develop the solution to the 
problem statement (one of the items within the file). I've done the 
copy/paste on all 31 parts 4 times and I'm already tired. "This would be 
a good place to learn a little Python" says me. So I figure I can either 
go the requests route to the sharepoint site and navigate the path to 
the place where the work items are and then figure out (based on the 
responses) how to pick the right item and get the data back, or connect 
to the SP database and work through the schema (assuming I even have any 
access to the backend database). Has anyone here used Py for extracting 
item data from SP? Is there a preferred method for this? Thanks for the 
input - really appreciate the wealth of knowledge here.


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


Re: requests

2019-04-04 Thread Rhodri James

On 04/04/2019 12:57, Jack Dangler wrote:
Hi, all. Just getting started but already have an idea for something to 
save me some grief


we have lists of files that reside on a sharepoint site at work that we 
pick from. These have a variety of data items in them and we need to 
start the process by copying the entire contents into a local work file 
and then use that as a part of a runbook to develop the solution to the 
problem statement (one of the items within the file). I've done the 
copy/paste on all 31 parts 4 times and I'm already tired. "This would be 
a good place to learn a little Python" says me. So I figure I can either 
go the requests route to the sharepoint site and navigate the path to 
the place where the work items are and then figure out (based on the 
responses) how to pick the right item and get the data back, or connect 
to the SP database and work through the schema (assuming I even have any 
access to the backend database). Has anyone here used Py for extracting 
item data from SP? Is there a preferred method for this? Thanks for the 
input - really appreciate the wealth of knowledge here.


My personal experience with SharePoint has always been to use something 
less painful :-)  However, a quick Google turned up two packages in 
PyPI: SharePlum (https://pypi.org/project/SharePlum/) and sharepoint 
(https://pypi.org/project/sharepoint/).  These may do a lot of the heavy 
lifting for you.  I suggest you have a quick browse through the 
documentation and see if it makes sense for your needs.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


RE: scalable bottleneck

2019-04-04 Thread Schachner, Joseph
If you are using Python 3, range does not create at list, it is a generator.   
If you're using Python 2.x, use xrange instead of range.  xrange is a 
generator.   In Python 3 there is no xrange, they just made range the generator.

--- Joseph S.

-Original Message-
From: Sayth Renshaw  
Sent: Wednesday, April 3, 2019 5:42 PM
To: python-list@python.org
Subject: scalable bottleneck

In an email, I received this question as part of a newsletter.

def fetch_squares ( max_root ):
squares = []
for x in range ( max_root ):
squares . append (x **2)
return squares

MAX = 5

for square in fetch_squares (MAX ):
 do_something_with ( square )

1) Do you see a memory bottleneck here? If so, what is it?
2) Can you think of a way to fix the memory bottleneck?

Want to know if I am trying to solve the correct bottleneck.
I am thinking that the bottleneck is to create a list only to iterate the same 
list you created sort of doubling the time through.

Is that the correct problem to solve?

If it is then I thought the best way is just to supply the numbers on the fly, 
a generator.

def supply_squares(max_root):
for x in max_root:
yield x

MAX = 5


So then I set up a loop and do whatever is needed. At this time I am creating 
generator objects. But is this the correct way to go? More of a am I thinking 
correctly questino.

item = 0
while item < MAX:
print(supply_squares(item))
item += 1




Thanks

Sayth

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


Re: From parsing a class to code object to class to mappingproxy to object (oh my!)

2019-04-04 Thread adam . preble
On Monday, April 1, 2019 at 1:23:42 AM UTC-5, Gregory Ewing wrote:
> adam.pre...@gmail.com wrote:
> https://eli.thegreenplace.net/2012/06/15/under-the-hood-of-python-class-definitions
> 
> Briefly, it creates a dict to serve as the class's namespace dict,
> then executes the class body function passed to it, with that dict
> as the local namespace. So method defs and other assignments go
> straight into what will become the class namespace when the class
> object is created.

Thanks for the response. I was meaning to write back earlier, but I've been 
spending my free Python time in the evenings reimplementing what I'm doing to 
work more correctly. I'm guessing before the code object representing the class 
body gets run, __build_class__ is establishing various dunders such as 
__name__, __qual_name__, and __module__. I haven't fully penetrated that, but I 
also took an embarrassingly long amount of time to fully comprehend LOAD_NAME 
versus LOAD_FAST versus LOAD_GLOBAL...
-- 
https://mail.python.org/mailman/listinfo/python-list


I want understand how this word wrap program playing on input

2019-04-04 Thread Arup Rakshit
I am reading a Python book, where the author used a simple word wrap program to 
explain another concept. But I am not understanding some parts of the program.

def wrap(text, line_length):
"""Wrap a string to a specified line length"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word)

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

wealth_of_nations = "The annual labour of every nation is the fund which 
or" \
"iginally supplies it with all the necessaries and conveniencies of life 
wh" \
"ich it annually consumes, and which consist always either in the 
immediate" \
" produce of that labour, or in what is purchased with that produce from 
ot" \
"her nations. According, therefore, as this produce, or what is purchased 
w" \
"ith it, bears a greater or smaller proportion to the number of those who 
a" \
"re to consume it, the nation will be better or worse supplied with all 
the" \
" necessaries and conveniencies for which it has occasion."

Now when I call it:

python3 -i wrapper.py 
>>> wrap(wealth_of_nations, 25)
'The annual labour of every\nnation is the fund which\noriginally supplies 
it with\nall the necessaries and\nconveniencies of life which\nit annually 
consumes, and\nwhich consist always either\nin the immediate produce of\nthat 
labour, or in what is\npurchased with that produce\nfrom other 
nations.\nAccording, therefore, as\nthis produce, or what is\npurchased with 
it, bears a\ngreater or smaller\nproportion to the number of\nthose who are to 
consume it,\nthe nation will be better or\nworse supplied with all 
the\nnecessaries and\nconveniencies for which it\nhas occasion.'
>>> print(_)
The annual labour of every
nation is the fund which
originally supplies it with
all the necessaries and
conveniencies of life which
it annually consumes, and
which consist always either
in the immediate produce of
that labour, or in what is
purchased with that produce
from other nations.
According, therefore, as
this produce, or what is
purchased with it, bears a
greater or smaller
proportion to the number of
those who are to consume it,
the nation will be better or
worse supplied with all the
necessaries and
conveniencies for which it
has occasion.

My questions are:

1. why  `line_length` is set to `current_line_length` initially?
2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
3. Also why that `if` test is required there.

Can anyone help me to understand this program, I am totally confused.

Thanks,

Arup Rakshit
a...@zeit.io



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


Re: I want understand how this word wrap program playing on input

2019-04-04 Thread Chris Angelico
On Fri, Apr 5, 2019 at 5:34 AM Arup Rakshit  wrote:
> lines_of_words = []
> current_line_length = line_length
> for word in words:
> if current_line_length + len(word) > line_length:
> lines_of_words.append([]) # new line
> current_line_length = 0
> lines_of_words[-1].append(word)
> current_line_length += len(word)
>
> My questions are:
>
> 1. why  `line_length` is set to `current_line_length` initially?
> 2. In the if clause why `current_line_length` is set back to 0 every time the 
> condition is true.
> 3. Also why that `if` test is required there.
>

I'll start with questions 2 and 3, then wheel back to #1.

The 'current_line_length' variable is a classic running counter. It
counts something until it hits a limit, then gets reset. Imagine
emulating a vending machine - you add up the coins inserted until it
reaches the value of the product, then dispense the product and reset
the "money inserted" counter to zero. In this case, the word wrapping
is done by saying "will this word fit? If not, go to a new line" for
every word.

So, to come back to question 1: The counter is initially full, rather
than empty, to force the creation of a new line at the start of the
loop. The very first word seen (since a "word" is guaranteed to have
at least one character in it - text.split() makes sure of that) will
be shown up as "nope, won't fit", so the list of lists gets its first
value added to it. As an alternative, it could have been written like
this:

lines_of_words = [ [] ] # one initial line
current_line_length = 0 # the initial line is empty

Depending on the algorithm, sometimes it's easiest to force something
to happen on the first iteration, and sometimes it's easiest to force
it to happen at the end. (And sometimes both.) You'll see tricks like
this in a lot of places.

Hope that helps! :)

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


RE: I want understand how this word wrap program playing on input

2019-04-04 Thread David Raymond
The function is constructing a list of the lines, which it will combine at the 
end. Answering the questions in reverse order:

3. Also why that `if` test is required there.
The if statement is saying "I don't have room on my current line for the next 
word, so time to start a new line"

2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
current_line_length is, well, the length of the current line. Where it's set to 
0 is when you've just started a new line, so it's saying "I'm now on a new line 
and have used up 0 characters so far on that line"

1. why  `line_length` is set to `current_line_length` initially?
lines_of_words starts out empty. Setting current_line_length to line_length 
triggers the "hey, I need to make a new line" on the first word, effectively 
creating the first line.



Also, I feel like there's a bug in this, where

current_line_length += len(word)

should be...

current_line_length += (len(word) + 1)

Otherwise the line length doesn't count the spaces. And a lot of little words 
will result in longer lines than you asked for.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Arup Rakshit
Sent: Thursday, April 04, 2019 2:33 PM
To: Python
Subject: I want understand how this word wrap program playing on input 

I am reading a Python book, where the author used a simple word wrap program to 
explain another concept. But I am not understanding some parts of the program.

def wrap(text, line_length):
"""Wrap a string to a specified line length"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word)

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

wealth_of_nations = "The annual labour of every nation is the fund which 
or" \
"iginally supplies it with all the necessaries and conveniencies of life 
wh" \
"ich it annually consumes, and which consist always either in the 
immediate" \
" produce of that labour, or in what is purchased with that produce from 
ot" \
"her nations. According, therefore, as this produce, or what is purchased 
w" \
"ith it, bears a greater or smaller proportion to the number of those who 
a" \
"re to consume it, the nation will be better or worse supplied with all 
the" \
" necessaries and conveniencies for which it has occasion."

Now when I call it:

python3 -i wrapper.py 
>>> wrap(wealth_of_nations, 25)
'The annual labour of every\nnation is the fund which\noriginally supplies 
it with\nall the necessaries and\nconveniencies of life which\nit annually 
consumes, and\nwhich consist always either\nin the immediate produce of\nthat 
labour, or in what is\npurchased with that produce\nfrom other 
nations.\nAccording, therefore, as\nthis produce, or what is\npurchased with 
it, bears a\ngreater or smaller\nproportion to the number of\nthose who are to 
consume it,\nthe nation will be better or\nworse supplied with all 
the\nnecessaries and\nconveniencies for which it\nhas occasion.'
>>> print(_)
The annual labour of every
nation is the fund which
originally supplies it with
all the necessaries and
conveniencies of life which
it annually consumes, and
which consist always either
in the immediate produce of
that labour, or in what is
purchased with that produce
from other nations.
According, therefore, as
this produce, or what is
purchased with it, bears a
greater or smaller
proportion to the number of
those who are to consume it,
the nation will be better or
worse supplied with all the
necessaries and
conveniencies for which it
has occasion.

My questions are:

1. why  `line_length` is set to `current_line_length` initially?
2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
3. Also why that `if` test is required there.

Can anyone help me to understand this program, I am totally confused.

Thanks,

Arup Rakshit
a...@zeit.io



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


Re: I want understand how this word wrap program playing on input

2019-04-04 Thread MRAB

On 2019-04-04 19:53, David Raymond wrote:

The function is constructing a list of the lines, which it will combine at the 
end. Answering the questions in reverse order:

3. Also why that `if` test is required there.
The if statement is saying "I don't have room on my current line for the next word, 
so time to start a new line"

2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
current_line_length is, well, the length of the current line. Where it's set to 0 is when 
you've just started a new line, so it's saying "I'm now on a new line and have used 
up 0 characters so far on that line"

1. why  `line_length` is set to `current_line_length` initially?
lines_of_words starts out empty. Setting current_line_length to line_length triggers the 
"hey, I need to make a new line" on the first word, effectively creating the 
first line.



Also, I feel like there's a bug in this, where

current_line_length += len(word)

should be...

current_line_length += (len(word) + 1)

Otherwise the line length doesn't count the spaces. And a lot of little words 
will result in longer lines than you asked for.

Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a 
line won't be followed by a space (off-by-one). The easiest fix for that 
is to add 1 to line_length initially, another little trick.


-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Arup Rakshit
Sent: Thursday, April 04, 2019 2:33 PM
To: Python
Subject: I want understand how this word wrap program playing on input

I am reading a Python book, where the author used a simple word wrap program to 
explain another concept. But I am not understanding some parts of the program.

 def wrap(text, line_length):
 """Wrap a string to a specified line length"""
 words = text.split()
 lines_of_words = []
 current_line_length = line_length
 
 for word in words:

 if current_line_length + len(word) > line_length:
 lines_of_words.append([]) # new line
 current_line_length = 0
 lines_of_words[-1].append(word)
 current_line_length += len(word)
 
 lines = [' '.join(line_of_words) for line_of_words in lines_of_words]

 return '\n'.join(lines)
 
 wealth_of_nations = "The annual labour of every nation is the fund which or" \

 "iginally supplies it with all the necessaries and conveniencies of life 
wh" \
 "ich it annually consumes, and which consist always either in the 
immediate" \
 " produce of that labour, or in what is purchased with that produce from 
ot" \
 "her nations. According, therefore, as this produce, or what is purchased 
w" \
 "ith it, bears a greater or smaller proportion to the number of those who 
a" \
 "re to consume it, the nation will be better or worse supplied with all 
the" \
 " necessaries and conveniencies for which it has occasion."

Now when I call it:

 python3 -i wrapper.py
 >>> wrap(wealth_of_nations, 25)
 'The annual labour of every\nnation is the fund which\noriginally supplies 
it with\nall the necessaries and\nconveniencies of life which\nit annually 
consumes, and\nwhich consist always either\nin the immediate produce of\nthat 
labour, or in what is\npurchased with that produce\nfrom other 
nations.\nAccording, therefore, as\nthis produce, or what is\npurchased with 
it, bears a\ngreater or smaller\nproportion to the number of\nthose who are to 
consume it,\nthe nation will be better or\nworse supplied with all 
the\nnecessaries and\nconveniencies for which it\nhas occasion.'
 >>> print(_)
 The annual labour of every
 nation is the fund which
 originally supplies it with
 all the necessaries and
 conveniencies of life which
 it annually consumes, and
 which consist always either
 in the immediate produce of
 that labour, or in what is
 purchased with that produce
 from other nations.
 According, therefore, as
 this produce, or what is
 purchased with it, bears a
 greater or smaller
 proportion to the number of
 those who are to consume it,
 the nation will be better or
 worse supplied with all the
 necessaries and
 conveniencies for which it
 has occasion.

My questions are:

1. why  `line_length` is set to `current_line_length` initially?
2. In the if clause why `current_line_length` is set back to 0 every time the 
condition is true.
3. Also why that `if` test is required there.

Can anyone help me to understand this program, I am totally confused.

Thanks,

Arup Rakshit
a...@zeit.io





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


Re: I want understand how this word wrap program playing on input

2019-04-04 Thread Chris Angelico
On Fri, Apr 5, 2019 at 6:16 AM MRAB  wrote:
>
> On 2019-04-04 19:53, David Raymond wrote:
> > The function is constructing a list of the lines, which it will combine at 
> > the end. Answering the questions in reverse order:
> >
> > 3. Also why that `if` test is required there.
> > The if statement is saying "I don't have room on my current line for the 
> > next word, so time to start a new line"
> >
> > 2. In the if clause why `current_line_length` is set back to 0 every time 
> > the condition is true.
> > current_line_length is, well, the length of the current line. Where it's 
> > set to 0 is when you've just started a new line, so it's saying "I'm now on 
> > a new line and have used up 0 characters so far on that line"
> >
> > 1. why  `line_length` is set to `current_line_length` initially?
> > lines_of_words starts out empty. Setting current_line_length to line_length 
> > triggers the "hey, I need to make a new line" on the first word, 
> > effectively creating the first line.
> >
> >
> >
> > Also, I feel like there's a bug in this, where
> >
> > current_line_length += len(word)
> >
> > should be...
> >
> > current_line_length += (len(word) + 1)
> >
> > Otherwise the line length doesn't count the spaces. And a lot of little 
> > words will result in longer lines than you asked for.
> >
> Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a
> line won't be followed by a space (off-by-one). The easiest fix for that
> is to add 1 to line_length initially, another little trick.

Or, equivalently, to reset current_line_length to -1, which is an elegant hack.

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


RE: I want understand how this word wrap program playing on input

2019-04-04 Thread David Raymond
> Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a
> line won't be followed by a space (off-by-one). The easiest fix for that
> is to add 1 to line_length initially, another little trick.

> Or, equivalently, to reset current_line_length to -1, which is an elegant 
> hack.

I'm actually going to stand my ground on this one. The incrementing of the line 
length happens _after_ you've added that word to the line, and the check for 
length is checking against the length of the new word. So adding the 1 for "the 
space at the end" won't come into effect until the next word, where you're 
asking "if I add this new word..." at which point you'll want to have included 
that space.


So copy and paste wrap to wrap2 and make your changes to wrap2, then run 
something like this, playing with the line length to check. The pipes show the 
last allowed position of a character.



def wrap(text, line_length):
"""Wrap a string to a specified line length
Original Version"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word)

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

def wrap2(text, line_length):
"""Wrap a string to a specified line length
Altered Version"""
words = text.split()
lines_of_words = []
current_line_length = line_length

for word in words:
if current_line_length + len(word) > line_length:
lines_of_words.append([]) # new line
current_line_length = 0
lines_of_words[-1].append(word)
current_line_length += len(word) + 1

lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
return '\n'.join(lines)

foo = "a b antidisestablishmentarianism c d e f g h i j k l m n o p queue are 
ess tee you vee doubleyou ecks why zee"

for wrapWidth in range(12, 19):
print(f"Width = {wrapWidth:d}")
showWidth = " " * (wrapWidth - 1) + "|"
print(showWidth)
print(wrap(foo, wrapWidth))
print(showWidth)
print(wrap2(foo, wrapWidth))
print(showWidth)
print("\n")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I want understand how this word wrap program playing on input

2019-04-04 Thread DL Neil

Arup,

On 5/04/19 7:33 AM, Arup Rakshit wrote:

I am reading a Python book, where the author used a simple word wrap program to 
explain another concept. But I am not understanding some parts of the program.

...

A technique for solving this sort of comprehension-problem is to 
simulate the operations of the computer/CPU on-paper. 
Instruction-by-instruction, write down the names of the objects 
instantiated and their (current) values. As you loop through the code, 
those (current) values change, and you will see exactly how they change 
- divining (and defining) the 'why', as you go...


Of course, only old-***s (like me) have the skills to handle pen/pencil 
and paper technology! So, may I recommend an excellent tool which will 
(hopefully) achieve the same ends for you: http://pythontutor.com/



PS don't be shy about mentioning your "book", its "author", and its 
title (hey, go 'full-APA' adding ISBN, pageNR...). Such will be a credit 
to the author(s) and a possible recommendation/inspiration to fellow 
Pythonista!


--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Logging cf Reporting = Friday Filosofical Finking

2019-04-04 Thread DL Neil
Is the logging module an ideal means to provide (printed) user reports, 
or is it a 'bad fit' and not designed/fit for such a purpose?



PSL's logging module (per discussion 'here' earlier this week) is often 
quietly avoided by 'the average Python programmer'. It is unwieldy, yet 
that is, in-part, a function of its power and flexibility. Its 
explanation in the Library docs requires multiple pages - and even after 
sending us first to the 'HowTo', still many people turn away (vaguely 
promising to 'come back later'). For the nit-picky amongst us: it 
dramatically fails PEP-8* (and we won't even mention higher numbers).


Once the framework is learned, its flexibility appreciated, and its 
strictures accepted; (IMHO) the library comes into its own. Over the 
years, the range of output options ("handlers") has grown and become 
ever more sophisticated. Simple text files are now supplemented by 
"rotating" mechanisms; simple socket transmissions have formalised into 
SMTP (email), HTTP, queues, and datagrams; and both MS-Win and *nix 
syslog mechanisms are covered. Similarly, the traditional formatting of 
messages with ToD-clock, technicalIDs, and error-level preceding the 
'real content', whilst still standard, can be completely replaced to 
suit the application and circumstance.


The received-wisdom of text books and tutorials is that Python trainees 
should 'evolve' from using quick-and-dirty debug-print functionality, to 
logging. This idea(l) promotes the virtue of providing a full 
information flow when it is vitally-needed, but not cluttering-up the 
display with excessive and extraneous data, when not - especially when 
GUIs are involved! (plus obviating that pesky-business of going-through 
ensuring every debug-print is deleted or commented-out, before delivery 
to 'live'!) However, there's always a lot to learn - too many toys, too 
little time!


Recently, I was building a little "batch job"# for an author-friend 
who'd lost control of his extensive and burgeoning library of files, 
back-ups, half-dead disks, archives... Being a 'good boy'& I was 
throwing all the debug/test/demo info out to a log-file. Without (my) 
thinking too much, all the useful listings ended-up in the same place. 
Oops! Rather than bringing them 'back' out to the screen (which, because 
he prefers to work from paper rather than the screen - "now you tell 
me". Sigh!) would also have meant capturing sys.stdout to grant the user 
his (late-expressed) desire; I established another logger and made minor 
mods to the code so that it would instead emit the 'useful' output 
there. Hence, an "output report".


I was chuckling at how my 'mistake' turned into a 'plus', because 
without a lot of work to 'fix' things, the user was given exactly what 
he wanted! ("oh, and it would be nice if you could send the file to me 
by email..." - they're always, um, never, (quite) satisfied...)



In the more general case, when at least some of an application's output 
goes to file (or similar), is it a 'smart move'% to use the logging 
library to collect more than behind-the-scenes debugging, or 
application/syslog-level data?



* but then, so do I!
# https://en.wikipedia.org/wiki/Batch_processing
& if you believe that, you'll believe anything!
% 
https://www.quora.com/What-is-meant-by-the-Mark-Twain-quote-To-a-man-with-a-hammer-everything-looks-like-a-nail

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: I want understand how this word wrap program playing on input

2019-04-04 Thread MRAB

On 2019-04-04 20:48, David Raymond wrote:

Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a
line won't be followed by a space (off-by-one). The easiest fix for that
is to add 1 to line_length initially, another little trick.



Or, equivalently, to reset current_line_length to -1, which is an elegant hack.


I'm actually going to stand my ground on this one. The incrementing of the line length happens 
_after_ you've added that word to the line, and the check for length is checking against the length 
of the new word. So adding the 1 for "the space at the end" won't come into effect until 
the next word, where you're asking "if I add this new word..." at which point you'll want 
to have included that space.


So copy and paste wrap to wrap2 and make your changes to wrap2, then run 
something like this, playing with the line length to check. The pipes show the 
last allowed position of a character.


[snip]
Oops! You're correct.
--
https://mail.python.org/mailman/listinfo/python-list


Re: From parsing a class to code object to class to mappingproxy to object (oh my!)

2019-04-04 Thread adam . preble
On Thursday, April 4, 2019 at 1:17:02 PM UTC-5, adam@gmail.com wrote:

> Thanks for the response. I was meaning to write back earlier, but I've been 
> spending my free Python time in the evenings reimplementing what I'm doing to 
> work more correctly. I'm guessing before the code object representing the 
> class body gets run, __build_class__ is establishing various dunders such as 
> __name__, __qual_name__, and __module__. I haven't fully penetrated that, but 
> I also took an embarrassingly long amount of time to fully comprehend 
> LOAD_NAME versus LOAD_FAST versus LOAD_GLOBAL...

I was blabbing on this while I was away from my examples so I did botch this up 
a bit. I guess when building the class, __name__ will get set beforehand, it 
the program's setting __qualname__ and __module__ from there. Something I don't 
really understand from a code generation perspective is the switch over to 
STORE_NAME for class methods. When I disassemble regular function definitions, 
I expect to see the result of MAKE_FUNCTION stored using STORE_FAST. However, 
in the code generated from a class body, it's a STORE_NAME. I know this is 
signifying to the interpeter not to rely on sourcing __init__ locally, but I 
don't understand why it suddenly has decided that now that it's in a class body.

I'm in a situation where I am have to generate those opcodes so I'm trying to 
figure out what the magic switch is--if it isn't just that I'm inside a class 
definition now.
-- 
https://mail.python.org/mailman/listinfo/python-list


I really liked this Javscript FizzBuzz can it be as nice in Python?

2019-04-04 Thread Sayth Renshaw
I saw this fizzbuzz in Eloquent Javascript and thought its really nice. Not all 
the usual if else version, just if.

for (let n = 1; n <= 100; n++) {
  let output = "";
  if (n % 3 == 0) output += "Fizz";
  if (n % 5 == 0) output += "Buzz";
  console.log(output || n);
}

I can't quite get a nice version like this out.

This was my attempt to mimick it. I am sure Python can get it cleaner, but I 
had never thought about not doing if else for Fizzbuzz its just the way I did 
it.

n = range(100)
output = ""
for num in n:
if (num % 3 == 0): output += "Fizz"
if (num % 5 == 0): output += "Buzz"
print(output or num)   

Haven't quite got it. But is possible nice and succinct like the javascript 
version. Maybe lambda will do it, gonna try that.

Any unique mindlowing style FizzBuzz I would never have considered and can 
learn off?

Cheers

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