Re: [Tutor] Q about .join() Thanks!

2017-02-14 Thread Peter Otten
Danny Yoo wrote:

> Moreover, most implementations *deliberately* randomize their iteration
> order to avoid a particular kind of hash collision attack out there in
> the wild.  See:
 
In CPython the hash() of a string may change between different runs to fend 
off hash collision attacks, but that does not necessarily change the order 
of iteration:

In Python 3.4 for example both order and hash value change

$ python3.4 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
-1599197652882818545 baz bar foo
$ python3.4 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
-7773300350121034240 foo bar baz
$ python3.4 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
4096077922251392823 baz bar foo

In Python 3.6 on the other side the hash values still change, but 
(insertion) order is preserved:

$ python3.6 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
22453670082131454 foo bar baz
$ python3.6 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
4601604916521659267 foo bar baz
$ python3.6 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
5190466601789110813 foo bar baz

So if the OP had used Python 3.6 she would have seen the behaviour she 
expected. However, quoting 


"""
The order-preserving aspect of this new [dict] implementation is considered 
an implementation detail and should not be relied upon
"""

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


[Tutor] NameError: name 'hurst' is not defined

2017-02-14 Thread Allan Tanaka via Tutor
Hi. Not sure why this code produces the error like this. This error appears 
when i run the code of print "Hurst(GBM):   %s" % hurst(gbm): 
Traceback (most recent call last):  File "", line 1, in     
print "Hurst(GBM):   %s" % hurst(gbm)NameError: name 'hurst' is not defined

Here is the full code:>>> import statsmodels.tsa.stattools as ts
>>> import urllib>>> from datetime import datetime>>> from pandas_datareader 
>>> import data, wb>>> from pandas_datareader.data import DataReader>>> goog = 
>>> DataReader("GOOG", "yahoo", datetime(2000,1,1), datetime(2017,1,1))>>> 
>>> ts.adfuller(goog['Adj Close'], 1>>> import numpy as np
>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract>>> from 
>>> numpy.random import randn>>> def hurst(ts): lags = range(2, 100) tau = 
>>> [np.sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags] poly = 
>>> np.polyfit(log(lags), log(tau), 1) return poly[0]*2.0>>> gbm = 
>>> log(cumsum(randn(10))+1000)>>> mr = log(randn(10)+1000)>>> tr = 
>>> log(cumsum(randn(10)+1)+1000)>>> print "Hurst(GBM):   %s" % hurst(gbm)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Q about .join() Thanks!

2017-02-14 Thread Joaquin Alzola
>I find out that the outcome for using .join() on a dictionary is totally 
>different than it using on list or string. for example,

 > >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
 > >>>
 > boy:good:doiido:hello
>So my question is why the outcome doesn't show sequentially as the same 
>sequence of the original seq4?  What pattern do those "keys" of the dictionary 
>in the outcome show ?

Dictionaries are not an ordered sequence.

In order to do that you need to create and OrderedDict (in collections)

Modern Dictionaries --> https://www.youtube.com/watch?v=p33CVV29OG8
The mighty Dictionary --> https://www.youtube.com/watch?v=C4Kc8xzcA68&t=142s
This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] NameError: name 'hurst' is not defined

2017-02-14 Thread Alan Gauld via Tutor
On 14/02/17 01:55, Allan Tanaka via Tutor wrote:
> Hi. Not sure why this code produces the error like this. This error appears 
> when i run the code of print "Hurst(GBM):   %s" % hurst(gbm): 
> Traceback (most recent call last):  File "", line 1, in   
>   print "Hurst(GBM):   %s" % hurst(gbm)NameError: name 'hurst' is not defined
> 
> Here is the full code:>>> import statsmodels.tsa.stattools as ts
 import urllib>>> from datetime import datetime>>> from pandas_datareader 
 import data, wb>>> from pandas_datareader.data import DataReader>>> goog = 
 DataReader("GOOG", "yahoo", datetime(2000,1,1), datetime(2017,1,1))>>> 
 ts.adfuller(goog['Adj Close'], 1>>> import numpy as np
 from numpy import cumsum, log, polyfit, sqrt, std, subtract>>> from 
 numpy.random import randn>>> def hurst(ts): lags = range(2, 100) tau = 
 [np.sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags] poly = 
 np.polyfit(log(lags), log(tau), 1) return poly[0]*2.0>>> gbm = 
 log(cumsum(randn(10))+1000)>>> mr = log(randn(10)+1000)>>> tr = 
 log(cumsum(randn(10)+1)+1000)>>> print "Hurst(GBM):   %s" % hurst(gbm)

The mail system has mangled your code, you need to post
in plain text to preserve layout. I'll try to reconstruct
it based on >>> as the interpreter prompt:

>>> import urllib
>>> from datetime import datetime
>>> from pandas_datareader import data, wb
>>> from pandas_datareader.data import DataReader
>>> goog = DataReader("GOOG", "yahoo", datetime(2000,1,1),
  datetime(2017,1,1))
>>> ts.adfuller(goog['Adj Close'], 1

there seems to be a missing closing  paren here?
Also where does 'ts' come from, its not defined above?

>>> import numpy as np
>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract
>>> from numpy.random import randn
>>> def hurst(ts):
  lags = range(2, 100)
  tau = [np.sqrt(std(subtract(ts[lag:], ts[:-lag])))
 for lag in lags]
  poly = np.polyfit(log(lags), log(tau), 1)
  return poly[0]*2.0
>>> gbm = log(cumsum(randn(10))+1000)
>>> mr = log(randn(10)+1000)
>>> tr = log(cumsum(randn(10)+1)+1000)
>>> print "Hurst(GBM):   %s" % hurst(gbm)

Is that right?
Apart from the missing ')' and undefined ts, it looks OK.
Have you tried putting it into a file and running it
as a script? That will avoid risk of pollution from
any previous context in the interpreter.


-- 
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] NameError: name 'hurst' is not defined

2017-02-14 Thread Peter Otten
Allan Tanaka via Tutor wrote:

> Hi. Not sure why this code produces the error like this. This error
> appears when i run the code of print "Hurst(GBM):   %s" % hurst(gbm):
> Traceback (most recent call last):  File "", line 1, in
> print "Hurst(GBM):   %s" % hurst(gbm)NameError: name 'hurst'
> is not defined
> 
> Here is the full code:>>> import statsmodels.tsa.stattools as ts
 import urllib>>> from datetime import datetime>>> from
 pandas_datareader import data, wb>>> from pandas_datareader.data import
 DataReader>>> goog = DataReader("GOOG", "yahoo", datetime(2000,1,1),
 datetime(2017,1,1))>>> ts.adfuller(goog['Adj Close'], 1>>> import numpy
 as np from numpy import cumsum, log, polyfit, sqrt, std, subtract>>>
 from numpy.random import randn>>> def hurst(ts): lags = range(2, 100)
 tau = [np.sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
 poly = np.polyfit(log(lags), log(tau), 1) return poly[0]*2.0>>> gbm =
 log(cumsum(randn(10))+1000)>>> mr = log(randn(10)+1000)>>> tr =
 log(cumsum(randn(10)+1)+1000)>>> print "Hurst(GBM):   %s" %
 hurst(gbm)

See that mess? You need to check your mail client. When I undo the 
scrambling...

$ cat hurst.py 
import statsmodels.tsa.stattools as ts
import urllib
from datetime import datetime
from pandas_datareader import data, wb
from pandas_datareader.data import DataReader

goog = DataReader("GOOG", "yahoo", datetime(2000,1,1), datetime(2017,1,1))
ts.adfuller(goog['Adj Close'], 1)

import numpy as np
from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn

def hurst(ts): 
lags = range(2, 100) 
tau = [np.sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
poly = np.polyfit(log(lags), log(tau), 1)
return poly[0]*2.0

gbm = log(cumsum(randn(10))+1000)
mr = log(randn(10)+1000)
tr = log(cumsum(randn(10)+1)+1000)

print "Hurst(GBM):   %s" % hurst(gbm)

...the code runs without error:

$ python hurst.py 
Hurst(GBM):   0.502272832664


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


Re: [Tutor] NameError: name 'hurst' is not defined

2017-02-14 Thread Alan Gauld via Tutor
On 14/02/17 10:11, Alan Gauld via Tutor wrote:

 ts.adfuller(goog['Adj Close'], 1
> 
> there seems to be a missing closing  paren here?
> Also where does 'ts' come from, its not defined above?

Ah! I see from Peters post that I missed the first line of code.
But I still see a missing ')'...

-- 
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] Exponential function

2017-02-14 Thread Aaliyah Ebrahim
Hi

For the function 1*10^x, is there a specific way of computing *10^x or will
it just be the following :

1*10**x

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


Re: [Tutor] Tutor Digest, Vol 156, Issue 33

2017-02-14 Thread Borisco Bizaro
Please l will like to have a mentor how can I have it
On Feb 14, 2017 09:53,  wrote:

> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
>1. Re: Access a .tcl file in python (Alan Gauld)
>2. Re: Q about .join()   Thanks! (David Rock)
>3. Re: Fwd: Re: GUI for ANSI colors (Alan Gauld)
>4. Re: Q about .join() Thanks! (Alan Gauld)
>5. Re: Q about .join() Thanks! (Danny Yoo)
>6. Re: Q about .join() Thanks! (Peter Otten)
>
>
> -- Forwarded message --
> From: Alan Gauld 
> To: tutor@python.org
> Cc:
> Date: Tue, 14 Feb 2017 01:17:39 +
> Subject: Re: [Tutor] Access a .tcl file in python
> On 13/02/17 19:37, Lily ANTONY wrote:
>
> > I have a .tcl file.Does anyone know how to load a .tcl file in to
> python?I
> > need to call the .tcl file in
> > a python program..Thanks!
>
> In general you can't run code for one language in a
> program written in another language. (There are a few
> exceptions, usually where one language is written in
> the other).
>
> The nearest you can usually get is to execute the
> Tcl program using the tcl interpreter (tclsh) from
> the OS using a module like subprocess.
>
> However, Tcl and Python do have a connection  via
> the Tkinter module and you can call tcl code
> using Tkinter like so(Python v3):
>
> import tkinter
> tcl = tkinter.Tcl()
> tcl.eval("""
> puts [expr 4+5]  # or any other arbitrary tcl
> """)
>
> So to execute a Tcl file you can read the contents
> into a string and then execute that string. Note that
> the return value will always be a string.
>
> HTH
>
> --
> 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
>
>
>
>
>
> -- Forwarded message --
> From: David Rock 
> To: "tutor@python.org" 
> Cc:
> Date: Mon, 13 Feb 2017 19:20:52 -0600
> Subject: Re: [Tutor] Q about .join() Thanks!
>
> > On Feb 13, 2017, at 12:34, SIJIA CHEN  wrote:
> >
> > I find out that the outcome for using .join() on a dictionary is totally
> different than it using on list or string. for example,
> >
>  seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>  print ':'.join(seq4)
> >  boy:good:doiido:hello
> > So my question is why the outcome doesn't show sequentially as the same
> sequence of the original seq4?  What pattern do those "keys" of the
> dictionary in the outcome show ?
>
> Dictionaries (in particular, their keys) are unordered.  You can not rely
> on them to be in a particular sequence.  The reason for this, from a
> practical perspective, is you are expected to ask for the specific key; you
> should not care about the ordering.
>
> Lists, on the other hand, have a specific order by design so will always
> be in the order they were created.
>
> What are you trying to do with join() on a dictionary in the first place?
> Is there a specific outcome you are trying to get?  It’s unlikely that
> using join on a dictionary is what you actually want.
>
> —
> David Rock
> da...@graniteweb.com
>
>
>
>
>
>
> -- Forwarded message --
> From: Alan Gauld 
> To: tutor@python.org
> Cc:
> Date: Tue, 14 Feb 2017 01:26:45 +
> Subject: Re: [Tutor] Fwd: Re: GUI for ANSI colors
> On 14/02/17 00:58, Alan Gauld forwarded:
>
> > red = '\033[91m'
> > yel = '\033[93m'
> > blu = '\033[34m'
> > grn = '\033[32m'
>
> These are indeed the ANSI codes for the colours but ANSI
> codes only work in an ANSI terminal and GUIs are not
> ANSI terminals. Instead they have their own ideas on
> colours and you need to use those. They all support
> the standard 16 colours used in ANSI however, so in
> your case (for Tkinter at least) just use the names
> 'red', 'yellow', 'blue' and 'green'
>
> > colors = [red, yel, blu, grn]
>
> colors = ['red', 'yellow', 'blue', 'green']
>
>
> > final_word = ''.join(choice(colors) + char for char in word)
>
> But again that won't work in a GUI, you need to use the GUIs
> way of colouring. In Tkinter that's via tags as per my
> previous post, reproduced here).
>
> > ###
> > from Tkinter import *
> >
> > n = 0
> > top = Tk()
> > t = Text(top, width=25, height=2)
> > t.pack()
> >
> > # set up tags for the colors
> > colors = ['red','blue','green']
> > for col in colors:
> > t.tag_config(col, foreground=col)
> >
> > def addWords():
> > n = 0
> > for word in "Hello ", "bright ", "world ":
> > t.insert(END,wo

Re: [Tutor] Exponential function

2017-02-14 Thread Alan Gauld via Tutor
On 14/02/17 12:03, Aaliyah Ebrahim wrote:

> For the function 1*10^x, is there a specific way of computing *10^x or will
> it just be the following :
> 
> 1*10**x

To compute it if you don't know x in advance then yes,
use something like

value = 10**x

But if you know the value in advance you can write it in
a more compact form as:

value = 1e5  # or 3e7 or whatever...

say(if x=5).

Experiment at the interactive prompt to see how it works.

-- 
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] Tutor Digest, Vol 156, Issue 33

2017-02-14 Thread Alan Gauld via Tutor
As per my offline reply, just send messages to the list and
the whole group will act as a virtual mentor.

One other thing though. Please start new topics with a new message.
Do NOT reply to an existing thread as it messes up the archives.
And especially do not reply to a digest message.

And use a meaningful subject line.

Its very hard to find a question in the archive if the
subject is

Re: Tutor Digest, Vol 156, Issue 33

or similar.

(unless of course you really are asking a question about
that particular digest message rather than its content
 - but if so it should probably go to the list owners.)

It's much easier to find a message with a subject like

help with python log functions

or whatever.

-- 
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] Exponential function

2017-02-14 Thread eryk sun
On Tue, Feb 14, 2017 at 11:01 PM, Alan Gauld via Tutor  wrote:
> To compute it if you don't know x in advance then yes,
> use something like
>
> value = 10**x
>
> But if you know the value in advance you can write it in
> a more compact form as:
>
> value = 1e5  # or 3e7 or whatever...

10**5 is an int and 1e5 is a float. This could lead to a significant
loss of precision depending on the calculation. For example:

>>> x = 10**5 * 1234567890123456789
>>> y = 1e5 * 1234567890123456789
>>> x - y
16777216.0

Replacing 10**5 with 10 is a compile-time optimization (constant
expression folding), so you needn't worry about manually optimizing
it. For example:

>>> compile('10**5', '', 'exec').co_consts
(10, 5, None, 10)

The compiled bytecode refers to the pre-computed constant:

>>> dis.dis(compile('10**5', '', 'exec'))
  1   0 LOAD_CONST   3 (10)
  3 POP_TOP
  4 LOAD_CONST   2 (None)
  7 RETURN_VALUE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor