Re: [Tutor] Only appending one object to list, when I am expecting more than 1

2019-02-27 Thread Alex Kleider

On 2019-02-27 05:25, AdamC wrote:
That's great - bug found. Thanks. However the next question is, how do 
I
create an instance of a class with variable parameters (i.e. with 
dateAdded
already computed and stored, or with dateAdded created for the first 
time)?


Might this work?

class myClass(object):
  __init__(self, added_date=None):
if added_date:
self.date_attribute = added_date
else:
self.date_attribute = my_way_of_calculating_added_date()

You can then instantiate in either of these ways:

my_instance = myClass()

or

my_instance = myClass(my_way_of_calculating_added_date())
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread boB Stepp
On Wed, Feb 27, 2019 at 6:50 PM Chip Wachob  wrote:
>
> Hello again,
>
> As always, this list has been very helpful, and thank you.
>
> So, I thought I was good to go until I attempted to run my code on a
> Windows 7 vintage machine this morning.  The application is intended to be
> run in the command window in Windows, from the terminal in Linux...
>
> In the code I had included the ANSI escape characters so I could invert the
> text and change the color.  Basically, I wanted to make the warning / error
> messages stand out from the crowd.

As I have been on a "curses" kick lately, I wonder if it would work
for you?  Of course this means another module, but it is in the
standard lib on all non-Windows Python versions.  For Windows I found
this thread on stackoverflow:

https://stackoverflow.com/questions/32417379/what-is-needed-for-curses-in-python-3-4-on-windows7

The checked answer gives a link to binaries for Windows, which seems
to support all Python versions through 3.7, including 2.7.

Just a thought...

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


Re: [Tutor] Why does using "window.addchr()" to place a character at the lower right corner raise an exception?

2019-02-27 Thread boB Stepp
On Wed, Feb 27, 2019 at 8:09 PM Alex Kleider  wrote:
>
> On 2019-02-27 17:48, boB Stepp wrote:
> > Under https://docs.python.org/3/library/curses.html#window-objects in
> > the curses docs, it states:
> >
> > 
> > window.addch(ch[, attr])
> > window.addch(y, x, ch[, attr])
> > [...]
> >
> > Note
> >
> > Writing outside the window, subwindow, or pad raises a curses.error.
> > Attempting to write to the lower right corner of a window, subwindow,
> > or pad will cause an exception to be raised after the character is
> > printed.
>
> Could it be that as soon as the character is printed the cursor moves to
> the 'next location' which is outside of the 'window, subwindow, or pad'
> and it is this which causes the error to be raised?

Alex, I think you have nailed it!  Everything I have read so far
indicates that an exception will be thrown whenever attempting to
write outside of a window, subwindow or pad.  So it would appear to
follow that if for some reason I need to write something to that very
last cell I will have to handle the exception generated.

Thanks!


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


Re: [Tutor] Why does using "window.addchr()" to place a character at the lower right corner raise an exception?

2019-02-27 Thread Alex Kleider

On 2019-02-27 17:48, boB Stepp wrote:

Under https://docs.python.org/3/library/curses.html#window-objects in
the curses docs, it states:


window.addch(ch[, attr])
window.addch(y, x, ch[, attr])
[...]

Note

Writing outside the window, subwindow, or pad raises a curses.error.
Attempting to write to the lower right corner of a window, subwindow,
or pad will cause an exception to be raised after the character is
printed.


Could it be that as soon as the character is printed the cursor moves to 
the 'next location' which is outside of the 'window, subwindow, or pad' 
and it is this which causes the error to be raised?

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


Re: [Tutor] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread eryk sun
On 2/27/19, Alan Gauld via Tutor  wrote:
> On 27/02/2019 18:28, Chip Wachob wrote:
>> Windows 7 vintage machine this morning.
>
> Caveat: I have no direct experience on Windows 7 - I jumped
> from XP to Windows 8... Also I haven't used ANSIO codes in
> Windows since the heady days of Windows 98! But...
>
>> run in the command window in Windows, from the terminal in Linux...
>
> Be aware that ANSI codes in DOS don't correspond directly
> with the VT100 control codes commonly used in Linux etc.
> You might get some things that either don;t work or work
> slightly differently.
>
>> In the code I had included the ANSI escape characters so I could invert
>> the
>> text and change the color.
>
> Again remember that old ANSI terminals didn't have colors.
> (They were either green on black, white on black, or if you
> were really fancy, amber on black).
>
> Color came later so some terminal emulators won't do anything
> with those codes.
>
>> As I have now learned, Windows 7 does not support this functionality.
>> What do the experts out there suggest as the path of least pain and
>> suffering?
>
> No idea if this works on Windows 7 but what we used to have
> to do was load the ANSI.SYS driver in the CONFIG.SYS file
> located in the root directory of the boot drive.

Windows NT systems use the Windows console API, which talks to a
console that's hosted in a system process. In Windows 7+, each console
is hosted in an instance of conhost.exe. In older versions of NT,
consoles are hosted by threads in the session server process,
csrss.exe.

In 32-bit builds of NT, there's integration between the console and
the virtual DOS machine (ntvdm.exe). This allows running old 16-bit
DOS programs/drivers such as COMMAND.COM and ANSI.SYS in an emulated
DOS environment. 64-bit builds no longer support NTVDM. Anyway, it
wouldn't apply to Windows console applications such as python.exe,
which have nothing to do with DOS.

The only practical option is to implement VT sequences using the
console API, such as is done by the colorama package. Other options
require installing an alternate console (e.g. ConEmu) or a program
that hacks the console API in each process via DLL injection (e.g.
ANSICON).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Why does using "window.addchr()" to place a character at the lower right corner raise an exception?

2019-02-27 Thread boB Stepp
Under https://docs.python.org/3/library/curses.html#window-objects in
the curses docs, it states:


window.addch(ch[, attr])
window.addch(y, x, ch[, attr])
[...]

Note

Writing outside the window, subwindow, or pad raises a curses.error.
Attempting to write to the lower right corner of a window, subwindow,
or pad will cause an exception to be raised after the character is
printed.


Why is this?  What is special about the lower right corner of a
terminal window?  I am guessing this is some relic of the original
terminal era.

TIA!

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


Re: [Tutor] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread eryk sun
On 2/27/19, Chip Wachob  wrote:
>
> I've been spending the morning looking at different solutions.  Many of
> them seem to involve either including yet aother module, or, worse (IMHO) a
> C library that will 'emulate' the ANSI escape sequences, API calls, etc.

To support virtual-terminal sequences prior to Windows 10, you'll need
a package such as colorama [1] that implements VT sequences in terms
of the Windows console API -- unless you're willing to require an
alternate console such as ConEmu or an API hack such as ANSICON.

[1]: https://pypi.org/project/colorama
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread Alan Gauld via Tutor
On 27/02/2019 18:28, Chip Wachob wrote:
> Windows 7 vintage machine this morning. 

Caveat: I have no direct experience on Windows 7 - I jumped
from XP to Windows 8... Also I haven't used ANSIO codes in
Windows since the heady days of Windows 98! But...

> run in the command window in Windows, from the terminal in Linux...

Be aware that ANSI codes in DOS don't correspond directly
with the VT100 control codes commonly used in Linux etc.
You might get some things that either don;t work or work
slightly differently.

> In the code I had included the ANSI escape characters so I could invert the
> text and change the color. 

Again remember that old ANSI terminals didn't have colors.
(They were either green on black, white on black, or if you
were really fancy, amber on black).

Color came later so some terminal emulators won't do anything
with those codes.

> As I have now learned, Windows 7 does not support this functionality.
> What do the experts out there suggest as the path of least pain and
> suffering?

No idea if this works on Windows 7 but what we used to have
to do was load the ANSI.SYS driver in the CONFIG.SYS file
located in the root directory of the boot drive.

A quick Google suggests that Windows supports config.nt
as a replacement for CONFIG.SYS.

Try creating such a file (or editing the existing one) with the line

DEVICE=ANSI.SYS

And see if that helps. (You might need a reboot or at
least a restart of the console)

Also it seems that the 32 bit version of ANSI.SYS does not support
cursor positioning. But if you only want bold/flashing/underlined
text in differing colors you should be OK...


-- 
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] ANSI / VT100 Escape Codes in Windows 7 Environment

2019-02-27 Thread Chip Wachob
Hello again,

As always, this list has been very helpful, and thank you.

So, I thought I was good to go until I attempted to run my code on a
Windows 7 vintage machine this morning.  The application is intended to be
run in the command window in Windows, from the terminal in Linux...

In the code I had included the ANSI escape characters so I could invert the
text and change the color.  Basically, I wanted to make the warning / error
messages stand out from the crowd.

Eg:
# there's an error in the loop, report it
# format reverse with yellow background
print "\033[7m\033[33m"
print " ERROR IN LOOP SIZE"
.
.
.
# clear formatting
print "\033[m"

As I have now learned, Windows 7 does not support this functionality.
Development had been done on a Windows 10 (and Ubuntu) machine where this
worked just fine.

I've been spending the morning looking at different solutions.  Many of
them seem to involve either including yet aother module, or, worse (IMHO) a
C library that will 'emulate' the ANSI escape sequences, API calls, etc.

Before I go tearing into my currently working code, I'm looking for
feedback from those much more experienced.

I need this code to run correctly on both Windows 7 and 10 (future Linux)
and I'm stuck with Python 2.7 due to other module limitations.

What do the experts out there suggest as the path of least pain and
suffering?

Again, thank you for your time and guidance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Only appending one object to list, when I am expecting more than 1

2019-02-27 Thread Alan Gauld via Tutor
On 27/02/2019 13:25, AdamC wrote:
> That's great - bug found. Thanks. However the next question is, how do I
> create an instance of a class with variable parameters (i.e. with dateAdded
> already computed and stored, or with dateAdded created for the first time)?
> 
> I hope that makes sense.

Not really.
There are several possible interpretations of your question.

When asking concept things it often helps to include some
hypothetical examples of what you would like to happen.
For example in this case you might say you want:

c = myClass()  # default
c2 = myClass(anInt,aString)  # two args
c3 = myClass(aDate,aFloat)   # two different args
etc.

Or you maybe wanted something else? If so show
us how you think it would work and we can then
show you how to get something close.

-- 
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] Only appending one object to list, when I am expecting more than 1

2019-02-27 Thread Mats Wichmann
Without spending a lot of time on an answer, you can use default arguments in 
the cases where only the number of arts differs in your instance creation. If 
there's larger differences, look into @classmethod.

On February 27, 2019 5:25:02 AM PST, AdamC  wrote:
>That's great - bug found. Thanks. However the next question is, how do
>I
>create an instance of a class with variable parameters (i.e. with
>dateAdded
>already computed and stored, or with dateAdded created for the first
>time)?
>
>I hope that makes sense.
>
>Adam
>
>On Tue, 26 Feb 2019 at 18:24, Tobiah  wrote:
>
>>
>>
>> On 2/26/19 6:39 AM, AdamC wrote:
>> > Sorry folks - my code didn't work due to my debug var count to
>ensure
>> that
>> > I was looping properly.
>> >
>> > It should be:
>>
>> As was pointed out, the problem is not in your code.  It's in your
>> data.  You only have one record with a proper 'tpe' value, so that's
>> all you get in your media list.
>>
>>
>> Toby
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>-- 
>--
>You back your data up on the same planet?
>http://www.monkeez.org
>PGP key: 0x7111B833
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Only appending one object to list, when I am expecting more than 1

2019-02-27 Thread Tobiah

Without fully understanding what you're getting at, I'll offer this:

class Car(object):

def __init__(self, **kwargs):

self.features = {
'make': 'Hyundai',
'color': 'purple'
}

self.features.update(kwargs)


c = Car(color='yellow', year=2017)


print c.features

output:

{'color': 'yellow', 'make': 'Hyundai', 'year': 2017}





On 2/27/19 5:25 AM, AdamC wrote:

That's great - bug found. Thanks. However the next question is, how do I
create an instance of a class with variable parameters (i.e. with dateAdded
already computed and stored, or with dateAdded created for the first time)?

I hope that makes sense.

Adam

On Tue, 26 Feb 2019 at 18:24, Tobiah  wrote:




On 2/26/19 6:39 AM, AdamC wrote:

Sorry folks - my code didn't work due to my debug var count to ensure

that

I was looping properly.

It should be:


As was pointed out, the problem is not in your code.  It's in your
data.  You only have one record with a proper 'tpe' value, so that's
all you get in your media list.


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





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


Re: [Tutor] I flip a coin 1 million times what is the consecutive times it will come up head and come up tails

2019-02-27 Thread Abdur-Rahmaan Janhangeer
XD that will help in the future, yes it was not unreadable this one.

Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I flip a coin 1 million times what is the consecutive times it will come up head and come up tails

2019-02-27 Thread Steven D'Aprano
On Tue, Feb 26, 2019 at 01:34:33PM -0700, shaef...@q.com wrote:

> I am learning python.  I wanted to test my ability by making a program to
> keep track of the flip of a coin to find how many consecutive times it came
> up heads and tails.

Here's how to count the number of times it comes up heads (1) or tails 
(0):

from collections import Counter
from random import randint
c = Counter(randint(0, 1) for i in range(100))
print(c)

Obviously when you do it you will get different results, but here's 
mine:

Counter({1: 500481, 0: 499519})


If you want to count how many *consecutive* heads or tails happen, we 
can do this:

from itertools import groupby
it = groupby(randint(0, 1) for i in range(100))
c = Counter(len(tuple(x[1])) for x in it)
print(c)


When I run it, I get this:

Counter({1: 250180, 2: 124648, 3: 62760, 4: 31075, 5: 15707, 6: 7998, 7: 
3778, 8: 1966, 9: 989, 10: 501, 11: 219, 12: 136, 13: 49, 14: 22, 15: 
11, 16: 6, 18: 4, 17: 1})


which tells me that there are 250180 sequences of one head or one tail; 
124648 sequences of two heads or two tails; 62760 sequences of three 
heads or three tails, and so on to 4 sequences of 18 heads or tails.

If you want to see what is going on a bit better:

it = groupby(randint(0, 1) for i in range(20)) # a bit smaller...
for k, x in it:
t = tuple(x)
print("got", len(t), "consecutive", ("heads" if k==1 else "tails"), t)


which when I ran it gave me:

got 1 consecutive heads (1,)
got 1 consecutive tails (0,)
got 4 consecutive heads (1, 1, 1, 1)
got 3 consecutive tails (0, 0, 0)
got 1 consecutive heads (1,)
got 1 consecutive tails (0,)
got 7 consecutive heads (1, 1, 1, 1, 1, 1, 1)
got 1 consecutive tails (0,)
got 1 consecutive heads (1,)




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


Re: [Tutor] Only appending one object to list, when I am expecting more than 1

2019-02-27 Thread AdamC
That's great - bug found. Thanks. However the next question is, how do I
create an instance of a class with variable parameters (i.e. with dateAdded
already computed and stored, or with dateAdded created for the first time)?

I hope that makes sense.

Adam

On Tue, 26 Feb 2019 at 18:24, Tobiah  wrote:

>
>
> On 2/26/19 6:39 AM, AdamC wrote:
> > Sorry folks - my code didn't work due to my debug var count to ensure
> that
> > I was looping properly.
> >
> > It should be:
>
> As was pointed out, the problem is not in your code.  It's in your
> data.  You only have one record with a proper 'tpe' value, so that's
> all you get in your media list.
>
>
> Toby
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


-- 
--
You back your data up on the same planet?
http://www.monkeez.org
PGP key: 0x7111B833
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I flip a coin 1 million times what is the consecutive times it will come up head and come up tails

2019-02-27 Thread Alan Gauld via Tutor
On 27/02/2019 09:35, Abdur-Rahmaan Janhangeer wrote:
> maybe try dpaste.de or something like that for code pasting ^^_

That can be useful if it's very long code but this is
actually quite a short program and it's much easier to
reply to if the code is in the message so I think the
OP did the right thing in this case.

-- 
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] I flip a coin 1 million times what is the consecutive times it will come up head and come up tails

2019-02-27 Thread Abdur-Rahmaan Janhangeer
maybe try dpaste.de or something like that for code pasting ^^_

Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor