Re: [Tutor] Looking for some direction

2019-05-12 Thread David L Neil

On 12/05/19 10:57 AM, Alan Gauld via Tutor wrote:

On 11/05/2019 19:59, Cranky Frankie wrote:

...

1) For the IDE I'm most comfortable with Netbeans/Java,


In that case use Netbeans. I use Netbeans myself when working
with Java and have played with its Python implementation and
its OK. Personally for serious Python work I just use vim
and 3 consoles, but that's a matter of taste. but you should
minimise the number of learning points and learning a new IDE
while also learning OOP (and a GUI framework?) is just
adding extra work.



"3 consoles": what is the purpose of each?
(my first reaction stemmed from many editors including a built-in console)

--
Regards =dn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Collating date data from a csv file

2019-05-12 Thread David L Neil

Hi Dave,

I also volunteer to do PAT safety testing during my "20% time". 
Clambering around Snowdonia as a boy, I eschewed* the Rheilffordd yr 
Wyddfa/SMR in favor of shanks' pony...


* OK, I was made to...! For the good of my soul???


On 9/05/19 8:04 AM, Dave Hill wrote:
I have a csv file which details the results of equipment tests, I carry 
out PAT testing as a volunteer at a heriatge railway in N. Wales. I want 
to extract how many items were tested on each test day. So far I have 
generated a List of test dates, but I am now stalled at how to 
efficiently count numbers tested on each date.


Can I have a list of tuples, where one item is the date and the second 
the count?


or is there a better construct?

Thanks in advance,

Dave

For completeness, I have listed below an extract from a target file, 
where the 10 digit number is the UNIX timestamp


182 1515001232
 Toaster 13 2000 1
183 1515001259    Contact Grill 13 2000 1
245 1515001367
 3G Cube Adaptor 13 0 1
246 1515001396     13A IEC Lead 5 0 1
248 1515001415
 Worktop Light 3 30 1
420 1515001440
 Fly killer 0 0 1
424 1515001461
 Dairy fridge 13 0 1
427 1513277293    Fire 13 0 1
429 1515001489
 Toaster Avanti 13 0 1



When you say "target file", is this coming off the tester via a link 
cable to your PC, or are you capturing by hand to a spreadsheet?


A tactic which many people 'miss' is that a workbook may contain 
multiple spreadsheets, and that the data on one spreadsheet may be 
auto-magically 'copied' onto another. Thus if the above is data coming 
off the PAT into one spreadsheet, I would immediately create a more 
meaningful sheet, 'for human consumption', which has column headings and 
converts (re-formats) the timestamp into a readable date (as suggested 
elsewhere), but is otherwise pretty-much a direct copy. We now have a 
sheet used for data capture/computer processing and something separate 
(and prettier) as a report/presentation for people.


From the spec, above, we are only interested in the date. Remember that 
considering the whole timestamp only makes life confusing. So convert 
them (only) to dates. These can be strings because Python compares 
strings as easily as dates!  The time component could be retained if 
sequence (of testing) might be important.


The sad reality is that a daily count could be accomplished in either 
LO-Writer or MS-Excel. No reason why you shouldn't use Python though.


(Assuming that the data appears in (forward or reverse) date sequence) 
Read-in the data sheet/CSV file, row-by-row, taking note of the date of 
the first data-entry, and starting to count from one. Then increment for 
each row where the date matches. When the dates don't match, report, 
reset the counter, and note the new date.


How will you lay-out and present this report? Another spreadsheet? 
Screen? Paper?


When you say "count numbers tested on each date", the above method will 
let you know a (single) daily total of tests-performed.


Did you (also) mean that you want to track how many of tests were 
performed within categories of devices, eg how many toasters on the one 
day? In which case, further design consideration is required, eg which 
devices fit into which category and how to match "Toaster" with "Toaster 
Avanti"...


--
Regards =dn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread Alan Gauld via Tutor
On 12/05/2019 00:24, David L Neil wrote:

> "3 consoles": what is the purpose of each?
> (my first reaction stemmed from many editors including a built-in console)

One for vim,
One for the Python interpreter
One for an OS shell used for running/testing the app plus any
miscellaneous Unixy type things I need to do (sed/grep/awk/git etc).

I'll usually also have a web browser for reading
documentation if necessary, although that's mostly
done in the interpreter using dir()/help().

Alt-Tab and the X cut 'n paste mechanism provides
enough integration between windows.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Local variable look up outside the function and method

2019-05-12 Thread Arup Rakshit
In the following the function, x is reachable outside the scope of foo function.

In [1]: x = 10

In [2]: def foo():
   ...: return x
   ...:

In [3]: print(foo())
10

But why it is not the case when the look up happens inside a instance method of 
a class?

In [1]: class Foo:
   ...: x = 10
   ...: def bar(self):
   ...: return x
   ...:

In [2]: Foo().bar()
---
NameError Traceback (most recent call last)
 in 
> 1 Foo().bar()

 in bar(self)
  2 x = 10
  3 def bar(self):
> 4 return x

NameError: name 'x' is not defined

I figured I have to access it like:

In [1]: class Foo:
   ...: x = 10
   ...: def bar(self):
   ...: return self.__class__.x
   ...:
   ...: print(Foo().bar())
10

Just want to know how these search happens in 2 contexts.


Thanks,

Arup Rakshit
a...@zeit.io



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Local variable look up outside the function and method

2019-05-12 Thread Alan Gauld via Tutor
On 12/05/2019 14:04, Arup Rakshit wrote:

The first case x is a global variable - which in
Python really means module level.

In the second case you have placed it inside
Foo so you need to specify that that is where
it is located. Classes encapsulate their own
methods and attributes, that is one of their
purposes. and just as you can't access a method
without preceding it with self similarly you
can't access an attribute without providing
the object or class reference.


> I figured I have to access it like:
> 
> In [1]: class Foo:
>...: x = 10
>...: def bar(self):
>...: return self.__class__.x

Remember that any time you find yourself using
dunder techniques ask if there is an easier way.
There usually is...

You can just use

 return self.x

or, possibly better:

 return Foo.x

You know x is declared in the context of Foo so refer
to it directly. The self approach runs the risk of an
instance attribute of the same name being created and
overriding the class attribute. (But sometimes that's
what you want.)

If you had an inheritance hierarchy where x was defined
in multiple places the .__class__ approach would work
better (probably even be needed). But in most cases these
kinds of variables are defined once in the class and
you are safe to access it by name - and it makes the
code more readable and unambiguous.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Local variable look up outside the function and method

2019-05-12 Thread Martin A. Brown


Hello Arup,

> In the following the function, x is reachable outside the scope of foo 
> function.
> In [1]: x = 10
> In [2]: def foo():
>...: return x
> In [3]: print(foo())
> 10

> But why it is not the case when the look up happens inside a 
> instance method of a class?
> 
> In [1]: class Foo:
>...: x = 10
>...: def bar(self):
>...: return x
> In [2]: Foo().bar()
> ---
> NameError Traceback (most recent call last)
>  in 
> > 1 Foo().bar()
> 
>  in bar(self)
>   2 x = 10
>   3 def bar(self):
> > 4 return x
> 
> NameError: name 'x' is not defined
> 
> I figured I have to access it like:
> 
> In [1]: class Foo:
>...: x = 10
>...: def bar(self):
>...: return self.__class__.x
>...:
>...: print(Foo().bar())
> 10
> 
> Just want to know how these search happens in 2 contexts.

Probably the following is the best section of the documentation to 
read:

  https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables

Here's also a little sample that may help.  By the way, I wouldn't 
normally be doing things like "unset self.x" and creating those 
peculiar methods, but I thought it might be instructive in this 
case.  This is using the same sort of technique you were with your 
class called Foo.  (I don't know all of the reasons why to inherit 
from object, but I usually do...)

class Foo(object):
x = 10  # -- class variable

def __init__(self):
self.y = 20  # -- instance variable named 'y'

def setinstancex(self, x):
self.x = x

def removeinstancex(self):
del self.x

def report(self):
return (self.x, self.y)

if __name__ == '__main__':
f = Foo()
print('# -- f.x retrieves class value, f.y instance value')
print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x))
print()

f.setinstancex(3.14159)
print('# -- f.x retrieves instance value, f.y instance value')
print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x))
print()

print('# -- warning, we are about to change the value for Foo.x')
print()
Foo.x = 6.022141e-23

print('# -- f.x retrieves class value, f.y instance value')
print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x))
print()

f.removeinstancex()
print('# -- f.x retrieves class value, f.y instance value')
print('f.x = %s, f.y = %s, Foo.x = %s' % (f.x, f.y, Foo.x))
print()


Good luck,

-Martin

--
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread David L Neil

On 12/05/19 7:59 PM, Alan Gauld via Tutor wrote:

On 12/05/2019 00:24, David L Neil wrote:


"3 consoles": what is the purpose of each?
(my first reaction stemmed from many editors including a built-in console)


One for vim,


yes - that 'replaces' the window/app running an IDE/GUI-based editor.


*

One for the Python interpreter

*


One for an OS shell used for running/testing the app plus any
miscellaneous Unixy type things I need to do (sed/grep/awk/git etc).


Interestingly, I split these into two - my laziness for running/testing 
is 'Save, Alt-Tab, Up-arrow, Enter' which would be 'ruined' by using the 
cmdLN for anything else.


I might even 'raise' your bid, by adding a third terminal here - when 
needing to ssh into a VPS or other remote m/c.




I'll usually also have a web browser for reading
documentation if necessary, although that's mostly
done in the interpreter using dir()/help().


+1 (to make it 'worse', I have two web browsers open - plain-Firefox for 
'ordinary browsing' and the FF Development Edition for, well, 
development work. Yes, that habit probably came out of testing web stuff 
in multiple browsers, but now the DE is where I cruise the PSL docs, 
etc, as a de-facto separation-of-concerns in my mind.




Alt-Tab and the X cut 'n paste mechanism provides
enough integration between windows.


Very handy!


*
> One for the Python interpreter
*

I also do this, all-the-time. My youngest/new grad colleague observing 
this last week, was most puzzled. He felt it was 'double-handling' 
because "most ideas could/should be hashed-out in TDD design" cf 'code 
experimentation'. Will be interesting to see if he takes-on the idea, or 
continues to tease 'the old boy'...


(any comment if this is my failing to (fully) appreciate TDD philosophy?)


I'm using Gnome Terminal under Fedora (Linux). This allows multiple 
terminals in tabs (and thus Ctrl-Tab rapid-switching). However, it 
irritates me that whilst I can set "profiles" for particular purposes; 
there does not seem to be a way to save a 'session'. Thus each time 
Terminal re-starts, I have to re-build each terminal, manually.


(suggestions of other similar tools would be most welcome)


I'm also conscious of advising the OP on IDEs/editors: in that I am 
currently using Sublime Text but find the "build" mechanism quite alien 
to Python (HTML, CSS, JS, etc - but not Rust!). Similarly, I need to 
learn more about using ST's built-in terminal (preferably at full-size 
rather than a few lines at the bottom of the editing window). That might 
cut-down on one concurrent terminal...


(similarly, am open to suggestions for improving Python dev using ST)

Thanks for your feedback!
--
Regards =dn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Collating date data from a csv file

2019-05-12 Thread Dave Hill

Hi David,

Firstly, thank you for your reply.

One condition of my agreeing to undertake PAT was that I got a PAT 
machine that stored the data, as I am better at programming than paperwork!


I have a Megger PAT 420 which provides a data backup to a USB stick, and 
thence transfer to my laptop, which is network connected a dual HDD 
system (Buffalo), which automatically copies the main HDD to the slave HDD.


I found out by accident that the Megger PAT 420 data backup is actually 
an SQLite database, so that is my route for access. Having played with 
Python on Raspberry Pi's, I thought I would explore Python for data 
processing, and now, I have a set of programs which extract SQLite, to 
'csv' then collate/process this data and produce a multi-page ODS 
spreadsheet document, which lists tests by location. I also have an 
application which extracts appliances requiring testing within a  +/- 30 
day window, so I have a target for testing.


My biggest problem is locating kit, and keeping up with 
removals/disposals and new acquisitions, but the guys are getting a bit 
better at communication!


I thought it would be useful to the 'management' to have a handle on 
progress, and stats on tested/new/disposed, etc, hence the latest question.


I convert the datetimestamp to Gregorian ordinal date, as this is easier 
to use in accessing the resulting 'defaultdict', do the counting and 
then convert the date back to something comprehensible for writing to an 
ODS spreadsheet.


Having seen todays posts I am going to look at wxPython, as a front-end 
(and possibly display?)


Thank you for your consideration

Dave

On 12/05/2019 04:20, David L Neil wrote:

Hi Dave,

I also volunteer to do PAT safety testing during my "20% time". 
Clambering around Snowdonia as a boy, I eschewed* the Rheilffordd yr 
Wyddfa/SMR in favor of shanks' pony...


* OK, I was made to...! For the good of my soul???


On 9/05/19 8:04 AM, Dave Hill wrote:
I have a csv file which details the results of equipment tests, I 
carry out PAT testing as a volunteer at a heriatge railway in N. 
Wales. I want to extract how many items were tested on each test day. 
So far I have generated a List of test dates, but I am now stalled at 
how to efficiently count numbers tested on each date.


Can I have a list of tuples, where one item is the date and the 
second the count?


or is there a better construct?

Thanks in advance,

Dave

For completeness, I have listed below an extract from a target file, 
where the 10 digit number is the UNIX timestamp


182 1515001232
 Toaster 13 2000 1
183 1515001259    Contact Grill 13 2000 1
245 1515001367
 3G Cube Adaptor 13 0 1
246 1515001396     13A IEC Lead 5 0 1
248 1515001415
 Worktop Light 3 30 1
420 1515001440
 Fly killer 0 0 1
424 1515001461
 Dairy fridge 13 0 1
427 1513277293    Fire 13 0 1
429 1515001489
 Toaster Avanti 13 0 1



When you say "target file", is this coming off the tester via a link 
cable to your PC, or are you capturing by hand to a spreadsheet?


A tactic which many people 'miss' is that a workbook may contain 
multiple spreadsheets, and that the data on one spreadsheet may be 
auto-magically 'copied' onto another. Thus if the above is data coming 
off the PAT into one spreadsheet, I would immediately create a more 
meaningful sheet, 'for human consumption', which has column headings 
and converts (re-formats) the timestamp into a readable date (as 
suggested elsewhere), but is otherwise pretty-much a direct copy. We 
now have a sheet used for data capture/computer processing and 
something separate (and prettier) as a report/presentation for people.


From the spec, above, we are only interested in the date. Remember 
that considering the whole timestamp only makes life confusing. So 
convert them (only) to dates. These can be strings because Python 
compares strings as easily as dates!  The time component could be 
retained if sequence (of testing) might be important.


The sad reality is that a daily count could be accomplished in either 
LO-Writer or MS-Excel. No reason why you shouldn't use Python though.


(Assuming that the data appears in (forward or reverse) date sequence) 
Read-in the data sheet/CSV file, row-by-row, taking note of the date 
of the first data-entry, and starting to count from one. Then 
increment for each row where the date matches. When the dates don't 
match, report, reset the counter, and note the new date.


How will you lay-out and present this report? Another spreadsheet? 
Screen? Paper?


When you say "count numbers tested on each date", the above method 
will let you know a (single) daily total of tests-performed.


Did you (also) mean that you want to track how many of tests were 
performed within categories of devices, eg how many toasters on the 
one day? In which case, furth

[Tutor] What is this code doing? What is it?

2019-05-12 Thread Matthew Polack
We're beginners  trying to learn Python and have this sample code:

https://github.com/PySimpleGUI/PySimpleGUI/blob/master/ProgrammingClassExamples/Win10%20versions/2a.%20PSG%20(checkbox%20and%20radiobuttons)%20-%20Copy.py


There is a section of code that has this line::

result = str(' Cost: ' + '${:.2f}'.format(cost))

But I don't understamd what the curly brace part is actually doing:
{:}

'${:.2f}'

I think the 2f means to use 2 decimal places...but what does the rest of
this do?

..curly braces apparenly are for dictionaries...but I don't get how this is
a dictionary..or what this {:} command is actually doing?

Thanks for any clues or links to an easy to understand tutorial or
something on this feature.

- Matt

-- 
**Disclaimer: *Whilst every attempt has been made to ensure that material 
contained in this email is free from computer viruses or other defects, the 
attached files are provided, and may only be used, on the basis that the 
user assumes all responsibility for use of the material transmitted. This 
email is intended only for the use of the individual or entity named above 
and may contain information that is confidential and privileged. If you are 
not the intended recipient, please note that any dissemination, 
distribution or copying of this email is strictly prohibited. If you have 
received this email in error, please notify us immediately by return email 
or telephone +61 3 5382 2529** and destroy the original message.*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread Alan Gauld via Tutor
On 12/05/2019 10:15, David L Neil wrote:

> Interestingly, I split these into two - my laziness for running/testing 
> is 'Save, Alt-Tab, Up-arrow, Enter' which would be 'ruined' by using the 
> cmdLN for anything else.

In a bash shell I use Ctr-R (for reverse search) and hit py to
run the last python command.

So for me its
Alt-Tab, Cmd-R, py

2 characters extra and I get to use the OS for whatever I like in
between... :-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] cython and threads, and feedparser

2019-05-12 Thread nathan tech
Hello!

After a day of various attempts yesterday, I managed to get cython 
installed on my windows 10 machine.

Allow me to prefix this by saying, if there is somewhere else I should 
put this, II'M SORRY!


So I ran cython on my python project, and it worked fine, there was one 
error about an int, which I have put at the end of this document.

In summary though I don't think it has any relevance, might be me though.


Here's the problem.

In my program, when you load a new feed, the program launches a separate 
thread for using feedparser to download the rss feed, like this:

def loadfeed(feed):

  # do the feed loading code here

  # play a sound to say the feed was loaded


the loadfeed function is called through the threading module.

I put some print statements in, to establish what is going on in cython.

According to what I see, it launches the thread fine, it starts, and 
then it gets to this line:

     newfeed=feedparser.parse(url)

And just, stops.

I admit, it could be a speed problem, as I've not had the patients to 
let it run past about a minute, but in the normal, uncythonised python 
code, it runs in between 1 and 5 seconds, based on size of feed.

It doesn't error, it just acts like it has frozen.

The thread doesn't disappear, because the little status checker I have 
set up to keep an eye on threads within the program says it is still 
running, it just... seems not to.

I know this because, after the line above, I then have:

     print '2'

Which it never does.


I was wondering if there is a common cython... thing, I have missed 
here, where you have to compile feedparser separately? not at all? or 
something.

My worry is not speed, in this instance, 1 to 5 seconds is fine, all 
things considered and it being in a separate thread... I'm unworried at 
those timings.

My aim with cython was a simple, but nice enough, layer of protection

That being said, in my research I had read about the gil? Or something 
similar to do with only one python thread running at any one time in cython?

I admit to not understanding this, though.


Any explanations anyone can offer here would be greatly appreciated.

Thank you very much.

A confused Nate.


Compilation:

running build_ext
cythoning rss.py to rss.c
building 'rss' extension
C:\Users\natha\AppData\Local\Programs\Common\Microsoft\Visual C++ for 
Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG 
-Ic:\python2732\include -Ic:\python2732\PC /Tcrss.c 
/Fobuild\temp.win32-2.7\Release\rss.obj /openmp
rss.c
rss.c(6931) : warning C4047: '=' : 'int' differs in levels of 
indirection from 'PyObject *'
C:\Users\natha\AppData\Local\Programs\Common\Microsoft\Visual C++ for 
Python\9.0\VC\Bin\link.exe /DLL /nologo /INCREMENTAL:NO 
/LIBPATH:c:\python2732\libs /LIBPATH:c:\python2732\PCbuild 
/LIBPATH:c:\python2732\PC\VS9.0 /EXPORT:initrss 
build\temp.win32-2.7\Release\rss.obj "/OUT:C:\Users\natha\Dropbox\coding 
projects\python\luna rss\rss.pyd" 
/IMPLIB:build\temp.win32-2.7\Release\rss.lib 
/MANIFESTFILE:build\temp.win32-2.7\Release\rss.pyd.manifest
    Creating library build\temp.win32-2.7\Release\rss.lib and object 
build\temp.win32-2.7\Release\rss.exp
C:\Users\natha\AppData\Local\Programs\Common\Microsoft\Visual C++ for 
Python\9.0\WinSDK\Bin\mt.exe -nologo -manifest 
build\temp.win32-2.7\Release\rss.pyd.manifest 
"-outputresource:C:\Users\natha\Dropbox\coding projects\python\luna 
rss\rss.pyd;2"

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Collating date data from a csv file

2019-05-12 Thread Alan Gauld via Tutor
On 12/05/2019 11:27, Dave Hill wrote:

> I found out by accident that the Megger PAT 420 data backup is actually 
> an SQLite database, so that is my route for access. Having played with 
> Python on Raspberry Pi's, I thought I would explore Python for data 
> processing, and now, I have a set of programs which extract SQLite, to 
> 'csv' then collate/process this data and produce a multi-page ODS 
> spreadsheet document, which lists tests by location. I also have an 
> application which extracts appliances requiring testing within a  +/- 30 
> day window, so I have a target for testing.

You could do it all in native SQLite SQL of course.
You can tell  sqlite to output its results in Excel
CSV format and to a file rather than (or in addition to)
stdout.

So you can write a standard query and have it generate
your Excel readable file directly.

You can then automate that as a batch job in the OS...
Assuming you run the same reports regularly.

Much as I love Python sometimes the native tools are
even better...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is this code doing? What is it?

2019-05-12 Thread Alan Gauld via Tutor
On 12/05/2019 12:31, Matthew Polack wrote:

> result = str(' Cost: ' + '${:.2f}'.format(cost))
> 
> But I don't understamd what the curly brace part is actually doing:

> ..curly braces apparenly are for dictionaries...but I don't get how this is
> a dictionary..or what this {:} command is actually doing?

Curly braces inside a string are nothing to do with dictionaries or
sets(which also use them).
They are placeholders for inserted data using the format() string
method. Search the docs for string formatting, you will find a page on
the subject with many examples. (or you could read the "Simple
Sequences" topic in my tutorial - about half way down - see below)

There is another, older style of formatting that uses % characters
instead of {}. You will see both styles used regularly in Python
code. Some folks prefer one, some the other.

They are both very powerful ways of constructing output strings with
data inserted. {} and format() has a few extra tricks once you get
into advanced uses, but % style does most of the same things (and
has the advantage of being used in other languages too so you only
need to remember one style!).


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Local variable look up outside the function and method

2019-05-12 Thread boB Stepp
On Sun, May 12, 2019 at 8:05 AM Arup Rakshit  wrote:
>
> In the following the function, x is reachable outside the scope of foo 
> function.
>
> In [1]: x = 10
>
> In [2]: def foo():
>...: return x
>...:
>
> In [3]: print(foo())
> 10

To what the others have said I wish to point out that if x is
reassigned _inside the function_ x's scope is now local inside the
function while x also still has a module scope. Consider:

3.6.8:  x = 10
3.6.8:  def foo():
... x = 5
... print("In foo x =", x)
...
3.6.8:  foo()
In foo x = 5
3.6.8:  print("In module scope, x =", x)
In module scope, x = 10


boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread boB Stepp
On Sun, May 12, 2019 at 1:05 PM David L Neil
 wrote:

> I'm using Gnome Terminal under Fedora (Linux). This allows multiple
> terminals in tabs (and thus Ctrl-Tab rapid-switching). However, it
> irritates me that whilst I can set "profiles" for particular purposes;
> there does not seem to be a way to save a 'session'. Thus each time
> Terminal re-starts, I have to re-build each terminal, manually.
>
> (suggestions of other similar tools would be most welcome)

I may be mistaken, but I think that a terminal multiplexer like tmux
(https://github.com/tmux/tmux/wiki) is capable of session management.
I have no personal use of tmux, but have been intrigued enough about
others referring to it that eventually I will get around to seriously
checking it out.

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread boB Stepp
On Sun, May 12, 2019 at 5:19 PM boB Stepp  wrote:
>
> On Sun, May 12, 2019 at 1:05 PM David L Neil
>  wrote:
>
> > I'm using Gnome Terminal under Fedora (Linux). This allows multiple
> > terminals in tabs (and thus Ctrl-Tab rapid-switching). However, it
> > irritates me that whilst I can set "profiles" for particular purposes;
> > there does not seem to be a way to save a 'session'. Thus each time
> > Terminal re-starts, I have to re-build each terminal, manually.
> >
> > (suggestions of other similar tools would be most welcome)
>
> I may be mistaken, but I think that a terminal multiplexer like tmux
> (https://github.com/tmux/tmux/wiki) is capable of session management.
> I have no personal use of tmux, but have been intrigued enough about
> others referring to it that eventually I will get around to seriously
> checking it out.

Actually, tmux is starting to look more and more interesting.  David,
I think you might this helpful.  I am currently looking at an
introduction to tmux by a Stack Overflow developer.  This article is
at https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/
(There was a link to this on the tmux wiki I sent out a link to
earlier.)  I think I may start playing around with this!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread Cameron Simpson

On 12May2019 17:19, boB Stepp  wrote:

On Sun, May 12, 2019 at 1:05 PM David L Neil
 wrote:

I'm using Gnome Terminal under Fedora (Linux). This allows multiple
terminals in tabs (and thus Ctrl-Tab rapid-switching). However, it
irritates me that whilst I can set "profiles" for particular purposes;
there does not seem to be a way to save a 'session'. Thus each time
Terminal re-starts, I have to re-build each terminal, manually.

(suggestions of other similar tools would be most welcome)


I may be mistaken, but I think that a terminal multiplexer like tmux
(https://github.com/tmux/tmux/wiki) is capable of session management.
I have no personal use of tmux, but have been intrigued enough about
others referring to it that eventually I will get around to seriously
checking it out.


Tmux is great, but I mostly use it for persistent sessions. It has 
facilities for running multiple panes in one terminal, which could be 
useful for remote sessions; locally I just use multiple panes in my 
terminal emulator, and in fact just multiple local panes connected to 
remote sessions anyway.


I do use tmux panes in email though: I invoke mutt in a tmux session and 
replies start in a subpane, with mutt's index view in the upper 
(smaller) pane. But otherwise it is almost all persistent (and named) 
sessions.


Now, I am spoilt: on a Mac I have access to iTerm3, which does: multiple 
tabs _and multiple panes and subpanes. My usual coding layout is a 
terminal in the left half of the screen running vim (with, usually, two 
_vim_ windows in it, vertically split). Often, that window gets a 
horizontal split, with a short and wide terminal pane below the editors, 
where I have a shell. The right hand half is antoher terminal, usually 
split intovarious panes, often 2 more evenly split. FOr shells and 
Python interpreters.


But a really good terminal emulator is an outstanding tool. And iTerm3 
seems to outshine everything else (Mac only alas - I'd like to find a 
food X11 equivalent - everything I've tried is deficient).  Tabs and 
multiple panes is hugely flexible.  It also has a toggle keystroke to 
expand a particular pane to the full window for when you want lots of 
text (diffs, commiting, etc).


So unlike Alan, since it is all one Mac app (iTerm3), there's no Alt-TAB 
to switch apps, and since it does focus-follows-mouse cut/paste is very 
fast (iTerm3 has a mode like most X11 apps: select implicitly copies, so 
no Cmd-C copy keystroke for me either).


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for some direction

2019-05-12 Thread Alan Gauld via Tutor
On 12/05/2019 23:19, boB Stepp wrote:

> I may be mistaken, but I think that a terminal multiplexer like tmux
> (https://github.com/tmux/tmux/wiki) is capable of session management.
> I have no personal use of tmux, but have been intrigued enough about
> others referring to it that eventually I will get around to seriously
> checking it out.

Me too. its been on my "must check that out" list for years!
But I really must


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is this code doing? What is it?

2019-05-12 Thread eryk sun
On 5/12/19, Alan Gauld via Tutor  wrote:
>
> They are both very powerful ways of constructing output strings with
> data inserted. {} and format() has a few extra tricks once you get
> into advanced uses, but % style does most of the same things (and
> has the advantage of being used in other languages too so you only
> need to remember one style!).

IMHO, given the choice of learning only one, learn the newer
curly-brace string formatting system. It's extensible since it's based
on a __format__ special method that a type (i.e. class) can override.

Take the Decimal type, for instance. By default it supports 28 digits
of precision. For example:

>>> from decimal import Decimal
>>> n = Decimal('0.12340567891234056789123405669') * 10
>>> n
Decimal('1.234056789123405678912340567')

Decimal implements the __format__ special method with its own
implementation of the "f" format specifier, which faithfully renders
the value as a string.

>>> '{:0.27f}'.format(n)
'1.234056789123405678912340567'

We can also use this directly with the built-in format() function in Python 3:

>>> format(n, '0.27f')
'1.234056789123405678912340567'

On the other hand, if we use a string's percent formatting, its "f"
format specifier has a fixed implementation that first converts a
number to a Python float, as opposed to delegating the string
conversion to the object itself.

>>> float(n)
1.2340567891234058
>>> '%0.27f' % n
'1.234056789123405772912178691'

As you can see in the formatted output, everything after
"1.234056789123405" is wrong. This is because a CPython float is
implemented as a C double-precision value, which only has about 16
decimal digits of precision.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor