Re: [Tutor] Python sqlite3 issue

2014-10-21 Thread Peter Otten
Alan Gauld wrote:

> Finally when creating tables you can use:
> 
> DROP TABLE IF EXISTS TOPICS;
> 
> Prior to creating it. That will ensure you don't get an existing
> table error.

Another option is to only create the table if it does not already exist:

create table if not exists topics ...


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


Re: [Tutor] Python sqlite3 issue

2014-10-21 Thread Alan Gauld

On 21/10/14 09:18, Peter Otten wrote:


Another option is to only create the table if it does not already exist:

create table if not exists topics ...


The danger with that one is that if you are changing the structure of 
the table you can wind up keeping the old structure by accident and your 
queries no longer match the table. This is especially common in SQLite 
since the ALTER TABLE command is so weak in capability that you often 
have to recreate tables to make changes.


In more powerful SQL databases I use the "if not exists" quite
often for creates because I can do most restructuring via ALTER,
but in SQLite I usually drop and then create.

The exception to that is of course if you are working on a pre-populated 
database with many tables. Then the exists check

can prevent you wiping out a key tabl;e and its data by accidental
name duplication.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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 am I not understanding?

2014-10-21 Thread Clayton Kirkwood
Thanks all for the insight. I'm not sure I fully understand all of the code
snippets, but in time...

This is finally what I came up with:

raw_table = ('''
a: Ask  y: Dividend Yield
b: Bid  d: Dividend per Share
b2: Ask (Realtime)  r1: Dividend Pay Date
b3: Bid (Realtime)  q: Ex-Dividend Date
p: Previous Close
o: Open
Date
''')


import re, string
dict={}
key_name = raw_table.replace('\t','\n')
for each_line in  key_name.splitlines():
if ':' in each_line:#this design had to
do with a few different lines
for key, value in [each_line.split(':')]:   #see the last line
in the data. I still don't fully
dict[key.strip()] = value.strip()   #understand the second for
and the square brackets.

#I presume that they force the source to look and feel like a tuple or list,
but not sure. I think they force two strings into a two items of a tuple???
Please let me know if I munged bad code together:<))

Clayton


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


Re: [Tutor] Registering callbacks and .DLL

2014-10-21 Thread Wilson, Pete
This worked. Yeah! Thank you so much. Pete

> -Original Message-
> From: eryksun [mailto:eryk...@gmail.com]
> Sent: Friday, October 17, 2014 8:53 PM
> To: Wilson, Pete
> Cc: tutor@python.org
> Subject: Re: [Tutor] Registering callbacks and .DLL
> 
> On Thu, Oct 16, 2014 at 6:35 PM, Wilson, Pete 
> wrote:
> >
> > The .DLL was written in C++ is working with C++ apps calling it.
> 
> ctypes doesn't support the platform C++ ABI (I don't think the VC++ ABI
> is even stable), classes, STL containers, or exceptions [*]. It isn't
> "cpptypes". To work with ctypes, a C++ library needs an extern "C"
> interface. The library you're using probably qualifies, but try a
> simple test program in C before jumping into ctypes.
> 
> [*] On Windows ctypes has limited support for Structured Exception
> Handling (SEH), a Microsoft extension of ANSI C. Inadvertently it also
> handles any exception raised by Win32 RaiseException, such as VC++
> exceptions. The code for VC++ exceptions is 0xE06D7363, i.e. "\xE0"
> "msc". ctypes isn't looking for this code and doesn't delve deeper to
> get the C++ exception type, so the OSError it raises is almost useless.
> Pretend this doesn't exist. SEH support is only implemented for the few
> cases ctypes handles explicitly such as access violations.
> 
> > I tried the methods in section 15.17.1.17 with the qsort() and
> > CFUNCTYPE, but it is not working.  My code and the .dll are attached.
> >
> > from ctypes import *
> >
> > pt_dll = cdll.LoadLibrary("c:/py_stuff/ProductionTest.dll")
> 
> You can use CDLL instead. It's fewer keystrokes.
> 
> from ctypes import *
> 
> pt_dll = CDLL("c:/py_stuff/ProductionTest.dll")
> 
> If the functions use the stdcall convention, substitute WinDLL for
> CDLL. If there's a mix of calling conventions you can simply load the
> library twice, once as CDLL and again as WinDLL. They'll each have the
> same _handle attribute.
> 
> You can also define prototypes manually via CFUNCTYPE and WINFUNCTYPE.
> Then instantiate them with a 2-tuple (name_or_ordinal, library), e.g.
> 
> libc = CDLL('msvcr100')
> 
> atoi_t = CFUNCTYPE(c_int, c_char_p)
> atoi = atoi_t(('atoi', libc))
> 
> >>> atoi(b'42')
> 42
> 
> FYI, 64-bit Windows has a single calling convention, so if you switch
> to 64-bit Python you don't have to worry about cdecl vs stdcall.
> 
> http://en.wikipedia.org/wiki/X86_calling_conventions
> 
> > reg_send_serial_data = pt_dll.RegSendSerialData
> >
> > class SendSerialData_t(Structure):
> > _fields_ = [("tx_data", c_void_p),
> >("size", c_uint8)]
> >
> > send_serial_data = SendSerialData_t()
> 
> SendSerialData_t is a function pointer type, not a data structure.
> Here are the C prototypes from the attached PDF:
> 
> typedef void (*SendSerialData_t) (uint8_t *tx_data, uint8_t size);
> 
> void RegSendSerialData(SendSerialData_t SendSerialData);
> 
> A SenedSerialData_t function takes two parameters (uint8_t *, uint8_t)
> and returns nothing (void).
> 
> ctypes declarations:
> 
> SendSerialData_t = CFUNCTYPE(None, POINTER(c_uint8), c_uint8)
> 
> reg_send_serial_data = pt_dll.RegSendSerialData
> reg_send_serial_data.argtypes = [SendSerialData_t]
> reg_send_serial_data.restype = None
> 
> The first argument to CFUNCTYPE is the return type. Use None for void.
> 
> Next define the Python callback.
> 
> def send_serial_data(tx_data, size):
> # testing
> print tx_data, size
> print tx_data[:size]
> 
> cb_send_serial_data = SendSerialData_t(send_serial_data)
> 
> Finally, register the callback with the library:
> 
> reg_send_serial_data(cb_send_serial_data)
> 
> It's vital that you keep a reference to cb_send_serial_data (as a
> global, an instance attribute, in a container, etc). This prevents the
> callback from being deallocated while it's possible the library can
> call it. Otherwise at best you'll get an  access violation (or segfault
> on POSIX systems), but probably a less obvious error.
> 
> Next your test code sets up ProdBatVolRequest, which is prototyped as
> follows:
> 
> typedef void (*BatVolReadRequest_cb)(uint16_t bat_vol, uint8_t
> status);
> 
> typedef struct {
> BatVolReadRequest_cb BatVolReadConf;
> } BatVolReadRequest_t;
> 
> void ProdBatVolReadRequest(BatVolReadRequest_t BatVolReadParam);
> 
> ProdBatVolReadRequest is passed a struct by value that consists of a
> single function pointer. You can skip defining this struct and just
> pass the function pointer. It's the same ABI for x86 and x64.
> 
> BatVolReadRequest_t = CFUNCTYPE(None, c_uint16, c_uint8)
> 
> prod_bat_vol_read_request = pt_dll.ProdBatVolReadRequest
> prod_bat_vol_read_request.argtypes = [BatVolReadRequest_t]
> prod_bat_vol_read_request.restype = None
> 
> def bat_vol_read(bat_vol, status):
> # testing
> print bat_vol, status
> 
> cb_bat_vol_read = BatVolReadRequest_t(bat_vol_read)
> 
> prod_bat_vol_read_request

Re: [Tutor] what am I not understanding?

2014-10-21 Thread Alan Gauld

On 20/10/14 22:18, Clayton Kirkwood wrote:


for each_line in  key_name.splitlines():
 if ':' in each_line:   #this design had to
 for key, value in [each_line.split(':')]:  #see the last line


You shouldn't need the [] around split(), it generates a list for you.
In fact I'd expect the [] to break it since there will only be a single 
element(a list) in the outer list.



#I presume that they force the source to look and feel like a tuple or list,


No, they create a list with whatever strip() produces. Here is a simpler 
example:


mylist = [1,2,3]
outerlist = [mylist]

outerlist is now [[1,2,3]]


I think they force two strings into a two items of a tuple???


I think you might be referring to the unpacking of the list into 
key,value above?


key,value = [2,3]

makes key=2 and value=3

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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 am I not understanding?

2014-10-21 Thread Dave Angel
"Clayton Kirkwood"  Wrote in message:
> Thanks all for the insight. I'm not sure I fully understand all of the code
> snippets, but in time...
> 
> This is finally what I came up with:
> 
> raw_table = ('''
> a: Asky: Dividend Yield
> b: Bidd: Dividend per Share
> b2: Ask (Realtime)r1: Dividend Pay Date
> b3: Bid (Realtime)q: Ex-Dividend Date
> p: Previous Close
> o: Open
> Date
> ''')
> 
> 
> import re, string
> dict={}
> key_name = raw_table.replace('\t','\n')
> for each_line in  key_name.splitlines():
> if ':' in each_line:  #this design had to
> do with a few different lines
> for key, value in [each_line.split(':')]: #see the last line
> in the data. I still don't fully
> dict[key.strip()] = value.strip() #understand the second for
> and the square brackets.
> 
> #I presume that they force the source to look and feel like a tuple or list,
> but not sure. I think they force two strings into a two items of a tuple???
> Please let me know if I munged bad code together:<))

I dont think you want the for loop at all. It just undoes the
 mistake of the extra brackets. Try replacing the for loop with
 :

 key, value in each_line.split(':')

And dedent the following line


-- 
DaveA

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


[Tutor] Question on a select statement with ODBC

2014-10-21 Thread Al Bull
Windows 7.0
Python 3.3.4

I am accessing a database table via ODBC.   The table I am accessing can
have multiple records per ord_dbasub.  Is there a way I can structure the
select statement to retrieve only the most current record (based on
ord_date)?

I used to program in PL/I and assembly in the old days but my knowledge of
SQL and Python is still pretty limited.   Here is what I have so far..

import pyodbc
cnxn = pyodbc.connect("DSN=Quickfill DEMO Database")
cursor = cnxn.cursor()

ord_rows = cursor.execute("select ord_dbasub, ord_pub,
ord_date,ord_service,"
  "ord_agency, ord_woa, ord_status,"
  "ord_channel, ord_source, ord_giftcomp,"
  "ord_cnreason "
  "from ord "
  "Where ord_pub='QWKFIL'"
  "order by ord_dbasub, ord_date").fetchall()

for row in ord_rows:
   # Print for testing.  Remove later
   print (row.ord_dbasub, row.ord_date, row.ord_pub)
   # Add code here to find the most recent order per DBASUB and delete other
orders
   # Also add exclusion code to be applied to survivor record


This code works so far.   I just need to determine how to remove all but the
most current record per ord_dbasub.Assuming I can't remover it via the
select statement I'll have to traverse the array (Tuples in Python,
correct?) and remove the ones I don't need.   What's the best way to do
that?

I have to admit that the concept of tuples & dictionaries has me a little
bit confused.I'm used to working with arrays and arrays of structures.

Thanks in advance for your help!


Al Bull, Chief Technology Officer/Owner
Publishers Data Management Group


a.b...@pubdmgroup.com
815-732-5297


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


Re: [Tutor] what am I not understanding?

2014-10-21 Thread Clayton Kirkwood


!-Original Message-
!From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
!Behalf Of Dave Angel
!Sent: Tuesday, October 21, 2014 6:51 AM
!To: tutor@python.org
!Subject: Re: [Tutor] what am I not understanding?
!
!"Clayton Kirkwood"  Wrote in message:
!> Thanks all for the insight. I'm not sure I fully understand all of the
!> code snippets, but in time...
!>
!> This is finally what I came up with:
!>
!> raw_table = ('''
!> a: Ask   y: Dividend Yield
!> b: Bid   d: Dividend per Share
!> b2: Ask (Realtime)   r1: Dividend Pay Date
!> b3: Bid (Realtime)   q: Ex-Dividend Date
!> p: Previous Close
!> o: Open
!> Date
!> ''')
!>
!>
!> import re, string
!> dict={}
!> key_name = raw_table.replace('\t','\n') for each_line in
!> key_name.splitlines():
!> if ':' in each_line: #this design had to
!> do with a few different lines
!> for key, value in [each_line.split(':')]:#see the last line
!> in the data. I still don't fully
!> dict[key.strip()] = value.strip()#understand the
second
!for
!> and the square brackets.
!>
!> #I presume that they force the source to look and feel like a tuple or
!> list, but not sure. I think they force two strings into a two items of
!a tuple???
!> Please let me know if I munged bad code together:<))
!
!I dont think you want the for loop at all. It just undoes the  mistake
!of the extra brackets. Try replacing the for loop with
! :
!
! key, value in each_line.split(':')
!
!And dedent the following line

Yep, that worked well. Thanks. I was lead to my initial line by something
else not working.

Clayton


!
!
!--
!DaveA
!
!___
!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


[Tutor] Full screen mode

2014-10-21 Thread Nathan Spencer
Hi there,

I'm running python on Scientific Linux.  The problem is that I'm
permanently stuck in full screen mode.  Exiting out of that mode (via F11)
doesn't do anything.  Do you have any suggestions?

Thanks,
Nathan

-- 
Nathaniel J. Spencer, PhD
Post-Doctoral Research Associate
Psychoacoustics Lab
School of Health and Rehabilitative Sciences
University of Pittsburgh
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on a select statement with ODBC

2014-10-21 Thread Alan Gauld

On 21/10/14 19:57, Al Bull wrote:


have multiple records per ord_dbasub.  Is there a way I can structure the
select statement to retrieve only the most current record (based on
ord_date)?


Yes, the cursor can be told to only retrieve N records, in your case 1.

SELECT ord_dbasub, ord_pub,ord_date,ord_service,
   ...
FROM ord
WHERE ord_pub='QWKFIL'
ORDER BY ord_dbasub, ord_date
LIMIT 1

If the sort order is wrong you can specify ASC or DESC to reverse
it as needed.


ord_rows = cursor.execute("select ord_dbasub, ord_pub,
ord_date,ord_service,"
   "ord_agency, ord_woa, ord_status,"
   "ord_channel, ord_source, ord_giftcomp,"
   "ord_cnreason "
   "from ord "
   "Where ord_pub='QWKFIL'"
   "order by ord_dbasub, ord_date").fetchall()


Rather than all those quotes you can use triple quotes:

ord_rows = cursor.execute('''select ord_dbasub, ord_pub,
 ord_date,ord_service,
 ord_agency, ord_woa, ord_status,
etc...
 order by ord_dbasub, ord_date
 limit 1''').fetchall()

for row in ord_rows:
print (row.ord_dbasub, row.ord_date, row.ord_pub)
# Add code here to find the most recent order per DBASUB and delete other
orders


If it's in order you could just access the first row using an index.

print (ord_rows[0])


I have to admit that the concept of tuples & dictionaries has me a little
bit confused.I'm used to working with arrays and arrays of structures.


tuples are just read-only lists, which, in turn, are arrays that can 
hold any data type.


tuples are also like records without named fields. You can use a named 
tuple from the collections module which is even more like a record.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Full screen mode

2014-10-21 Thread Danny Yoo
On Tue, Oct 21, 2014 at 2:14 PM, Nathan Spencer  wrote:
> Hi there,
>
> I'm running python on Scientific Linux.  The problem is that I'm permanently
> stuck in full screen mode.  Exiting out of that mode (via F11) doesn't do
> anything.  Do you have any suggestions?


Hmmm... unfortunately, I do not know what you mean by full screen
mode, and am unfamiliar with the Scientific Linux environment.  The
standard Python distribution, itself, should not include anything that
uses full screen mode, so you are probably running some third-party
IDE or editor.  The problem you're having doesn't sound like a Python
problem.  You may have better luck asking on a forum that's
specialized toward your computing environment.  You should probably
check with the scientific-linux-users mailing list:

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


Re: [Tutor] Full screen mode

2014-10-21 Thread Alan Gauld

On 21/10/14 22:14, Nathan Spencer wrote:


I'm running python on Scientific Linux.


I don't know it but assume it starts in normal X Windows fashion and you 
can create a normal Terminal/Console window? eg an xterm?



permanently stuck in full screen mode.  Exiting out of that mode (via
F11) doesn't do anything.


Is this for everything or just Python? If its just Python how
are you running it? Are you using an IDE(eg IDLE/Eclipse?)
or maybe IPython? Or are you just typing python at the shell?

If its everything then it sounds like you have switched to
a virtual console. Try hitting Ctrl-Alt-F7 to get back to
your X session.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Full screen mode

2014-10-21 Thread Steven D'Aprano
On Tue, Oct 21, 2014 at 05:14:57PM -0400, Nathan Spencer wrote:
> Hi there,
> 
> I'm running python on Scientific Linux.  The problem is that I'm
> permanently stuck in full screen mode.  Exiting out of that mode (via F11)
> doesn't do anything.  Do you have any suggestions?

I don't know what "full screen mode" means. Do you mean maximized?

It will help if you can tell us how you are starting Python. Python is a 
programming language, and there are many different environments that you 
might be using to run code in that programming language, e.g.:

- via the terminal
- in the Python interactive interpreter
- via IDLE
- via some third party IDE (integrated development environment)
- embedded in another application

What command do you type or button or menu do you click to start Python?

If you can see the standard window title bar, what happens if you click 
the "maximise" box next to the close box?


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