Re: [Tutor] anomaly

2018-04-20 Thread Alan Gauld via Tutor
On 20/04/18 05:53, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> I have a situation in which the same code gives an error in idle but works
> in qtconsole
> regards,
> 
> *​in idle*
> v = np.zeros(len(x))
> 
> for i in range(len(x)):
> if x[i] < 1.0:
> v[i] = 0

You don't give us any clue to what x (or V) is,
so its hard to know what is going on.

Also how are you running the code in IDLE?
Are you running the same file using the Run
menu option? Or are you importing your module?

Also which Python version are you using?

-- 
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] anomaly

2018-04-20 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 20/04/18 05:53, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
>> I have a situation in which the same code gives an error in idle but
>> works in qtconsole
>> regards,
>> 
>> *​in idle*
>> v = np.zeros(len(x))
>> 
>> for i in range(len(x)):
>> if x[i] < 1.0:
>> v[i] = 0
> 
> You don't give us any clue to what x (or V) is,
> so its hard to know what is going on.

The code may be the same, but it's probably not fed the same data. I'm 
guessing that x is a numpy array. The code above produces an error if the 
array has more than one dimension:

$ python3
Python 3.4.3 (default, Nov 28 2017, 16:41:13) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> x = numpy.array([1,2,3])
>>> for i in range(len(x)):
... if x[i] < 1.0: pass
... 
>>> x = numpy.array([[1,2,3], [4,5,6]])
>>> for i in range(len(x)):
... if x[i] < 1.0: pass
... 
Traceback (most recent call last):
  File "", line 2, in 
ValueError: The truth value of an array with more than one element is 
ambiguous. Use a.any() or a.all()

Comparing an array with a scalar uses broadcasting to build another array

>>> numpy.array([1,2,3]) < 3
array([ True,  True, False], dtype=bool)

and the numpy developers forbid using an array in a boolean context because 
it's not clear what bool(some_array) is supposed to return. Instead you have 
to make explicit if you want all values or at least one value to be truthy.

> Also how are you running the code in IDLE?
> Are you running the same file using the Run
> menu option? Or are you importing your module?
> 
> Also which Python version are you using?
 

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


Re: [Tutor] File handling Tab separated files

2018-04-20 Thread Alan Gauld via Tutor

Use Reply-All or Reply-List to include the mailing list in replies.

On 20/04/18 09:10, Niharika Jakhar wrote:
> Hi
>
> I want to store the data of file into a data structure which has 11
> objects per line , something like this:
> 2354     somethin2  23nothing       23214.
>
>
> so I was trying to split the lines using \n and storer each line in a
> list so I have a list of 11 objects, then I need to retrieve the last
> two position,

You are using the csv module so you don't need to split the lines, the
csv reader has already done that for you. It generates a sequence of
tuples, one per line.

So you only need to do something like:

results = []
with open(filename) as f:
 for line in csv.reader(f, delimiter='\t'):
 if line[-1] == line[-2]:
    results.append(line[2],line[3])

Let the library do the work.

You can see what the reader is doing by inserting a print(line) call
instead of the if statement. When using a module for the first time
don't be afraid to use print to check the input/output values.
Its better than guessing.


-- 
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] Code to download credit card statement and add to DB

2018-04-20 Thread Kuan Lu
Hello Mr. Tutor:

I constructed a pieced of code utilizing requests and pyodbc to download 
creditcard statement transactions and insert them into a DB table based on the 
code published by Louis Millette here: 
https://github.com/louismillette/Banking/tree/master.

Now I am a total newbie to Python even though I had some experience writing in 
other languages. The code works like a charm until where it comes to inserting 
the transactions into the table. As you can see in my code, in order to avoid 
duplicates being inserted into the table I’m trying to compared the downloaded 
statement line to each existing line in the table through nested loop and 
insert only when there’s no match. I kind of intuitively expect that there is 
another much simpler way to compare each downloaded line to all the inserted 
lines and find out if it is already in there. Obviously the approach I’m using 
doesn’t really work as I want it to. Can you please give me some hints as to 
how to get the loops to work as I explained or, if there is another better way 
to do it, what would that look like?

Thanks a lot for your guidance.

Kaun



   KUAN LU, CPA, CGA  |  FINANCE TEAM LEAD


  Email: k...@talize.com |
  Tel.:416-757-7008,212 | Fax.:416-757-9656
67 Alexdon Rd. Unit 1, North York, M3J 2B5 
[cid:image8b62b4.PNG@56bf8aa6.478ff844] 

   [cid:image60c6be.PNG@4e534b25.42997eac] 
   
[cid:imagecf6195.PNG@85b08044.4f853faf] 
   
[cid:image5aee5c.PNG@df0af238.4cb43c7f]   
 [cid:image53396f.PNG@356ad215.46ac24a4] 
   
[cid:imagede763d.PNG@0b16b53d.47948094] 

The information transmitted, including attachments, is intended only for the 
person(s) or entity to which it is addressed and may contain confidential 
and/or privileged material. Any review, retransmission, dissemination or other 
use of, or taking of any action in reliance upon this information by persons or 
entities other than the intended recipient is prohibited. If you received this 
in error please contact the sender and destroy any copies of this information.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code to download credit card statement and add to DB

2018-04-20 Thread Kuan Lu
Hello Mr. Tutor:

I constructed a pieced of code utilizing requests and pyodbc to download 
creditcard statement transactions and insert them into a DB table based on the 
code published by Louis Millette here: 
https://github.com/louismillette/Banking/tree/master.

Now I am a total newbie to Python even though I had some experience writing in 
other languages. The code works like a charm until where it comes to inserting 
the transactions into the table. As you can see in my code, in order to avoid 
duplicates being inserted into the table I’m trying to compared the downloaded 
statement line to each existing line in the table through nested loop and 
insert only when there’s no match. I kind of intuitively expect that there is 
another much simpler way to compare each downloaded line to all the inserted 
lines and find out if it is already in there. Obviously the approach I’m using 
doesn’t really work as I want it to. Can you please give me some hints as to 
how to get the loops to work as I explained or, if there is another better way 
to do it, what would that look like?

Thanks a lot for your guidance.

Kuan

The code is listed as below:

import os
import requests
import datetime
import re
import json
import pyodbc
from datetime import date, timedelta

pyodbc.lowercase = False

#connect to the server through pyodbc
server = '\\XXX'
database = 'XX'
username = 'XXX'
password = 'X'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL 
Server};SERVER=\\XXX;DATABASE=XXX;UID=XXX;PWD=X', autocommit=True)
cursor = cnxn.cursor()

#connect to the Utilities DB and determin the latest date of statement line(s) 
imported
cursor.execute("SELECT MAX(TransDate)  AS TransDate FROM 
Visa.CreditCardStatement")
Latest_Statement_Date=cursor.fetchone()

#login and date range parameters
_username = "X"
_password = "XX"
dateFrom = Latest_Statement_Date.TransDate - timedelta(2)
dateUntil = date.today()

#Firstly, you'll need to create an authentication request to acquire an X Auth 
Token.
# The headers are pulled from a request on my computer, feel free to change 
them to headers acquired from yours or another header.
# Note that the URL is an encrypted HTTPS request, as are all calls made to the 
API.
authenticate_request = requests.post(
url="https://www.cibconline.cibc.com/ebm-anp/api/v1/json/sessions";,
json={"card": {"value": "{}".format(_username), "description": "", 
"encrypted": False, "encrypt": True},
  "password": "{}".format(_password)},
headers={
"Host": "www.cibconline.cibc.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) 
Gecko/20100101 Firefox/53.0",
"Accept": "application/vnd.api+json",
"Accept-Language": "en",
"Accept-Encoding": "gzip, deflate, br",

"Referer":"https://www.cibconline.cibc.com/ebm-resources/public/banking/cibc/client/web/index.html";,
"Content-Type": "application/vnd.api+json",
"Client-Type": "default_web",
"X-Auth-Token": "",
"brand": "cibc",
"WWW-Authenticate": "CardAndPassword",
"X-Requested-With": "XMLHttpRequest",
"Content-Length": "112",
"Connection": "keep-alive",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
}
)

#Next you'll need to save the cookies, response header, and X Auth Token
cookies = dict(authenticate_request.cookies)
#self.cookies = cookies
authenticate_response_headers = authenticate_request.headers
X_Auth_Token = authenticate_response_headers['X-Auth-Token']
#self.X_Auth_Token = X_Auth_Token

#Make a login request like below.  Again the headers are not rigid, however, 
the "X-Auth-Token" filed must be the X Auth Token from earlier.
login_request = requests.get(

url="https://www.cibconline.cibc.com/ebm-anp/api/v1/profile/json/userPreferences";,
headers={
"Host": "www.cibconline.cibc.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) 
Gecko/20100101 Firefox/53.0",
"Accept": "application/vnd.api+json",
"Accept-Language": "en",
"Accept-Encoding": "gzip, deflate, br",

"Referer":"https://www.cibconline.cibc.com/ebm-resources/public/banking/cibc/client/web/index.html";,
"Content-Type": "application/vnd.api+json",
"Client-Type": "default_web",
"brand": "cibc",
"X-Auth-Token": X_Auth_Token,
"X-Requested-With": "XMLHttpRequest",
"Connection": "keep-alive",
},
cookies=cookies
)

#after logging in as yourself, go ahead and pull your default account id from 
the response
login_request_response = login_request.json()

#The Visa Account ID
defaultAccountId = "XXX"

#the dateFrom and dateUntil arguments are python datetimes representing from 
when until when you want to pull credit and debit entries
account_requests = req