[Tutor] How to test for the existence of a table in a sqlite3 db?
I want to use Alan's (and others') idea to run a SQL file to create a table if that table does not exist. Alan suggested using executescript() to do this. I misunderstood Alan and thought that this would take a filename and execute it. Instead, it appears that I must pass to it a string which is a SQL script. So after lots of fooling around in the interpreter I arrived at: py3: import sqlite3 py3: conn = sqlite3.connect(':memory:') py3: c = conn.cursor() py3: try: ... c.execute('select * from BloodPressureReadings') ... except sqlite3.OperationalError: ... with open('create_sqlite3_db.sql') as f: ... sql = f.read() ... c.executescript(sql) ... The file 'create_sqlite3_db.sql' contains: CREATE TABLE BloodPressureReadings ( ReadingID INTEGER PRIMARY KEY, Date TEXT, Time TEXT, SystolicBP INTEGER, DiastolicBP INTEGER, Comments TEXT); So at this point I am only creating an empty table. The above "works", but my "try" check is awful! What can I replace it with to just see if there is *any* table in the chosen database? In the code Peter supplied in the thread, "How is database creation normally handled?", he used in his function, "ensure_db(filename)": cursor.execute("create table if not exists addresses (name, email);") which is sweet, but I don't see how I can apply this idea if I insist on using a SQL file to create my table(s). BTW, in the docs at https://docs.python.org/3/library/sqlite3.html I found no mention of the actual exception I caught, "OperationalError". Should not this be in the docs? -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with program
On 13/10/17 18:53, Alan Gauld via Tutor wrote: > On 13/10/17 13:04, Chris Coleman wrote: > >> def_init_(self,chat): > >> File "scripts/bird.py", line 4 >> def_init_(self,chat): >> ^ >> SyntaxError: invalid syntax > > There are two problems here. I meant to add that the syntax error is the colon at the end. Python sees what you've written as a function call but it doesn't know what to do with the colon. But if you add the space it sees it as a method definition and all is well. And once you add the second underscores it then recognises it as a dunder method definition and all is even better. :-) -- 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] using while loop for read process memory
Sorry Alan, Steve, everyone Can you take a look of this please? Here is my question about the memory: So I have a base address of a chunk of memory from it's size, from VirtualQueryEx (if you dont use windows, it's ok, it's not about how u get these values, because I think the base concept is the same) start = mbi.BaseAddress finish = mbi.RegionSize So at this time, I use while and this is how it looks like while index < finish: # access the memory here: while memory function( index) # then index += 1, for the inner loop ## this line complete the outer while loop index += mbi.RegionSize so Why did I put down index += 1 ? That's because what I think about the memory looks like this (short)(int)(double)(int)(int)(int)(double) and so on, since I can't predict which address is the beginning of a double, the only way to deal with that is to use increment by 1. Now, from what I have been reading, it seems there is a better way to do it, for instance, a for loop. for(start,finish, 8) why 8? because double begins at exact 0 or multiple of 8 bytes, right? On Thu, Oct 12, 2017 at 6:54 PM, Michael Cwrote: > Here is my question about the memory: > > So I have a base address of a chunk of memory from it's size, from > VirtualQueryEx > (if you dont use windows, it's ok, it's not about how u get these values, > because I think > the base concept is the same) > > start = mbi.BaseAddress > finish = mbi.RegionSize > > So at this time, I use while and this is how it looks like > > while index < finish: ># access the memory here: >while memory function( index) ># then index += 1, for the inner loop > > ## this line complete the outer while loop > index += mbi.RegionSize > > > so Why did I put down index += 1 ? > > That's because what I think about the memory looks like this > (short)(int)(double)(int)(int)(int)(double) and so on, > > since I can't predict which address is the beginning of a double, the only > way > to deal with that is to use increment by 1. > > Now, from what I have been reading, it seems there is a better way to do > it, > for instance, a for loop. > > for(start,finish, 8) > > why 8? because double begins at exact 0 or multiple of 8 bytes, right? > > > > On Sun, Oct 8, 2017 at 4:46 PM, Alan Gauld via Tutor > wrote: > >> On 08/10/17 20:18, Michael C wrote: >> > This is the red part >> > index = current_address >> > end = current_address + mbi.RegionSize >> > >> > while index < end: >> > if ReadProcessMemory(Process, index, ctypes.byref(buffer), \ >> > ctypes.sizeof(buffer), >> > ctypes.byref(nread)): >> > ## value comparison to be implemented. >> > pass >> > else: >> > raise ctypes.WinError(ctypes.get_last_error()) >> > >> > index += 1 >> >> I haven't been following this closely so may be way off here, >> but does this mean you are incrementing the memory address >> by 1? If so you are only increasing the pointer by 1 byte >> but you are, presumably, reading multiple bytes at a time >> (the size of the buffer presumably). >> >> Do you perhaps need to treat the buffer as a byte array >> and use something like the struct module to decode it? >> (assuming you know what you are reading...?) >> >> But I may be way off, I'm just going on a cursory look. >> >> -- >> 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with program
On 13/10/17 13:04, Chris Coleman wrote: just learning python as my first programming language. going through the book "python in easy steps" by mike mcgrath. i am going through the programs in chapter 7 and can't get them to work. here is the first one in the chapter: class Bird: '''A base class to define bird properties.''' count=0 def_init_(self,chat): self.sound=chat Bird.count+=1 def talk(self): return self.sound from Bird import* print('\nClass Instances Of:\n',Bird._doc_) polly=Bird('Squawk,squawk!') print('\nNumber Of Birds:',polly.count) print('Polly Says:',polly.talk()) harry=Bird('Tweet,tweet!') print('\nNumber Of Birds:',harry.count) print('Harry Says:',harry.talk()) i am getting this error message: File "scripts/bird.py", line 4 def_init_(self,chat): You need a space between the `def` and the `__init__`. ^ SyntaxError: invalid syntax what am i doing or not doing that is causing this? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] OT: New book on pytest
I just got the book in The Pragmatic Programmers series, "Python Testing with pytest -- Simple, Rapid, Effective, and Scalable" by Brian Okken, c. 2017. I've gone through about three chapters so far and I am really liking this book and pytest. Now that I am playing around with pytest, I must say I like its simpler ways of doing things. And I haven't lost anything! It runs all of my existing unittest code just fine. Thought I would mention this book in case anyone on the Tutor list has thought about trying pytest, but wanted a book on it. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with program
On 13/10/17 13:04, Chris Coleman wrote: > def_init_(self,chat): > File "scripts/bird.py", line 4 > def_init_(self,chat): > ^ > SyntaxError: invalid syntax There are two problems here. The first is that you need a space after the def. The second is that there should be two underscores on each side of init, so: def __init__(): The double underscore is a common structure in Python that indicates a method that is handled in a special way by Python, they are often called "dunder" methods. But it often catches beginners out, especially if your tutorial font does not make the double underscore obvious. -- 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] problem with program
just learning python as my first programming language. going through the book "python in easy steps" by mike mcgrath. i am going through the programs in chapter 7 and can't get them to work. here is the first one in the chapter: class Bird: '''A base class to define bird properties.''' count=0 def_init_(self,chat): self.sound=chat Bird.count+=1 def talk(self): return self.sound from Bird import* print('\nClass Instances Of:\n',Bird._doc_) polly=Bird('Squawk,squawk!') print('\nNumber Of Birds:',polly.count) print('Polly Says:',polly.talk()) harry=Bird('Tweet,tweet!') print('\nNumber Of Birds:',harry.count) print('Harry Says:',harry.talk()) i am getting this error message: File "scripts/bird.py", line 4 def_init_(self,chat): ^ SyntaxError: invalid syntax what am i doing or not doing that is causing this? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sibling import
On 2017-10-12 15:58, Mats Wichmann wrote: On 10/12/2017 05:15 AM, Atar new wrote: Hi Team, Here is my problem. I want to use sibling import but it is not working . I know taht if we add the directory in sys.path ,it will work. But I have to package the whole application and will create a setup.py file out of it . What is the standard way to do it? 1. mkdir A 2. mkdir B 3. 4. touch A/__init__.py 5. touch B/__init__.py 6. 7. touch A/foo.py 8. touch B/bar.py 9. 10. cat B/bar.py 11. from A import foo 12. 13. 14. python B/bar.py 15. ImportError: No module named A Thanks Anju This isn't the way: from the context of bar.py in B, there is no A. You generally speaking want a relative import (from .A import foo) for modern python versions, but because of the path structure you've set up, even that won't work, the script doing the importing would need to be in the top directory of your package. "Sibling" imports just don't work well. There was a PEP somewhere about this, which as I recall required some horrid looking hack. So with a bit of hunting, https://www.python.org/dev/peps/pep-0366 and more reading at https://www.python.org/dev/peps/pep-0338 I think Anju can solve his problem simply by adding his working directory (parent of A and B) to PYTHONPATH. Some one on this list some time ago provided me with the following bash magic to accomplish this: export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}$(pwd)" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] coding help with maxwell-boltzmann distribution
Yes we can vectorize. regards, Sarma. On Fri, Oct 13, 2017 at 9:43 PM, Peter Otten <__pete...@web.de> wrote: > D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > > f = np.zeros(40) > > v = np.arange(0,4,0.1) > > for i in np.arange(0, 40): > > f[i] = v[i]**2*(np.exp(-v[i]**2)) > > Note that you can write this without Python loop as > > v = np.arange(0, 4, 0.1) > f = v**2 * np.exp(-v**2) > > > ___ > 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] coding help with maxwell-boltzmann distribution
D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > f = np.zeros(40) > v = np.arange(0,4,0.1) > for i in np.arange(0, 40): > f[i] = v[i]**2*(np.exp(-v[i]**2)) Note that you can write this without Python loop as v = np.arange(0, 4, 0.1) f = v**2 * np.exp(-v**2) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] coding help with maxwell-boltzmann distribution
Except for some constants the essential behaviour of Maxweell-Boltzmann distribution is determined by v**2 * exp(-v**2) The following code will gove you a plot of the shape of the curve. from matplotlib import pyplot as plt import numpy as np f = np.zeros(40) v = np.arange(0,4,0.1) for i in np.arange(0, 40): f[i] = v[i]**2*(np.exp(-v[i]**2)) plt.plot(v,f) plt.show() regards, Sarma. On Fri, Oct 13, 2017 at 11:28 AM, Mark Lawrence via Tutorwrote: > On 12/10/17 21:22, Cameron McKay wrote: > >> Hello, >> >> I've never used python trying to plot a graph. Thus I am having >> difficulties trying to plot the maxwell-boltzmann distribution. right now >> i've defined the y-axis given the probability, but the difficult part is >> trying to plot x in the form of: >> >> x = v/(2kT/m)^(1/2) >> >> before i used the linspace function but i believe that was wrong as it >> just >> gave me an exponential growth function as i need a bellcurve. >> >> Thanks for looking into this, >> >> Cameron >> >> > Hopefully this helps https://docs.scipy.org/doc/sci > py/reference/generated/scipy.stats.maxwell.html > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > > ___ > 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] using while loop for read process memory
On 13/10/17 02:58, Michael C wrote: > end = current_address + mbi.RegionSize - 7 > > then it doesn't complain anymore. I think it's because I ran this in a > while loop with start += 1 > so in the last 7 bytes, I'd be reading past the end of this memory chunk. > > Is this right? Yes, almost certainly. That's what both Steve and I were alluding to in our earlier responses, you were incrementing by 1 byte but reading more than one byte so there was a high probability of you reading past the end. But subtracting 7 is only the correct answer if you are always reading 8 byte blocks, if you are reading different length blocks (for int/short/char etc) then you might need to do some kind of dynamic check based on sizeof(chunk)... if index+sizeof(chunk) > end data = read(chunk) else break -- 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] using while loop for read process memory
in fact, when I am using this: end = start + mbi.RegionSize I was getting error from the ReadProcessMemory function, and I couldn't figure it out why. Until I did this: end = current_address + mbi.RegionSize - 7 then it doesn't complain anymore. I think it's because I ran this in a while loop with start += 1 so in the last 7 bytes, I'd be reading past the end of this memory chunk. Is this right? On Thu, Oct 12, 2017 at 6:54 PM, Michael Cwrote: > Here is my question about the memory: > > So I have a base address of a chunk of memory from it's size, from > VirtualQueryEx > (if you dont use windows, it's ok, it's not about how u get these values, > because I think > the base concept is the same) > > start = mbi.BaseAddress > finish = mbi.RegionSize > > So at this time, I use while and this is how it looks like > > while index < finish: ># access the memory here: >while memory function( index) ># then index += 1, for the inner loop > > ## this line complete the outer while loop > index += mbi.RegionSize > > > so Why did I put down index += 1 ? > > That's because what I think about the memory looks like this > (short)(int)(double)(int)(int)(int)(double) and so on, > > since I can't predict which address is the beginning of a double, the only > way > to deal with that is to use increment by 1. > > Now, from what I have been reading, it seems there is a better way to do > it, > for instance, a for loop. > > for(start,finish, 8) > > why 8? because double begins at exact 0 or multiple of 8 bytes, right? > > > > On Sun, Oct 8, 2017 at 4:46 PM, Alan Gauld via Tutor > wrote: > >> On 08/10/17 20:18, Michael C wrote: >> > This is the red part >> > index = current_address >> > end = current_address + mbi.RegionSize >> > >> > while index < end: >> > if ReadProcessMemory(Process, index, ctypes.byref(buffer), \ >> > ctypes.sizeof(buffer), >> > ctypes.byref(nread)): >> > ## value comparison to be implemented. >> > pass >> > else: >> > raise ctypes.WinError(ctypes.get_last_error()) >> > >> > index += 1 >> >> I haven't been following this closely so may be way off here, >> but does this mean you are incrementing the memory address >> by 1? If so you are only increasing the pointer by 1 byte >> but you are, presumably, reading multiple bytes at a time >> (the size of the buffer presumably). >> >> Do you perhaps need to treat the buffer as a byte array >> and use something like the struct module to decode it? >> (assuming you know what you are reading...?) >> >> But I may be way off, I'm just going on a cursory look. >> >> -- >> 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using while loop for read process memory
Here is my question about the memory: So I have a base address of a chunk of memory from it's size, from VirtualQueryEx (if you dont use windows, it's ok, it's not about how u get these values, because I think the base concept is the same) start = mbi.BaseAddress finish = mbi.RegionSize So at this time, I use while and this is how it looks like while index < finish: # access the memory here: while memory function( index) # then index += 1, for the inner loop ## this line complete the outer while loop index += mbi.RegionSize so Why did I put down index += 1 ? That's because what I think about the memory looks like this (short)(int)(double)(int)(int)(int)(double) and so on, since I can't predict which address is the beginning of a double, the only way to deal with that is to use increment by 1. Now, from what I have been reading, it seems there is a better way to do it, for instance, a for loop. for(start,finish, 8) why 8? because double begins at exact 0 or multiple of 8 bytes, right? On Sun, Oct 8, 2017 at 4:46 PM, Alan Gauld via Tutorwrote: > On 08/10/17 20:18, Michael C wrote: > > This is the red part > > index = current_address > > end = current_address + mbi.RegionSize > > > > while index < end: > > if ReadProcessMemory(Process, index, ctypes.byref(buffer), \ > > ctypes.sizeof(buffer), > > ctypes.byref(nread)): > > ## value comparison to be implemented. > > pass > > else: > > raise ctypes.WinError(ctypes.get_last_error()) > > > > index += 1 > > I haven't been following this closely so may be way off here, > but does this mean you are incrementing the memory address > by 1? If so you are only increasing the pointer by 1 byte > but you are, presumably, reading multiple bytes at a time > (the size of the buffer presumably). > > Do you perhaps need to treat the buffer as a byte array > and use something like the struct module to decode it? > (assuming you know what you are reading...?) > > But I may be way off, I'm just going on a cursory look. > > -- > 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] coding help with maxwell-boltzmann distribution
On 12/10/17 21:22, Cameron McKay wrote: Hello, I've never used python trying to plot a graph. Thus I am having difficulties trying to plot the maxwell-boltzmann distribution. right now i've defined the y-axis given the probability, but the difficult part is trying to plot x in the form of: x = v/(2kT/m)^(1/2) before i used the linspace function but i believe that was wrong as it just gave me an exponential growth function as i need a bellcurve. Thanks for looking into this, Cameron Hopefully this helps https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.maxwell.html -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor