Re: Does this dataframe look correct?

2020-06-29 Thread Peter Otten
Jim wrote:

> linux mint 19.3, python 3.6
> 
> I wrote a program to download stock info from yahoo using yfinance. I
> have been running it unchanged for the past 3 months, today it gave an
> error. When looping through a list of stocks the error is random, never
> the same position in the list.
> 
> I wrote the following little test script to show the error:
> 
> import yfinance as yf
> import pandas as pd
> day = '2020-06-25'
> aapl = yf.Ticker('AAPL')
> hist = aapl.history(start=day)
> print(hist)
> close = hist.loc[day]['Close']
> 
> I ran it 10 times 8 times I got a dataframe and 2 times I got the error
> shown below:
> 
> (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3
> /home/jfb/Dev/Python/test_yfinance.py
>OpenHigh Low   CloseVolume
> Date
> 
> 2020-06-25  360.70  365.00  357.57  364.84  34380600
> 2020-06-26  364.41  365.32  353.02  353.63  51270100
> 
> (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3
> /home/jfb/Dev/Python/test_yfinance.py
> Traceback (most recent call last):
>File "/home/jfb/Dev/Python/test_yfinance.py", line 13, in 
>  hist = aapl.history(start=day)
>File
> "/home/jfb/EVs/env36/lib/python3.6/site-packages/yfinance/base.py", line
> 155, in history
>  data = data.json()
>File
> "/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py",
> line 897, in json
>  return complexjson.loads(self.text, **kwargs)
>File
> "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/__init__.py",
> line 518, in loads
>  return _default_decoder.decode(s)
>File
> "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py",
> line 370, in decode
>  obj, end = self.raw_decode(s)
>File
> "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py",
> line 400, in raw_decode
>  return self.scan_once(s, idx=_w(s, idx).end())
> simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char
> 0)
> 
> I don't know pandas that well. My only contact with it is when a module
> I am using depends on it. So does the dataframe look correct?
> 
> The error complains of line 1, column 1. Just looking at the dataframe
> it looks like Date is on a different line from the rest of the headers
> or is that just the result of being printed in the terminal?
> 
> On the yfinance github issues page there were a few people reporting
> this error. A couple of people reported a work around using try/except.
> It worked for some people and not others. It didn't work for me.
> 
> I'd appreciate any advice you could give.

My guess is that pandas is not the source of the problem. The error occurs 
when simplejson tries to parse an empty string:

>>> import simplejson
>>> simplejson.loads("")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 488, in 
loads
return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in 
decode
obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 389, in 
raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 
0)

This probably means that yahoo returns an empty string instead of the 
expected JSON. If the error occurs only sporadically and you can identify 
the downloading code in the API source you can try and replace (pseudo-code)

json_data = download_from_yahoo()
df = dataframe_from_json(json_data)

with

while True:
json_data = download_from_yahoo()
if json_data: break
time.sleep(1)  # wait a moment, then retry download
df = dataframe_from_json(json_data)





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


Re: Solved: Re: Missing python curses functions?

2020-06-29 Thread Tony Flury via Python-list
Maybe you should raise a bug (bugs.python.org) and flag that this 
function is missing.


It could be that it can be introduced by whoever is maintaining the 
existing code.


On 20/05/2020 08:31, Alan Gauld via Python-list wrote:

On 19/05/2020 20:53, Alan Gauld via Python-list wrote:


One of the functions discussed that does not appear to have
a Python equivalent is attr_get() which gets the current
attributes.

OK, Using inch() I've written the following function:


def attr_get(win):
 """ return current window attributes.
 If a character exists at the bottom right position it will be lost!
 """
 y,x = win.getmaxyx() # how many lines in the window?
 win.insch(y-1,0,' ') # insert a space at bottom left
 ch = win.inch(y-1,0) # now read the char (including attributes)
 win.delch(y-1,0) # remove the char from window
 return ch

And it can be used like:

import curses
scr = curses.initscr()
# uncomment next line to test
# scr.attrset(curses.A_UNDERLINE)

atts = attr_get(scr)
if atts & cur.A_UNDERLINE:
 scr.addstr("Underline is on")
else:
 scr.addstr("Underline is off")

scr.refresh()
scr.getch()
curses.endwin()

Just in case its useful to anyone else...

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


i don't understand this python class

2020-06-29 Thread joseph pareti
I have piece of code with constructs as follows:

*class* *SentimentNetwork**:*

*def* __init__*(*self*,* reviews*,* labels*,* hidden_nodes *=* 10*,*
learning_rate *=* 0.1*):*



np*.*random*.*seed*(*1*)*



   self*.*init_network*(**len**(*self*.*review_vocab*),*hidden_nodes*,*
1*,* learning_rate*)*







*def* init_network*(*self*,* input_nodes*,* hidden_nodes*,* output_nodes
*,* learning_rate*):*

# Store the number of nodes in input, hidden, and output layers.

self*.*input_nodes *=* input_nodes

self*.*hidden_nodes *=* hidden_nodes

self*.*output_nodes *=* output_nodes

which makes me think about the redundant usage of* init_network:*

   1. as a method, AND
   2. as a property

So far I have only seen codes where the 2 things are separated, e.g. :

*import* insurance *as* ins

*class* *Vehicle**:*

*def* __init__*(*self*,* speed*,* make*):*

self*.*speed *=* speed

self*.*make *=* make

*class* *car**(*Vehicle*):*

*def* __init__*(*self*,* speed*,* make*):*

Vehicle*.*__init__*(*self*,* speed*,* make*)*

self*.*insurance *=* ins*.*calc*(*make*)*

*def* show_out*(*self*):*

*print**(*'vehicle is '*,*self*.*make*,*' insurance premium '*,*self
*.*insurance*)*

*def* claim*(*self*,* discount*):*

X *=* self*.*insurance *+* discount

*return* X


And hence I am not sure about the behavior of the first code in this email.
-- 
Regards,
Joseph Pareti - Artificial Intelligence consultant
Joseph Pareti's AI Consulting Services
https://www.joepareti54-ai.com/
cell +49 1520 1600 209
cell +39 339 797 0644
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: i don't understand this python class

2020-06-29 Thread Calvin Spealman
You are misreading the original example. `init_network` is defined as a
method on the class, and called in its initializer. There is no property
named "init_network".

On Mon, Jun 29, 2020 at 1:18 PM joseph pareti  wrote:

> I have piece of code with constructs as follows:
>
> *class* *SentimentNetwork**:*
>
> *def* __init__*(*self*,* reviews*,* labels*,* hidden_nodes *=* 10*,*
> learning_rate *=* 0.1*):*
>
>
>
> np*.*random*.*seed*(*1*)*
>
>
>
>self*.*init_network*(**len**(*self*.*review_vocab*),*hidden_nodes*,*
> 1*,* learning_rate*)*
>
>
>
>
>
>
>
> *def* init_network*(*self*,* input_nodes*,* hidden_nodes*,*
> output_nodes
> *,* learning_rate*):*
>
> # Store the number of nodes in input, hidden, and output layers.
>
> self*.*input_nodes *=* input_nodes
>
> self*.*hidden_nodes *=* hidden_nodes
>
> self*.*output_nodes *=* output_nodes
>
> which makes me think about the redundant usage of* init_network:*
>
>1. as a method, AND
>2. as a property
>
> So far I have only seen codes where the 2 things are separated, e.g. :
>
> *import* insurance *as* ins
>
> *class* *Vehicle**:*
>
> *def* __init__*(*self*,* speed*,* make*):*
>
> self*.*speed *=* speed
>
> self*.*make *=* make
>
> *class* *car**(*Vehicle*):*
>
> *def* __init__*(*self*,* speed*,* make*):*
>
> Vehicle*.*__init__*(*self*,* speed*,* make*)*
>
> self*.*insurance *=* ins*.*calc*(*make*)*
>
> *def* show_out*(*self*):*
>
> *print**(*'vehicle is '*,*self*.*make*,*' insurance premium
> '*,*self
> *.*insurance*)*
>
> *def* claim*(*self*,* discount*):*
>
> X *=* self*.*insurance *+* discount
>
> *return* X
>
>
> And hence I am not sure about the behavior of the first code in this email.
> --
> Regards,
> Joseph Pareti - Artificial Intelligence consultant
> Joseph Pareti's AI Consulting Services
> https://www.joepareti54-ai.com/
> cell +49 1520 1600 209
> cell +39 339 797 0644
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>

-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M: +1.336.210.5107
[image: https://red.ht/sig] 
TRIED. TESTED. TRUSTED. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Does this dataframe look correct?

2020-06-29 Thread Jim

On 6/29/20 2:16 AM, Peter Otten wrote:

Jim wrote:


linux mint 19.3, python 3.6

I wrote a program to download stock info from yahoo using yfinance. I
have been running it unchanged for the past 3 months, today it gave an
error. When looping through a list of stocks the error is random, never
the same position in the list.

I wrote the following little test script to show the error:

import yfinance as yf
import pandas as pd
day = '2020-06-25'
aapl = yf.Ticker('AAPL')
hist = aapl.history(start=day)
print(hist)
close = hist.loc[day]['Close']

I ran it 10 times 8 times I got a dataframe and 2 times I got the error
shown below:

(env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3
/home/jfb/Dev/Python/test_yfinance.py
OpenHigh Low   CloseVolume
Date

2020-06-25  360.70  365.00  357.57  364.84  34380600
2020-06-26  364.41  365.32  353.02  353.63  51270100

(env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3
/home/jfb/Dev/Python/test_yfinance.py
Traceback (most recent call last):
File "/home/jfb/Dev/Python/test_yfinance.py", line 13, in 
  hist = aapl.history(start=day)
File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/yfinance/base.py", line
155, in history
  data = data.json()
File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py",
line 897, in json
  return complexjson.loads(self.text, **kwargs)
File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/__init__.py",
line 518, in loads
  return _default_decoder.decode(s)
File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py",
line 370, in decode
  obj, end = self.raw_decode(s)
File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py",
line 400, in raw_decode
  return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char
0)

I don't know pandas that well. My only contact with it is when a module
I am using depends on it. So does the dataframe look correct?

The error complains of line 1, column 1. Just looking at the dataframe
it looks like Date is on a different line from the rest of the headers
or is that just the result of being printed in the terminal?

On the yfinance github issues page there were a few people reporting
this error. A couple of people reported a work around using try/except.
It worked for some people and not others. It didn't work for me.

I'd appreciate any advice you could give.


My guess is that pandas is not the source of the problem. The error occurs
when simplejson tries to parse an empty string:


import simplejson
simplejson.loads("")

Traceback (most recent call last):
   File "", line 1, in 
   File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 488, in
loads
 return _default_decoder.decode(s)
   File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in
decode
 obj, end = self.raw_decode(s)
   File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 389, in
raw_decode
 return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char
0)

This probably means that yahoo returns an empty string instead of the
expected JSON. If the error occurs only sporadically and you can identify
the downloading code in the API source you can try and replace (pseudo-code)

json_data = download_from_yahoo()
df = dataframe_from_json(json_data)

with

while True:
 json_data = download_from_yahoo()
 if json_data: break
 time.sleep(1)  # wait a moment, then retry download
df = dataframe_from_json(json_data)



Thanks, I have it working now by wrapping a for loop in a try/except. 
When I get a chance I will use yours and MRAB's suggestions to try to 
figure out just what caused the problem.


Regards,  Jim


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


Re: [RELEASE] mpdecimal-2.5.0

2020-06-29 Thread Antoine Pitrou


Hi Stefan

On Mon, 29 Jun 2020 15:27:01 +0200
Stefan Krah  wrote:
> Hi,
> 
> I've released mpdecimal-2.5.0:
> 
>http://www.bytereef.org/mpdecimal/index.html
> 
> 15417edc8e12a57d1d9d75fa7e3f22b158a3b98f44db9d694cfd2acde8dfa0ca  
> mpdecimal-2.5.0.tar.gz
> 
> Starting with Python 3.9, this version should be used for an external 
> libmpdec.
> 
> Python versions 3.7 and 3.8 should use the previous version mpdecimal-2.4.2.

Speaking of which, is there a plan to incorporate a C API to the
_decimal module?

Currently, there's no reliable way AFAIK, from C or C++, to take a
PyObject* and acccess the underlying `mpd_t*` directly, for fast
conversions.

Regards

Antoine.


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


Re: i don't understand this python class

2020-06-29 Thread DL Neil via Python-list

On 30/06/20 5:14 AM, joseph pareti wrote:

I have piece of code with constructs as follows:

*class* *SentimentNetwork**:*

 *def* __init__*(*self*,* reviews*,* labels*,* hidden_nodes *=* 10*,*
learning_rate *=* 0.1*):*
 np*.*random*.*seed*(*1*)*
self*.*init_network*(**len**(*self*.*review_vocab*),*hidden_nodes*,*
1*,* learning_rate*)*

 *def* init_network*(*self*,* input_nodes*,* hidden_nodes*,* output_nodes
*,* learning_rate*):*
 # Store the number of nodes in input, hidden, and output layers.
 self*.*input_nodes *=* input_nodes
 self*.*hidden_nodes *=* hidden_nodes
 self*.*output_nodes *=* output_nodes

which makes me think about the redundant usage of* init_network:*

1. as a method, AND
2. as a property



Let's imagine this code-functionality, but where all of the 
initialisation is performed in the single __init__ method:-


- there would be four lines of code instead of eight, saving typing-time 
and reading-time
- when reading the code, we would not have to 'jump' our eyes from 
__init__() to init_network, just to see what happens at instantiation


So, why bother?
(is that the nub of your question?)

Consider now the pertinent four lines:

- seed the PRNG (Pseudo-Random Number Generator)
- set number of input_nodes
- set number of hidden_nodes
- set number of output_notes

per my favorite (hi-brown and highly-technical) TV program(me): "which 
of these is not like the others?"


Add to such thinking, that there is apparently much more in the 
initialiser than illustrated (presumably for brevity - thanks!), and you 
can comprehend that there are several *different* functions that all 
have to happen upon instantiation.


So, although init_network() as a separate method is *functionally* 
unnecessary, by separating those tasks from 'everything else that has to 
happen' (and presumably also bunching groups of those tasks together 
under applicable headings!) is a very helpful *documentation* technique 
- in fact, I put it to you, that the name of the method is more helpful 
to a reader, than is the explanatory comment! Your thoughts?


(which #comment 'should' be a docstring, BTW)


The word "property" may have a slightly different definition in Python 
than in other programming languages you have used. If that part of the 
question hasn't been adequately-covered (albeit en-passant) please 
explain further...



Finally, please be aware that there is a Python-Tutor Discussion List 
for the pleasure of both learners and tutors.

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