From robertvstepp at gmail.com Sun Oct 1 01:09:55 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 1 Oct 2017 00:09:55 -0500 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? Message-ID: On the main Python list there is a thread entitled, "Beginners and experts (Batchelder blog post)", and Stefan Ram mentioned: "... The fact that programmers can't program is known since the invention of the "FizzBuzz" programmer test. ..." I vaguely recall seeing some reference to "FizzBuzz" in my past Internet wanderings, but did not recall any specifics, so did a search for it. My first hit was http://wiki.c2.com/?FizzBuzzTest So I went there and was informed: The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:"Write a program that prints the numbers from 1 to 100. But for multiples of three print ?Fizz? instead of the number and for the multiples of five print ?Buzz?. For numbers which are multiples of both three and five print ?FizzBuzz?." I did *not* read any further, opened up gVim, and in less than 5 minutes had: for i in range(1, 101): if i % 15 == 0: print('FizzBuzz') elif i % 3 == 0: print('Fizz') elif i % 5 == 0: print('Buzz') else: print(i) If I understand the problem correctly, this gives the desired result. I would have had this in < 30 seconds if I did not stop at each stage and verified I was getting my expected result. I don't claim to be a *real* programmer and am definitely no where approaching a professional programmer, but this question seems ridiculously easy! Surely I am missing something obvious and substantial? Of course, I could have done tests, comments, prettied up the output, etc., but the essential problem seems to be trivial for *anyone* exposed to the if - elif - else structure and is aware of the modulus operator. What am I missing??? How can anyone in any programming language not get this question right? -- boB From steve at pearwood.info Sun Oct 1 01:32:02 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 1 Oct 2017 16:32:02 +1100 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: References: Message-ID: <20171001053202.GZ13110@ando.pearwood.info> On Sun, Oct 01, 2017 at 12:09:55AM -0500, boB Stepp wrote: > If I understand the problem correctly, this gives the desired result. > I would have had this in < 30 seconds if I did not stop at each stage > and verified I was getting my expected result. I don't claim to be a > *real* programmer and am definitely no where approaching a > professional programmer, but this question seems ridiculously easy! Congratulations, according to some, you are a better programmer than 99% of self-described programmers. https://blog.codinghorror.com/why-cant-programmers-program/ For a dissenting view: https://www.skorks.com/2010/10/99-out-of-100-programmers-cant-program-i-call-bullshit/ -- Steve From robertvstepp at gmail.com Sun Oct 1 01:56:11 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 1 Oct 2017 00:56:11 -0500 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: <20171001053202.GZ13110@ando.pearwood.info> References: <20171001053202.GZ13110@ando.pearwood.info> Message-ID: On Sun, Oct 1, 2017 at 12:32 AM, Steven D'Aprano wrote: > On Sun, Oct 01, 2017 at 12:09:55AM -0500, boB Stepp wrote: > >> If I understand the problem correctly, this gives the desired result. >> I would have had this in < 30 seconds if I did not stop at each stage >> and verified I was getting my expected result. I don't claim to be a >> *real* programmer and am definitely no where approaching a >> professional programmer, but this question seems ridiculously easy! > > Congratulations, according to some, you are a better programmer than 99% > of self-described programmers. I definitely was *not* looking for a pat on the back. I just could not believe that "FizzBuzz" (Or similar questions.) would ever be needed in a job interview for programming/software engineering. I honestly thought that I was missing something. I know that I have had some really bad *blooper* moments in the past on this list! > https://blog.codinghorror.com/why-cant-programmers-program/ I truly hope that the above article does not reflect reality! > For a dissenting view: > > https://www.skorks.com/2010/10/99-out-of-100-programmers-cant-program-i-call-bullshit/ Hopefully this one provides a more accurate picture, though it is not a rosy view either. The more I read and study, the more I follow this and the main list, the more I realize how truly little I know ... -- boB From breamoreboy at yahoo.co.uk Sun Oct 1 00:11:21 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 1 Oct 2017 05:11:21 +0100 Subject: [Tutor] logging to cmd.exe In-Reply-To: References: Message-ID: On 26/09/2017 12:22, Albert-Jan Roskam wrote: > > PS: sorry about the missing quote (>>) markers. Hotmail can't do this. Is Gmail better? > Get a decent email client and it'll do the work for you. I use Thunderbird on Windows with hotmail, gmail and yahoo addresses and never have a problem. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email has been checked for viruses by AVG. http://www.avg.com From steve.lett777 at gmail.com Sun Oct 1 04:38:12 2017 From: steve.lett777 at gmail.com (Steve Lett) Date: Sun, 1 Oct 2017 19:38:12 +1100 Subject: [Tutor] Beginner's guessing game In-Reply-To: References: Message-ID: Can u please tell me why this program does not work in line 28? That is guessesTaken. It reads 0 when it should be a larger number. I am a beginner having another try to get it! Thank you, Steve From joel.goldstick at gmail.com Sun Oct 1 06:55:29 2017 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 1 Oct 2017 06:55:29 -0400 Subject: [Tutor] Beginner's guessing game In-Reply-To: References: Message-ID: On Sun, Oct 1, 2017 at 4:38 AM, Steve Lett wrote: > Can u please tell me why this program does not work in line 28? That is > guessesTaken. It reads 0 when it should be a larger number. > > I am a beginner having another try to get it! > > Thank you, Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Welcome Steve Please copy and paste your code into your email body. Attachments don't come through this list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From alan.gauld at yahoo.co.uk Sun Oct 1 07:04:13 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 1 Oct 2017 12:04:13 +0100 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: References: <20171001053202.GZ13110@ando.pearwood.info> Message-ID: On 01/10/17 06:56, boB Stepp wrote: > I definitely was *not* looking for a pat on the back. I just could > not believe that "FizzBuzz" (Or similar questions.) would ever be > needed in a job interview for programming/software engineering. The fizzbuzz one is definitely a bit too simplistic, but the one cited by McConnel (reverse a linked list in C) is typical of the kind of question we used. And yes, most candidates failed. Some of that is interview nerves so we would give them some hints and see if they could find the errors themselves. But some people literally couldn't even start! Another common ploy we used was to ask the candidate to find say 2 points of failure in a short function - say 4 or 5 lines long. And tell me how they'd fix it. Even fewer candidates can pass that one. > I truly hope that the above article does not reflect reality! I think it exaggerates (or maybe things have gotten much worse in the last 10 years!) but I do think more than half the applicants we got couldn't really program. (It doesn't help that many people today think that writing HTML/CSS is "programming"!) A final note. The jobs I was interviewing for were all internal vacancies advertised only within our own IT department. So all the candidates were already employed by us as IT people.... If you advertised externally it probably would be much worse. -- 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 From alan.gauld at yahoo.co.uk Sun Oct 1 07:20:45 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 1 Oct 2017 12:20:45 +0100 Subject: [Tutor] Beginner's guessing game In-Reply-To: References: Message-ID: On 01/10/17 09:38, Steve Lett wrote: > Can u please tell me why this program does not work in line 28? That is > guessesTaken. It reads 0 when it should be a larger number. > > I am a beginner having another try to get it! > > Thank you, Steve Welcome Steve, but I can't see any program? Did you send it as an attachment? If so the server probably stripped it off for security reasons. Usually you can just paste the text into the email message body. (use plain text format to preserve layout) Also please include any error messages you get in their entirety and tell us your OS and Python version. -- 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 From alan.gauld at yahoo.co.uk Sun Oct 1 07:31:04 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 1 Oct 2017 12:31:04 +0100 Subject: [Tutor] Most common words in a text file In-Reply-To: References: Message-ID: On 30/09/17 18:12, Sri G. wrote: > import sysimport collections I assume that should be two lines? But you can also import multiple modules on a single line. import sys, collections Although some folks don't like that style. > def find_most_common_words(textfile, top=10): > ''' Returns the most common words in the textfile.''' The comment is slightly inaccurate since you really return a dict of the most common words *with the counts* added. It is good practice to specify the precise return type (list, tuple, dict etc) since that tells the user what they can do with it once they have it. Also by using the parameter textfile it is not clear whether I should pass a file object or a file name. Again it helps users if the comment is as specific as possible. > textfile = open(textfile) > text = textfile.read().lower() potential memory hog, others have already suggested reading line by line > textfile.close() > words = collections.Counter(text.split()) # how often each word appears > > return dict(words.most_common(top)) -- 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 From mats at wichmann.us Sun Oct 1 10:23:42 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 1 Oct 2017 08:23:42 -0600 Subject: [Tutor] Most common words in a text file In-Reply-To: References: Message-ID: <7b05253f-0676-15d7-2ebe-00499a919558@wichmann.us> On 09/30/2017 11:12 AM, Sri G. wrote: > I'm learning programming with Python. > > I?ve written the code below for finding the most common words in a text > file that has about 1.1 million words. It's working fine, but I believe > there is always room for improvement. > > When run, the function in the script gets a text file from the command-line > argument sys.argv[1], opens the file in read mode, converts the text to > lowercase, makes a list of words from the text after removing any > whitespaces or empty strings, and stores the list elements as dictionary > keys and values in a collections.Counter object. Finally, it returns a > dictionary of the most common words and their counts. The > words.most_common() method gets its argument from the optional top > parameter. > > import sysimport collections > def find_most_common_words(textfile, top=10): > ''' Returns the most common words in the textfile.''' > > textfile = open(textfile) > text = textfile.read().lower() > textfile.close() > words = collections.Counter(text.split()) # how often each word appears > > return dict(words.most_common(top)) > > filename = sys.argv[1] > top_five_words = find_most_common_words(filename, 5) > > I need your comments please. Others have made some pertinent comments already. How much you spend time to "improve" a bit of code depends on what you're going to do with it. If you've solved your problem, and it's a one-shot: don't worry much about it. Nothing wrong with a bit of throwaway code (although things do sometimes take on a life much longer than you intended, I can say from experience!!!) I'd ask a question or two to think about: first off, if you know the intended use of this function always will be to get a "top 10 list" - then why convert it back to a dict (unsorted) to return after Counter.most_common() has just given you a sorted list? You're most likely going to have to take your top_five_words dictionary and turn it back into something sorted to report back out on the counts. Second, if your function is likely to be called from many places in your code, is it too specialized? Can you design it as a more general API that could be used for other things than this specific purpose? For example, perhaps you just want to return the Counter instance without the further processing of "most_common", and let the client decide what it wants to do with that object. From steve at pearwood.info Sun Oct 1 11:09:57 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 2 Oct 2017 02:09:57 +1100 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: References: <20171001053202.GZ13110@ando.pearwood.info> Message-ID: <20171001150957.GA13110@ando.pearwood.info> On Sun, Oct 01, 2017 at 12:04:13PM +0100, Alan Gauld via Tutor wrote: > The fizzbuzz one is definitely a bit too simplistic, but the one > cited by McConnel (reverse a linked list in C) is typical of > the kind of question we used. And yes, most candidates failed. > > Some of that is interview nerves so we would give them some > hints and see if they could find the errors themselves. But > some people literally couldn't even start! I should think not! It's been about 30 years since I've last needed to reverse a linked list (in Pascal, not C, but the principle is the same). Who does that these days? I would have *no idea* how to traverse a singly-linked list in reverse without making a copy of it first. Okay, if you're specifically looking for somebody to write low-level algorithmic code, that's one thing. But 95% of programmers spend 95% of their time using library calls. And the remaining time, they certainly don't have to come up with a "reverse this linked list" algorithm from scratch. Google it, or look it up in a book. -- Steve From mats at wichmann.us Sun Oct 1 11:40:10 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 1 Oct 2017 09:40:10 -0600 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: <20171001150957.GA13110@ando.pearwood.info> References: <20171001053202.GZ13110@ando.pearwood.info> <20171001150957.GA13110@ando.pearwood.info> Message-ID: On 10/01/2017 09:09 AM, Steven D'Aprano wrote: > On Sun, Oct 01, 2017 at 12:04:13PM +0100, Alan Gauld via Tutor wrote: > >> The fizzbuzz one is definitely a bit too simplistic, but the one >> cited by McConnel (reverse a linked list in C) is typical of >> the kind of question we used. And yes, most candidates failed. >> >> Some of that is interview nerves so we would give them some >> hints and see if they could find the errors themselves. But >> some people literally couldn't even start! > > I should think not! It's been about 30 years since I've last needed to > reverse a linked list (in Pascal, not C, but the principle is the same). > Who does that these days? > > I would have *no idea* how to traverse a singly-linked list in reverse > without making a copy of it first. > > Okay, if you're specifically looking for somebody to write low-level > algorithmic code, that's one thing. But 95% of programmers spend 95% of > their time using library calls. And the remaining time, they certainly > don't have to come up with a "reverse this linked list" algorithm from > scratch. Google it, or look it up in a book. My problem with programming tests too. There's only so much stuff you can keep in easily accessible memory, and the ability to find snippets and other answers and make use of them in order to help develop code efficiently is a more valuable programming skill than memorization of algorithms! - we're trying not to reinvent the wheel unless we absolutely have to. (We could go off on a tangent and argue about software patents here but I'll spare you all that). Many years ago I had a form of this discussion with my Physicist father, before computers were a big factor in his field: you needed to understand how you would arrive at an answer mainly in order to have a sense of whether something you were going to use was reasonable or off by orders of magnitude, because in real life you looked things up in the Rubber Book (https://en.wikipedia.org/wiki/CRC_Handbook_of_Chemistry_and_Physics) and other resources. We use google today in much the same way: why repeat work someone's already done? So employment-type programming tests often test for something that's not really part of your day-to-day routine as a programmer. Probably the best programming test there is look at code that's already been developed, but it's unfair to assume (as some companies do today) that a programmer's life's work will be sitting out in the open on github - that's just not true for everyone/everything, and not nearly all of the important skills of the professional programmer are in code either. From robertvstepp at gmail.com Sun Oct 1 15:22:21 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 1 Oct 2017 14:22:21 -0500 Subject: [Tutor] Fwd: Python programming for the absolute beginner In-Reply-To: References: Message-ID: ---------- Forwarded message ---------- From: Howard B Date: Sun, Oct 1, 2017 at 2:20 PM Subject: Re: [Tutor] Python programming for the absolute beginner To: boB Stepp The 2010 copyright version, ISBN 978-1-4354-5500-9, discusses installing Python 3.1 (main text on page 5, and Appendix A on page 405). You will be able to follow the book using any Python 3.x. Consider installing Python 3.6 (the latest version) from Anaconda -- available completely free and for all operating systems. This is a well supported and commonly used package. https://www.anaconda.com/download/ The installation also includes the Spyder and Jupyter development environments, which you may find useful. On Sat, Sep 30, 2017 at 7:59 AM, boB Stepp wrote: > On Fri, Sep 29, 2017 at 1:20 PM, Alan Gauld via Tutor > wrote: > > On 29/09/17 08:51, Peter Collidge wrote: > >> I have borrowed the above book from my local library but I believe it > was > >> written in 2010 and as a result I am having difficulty in deciding which > >> version of Python to download. > >> Can anyone help? > > > > If you want to follow the book use the version the book > > uses - probably 2.6 or something close? > > I no longer have a copy of this book, but I am fairly certain it uses > an early version of Python 3. The main problem, though, is it uses a > customized version of Pygame for its later gaming code called > Livewires, so that would have to be Python-version-compatible. IIRC, > the author had a download site where you could get the whole package > together: correct Python version used in book, correct Livewires > version, etc., and that this was addressed in the book (Maybe an > appendix? Or the first get things setup chapter? Can't remember for > sure.) > > If the OP goes on to use the sequel to this book, named "More Python > Programming for the Absolute Beginner" (By a different author.), > *that* book uses straight-up Pygame, again with an early version of > Python 3. Again, I don't think there will be any problems with the > exact version of Python 3 used as long as the version of Pygame is > compatible with the chosen Python. > > > > -- > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- boB From alan.gauld at yahoo.co.uk Sun Oct 1 15:48:42 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 1 Oct 2017 20:48:42 +0100 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: <20171001150957.GA13110@ando.pearwood.info> References: <20171001053202.GZ13110@ando.pearwood.info> <20171001150957.GA13110@ando.pearwood.info> Message-ID: On 01/10/17 16:09, Steven D'Aprano wrote: >> The fizzbuzz one is definitely a bit too simplistic, but the one >> cited by McConnel (reverse a linked list in C) is typical of >> the kind of question we used. And yes, most candidates failed. > > I would have *no idea* how to traverse a singly-linked list in reverse > without making a copy of it first. The point is you don't need to traverse it in reverse you simply iterate over it from the first element and prepend each node. It really is trivially simple - or should be for anyone purporting to be a C programmer. And its not about memorizing algorithms - I never would expect that - its about looking at the problem and thinking about the solution. And I should add that I was last interviewing programmers in the 1990's (for the millennium bug the very last time, and the code was in COBOL!). But to address another issue raised by Mats: > Probably the best programming test there is look at code > that's already been developed, Very true and in an ideal world what you would do, but... It is what we did with the bug finding test. But, as alluded to by Steve, it's really hard to do this for anything significant in an interview, since you would need to be familiar with the system's framework and libraries since most code consists of calls to libraries. And the candidates will probably not all be familiar or even aware of such, so, to level the playing field, you give them a simple but generic problem, like the fizzbiz or linked list tests. > ...not nearly all of the important skills of the > professional programmer are in code Absolutely, or at least for the software engineer. There is I believe a big cultural difference between, say the UK, and the USA in what a programming job looks like. (I'm less familiar with other countries, even in Europe) In the UK a "programmer" is a code monkey who takes a code spec (flow chart or pseudo code) and turns it into industrial strength code. It's a low paid (relatively) position and the programmer probably does not have any computer related degree, maybe a craft school diploma. I believe programmer salaries are currently around ?22-25K per annum. A software engineer can program but is also supposed to be able to capture and analyze client requirements produce a top level design/architecture, develop low level design/spec (possibly for handing to a "programmer") write the code, test it completely ("programmers" only do unit tests, not system tests) and write appropriate documentation. Salaries here range from about ?30-60K per annum depending on experience etc. When I was interviewing it was always for software engineers - we only employed programmers in our mainframe systems. Everyone else was an "engineer". So I guess expectations for the role might have a big bearing too. -- 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 From marc.tompkins at gmail.com Sun Oct 1 17:39:39 2017 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sun, 1 Oct 2017 14:39:39 -0700 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: References: <20171001053202.GZ13110@ando.pearwood.info> <20171001150957.GA13110@ando.pearwood.info> Message-ID: On Sun, Oct 1, 2017 at 12:48 PM, Alan Gauld via Tutor wrote: > But to address another issue raised by Mats: > > Probably the best programming test there is look at code > > that's already been developed, > > Very true and in an ideal world what you would do, but... > > It is what we did with the bug finding test. But, as alluded > to by Steve, it's really hard to do this for anything > significant in an interview, since you would need to be > familiar with the system's framework and libraries since > most code consists of calls to libraries. First off, a confession: I'm an English major, not CS or IT. But this conversation reminded me of my interview for (what turned out to be) my first-ever programming job. I'd been working as BOFH (operating a System/360) for a few months, and heard about a better job in an HP shop closer to where I lived. So I applied, and during the course of the interview I discovered that it was a blended position: programming during the day, operator in the evening. I kept a poker face, and when I was asked whether I'd written any COBOL I lied and said yes. (I knew the name, but had never seen a line of code - only BASIC and Pascal up to that time.) My test was, fortunately for me, not a start-from-scratch FizzBuzz problem, but debugging a report module that was on the department's "to-fix" list. It took me about twenty-five minutes to find the problem (it was an off-by-one bug, as I recall), most of which was taken up translating COBOL to Pascal in my head... I got the job, and went straight to the local university bookstore and bought every book I could find on COBOL. Good times, good times... From max.patient at icloud.com Sun Oct 1 14:36:36 2017 From: max.patient at icloud.com (Max Patient) Date: Sun, 01 Oct 2017 19:36:36 +0100 Subject: [Tutor] closing the window through code Message-ID: <7A5DD6D8-4CF2-413B-AD32-46339144ECB4@icloud.com> Hi there i am preparing for a mock exam using python and was wondering if you can help, my task is to create apassword checker and one of the criteria is to have a quit option. i have looked and looked but can't seem to find a bit of code allowing to do this! if there is any way you coild help me it woild be much appreciated yours sincerely Max Patient Sent from my iPhone From alan.gauld at yahoo.co.uk Sun Oct 1 19:58:06 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 2 Oct 2017 00:58:06 +0100 Subject: [Tutor] Am I missing something obvious about "FizzBuzz"? In-Reply-To: References: <20171001053202.GZ13110@ando.pearwood.info> <20171001150957.GA13110@ando.pearwood.info> Message-ID: On 01/10/17 22:39, Marc Tompkins wrote: >>> Probably the best programming test there is look at code >>> that's already been developed, >> >> It is what we did with the bug finding test. > My test was, fortunately for me, not a start-from-scratch FizzBuzz problem, > but debugging a report module that was on the department's "to-fix" list. I think that's the point, its much easier to use real code in a debug situation than in a write-something-new type test. If the code is halfway decent you can figure out what it does even if you don;t know the libraries - or even, as in your case, the language! But if you had been asked to write a new function in that reporting system (assuming you knew COBOL) that would have been much harder without knowing the database schema or the query libraries that existed. > It took me about twenty-five minutes to find the problem But here is another problem, when we interviewed folks we got an hour in total per candidate so if a single exercise took 25 minutes we would never have had a chance to explore the other skills they needed. It had to be 10-15 mins max. The reality is that interviewers need some kind of filter and there is no perfect one. I've come to the conclusion that the hire process should work on the basis that you take someone on for a 3 month trial, its the only real way to tell if they are any good. But legal and corporate requirements mean that's unlikely to ever happen. In the meantime interviewees and interviewers alike struggle to find a meaningful way to select real talent in a market where far more unsuitable candidates present themselves than suitable. I fact often we could not even get enough good candidates to fill the jobs available. It typically went like this: 10 jobs 1000 applicants (if it was advertised externally) 100 interviews (run by the HR team) 40 tested (this is where I came in) 8 selected + 2 temporary contractors hired Over the years we tried lots of different formats but the end results were always about the same. -- 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 From alan.gauld at yahoo.co.uk Sun Oct 1 20:12:07 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 2 Oct 2017 01:12:07 +0100 Subject: [Tutor] closing the window through code In-Reply-To: <7A5DD6D8-4CF2-413B-AD32-46339144ECB4@icloud.com> References: <7A5DD6D8-4CF2-413B-AD32-46339144ECB4@icloud.com> Message-ID: On 01/10/17 19:36, Max Patient wrote: > my task is to create a password checker and one of the > criteria is to have a quit option. What does the quit do? Quit the login? or quit the application? Based on your subject line I'm assuming just quit the login - but then how would you do anything in the app? You don't say what GUI framework you are using (and from the subject I assume it is a GUI?) but most have some kind of quit function. For example in Tkinter there is a quit() method in the top level widget and inherited by all the lower level ones. If you do just want to close a window/dialog the normal approach is just to make it invisible. In Tkinter that's done using the withdraw() method of a window. (restore it with deiconify()) Other frameworks will have their equivalent methods/functions. In some GUIs it's the window's state value that controls visibility. -- 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 From renukeshnk15 at gmail.com Tue Oct 3 04:48:23 2017 From: renukeshnk15 at gmail.com (renukesh nk) Date: Tue, 3 Oct 2017 14:18:23 +0530 Subject: [Tutor] script guidelines Message-ID: requirement: i have a directory , that contains multiple sub directories, each sub directory has multiple text and log files, my script fetches the required lines from all the sub directories and stores it in one text file. but i want it to store separate text file for each sub directory ,after fetching the contents. can anyone please help me where to edit my script. my script is currently dumping all in on file instead of separate file for each directory. -------------- next part -------------- import zipfile,fnmatch,os import os, shutil, re, glob from os.path import isfile, join from os import walk root_src_dir = r'E:\\New folder' root_dst_dir = "E:\\destination" for src_dir, dirs, files in os.walk(root_src_dir): dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1) if not os.path.exists(dst_dir): os.makedirs(dst_dir) for file_ in files: src_file = os.path.join(src_dir, file_) dst_file = os.path.join(dst_dir, file_) if os.path.exists(dst_file): os.remove(dst_file) shutil.copy(src_file, dst_dir) rootPath = r"E:\\destination" pattern = '*.zip' for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): print(os.path.join(root, filename)) zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0])) os.chdir(rootPath) for file in glob.glob(pattern): f = open((file.rsplit(".", 1)[0]) + "ANA.txt", "w") f.close() ##here folder output mypath =r"E:\\destination" newpath = os.path.expanduser("E:\\destination") filenam = "1.txt" f = [] path = '' path1 = [] for (dirpath, dirnames, filenames) in walk(mypath): if isfile(join(mypath, dirpath)): path1.extend(join(mypath, dirpath)) if os.path.isdir(join(mypath, dirpath)): for f in filenames: path1.append(str(join(mypath, dirpath, f))) print(path1) newf = open(os.path.join(newpath, filenam ), "w+") myarray = {"ERROR", "error"} for element in myarray: elementstring = ''.join(element) for f in path1: openfile = open(os.path.join(path, f), "r") for line in openfile: if elementstring in line: newf.write(line) newf.close() openfile.close() From alan.gauld at yahoo.co.uk Tue Oct 3 14:17:56 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 3 Oct 2017 19:17:56 +0100 Subject: [Tutor] script guidelines In-Reply-To: References: Message-ID: On 03/10/17 09:48, renukesh nk wrote: > requirement: > i have a directory , that contains multiple sub directories, each sub > directory has multiple text and log files, my script fetches the required > lines from all the sub directories and stores it in one text file. > I'm not too sure what you mean by writing the "required lines" but I've added some general comments below > but i want it to store separate text file for each sub directory ,after > fetching the contents. can anyone please help me where to edit my script. It is better if you copy the script into the body of the email, attachments often get stripped by the server so we can't see them. In this case you got lucky... Comments below: > import zipfile,fnmatch,os > import os, shutil, re, glob you import os twice, its not a bug but it is a potential future maintenance gotcha. > from os.path import isfile, join you use os.path.xxx so don't need this import > from os import walk You use os.walk so don't need this import. > root_src_dir = r'E:\\New folder' > root_dst_dir = "E:\\destination" dst_dir should be a raw string too. And why double quotes here and single quotes above? It's good to be consistent. > for src_dir, dirs, files in os.walk(root_src_dir): > dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1) > if not os.path.exists(dst_dir): > os.makedirs(dst_dir) > for file_ in files: > src_file = os.path.join(src_dir, file_) > dst_file = os.path.join(dst_dir, file_) > if os.path.exists(dst_file): > os.remove(dst_file) > shutil.copy(src_file, dst_dir) This just copies everything so why not use the shutil.copytree() function instead? > rootPath = r"E:\\destination" Rather than duplicate the string use root_dst_dir > pattern = '*.zip' > for root, dirs, files in os.walk(rootPath): This is where you do stuff at a per directory level so this is probably where you want to create your logfile and write to it. But i'm still not sure what you are looking to write... > for filename in fnmatch.filter(files, pattern): > print(os.path.join(root, filename)) > zipfile.ZipFile(os.path.join(root, > filename)).extractall(os.path.join(root, > os.path.splitext(filename)[0])) This chunk looks at every zip file and extracts the contents into the same folder(what if there are duplicate filenames?) But you don't do anything with those files? > os.chdir(rootPath) > for file in glob.glob(pattern): > f = open((file.rsplit(".", 1)[0]) + "ANA.txt", "w") > > f.close() Then you change to the destination root and iterate over any zip files at the root level, opening a new file and immediately closing it again? At this point I'm confused? It would be good to put a comment before each block explaining the intended purpose of the code - what it is expected to achieve and why you are doing it. > ##here folder output > mypath =r"E:\\destination" Again this could use root_dst_dir to avoid string duplication (and potential error) > newpath = os.path.expanduser("E:\\destination") expanduser() replaces an initial tilde(~) with the homedir but since you don't have an initial tilde its completely redundant here. > filenam = "1.txt" > f = [] > path = '' > path1 = [] These look suspiciously like bad variable names. What is their purpose? Is 'path1' really a list? Usually lists suggest a plural name, such as paths. > for (dirpath, dirnames, filenames) in walk(mypath): > if isfile(join(mypath, dirpath)): This should never be true since mypath is a directory and dirpath is, by definition, the folder that walk() is currently reading. But joining them makes no sense since dirpath should already include mypath. > path1.extend(join(mypath, dirpath)) > if os.path.isdir(join(mypath, dirpath)): Likewise because you join() them the result will probably not exist so the tst will fail. > for f in filenames: > path1.append(str(join(mypath, dirpath, f))) If this does get called it will probably have broken paths since you use join again. If you just want the files use the list of files that os.walk gave you - it has already separated the contents into dirs and files as part of its return value. > print(path1) > > newf = open(os.path.join(newpath, filenam ), "w+") Why use 'w+'? You only need that if you are going to be reading and writing the file - usually involving use of tell()/seek() etc. You don't do any of that so keep things simple and just use 'w'. > myarray = {"ERROR", "error"} Its not an array its a set > for element in myarray: > elementstring = ''.join(element) This is a very complex way of writing elementstring = 'Error' Although it does have a frisson of interest because the actual set element chosen is theoretically arbitrary since sets are unordered. It's not clear what you are trying to achieve but I suspect this is not it. > for f in path1: > openfile = open(os.path.join(path, f), "r") Since path is an empty string this is effectively saying openfile = open(f, "r") > for line in openfile: > if elementstring in line: > newf.write(line) > newf.close() > openfile.close() You should be closing openfile in the loop above where you open it. But better still you should be using the with open((f,'r') as openfile: idiom which will auto close the file for you. I'm still not clear on what you are trying to do(*) but it looks like you are making it much more difficult than I suspect it needs to be. It will also be pretty slow since you walk the directory tree multiple times when one copytree() followed by one walk() should, I think, suffice. (*)I think it's to copy the initial file structure then search all files in the copy for lines including the strings 'Error' or 'error' and write those lines to a logfile, one per folder. But I'm not really certain. -- 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 From mysecretrobotfactory at gmail.com Tue Oct 3 17:30:43 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 3 Oct 2017 14:30:43 -0700 Subject: [Tutor] ctypes wintypes Message-ID: Hi all: I am trying to create SYSTEM_INFO structure and MEMORY_BASIC_INFORMATION structure I think there are modules for this purpose? Is it the ctypes.wintypes? if so, please point me to a documentation for it. Thanks! From alan.gauld at yahoo.co.uk Tue Oct 3 19:50:23 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 4 Oct 2017 00:50:23 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On 03/10/17 22:30, Michael C wrote: > I am trying to create SYSTEM_INFO structure and MEMORY_BASIC_INFORMATION > structure > > I think there are modules for this purpose? Is it the ctypes.wintypes? wintypes does define many of the standard Win32 API types but sadly neither of the two you mention seem to be included. Since the module consists entirely of type definitions the easiest way to find out what it holds is probably just to open the module source code and search/look. On my system(Linux) it lives in: /usr/lib/python3.4/ctypes/wintypes.py You might be able to create your own definition based on the Windows type data and combining the more primitive types that are declared in wintypes. -- 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 From steve.lett777 at gmail.com Tue Oct 3 17:49:53 2017 From: steve.lett777 at gmail.com (steve.lett777 at gmail.com) Date: Wed, 4 Oct 2017 08:49:53 +1100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <59d40603.0c95630a.c73ed.584f@mx.google.com> Can u please tell me why this program does not work in line 28? That is guessesTaken. It reads 0 when it should be a larger number. I am a beginner having another try to get it! Thank you, Steve PS. Python 3.5.1 and Win10 # This is a Guess the Number game. import random guessesTaken = 0 print('Hello! What is your name?') myName = input() number = random.randint(1, 20) print('Well, ' + myName + ', I am thinking of a number between 1 and 20.') for i in range(6): ??? print('Take a guess.') # Four spaces in front of "print" ??? guess = input() ??? guess = int(guess) ??? if guess < number: ??????? print('Your guess is too low.') # Eight spaces in front of "print" ??? if guess > number: ??????? print('Your guess is too high.') ??? if guess == number: ??????? break if guess == number: ??? guessesTaken = str(guessesTaken) ??? print('Good job, ' + myName + '! You guessed my number in ' + ????? guessesTaken + ' guesses!') if guess != number: ??? number = str(number) ???print('Nope. The number I was thinking of was ' + number + '.') Sent from Mail for Windows 10 From: tutor-request at python.org Sent: Tuesday, 3 October 2017 3:02 AM To: tutor at python.org Subject: Tutor Digest, Vol 164, Issue 5 --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus From mysecretrobotfactory at gmail.com Tue Oct 3 20:04:28 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 3 Oct 2017 17:04:28 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: i see i see. On Tue, Oct 3, 2017 at 4:50 PM, Alan Gauld via Tutor wrote: > On 03/10/17 22:30, Michael C wrote: > > > I am trying to create SYSTEM_INFO structure and MEMORY_BASIC_INFORMATION > > structure > > > > I think there are modules for this purpose? Is it the ctypes.wintypes? > > wintypes does define many of the standard Win32 API types > but sadly neither of the two you mention seem to be included. > > Since the module consists entirely of type definitions > the easiest way to find out what it holds is probably > just to open the module source code and search/look. > > On my system(Linux) it lives in: > > /usr/lib/python3.4/ctypes/wintypes.py > > You might be able to create your own definition based > on the Windows type data and combining the more primitive > types that are declared in wintypes. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Tue Oct 3 20:29:12 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 4 Oct 2017 01:29:12 +0100 Subject: [Tutor] (no subject) In-Reply-To: <59d40603.0c95630a.c73ed.584f@mx.google.com> References: <59d40603.0c95630a.c73ed.584f@mx.google.com> Message-ID: On 03/10/17 22:49, steve.lett777 at gmail.com wrote: > That is guessesTaken. It reads 0 when it should be a larger number. What makes you think so? You never increase it from its initial value so, quite correctly it stays at zero. > I am a beginner having another try to get it! Remember the computer just does what you tell it and nothing more. It has no idea what you are trying to do so it needs you to tell it exactly what to do. Every single thing. And in this case you need to add 1 to guessesTaken each time the user makes a guess. > # This is a Guess the Number game. > import random > > guessesTaken = 0 > > print('Hello! What is your name?') > myName = input() > > number = random.randint(1, 20) > print('Well, ' + myName + ', I am thinking of a number between 1 and 20.') > > for i in range(6): > ??? print('Take a guess.') # Four spaces in front of "print" > ??? guess = input() > ??? guess = int(guess) you probably should have a line here like: guesesTaken += 1 > ??? if guess < number: > ??????? print('Your guess is too low.') # Eight spaces in front of "print" > > ??? if guess > number: > ??????? print('Your guess is too high.') > > ??? if guess == number: > ??????? break > > if guess == number: > ??? guessesTaken = str(guessesTaken) > ??? print('Good job, ' + myName + '! You guessed my number in ' + > ????? guessesTaken + ' guesses!') > > if guess != number: > ??? number = str(number) > ???print('Nope. The number I was thinking of was ' + number + '.') -- 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 From mysecretrobotfactory at gmail.com Tue Oct 3 23:12:44 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 3 Oct 2017 20:12:44 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: Is there a module that does this for me? If it exists, how do I find it? thanks On Tue, Oct 3, 2017 at 5:04 PM, Michael C wrote: > i see i see. > > On Tue, Oct 3, 2017 at 4:50 PM, Alan Gauld via Tutor > wrote: > >> On 03/10/17 22:30, Michael C wrote: >> >> > I am trying to create SYSTEM_INFO structure and >> MEMORY_BASIC_INFORMATION >> > structure >> > >> > I think there are modules for this purpose? Is it the ctypes.wintypes? >> >> wintypes does define many of the standard Win32 API types >> but sadly neither of the two you mention seem to be included. >> >> Since the module consists entirely of type definitions >> the easiest way to find out what it holds is probably >> just to open the module source code and search/look. >> >> On my system(Linux) it lives in: >> >> /usr/lib/python3.4/ctypes/wintypes.py >> >> You might be able to create your own definition based >> on the Windows type data and combining the more primitive >> types that are declared in wintypes. >> >> -- >> 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 at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From newsletter at emil.mydigitaljet.com Tue Oct 3 22:27:51 2017 From: newsletter at emil.mydigitaljet.com (Trillonario) Date: Wed, 04 Oct 2017 02:27:51 +0000 Subject: [Tutor] Te da 1 ticket para que pruebes Message-ID: Una mega loter?a para cambiar su vida En Estados Unidos hay premios tan grandes como US$656 millones para que usted gane. Y no tiene que viajar a ning?n lado, ?tan s?lo tiene que tener su billete! Trillonario le hace posible participar en esta loter?a sin cruzar la frontera. Reg?strese ahora y consiga inmediatamente ? sin costo alguno - un billete oficial para la loter?a Mega Millions. Emocionante, ?verdad? Usted juega en una de las loter?as m?s grandes de los Estados Unidos, ?sin gastar un solo centavo! Pruebe ahora nuestro servicio, ?es gratis! JUEGUE GRATIS ?Necesita ayuda? Si tiene una pregunta o un comentario, cont?ctenos. Email: clientes at trillonario.com.mx . Si no puede visualizar el contenido, h?galo en nuestra web: http://emil.mydigitaljet.com/panel/wsJFD17757463_AgTL07f56183e95881848acc5ef03e499a9b/0wkp2331-wrJV1006-I8c565.html - If you can not display the content correctly, do it on our website: http://emil.mydigitaljet.com/panel/E11vD17757463/nZPi07f56183e95881848acc5ef03e499a9b/iP1b2331.kfgi1006-U1PS65.html . RIGHTS ABOUT YOUR PERSONAL DATA: In compliance with the provisions of the Protection of Personal Data of different lesgilations: "In any communication for the purpose of advertising carried out by mail, telephone, email, Internet or other means of distance known, expressly And outstanding, the possibility of the holder of the request to request the withdrawal or blocking, in whole or in part, of his name from the database. This is a commercial shipment according to the US CAN-SPAM law DERECHOS SOBRE SUS DATOS PERSONALES: En cumplimiento de lo dispuesto en la Protecci?n de Datos Personales de distintas lesgilaciones: ?En toda comunicaci?n con fines de publicidad que se realice por correo, tel?fono, correo electr?nico, Internet u otro medio a distancia a conocer, se deber? indicar, en forma expresa y destacada, la posibilidad del titular del dato de solicitar el retiro o bloqueo, total o parcial, de su nombre de la base de datos. Este es un envio comercial de acuerdo a la ley US CAN-SPAM . En caso de no querer recibir mas notificaciones / In case you do not want to receive more notifications: Cancel subscription.:http://emil.mydigitaljet.com/panel/WmdRU17757463_ey9W07f56183e95881848acc5ef03e499a9b-1R7z1006.pLWE2331.html . Email comercial eviado a tutor at python.org por Trillonario es operado por: ?Triangulum N.V., Landhuis Groot Kwartier, Groot Kwartierweg 12, Willemstad, Cura?ao con Bases y Tecnologia: . Envie sus comunicaciones a nuestras bases Opt-In de: Argentina Brasil Chile Colombia Espa?a Mexico Peru From alan.gauld at yahoo.co.uk Wed Oct 4 06:15:09 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 4 Oct 2017 11:15:09 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On 04/10/17 04:12, Michael C wrote: > Is there a module that does this for me? > If it exists, how do I find it? Google is your friend. What you need to remember is that modules only get created if someone else has the same need as you. And usually if its a repeated need since it takes time and effort to create and publish a module. The more specialized the topic, the less likely that somebody has done it often enough to go to the trouble of creating a module. Good luck. -- 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 From edmundopierre at yahoo.com Wed Oct 4 09:40:27 2017 From: edmundopierre at yahoo.com (edmundo pierre) Date: Wed, 4 Oct 2017 13:40:27 +0000 (UTC) Subject: [Tutor] I am trying to create a list of object and want to display that list on the screen on my Tkinter screen References: <1895807557.1241551.1507124427745.ref@mail.yahoo.com> Message-ID: <1895807557.1241551.1507124427745@mail.yahoo.com> Hello, I am writing a code where I am using a empty list and I will fill up that list with object. Then? I will show those on my Tkinter screen that I create. But when I run my code ,it crashes. Any help? # Variable? ?a = Intvar()? ? a1 = DoubleVar() #Create a function def Answe():? ? ? A = a.get()? ? ? B = [ ]? ? ? C = a1.get()? ? ? while A > 0:? ? ? ? ? ? ?C = a1.get()?? ? ? ? ? ? ? B.append(C)? ? ? ? ? ? ? A = A - 1 When I run this function about my program freezes. I do not know how to add element in a list using Tkinter. Edwin From alan.gauld at yahoo.co.uk Wed Oct 4 12:42:26 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 4 Oct 2017 17:42:26 +0100 Subject: [Tutor] I am trying to create a list of object and want to display that list on the screen on my Tkinter screen In-Reply-To: <1895807557.1241551.1507124427745@mail.yahoo.com> References: <1895807557.1241551.1507124427745.ref@mail.yahoo.com> <1895807557.1241551.1507124427745@mail.yahoo.com> Message-ID: On 04/10/17 14:40, edmundo pierre via Tutor wrote: > Hello, > I am writing a code When posting code please use plain text. HTML gets mangled so we can't see what your code looks like.(see below) > where I am using a empty list and I will fill up that > list with object. Then? I will show those on my Tkinter> screen that I create. But when I run my code ,it crashes. Any help? You haven't shown us any code that runs let alone freezes. Also apart from the 'a' and 'a1' var types there is no Tkinter involved. > # Variable? ?a = Intvar()? ? a1 = DoubleVar() > #Create a function > def Answe():? ? ? A = a.get()? ? ? B = [ ]? ? ? C = a1.get()? ? ? while A > 0:? ? ? ? ? ? ?C = a1.get()?? ? ? ? ? ? ? B.append(C)? ? ? ? ? ? ? A = A - 1 > When I run this function about my program freezes. It shouldn't do anything. You have only defined a couple of variables and a function, but nowhere do you have any code that does anything. You need to post all of the code if this is not it. > I do not know how to add element in a list using Tkinter. This has nothing to do with Tkinter. You are using a standard Python list: B = [] so you use standard Python to append elements, which you are already doing. B.append(C) But that list will not be displayed anywhere because you don't have any code to do so. You need to create a Tkinter window with a widget to display the list - it could be a Label, a Text widget or a Listbox, or any of several other options. For a Label called myLabel you would just assign the str() representation of your list to the text property: myLabel['text'] = str(B) It all depends on how you want it to appear. -- 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 From sjeik_appie at hotmail.com Wed Oct 4 16:15:21 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 4 Oct 2017 20:15:21 +0000 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: (sorry for top-posting) Perhaps this? https://pythonhosted.org/psutil/ ________________________________ From: Tutor on behalf of Michael C Sent: Tuesday, October 3, 2017 9:30:43 PM To: python tutor Subject: [Tutor] ctypes wintypes Hi all: I am trying to create SYSTEM_INFO structure and MEMORY_BASIC_INFORMATION structure I think there are modules for this purpose? Is it the ctypes.wintypes? if so, please point me to a documentation for it. Thanks! _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mats at wichmann.us Thu Oct 5 10:14:18 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 5 Oct 2017 08:14:18 -0600 Subject: [Tutor] (no subject) In-Reply-To: References: <59d40603.0c95630a.c73ed.584f@mx.google.com> Message-ID: <341a5ffd-5f5c-5f93-f079-cb79148e4a2a@wichmann.us> On 10/03/2017 06:29 PM, Alan Gauld via Tutor wrote: > On 03/10/17 22:49, steve.lett777 at gmail.com wrote: > >> That is guessesTaken. It reads 0 when it should be a larger number. > > What makes you think so? > You never increase it from its initial value so, quite correctly it > stays at zero. Adding some optimization thoughts below in addition: > >> I am a beginner having another try to get it! > > Remember the computer just does what you tell it and nothing more. > It has no idea what you are trying to do so it needs you to tell it > exactly what to do. Every single thing. > > And in this case you need to add 1 to guessesTaken each time > the user makes a guess. > >> # This is a Guess the Number game. >> import random >> >> guessesTaken = 0 >> >> print('Hello! What is your name?') >> myName = input() >> >> number = random.randint(1, 20) >> print('Well, ' + myName + ', I am thinking of a number between 1 and 20.') >> >> for i in range(6): >> ??? print('Take a guess.') # Four spaces in front of "print" >> ??? guess = input() >> ??? guess = int(guess) > > you probably should have a line here like: > > guesesTaken += 1 > >> ??? if guess < number: >> ??????? print('Your guess is too low.') # Eight spaces in front of "print" >> >> ??? if guess > number: >> ??????? print('Your guess is too high.') >> >> ??? if guess == number: >> ??????? break This sequence doesn't feel ideal: if guess == number you break out of the script, then the first thing thereafter is you do the same check again. Why not move that work up to the first check inside the for loop? >> if guess == number: >> ??? guessesTaken = str(guessesTaken) >> ??? print('Good job, ' + myName + '! You guessed my number in ' + >> ????? guessesTaken + ' guesses!') >> >> if guess != number: >> ??? number = str(number) >> ???print('Nope. The number I was thinking of was ' + number + '.') Second, you need to still detect the case where you exited the loop without ever matching. But it doesn't have to be a disconnected test like this one which risks at some time becoming detached - maybe you add some code in front of it, then later change it, then change it some more and at some point guess becomes changed and the test is invalid. It sounds silly here, but this happens in real code that is more complex. Python has a syntax feature to help you out here: the for statement can take an else clause, which means "finished the loop without otherwise breaking out of it". This would make the conceptual flow look like: for i in maximum-guesses: # prompt for guess # guidance if wrong if correct: # congratulate break else: # guesses ran out, commiserate From eryksun at gmail.com Thu Oct 5 14:34:18 2017 From: eryksun at gmail.com (eryk sun) Date: Thu, 5 Oct 2017 19:34:18 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Tue, Oct 3, 2017 at 10:30 PM, Michael C wrote: > > I am trying to create SYSTEM_INFO structure and MEMORY_BASIC_INFORMATION > structure First, avoid relying on constants, enumerations, and structures published on MSDN. It's not always right. Get the SDK and use the header files instead. MEMORY_BASIC_INFORMATION is defined in winnt.h, and SYSTEM_INFO is defined in sysinfoapi.h. MEMORY_BASIC_INFORMATION is simple. Don't worry about the MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions. Those are meant for a debugger that's reading this structure directly from the memory of another process. SYSTEM_INFO is a bit tricky, given the anonymous struct and union. I prefer to nest the definitions, but you could flatten it as separate definitions if you like. Refer to the docs for how to use _anonymous_: https://docs.python.org/3/library/ctypes#ctypes.Structure._anonymous_ Here are the definitions. Please don't mindlessly copy and paste. Recreate them on your own and use this example as a reference. import ctypes from ctypes.wintypes import WORD, DWORD, LPVOID PVOID = LPVOID SIZE_T = ctypes.c_size_t # https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulonglong): DWORD_PTR = ctypes.c_ulonglong elif ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong): DWORD_PTR = ctypes.c_ulong class MEMORY_BASIC_INFORMATION(ctypes.Structure): """https://msdn.microsoft.com/en-us/library/aa366775""" _fields_ = (('BaseAddress', PVOID), ('AllocationBase', PVOID), ('AllocationProtect', DWORD), ('RegionSize', SIZE_T), ('State', DWORD), ('Protect', DWORD), ('Type', DWORD)) PMEMORY_BASIC_INFORMATION = ctypes.POINTER(MEMORY_BASIC_INFORMATION) class SYSTEM_INFO(ctypes.Structure): """https://msdn.microsoft.com/en-us/library/ms724958""" class _U(ctypes.Union): class _S(ctypes.Structure): _fields_ = (('wProcessorArchitecture', WORD), ('wReserved', WORD)) _fields_ = (('dwOemId', DWORD), # obsolete ('_s', _S)) _anonymous_ = ('_s',) _fields_ = (('_u', _U), ('dwPageSize', DWORD), ('lpMinimumApplicationAddress', LPVOID), ('lpMaximumApplicationAddress', LPVOID), ('dwActiveProcessorMask', DWORD_PTR), ('dwNumberOfProcessors', DWORD), ('dwProcessorType', DWORD), ('dwAllocationGranularity', DWORD), ('wProcessorLevel', WORD), ('wProcessorRevision', WORD)) _anonymous_ = ('_u',) LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO) From eryksun at gmail.com Thu Oct 5 16:13:11 2017 From: eryksun at gmail.com (eryk sun) Date: Thu, 5 Oct 2017 21:13:11 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Thu, Oct 5, 2017 at 8:27 PM, Michael C wrote: > > How do I see the values of each field? This doesn't work. > > print(PMEMORY_BASIC_INFORMATION.Protect) Create an instance of MEMORY_BASIC_INFORMATION and pass a pointer to it via byref(). For example, the following queries the region of memory of the VirtualQuery function itself. kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) MEM_COMMIT = 0x1000 PAGE_EXECUTE_READ = 0x20 PAGE_EXECUTE_WRITECOPY = 0x80 VirtualQuery = kernel32.VirtualQuery VirtualQuery.restype = SIZE_T VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T) mbi = MEMORY_BASIC_INFORMATION() VirtualQuery(VirtualQuery, ctypes.byref(mbi), ctypes.sizeof(mbi)) >>> mbi.AllocationBase == kernel32._handle True >>> mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY True >>> mbi.BaseAddress 140703181352960 >>> mbi.RegionSize 364544 >>> mbi.State == MEM_COMMIT True >>> mbi.Protect == PAGE_EXECUTE_READ True From mysecretrobotfactory at gmail.com Thu Oct 5 15:27:53 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 5 Oct 2017 12:27:53 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: First of all, thanks for the reply. How do I see the values of each field? This doesn't work. print(PMEMORY_BASIC_INFORMATION.Protect) thanks! On Thu, Oct 5, 2017 at 11:34 AM, eryk sun wrote: > On Tue, Oct 3, 2017 at 10:30 PM, Michael C > wrote: > > > > I am trying to create SYSTEM_INFO structure and MEMORY_BASIC_INFORMATION > > structure > > First, avoid relying on constants, enumerations, and structures > published on MSDN. It's not always right. Get the SDK and use the > header files instead. MEMORY_BASIC_INFORMATION is defined in winnt.h, > and SYSTEM_INFO is defined in sysinfoapi.h. > > MEMORY_BASIC_INFORMATION is simple. Don't worry about the > MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions. > Those are meant for a debugger that's reading this structure directly > from the memory of another process. > > SYSTEM_INFO is a bit tricky, given the anonymous struct and union. I > prefer to nest the definitions, but you could flatten it as separate > definitions if you like. Refer to the docs for how to use _anonymous_: > > https://docs.python.org/3/library/ctypes#ctypes.Structure._anonymous_ > > Here are the definitions. Please don't mindlessly copy and paste. > Recreate them on your own and use this example as a reference. > > import ctypes > from ctypes.wintypes import WORD, DWORD, LPVOID > > PVOID = LPVOID > SIZE_T = ctypes.c_size_t > > # https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR > if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulonglong): > DWORD_PTR = ctypes.c_ulonglong > elif ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong): > DWORD_PTR = ctypes.c_ulong > > class MEMORY_BASIC_INFORMATION(ctypes.Structure): > """https://msdn.microsoft.com/en-us/library/aa366775""" > _fields_ = (('BaseAddress', PVOID), > ('AllocationBase', PVOID), > ('AllocationProtect', DWORD), > ('RegionSize', SIZE_T), > ('State', DWORD), > ('Protect', DWORD), > ('Type', DWORD)) > > PMEMORY_BASIC_INFORMATION = ctypes.POINTER(MEMORY_BASIC_INFORMATION) > > class SYSTEM_INFO(ctypes.Structure): > """https://msdn.microsoft.com/en-us/library/ms724958""" > class _U(ctypes.Union): > class _S(ctypes.Structure): > _fields_ = (('wProcessorArchitecture', WORD), > ('wReserved', WORD)) > _fields_ = (('dwOemId', DWORD), # obsolete > ('_s', _S)) > _anonymous_ = ('_s',) > _fields_ = (('_u', _U), > ('dwPageSize', DWORD), > ('lpMinimumApplicationAddress', LPVOID), > ('lpMaximumApplicationAddress', LPVOID), > ('dwActiveProcessorMask', DWORD_PTR), > ('dwNumberOfProcessors', DWORD), > ('dwProcessorType', DWORD), > ('dwAllocationGranularity', DWORD), > ('wProcessorLevel', WORD), > ('wProcessorRevision', WORD)) > _anonymous_ = ('_u',) > > LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO) > From mysecretrobotfactory at gmail.com Thu Oct 5 15:34:28 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 5 Oct 2017 12:34:28 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: Sorry about asking these super obvious little things, I am actually a 1st student, but I acing my programming 101 at the moment lol On Thu, Oct 5, 2017 at 12:27 PM, Michael C wrote: > First of all, thanks for the reply. > > > How do I see the values of each field? This doesn't work. > > print(PMEMORY_BASIC_INFORMATION.Protect) > > thanks! > > On Thu, Oct 5, 2017 at 11:34 AM, eryk sun wrote: > >> On Tue, Oct 3, 2017 at 10:30 PM, Michael C >> wrote: >> > >> > I am trying to create SYSTEM_INFO structure and >> MEMORY_BASIC_INFORMATION >> > structure >> >> First, avoid relying on constants, enumerations, and structures >> published on MSDN. It's not always right. Get the SDK and use the >> header files instead. MEMORY_BASIC_INFORMATION is defined in winnt.h, >> and SYSTEM_INFO is defined in sysinfoapi.h. >> >> MEMORY_BASIC_INFORMATION is simple. Don't worry about the >> MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions. >> Those are meant for a debugger that's reading this structure directly >> from the memory of another process. >> >> SYSTEM_INFO is a bit tricky, given the anonymous struct and union. I >> prefer to nest the definitions, but you could flatten it as separate >> definitions if you like. Refer to the docs for how to use _anonymous_: >> >> https://docs.python.org/3/library/ctypes#ctypes.Structure._anonymous_ >> >> Here are the definitions. Please don't mindlessly copy and paste. >> Recreate them on your own and use this example as a reference. >> >> import ctypes >> from ctypes.wintypes import WORD, DWORD, LPVOID >> >> PVOID = LPVOID >> SIZE_T = ctypes.c_size_t >> >> # https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR >> if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulonglong): >> DWORD_PTR = ctypes.c_ulonglong >> elif ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong): >> DWORD_PTR = ctypes.c_ulong >> >> class MEMORY_BASIC_INFORMATION(ctypes.Structure): >> """https://msdn.microsoft.com/en-us/library/aa366775""" >> _fields_ = (('BaseAddress', PVOID), >> ('AllocationBase', PVOID), >> ('AllocationProtect', DWORD), >> ('RegionSize', SIZE_T), >> ('State', DWORD), >> ('Protect', DWORD), >> ('Type', DWORD)) >> >> PMEMORY_BASIC_INFORMATION = ctypes.POINTER(MEMORY_BASIC_INFORMATION) >> >> class SYSTEM_INFO(ctypes.Structure): >> """https://msdn.microsoft.com/en-us/library/ms724958""" >> class _U(ctypes.Union): >> class _S(ctypes.Structure): >> _fields_ = (('wProcessorArchitecture', WORD), >> ('wReserved', WORD)) >> _fields_ = (('dwOemId', DWORD), # obsolete >> ('_s', _S)) >> _anonymous_ = ('_s',) >> _fields_ = (('_u', _U), >> ('dwPageSize', DWORD), >> ('lpMinimumApplicationAddress', LPVOID), >> ('lpMaximumApplicationAddress', LPVOID), >> ('dwActiveProcessorMask', DWORD_PTR), >> ('dwNumberOfProcessors', DWORD), >> ('dwProcessorType', DWORD), >> ('dwAllocationGranularity', DWORD), >> ('wProcessorLevel', WORD), >> ('wProcessorRevision', WORD)) >> _anonymous_ = ('_u',) >> >> LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO) >> > > From rikudou__sennin at live.com Thu Oct 5 12:38:00 2017 From: rikudou__sennin at live.com (adil gourinda) Date: Thu, 5 Oct 2017 16:38:00 +0000 Subject: [Tutor] Tkinter's Documentation Message-ID: Where can i find the reference documentation of "Tkinter" and if possible in PDF forme? Because there is no documentation on the widgets in "python library reference". Thanks From alan.gauld at yahoo.co.uk Fri Oct 6 05:05:27 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 6 Oct 2017 10:05:27 +0100 Subject: [Tutor] Tkinter's Documentation In-Reply-To: References: Message-ID: On 05/10/17 17:38, adil gourinda wrote: > Where can i find the reference documentation of "Tkinter" Tkinter is a module, not part of the language, so it is documented in the modules section. But the documentation is not 100% complete and for details you often need to look at the Tk/Tcl documentation too. Also there are several Tkinter web sites that offer additional information. One of the official web pages has links to them. This is probably the most comprehensive: https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html And there is a PDF link on their web page. -- 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 From renukeshnk15 at gmail.com Fri Oct 6 06:07:36 2017 From: renukeshnk15 at gmail.com (renukesh nk) Date: Fri, 6 Oct 2017 15:37:36 +0530 Subject: [Tutor] script guidelines In-Reply-To: References: Message-ID: currently m using pycharm , interpreter = python 3.6 i am getting th error as below, what might be the reason for this. UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 159: character maps to On Tue, Oct 3, 2017 at 2:18 PM, renukesh nk wrote: > requirement: > i have a directory , that contains multiple sub directories, each sub > directory has multiple text and log files, my script fetches the required > lines from all the sub directories and stores it in one text file. > > but i want it to store separate text file for each sub directory ,after > fetching the contents. can anyone please help me where to edit my script. > > my script is currently dumping all in on file instead of separate file for > each directory. > > From alan.gauld at yahoo.co.uk Fri Oct 6 06:25:29 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 6 Oct 2017 11:25:29 +0100 Subject: [Tutor] script guidelines In-Reply-To: References: Message-ID: On 06/10/17 11:07, renukesh nk wrote: > currently m using pycharm , interpreter = python 3.6 > i am getting th error as below, what might be the reason for this. > > UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 159: > character maps to It looks like you have a character in your charmap that the decoder can't decode. But without seeing any code it's hard to be more specific. Please always include the full error message every time. And sending at least the enclosing function, but preferably the whole source code if its not too long(<100 lines?) or under confidentiality constraints. (If the latter try to recreate the issue in a small snippet you can post) -- 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 From steve at pearwood.info Fri Oct 6 06:34:18 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 6 Oct 2017 21:34:18 +1100 Subject: [Tutor] script guidelines In-Reply-To: References: Message-ID: <20171006103418.GF9068@ando.pearwood.info> On Fri, Oct 06, 2017 at 03:37:36PM +0530, renukesh nk wrote: > currently m using pycharm , interpreter = python 3.6 > i am getting th error as below, what might be the reason for this. > > UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 159: > character maps to When you get an error, Python prints a full traceback showing the full chain of function calls. This is useful. Without it, we cannot even begin to solve the problem. All we can say is that, somewhere, somehow, for some reason, you are trying to decode some bytes from somewhere (we don't know where) to text, and there is an error doing so. If I look deeply into my magic crystal ball, I *think* the problem is that you are trying to read a file (maybe a HTML file downloaded from the Internet?) which is encoded differently than you expect. But I can't replicate the error you get: py> b'\x81'.decode('utf-8') Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte If this is a HTML file, have you tried inspecting the HTML to see what encoding you should use? -- Steve From alan.gauld at yahoo.co.uk Fri Oct 6 08:39:14 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 06 Oct 2017 13:39:14 +0100 Subject: [Tutor] Tkinter's Documentation In-Reply-To: Message-ID: <5d9e30ce-aca5-49cc-a389-5dd66be27f77@email.android.com> Tkinter is very much alive and has an active developers list. However Tkinter is just a wrapper around Tcl/Tk so there is only activity in Tkinter when there is something new in Tk. But both projects are going strong. On 6 Oct 2017 12:45 pm, adil gourinda wrote: This link exists in "python library reference under the name "Tkinter reference: a GUI for Python", but after sending an email to them (I wanted to participate with some suggestions) I didn't get any response, So I thought that it is like a dead project, for this reason why I asked for an alternative. Sorry if my first message was not so clear -------------------------------------------------------------------------- From: Tutor on behalf of Alan Gauld via Tutor Sent: Friday, October 6, 2017 10:05:27 AM To: tutor at python.org Subject: Re: [Tutor] Tkinter's Documentation ** On 05/10/17 17:38, adil gourinda wrote: > Where can i find the reference documentation of "Tkinter" Tkinter is a module, not part of the language, so it is documented in the modules section. But the documentation is not 100% complete and for details you often need to look at the Tk/Tcl documentation too. Also there are several Tkinter web sites that offer additional information. One of the official web pages has links to them. This is probably the most comprehensive: [1]https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html And there is a PDF link on their web page. -- Alan G Author of the Learn to Program web site [2]http://www.alan-g.me.uk/ [3]http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: [4]http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist** -** Tutor at python.org To unsubscribe or change subscription options: [5]https://mail.python.org/mailman/listinfo/tutor References Visible links 1. https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html 2. http://www.alan-g.me.uk/ 3. http://www.amazon.com/author/alan_gauld 4. http://www.flickr.com/photos/alangauldphotos 5. https://mail.python.org/mailman/listinfo/tutor From rikudou__sennin at live.com Fri Oct 6 07:45:04 2017 From: rikudou__sennin at live.com (adil gourinda) Date: Fri, 6 Oct 2017 11:45:04 +0000 Subject: [Tutor] Tkinter's Documentation In-Reply-To: References: , Message-ID: This link exists in "python library reference under the name "Tkinter reference: a GUI for Python", but after sending an email to them (I wanted to participate with some suggestions) I didn't get any response, So I thought that it is like a dead project, for this reason why I asked for an alternative. Sorry if my first message was not so clear ________________________________ From: Tutor on behalf of Alan Gauld via Tutor Sent: Friday, October 6, 2017 10:05:27 AM To: tutor at python.org Subject: Re: [Tutor] Tkinter's Documentation On 05/10/17 17:38, adil gourinda wrote: > Where can i find the reference documentation of "Tkinter" Tkinter is a module, not part of the language, so it is documented in the modules section. But the documentation is not 100% complete and for details you often need to look at the Tk/Tcl documentation too. Also there are several Tkinter web sites that offer additional information. One of the official web pages has links to them. This is probably the most comprehensive: https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html And there is a PDF link on their web page. -- 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 at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From eryksun at gmail.com Fri Oct 6 14:48:06 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Oct 2017 19:48:06 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Fri, Oct 6, 2017 at 7:26 PM, Michael C wrote: > > I started out with what you gave me: > [...] > > I am trying to acquire "lpMinimumApplicationAddress" and > "lpMaximumApplicationAddress" from system_info, so I did this, > >>code > Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) > Kernel32.GetSystemInfo(LPSYSTEM_INFO) > print(LPLPSYSTEM_INFO.lpMinimumApplicationAddress) It's the same pattern as before. Create a SYSTEM_INFO instance, which allocates the block of memory for the information, and pass GetSystemInfo a pointer. For example: kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) kernel32.GetSystemInfo.restype = None kernel32.GetSystemInfo.argtypes = (LPSYSTEM_INFO,) sysinfo = SYSTEM_INFO() kernel32.GetSystemInfo(ctypes.byref(sysinfo)) Here are the minimum and maximum addresses for a 64-bit process, formatted in hexadecimal: >>> hex(sysinfo.lpMinimumApplicationAddress) '0x10000' >>> hex(sysinfo.lpMaximumApplicationAddress) '0x7ffffffeffff' From eryksun at gmail.com Fri Oct 6 15:03:48 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Oct 2017 20:03:48 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Fri, Oct 6, 2017 at 7:43 PM, Michael C wrote: > Sorry but I dont understand this line: > > mbi = MEMORY_BASIC_INFORMATION() > > This creates a instance of the class? Yes, and this allocates sizeof(MEMORY_BASIC_INFORMATION) bytes at addressof(mbi), which you pass to a function by reference via byref(mbi). > Also, I thought with VirtualQueryEx, what you need for it > is a handle, which I acquire from this > Process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, > False, PID) My example called VirtualQuery, not VirtualQueryEx. Internally VirtualQuery calls VirtualQueryEx using the pseudo handle (HANDLE)(-1), which refers to the current process. > and then feed it to the function like so: > > VirtualQuery(Process, ctypes.byref(mbi), ctypes.sizeof(mbi)) > > I know it doesn't work. But what are these lines for? They don't look like > handle to me: > > VirtualQuery = kernel32.VirtualQuery > VirtualQuery.restype = SIZE_T > VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T) In the above, I'm setting the function pointer's argtypes attribute to the types of the 3 parameters that VirtualQuery takes: the target address (i.e. LPVOID), a pointer to the buffer (i.e. PMEMORY_BASIC_INFORMATION), and the size of the buffer (SIZE_T). This is to allow ctypes to correctly check and convert arguments passed to the function. VirtualQueryEx has four parameters, starting with the handle to the target process, hProcess. The remaining 3 are the same as VirtualQuery. From alan.gauld at yahoo.co.uk Fri Oct 6 16:33:47 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 06 Oct 2017 21:33:47 +0100 Subject: [Tutor] Tkinter's Documentation Message-ID: <3733b4c1-ec02-4969-b95b-9f4881aba346@email.android.com> On 6 Oct 2017 7:41 pm, adil gourinda wrote: Thanks, Now it is more clear, So I have to wait for the next version of tkinter if I want someone to listen to me. Not at all, as I said, there is an active developers list. They will listen and advise whether it's something you can do, or something that can be changed in Tkinter or something you need to talk to the Tcl/Tk developers about. Alan g. -------------------------------------------------------------------------- From: Alan Gauld Sent: Friday, October 6, 2017 1:39:14 PM To: adil gourinda Cc: tutor at python.org Subject: Re: [Tutor] Tkinter's Documentation Tkinter is very much alive and has an active developers list. However Tkinter is just a wrapper around Tcl/Tk so there is only activity in Tkinter when there is something new in Tk. But both projects are going strong. On 6 Oct 2017 12:45 pm, adil gourinda wrote: This link exists in "python library reference under the name "Tkinter reference: a GUI for Python", but after sending an email to them (I wanted to participate with some suggestions) I didn't get any response, So I thought that it is like a dead project, for this reason why I asked for an alternative. Sorry if my first message was not so clear -------------------------------------------------------------------------- From: Tutor on behalf of Alan Gauld via Tutor Sent: Friday, October 6, 2017 10:05:27 AM To: tutor at python.org Subject: Re: [Tutor] Tkinter's Documentation On 05/10/17 17:38, adil gourinda wrote: > Where can i find the reference documentation of "Tkinter" Tkinter is a module, not part of the language, so it is documented in the modules section. But the documentation is not 100% complete and for details you often need to look at the Tk/Tcl documentation too. Also there are several Tkinter web sites that offer additional information. One of the official web pages has links to them. This is probably the most comprehensive: [1]https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html And there is a PDF link on their web page. -- Alan G Author of the Learn to Program web site [2]http://www.alan-g.me.uk/ [3]http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: [4]http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: [5]https://mail.python.org/mailman/listinfo/tutor References Visible links 1. https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html 2. http://www.alan-g.me.uk/ 3. http://www.amazon.com/author/alan_gauld 4. http://www.flickr.com/photos/alangauldphotos 5. https://mail.python.org/mailman/listinfo/tutor From rikudou__sennin at live.com Fri Oct 6 14:41:12 2017 From: rikudou__sennin at live.com (adil gourinda) Date: Fri, 6 Oct 2017 18:41:12 +0000 Subject: [Tutor] Tkinter's Documentation In-Reply-To: <5d9e30ce-aca5-49cc-a389-5dd66be27f77@email.android.com> References: , <5d9e30ce-aca5-49cc-a389-5dd66be27f77@email.android.com> Message-ID: Thanks, Now it is more clear, So I have to wait for the next version of tkinter if I want someone to listen to me. ________________________________ From: Alan Gauld Sent: Friday, October 6, 2017 1:39:14 PM To: adil gourinda Cc: tutor at python.org Subject: Re: [Tutor] Tkinter's Documentation Tkinter is very much alive and has an active developers list. However Tkinter is just a wrapper around Tcl/Tk so there is only activity in Tkinter when there is something new in Tk. But both projects are going strong. On 6 Oct 2017 12:45 pm, adil gourinda wrote: This link exists in "python library reference under the name "Tkinter reference: a GUI for Python", but after sending an email to them (I wanted to participate with some suggestions) I didn't get any response, So I thought that it is like a dead project, for this reason why I asked for an alternative. Sorry if my first message was not so clear ________________________________ From: Tutor on behalf of Alan Gauld via Tutor Sent: Friday, October 6, 2017 10:05:27 AM To: tutor at python.org Subject: Re: [Tutor] Tkinter's Documentation On 05/10/17 17:38, adil gourinda wrote: > Where can i find the reference documentation of "Tkinter" Tkinter is a module, not part of the language, so it is documented in the modules section. But the documentation is not 100% complete and for details you often need to look at the Tk/Tcl documentation too. Also there are several Tkinter web sites that offer additional information. One of the official web pages has links to them. This is probably the most comprehensive: https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html And there is a PDF link on their web page. -- 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 at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From mysecretrobotfactory at gmail.com Fri Oct 6 14:26:41 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 11:26:41 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: Hi Eryk Sun: I started out with what you gave me: >code starts class SYSTEM_INFO(ctypes.Structure): """https://msdn.microsoft.com/en-us/library/ms724958""" class _U(ctypes.Union): class _S(ctypes.Structure): _fields_ = (('wProcessorArchitecture', WORD), ('wReserved', WORD)) _fields_ = (('dwOemId', DWORD), # obsolete ('_s', _S)) _anonymous_ = ('_s',) _fields_ = (('_u', _U), ('dwPageSize', DWORD), ('lpMinimumApplicationAddress', LPVOID), ('lpMaximumApplicationAddress', LPVOID), ('dwActiveProcessorMask', DWORD_PTR), ('dwNumberOfProcessors', DWORD), ('dwProcessorType', DWORD), ('dwAllocationGranularity', DWORD), ('wProcessorLevel', WORD), ('wProcessorRevision', WORD)) _anonymous_ = ('_u',) LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO) > code ends I am trying to acquire "lpMinimumApplicationAddress" and "lpMaximumApplicationAddress" from system_info, so I did this, >code Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) Kernel32.GetSystemInfo(LPSYSTEM_INFO) print(LPLPSYSTEM_INFO.lpMinimumApplicationAddress) >code ends and then it says Traceback (most recent call last): File "C:/Users/AwesomeGuy/Google Drive/My life of hacking/SWTOR/mah scanner/with_eryk_sun_s_help_peace by peace.py", line 55, in Kernel32.GetSystemInfo(LPSYSTEM_INFO) ctypes.ArgumentError: argument 1: : Don't know how to convert parameter 1 thanks for reading! On Thu, Oct 5, 2017 at 1:13 PM, eryk sun wrote: > On Thu, Oct 5, 2017 at 8:27 PM, Michael C > wrote: > > > > How do I see the values of each field? This doesn't work. > > > > print(PMEMORY_BASIC_INFORMATION.Protect) > > Create an instance of MEMORY_BASIC_INFORMATION and pass a pointer to > it via byref(). For example, the following queries the region of > memory of the VirtualQuery function itself. > > kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) > > MEM_COMMIT = 0x1000 > PAGE_EXECUTE_READ = 0x20 > PAGE_EXECUTE_WRITECOPY = 0x80 > > VirtualQuery = kernel32.VirtualQuery > VirtualQuery.restype = SIZE_T > VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T) > > mbi = MEMORY_BASIC_INFORMATION() > VirtualQuery(VirtualQuery, ctypes.byref(mbi), ctypes.sizeof(mbi)) > > >>> mbi.AllocationBase == kernel32._handle > True > >>> mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY > True > >>> mbi.BaseAddress > 140703181352960 > >>> mbi.RegionSize > 364544 > >>> mbi.State == MEM_COMMIT > True > >>> mbi.Protect == PAGE_EXECUTE_READ > True > From mysecretrobotfactory at gmail.com Fri Oct 6 14:43:04 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 11:43:04 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: Sorry but I dont understand this line: mbi = MEMORY_BASIC_INFORMATION() This creates a instance of the class? Also, I thought with VirtualQueryEx, what you need for it is a handle, which I acquire from this Process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, False, PID) and then feed it to the function like so: VirtualQuery(Process, ctypes.byref(mbi), ctypes.sizeof(mbi)) I know it doesn't work. But what are these lines for? They don't look like handle to me: VirtualQuery = kernel32.VirtualQuery VirtualQuery.restype = SIZE_T VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T) thanks ! On Thu, Oct 5, 2017 at 1:13 PM, eryk sun wrote: > On Thu, Oct 5, 2017 at 8:27 PM, Michael C > wrote: > > > > How do I see the values of each field? This doesn't work. > > > > print(PMEMORY_BASIC_INFORMATION.Protect) > > Create an instance of MEMORY_BASIC_INFORMATION and pass a pointer to > it via byref(). For example, the following queries the region of > memory of the VirtualQuery function itself. > > kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) > > MEM_COMMIT = 0x1000 > PAGE_EXECUTE_READ = 0x20 > PAGE_EXECUTE_WRITECOPY = 0x80 > > VirtualQuery = kernel32.VirtualQuery > VirtualQuery.restype = SIZE_T > VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T) > > mbi = MEMORY_BASIC_INFORMATION() > VirtualQuery(VirtualQuery, ctypes.byref(mbi), ctypes.sizeof(mbi)) > > >>> mbi.AllocationBase == kernel32._handle > True > >>> mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY > True > >>> mbi.BaseAddress > 140703181352960 > >>> mbi.RegionSize > 364544 > >>> mbi.State == MEM_COMMIT > True > >>> mbi.Protect == PAGE_EXECUTE_READ > True > From mysecretrobotfactory at gmail.com Fri Oct 6 16:12:32 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 13:12:32 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: Hi all: How do I create a buffer, or rather, is a buffer just a variable? How do I create a pointer to it? This code ran fine (thanks to you, Eryk, I now know about how to work VirtualQueryEx work) until when I ran the read process memory part. I think I am not feeding the function properly. Please look at the red part of this code Thanks! >code starts here mbi = MEMORY_BASIC_INFORMATION() sysinfo.lpMinimumApplicationAddress print('VirtualQueryEx ran properly?',Kernel32.VirtualQueryEx(Process, \ sysinfo.lpMinimumApplicationAddress, ctypes.byref(mbi),ctypes.sizeof(mbi))) print('') print('mbi start') print('mbi.BaseAddress: ',mbi.BaseAddress) print('mbi.AllocationBase: ',mbi.AllocationBase) print('mbi.AllocationProtect: ',mbi.AllocationProtect) print('mbi.RegionSize: ',mbi.RegionSize) print('mbi.State: ',mbi.State) print('mbi.Protect: ', mbi.Protect) print('mbi.Type: ',mbi.Type) buffer = ctypes.create_string_buffer(4) bufferSize = (ctypes.sizeof(buffer)) ReadProcessMemory = Kernel32.ReadProcessMemory if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, None): print('buffer is: ',buffer) else: print('something is wrong') On Fri, Oct 6, 2017 at 12:03 PM, eryk sun wrote: > On Fri, Oct 6, 2017 at 7:43 PM, Michael C > wrote: > > Sorry but I dont understand this line: > > > > mbi = MEMORY_BASIC_INFORMATION() > > > > This creates a instance of the class? > > Yes, and this allocates sizeof(MEMORY_BASIC_INFORMATION) bytes at > addressof(mbi), which you pass to a function by reference via > byref(mbi). > > > Also, I thought with VirtualQueryEx, what you need for it > > is a handle, which I acquire from this > > Process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_ > READ, > > False, PID) > > My example called VirtualQuery, not VirtualQueryEx. Internally > VirtualQuery calls VirtualQueryEx using the pseudo handle > (HANDLE)(-1), which refers to the current process. > > > and then feed it to the function like so: > > > > VirtualQuery(Process, ctypes.byref(mbi), ctypes.sizeof(mbi)) > > > > I know it doesn't work. But what are these lines for? They don't look > like > > handle to me: > > > > VirtualQuery = kernel32.VirtualQuery > > VirtualQuery.restype = SIZE_T > > VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T) > > In the above, I'm setting the function pointer's argtypes attribute to > the types of the 3 parameters that VirtualQuery takes: the target > address (i.e. LPVOID), a pointer to the buffer (i.e. > PMEMORY_BASIC_INFORMATION), and the size of the buffer (SIZE_T). This > is to allow ctypes to correctly check and convert arguments passed to > the function. > > VirtualQueryEx has four parameters, starting with the handle to the > target process, hProcess. The remaining 3 are the same as > VirtualQuery. > From eryksun at gmail.com Fri Oct 6 16:55:24 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Oct 2017 21:55:24 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Fri, Oct 6, 2017 at 9:12 PM, Michael C wrote: > > How do I create a buffer, or rather, is a buffer just a variable? A buffer is a block of memory for an I/O operation. For example, if you need to read a 4-byte (32-bit) integer at an address in another process, the 'buffer' could be ctypes.c_int32(). In general, to read an arbitrary-sized block of memory, use ctypes.create_string_buffer() to create a char array. > How do I create a pointer to it? Pass it byref(). > print('mbi.State: ',mbi.State) Check whether mbi.State is MEM_COMMIT before trying to read it. If it's MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail. > buffer = ctypes.create_string_buffer(4) > bufferSize = (ctypes.sizeof(buffer)) > > ReadProcessMemory = Kernel32.ReadProcessMemory > > if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, None): > print('buffer is: ',buffer) > else: > print('something is wrong') Don't print "something is wrong". You're capturing the thread's last error value, so use it to raise an informative exception. For example: if not success: raise ctypes.WinError(ctypes.get_last_error()) From eryksun at gmail.com Fri Oct 6 17:38:01 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Oct 2017 22:38:01 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Fri, Oct 6, 2017 at 10:06 PM, Michael C wrote: > like this? > > buffer = ctypes.byref(ctypes.create_string_buffer(4)) No, the buffer is the array created by create_string_buffer, which you pass byref(). In the following example I create a `test` buffer that contains "spam", and I use the pseudo-handle from GetCurrentProcess with ReadProcessMemory to read this buffer into a target `buffer`. It's silly to do this in the current process, but it's just an example. import ctypes from ctypes.wintypes import HANDLE, LPVOID kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) SIZE_T = ctypes.c_size_t LPSIZE_T = ctypes.POINTER(SIZE_T) kernel32.GetCurrentProcess.restype = HANDLE kernel32.ReadProcessMemory.argtypes = (HANDLE, LPVOID, LPVOID, SIZE_T, LPSIZE_T) hProcess = kernel32.GetCurrentProcess() test = ctypes.create_string_buffer(b'spam') address = ctypes.addressof(test) buffer = ctypes.create_string_buffer(4) nread = SIZE_T() success = kernel32.ReadProcessMemory(hProcess, address, ctypes.byref(buffer), ctypes.sizeof(buffer), ctypes.byref(nread)) if not success: raise ctypes.WinError(ctypes.get_last_error()) print(buffer[:]) From eryksun at gmail.com Fri Oct 6 17:53:54 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Oct 2017 22:53:54 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Fri, Oct 6, 2017 at 10:26 PM, Michael C wrote: > > base = mbi.BaseAddress > buffer = ctypes.c_int32() > buffer_pointer = ctypes.byref(buffer) > ReadProcessMemory = Kernel32.ReadProcessMemory > > if ReadProcessMemory(Process, base, buffer_pointer, mbi.RegionSize, None): > print('buffer is: ',buffer) > else: > raise ctypes.WinError(ctypes.get_last_error()) If you need to read RegionSize bytes, then you have to allocate a buffer that's RegionSize bytes: buffer = ctypes.create_string_buffer(mbi.RegionSize) Or use a smaller buffer and loop until the total number of bytes read is RegionSize. Also, remember to check that the state is MEM_COMMIT. You cannot read an address range that's free or reserved. It must be committed, i.e. backed by physical storage. From eryksun at gmail.com Fri Oct 6 18:29:36 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 6 Oct 2017 23:29:36 +0100 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: On Fri, Oct 6, 2017 at 11:05 PM, Michael C wrote: > For this read process memory, if I am trying compose a LPCVOID > lpBaseAddress, am I not making a variable that equals to mbi.BaseAddress, > and then making a pointer pointing to it? > > start_address = mbi.BaseAddress > LPCVOID = ctypes.byref(start_address) LPCVOID is a pointer type; don't use it as a variable name because it's confusing to someone who's reading your code. The `BaseAddress` field is an LPVOID, which is an alias for ctypes.c_void_p. Simple C types such as c_void_p are automatically converted to Python native types such as int, bytes, and str. It's fine that mbi.BaseAddress is a Python int. With argtypes defined for ReadProcessMemory, ctypes will convert the int back to a void pointer for you automatically. From mysecretrobotfactory at gmail.com Fri Oct 6 17:06:50 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 14:06:50 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: like this? buffer = ctypes.byref(ctypes.create_string_buffer(4)) On Fri, Oct 6, 2017 at 1:55 PM, eryk sun wrote: > On Fri, Oct 6, 2017 at 9:12 PM, Michael C > wrote: > > > > How do I create a buffer, or rather, is a buffer just a variable? > > A buffer is a block of memory for an I/O operation. For example, if > you need to read a 4-byte (32-bit) integer at an address in another > process, the 'buffer' could be ctypes.c_int32(). In general, to read > an arbitrary-sized block of memory, use ctypes.create_string_buffer() > to create a char array. > > > How do I create a pointer to it? > > Pass it byref(). > > > print('mbi.State: ',mbi.State) > > Check whether mbi.State is MEM_COMMIT before trying to read it. If > it's MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail. > > > buffer = ctypes.create_string_buffer(4) > > bufferSize = (ctypes.sizeof(buffer)) > > > > ReadProcessMemory = Kernel32.ReadProcessMemory > > > > if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, > None): > > print('buffer is: ',buffer) > > else: > > print('something is wrong') > > Don't print "something is wrong". You're capturing the thread's last > error value, so use it to raise an informative exception. For example: > > if not success: > raise ctypes.WinError(ctypes.get_last_error()) > From mysecretrobotfactory at gmail.com Fri Oct 6 17:26:27 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 14:26:27 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: This is my updated version, it still doesn't work :( base = mbi.BaseAddress buffer = ctypes.c_int32() buffer_pointer = ctypes.byref(buffer) ReadProcessMemory = Kernel32.ReadProcessMemory if ReadProcessMemory(Process, base, buffer_pointer, mbi.RegionSize, None): print('buffer is: ',buffer) else: raise ctypes.WinError(ctypes.get_last_error()) On Fri, Oct 6, 2017 at 2:06 PM, Michael C wrote: > like this? > > buffer = ctypes.byref(ctypes.create_string_buffer(4)) > > On Fri, Oct 6, 2017 at 1:55 PM, eryk sun wrote: > >> On Fri, Oct 6, 2017 at 9:12 PM, Michael C >> wrote: >> > >> > How do I create a buffer, or rather, is a buffer just a variable? >> >> A buffer is a block of memory for an I/O operation. For example, if >> you need to read a 4-byte (32-bit) integer at an address in another >> process, the 'buffer' could be ctypes.c_int32(). In general, to read >> an arbitrary-sized block of memory, use ctypes.create_string_buffer() >> to create a char array. >> >> > How do I create a pointer to it? >> >> Pass it byref(). >> >> > print('mbi.State: ',mbi.State) >> >> Check whether mbi.State is MEM_COMMIT before trying to read it. If >> it's MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail. >> >> > buffer = ctypes.create_string_buffer(4) >> > bufferSize = (ctypes.sizeof(buffer)) >> > >> > ReadProcessMemory = Kernel32.ReadProcessMemory >> > >> > if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, >> None): >> > print('buffer is: ',buffer) >> > else: >> > print('something is wrong') >> >> Don't print "something is wrong". You're capturing the thread's last >> error value, so use it to raise an informative exception. For example: >> >> if not success: >> raise ctypes.WinError(ctypes.get_last_error()) >> > > From mysecretrobotfactory at gmail.com Fri Oct 6 18:05:42 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 15:05:42 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: For this read process memory, if I am trying compose a LPCVOID lpBaseAddress, am I not making a variable that equals to mbi.BaseAddress, and then making a pointer pointing to it? start_address = mbi.BaseAddress LPCVOID = ctypes.byref(start_address) ? But I get this start = ctypes.byref(mbi.BaseAddress) TypeError: byref() argument must be a ctypes instance, not 'int' On Fri, Oct 6, 2017 at 2:53 PM, eryk sun wrote: > On Fri, Oct 6, 2017 at 10:26 PM, Michael C > wrote: > > > > base = mbi.BaseAddress > > buffer = ctypes.c_int32() > > buffer_pointer = ctypes.byref(buffer) > > ReadProcessMemory = Kernel32.ReadProcessMemory > > > > if ReadProcessMemory(Process, base, buffer_pointer, mbi.RegionSize, > None): > > print('buffer is: ',buffer) > > else: > > raise ctypes.WinError(ctypes.get_last_error()) > > If you need to read RegionSize bytes, then you have to allocate a > buffer that's RegionSize bytes: > > buffer = ctypes.create_string_buffer(mbi.RegionSize) > > Or use a smaller buffer and loop until the total number of bytes read > is RegionSize. > > Also, remember to check that the state is MEM_COMMIT. You cannot read > an address range that's free or reserved. It must be committed, i.e. > backed by physical storage. > From mysecretrobotfactory at gmail.com Fri Oct 6 18:36:23 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 6 Oct 2017 15:36:23 -0700 Subject: [Tutor] ctypes wintypes In-Reply-To: References: Message-ID: I think I pieced together what you have been helping me with, but this still raise a error I have been loosely following this guide: https://www.codeproject.com/articles/716227/csharp-how-to-scan-a-process-memory >code start. import ctypes from ctypes.wintypes import WORD, DWORD, LPVOID PVOID = LPVOID SIZE_T = ctypes.c_size_t # https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulonglong): DWORD_PTR = ctypes.c_ulonglong elif ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong): DWORD_PTR = ctypes.c_ulong class SYSTEM_INFO(ctypes.Structure): """https://msdn.microsoft.com/en-us/library/ms724958""" class _U(ctypes.Union): class _S(ctypes.Structure): _fields_ = (('wProcessorArchitecture', WORD), ('wReserved', WORD)) _fields_ = (('dwOemId', DWORD), # obsolete ('_s', _S)) _anonymous_ = ('_s',) _fields_ = (('_u', _U), ('dwPageSize', DWORD), ('lpMinimumApplicationAddress', LPVOID), ('lpMaximumApplicationAddress', LPVOID), ('dwActiveProcessorMask', DWORD_PTR), ('dwNumberOfProcessors', DWORD), ('dwProcessorType', DWORD), ('dwAllocationGranularity', DWORD), ('wProcessorLevel', WORD), ('wProcessorRevision', WORD)) _anonymous_ = ('_u',) LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO) Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) Kernel32.GetSystemInfo.restype = None Kernel32.GetSystemInfo.argtypes = (LPSYSTEM_INFO,) sysinfo = SYSTEM_INFO() Kernel32.GetSystemInfo(ctypes.byref(sysinfo)) print(sysinfo.lpMinimumApplicationAddress) print(sysinfo.lpMaximumApplicationAddress) # maybe it will change, maybe it won't. Assuming it won't. # 2nd, get Open process. PID = 1234 PROCESS_QUERY_INFORMATION = 0x0400 PROCESS_VM_READ = 0x0010 Process = Kernel32.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, False, PID) print('process:', Process) # 3rd class MEMORY_BASIC_INFORMATION(ctypes.Structure): """https://msdn.microsoft.com/en-us/library/aa366775""" _fields_ = (('BaseAddress', PVOID), ('AllocationBase', PVOID), ('AllocationProtect', DWORD), ('RegionSize', SIZE_T), ('State', DWORD), ('Protect', DWORD), ('Type', DWORD)) ##PMEMORY_BASIC_INFORMATION = ctypes.POINTER(MEMORY_BASIC_INFORMATION) mbi = MEMORY_BASIC_INFORMATION() ##sysinfo.lpMinimumApplicationAddress print('VirtualQueryEx ran properly?',Kernel32.VirtualQueryEx(Process, \ None, ctypes.byref(mbi),ctypes.sizeof(mbi))) # sysinfo.lpMinimumApplicationAddress replaced by None print('') print('mbi start') print('mbi.BaseAddress: ',mbi.BaseAddress) print('mbi.AllocationBase: ',mbi.AllocationBase) print('mbi.AllocationProtect: ',mbi.AllocationProtect) print('mbi.RegionSize: ',mbi.RegionSize) print('mbi.State: ',mbi.State) print('mbi.Protect: ', mbi.Protect) print('mbi.Type: ',mbi.Type) buffer = ctypes.create_string_buffer(mbi.RegionSize) nread = SIZE_T() start = ctypes.c_void_p(mbi.BaseAddress) ##start_pointer = ctypes.byref(start) ReadProcessMemory = Kernel32.ReadProcessMemory if ReadProcessMemory(Process, start, ctypes.byref(buffer), \ ctypes.sizeof(buffer), ctypes.byref(nread)): print('buffer is: ',buffer) else: raise ctypes.WinError(ctypes.get_last_error()) # once I figure out read process memory, I'll combine it with virtual process memory. # if they don't equal to that, then it's time to move to the next thing? # Don't do read memory yet. # make it traverse through all memory and print out when protect and state # are both true. ## ##MEM_COMMIT = 0x00001000; ##PAGE_READWRITE = 0x04; ## ##current_address = sysinfo.lpMinimumApplicationAddress ##end_address = sysinfo.lpMaximumApplicationAddress ## ##while current_address < end_address: ## Kernel32.VirtualQueryEx(Process, \ ## current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) ## ## if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : ## print(current_address) ## print('Both are true') ## ## ## current_address += mbi.RegionSize On Fri, Oct 6, 2017 at 3:29 PM, eryk sun wrote: > On Fri, Oct 6, 2017 at 11:05 PM, Michael C > wrote: > > For this read process memory, if I am trying compose a LPCVOID > > lpBaseAddress, am I not making a variable that equals to > mbi.BaseAddress, > > and then making a pointer pointing to it? > > > > start_address = mbi.BaseAddress > > LPCVOID = ctypes.byref(start_address) > > LPCVOID is a pointer type; don't use it as a variable name because > it's confusing to someone who's reading your code. > > The `BaseAddress` field is an LPVOID, which is an alias for > ctypes.c_void_p. Simple C types such as c_void_p are automatically > converted to Python native types such as int, bytes, and str. It's > fine that mbi.BaseAddress is a Python int. With argtypes defined for > ReadProcessMemory, ctypes will convert the int back to a void pointer > for you automatically. > From mysecretrobotfactory at gmail.com Sat Oct 7 17:00:25 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 7 Oct 2017 14:00:25 -0700 Subject: [Tutor] How do I scan memory for singles, doubles and so on? Message-ID: Hi all: I am working on a memory scanner, and the source code and output is as following: Now, I know why my buffer from read process memory looks like values such as "67108864" ; it's because I read into the buffer entire chunk of memory at a time, because I fed read process memory this: "mbi.RegionSize" Now, how do I read for values such as doubles? I am guessing I need to use a for loop to scan for small bits of memory chunk at a time. Is there a way to do it? Thanks! >output starts buffer is: c_ulong(0) buffer is: c_ulong(0) buffer is: c_ulong(6385664) buffer is: c_ulong(67108864) buffer is: c_ulong(7761920) buffer is: c_ulong(7798784) buffer is: c_ulong(7872512) buffer is: c_ulong(8007680) buffer is: c_ulong(8044544) buffer is: c_ulong(8069120) buffer is: c_ulong(8216576) buffer is: c_ulong(0) buffer is: c_ulong(0) buffer is: c_ulong(3976) buffer is: c_ulong(0) buffer is: c_ulong(0) buffer is: c_ulong(1318755581) buffer is: c_ulong(0) buffer is: c_ulong(0) buffer is: c_ulong(0) buffer is: c_ulong(0) > code starts buffer = ctypes.c_uint() nread = SIZE_T() start = ctypes.c_void_p(mbi.BaseAddress) ReadProcessMemory = Kernel32.ReadProcessMemory MEM_COMMIT = 0x00001000; PAGE_READWRITE = 0x04; current_address = sysinfo.lpMinimumApplicationAddress end_address = sysinfo.lpMaximumApplicationAddress while current_address < end_address: Kernel32.VirtualQueryEx(Process, \ current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : if ReadProcessMemory(Process, current_address, ctypes.byref(buffer), \ ctypes.sizeof(buffer), ctypes.byref(nread)): print('buffer is: ',buffer) else: raise ctypes.WinError(ctypes.get_last_error()) current_address += mbi.RegionSize From mysecretrobotfactory at gmail.com Sat Oct 7 17:10:40 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 7 Oct 2017 14:10:40 -0700 Subject: [Tutor] How do I scan memory for singles, doubles and so on? In-Reply-To: References: Message-ID: Or to put it better, I think, it's How do I set up ReadProcessMemory, so that it returns a double instead of 129819721. On Sat, Oct 7, 2017 at 2:00 PM, Michael C wrote: > Hi all: > > I am working on a memory scanner, and the source code and output is as > following: > > Now, I know why my buffer from read process memory looks like values such > as "67108864" ; it's because I read into the buffer entire chunk of memory > at a time, because I fed read process memory this: "mbi.RegionSize" > > Now, how do I read for values such as doubles? > I am guessing I need to use a for loop to scan for small bits of memory > chunk > at a time. > > Is there a way to do it? > > Thanks! > > > > > >output starts > > buffer is: c_ulong(0) > buffer is: c_ulong(0) > buffer is: c_ulong(6385664) > buffer is: c_ulong(67108864) > buffer is: c_ulong(7761920) > buffer is: c_ulong(7798784) > buffer is: c_ulong(7872512) > buffer is: c_ulong(8007680) > buffer is: c_ulong(8044544) > buffer is: c_ulong(8069120) > buffer is: c_ulong(8216576) > buffer is: c_ulong(0) > buffer is: c_ulong(0) > buffer is: c_ulong(3976) > buffer is: c_ulong(0) > buffer is: c_ulong(0) > buffer is: c_ulong(1318755581) > buffer is: c_ulong(0) > buffer is: c_ulong(0) > buffer is: c_ulong(0) > buffer is: c_ulong(0) > > > code starts > > buffer = ctypes.c_uint() > nread = SIZE_T() > > start = ctypes.c_void_p(mbi.BaseAddress) > > ReadProcessMemory = Kernel32.ReadProcessMemory > > MEM_COMMIT = 0x00001000; > PAGE_READWRITE = 0x04; > > current_address = sysinfo.lpMinimumApplicationAddress > end_address = sysinfo.lpMaximumApplicationAddress > > while current_address < end_address: > Kernel32.VirtualQueryEx(Process, \ > current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) > > if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : > > if ReadProcessMemory(Process, current_address, > ctypes.byref(buffer), \ > ctypes.sizeof(buffer), ctypes.byref(nread)): > print('buffer is: ',buffer) > else: > raise ctypes.WinError(ctypes.get_last_error()) > > current_address += mbi.RegionSize > > From mats at wichmann.us Sat Oct 7 21:58:06 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 07 Oct 2017 19:58:06 -0600 Subject: [Tutor] How do I scan memory for singles, doubles and so on? In-Reply-To: References: Message-ID: <7F65B1E7-125E-4B18-B4BA-7EE2F24285E0@wichmann.us> it might help if you mention what you are trying to do. if it is forensics, there a bunch of python tools in that area. your problem may already have solutions you could use. On October 7, 2017 3:00:25 PM MDT, Michael C wrote: >Hi all: > >I am working on a memory scanner, and the source code and output is as >following: > >Now, I know why my buffer from read process memory looks like values >such >as "67108864" ; it's because I read into the buffer entire chunk of >memory >at a time, because I fed read process memory this: "mbi.RegionSize" > >Now, how do I read for values such as doubles? >I am guessing I need to use a for loop to scan for small bits of memory >chunk >at a time. > >Is there a way to do it? > >Thanks! > > > > >>output starts > >buffer is: c_ulong(0) >buffer is: c_ulong(0) >buffer is: c_ulong(6385664) >buffer is: c_ulong(67108864) >buffer is: c_ulong(7761920) >buffer is: c_ulong(7798784) >buffer is: c_ulong(7872512) >buffer is: c_ulong(8007680) >buffer is: c_ulong(8044544) >buffer is: c_ulong(8069120) >buffer is: c_ulong(8216576) >buffer is: c_ulong(0) >buffer is: c_ulong(0) >buffer is: c_ulong(3976) >buffer is: c_ulong(0) >buffer is: c_ulong(0) >buffer is: c_ulong(1318755581) >buffer is: c_ulong(0) >buffer is: c_ulong(0) >buffer is: c_ulong(0) >buffer is: c_ulong(0) > >> code starts > >buffer = ctypes.c_uint() >nread = SIZE_T() > >start = ctypes.c_void_p(mbi.BaseAddress) > >ReadProcessMemory = Kernel32.ReadProcessMemory > >MEM_COMMIT = 0x00001000; >PAGE_READWRITE = 0x04; > >current_address = sysinfo.lpMinimumApplicationAddress >end_address = sysinfo.lpMaximumApplicationAddress > >while current_address < end_address: > Kernel32.VirtualQueryEx(Process, \ > current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) > > if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : > > if ReadProcessMemory(Process, current_address, >ctypes.byref(buffer), \ > ctypes.sizeof(buffer), ctypes.byref(nread)): > print('buffer is: ',buffer) > else: > raise ctypes.WinError(ctypes.get_last_error()) > > current_address += mbi.RegionSize >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Sent from my Android device with K-9 Mail. Please excuse my brevity. From mysecretrobotfactory at gmail.com Sat Oct 7 22:38:55 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 7 Oct 2017 19:38:55 -0700 Subject: [Tutor] How do I scan memory for singles, doubles and so on? In-Reply-To: <7F65B1E7-125E-4B18-B4BA-7EE2F24285E0@wichmann.us> References: <7F65B1E7-125E-4B18-B4BA-7EE2F24285E0@wichmann.us> Message-ID: Oh I am trying to write my own memory scanner, because I thought the Cheat Engine is pretty neat and I am just trying make one for myself. Onto the problem, I think what happens with Readprocessmemory is that BOOL WINAPI ReadProcessMemory( _In_ HANDLE hProcess, _In_ LPCVOID lpBaseAddress, _Out_ LPVOID lpBuffer, _In_ SIZE_T nSize, _Out_ SIZE_T *lpNumberOfBytesRead ); for LPVOID lpbuffer, it should be a buffer = ctypes.c_double because i am trying to search for a double. However, the interpreter gives me this: ReadProcessMemory(Process, current_address, ctypes.byref(buffer), \ TypeError: byref() argument must be a ctypes instance, not '_ctypes.PyCSimpleType' so I am using buffer = ctypes.c_uint() instead. It returns things like "c_ulong(2006549856)" , though. 2nd, I believe _In_ SIZE_T nSize, means I tell the interpreter to read that much data, which means I can use this parameter to get doubles, which is what I want! However, I am using ctypes.sizeof(buffer) for it, so, I need either to change my buffer to a double, or to tell this parameter to search for doubles somehow. Am I on the right track? Thanks! On Sat, Oct 7, 2017 at 6:58 PM, Mats Wichmann wrote: > it might help if you mention what you are trying to do. if it is > forensics, there a bunch of python tools in that area. your problem may > already have solutions you could use. > > On October 7, 2017 3:00:25 PM MDT, Michael C com> wrote: > >Hi all: > > > >I am working on a memory scanner, and the source code and output is as > >following: > > > >Now, I know why my buffer from read process memory looks like values > >such > >as "67108864" ; it's because I read into the buffer entire chunk of > >memory > >at a time, because I fed read process memory this: "mbi.RegionSize" > > > >Now, how do I read for values such as doubles? > >I am guessing I need to use a for loop to scan for small bits of memory > >chunk > >at a time. > > > >Is there a way to do it? > > > >Thanks! > > > > > > > > > >>output starts > > > >buffer is: c_ulong(0) > >buffer is: c_ulong(0) > >buffer is: c_ulong(6385664) > >buffer is: c_ulong(67108864) > >buffer is: c_ulong(7761920) > >buffer is: c_ulong(7798784) > >buffer is: c_ulong(7872512) > >buffer is: c_ulong(8007680) > >buffer is: c_ulong(8044544) > >buffer is: c_ulong(8069120) > >buffer is: c_ulong(8216576) > >buffer is: c_ulong(0) > >buffer is: c_ulong(0) > >buffer is: c_ulong(3976) > >buffer is: c_ulong(0) > >buffer is: c_ulong(0) > >buffer is: c_ulong(1318755581) > >buffer is: c_ulong(0) > >buffer is: c_ulong(0) > >buffer is: c_ulong(0) > >buffer is: c_ulong(0) > > > >> code starts > > > >buffer = ctypes.c_uint() > >nread = SIZE_T() > > > >start = ctypes.c_void_p(mbi.BaseAddress) > > > >ReadProcessMemory = Kernel32.ReadProcessMemory > > > >MEM_COMMIT = 0x00001000; > >PAGE_READWRITE = 0x04; > > > >current_address = sysinfo.lpMinimumApplicationAddress > >end_address = sysinfo.lpMaximumApplicationAddress > > > >while current_address < end_address: > > Kernel32.VirtualQueryEx(Process, \ > > current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) > > > > if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : > > > > if ReadProcessMemory(Process, current_address, > >ctypes.byref(buffer), \ > > ctypes.sizeof(buffer), ctypes.byref(nread)): > > print('buffer is: ',buffer) > > else: > > raise ctypes.WinError(ctypes.get_last_error()) > > > > current_address += mbi.RegionSize > >_______________________________________________ > >Tutor maillist - Tutor at python.org > >To unsubscribe or change subscription options: > >https://mail.python.org/mailman/listinfo/tutor > > -- > Sent from my Android device with K-9 Mail. Please excuse my brevity. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Sat Oct 7 23:18:55 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 7 Oct 2017 20:18:55 -0700 Subject: [Tutor] How do I scan memory for singles, doubles and so on? In-Reply-To: References: <7F65B1E7-125E-4B18-B4BA-7EE2F24285E0@wichmann.us> Message-ID: I am following some examples online such as this one: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce0cc398-2b96-4688-b8a4-b5f4c9ebc064/memory-searcher-with-virtualqueryex-and-readprocessmemory?forum=vclanguage i think I got most of it right, so this following part is what I would like you to look at: DWORD read = 0; LPVOID buffer = 0 (ReadProcessMemory(hackProcess, (void*)start, &buffer, sizeof(int), &read) So, what's the Python equivalent statements for sizeof(int) ? On Sat, Oct 7, 2017 at 7:38 PM, Michael C wrote: > Oh I am trying to write my own memory scanner, because I thought the Cheat > Engine is pretty neat and I am just trying make one for myself. > > Onto the problem, I think what happens with Readprocessmemory is that > > BOOL WINAPI ReadProcessMemory( > _In_ HANDLE hProcess, > _In_ LPCVOID lpBaseAddress, _Out_ LPVOID lpBuffer, > _In_ SIZE_T nSize, > _Out_ SIZE_T *lpNumberOfBytesRead > ); > > > for LPVOID lpbuffer, it should be a > > buffer = ctypes.c_double > > because i am trying to search for a double. > However, the interpreter gives me this: > > ReadProcessMemory(Process, current_address, ctypes.byref(buffer), \ > TypeError: byref() argument must be a ctypes instance, not > '_ctypes.PyCSimpleType' > > > so I am using > buffer = ctypes.c_uint() > instead. It returns things like "c_ulong(2006549856)" , though. > > 2nd, I believe _In_ SIZE_T nSize, means I tell the interpreter to read > that much > data, which means I can use this parameter to get doubles, which is what I > want! > > However, I am using > > ctypes.sizeof(buffer) > > for it, so, I need either to change my buffer to a double, or to tell this > parameter to search for > doubles somehow. > > > Am I on the right track? > > > Thanks! > > > > On Sat, Oct 7, 2017 at 6:58 PM, Mats Wichmann wrote: > >> it might help if you mention what you are trying to do. if it is >> forensics, there a bunch of python tools in that area. your problem may >> already have solutions you could use. >> >> On October 7, 2017 3:00:25 PM MDT, Michael C < >> mysecretrobotfactory at gmail.com> wrote: >> >Hi all: >> > >> >I am working on a memory scanner, and the source code and output is as >> >following: >> > >> >Now, I know why my buffer from read process memory looks like values >> >such >> >as "67108864" ; it's because I read into the buffer entire chunk of >> >memory >> >at a time, because I fed read process memory this: "mbi.RegionSize" >> > >> >Now, how do I read for values such as doubles? >> >I am guessing I need to use a for loop to scan for small bits of memory >> >chunk >> >at a time. >> > >> >Is there a way to do it? >> > >> >Thanks! >> > >> > >> > >> > >> >>output starts >> > >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(6385664) >> >buffer is: c_ulong(67108864) >> >buffer is: c_ulong(7761920) >> >buffer is: c_ulong(7798784) >> >buffer is: c_ulong(7872512) >> >buffer is: c_ulong(8007680) >> >buffer is: c_ulong(8044544) >> >buffer is: c_ulong(8069120) >> >buffer is: c_ulong(8216576) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(3976) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(1318755581) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(0) >> >buffer is: c_ulong(0) >> > >> >> code starts >> > >> >buffer = ctypes.c_uint() >> >nread = SIZE_T() >> > >> >start = ctypes.c_void_p(mbi.BaseAddress) >> > >> >ReadProcessMemory = Kernel32.ReadProcessMemory >> > >> >MEM_COMMIT = 0x00001000; >> >PAGE_READWRITE = 0x04; >> > >> >current_address = sysinfo.lpMinimumApplicationAddress >> >end_address = sysinfo.lpMaximumApplicationAddress >> > >> >while current_address < end_address: >> > Kernel32.VirtualQueryEx(Process, \ >> > current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) >> > >> > if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : >> > >> > if ReadProcessMemory(Process, current_address, >> >ctypes.byref(buffer), \ >> > ctypes.sizeof(buffer), ctypes.byref(nread)): >> > print('buffer is: ',buffer) >> > else: >> > raise ctypes.WinError(ctypes.get_last_error()) >> > >> > current_address += mbi.RegionSize >> >_______________________________________________ >> >Tutor maillist - Tutor at python.org >> >To unsubscribe or change subscription options: >> >https://mail.python.org/mailman/listinfo/tutor >> >> -- >> Sent from my Android device with K-9 Mail. Please excuse my brevity. >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From mysecretrobotfactory at gmail.com Sat Oct 7 23:55:29 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 7 Oct 2017 20:55:29 -0700 Subject: [Tutor] How do I scan memory for singles, doubles and so on? In-Reply-To: References: <7F65B1E7-125E-4B18-B4BA-7EE2F24285E0@wichmann.us> Message-ID: update: I should have put down buffer = ctypes.c_double() instead of buffer = ctypes.c_double. Sorry all On Sat, Oct 7, 2017 at 8:18 PM, Michael C wrote: > I am following some examples online such as this one: > https://social.msdn.microsoft.com/Forums/vstudio/en-US/ > ce0cc398-2b96-4688-b8a4-b5f4c9ebc064/memory-searcher- > with-virtualqueryex-and-readprocessmemory?forum=vclanguage > > > i think I got most of it right, so this following part is what I would > like you to look at: > > DWORD read = 0; > LPVOID buffer = 0 > > (ReadProcessMemory(hackProcess, (void*)start, &buffer, sizeof(int), &read) > > > So, what's the Python equivalent statements for sizeof(int) ? > > > > > > > On Sat, Oct 7, 2017 at 7:38 PM, Michael C > wrote: > >> Oh I am trying to write my own memory scanner, because I thought the >> Cheat Engine is pretty neat and I am just trying make one for myself. >> >> Onto the problem, I think what happens with Readprocessmemory is that >> >> BOOL WINAPI ReadProcessMemory( >> _In_ HANDLE hProcess, >> _In_ LPCVOID lpBaseAddress, _Out_ LPVOID lpBuffer, >> _In_ SIZE_T nSize, >> _Out_ SIZE_T *lpNumberOfBytesRead >> ); >> >> >> for LPVOID lpbuffer, it should be a >> >> buffer = ctypes.c_double >> >> because i am trying to search for a double. >> However, the interpreter gives me this: >> >> ReadProcessMemory(Process, current_address, ctypes.byref(buffer), \ >> TypeError: byref() argument must be a ctypes instance, not >> '_ctypes.PyCSimpleType' >> >> >> so I am using >> buffer = ctypes.c_uint() >> instead. It returns things like "c_ulong(2006549856)" , though. >> >> 2nd, I believe _In_ SIZE_T nSize, means I tell the interpreter to >> read that much >> data, which means I can use this parameter to get doubles, which is what >> I want! >> >> However, I am using >> >> ctypes.sizeof(buffer) >> >> for it, so, I need either to change my buffer to a double, or to tell >> this parameter to search for >> doubles somehow. >> >> >> Am I on the right track? >> >> >> Thanks! >> >> >> >> On Sat, Oct 7, 2017 at 6:58 PM, Mats Wichmann wrote: >> >>> it might help if you mention what you are trying to do. if it is >>> forensics, there a bunch of python tools in that area. your problem may >>> already have solutions you could use. >>> >>> On October 7, 2017 3:00:25 PM MDT, Michael C < >>> mysecretrobotfactory at gmail.com> wrote: >>> >Hi all: >>> > >>> >I am working on a memory scanner, and the source code and output is as >>> >following: >>> > >>> >Now, I know why my buffer from read process memory looks like values >>> >such >>> >as "67108864" ; it's because I read into the buffer entire chunk of >>> >memory >>> >at a time, because I fed read process memory this: "mbi.RegionSize" >>> > >>> >Now, how do I read for values such as doubles? >>> >I am guessing I need to use a for loop to scan for small bits of memory >>> >chunk >>> >at a time. >>> > >>> >Is there a way to do it? >>> > >>> >Thanks! >>> > >>> > >>> > >>> > >>> >>output starts >>> > >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(6385664) >>> >buffer is: c_ulong(67108864) >>> >buffer is: c_ulong(7761920) >>> >buffer is: c_ulong(7798784) >>> >buffer is: c_ulong(7872512) >>> >buffer is: c_ulong(8007680) >>> >buffer is: c_ulong(8044544) >>> >buffer is: c_ulong(8069120) >>> >buffer is: c_ulong(8216576) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(3976) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(1318755581) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(0) >>> >buffer is: c_ulong(0) >>> > >>> >> code starts >>> > >>> >buffer = ctypes.c_uint() >>> >nread = SIZE_T() >>> > >>> >start = ctypes.c_void_p(mbi.BaseAddress) >>> > >>> >ReadProcessMemory = Kernel32.ReadProcessMemory >>> > >>> >MEM_COMMIT = 0x00001000; >>> >PAGE_READWRITE = 0x04; >>> > >>> >current_address = sysinfo.lpMinimumApplicationAddress >>> >end_address = sysinfo.lpMaximumApplicationAddress >>> > >>> >while current_address < end_address: >>> > Kernel32.VirtualQueryEx(Process, \ >>> > current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) >>> > >>> > if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : >>> > >>> > if ReadProcessMemory(Process, current_address, >>> >ctypes.byref(buffer), \ >>> > ctypes.sizeof(buffer), ctypes.byref(nread)): >>> > print('buffer is: ',buffer) >>> > else: >>> > raise ctypes.WinError(ctypes.get_last_error()) >>> > >>> > current_address += mbi.RegionSize >>> >_______________________________________________ >>> >Tutor maillist - Tutor at python.org >>> >To unsubscribe or change subscription options: >>> >https://mail.python.org/mailman/listinfo/tutor >>> >>> -- >>> Sent from my Android device with K-9 Mail. Please excuse my brevity. >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >>> >> >> > From mysecretrobotfactory at gmail.com Sun Oct 8 13:20:13 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sun, 8 Oct 2017 10:20:13 -0700 Subject: [Tutor] using while loop for read process memory Message-ID: Hi all: I have the following code, and somehow I must have fed the read process Memory incorrectly. what the code does is to check a region of memory to see whether or not it can be scanned. mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT If this is true,then it proceeds to scan the memory fro current_address to current_address + mbi.RegionSize. However, a strange thing happens: The loop runs twice successfully, and then it pops: raise ctypes.WinError(ctypes.get_last_error()) OSError: [WinError 299] Only part of a ReadProcessMemory or WriteProcessMemory request was completed. Now, I know the problem is not with VirtualQueryEx, because if I comment out the red part and just run VirtualQueryEx, it would actually skim through all regions without a single error. The red part is the problem. I have tried to modify the loop. Somehow, if I use this: index = current_address end = current_address + mbi.RegionSize - 7 Where the end is less by 7, the loop would not pop any error and it would finish the loop What did I do wrong? thanks! >code starts current_address = sysinfo.lpMinimumApplicationAddress end_address = sysinfo.lpMaximumApplicationAddress while current_address < end_address: Kernel32.VirtualQueryEx(Process, \ current_address, ctypes.byref(mbi),ctypes.sizeof(mbi)) if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT : print('This region can be scanned!') 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 current_address += mbi.RegionSize From mats at wichmann.us Sun Oct 8 15:16:24 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 8 Oct 2017 13:16:24 -0600 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: Message-ID: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> On 10/08/2017 11:20 AM, Michael C wrote: > Hi all: > Now, I know the problem is not with VirtualQueryEx, because if I comment out > the red part and just run VirtualQueryEx, it would actually skim through > all regions > without a single error. > > The red part is the problem. what red part? colors don't come through mailers that use text-based settings. This is an example of what your mail looks like to many of us: https://mail-archive.com/tutor at python.org/msg77570.html please explain in words. From mysecretrobotfactory at gmail.com Sun Oct 8 15:18:21 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sun, 8 Oct 2017 12:18:21 -0700 Subject: [Tutor] using while loop for read process memory In-Reply-To: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: 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 On Oct 8, 2017 12:16 PM, "Mats Wichmann" wrote: > On 10/08/2017 11:20 AM, Michael C wrote: > > Hi all: > > > Now, I know the problem is not with VirtualQueryEx, because if I comment > out > > the red part and just run VirtualQueryEx, it would actually skim through > > all regions > > without a single error. > > > > The red part is the problem. > > what red part? colors don't come through mailers that use text-based > settings. This is an example of what your mail looks like to many of us: > > https://mail-archive.com/tutor at python.org/msg77570.html > > please explain in words. > From mysecretrobotfactory at gmail.com Sun Oct 8 15:47:14 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sun, 8 Oct 2017 12:47:14 -0700 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: I'll explain better when I get on a pc. On Oct 8, 2017 12:18 PM, "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 > > On Oct 8, 2017 12:16 PM, "Mats Wichmann" wrote: > >> On 10/08/2017 11:20 AM, Michael C wrote: >> > Hi all: >> >> > Now, I know the problem is not with VirtualQueryEx, because if I >> comment out >> > the red part and just run VirtualQueryEx, it would actually skim through >> > all regions >> > without a single error. >> > >> > The red part is the problem. >> >> what red part? colors don't come through mailers that use text-based >> settings. This is an example of what your mail looks like to many of us: >> >> https://mail-archive.com/tutor at python.org/msg77570.html >> >> please explain in words. >> > From alan.gauld at yahoo.co.uk Sun Oct 8 19:46:19 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 9 Oct 2017 00:46:19 +0100 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: 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 From steve at pearwood.info Sun Oct 8 20:12:13 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 9 Oct 2017 11:12:13 +1100 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: Message-ID: <20171009001213.GD24769@ando.pearwood.info> I have no idea about ctypes or Windows, but it seems to me that you are creating a rod for your own back by using a while loop here. Why use a primitive, low-level looping construct when Python gives you much better tools? My *guess* is that somewhere you are miscalcuating when to stop, and trying to read beyond the valid region. Your code uses nested while loops. But since you already know the beginning and end of the loop, that is much better written as for-loops (and will be faster too). It's not clear to me how much memory you expect to be reading at a time. I *guess* that you read blocks of memory the size of mbi at a time. If your memory is: abcdefghijklmnopqrstuvwxyz... and mbi is (lets say) *six* chars long, then you want to read: abcdef ghijkl mnopqr stuvwx yz... Then, within each mbi-sized block, if each buffer is (say) *two* chars long, you want to read: ab cd ef Is that right? If not, you will have to adjust the following to better suit your intention. # Untested, as I don't run Windows. blocksize = ctypes.sizeof(mbi) buffer_blocksize = ctypes.sizeof(buffer) for current_address in range( sysinfo.lpMinimumApplicationAddress, sysinfo.lpMaximumApplicationAddress, blocksize ): # process the current address here Kernel32.VirtualQueryEx( Process, current_address, ctypes.byref(mbi), blocksize ) # Note that there's no need for a backslash \ to continue # lines inside open brackets and parentheses; by # convention such lines are indented extra to allow them # to stand out. Feel free to make it a bit more compact if # you prefer it that way. if mbi.Protect == PAGE_READWRITE and mbi.State == MEM_COMMIT: print('This region can be scanned!') # which region? for index in range( current_address, current_address + mbi.RegionSize, buffer_blocksize ): if ReadProcessMemory( Process, index, ctypes.byref(buffer), buffer_blocksize, ctypes.byref(nread) ): ## FIXME implement value comparison pass else: raise ctypes.WinError(ctypes.get_last_error()) Hope this helps. -- Steve From mysecretrobotfactory at gmail.com Sun Oct 8 19:49:31 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sun, 8 Oct 2017 16:49:31 -0700 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: thank for replying, but I am toast, so I'll reply tomorrow, thanks! 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Mon Oct 9 11:29:33 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 9 Oct 2017 09:29:33 -0600 Subject: [Tutor] How do I scan memory for singles, doubles and so on? In-Reply-To: References: <7F65B1E7-125E-4B18-B4BA-7EE2F24285E0@wichmann.us> Message-ID: <8812e61f-1ef3-a3b1-f1c8-1a8dc591316a@wichmann.us> On 10/07/2017 09:18 PM, Michael C wrote: > I am following some examples online such as this one: > https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce0cc398-2b96-4688-b8a4-b5f4c9ebc064/memory-searcher-with-virtualqueryex-and-readprocessmemory?forum=vclanguage > > > i think I got most of it right, so this following part is what I would like > you to look at: > > DWORD read = 0; > LPVOID buffer = 0 > > (ReadProcessMemory(hackProcess, (void*)start, &buffer, sizeof(int), &read) > > > So, what's the Python equivalent statements for sizeof(int) ? There isn't one, directly, since python types are dynamic (and once Python knows something is an int it can morph into a bigint later). But probably for your purposes, this would give what you want: Python 3.6.2 (default, Sep 22 2017, 08:28:09) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> ctypes.sizeof(ctypes.c_int) 4 >>> ctypes.sizeof(ctypes.c_long) 8 >>> ctypes.sizeof(ctypes.c_float) 4 >>> ctypes.sizeof(ctypes.c_double) 8 >>> From newatar51 at gmail.com Thu Oct 12 07:15:39 2017 From: newatar51 at gmail.com (Atar new) Date: Thu, 12 Oct 2017 16:45:39 +0530 Subject: [Tutor] sibling import Message-ID: 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 From alan.gauld at yahoo.co.uk Thu Oct 12 17:00:53 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 12 Oct 2017 22:00:53 +0100 Subject: [Tutor] sibling import In-Reply-To: References: Message-ID: On 12/10/17 12:15, Atar new wrote: > 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. > Why not put the package in the same folder as your top level script? I think that should work. > But I have to package the whole application and will create a setup.py file > out of it . In that case either install the packages in the standard package area for your system so that they will be within sys.path or, as above, put everything under a single root dir > What is the standard way to do it? > mkdir PROJ cd PROJ > 1. mkdir A > 3. > 4. touch A/__init__.py > 6. > 7. touch A/foo.py > 8. touch bar.py > 9. > 10. cat bar.py > 11. from A import foo > 12. > 13. > 14. python bar.py Should work I think. -- 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 From mats at wichmann.us Thu Oct 12 18:58:11 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 12 Oct 2017 16:58:11 -0600 Subject: [Tutor] sibling import In-Reply-To: References: Message-ID: <0397ee35-2f77-ee74-eafa-9480aa8200bc@wichmann.us> 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 From cmmckay at ualberta.ca Thu Oct 12 16:22:11 2017 From: cmmckay at ualberta.ca (Cameron McKay) Date: Thu, 12 Oct 2017 14:22:11 -0600 Subject: [Tutor] coding help with maxwell-boltzmann distribution Message-ID: 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 From lemade at hotmail.com Thu Oct 12 17:29:02 2017 From: lemade at hotmail.com (Le Mar) Date: Thu, 12 Oct 2017 21:29:02 +0000 Subject: [Tutor] Variable list problem Message-ID: hi i'm wondering if anyone could help me with this exercise: 1. Read an integer number from the console, store the result in a variable n 2. Read ?n? integers from the console and store them in a list 3. Sort the list in reverse order ? from highest to lowest 4. Loop over the elements in the list and print them each on their own line to the console Inputs: 1 number n ? number of integers to read, list of integers Outputs: n integers, one per line, sorted from highest to lowest Thanks a lot. From alan.gauld at yahoo.co.uk Thu Oct 12 20:11:37 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 13 Oct 2017 01:11:37 +0100 Subject: [Tutor] coding help with maxwell-boltzmann distribution In-Reply-To: References: Message-ID: On 12/10/17 21:22, Cameron McKay wrote: > I've never used python trying to plot a graph. Thus I am having > difficulties trying to plot the maxwell-boltzmann distribution. Bear in mind that this list is for people learning the Python language and its standard library(as defined on python.org). As such most of us will have no specialist domain knowledge of your area. > 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) It's not clear how x and y are related - which is what you are seemingly trying to plot? You define x in terms of v, k, T, and m but you offer no clue what these are, where they come from etc. Are they variables? constants? Are you trying a 3D plot of x/y against one of these values? Also I don't think ^(1/2) - a bitwise xor - is the python expression you really want? I'm guessing you maybe mean pow(0.5) or math.sqrt()? > before i used the linspace function linespace is not a builtin, is it part of the plotting library you are using? I'm guessing it's the SciPy package but I'm not sure? > Thanks for looking into this, You need to give us a lot more context. If it is a SciPy problem there is a SciPy mailing list where you may get more specialist readers who already are familiar with your problem domain. But for this list you need to assume we have almost zero knowledge of the domain, only of the language. -- 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 From alan.gauld at yahoo.co.uk Thu Oct 12 20:16:25 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 13 Oct 2017 01:16:25 +0100 Subject: [Tutor] Variable list problem In-Reply-To: References: Message-ID: On 12/10/17 22:29, Le Mar wrote: > hi i'm wondering if anyone could help me with this exercise: > We can give you tips but we don;t do your homework for you. > 1. Read an integer number from the console, store the result in a variable n > 2. Read ?n? integers from the console and store them in a list > 3. Sort the list in reverse order ? from highest to lowest > 4. Loop over the elements in the list and print them each on their own line to the console Since you give the exact steps needed we have to assume that there is some part of this that you don't know how to do. Which step is proving difficult? 1) use the input() function and convert the string to an integer. 2) use a loop (and the same code as in 1) to collect the numbers in a list 3) lists have methods for sorting and reversing their contents 4) use another loop and the print() function. -- 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 From wrw at mac.com Thu Oct 12 23:48:10 2017 From: wrw at mac.com (William Ray Wing) Date: Thu, 12 Oct 2017 23:48:10 -0400 Subject: [Tutor] coding help with maxwell-boltzmann distribution In-Reply-To: References: Message-ID: <21C580F2-6FA5-458F-999F-CF4B2A05DE53@mac.com> > On Oct 12, 2017, at 4:22 PM, 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) First of all, this only part of the formula for the Maxwell Boltzmann distribution function. It is usually written as: F(v) = (m/2 pi kT)^3/2 * 4 pi v^2 exp(-mv^2/2kT) The independent variable is v, everything else is constants. So what you want is an array of values of v (which will be the x axis). Then use a for loop to calculate F(v) for each v in the range. Finally, importing matplotlib, and plotting F(v), the y axis, as a function of the x axis will give you your (sort of) bell curve. Hope these hints help. Bill > > 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 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From breamoreboy at yahoo.co.uk Fri Oct 13 01:58:03 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 13 Oct 2017 06:58:03 +0100 Subject: [Tutor] coding help with maxwell-boltzmann distribution In-Reply-To: References: Message-ID: 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 From mysecretrobotfactory at gmail.com Thu Oct 12 21:54:10 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 12 Oct 2017 18:54:10 -0700 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Thu Oct 12 21:58:47 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 12 Oct 2017 18:58:47 -0700 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: 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 C wrote: > 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 at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From alan.gauld at yahoo.co.uk Fri Oct 13 04:37:46 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 13 Oct 2017 09:37:46 +0100 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: 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 From dvnsarma at gmail.com Fri Oct 13 11:35:06 2017 From: dvnsarma at gmail.com (=?UTF-8?B?RC5WLk4uU2FybWEg4LCh4LC/LuCwteCwvy7gsI7gsKjgsY0u4LC24LCw4LGN4LCu?=) Date: Fri, 13 Oct 2017 21:05:06 +0530 Subject: [Tutor] coding help with maxwell-boltzmann distribution In-Reply-To: References: Message-ID: 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 Tutor wrote: > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Fri Oct 13 12:13:38 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 13 Oct 2017 18:13:38 +0200 Subject: [Tutor] coding help with maxwell-boltzmann distribution References: Message-ID: 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) From dvnsarma at gmail.com Fri Oct 13 12:50:32 2017 From: dvnsarma at gmail.com (=?UTF-8?B?RC5WLk4uU2FybWEg4LCh4LC/LuCwteCwvy7gsI7gsKjgsY0u4LC24LCw4LGN4LCu?=) Date: Fri, 13 Oct 2017 22:20:32 +0530 Subject: [Tutor] coding help with maxwell-boltzmann distribution In-Reply-To: References: Message-ID: Yes we can vectorize. regards, Sarma. On Fri, Oct 13, 2017 at 9:43 PM, Peter Otten <__peter__ at 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From akleider at sonic.net Fri Oct 13 12:55:55 2017 From: akleider at sonic.net (Alex Kleider) Date: Fri, 13 Oct 2017 09:55:55 -0700 Subject: [Tutor] sibling import In-Reply-To: <0397ee35-2f77-ee74-eafa-9480aa8200bc@wichmann.us> References: <0397ee35-2f77-ee74-eafa-9480aa8200bc@wichmann.us> Message-ID: <681642c03102f6e899c3b01268d7e334@sonic.net> 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)" From anrkris at gmail.com Fri Oct 13 08:04:56 2017 From: anrkris at gmail.com (Chris Coleman) Date: Fri, 13 Oct 2017 08:04:56 -0400 Subject: [Tutor] problem with program Message-ID: 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? From alan.gauld at yahoo.co.uk Fri Oct 13 13:53:50 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 13 Oct 2017 18:53:50 +0100 Subject: [Tutor] problem with program In-Reply-To: References: Message-ID: 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 From robertvstepp at gmail.com Fri Oct 13 18:33:22 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 13 Oct 2017 17:33:22 -0500 Subject: [Tutor] OT: New book on pytest Message-ID: 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 From breamoreboy at yahoo.co.uk Fri Oct 13 13:40:08 2017 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 13 Oct 2017 18:40:08 +0100 Subject: [Tutor] problem with program In-Reply-To: References: Message-ID: 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 From mysecretrobotfactory at gmail.com Fri Oct 13 19:10:04 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 13 Oct 2017 16:10:04 -0700 Subject: [Tutor] using while loop for read process memory In-Reply-To: References: <0b2f0831-b0d6-8fa6-3d90-f89aed708338@wichmann.us> Message-ID: 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 C wrote: > 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 at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From alan.gauld at yahoo.co.uk Fri Oct 13 19:27:35 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 14 Oct 2017 00:27:35 +0100 Subject: [Tutor] problem with program In-Reply-To: References: Message-ID: 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 From robertvstepp at gmail.com Sat Oct 14 01:43:13 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 14 Oct 2017 00:43:13 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? Message-ID: 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 From robertvstepp at gmail.com Sat Oct 14 02:43:54 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 14 Oct 2017 01:43:54 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sat, Oct 14, 2017 at 1:27 AM, srinivas devaki wrote: > SQL has "CREATE TABLE IF NOT EXISTS" > > so you change your instructions to > ```python > import sqlite3 > conn = sqlite3.connect(':memory:') > c = conn.cursor() > c.execute(""" > CREATE TABLE IF NOT EXISTS BloodPressureReadings ( > ReadingID INTEGER PRIMARY KEY, > Date TEXT, > Time TEXT, > SystolicBP INTEGER, > DiastolicBP INTEGER, > Comments TEXT); > """) > c.execute("SELECT * FROM BloodPressureReadings") But I do *not* want to create this table from within my python code; instead, I want to run the table creation commands from a file if the table does not already exist. This way I have more flexibility if I later change to different database products, for testing purposes, etc. I am currently trying to make the following work: py3: tb_exists = c.execute('select name from sqlite_master where type="table"') so that I can embed this in an if statement. But it is not behaving quite like I hope yet. So I am still experimenting and Googling ... boB From robertvstepp at gmail.com Sat Oct 14 03:11:44 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 14 Oct 2017 02:11:44 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sat, Oct 14, 2017 at 1:43 AM, boB Stepp wrote: > I am currently trying to make the following work: > > py3: tb_exists = c.execute('select name from sqlite_master where type="table"') > > so that I can embed this in an if statement. But it is not behaving > quite like I hope yet. So I am still experimenting and Googling ... I'm tired and not thinking clearly anymore, but I think I have something to give me the basis of what I want to do. First, what happens if the table does *not* exist: py3: import sqlite3 py3: conn = sqlite3.connect(':memory:') py3: c = conn.cursor() py3: tb_exists = "select name from sqlite_master where type='table' and name='test'" py3: tb_ck = c.execute(tb_exists).fetchone() py3: tb_ck py3: print(tb_ck) None So I get "None" as a result if the target table has not been created yet. But if I *do* create the table I want: py3: with open('create_sqlite3_db.sql') as f: ... sql = f.read() ... py3: c.executescript(sql) py3: tb_exists = "select name from sqlite_master where type='table' and name='BloodPressureReadings'" py3: tb_ck = c.execute(tb_exists).fetchone() py3: print(tb_ck) ('BloodPressureReadings',) So it is looking like I can use this technique to determine if I need to create the BloodPressureReadings table or not. Am I on track here or is there a better technique? -- boB From robertvstepp at gmail.com Sat Oct 14 03:17:03 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 14 Oct 2017 02:17:03 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sat, Oct 14, 2017 at 2:11 AM, boB Stepp wrote: > So I get "None" as a result if the target table has not been created > yet. But if I *do* create the table I want: > > py3: with open('create_sqlite3_db.sql') as f: > ... sql = f.read() > ... > py3: c.executescript(sql) > > py3: tb_exists = "select name from sqlite_master where type='table' > and name='BloodPressureReadings'" > py3: tb_ck = c.execute(tb_exists).fetchone() > py3: print(tb_ck) > ('BloodPressureReadings',) > > So it is looking like I can use this technique to determine if I need > to create the BloodPressureReadings table or not. Am I on track here > or is there a better technique? It just occurred to me after sending the above, does something like "sqlite_master" exist for other database programs than sqlite3? Can I abstract out "sqlite_master" and replace it with a variable so that I can handle any kind of database? -- boB From mr.eightnoteight at gmail.com Sat Oct 14 02:27:05 2017 From: mr.eightnoteight at gmail.com (srinivas devaki) Date: Sat, 14 Oct 2017 11:57:05 +0530 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: SQL has "CREATE TABLE IF NOT EXISTS" so you change your instructions to ```python import sqlite3 conn = sqlite3.connect(':memory:') c = conn.cursor() c.execute(""" CREATE TABLE IF NOT EXISTS BloodPressureReadings ( ReadingID INTEGER PRIMARY KEY, Date TEXT, Time TEXT, SystolicBP INTEGER, DiastolicBP INTEGER, Comments TEXT); """) c.execute("SELECT * FROM BloodPressureReadings") ``` On Sat, Oct 14, 2017 at 11:13 AM boB Stepp wrote: > > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From mr.eightnoteight at gmail.com Sat Oct 14 03:20:40 2017 From: mr.eightnoteight at gmail.com (srinivas devaki) Date: Sat, 14 Oct 2017 12:50:40 +0530 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sat, Oct 14, 2017 at 12:41 PM, boB Stepp wrote: > py3: c.executescript(sql) > > py3: tb_exists = "select name from sqlite_master where type='table' > and name='BloodPressureReadings'" > py3: tb_ck = c.execute(tb_exists).fetchone() > py3: print(tb_ck) > ('BloodPressureReadings',) > > So it is looking like I can use this technique to determine if I need > to create the BloodPressureReadings table or not. Am I on track here > or is there a better technique? > awesome trick. but most of the time applications had to exported out of sqlite to either mysql or postgresql at which time it would be a lot of refactoring as this trick uses sqlite internals to check if a table exists. fetchone would always return a tuple as we did count in the sql instruction. so it would be easier to embed in an if statement. Regards Srinivas Devaki Software Developer at Zomato, New Delhi Phone: +91 9491 383 249 Telegram: @eightnoteight From alan.gauld at yahoo.co.uk Sat Oct 14 04:07:21 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 14 Oct 2017 09:07:21 +0100 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On 14/10/17 06:43, boB Stepp wrote: > 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. c.executescript(sqlFile.read()) -- 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 From Steve.Flynn at capita.co.uk Sat Oct 14 03:28:07 2017 From: Steve.Flynn at capita.co.uk (Flynn, Stephen (L & P - IT)) Date: Sat, 14 Oct 2017 07:28:07 +0000 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: , Message-ID: Yep - oracle for example has hundreds of tables which hold metadata. for example (this will look terrible due to the length of it but if you assume it's 3 lines; a title line, some underlining for each column and the data itself): SQL> select * from all_tables where owner = 'FLYNNS' and table_name = 'QC11483'; OWNER TABLE_NAME TABLESPACE_NAME CLUSTER_NAME IOT_NAME STATUS PCT_FREE PCT_USED INI_TRANS MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE FREELISTS FREELIST_GROUPS LOGGING BACKED_UP NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE CHAIN_CNT AVG_ROW_LEN AVG_SPACE_FREELIST_BLOCKS NUM_FREELIST_BLOCKS DEGREE INSTANCES CACHE TABLE_LOCK SAMPLE_SIZE LAST_ANALYZED PARTITIONED IOT_TYPE TEMPORARY SECONDARY NESTED BUFFER_POOL ROW_MOVEMENT GLOBAL_STATS USER_STATS DURATION SKIP_CORRUPT MONITORING CLUSTER_OWNER DEPENDENCIES COMPRESSION DROPPED ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ -------- ---------- ---------- ---------- ---------- -------------- ----------- ----------- ----------- ------------ ---------- --------------- ------- --------- ---------- ---------- ------------ ---------- ---------- ----------- ------------------------- ------------------- ---------- ---------- ----- ---------- ----------- ------------- ----------- ------------ --------- --------- ------ ----------- ------------ ------------ ---------- --------------- ------------ ---------- ------------------------------ ------------ ----------- ------- FLYNNS QC11483 USERS VALID 10 1 255 65536 1 2147483645 NO N 61517 1000 0 0 0 108 0 0 1 1 N ENABLED 61517 30/08/2017 16 NO N N NO DEFAULT DISABLED YES NO DISABLED YES DISABLED DISABLED NO Executed in 0.141 seconds There's something similar for SQL Server, DB2, ADDABAS, PostGres,, and so forth S. ________________________________ From: Tutor on behalf of boB Stepp Sent: 14 October 2017 08:17 To: tutor Subject: Re: [Tutor] How to test for the existence of a table in a sqlite3 db? On Sat, Oct 14, 2017 at 2:11 AM, boB Stepp wrote: > So I get "None" as a result if the target table has not been created > yet. But if I *do* create the table I want: > > py3: with open('create_sqlite3_db.sql') as f: > ... sql = f.read() > ... > py3: c.executescript(sql) > > py3: tb_exists = "select name from sqlite_master where type='table' > and name='BloodPressureReadings'" > py3: tb_ck = c.execute(tb_exists).fetchone() > py3: print(tb_ck) > ('BloodPressureReadings',) > > So it is looking like I can use this technique to determine if I need > to create the BloodPressureReadings table or not. Am I on track here > or is there a better technique? It just occurred to me after sending the above, does something like "sqlite_master" exist for other database programs than sqlite3? Can I abstract out "sqlite_master" and replace it with a variable so that I can handle any kind of database? -- boB _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor This email is security checked and subject to the disclaimer on web-page: http://www.capita.co.uk/email-disclaimer.aspx From __peter__ at web.de Sat Oct 14 05:45:08 2017 From: __peter__ at web.de (Peter Otten) Date: Sat, 14 Oct 2017 11:45:08 +0200 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: boB Stepp wrote: > On Sat, Oct 14, 2017 at 2:11 AM, boB Stepp wrote: > >> So I get "None" as a result if the target table has not been created >> yet. But if I *do* create the table I want: >> >> py3: with open('create_sqlite3_db.sql') as f: >> ... sql = f.read() >> ... >> py3: c.executescript(sql) >> >> py3: tb_exists = "select name from sqlite_master where type='table' >> and name='BloodPressureReadings'" >> py3: tb_ck = c.execute(tb_exists).fetchone() >> py3: print(tb_ck) >> ('BloodPressureReadings',) >> >> So it is looking like I can use this technique to determine if I need >> to create the BloodPressureReadings table or not. Am I on track here >> or is there a better technique? > > It just occurred to me after sending the above, does something like > "sqlite_master" exist for other database programs than sqlite3? Can I > abstract out "sqlite_master" and replace it with a variable so that I > can handle any kind of database? If this is a long term project there will be changes in the schema. However, I don't think it is necessary to check for individual tables. You typically start with a few tables create table alpha create table beta create table gamma and later add a few more or change columns alter table alpha create table delta In this example you have three versions of the database version 0: empty version 1: three tables version 2: four tables, one table modified If you add a table for your own metadata you ownly need to store that version number. The necessary steps when you open the database are then - read version from bobs_metadata (if that fails you are at version 0) If you are at version 0 "migrate" to version one: - execute script that creates alpha, beta, gamma, and bobs_metadata, the latter with one row containing version=1 If you are at version 1 migrate to version two: - execute migration script from 1 to 2 modifying alpha, creating delta, and updating to version=2 If you are at version 2: - do nothing, database is already in the state required by your application. Pseudo-code: scripts = ["0to1.sql", "1to2.sql"] current_version = get_schema_version() # this has to catch the # OperationalError for scriptname in scripts[current_version:]: with open(scriptname) as f: script = f.read() executescript(script) From chris2014 at postbox.xyz Sat Oct 14 11:44:33 2017 From: chris2014 at postbox.xyz (Chris) Date: Sat, 14 Oct 2017 17:44:33 +0200 Subject: [Tutor] Tree again: iterator, yield, increase (treelib) Message-ID: <20171014174433.05f45c3a@cd> All, I've a question about treelib library from pip. Treelib stores a tree and offers functions to add, delete or move nodes. Furthermore, you can print a tree like this: Harry ??? Harry2 ??? Harry3 ??? Harry4 ??? Jane ??? Jane2 ? ??? Jane2.1 ? ? ??? Jane2.1.1 ? ??? Jane2.2 ? ??? Jane2.3 ??? Jane3 I'm trying to understand how the print function is working. 1. The documentation [2] says, you have to call tree.show() to print the tree above. 2. tree.show calls the self__print_backend(...) 3. It seems that nid is initialized in get_iter, Line 218 [1] 4. nid is passed as parameter to __get_iter and the other participating funtions 5. the node with the id nid is fetched in line 222. 6. In Line 190 there's a loop. I don't understand what increments nid or what makes the __get_iter function loop through the self._nodes dictionary defined in Line 106? Couldn't the __get_iter function iterate another list or dictionary? Which line says that you want to get every Node in self._nodes? Thank you in advance! - Chris [1] https://github.com/caesar0301/treelib/blob/master/treelib/tree.py [2] http://treelib.readthedocs.io/en/latest/examples.html#basic-usage From alan.gauld at yahoo.co.uk Sat Oct 14 19:30:42 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 15 Oct 2017 00:30:42 +0100 Subject: [Tutor] Tree again: iterator, yield, increase (treelib) In-Reply-To: <20171014174433.05f45c3a@cd> References: <20171014174433.05f45c3a@cd> Message-ID: On 14/10/17 16:44, Chris wrote: > I've a question about treelib library from pip. The list focus is the language and standard library so if you want to ask about other code you need to give us more context. treelib is not a commonly discussed module, in fact this the first mention I've seen. > Treelib stores a tree and offers functions to add, delete or move > nodes. Furthermore, you can print a tree like this: > I'm trying to understand how the print function is working. You need to show us some code. specifically the functions that you mention below. Don't assume we know them or that we will be downloading a random module just to read it. > 1. The documentation [2] says, you have to call tree.show() to print > the tree above. > 2. tree.show calls the self__print_backend(...) > 3. It seems that nid is initialized in get_iter, Line 218 [1] > 4. nid is passed as parameter to __get_iter and the other > participating funtions > 5. the node with the id nid is fetched in line 222. > 6. In Line 190 there's a loop. > > I don't understand what increments nid or what makes the __get_iter > function loop through the self._nodes dictionary defined in Line 106? Neither do I without seeing the code. Although the classic approach to traversing trees is to use recursion so, do any of the functions call themselves? -- 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 From cs at cskk.id.au Sat Oct 14 20:40:36 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 15 Oct 2017 11:40:36 +1100 Subject: [Tutor] Tree again: iterator, yield, increase (treelib) In-Reply-To: <20171014174433.05f45c3a@cd> References: <20171014174433.05f45c3a@cd> Message-ID: <20171015004036.GA43322@cskk.homeip.net> On 14Oct2017 17:44, Chris wrote: >3. It seems that nid is initialized in get_iter, Line 218 [1] >4. nid is passed as parameter to __get_iter and the other >participating funtions >5. the node with the id nid is fetched in line 222. >6. In Line 190 there's a loop. > >I don't understand what increments nid or what makes the __get_iter >function loop through the self._nodes dictionary defined in Line 106? >Couldn't the __get_iter function iterate another list or dictionary? >Which line says that you want to get every Node in self._nodes? [...] >[1] https://github.com/caesar0301/treelib/blob/master/treelib/tree.py "nid" is not a counter, it is a node id, a key into the dictionary. The loop starting on line 240 advances through all the immediate children of the current node, and calls __get_iter using each child's identifier as the "nid" parameter. In this way the whole tree is traversed. Cheers, Cameron Simpson (formerly cs at zip.com.au) From robertvstepp at gmail.com Sun Oct 15 01:07:19 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 15 Oct 2017 00:07:19 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sat, Oct 14, 2017 at 4:45 AM, Peter Otten <__peter__ at web.de> wrote: > If this is a long term project there will be changes in the schema. > However, I don't think it is necessary to check for individual tables. You > typically start with a few tables > > create table alpha > create table beta > create table gamma > > and later add a few more or change columns > > alter table alpha > create table delta > > In this example you have three versions of the database > > version 0: empty > version 1: three tables > version 2: four tables, one table modified > > If you add a table for your own metadata you ownly need to store that > version number. The necessary steps when you open the database are then > > - read version from bobs_metadata (if that fails you are at version 0) > > If you are at version 0 "migrate" to version one: > > - execute script that creates alpha, beta, gamma, and bobs_metadata, the > latter with one row containing version=1 > > If you are at version 1 migrate to version two: > > - execute migration script from 1 to 2 modifying alpha, creating delta, and > updating to version=2 > > If you are at version 2: > > - do nothing, database is already in the state required by your application. I am puzzled. If one is using version control, then what is the advantage of this incremental approach in creating and populating the database? Instead, why not just have a single SQL file that generates the finished initial state of the db for the current version? One can always use the version control system to roll back to an earlier stage. > Pseudo-code: > > scripts = ["0to1.sql", "1to2.sql"] > current_version = get_schema_version() # this has to catch the > # OperationalError > for scriptname in scripts[current_version:]: > with open(scriptname) as f: > script = f.read() > executescript(script) My current project structure for this program: blood_pressures/ |----.git/ |----blood_pressures/ |--------|--__init__.py |--------|--main.py |----database/ |--------|--__init__.py # Is this really needed in this folder? |--------|--blood_pressure.db |--------|--create_sqlite3_db.sql |----tests/ |--------|--__init__.py # Oh, no! No tests yet!! .gitignore The current SQL file, create_sqlite3_db.sql: ================================================================================ -- This file will create version 1 of the database. -------------------------------------------------------------------------------- CREATE TABLE BloodPressureReadings ( ReadingID INTEGER PRIMARY KEY, Date TEXT, Time TEXT, SystolicBP INTEGER, DiastolicBP INTEGER, Comments TEXT); -- This table is intended to only have one row with one entry: the current -- database version. Whenever the program is started this entry will be checked -- to determine if the database needs to be updated (or not) to the current -- version from the earlier SQL database creation files. CREATE TABLE CurrentDBVersion ( VersionNumber INTEGER); -- Insert starting version number of database: INSERT INTO CurrentDBVersion (VersionNumber) VALUES (1); ================================================================================ And my current effort to implement your guidance: ================================================================================ #!/usr/bin/env python3 """This file starts the blood pressure readings program.""" import sqlite3 def ensure_db(filename): """Open the database, "filename", if it exists; otherwise, create a database named "filename".""" db = sqlite3.connect(filename) cur = db.cursor() try: sql_cmd = "SELECT VersionNumber FROM CurrentDBVersion" # First element of returned tuple will be the db version number: current_db_version = int(cur.execute(sql_cmd).fetchone()[0]) except sqlite3.OperationalError: # This means that the database and the table, "CurrentDBVersion", has # not yet been created, implying "version 0". current_db_version = 0 finally: sql_scripts = ["../database/create_sqlite3_db.sql"] for sql_scriptname in sql_scripts[current_db_version:]: with open(sql_scriptname) as f: cur.executescript(f.read()) return db if __name__ == "__main__": db_filename = "../database/blood_pressure.db" ensure_db(db_filename) # Not doing anything with returned db yet. ================================================================================ I have chickened out and not done a TDD approach yet. I will probably pause here, wait for feedback from this list, and try to figure out how I should test what I have so far. And how do you test SQL scripts anyway? Some things I am still pondering: 1) If I adopt the incremental approach to creating and initializing the working db, then it seems that the list, "sql_scripts", should not be hard-coded into the program. It seems to me it should be off somewhere by itself with perhaps other things that might evolve/change over time in its own file where it (and its brethren) are easy to locate and update. 2) Likewise, "db_filename", is currently hard-coded in the if block to start the program. I have not decided yet what the end result will be, but I might want to allow for the possibility of allowing the user (me) to create multiple databases. Also, when I figure out how to test this database stuff, I imagine I will be using a test db for the testing, not the actual one. Again, this argues for not hard-coding the database name. 3) I am supposed to be delving into writing classes on this project. Should the code so far stay as a function or get incorporated into a class? My original intent was to do a class for the BloodPressureReadings table, but I am not at the point of going there yet. 4) I wish there was a PEP 8 for SQL! I have several SQL books I have consulted, but I have gotten conflicting suggestions for SQL code style. I have tried to adopt something that seems to me to be both consistent and reasonable, but is it good enough? I await the arrival of the list's wisdom! Cheers! -- boB From robertvstepp at gmail.com Sun Oct 15 01:48:27 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 15 Oct 2017 00:48:27 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sun, Oct 15, 2017 at 12:07 AM, boB Stepp wrote: > ================================================================================ > #!/usr/bin/env python3 > > """This file starts the blood pressure readings program.""" > > import sqlite3 > > def ensure_db(filename): > """Open the database, "filename", if it exists; otherwise, create a > database named "filename".""" > > db = sqlite3.connect(filename) > cur = db.cursor() > > try: > sql_cmd = "SELECT VersionNumber FROM CurrentDBVersion" > > # First element of returned tuple will be the db version number: > current_db_version = int(cur.execute(sql_cmd).fetchone()[0]) > > except sqlite3.OperationalError: > # This means that the database and the table, "CurrentDBVersion", has > # not yet been created, implying "version 0". > current_db_version = 0 > > finally: > sql_scripts = ["../database/create_sqlite3_db.sql"] > for sql_scriptname in sql_scripts[current_db_version:]: > with open(sql_scriptname) as f: > cur.executescript(f.read()) > > return db I have not used a "finally" block before. I just had the thought that maybe it would run even if an uncaught exception might occur. I tried to test this thought by generating a deliberate NameError in the "try" block and added a print to the "finally" clause. I got the intended NameError with no evidence of the added print printing. But I thought I would ask just to be sure: If an uncaught exception occurs, will the "finally" clause execute? > > I have chickened out and not done a TDD approach yet. I will probably > pause here, wait for feedback from this list, and try to figure out > how I should test what I have so far. And how do you test SQL scripts > anyway? > > Some things I am still pondering: I forgot to add (5): 5) How should I handle the cursor object? When I should I close it? My function returns "db" which I presume keeps the connection to the database. But what happens to the cursor object as I have written the function, since I never closed it? -- boB From __peter__ at web.de Sun Oct 15 03:56:22 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 15 Oct 2017 09:56:22 +0200 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: boB Stepp wrote: > On Sat, Oct 14, 2017 at 4:45 AM, Peter Otten <__peter__ at web.de> wrote: > >> If this is a long term project there will be changes in the schema. >> However, I don't think it is necessary to check for individual tables. >> You typically start with a few tables >> >> create table alpha >> create table beta >> create table gamma >> >> and later add a few more or change columns >> >> alter table alpha >> create table delta >> >> In this example you have three versions of the database >> >> version 0: empty >> version 1: three tables >> version 2: four tables, one table modified >> >> If you add a table for your own metadata you ownly need to store that >> version number. The necessary steps when you open the database are then >> >> - read version from bobs_metadata (if that fails you are at version 0) >> >> If you are at version 0 "migrate" to version one: >> >> - execute script that creates alpha, beta, gamma, and bobs_metadata, the >> latter with one row containing version=1 >> >> If you are at version 1 migrate to version two: >> >> - execute migration script from 1 to 2 modifying alpha, creating delta, >> and >> updating to version=2 >> >> If you are at version 2: >> >> - do nothing, database is already in the state required by your >> application. > > I am puzzled. If one is using version control, then what is the > advantage of this incremental approach in creating and populating the > database? Instead, why not just have a single SQL file that generates > the finished initial state of the db for the current version? One can > always use the version control system to roll back to an earlier > stage. My idea was presented under the assumption that the there is user data entered in version 1 that needs to be preserved when version 2 of the application replaces 1. > I have chickened out and not done a TDD approach yet. I will probably > pause here, wait for feedback from this list, and try to figure out > how I should test what I have so far. And how do you test SQL scripts > anyway? Example: Run the routine to enter a row, then check if it's there and contains what you expected. That should fail before the script is run, and succeed afterwards. > > Some things I am still pondering: > > 1) If I adopt the incremental approach to creating and initializing > the working db, then it seems that the list, "sql_scripts", should not > be hard-coded into the program. It seems to me it should be off > somewhere by itself with perhaps other things that might evolve/change > over time in its own file where it (and its brethren) are easy to > locate and update. I think it should be hardcoded. You don't want to run arbitrary scripts that happen to be in a folder, say. Version control can take care of any changes. > > 2) Likewise, "db_filename", is currently hard-coded in the if block > to start the program. I have not decided yet what the end result will > be, but I might want to allow for the possibility of allowing the user > (me) to create multiple databases. If one user needs multiple databases that /may/ be an indication that you are not storing enough information in the database. Bad: one database per patient. You are using the file system as a meta- database. Better: an additional patients table and a column patientid in all tables containing patient data. > Also, when I figure out how to > test this database stuff, I imagine I will be using a test db for the > testing, not the actual one. Again, this argues for not hard-coding > the database name. > > 3) I am supposed to be delving into writing classes on this project. > Should the code so far stay as a function or get incorporated into a > class? My original intent was to do a class for the > BloodPressureReadings table, but I am not at the point of going there > yet. Relax. A function is an instance of a class with no state and a __call__ method ;) > 4) I wish there was a PEP 8 for SQL! I have several SQL books I have > consulted, but I have gotten conflicting suggestions for SQL code > style. I have tried to adopt something that seems to me to be both > consistent and reasonable, but is it good enough? > > I await the arrival of the list's wisdom! > > Cheers! From __peter__ at web.de Sun Oct 15 04:09:08 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 15 Oct 2017 10:09:08 +0200 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: boB Stepp wrote: > I have not used a "finally" block before. I just had the thought that > maybe it would run even if an uncaught exception might occur. I tried > to test this thought by generating a deliberate NameError in the "try" > block and added a print to the "finally" clause. I got the intended > NameError with no evidence of the added print printing. But I thought > I would ask just to be sure: If an uncaught exception occurs, will > the "finally" clause execute? Yes. >>> try: ... 1/0 ... except ValueError: ... print("not triggered") ... finally: ... print("ALWAYS TRIGGERED") ... ALWAYS TRIGGERED Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero If something seems too complex in your real code try to come up with a similar setup in the interactive interpreter (or use a minimal demo script if that proves impractical). From s.shall at virginmedia.com Sun Oct 15 07:52:59 2017 From: s.shall at virginmedia.com (Sydney Shall) Date: Sun, 15 Oct 2017 12:52:59 +0100 Subject: [Tutor] New Item In-Reply-To: References: <1562227876.4249932.1506564830121.JavaMail.zimbra@comcast.net> <1509230025.4273771.1506567070915.JavaMail.zimbra@comcast.net> Message-ID: <74a50c71-502b-0913-b146-60c70d7c0597@virginmedia.com> On 28/09/2017 11:46, Peter Otten wrote: > LARRYSTALEY07 at comcast.net wrote: > >> I am very new to Python and appreciate the input as I was able to fully >> install Python with all needed libraries (i.e., numpy, pandas, etc.). >> However, I now have an application question in needed to construct a 2D >> Histogram. >> >> Basically, I have an Excel file that includes three columns: >> Column 1 - Gender (Male or Female) >> Column 2 - Height (in inches) >> Column 3 - Hand Span (in inches) > > I have yet to grok your code samples, but my feeling is that your approach > is too low-level. Do you mean something like > > http://matplotlib.org/examples/pylab_examples/hist2d_demo.html > > by "2d histograms"? That would require very little code written by yourself: > > import pandas as pd > from matplotlib import pyplot > > filename = "size.xls" > sheetname = "first" > > data = pd.read_excel(filename, sheetname) > > for index, sex in enumerate(["female", "male"], 1): > pyplot.figure(index) > subset = data[data["Gender"] == sex] > pyplot.hist2d(subset["Height"].values, subset["Hand Span"].values) > > pyplot.show() > > >> >> >> data=readExcel(excelfile) >> X=np.array(data[:,1],dtype=float); >> S=np.array(data[:,2],dtype=float); >> T=np.array(data[:,0],dtype=str); >> >> >> >> >> Finally, is my intended coding for the actual 2D histogram. I will get min >> and max from the height and hand span arrays. Note I am still learning >> Python and looping is new to me: >> >> >> >> >> # Define histogram classifier to build histogram using two variables >> def Build1DHistogramClassifier(X, S, smin, smax,T,B,xmin,xmax): >> HF=np.zeros(B).astype('int32'); >> HM=np.zeros(B).astype('int32'); >> binindices1=(np.round(((B-1)*(X-xmin)/(xmax-xmin)))).astype('int32'); >> binindices2=(np.round(((B-1)*(S-smin)/(smax-smin)))).astype('int32'); >> for i,b in enumerate(binindices1): >> for i,c in enumerate(bindindices2): >> if T[i]=='Female': >> HF[b,c]+=1; >> else: >> HM[b,c]+=1; >> return [HF, HM] Hi, I have a similar problem, but my data is not in excel but is in OpenOffice "Spreadsheet', but not in "Database". My question is can I use a similar simple procedure as that given by Peter Otten. Ta muchly. -- Sydney From __peter__ at web.de Sun Oct 15 09:37:50 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 15 Oct 2017 15:37:50 +0200 Subject: [Tutor] New Item References: <1562227876.4249932.1506564830121.JavaMail.zimbra@comcast.net> <1509230025.4273771.1506567070915.JavaMail.zimbra@comcast.net> <74a50c71-502b-0913-b146-60c70d7c0597@virginmedia.com> Message-ID: Sydney Shall wrote: > On 28/09/2017 11:46, Peter Otten wrote: >> LARRYSTALEY07 at comcast.net wrote: >> >>> I am very new to Python and appreciate the input as I was able to fully >>> install Python with all needed libraries (i.e., numpy, pandas, etc.). >>> However, I now have an application question in needed to construct a 2D >>> Histogram. >>> >>> Basically, I have an Excel file that includes three columns: >>> Column 1 - Gender (Male or Female) >>> Column 2 - Height (in inches) >>> Column 3 - Hand Span (in inches) >> >> I have yet to grok your code samples, but my feeling is that your >> approach is too low-level. Do you mean something like >> >> http://matplotlib.org/examples/pylab_examples/hist2d_demo.html >> >> by "2d histograms"? That would require very little code written by >> yourself: >> >> import pandas as pd >> from matplotlib import pyplot >> >> filename = "size.xls" >> sheetname = "first" >> >> data = pd.read_excel(filename, sheetname) >> >> for index, sex in enumerate(["female", "male"], 1): >> pyplot.figure(index) >> subset = data[data["Gender"] == sex] >> pyplot.hist2d(subset["Height"].values, subset["Hand Span"].values) >> >> pyplot.show() > I have a similar problem, but my data is not in excel but is in > OpenOffice "Spreadsheet', but not in "Database". > > My question is can I use a similar simple procedure as that given by > Peter Otten. There doesn't seem to be direct support for the ods file format in pandas. Your easiest option is to open the file in OpenOffice and save as xls or csv. If you don't want to go that route you can install a library that can read ods files. With https://pypi.python.org/pypi/pyexcel-ods/0.3.1 the above example should work after the following modifications: import pandas as pd from matplotlib import pyplot import pyexcel_ods def read_ods(filename, sheetname): table = pyexcel_ods.read_data(filename)[sheetname] return pd.DataFrame(table[1:], columns=table[0]) filename = "size.ods" sheetname = "first" data = read_ods(filename, sheetname) for index, sex in enumerate(["female", "male"], 1): pyplot.figure(index) subset = data[data["Gender"] == sex] pyplot.hist2d(subset["Height"].values, subset["Hand Span"].values) pyplot.show() From chris2014 at postbox.xyz Sun Oct 15 07:23:35 2017 From: chris2014 at postbox.xyz (Chris) Date: Sun, 15 Oct 2017 13:23:35 +0200 Subject: [Tutor] Tree again: iterator, yield, increase (treelib) In-Reply-To: <20171015004036.GA43322@cskk.homeip.net> References: <20171014174433.05f45c3a@cd> <20171015004036.GA43322@cskk.homeip.net> Message-ID: <20171015132335.1e929490@cd> On Sun, 15 Oct 2017 11:40:36 +1100 Cameron Simpson wrote: > The loop starting on line 240 advances through all the immediate > children of the current node, and calls __get_iter using each child's > identifier as the "nid" parameter. In this way the whole tree is > traversed. Thank you for your quick reply. I inadvertently thought, this part is only relevant when a filter is set by the user. I'll have a look again at it. - Chris From robertvstepp at gmail.com Sun Oct 15 11:09:09 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 15 Oct 2017 10:09:09 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sun, Oct 15, 2017 at 3:09 AM, Peter Otten <__peter__ at web.de> wrote: > boB Stepp wrote: > >> I have not used a "finally" block before. I just had the thought that >> maybe it would run even if an uncaught exception might occur. I tried >> to test this thought by generating a deliberate NameError in the "try" >> block and added a print to the "finally" clause. I got the intended >> NameError with no evidence of the added print printing. But I thought >> I would ask just to be sure: If an uncaught exception occurs, will >> the "finally" clause execute? > > > Yes. > >>>> try: > ... 1/0 > ... except ValueError: > ... print("not triggered") > ... finally: > ... print("ALWAYS TRIGGERED") > ... > ALWAYS TRIGGERED > Traceback (most recent call last): > File "", line 2, in > ZeroDivisionError: division by zero That is what I thought after reading about "finally". But look what happens if I modify my actual code to generate a NameError: ============================================================================================ def ensure_db(filename): """Open the database, "filename", if it exists; otherwise, create a database named "filename".""" db = sqlite3.connect(filename) cur = db.cursor() try: sql_cmd = "SELECT VersionNumber FROM CurrentDBVersion" a # This should generate a NameError # First element of returned tuple will be the db version number: current_db_version = int(cur.execute(sql_cmd).fetchone()[0]) except sqlite3.OperationalError: # This means that the database and the table, "CurrentDBVersion", has # not yet been created, implying "version 0". current_db_version = 0 finally: sql_scripts = ["../database/create_sqlite3_db.sql"] for sql_scriptname in sql_scripts[current_db_version:]: with open(sql_scriptname) as f: cur.executescript(f.read()) print("THIS IS THE FINALLY BLOCK!!!") # And this *should* print return db ============================================================================================ This results in the following Traceback: ============================================================================================ > py main.py Traceback (most recent call last): File "main.py", line 16, in ensure_db a NameError: name 'a' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 36, in ensure_db(db_filename) File "main.py", line 27, in ensure_db for sql_scriptname in sql_scripts[current_db_version:]: UnboundLocalError: local variable 'current_db_version' referenced before assignment ============================================================================================ So what is going on here? Why does "finally" not have its print executed? Does the "...another exception occurred:..." interrupt the normal flow of the "try/except/finally" structure and prevent the "finally" block from executing? -- boB From robertvstepp at gmail.com Sun Oct 15 11:22:20 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 15 Oct 2017 10:22:20 -0500 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On Sun, Oct 15, 2017 at 2:56 AM, Peter Otten <__peter__ at web.de> wrote: > boB Stepp wrote: >> >> I am puzzled. If one is using version control, then what is the >> advantage of this incremental approach in creating and populating the >> database? Instead, why not just have a single SQL file that generates >> the finished initial state of the db for the current version? One can >> always use the version control system to roll back to an earlier >> stage. > > My idea was presented under the assumption that the there is user data > entered in version 1 that needs to be preserved when version 2 of the > application replaces 1. Ah! I was missing the forest for the trees. Sorry 'bout that! >> I have chickened out and not done a TDD approach yet. I will probably >> pause here, wait for feedback from this list, and try to figure out >> how I should test what I have so far. And how do you test SQL scripts >> anyway? > > Example: Run the routine to enter a row, then check if it's there and > contains what you expected. That should fail before the script is run, and > succeed afterwards. I guess what bothers me here is that it seems I have to write some code in the tests file just to get the test database to the point where I can write the necessary asserts. But I would have to do that anyway, wouldn't I? The whole point of test fixtures, setup and tear down code, etc. It just looks like testing dbs will be somewhat messier than what I have dealt with to date. >> >> Some things I am still pondering: >> >> 1) If I adopt the incremental approach to creating and initializing >> the working db, then it seems that the list, "sql_scripts", should not >> be hard-coded into the program. It seems to me it should be off >> somewhere by itself with perhaps other things that might evolve/change >> over time in its own file where it (and its brethren) are easy to >> locate and update. > > I think it should be hardcoded. You don't want to run arbitrary scripts that > happen to be in a folder, say. Version control can take care of any changes. In some ways I think too much. I struggle here on what should be my best practice, to minimize the amount of hard-coded data (I think usually a good idea.) or, in a particular case like this one, to do the hard-coding. >> >> 2) Likewise, "db_filename", is currently hard-coded in the if block >> to start the program. I have not decided yet what the end result will >> be, but I might want to allow for the possibility of allowing the user >> (me) to create multiple databases. > > If one user needs multiple databases that /may/ be an indication that you > are not storing enough information in the database. I was thinking ahead to a follow-up project, the chess rating db. For this I had contemplated having separate sqlite3 database files for each school year instead of adding school year information to a single database. -- boB From __peter__ at web.de Sun Oct 15 12:00:26 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 15 Oct 2017 18:00:26 +0200 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: boB Stepp wrote: > On Sun, Oct 15, 2017 at 3:09 AM, Peter Otten <__peter__ at web.de> wrote: >> boB Stepp wrote: >> >>> I have not used a "finally" block before. I just had the thought that >>> maybe it would run even if an uncaught exception might occur. I tried >>> to test this thought by generating a deliberate NameError in the "try" >>> block and added a print to the "finally" clause. I got the intended >>> NameError with no evidence of the added print printing. But I thought >>> I would ask just to be sure: If an uncaught exception occurs, will >>> the "finally" clause execute? >> >> >> Yes. >> >>>>> try: >> ... 1/0 >> ... except ValueError: >> ... print("not triggered") >> ... finally: >> ... print("ALWAYS TRIGGERED") >> ... >> ALWAYS TRIGGERED >> Traceback (most recent call last): >> File "", line 2, in >> ZeroDivisionError: division by zero > > That is what I thought after reading about "finally". But look what > happens if I modify my actual code to generate a NameError: > def ensure_db(filename): > """Open the database, "filename", if it exists; otherwise, create a > database named "filename".""" > > db = sqlite3.connect(filename) > cur = db.cursor() > > try: > sql_cmd = "SELECT VersionNumber FROM CurrentDBVersion" > a # This should generate a NameError Note that at this point `current_db_version` is not yet defined. > # First element of returned tuple will be the db version number: > current_db_version = int(cur.execute(sql_cmd).fetchone()[0]) > > except sqlite3.OperationalError: > # This means that the database and the table, "CurrentDBVersion", > # has not yet been created, implying "version 0". > current_db_version = 0 > > finally: > sql_scripts = ["../database/create_sqlite3_db.sql"] > for sql_scriptname in sql_scripts[current_db_version:]: The finally suite was entered, but now there's another NameError (an UnboundLocalError, to be precise), for current_db_version, inside it. Code inside the finally suite is executed like it would be anywhere else, so anything after the point where the exception was triggered is not run. > with open(sql_scriptname) as f: > cur.executescript(f.read()) > print("THIS IS THE FINALLY BLOCK!!!") # And this *should* print > > return db > This results in the following Traceback: >> py main.py > Traceback (most recent call last): > File "main.py", line 16, in ensure_db > a > NameError: name 'a' is not defined > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "main.py", line 36, in > ensure_db(db_filename) > File "main.py", line 27, in ensure_db > for sql_scriptname in sql_scripts[current_db_version:]: > UnboundLocalError: local variable 'current_db_version' referenced > before assignment While I tend to find chained exceptions annoying they *do* provide all the necessary information. > So what is going on here? Why does "finally" not have its print > executed? Does the "...another exception occurred:..." interrupt the > normal flow of the "try/except/finally" structure and prevent the > "finally" block from executing? > From __peter__ at web.de Sun Oct 15 12:10:32 2017 From: __peter__ at web.de (Peter Otten) Date: Sun, 15 Oct 2017 18:10:32 +0200 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: boB Stepp wrote: > I was thinking ahead to a follow-up project, the chess rating db. For > this I had contemplated having separate sqlite3 database files for > each school year instead of adding school year information to a single > database. Then you need to access multiple databases to see the full history of a student and you will end up with a lot of redundant information (name, birthday, contact information); if there's an error in the data it will typically only be corrected for the latest year, your users will come up with creative ways to copy data from one year to the next, introducing errors in the process... In short: it will be a be a mess. From neilc at norwich.edu Mon Oct 16 09:10:16 2017 From: neilc at norwich.edu (Neil Cerutti) Date: Mon, 16 Oct 2017 13:10:16 +0000 (UTC) Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: On 2017-10-15, boB Stepp wrote: > Some things I am still pondering: > > 1) If I adopt the incremental approach to creating and > initializing the working db, then it seems that the list, > "sql_scripts", should not be hard-coded into the program. It > seems to me it should be off somewhere by itself with perhaps > other things that might evolve/change over time in its own file > where it (and its brethren) are easy to locate and update. An incremental approach is not recommended if you are using the sqlite3 module. In sqlite modifying table definitions is limited to the simple addition of a new row of data. Any other change requires you to create a new table, copy the old data into it, and then drop the old table. > 3) I am supposed to be delving into writing classes on this > project. Should the code so far stay as a function or get > incorporated into a class? My original intent was to do a > class for the BloodPressureReadings table, but I am not at the > point of going there yet. I don't recommend cobbling together your own ORM, if that's what you are asking. ;) > 4) I wish there was a PEP 8 for SQL! I have several SQL books > I have consulted, but I have gotten conflicting suggestions for > SQL code style. I have tried to adopt something that seems to > me to be both consistent and reasonable, but is it good enough? As long as you can imagine yourself reading it two years from now, and being able to understand it and make changes, it's probably an OK style. The two big hurdles for me were acquiescing to uppercase all the SQL keywords, and learning the quoting rules. My impression is that SQL is very old, and so lots of different styles are valid and just fine to use. Your main goal, at first, should be to stick with standard SQL as much as you possibly can, to make it easier to switch database engines should you ever wish to do so. -- Neil Cerutti From mysecretrobotfactory at gmail.com Mon Oct 16 16:04:40 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 16 Oct 2017 13:04:40 -0700 Subject: [Tutor] Windows Memory Basics Message-ID: Hi all: I don't understand this part about the memory: if I used VirtualQueryEx to find out if a region of memory is ok to scan, and it says it's ok, are the values in the region arranged like this: short,int,double,long,char, double, short in as in, random? I am asking this because, if it's random, then I'd have to run ReadProcessMemory by increasing the value of of my loop by ONE (1) at a time, like this for i in range(start_of_region, end_of_region, 1): ReadProcessMemory(Process, i, ctypes.byref(buffer), ctypes.sizeof(buffer), ctypes.byref(nread)) Is that correct? From alan.gauld at yahoo.co.uk Mon Oct 16 19:48:16 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 17 Oct 2017 00:48:16 +0100 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: On 16/10/17 21:04, Michael C wrote: > I don't understand this part about the memory: And I'm not sure I understand your question but... > if I used VirtualQueryEx to find out if a region of memory is ok to scan, > and it > says it's ok, are the values in the region arranged like this: > > short,int,double,long,char, double, short in > > as in, random? They won't be random, they'll be in the order that the program that wrote the memory chose them to be in. For example the memory might contain some program variables and those variables may be of different types (assuming a compiled language like C++, say). Or it may be holding a complex data structure, like a class, that has fields of different types. What those types are will not be obvious and unless you know what you are reading will be impossible to guess in most cases since it is just a sequence of bytes and one set of 8 bits looks a lot like any other. > I am asking this because, if it's random, then I'd have to run > ReadProcessMemory > by increasing the value of of my loop by ONE (1) at a time, like this That doesn't really help, you need to know what each chunk of data represents and then increment the index by the size of each corresponding data type. For example if you have a string of 8 UTF8 characters that will probably be 8 bytes long(some UTF characters are more than 8 bits). But those 8 bytes could equally be a floating point number or a long integer or a struct containing 2 32 bit ints. You have absolutely no way to tell. And if you increment your index by one you will then look at the first 7 bytes plus one other. What is the 8th byte? It could be the start of another float, another UTF8 character or something else entirely. Things are then further complicated by the tendency to store data on word boundaries, so either 4 or 8 byte chunks, but even that can't be guaranteed since it could be a compressed memory scheme in action or a piece of assembler code taking the 'law' into its own hands. And of course it may not represent anything since many programs set aside memory spaqce for later use and either fill it with zeros or some other arbitrary pattern, or just leave it with whatever bits happened to already be there. > for i in range(start_of_region, end_of_region, 1): > ReadProcessMemory(Process, i, ctypes.byref(buffer), > ctypes.sizeof(buffer), ctypes.byref(nread)) > > Is that correct? Probably not. If you know what data you are reading you can do what you want, but if it's just a random block of memory you are scanning then its almost impossible to determine for certain what the raw data represents. If you have access to a *nix system (or cygwin on windows) it may help you to see the nature of the problem by running od -x on a text file You can find out what is in it by looking at it in a text editor but the hex listing will be meaningless. If that's what simple text looks like imagine what a binary file containing mixed data is like. -- 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 From steve at pearwood.info Mon Oct 16 22:20:40 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 17 Oct 2017 13:20:40 +1100 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: <20171017022039.GZ9068@ando.pearwood.info> On Mon, Oct 16, 2017 at 01:04:40PM -0700, Michael C wrote: > Hi all: > > > I don't understand this part about the memory: > > if I used VirtualQueryEx to find out if a region of memory is ok to scan, > and it > says it's ok, are the values in the region arranged like this: > > short,int,double,long,char, double, short in > > as in, random? I am not a Windows expert, but I doubt it. Memory is always an array of bytes. How you interpret that memory depends on what you are doing with it, and there's no way to tell from outside how it should be interpreted. (Some very clever, perhaps too clever, can even give the same chunk of memory two or more *valid* interpretations at the same time.) This implies that unless you know that this chunk of memory has some special meaning to Windows, the choice of how to interpret the chunk of memory is up to you. If you have a block of memory in hexadecimal that looks like this: 1f78a924b6c00be4f7546cda298860951a6c30d75640e62f82c8f5c0f1cb0bfc then it is entirely up to you whether you interpret it as: (1) 64 one-byte values: 1f 78 a9 24 ... (2) 32 two-byte values: 1f78 a924 b6c0 ... (3) 16 four-byte values: 1f78a924 b6c00be4 ... or something else. You could interpret them as ASCII bytes, Unicode code points, signed integers, unsigned integers, single- or double-precision floating point numbers, strings, or anything you like. -- Steve From chris_roysmith at internode.on.net Tue Oct 17 02:25:04 2017 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Tue, 17 Oct 2017 17:25:04 +1100 Subject: [Tutor] problems using a listbox Message-ID: <9e9b170f-66e9-14ed-e2d4-b753944ff26c@internode.on.net> Hi, OS: Linux Chris-X451MA 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 17:28:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux Python 3.5.2 (default, Sep 14 2017, 22:51:06) I am trying to learn how to use a tkinter listbox. When I execute my experimental code, an odd index is printed immediately (output below code), index looks wrong (shouldn?t it be an integer). Also it doesn't print new values when I select an entry. --------------------------------------------------- #!/usr/bin/python3 #test listbox from tkinter import * class Dialog(Frame): ??? def __init__(self, master): ??????? Frame.__init__(self, master) ??????? self.list = Listbox(self, selectmode=EXTENDED) ??????? self.list.pack(fill=BOTH, expand=1) ??????? self.current = None ??????? self.poll() # start polling the list ??? def poll(self): ??????? now = self.list.curselection() ??????? if now != self.current: ??????????? self.list_has_changed(now) ??????????? self.current = now ??????? self.after(250, self.poll) ??? def list_has_changed(self, selection): ??????? print ("selection is", selection) snames=('fred', 'george', 'manuel', 'john', 'eric', 'terry') master = Tk() listbox = Listbox(master) listbox.grid(row=0) for item in snames: ??? listbox.insert(END, item) myindicator=Dialog.list_has_changed(master, listbox) mainloop() ----------------------------------------- output: --------------------------------------- ./listbox.py selection is .140537834621024 Thank you for any help Regards, Chris Roy-Smith From mysecretrobotfactory at gmail.com Mon Oct 16 19:53:39 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 16 Oct 2017 16:53:39 -0700 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: ah, i am bummed completely haha. Is there a way to tell which parts a variables so I can scan it? Maybe you could point me to some reading materials? thanks :) On Mon, Oct 16, 2017 at 4:48 PM, Alan Gauld via Tutor wrote: > On 16/10/17 21:04, Michael C wrote: > > > I don't understand this part about the memory: > > And I'm not sure I understand your question but... > > > if I used VirtualQueryEx to find out if a region of memory is ok to scan, > > and it > > says it's ok, are the values in the region arranged like this: > > > > short,int,double,long,char, double, short in > > > > as in, random? > > They won't be random, they'll be in the order that the > program that wrote the memory chose them to be in. For > example the memory might contain some program variables > and those variables may be of different types (assuming > a compiled language like C++, say). Or it may be holding > a complex data structure, like a class, that has fields > of different types. > > What those types are will not be obvious and unless you > know what you are reading will be impossible to guess > in most cases since it is just a sequence of bytes and > one set of 8 bits looks a lot like any other. > > > I am asking this because, if it's random, then I'd have to run > > ReadProcessMemory > > by increasing the value of of my loop by ONE (1) at a time, like this > > That doesn't really help, you need to know what each > chunk of data represents and then increment the index > by the size of each corresponding data type. > > For example if you have a string of 8 UTF8 characters > that will probably be 8 bytes long(some UTF characters > are more than 8 bits). But those 8 bytes could equally > be a floating point number or a long integer or a > struct containing 2 32 bit ints. You have absolutely > no way to tell. > > And if you increment your index by one you will then > look at the first 7 bytes plus one other. What is > the 8th byte? It could be the start of another float, > another UTF8 character or something else entirely. > > Things are then further complicated by the tendency > to store data on word boundaries, so either 4 or > 8 byte chunks, but even that can't be guaranteed > since it could be a compressed memory scheme in > action or a piece of assembler code taking the > 'law' into its own hands. > > And of course it may not represent anything since > many programs set aside memory spaqce for later use > and either fill it with zeros or some other arbitrary > pattern, or just leave it with whatever bits happened > to already be there. > > > for i in range(start_of_region, end_of_region, 1): > > ReadProcessMemory(Process, i, ctypes.byref(buffer), > > ctypes.sizeof(buffer), ctypes.byref(nread)) > > > > Is that correct? > > Probably not. If you know what data you are reading you > can do what you want, but if it's just a random block > of memory you are scanning then its almost impossible > to determine for certain what the raw data represents. > > If you have access to a *nix system (or cygwin > on windows) it may help you to see the nature > of the problem by running od -x on a text file > You can find out what is in it by looking at it > in a text editor but the hex listing will be > meaningless. If that's what simple text looks > like imagine what a binary file containing > mixed data is like. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Mon Oct 16 20:02:31 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 16 Oct 2017 17:02:31 -0700 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: Hold on, supposed by using Openprocess and VirtualQueryEx, I have the locations of all the memory the application is using, wouldn't this to be true? Say, a 8 byte data is somewhere in the region i am scanning. Ok, I know by scanning it like this for n in range(start,end,1) will read into another variable and mostly nothing, but unless a variable, that is, one number, can be truncated and exist in multiple locations like this double = 12345678 123 is at x001 45 is at x005 678 is at x010 unless a number can be broken up like that, wouldn't I, while use the silly 'increment by one' approach, actually luck out and get that value in it's actual position? On Mon, Oct 16, 2017 at 4:53 PM, Michael C wrote: > ah, i am bummed completely haha. > > Is there a way to tell which parts a variables so I can scan it? > Maybe you could point me to some reading materials? > > thanks :) > > On Mon, Oct 16, 2017 at 4:48 PM, Alan Gauld via Tutor > wrote: > >> On 16/10/17 21:04, Michael C wrote: >> >> > I don't understand this part about the memory: >> >> And I'm not sure I understand your question but... >> >> > if I used VirtualQueryEx to find out if a region of memory is ok to >> scan, >> > and it >> > says it's ok, are the values in the region arranged like this: >> > >> > short,int,double,long,char, double, short in >> > >> > as in, random? >> >> They won't be random, they'll be in the order that the >> program that wrote the memory chose them to be in. For >> example the memory might contain some program variables >> and those variables may be of different types (assuming >> a compiled language like C++, say). Or it may be holding >> a complex data structure, like a class, that has fields >> of different types. >> >> What those types are will not be obvious and unless you >> know what you are reading will be impossible to guess >> in most cases since it is just a sequence of bytes and >> one set of 8 bits looks a lot like any other. >> >> > I am asking this because, if it's random, then I'd have to run >> > ReadProcessMemory >> > by increasing the value of of my loop by ONE (1) at a time, like this >> >> That doesn't really help, you need to know what each >> chunk of data represents and then increment the index >> by the size of each corresponding data type. >> >> For example if you have a string of 8 UTF8 characters >> that will probably be 8 bytes long(some UTF characters >> are more than 8 bits). But those 8 bytes could equally >> be a floating point number or a long integer or a >> struct containing 2 32 bit ints. You have absolutely >> no way to tell. >> >> And if you increment your index by one you will then >> look at the first 7 bytes plus one other. What is >> the 8th byte? It could be the start of another float, >> another UTF8 character or something else entirely. >> >> Things are then further complicated by the tendency >> to store data on word boundaries, so either 4 or >> 8 byte chunks, but even that can't be guaranteed >> since it could be a compressed memory scheme in >> action or a piece of assembler code taking the >> 'law' into its own hands. >> >> And of course it may not represent anything since >> many programs set aside memory spaqce for later use >> and either fill it with zeros or some other arbitrary >> pattern, or just leave it with whatever bits happened >> to already be there. >> >> > for i in range(start_of_region, end_of_region, 1): >> > ReadProcessMemory(Process, i, ctypes.byref(buffer), >> > ctypes.sizeof(buffer), ctypes.byref(nread)) >> > >> > Is that correct? >> >> Probably not. If you know what data you are reading you >> can do what you want, but if it's just a random block >> of memory you are scanning then its almost impossible >> to determine for certain what the raw data represents. >> >> If you have access to a *nix system (or cygwin >> on windows) it may help you to see the nature >> of the problem by running od -x on a text file >> You can find out what is in it by looking at it >> in a text editor but the hex listing will be >> meaningless. If that's what simple text looks >> like imagine what a binary file containing >> mixed data is like. >> >> -- >> 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 at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From alan.gauld at yahoo.co.uk Tue Oct 17 04:45:16 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 17 Oct 2017 09:45:16 +0100 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: On 17/10/17 00:53, Michael C wrote: > ah, i am bummed completely haha. > > Is there a way to tell which parts a variables so I can scan it? > Maybe you could point me to some reading materials? There are some rules about where programs store data within their memory space, but typically that will only give you the start address of a data area. It still doesn't give you any clue as to what is stored in that area in terms of data types. As to reading material there are several books on OS that you could try. One of the easiest and shortest is "Fundamentals of OS" by Lister. But because its general in nature it won;t help with Windows specifics. For windows specifics its back to MSDN, but that is not very accessible. -- 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 From __peter__ at web.de Tue Oct 17 05:13:42 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 17 Oct 2017 11:13:42 +0200 Subject: [Tutor] problems using a listbox References: <9e9b170f-66e9-14ed-e2d4-b753944ff26c@internode.on.net> Message-ID: Chris Roy-Smith wrote: > OS: Linux Chris-X451MA 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 > 17:28:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux > > Python 3.5.2 (default, Sep 14 2017, 22:51:06) > > I am trying to learn how to use a tkinter listbox. When I execute my > experimental code, an odd index is printed immediately (output below > code), index looks wrong (shouldn?t it be an integer). Also it doesn't > print new values when I select an entry. > > --------------------------------------------------- > > #!/usr/bin/python3 > #test listbox > from tkinter import * > > class Dialog(Frame): > > def __init__(self, master): > Frame.__init__(self, master) > self.list = Listbox(self, selectmode=EXTENDED) > self.list.pack(fill=BOTH, expand=1) > self.current = None > self.poll() # start polling the list > > def poll(self): > now = self.list.curselection() > if now != self.current: > self.list_has_changed(now) > self.current = now > self.after(250, self.poll) > > def list_has_changed(self, selection): > print ("selection is", selection) > > > snames=('fred', 'george', 'manuel', 'john', 'eric', 'terry') > master = Tk() > > listbox = Listbox(master) > listbox.grid(row=0) > > for item in snames: > listbox.insert(END, item) The problem is your inconsistent use of the Dialog class which you do not instantiate. > myindicator=Dialog.list_has_changed(master, listbox) Here you call list_has_changed() like a function. The first argument (self) is your `master` object, the second is the listbox. In a nutshell you are doing >>> from tkinter import * >>> master = Tk() >>> listbox = Listbox(master) >>> print(listbox) .140206885684560 and what is displayed when you are printing a tkinter widget is its Tcl/Tk name -- which admittedly looks odd. > mainloop() A fix with minimal changes might be #!/usr/bin/python3 #test listbox from tkinter import * class Dialog(Frame): def __init__(self, master): Frame.__init__(self, master) self.list = Listbox(self, selectmode=EXTENDED) self.list.pack(fill=BOTH, expand=1) self.current = None self.poll() # start polling the list def poll(self): now = self.list.curselection() if now != self.current: self.list_has_changed(now) self.current = now self.after(250, self.poll) def list_has_changed(self, selection): print ("selection is", selection) snames=('fred', 'george', 'manuel', 'john', 'eric', 'terry') master = Tk() dialog = Dialog(master) dialog.pack() for item in snames: dialog.list.insert(END, item) mainloop() From alan.gauld at yahoo.co.uk Tue Oct 17 05:17:01 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 17 Oct 2017 10:17:01 +0100 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: On 17/10/17 01:02, Michael C wrote: > that is, one number, can be truncated and exist in multiple locations like > this > > double = 12345678 > > 123 is at x001 > 45 is at x005 > 678 is at x010 That won't happen, a single variable will always be in a a single area. But the representation won't be anything like you suggested. A single number 12345678(assuming its a decimal integer) will be stored as 0xbc614e, which is 3 bytes, so it will be part of a 4byte (assuming a 32bit integer) chunk of storage. Of course if the program declared the variable to be a long then the same 3 bytes will be stored within an 8 byte chunk. And if it was stored as a double floating point value then the byte representation will be entirely different (and I don't even know what that would be). > unless a number can be broken up like that, wouldn't I, > while use the silly 'increment by one' approach, > actually luck out and get that value in it's actual position? Yes, if you know that the decimal number 12345678 is stored somewhere in memory, you can scan looking for the 3 bytes 0xbc, 0x61,0x4e. And if you also know it was stored in a 32 bit int you can check for zero before the first byte (or second, or last) depending on the endian storage system used by your OS). But you still don't know for sure that you didn't just find a byte of 0xbc followed by the start of a UTF8 string beginning with the characters 'aN'... And there are likely to be several hits not just one. You need to figure out which are your number and which are just groups of 3 bytes that happen to look like it. If you are very clever you can look at the data surrounding each set of bytes and make a fair guess about ones which are not likely to be your variable (as above you might look to see if the following bytes are all viable ascii characters which might indicate that it was indeed a string and not your number). But that may still leave several candidates, and its all fraught with difficulty. If you do know the data types involved you can read your memory into a buffer and apply the struct module to interpret it (possibly in multiple ways) to extract the values but you must know the nature of what you are reading to be able to interpret it. ie. you need to know the types. Reading the bytes in memory is one thing, and relatively easy. Interpreting those bytes as actual data is nigh impossible unless you know in advance what data types you are looking at. -- 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 From james at uplinkzero.com Tue Oct 17 05:37:46 2017 From: james at uplinkzero.com (James Chapman) Date: Tue, 17 Oct 2017 10:37:46 +0100 Subject: [Tutor] Windows Memory Basics Message-ID: We're heading into advanced territory here and I might get told off but... Consider this C++ program for a second, it has a struct with different types of variables which sit next to each other in memory. When you print the byte values of the struct, you can see that there is no easy way to know which byte value belongs to which variable unless you already know the layout. ----------------- #include typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; #pragma pack(1) struct mem { char c; // 1 byte WORD wn; // 2 byte DWORD dwn; // 4 byte int i; // 4 byte BYTE b; // 1 byte }; int main() { mem s; s.c = 0xFF; s.wn = 0xFFFFF; s.dwn = 0xFFFFFFFF; s.i = 0xFFFFFFFF; s.b = 0xFF; BYTE * memPointer = reinterpret_cast(&s); for (int i = 0; i < sizeof(mem); i++) printf("%02d [0x%08x] = %x \n", i, memPointer, *memPointer++); return 0; } --------------------- Prints 00 [0xecf0f789] = ff 01 [0xecf0f78a] = ff 02 [0xecf0f78b] = ff 03 [0xecf0f78c] = ff 04 [0xecf0f78d] = ff 05 [0xecf0f78e] = ff 06 [0xecf0f78f] = ff 07 [0xecf0f790] = ff 08 [0xecf0f791] = ff 09 [0xecf0f792] = ff 10 [0xecf0f793] = ff 11 [0xecf0f794] = ff Packing can also come into play. If you change the packing to 2, you get this: 00 [0x7d4ffcd9] = ff 01 [0x7d4ffcda] = cc 02 [0x7d4ffcdb] = ff 03 [0x7d4ffcdc] = ff 04 [0x7d4ffcdd] = ff 05 [0x7d4ffcde] = ff 06 [0x7d4ffcdf] = ff 07 [0x7d4ffce0] = ff 08 [0x7d4ffce1] = ff 09 [0x7d4ffce2] = ff 10 [0x7d4ffce3] = ff 11 [0x7d4ffce4] = ff 12 [0x7d4ffce5] = ff 13 [0x7d4ffce6] = cc And if you change it to 4, you get this: 00 [0xf4f5fbf9] = ff 01 [0xf4f5fbfa] = cc 02 [0xf4f5fbfb] = ff 03 [0xf4f5fbfc] = ff 04 [0xf4f5fbfd] = ff 05 [0xf4f5fbfe] = ff 06 [0xf4f5fbff] = ff 07 [0xf4f5fc00] = ff 08 [0xf4f5fc01] = ff 09 [0xf4f5fc02] = ff 10 [0xf4f5fc03] = ff 11 [0xf4f5fc04] = ff 12 [0xf4f5fc05] = ff 13 [0xf4f5fc06] = cc 14 [0xf4f5fc07] = cc 15 [0xf4f5fc08] = cc In other words, even if you have the source code for the program you want to scan in memory, depending on the compiler settings the memory layout could have changed, or rather not be what you expected due to packing and alignment. Probably not the answer you were hoping for but I hope this helps. -- James On 17 October 2017 at 01:02, Michael C wrote: > Hold on, supposed by using Openprocess and VirtualQueryEx, I have the > locations of all the memory the application is using, wouldn't this to be > true? > > Say, a 8 byte data is somewhere in the region i am scanning. Ok, I know by > scanning it like this > for n in range(start,end,1) > > will read into another variable and mostly nothing, but unless a variable, > that is, one number, can be truncated and exist in multiple locations like > this > > double = 12345678 > > 123 is at x001 > 45 is at x005 > 678 is at x010 > > unless a number can be broken up like that, wouldn't I, while use the silly > 'increment by one' approach, actually luck out and get that value in it's > actual position? > > From alan.gauld at yahoo.co.uk Tue Oct 17 05:49:18 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 17 Oct 2017 10:49:18 +0100 Subject: [Tutor] problems using a listbox In-Reply-To: <9e9b170f-66e9-14ed-e2d4-b753944ff26c@internode.on.net> References: <9e9b170f-66e9-14ed-e2d4-b753944ff26c@internode.on.net> Message-ID: On 17/10/17 07:25, Chris Roy-Smith wrote: > I am trying to learn how to use a tkinter listbox. When I execute my > experimental code, an odd index is printed immediately (output below > code), index looks wrong (shouldn?t it be an integer). You pass in the widget so thats what gets printed (the dot notation is Tk's internal representation of the widget ID) > Also it doesn't print new values when I select an entry. I have no idea why you think it would, your code doesn't even try to do that? > class Dialog(Frame): > > ??? def __init__(self, master): > ??????? Frame.__init__(self, master) > ??????? self.list = Listbox(self, selectmode=EXTENDED) > ??????? self.list.pack(fill=BOTH, expand=1) > ??????? self.current = None > ??????? self.poll() # start polling the list > > ??? def poll(self): > ??????? now = self.list.curselection() > ??????? if now != self.current: > ??????????? self.list_has_changed(now) > ??????????? self.current = now > ??????? self.after(250, self.poll) > > ??? def list_has_changed(self, selection): > ??????? print ("selection is", selection) You define a class here but never create an instance of it. Also it polls for changes in curselection - those will only happen when you click on an item in the listbox in your dialog. > snames=('fred', 'george', 'manuel', 'john', 'eric', 'terry') > master = Tk() > > listbox = Listbox(master) > listbox.grid(row=0) > for item in snames: > listbox.insert(END, item) And here you create a different listbox not in your dialog and completely invisible to the code in your class. > myindicator=Dialog.list_has_changed(master, listbox) You are trying to call the method on the class passing in the external listbox id as the selection value. BTW. Polling is usually the wrong thing to do in Tkinter, you should bind a method to the mouse click event, something like: self.list.bind('', self.list_has_changed) And modify list_has_changed to: def list_has_changed(self): print ("selection is", self.list.curselection()) And you probably don't need the class, just use functions. Here is what I think you wanted: ############################ from tkinter import * def list_has_changed(event): #bind requires an event parameter print ("selection is", listbox.curselection()) names=('fred', 'george', 'manuel', 'john', 'eric', 'terry') master = Tk() listbox = Listbox(master) listbox.pack() for item in names: listbox.insert(END, item) listbox.bind('', list_has_changed) master.mainloop() ######################### Note that curselection() always returns a tuple since there could be multiple items elected. -- 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 From chris_roysmith at internode.on.net Tue Oct 17 05:33:44 2017 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Tue, 17 Oct 2017 20:33:44 +1100 Subject: [Tutor] problems using a listbox In-Reply-To: References: <9e9b170f-66e9-14ed-e2d4-b753944ff26c@internode.on.net> Message-ID: <62970c71-abb9-5e13-9160-e548f2b08d2e@internode.on.net> On 17/10/17 20:13, Peter Otten wrote: > #!/usr/bin/python3 > #test listbox > from tkinter import * > > class Dialog(Frame): > > def __init__(self, master): > Frame.__init__(self, master) > self.list = Listbox(self, selectmode=EXTENDED) > self.list.pack(fill=BOTH, expand=1) > self.current = None > self.poll() # start polling the list > > def poll(self): > now = self.list.curselection() > if now != self.current: > self.list_has_changed(now) > self.current = now > self.after(250, self.poll) > > def list_has_changed(self, selection): > print ("selection is", selection) > > > snames=('fred', 'george', 'manuel', 'john', 'eric', 'terry') > master = Tk() > > dialog = Dialog(master) > dialog.pack() > > for item in snames: > dialog.list.insert(END, item) > > mainloop() Thank you, that works, I have difficulties with object oriented coding. Never used a language where I needed it. Regards, Chris Roy-Smith From sjeik_appie at hotmail.com Wed Oct 18 16:09:31 2017 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 18 Oct 2017 20:09:31 +0000 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? Message-ID: On Oct 16, 2017 15:12, Neil Cerutti wrote: > > On 2017-10-15, boB Stepp wrote: > > Some things I am still pondering: > > > > 1) If I adopt the incremental approach to creating and > > initializing the working db, then it seems that the list, > > "sql_scripts", should not be hard-coded into the program. It > > seems to me it should be off somewhere by itself with perhaps > > other things that might evolve/change over time in its own file > > where it (and its brethren) are easy to locate and update. > > An incremental approach is not recommended if you are using the > sqlite3 module. In sqlite modifying table definitions is limited > to the simple addition of a new row of data. Any other change > requires you to create a new table, copy the old data into it, > and then drop the old table. Really? How so? I used ALTER TABLE ADD COLUMN the other day, which is part of this SQL dialect: https://sqlite.org/lang_altertable.html From alan.gauld at yahoo.co.uk Thu Oct 19 03:26:21 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 19 Oct 2017 08:26:21 +0100 Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? In-Reply-To: References: Message-ID: On 18/10/17 21:09, Albert-Jan Roskam wrote: > > On Oct 16, 2017 15:12, Neil Cerutti wrote: >> sqlite3 module. In sqlite modifying table definitions is limited >> to the simple addition of a new row of data. > > Really? How so? I used ALTER TABLE ADD COLUMN the other day, I think Neil meant that Sqlite ALTER is limited to adding *columns*. Adding rows would be an INSERT not an ALTER. The only other thing SQLite ALTER can do is change the table name. But most SQLs allow you to modify existing column definitions too, such as add defaults or conditions. You can't do that in SQLite, you need to create a new table (TEMP)and copy the data across then drop the old table and rename the new one to the old. All a bit tedious. -- 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 From neilc at norwich.edu Thu Oct 19 10:20:43 2017 From: neilc at norwich.edu (Neil Cerutti) Date: Thu, 19 Oct 2017 14:20:43 +0000 (UTC) Subject: [Tutor] How to test for the existence of a table in a sqlite3 db? References: Message-ID: On 2017-10-19, Alan Gauld via Tutor wrote: > On 18/10/17 21:09, Albert-Jan Roskam wrote: >> >> On Oct 16, 2017 15:12, Neil Cerutti wrote: > >>> sqlite3 module. In sqlite modifying table definitions is limited >>> to the simple addition of a new row of data. > >> >> Really? How so? I used ALTER TABLE ADD COLUMN the other day, > > I think Neil meant that Sqlite ALTER is limited to adding > *columns*. Adding rows would be an INSERT not an ALTER. > > The only other thing SQLite ALTER can do is change the table name. > > But most SQLs allow you to modify existing column definitions > too, such as add defaults or conditions. You can't do that in > SQLite, you need to create a new table (TEMP)and copy the data > across then drop the old table and rename the new one to the > old. All a bit tedious. Yes, I meant sqlite can only add columns. Thanks for the corrections. -- Neil Cerutti From mysecretrobotfactory at gmail.com Fri Oct 20 17:59:29 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 20 Oct 2017 14:59:29 -0700 Subject: [Tutor] I found something nice! Message-ID: Hi all! My project needs to use MEMORY_BASIC_INFORMATION() and SYSTEM_INFO() structures, and previously I thought there wasn't a way (thanks to ErykSun, who wrote it for me, which I have been using) Now I found out someone actually wrote a long time ago, so all I have to do is to download and install it! Now, I installed my numpy with pip install numpy and that worked like a charm but how do I get this? https://stackoverflow.com/questions/21401663/python-windows-api-dump-process-in-buffer-then-regex-search https://github.com/jaimeblasco/Open-Source-Hackers/blob/master/memYara/constants/structures.py Thanks for reading! From mysecretrobotfactory at gmail.com Sat Oct 21 14:57:12 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 21 Oct 2017 11:57:12 -0700 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: cool stuff! On Tue, Oct 17, 2017 at 2:17 AM, Alan Gauld via Tutor wrote: > On 17/10/17 01:02, Michael C wrote: > > > that is, one number, can be truncated and exist in multiple locations > like > > this > > > > double = 12345678 > > > > 123 is at x001 > > 45 is at x005 > > 678 is at x010 > > That won't happen, a single variable will always be in a a single > area. > > But the representation won't be anything like you suggested. A > single number 12345678(assuming its a decimal integer) will be > stored as 0xbc614e, which is 3 bytes, so it will be part of > a 4byte (assuming a 32bit integer) chunk of storage. > Of course if the program declared the variable to be a long > then the same 3 bytes will be stored within an 8 byte chunk. > And if it was stored as a double floating point value then > the byte representation will be entirely different (and > I don't even know what that would be). > > > unless a number can be broken up like that, wouldn't I, > > while use the silly 'increment by one' approach, > > actually luck out and get that value in it's actual position? > > Yes, if you know that the decimal number 12345678 is stored > somewhere in memory, you can scan looking for the 3 bytes 0xbc, > 0x61,0x4e. And if you also know it was stored in a 32 bit int > you can check for zero before the first byte (or second, or last) > depending on the endian storage system used by your OS). > > But you still don't know for sure that you didn't just find a > byte of 0xbc followed by the start of a UTF8 string beginning > with the characters 'aN'... And there are likely to be several > hits not just one. You need to figure out which are your number > and which are just groups of 3 bytes that happen to look like it. > > If you are very clever you can look at the data surrounding > each set of bytes and make a fair guess about ones which > are not likely to be your variable (as above you might look > to see if the following bytes are all viable ascii characters > which might indicate that it was indeed a string and not > your number). But that may still leave several candidates, > and its all fraught with difficulty. > > If you do know the data types involved you can read your > memory into a buffer and apply the struct module to > interpret it (possibly in multiple ways) to extract > the values but you must know the nature of what you are > reading to be able to interpret it. ie. you need to know > the types. > > Reading the bytes in memory is one thing, and relatively easy. > Interpreting those bytes as actual data is nigh impossible > unless you know in advance what data types you are looking at. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Sat Oct 21 14:58:34 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Sat, 21 Oct 2017 11:58:34 -0700 Subject: [Tutor] Windows Memory Basics In-Reply-To: References: Message-ID: I am going to put your reply in a special place, for the day I can understand it :) On Tue, Oct 17, 2017 at 2:37 AM, James Chapman wrote: > We're heading into advanced territory here and I might get told off but... > Consider this C++ program for a second, it has a struct with different > types of variables which sit next to each other in memory. When you print > the byte values of the struct, you can see that there is no easy way to > know which byte value belongs to which variable unless you already know the > layout. > > ----------------- > #include > > typedef unsigned char BYTE; > typedef unsigned short WORD; > typedef unsigned long DWORD; > > #pragma pack(1) > struct mem > { > char c; // 1 byte > WORD wn; // 2 byte > DWORD dwn; // 4 byte > int i; // 4 byte > BYTE b; // 1 byte > }; > > int main() > { > mem s; > s.c = 0xFF; > s.wn = 0xFFFFF; > s.dwn = 0xFFFFFFFF; > s.i = 0xFFFFFFFF; > s.b = 0xFF; > > BYTE * memPointer = reinterpret_cast(&s); > > for (int i = 0; i < sizeof(mem); i++) > printf("%02d [0x%08x] = %x \n", i, memPointer, *memPointer++); > > return 0; > } > --------------------- > > Prints > > 00 [0xecf0f789] = ff > 01 [0xecf0f78a] = ff > 02 [0xecf0f78b] = ff > 03 [0xecf0f78c] = ff > 04 [0xecf0f78d] = ff > 05 [0xecf0f78e] = ff > 06 [0xecf0f78f] = ff > 07 [0xecf0f790] = ff > 08 [0xecf0f791] = ff > 09 [0xecf0f792] = ff > 10 [0xecf0f793] = ff > 11 [0xecf0f794] = ff > > Packing can also come into play. If you change the packing to 2, you get > this: > > 00 [0x7d4ffcd9] = ff > 01 [0x7d4ffcda] = cc > 02 [0x7d4ffcdb] = ff > 03 [0x7d4ffcdc] = ff > 04 [0x7d4ffcdd] = ff > 05 [0x7d4ffcde] = ff > 06 [0x7d4ffcdf] = ff > 07 [0x7d4ffce0] = ff > 08 [0x7d4ffce1] = ff > 09 [0x7d4ffce2] = ff > 10 [0x7d4ffce3] = ff > 11 [0x7d4ffce4] = ff > 12 [0x7d4ffce5] = ff > 13 [0x7d4ffce6] = cc > > And if you change it to 4, you get this: > > 00 [0xf4f5fbf9] = ff > 01 [0xf4f5fbfa] = cc > 02 [0xf4f5fbfb] = ff > 03 [0xf4f5fbfc] = ff > 04 [0xf4f5fbfd] = ff > 05 [0xf4f5fbfe] = ff > 06 [0xf4f5fbff] = ff > 07 [0xf4f5fc00] = ff > 08 [0xf4f5fc01] = ff > 09 [0xf4f5fc02] = ff > 10 [0xf4f5fc03] = ff > 11 [0xf4f5fc04] = ff > 12 [0xf4f5fc05] = ff > 13 [0xf4f5fc06] = cc > 14 [0xf4f5fc07] = cc > 15 [0xf4f5fc08] = cc > > > In other words, even if you have the source code for the program you want > to scan in memory, depending on the compiler settings the memory layout > could have changed, or rather not be what you expected due to packing and > alignment. > > Probably not the answer you were hoping for but I hope this helps. > > -- > James > > > > > On 17 October 2017 at 01:02, Michael C > wrote: > >> Hold on, supposed by using Openprocess and VirtualQueryEx, I have the >> locations of all the memory the application is using, wouldn't this to be >> true? >> >> Say, a 8 byte data is somewhere in the region i am scanning. Ok, I know by >> scanning it like this >> for n in range(start,end,1) >> >> will read into another variable and mostly nothing, but unless a variable, >> that is, one number, can be truncated and exist in multiple locations like >> this >> >> double = 12345678 >> >> 123 is at x001 >> 45 is at x005 >> 678 is at x010 >> >> unless a number can be broken up like that, wouldn't I, while use the >> silly >> 'increment by one' approach, actually luck out and get that value in it's >> actual position? >> >> From histolitician at gmail.com Sun Oct 22 20:40:06 2017 From: histolitician at gmail.com (Historitical Show) Date: Sun, 22 Oct 2017 20:40:06 -0400 Subject: [Tutor] Coding question: How to call a function when the function is assigned a variable Message-ID: This is what I have so far. I am trying to make it so that it measures how long it takes the user to get 20 scores. If it takes 1 second or less,then then they are obviously spamming ENTER by holding it down. I want tocheck and if they are holding down the key, then it will pause the program. Here is what I have so far: import time coins = 0 score = 0 cptype = 1 start = time.time() end = time.time() def main(): global coins, score, cptype, start, end if start - end <= 1 and coins == 20: print() print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") print(" STOP! ") print(" You can't hold down the ENTER key! ") print(" Please type correctly! ") print(" The game will now pause temporarily ") print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") print() time.sleep(5) else: start is True type = input("") if type == "": score = score + 1 coins = coins + cptype end is True print(start - end) main() main() From renukeshnk15 at gmail.com Mon Oct 23 00:33:21 2017 From: renukeshnk15 at gmail.com (renukesh nk) Date: Mon, 23 Oct 2017 10:03:21 +0530 Subject: [Tutor] script guidance Message-ID: i want to download zip files from website , my script first lists all the url links to a text file and then fetches each url and tries to download zip files. but i am getting error as below: Running script.. https://sagamusix.dehttps:// sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/bass.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/bass_drums.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/drums.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/fx.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/pads_strings.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/powerchords.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/synths.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/tr-808.zip /n https://sagamusix.dehttps://sagamusix.de/sample_collection/tr-909.zip /n Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip Trying to reach https://sagamusix.dehttps:// sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip We failed to reach a server.https://sagamusix.dehttps:// sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip Reason: [Errno 11001] getaddrinfo failed bass.zip please help me to fix so that i acn download all the zip files code: import urllib2 from urllib2 import Request, urlopen, URLError #import urllib import os from bs4 import BeautifulSoup # import socket # socket.getaddrinfo('localhost', 8080) #Create a new directory to put the files into #Get the current working directory and create a new directory in it named test cwd = os.getcwd() newdir = cwd +"\\test" print "The current Working directory is " + cwd os.mkdir( newdir); print "Created new directory " + newdir newfile = open('zipfiles.txt','w') print newfile print "Running script.. " #Set variable for page to be open and url to be concatenated url = "https://sagamusix.de" page = urllib2.urlopen('https://sagamusix.de/en/samples/').read() #File extension to be looked for. extension = ".zip" #Use BeautifulSoup to clean up the page soup = BeautifulSoup(page, "html5lib") soup.prettify() #Find all the links on the page that end in .zip for anchor in soup.findAll('a', href=True): links = url + anchor['href'] if links.endswith(extension): newfile.write(links + '\n') newfile.close() #Read what is saved in zipfiles.txt and output it to the user #This is done to create presistent data newfile = open('zipfiles.txt', 'r') for line in newfile: print line + '/n' newfile.close() #Read through the lines in the text file and download the zip files. #Handle exceptions and print exceptions to the console with open('zipfiles.txt', 'r') as url: for line in url: if line.find('/'): print line.rsplit('/', 1)[1] try: ziplink = line #Removes the first 48 characters of the url to get the name of the file zipfile = line[24:] #Removes the last 4 characters to remove the .zip zipfile2 = zipfile[:3] print "Trying to reach " + ziplink response = urllib2.urlopen(ziplink) except URLError as e: print 'We failed to reach a server.'+ziplink if hasattr(e, 'reason'): print 'Reason: ', e.reason continue elif hasattr(e, 'code'): print 'The server couldnt fulfill the request.' print 'Error code: ', e.code continue else: zipcontent = response.read() completeName = os.path.join(newdir, zipfile2+ ".zip") with open (completeName, 'w') as f: print "downloading.. " + zipfile f.write(zipcontent) f.close() print "Script completed" From alan.gauld at yahoo.co.uk Mon Oct 23 03:15:54 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 23 Oct 2017 08:15:54 +0100 Subject: [Tutor] Coding question: How to call a function when the function is assigned a variable In-Reply-To: References: Message-ID: On 23/10/17 01:40, Historitical Show wrote: > Here is what I have so far: > > import time > coins = 0 > score = 0 > cptype = 1 > start = time.time() > end = time.time() Note that you are setting start and end to almost identical times and never change then later. > def main(): > global coins, score, cptype, start, end Rather than declare the variables above then make them global just define them inside the function. > if start - end <= 1 and coins == 20: > ... > time.sleep(5) > else: > start is True I'm not sure what you think this does but in fact it performs a test to see if start is the literal value True - which it isn't, it was set to time() So the test result will be False. But you don't store the result you throw it way. So the real effect of this line is to do nothing except waste a tiny amount of time. Maybe you wanted to reassign the time here? start = time.time() > type = input("") > if type == "": > score = score + 1 > coins = coins + cptype > end is True same as above, I suspect you really want to set the end time. > print(start - end) > main() While this will work it's not a good technique for a potentially indefinitely repeating loop, instead use a while. In your case you wanted to read 20 values, but you don't check that here. Why not use while score < 20: your code here.... ...without the call to main() -- 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 From __peter__ at web.de Mon Oct 23 05:56:04 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 23 Oct 2017 11:56:04 +0200 Subject: [Tutor] script guidance References: Message-ID: renukesh nk wrote: > i want to download zip files from website , my script first lists all the > url links to a text file and then fetches each url and tries to download > zip files. > > > but i am getting error as below: > Running script.. > https://sagamusix.dehttps:// > sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/bass.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/bass_drums.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/drums.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/fx.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/pads_strings.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/powerchords.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/synths.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/tr-808.zip > /n > https://sagamusix.dehttps://sagamusix.de/sample_collection/tr-909.zip > /n > Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip > > Trying to reach https://sagamusix.dehttps:// > sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip Look at the output above: there are two URLs glued together. You only want https://sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip but your attempt to remvoe the extra "https://sagamusix.de" > zipfile = line[24:] here > #Removes the last 4 characters to remove the .zip > zipfile2 = zipfile[:3] > print "Trying to reach " + ziplink is then ignored when you use the complete line below: > response = urllib2.urlopen(ziplink) Generally speaking your script is too complex. Start with something really simple, and later add error-handling as needed. Why would you prepend the site in the first place? Something like def download(ziplink, targetdirectory): filename = os.path.join(targetdirectory, posixpath.basename(ziplink)) print "downloading", ziplink print "--> ", filename urllib.urlretrieve(ziplink, filename) if __name__ == "__main__": targetdirectory = "test" os.mkdir(targetdirectory) page = urllib2.urlopen('https://sagamusix.de/en/samples/').read() soup = BeautifulSoup(page, "html5lib") for anchor in soup.findAll('a', href=True): link = anchor['href'] if link.endswith(".zip"): download(link, targetdirectory) should work and is perfectly fine for a throwaway script. Unrelated, but likely to trip you elsewhere: > if line.find('/'): >>> if "foo".find("/"): print "yes" ... yes >>> if "/foo".find("/"): print "yes" ... >>> i. e. str.find() is falsey only if the searched string begins with the search token. That's because find() returns the position (and 0 is a posibble position as indices in Python start with 0) and -1 (which is truthy) to indicate that the search token was not found. What you want is the `in` operator. >>> "/" in "foo" False >>> "/" in "/foo" True > We failed to reach a server.https://sagamusix.dehttps:// > sagamusix.de/other/Saga%20Musix%20-%20Colors%20of%20Synth1%20v1.0.zip > > Reason: [Errno 11001] getaddrinfo failed > bass.zip > > please help me to fix so that i acn download all the zip files > > code: > > import urllib2 > from urllib2 import Request, urlopen, URLError > #import urllib > import os > from bs4 import BeautifulSoup > # import socket > # socket.getaddrinfo('localhost', 8080) > > #Create a new directory to put the files into > #Get the current working directory and create a new directory in it named > #test > cwd = os.getcwd() > newdir = cwd +"\\test" > print "The current Working directory is " + cwd > os.mkdir( newdir); > print "Created new directory " + newdir > newfile = open('zipfiles.txt','w') > print newfile > > > print "Running script.. " > #Set variable for page to be open and url to be concatenated > url = "https://sagamusix.de" > page = urllib2.urlopen('https://sagamusix.de/en/samples/').read() > > #File extension to be looked for. > extension = ".zip" > > #Use BeautifulSoup to clean up the page > soup = BeautifulSoup(page, "html5lib") > soup.prettify() > > #Find all the links on the page that end in .zip > for anchor in soup.findAll('a', href=True): > links = url + anchor['href'] > if links.endswith(extension): > newfile.write(links + '\n') > newfile.close() > > #Read what is saved in zipfiles.txt and output it to the user > #This is done to create presistent data > newfile = open('zipfiles.txt', 'r') > for line in newfile: > print line + '/n' > newfile.close() > > #Read through the lines in the text file and download the zip files. > #Handle exceptions and print exceptions to the console > with open('zipfiles.txt', 'r') as url: > for line in url: > if line.find('/'): > print line.rsplit('/', 1)[1] > > try: > ziplink = line > #Removes the first 48 characters of the url to get the > name of the file > zipfile = line[24:] > #Removes the last 4 characters to remove the .zip > zipfile2 = zipfile[:3] > print "Trying to reach " + ziplink > response = urllib2.urlopen(ziplink) > except URLError as e: > > print 'We failed to reach a server.'+ziplink > if hasattr(e, 'reason'): > print 'Reason: ', e.reason > continue > elif hasattr(e, 'code'): > print 'The server couldnt fulfill the request.' > print 'Error code: ', e.code > continue > else: > zipcontent = response.read() > completeName = os.path.join(newdir, zipfile2+ ".zip") > with open (completeName, 'w') as f: > print "downloading.. " + zipfile > f.write(zipcontent) > f.close() > print "Script completed" > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From ps_python at yahoo.com Tue Oct 24 20:23:35 2017 From: ps_python at yahoo.com (kumar s) Date: Wed, 25 Oct 2017 00:23:35 +0000 (UTC) Subject: [Tutor] substitute using re.sub References: <996948972.3794110.1508891015538.ref@mail.yahoo.com> Message-ID: <996948972.3794110.1508891015538@mail.yahoo.com> Hi group,?I am trying to substitute in the following way and i cannot. Could you point out whats wrong in what i am doing.? >>> z'.|D' >>> re.sub(z,'1',z)'111' I just want only? '1' and not '111'.? I want:>>> re.sub(z,'1',z)'1' re.sub is repeatedly inserting 3 times because z has .|D . How can I substitute only 1.? ThanksKumar From __peter__ at web.de Wed Oct 25 04:55:16 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 25 Oct 2017 10:55:16 +0200 Subject: [Tutor] substitute using re.sub References: <996948972.3794110.1508891015538.ref@mail.yahoo.com> <996948972.3794110.1508891015538@mail.yahoo.com> Message-ID: kumar s via Tutor wrote: > Hi group, I am trying to substitute in the following way and i cannot. > Could you point out whats wrong in what i am doing. > >>>> z'.|D' >>>> re.sub(z,'1',z)'111' > I just want only '1' and not '111'. > I want:>>> re.sub(z,'1',z)'1' > re.sub is repeatedly inserting 3 times because z has .|D . How can I > substitute only 1. ThanksKumar Do you mean >>> import re >>> z = r".\D" >>> re.sub(re.escape(z), "1", z) '1' >>> re.sub(re.escape(z), "1", r"foo .\D bar .\D baz") 'foo 1 bar 1 baz' ? It works like this: >>> print(re.escape(z)) \.\\D i. e. re.escape() escapes the characters that have a special meaning in a regular expression, in your case the dot and the backslash. From alan.gauld at yahoo.co.uk Wed Oct 25 05:21:08 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 25 Oct 2017 10:21:08 +0100 Subject: [Tutor] substitute using re.sub In-Reply-To: <996948972.3794110.1508891015538@mail.yahoo.com> References: <996948972.3794110.1508891015538.ref@mail.yahoo.com> <996948972.3794110.1508891015538@mail.yahoo.com> Message-ID: On 25/10/17 01:23, kumar s via Tutor wrote: > Hi group,?I am trying to substitute in the following way and i cannot. C > >>>> z'.|D' >>>> re.sub(z,'1',z)'111' I don't really know what you are doing here. It looks like the mail system may have stripped some characters, Did you post in plain text? Also, in the call to sub() you have z twice which suggests you are trying to replace the pattern itself, is that correct? If so then the dot will match each character in the pattern and so you will get 111. If you want to match the literal . then you need to escape it. re.sub('\.|D','1','.|D') But that still matches the D too. To match the literal string you need to escape the | as well: re.sub('\.\|D),'1','.|D') At least that's what I think you want? But in that case a simple string replacement would be easier. '.|D'.replace('.|D','1') -- 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 From skgoyal721 at gmail.com Wed Oct 25 14:24:43 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Wed, 25 Oct 2017 23:54:43 +0530 Subject: [Tutor] Not able to import Tensorflow in python script Message-ID: Hello all, I was trying to run this check script of tensorflow after installing the tensorflow successfully. but it is not able to import tensorflow properly i think. but sometimes in python shell it gets imported. it is giving the error "can't import name template" script import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello)) errors: [0, 1, 2, 3, 4] at 0x7fc0298652b0> 0 1 2 3 4 Traceback (most recent call last): File "a.py", line 1, in import tensorflow as tf File "/home/goyal/tensorflow/lib/python3.5/site-packages/tensorflow/__init__.py", line 24, in from tensorflow.python import * File "/home/goyal/tensorflow/lib/python3.5/site-packages/tensorflow/python/__init__.py", line 47, in import numpy as np File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/__init__.py", line 142, in from . import add_newdocs File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/add_newdocs.py", line 13, in from numpy.lib import add_newdoc File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/lib/__init__.py", line 8, in from .type_check import * File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/lib/type_check.py", line 11, in import numpy.core.numeric as _nx File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/core/__init__.py", line 74, in from numpy.testing.nosetester import _numpy_tester File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/testing/__init__.py", line 10, in from unittest import TestCase File "/usr/lib/python3.5/unittest/__init__.py", line 59, in from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf, File "/usr/lib/python3.5/unittest/case.py", line 6, in import logging File "/usr/lib/python3.5/logging/__init__.py", line 28, in from string import Template ImportError: cannot import name 'Template' Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in from apport.report import Report File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in from urllib.request import urlopen File "/usr/lib/python3.5/urllib/request.py", line 88, in import http.client File "/usr/lib/python3.5/http/client.py", line 71, in import email.parser File "/usr/lib/python3.5/email/parser.py", line 12, in from email.feedparser import FeedParser, BytesFeedParser File "/usr/lib/python3.5/email/feedparser.py", line 27, in from email import message File "/usr/lib/python3.5/email/message.py", line 16, in from email import utils File "/usr/lib/python3.5/email/utils.py", line 40, in from email.charset import Charset File "/usr/lib/python3.5/email/charset.py", line 15, in import email.quoprimime File "/usr/lib/python3.5/email/quoprimime.py", line 44, in from string import ascii_letters, digits, hexdigits ImportError: cannot import name 'ascii_letters' Original exception was: Traceback (most recent call last): File "a.py", line 1, in import tensorflow as tf File "/home/goyal/tensorflow/lib/python3.5/site-packages/tensorflow/__init__.py", line 24, in from tensorflow.python import * File "/home/goyal/tensorflow/lib/python3.5/site-packages/tensorflow/python/__init__.py", line 47, in import numpy as np File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/__init__.py", line 142, in from . import add_newdocs File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/add_newdocs.py", line 13, in from numpy.lib import add_newdoc File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/lib/__init__.py", line 8, in from .type_check import * File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/lib/type_check.py", line 11, in import numpy.core.numeric as _nx File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/core/__init__.py", line 74, in from numpy.testing.nosetester import _numpy_tester File "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/testing/__init__.py", line 10, in from unittest import TestCase File "/usr/lib/python3.5/unittest/__init__.py", line 59, in from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf, File "/usr/lib/python3.5/unittest/case.py", line 6, in import logging File "/usr/lib/python3.5/logging/__init__.py", line 28, in from string import Template ImportError: cannot import name 'Template' please tell me what to do. From alan.gauld at yahoo.co.uk Wed Oct 25 16:27:39 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 25 Oct 2017 21:27:39 +0100 Subject: [Tutor] Not able to import Tensorflow in python script In-Reply-To: References: Message-ID: On 25/10/17 19:24, shubham goyal wrote: > I was trying to run this check script of tensorflow after installing the > tensorflow successfully. Its not part of the standard library so I'll need to assume you are correct and it installed correctly. However from some of the errors below it looks as if you \also need numpy/SciPy installed - have you done that too? If so then I hope somebody else can assist. You might find a tensorflow help forum or email address you can ask too? > "/home/goyal/tensorflow/lib/python3.5/site-packages/tensorflow/python/__init__.py", > line 47, in > import numpy as np > File > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/__init__.py", > line 142, in > from . import add_newdocs > File > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/add_newdocs.py", > line 13, in > from numpy.lib import add_newdoc > File > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/lib/__init__.py", > line 8, in > from .type_check import * > File > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/lib/type_check.py", > line 11, in > import numpy.core.numeric as _nx > File > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/core/__init__.py", > line 74, in > from numpy.testing.nosetester import _numpy_tester > File > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/testing/__init__.py", > line 10, in > from unittest import TestCase > File "/usr/lib/python3.5/unittest/__init__.py", line 59, in > from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf, > File "/usr/lib/python3.5/unittest/case.py", line 6, in > import logging > File "/usr/lib/python3.5/logging/__init__.py", line 28, in > from string import Template > ImportError: cannot import name 'Template' Do you have the right version of numpy for your module? -- 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 From __peter__ at web.de Wed Oct 25 16:55:57 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 25 Oct 2017 22:55:57 +0200 Subject: [Tutor] Not able to import Tensorflow in python script References: Message-ID: shubham goyal wrote: > Hello all, > I was trying to run this check script of tensorflow after installing the > tensorflow successfully. but it is not able to import tensorflow properly > i think. but sometimes in python > shell it gets imported. it is giving the error "can't import name > template" > import logging > File "/usr/lib/python3.5/logging/__init__.py", line 28, in > from string import Template > ImportError: cannot import name 'Template' Did you write a module called string.py yourself? If so, your module may be imported instead of the one in the standard library. Rename your module to mystring, say, and the error should go away. From skgoyal721 at gmail.com Wed Oct 25 16:32:46 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Thu, 26 Oct 2017 02:02:46 +0530 Subject: [Tutor] Not able to import Tensorflow in python script In-Reply-To: References: Message-ID: Hey Alan, I found this issue on internet. It turns out that if you have some file named string.py in the same folder as script which is using tensorflow, it give this kind of error. I don't know why but this was happening. Now it's working . Thank you for your help. On Oct 26, 2017 1:58 AM, "Alan Gauld via Tutor" wrote: > On 25/10/17 19:24, shubham goyal wrote: > > I was trying to run this check script of tensorflow after installing the > > tensorflow successfully. > > Its not part of the standard library so I'll need to assume you > are correct and it installed correctly. However from some of the > errors below it looks as if you \also need numpy/SciPy installed > - have you done that too? > > If so then I hope somebody else can assist. > > You might find a tensorflow help forum or email address you > can ask too? > > > > "/home/goyal/tensorflow/lib/python3.5/site-packages/ > tensorflow/python/__init__.py", > > line 47, in > > import numpy as np > > File > > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/__init__.py", > > line 142, in > > from . import add_newdocs > > File > > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/ > add_newdocs.py", > > line 13, in > > from numpy.lib import add_newdoc > > File > > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/ > lib/__init__.py", > > line 8, in > > from .type_check import * > > File > > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/ > lib/type_check.py", > > line 11, in > > import numpy.core.numeric as _nx > > File > > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/ > core/__init__.py", > > line 74, in > > from numpy.testing.nosetester import _numpy_tester > > File > > "/home/goyal/tensorflow/lib/python3.5/site-packages/numpy/ > testing/__init__.py", > > line 10, in > > from unittest import TestCase > > File "/usr/lib/python3.5/unittest/__init__.py", line 59, in > > from .case import (TestCase, FunctionTestCase, SkipTest, skip, > skipIf, > > File "/usr/lib/python3.5/unittest/case.py", line 6, in > > import logging > > File "/usr/lib/python3.5/logging/__init__.py", line 28, in > > from string import Template > > ImportError: cannot import name 'Template' > > Do you have the right version of numpy for your module? > > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From anrkris at gmail.com Thu Oct 26 16:02:34 2017 From: anrkris at gmail.com (Chris Coleman) Date: Thu, 26 Oct 2017 16:02:34 -0400 Subject: [Tutor] problem with program in python in easy steps Message-ID: i wrote these programs and saved them per instructions on page 128 and 129 in the book "python in easy steps". class Person: '''A base class to define Person properties.''' def__init__(self,name): self.name = name def speak( self,msg = '(Calling The Base Class)'): print(self.name,msg) from Person import* '''A derived class to define Man properties.''' class Man(Person): def speak(self,msg): print(self.name,':\n\tHello!',msg) from Person import* '''A derived class to define Hombre properties.''' class Hombre(Person): def speak(self,msg): print(self.name,':\n\tHola!',msg) from Man import* from Hombre import* guy_1 = Man('Richard') guy_2 = Hombre('Ricardo') guy_1.speak('It\'s a beautiful evening.\n') guy_2.speak('Es una tarde hermosa.\n') Person.speak(guy_1) Person.speak(guy_2) i ran the program override.py and get this error message: Traceback (most recent call last): File "scripts/override.py", line 1, in from Man import* File "/home/chris/scripts/Man.py", line 2 '''A derived class to define Man properties.''' ^ IndentationError: unexpected indent From alan.gauld at yahoo.co.uk Thu Oct 26 20:04:45 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 27 Oct 2017 01:04:45 +0100 Subject: [Tutor] problem with program in python in easy steps In-Reply-To: References: Message-ID: On 26/10/17 21:02, Chris Coleman wrote: > i wrote these programs and saved them per instructions on page 128 and 129 > in the book "python in easy steps". I don't know the book but... > > class Person: > '''A base class to define Person properties.''' > def__init__(self,name): > self.name = name > def speak( self,msg = '(Calling The Base Class)'): > print(self.name,msg) > I assume the book suggests storing these class definitions in separate files named after the classes? (A small point is that usual style is to name modules in all lower case) > from Person import* > '''A derived class to define Man properties.''' The string should start at the beginning of the line - it is not inside a new block so should not be indented. I assume the book recommends the from x import * style but thats considered bad practice. Only import the names you need or, if there are many, import just the module name (possibly with an alias). Like so: from modulename import name1, name2,etc... import modulename import modulename as alias The import * style can lead to weird bugs due to name collisions. > class Man(Person): > def speak(self,msg): > print(self.name,':\n\tHello!',msg) > > from Person import* > '''A derived class to define Hombre properties.''' Same problem with the string definition. > class Hombre(Person): > def speak(self,msg): > print(self.name,':\n\tHola!',msg) > > from Man import* > from Hombre import* > guy_1 = Man('Richard') > guy_2 = Hombre('Ricardo') > guy_1.speak('It\'s a beautiful evening.\n') > guy_2.speak('Es una tarde hermosa.\n') > Person.speak(guy_1) > Person.speak(guy_2) > > i ran the program override.py and get this error message: > > Traceback (most recent call last): > File "scripts/override.py", line 1, in > from Man import* > File "/home/chris/scripts/Man.py", line 2 > '''A derived class to define Man properties.''' > ^ > IndentationError: unexpected indent And this is telling you not to indent your string. -- 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 From robertvstepp at gmail.com Thu Oct 26 20:11:37 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 26 Oct 2017 19:11:37 -0500 Subject: [Tutor] problem with program in python in easy steps In-Reply-To: References: Message-ID: On Thu, Oct 26, 2017 at 3:02 PM, Chris Coleman wrote: > > i wrote these programs and saved them per instructions on page 128 and 129 > in the book "python in easy steps". > > class Person: > '''A base class to define Person properties.''' > def__init__(self,name): The above line should generate an error as there is no space between "def" and "__init__". > self.name = name > def speak( self,msg = '(Calling The Base Class)'): > print(self.name,msg) > > from Person import* > '''A derived class to define Man properties.''' > class Man(Person): > def speak(self,msg): > print(self.name,':\n\tHello!',msg) > > from Person import* > '''A derived class to define Hombre properties.''' > class Hombre(Person): > def speak(self,msg): > print(self.name,':\n\tHola!',msg) > > from Man import* > from Hombre import* > guy_1 = Man('Richard') > guy_2 = Hombre('Ricardo') > guy_1.speak('It\'s a beautiful evening.\n') > guy_2.speak('Es una tarde hermosa.\n') > Person.speak(guy_1) > Person.speak(guy_2) > > i ran the program override.py and get this error message: > > Traceback (most recent call last): > File "scripts/override.py", line 1, in > from Man import* > File "/home/chris/scripts/Man.py", line 2 > '''A derived class to define Man properties.''' > ^ > IndentationError: unexpected indent So, did you try removing the indentation in front of '''A derived class ... '''? And once you do, you will get the same error later on where you did the same thing. Recall that Python uses indentation to define its code blocks. -- boB From emylistsddg at gmail.com Sat Oct 28 01:10:12 2017 From: emylistsddg at gmail.com (eMyListsDDg) Date: Fri, 27 Oct 2017 22:10:12 -0700 Subject: [Tutor] howto install a "curses" lib on win7 Message-ID: <18610529369.20171027221012@gmail.com> on a win7 system with python ver=2.7.14, a python script i'm trying to utilize returns : #----------------------------------------------------------------- Traceback (most recent call last): File "salamandra.py", line 16, in import curses File "c:\python27\lib\curses\__init__.py", line 15, in from _curses import * ImportError: No module named _curses #----------------------------------------------------------------- if this machine was running linux probably wouldn't be an issue, but on Win7 how can i get a 'curses' lib installed so this script runs? tia From alan.gauld at yahoo.co.uk Sat Oct 28 03:39:35 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 28 Oct 2017 08:39:35 +0100 Subject: [Tutor] howto install a "curses" lib on win7 In-Reply-To: <18610529369.20171027221012@gmail.com> References: <18610529369.20171027221012@gmail.com> Message-ID: On 28/10/17 06:10, eMyListsDDg wrote: > on Win7 how can i get a 'curses' lib installed so this script runs? Do a Google search for windows curses, there are at least a couple of options. I've no experience of how well they work however because... ...as a last resort, install cygwin and use the Python that comes with cygwin which includes a curses module that works with the cygwin bash terminal. But cygwin is big and probably a sledgehammer for your nut if all you want is curses. -- 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 From emylistsddg at gmail.com Sat Oct 28 14:35:30 2017 From: emylistsddg at gmail.com (eMyListsDDg) Date: Sat, 28 Oct 2017 11:35:30 -0700 Subject: [Tutor] howto install a "curses" lib on win7 In-Reply-To: References: <18610529369.20171027221012@gmail.com> Message-ID: <946584729.20171028113530@gmail.com> i had found this one "curses-2.2-cp27-none-win_amd64.whl" at www.lfd.uci.edu/~gohlke/pythonlibs/#curses , used pip to install. earlier pip attempts failed, but that was me/user typo error i realized after your reply. got the syntax correct for pip and the .whl installed. i put link above incase another user has same issue, thanks for the help, the script runs and didn't need cygwin thankfully. > On 28/10/17 06:10, eMyListsDDg wrote: >> on Win7 how can i get a 'curses' lib installed so this script runs? > Do a Google search for windows curses, there are at least > a couple of options. I've no experience of how well they > work however because... > ...as a last resort, install cygwin and use the > Python that comes with cygwin which includes a curses module > that works with the cygwin bash terminal. But cygwin is big > and probably a sledgehammer for your nut if all you want is > curses. From mcanderson12 at gmail.com Sat Oct 28 16:43:54 2017 From: mcanderson12 at gmail.com (Mark Anderson) Date: Sat, 28 Oct 2017 21:43:54 +0100 Subject: [Tutor] confused about Pypi Message-ID: <59f4ec09.90921c0a.73550.44e7@mx.google.com> Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. Can anyone help please Regards Mark Anderson Sent from Mail for Windows 10 From mats at wichmann.us Sat Oct 28 18:15:19 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 28 Oct 2017 16:15:19 -0600 Subject: [Tutor] confused about Pypi In-Reply-To: <59f4ec09.90921c0a.73550.44e7@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> Message-ID: <03f85989-22e9-5f7d-93cf-1ee8d238be3b@wichmann.us> On 10/28/2017 02:43 PM, Mark Anderson wrote: > Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. > Can anyone help please > Regards > Mark Anderson Snide answer: "don't use Windows, it's a terrible platform for programmers". If the problem is you can run Python from cmd.exe (or Powershell), but you can't run "python -m pip ...", it's because pip ends up in a strange place sometimes: the Scripts subdirectory of the Python installation, and this location may not be in your path or in the Python module path. This may need to be added. Have a hunt on the internet about this, there should be some resources that help. If pip is found (possibly due to scripts dir in path), then you should just be able to run "pip install " without including the "python -m" part. On a system here with Python 3.6, {my home directory}/Local/Programs/Python/Python36-32/Scripts/pip.exe is where pip lives. If that's not the right problem, then still hunting on the internet should help, just about every conceivable problem on Windows has been encountered and solved. And there are a lot of them, sadly. From breamoreboy at gmail.com Sat Oct 28 18:07:46 2017 From: breamoreboy at gmail.com (Mark Lawrence) Date: Sat, 28 Oct 2017 23:07:46 +0100 Subject: [Tutor] confused about Pypi In-Reply-To: <59f4ec09.90921c0a.73550.44e7@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> Message-ID: On 28/10/17 21:43, Mark Anderson wrote: > Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. > Can anyone help please > Regards > Mark Anderson > Please read https://docs.python.org/3/using/windows.html, especially sections 3.3 and 3.4, and possibly http://pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From cs at cskk.id.au Sat Oct 28 19:14:42 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 29 Oct 2017 10:14:42 +1100 Subject: [Tutor] confused about Pypi In-Reply-To: <20171028231300.GA43571@cskk.homeip.net> References: <20171028231300.GA43571@cskk.homeip.net> Message-ID: <20171028231442.GA46247@cskk.homeip.net> On 29Oct2017 10:13, Cameron Simpson wrote: >On 28Oct2017 21:43, Mark Anderson wrote: >>Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. >>Can anyone help please >>Regards >>Mark Anderson > >In addition to the other two responses, please cut/paste the text from >your command prompt showing the prompt, your command and the complete error >message. Oh, I should add that I really mean the text. Not a screenshot - images are routinely stripped from this mailing list, and anyway are less useful that the plain text inserted directly into your message. Thanks, Cameron Simpson (formerly cs at zip.com.au) From cs at cskk.id.au Sat Oct 28 19:13:00 2017 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 29 Oct 2017 10:13:00 +1100 Subject: [Tutor] confused about Pypi In-Reply-To: <59f4ec09.90921c0a.73550.44e7@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> Message-ID: <20171028231300.GA43571@cskk.homeip.net> On 28Oct2017 21:43, Mark Anderson wrote: >Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. >Can anyone help please >Regards >Mark Anderson In addition to the other two responses, please cut/paste the text from your command prompt showing the prompt, your command and the complete error message. Cheers, Cameron Simpson (formerly cs at zip.com.au) From alan.gauld at yahoo.co.uk Sat Oct 28 19:50:53 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 29 Oct 2017 00:50:53 +0100 Subject: [Tutor] confused about Pypi In-Reply-To: <59f4ec09.90921c0a.73550.44e7@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> Message-ID: On 28/10/17 21:43, Mark Anderson wrote: > As I understand it I have to write certain text into > the command prompt Please note that this is probably the OS command prompt (a CMD window in windows - C:\WINDOWS> or similar) not the Python interpreter (which looks like >>>) > It tells me that python -m is not recognised. Always cut 'n paste the full error text, don't just try to describe it. We need very precise details of what exactly it looks like. 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 From mcanderson12 at gmail.com Sun Oct 29 03:57:33 2017 From: mcanderson12 at gmail.com (Mark Anderson) Date: Sun, 29 Oct 2017 07:57:33 +0000 Subject: [Tutor] confused about Pypi In-Reply-To: <20171028231300.GA43571@cskk.homeip.net> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> <20171028231300.GA43571@cskk.homeip.net> Message-ID: <59f589eb.8faedf0a.c6fed.591e@mx.google.com> Hi In short I am trying to download a third party package that is recommended for a program I am writing on the course. The text is C:\Users\marka> python -m pip "Pyglet" 'python' is not recognized as an internal or external command, operable program or batch file. Hoping you can help Mark Sent from Mail for Windows 10 From: Cameron Simpson Sent: 29 October 2017 00:44 To: Mark Anderson Cc: tutor at python.org Subject: Re: [Tutor] confused about Pypi On 28Oct2017 21:43, Mark Anderson wrote: >Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. >Can anyone help please >Regards >Mark Anderson In addition to the other two responses, please cut/paste the text from your command prompt showing the prompt, your command and the complete error message. Cheers, Cameron Simpson (formerly cs at zip.com.au) From mcanderson12 at gmail.com Sun Oct 29 04:05:01 2017 From: mcanderson12 at gmail.com (Mark Anderson) Date: Sun, 29 Oct 2017 08:05:01 +0000 Subject: [Tutor] FW: confused about Pypi In-Reply-To: <59f589eb.8faedf0a.c6fed.591e@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> <20171028231300.GA43571@cskk.homeip.net> <59f589eb.8faedf0a.c6fed.591e@mx.google.com> Message-ID: <59f58bac.8fd6500a.7ed3c.5101@mx.google.com> Apologies I missed a word out (added below) but error message the same anyway Sent from Mail for Windows 10 From: Mark Anderson Sent: 29 October 2017 07:57 To: tutor at python.org Subject: RE: [Tutor] confused about Pypi Hi In short I am trying to download a third party package that is recommended for a program I am writing on the course. The text is C:\Users\marka> python -m pip install "pyglet" 'python' is not recognized as an internal or external command, operable program or batch file. Hoping you can help Mark Sent from Mail for Windows 10 From: Cameron Simpson Sent: 29 October 2017 00:44 To: Mark Anderson Cc: tutor at python.org Subject: Re: [Tutor] confused about Pypi On 28Oct2017 21:43, Mark Anderson wrote: >Hello, I am currently doing an online course to learn Python. Generally ive followed it well and am enjoying my first go at programming. However the description of how to get modules from PyPi has left me confused. Ive only recently downloaded python so it already has pip installed. As I understand it I have to write certain text into the command prompt (I?m using windows 10). It tells me that python -m is not recognised. >Can anyone help please >Regards >Mark Anderson In addition to the other two responses, please cut/paste the text from your command prompt showing the prompt, your command and the complete error message. Cheers, Cameron Simpson (formerly cs at zip.com.au) From akishorecert at gmail.com Sun Oct 29 09:16:15 2017 From: akishorecert at gmail.com (Kishore Kumar Alajangi) Date: Sun, 29 Oct 2017 18:46:15 +0530 Subject: [Tutor] Listing link urls In-Reply-To: References: Message-ID: + tutor On Sun, Oct 29, 2017 at 6:57 AM, Kishore Kumar Alajangi < akishorecert at gmail.com> wrote: > Hi, > > I am facing an issue with listing specific urls inside web page, > > https://economictimes.indiatimes.com/archive.cms > > Page contains link urls by year and month vise, > Ex: /archive/year-2001,month-1.cms > > I am able to list all required urls using the below code, > > from bs4 import BeautifulSoup > import re, csv > import urllib.request > import scrapy > req = urllib.request.Request('http://economictimes.indiatimes.com/archive.cms', headers={'User-Agent': 'Mozilla/5.0'}) > > > links = [] > totalPosts = [] > url = "http://economictimes.indiatimes.com" > data = urllib.request.urlopen(req).read() > page = BeautifulSoup(data,'html.parser') > > for link in page.findAll('a', href = re.compile('^/archive/')): //retrieving urls starts with "archive" > l = link.get('href') > links.append(url+l) > > > with open("output.txt", "a") as f: > for post in links: > post = post + '\n' > f.write(post) > > *sample result in text file:* > > http://economictimes.indiatimes.com/archive/year-2001,month-1.cmshttp://economictimes.indiatimes.com/archive/year-2001,month-2.cmshttp://economictimes.indiatimes.com/archive/year-2001,month-3.cmshttp://economictimes.indiatimes.com/archive/year-2001,month-4.cmshttp://economictimes.indiatimes.com/archive/year-2001,month-5.cmshttp://economictimes.indiatimes.com/archive/year-2001,month-6.cms > > > List of urls I am storing in a text file, From the month urls I want to retrieve day urls starts with "/archivelist", I am using > > the below code, but I am not getting any result, If I check with inspect element the urls are available starting with /archivelist, > > > > Kindly help me where I am doing wrong. > > from bs4 import BeautifulSoup > import re, csv > import urllib.request > import scrapy > > file = open("output.txt", "r") > > > for i in file: > > urls = urllib.request.Request(i, headers={'User-Agent': 'Mozilla/5.0'}) > > data1 = urllib.request.urlopen(urls).read() > > page1 = BeautifulSoup(data1, 'html.parser') > > for link1 in page1.findAll(href = re.compile('^/archivelist/')): > > l1 = link1.get('href') > > print(l1) > > > Thanks, > > Kishore. > > > > > > From robertvstepp at gmail.com Sun Oct 29 15:44:55 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 29 Oct 2017 14:44:55 -0500 Subject: [Tutor] FW: confused about Pypi In-Reply-To: <59f58bac.8fd6500a.7ed3c.5101@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> <20171028231300.GA43571@cskk.homeip.net> <59f589eb.8faedf0a.c6fed.591e@mx.google.com> <59f58bac.8fd6500a.7ed3c.5101@mx.google.com> Message-ID: On Sun, Oct 29, 2017 at 3:05 AM, Mark Anderson wrote: > The text is > > C:\Users\marka> python -m pip install "pyglet" > 'python' is not recognized as an internal or external command, > operable program or batch file. I'm on Windows 7, but I have found when I have not bothered to set the path to the Python installation, that using the Python launcher as follows works: py -m pip install "pyglet" Perhaps it will work for you. BTW, are the quotes around pyglet needed? I don't recall ever having to use quotes to install via pip. HTH, boB From marc.tompkins at gmail.com Sun Oct 29 20:39:23 2017 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sun, 29 Oct 2017 17:39:23 -0700 Subject: [Tutor] confused about Pypi In-Reply-To: <59f4ec09.90921c0a.73550.44e7@mx.google.com> References: <59f4ec09.90921c0a.73550.44e7@mx.google.com> Message-ID: On Sat, Oct 28, 2017 at 1:43 PM, Mark Anderson wrote: > Hello, I am currently doing an online course to learn Python. Generally > ive followed it well and am enjoying my first go at programming. However > the description of how to get modules from PyPi has left me confused. Ive > only recently downloaded python so it already has pip installed. As I > understand it I have to write certain text into the command prompt (I?m > using windows 10). It tells me that python -m is not recognised. > Can anyone help please > I strongly recommend the excellent "pip-Win" utility. It's a very small download and a very quick install; it figures out your various virtual environment(s) and makes everything incredibly easy. I feel like I'm cheating by using it, frankly, but the fact is that I like my tools to work for ME, not the other way around; I came here to use Python, not fight with command-line syntax. https://sites.google.com/site/pydatalog/python/pip-for-windows From voipdev.sourav at gmail.com Mon Oct 30 06:34:28 2017 From: voipdev.sourav at gmail.com (sourav voip) Date: Mon, 30 Oct 2017 16:04:28 +0530 Subject: [Tutor] request.post in If condition Message-ID: Hi All, I'm trying to hit request.post with condition using if-else as below... I;m posting the full script here...as I've tried declaring post url details tested with multiple places. From voipdev.sourav at gmail.com Mon Oct 30 06:42:27 2017 From: voipdev.sourav at gmail.com (sourav voip) Date: Mon, 30 Oct 2017 16:12:27 +0530 Subject: [Tutor] request.post in If condition In-Reply-To: References: Message-ID: Hi All, I'm trying to hit request.post with condition using if-else as below... I;m posting the full script here...as I've tried declaring post url details tested with multiple places.. If condition for disk utiliztion is working perfect ,however request.post is not hitting. Please suggest any hint/clue as I'm a new learner in python. Regards, Sourav ------------------------------------------------------------ ------------------------- #!/usr/bin/python import re,sys,commands import requests # Set the request parameters url = 'https://devxxxx.service-now.com/api/now/table/incident' # Eg. User name="admin", Password="admin" for this code sample. user = 'admin' pwd = 'xxxx' # Set proper headers headers = {"Content-Type":"application/json","Accept":"application/json"} ################# #Set variables command = "df /" critical = 50.0 warning = 40.0 ################# #build regex dfPattern = re.compile('[0-9]+') #get disk utilization diskUtil = commands.getstatusoutput(command) #split out the util % diskUtil = diskUtil[1].split()[11] #look for a match. If no match exit and return an #UNKNOWN (3) state to Nagios matchobj = dfPattern.match(diskUtil) if (matchobj): diskUtil = eval(matchobj.group(0)) else: print "STATE UNKNOWN" sys.exit(3) #Determine state to pass to Nagios #CRITICAL = 2 #WARNING = 1 #OK = 0 if diskUtil >= critical: url = 'https://devxxxx.service-now.com/api/now/table/incident' user = 'admin' pwd = 'xxxx' headers = {"Content-Type":"application/json","Accept":"application/ json"} requests.post(url, auth=(user, pwd), headers=headers ,data="{\"assignment_group\":\Hardware\",\"short_description\":\"Threshold critical\"}") print "FREE SPACE CRITICAL: '/' is %.2f%% full" % (float(diskUtil)) sys.exit(2) elif diskUtil >= warning: requests.post(url, auth=(user, pwd), headers=headers ,data="{\"assignment_group\":\Hardware\",\"short_description\":\"Threshold Warning\"}") print "FREE SPACE WARNING: '/' is %.2f%% full" % (float(diskUtil)) sys.exit(1) else: print "FREE SPACE OK: '/' is %.2f%% full" % (float(diskUtil)) sys.exit(0) On Mon, Oct 30, 2017 at 4:04 PM, sourav voip wrote: > Hi All, > > I'm trying to hit request.post with condition using if-else as below... > I;m posting the full script here...as I've tried declaring post url > details tested with multiple places. > > > > > From Ian at AlchemyTransformation.net Mon Oct 30 17:02:54 2017 From: Ian at AlchemyTransformation.net (Alchemy) Date: Mon, 30 Oct 2017 17:02:54 -0400 Subject: [Tutor] Help with python 2.7 Message-ID: <7345737E-DA15-47C8-AE3B-2AC70BC2005B@AlchemyTransformation.net> Hello, I am in the midst of a career change from renovations to computers. I?m used to working conceptually and understand coding is that. I am taking an online opencourseare classfrom MIT ?Introduction to computer science and programming?. The class uses python 2.7, has video lectures and homework assignments that are coding exercises. Python seems like the root to everything in the computers possabilities and I want to internalize it. I?m stuck on the homework ,butmore importantly stuck on the concepts behind the homework. Can your orginazation help? gratefully, Ian From alan.gauld at yahoo.co.uk Mon Oct 30 19:43:32 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 30 Oct 2017 23:43:32 +0000 Subject: [Tutor] Help with python 2.7 In-Reply-To: <7345737E-DA15-47C8-AE3B-2AC70BC2005B@AlchemyTransformation.net> References: <7345737E-DA15-47C8-AE3B-2AC70BC2005B@AlchemyTransformation.net> Message-ID: On 30/10/17 21:02, Alchemy wrote: > I?m stuck on the homework ,butmore importantly stuck on the concepts behind the homework. > > Can your orginazation help? Yes, we are happy to help with homework although we won't usually give you the full answer. We will make suggestions and are happy to review or comment on code. To make life easier: Always post any code in plain text (email messes up the formatting of html/rtf) And put it inline not as an attachment. Always post the full text of any error messages. Mention the OS and Python versions. -- 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 From shelzmike at gmail.com Mon Oct 30 19:44:33 2017 From: shelzmike at gmail.com (Mike Miller) Date: Mon, 30 Oct 2017 23:44:33 +0000 Subject: [Tutor] Help with python 2.7 In-Reply-To: <7345737E-DA15-47C8-AE3B-2AC70BC2005B@AlchemyTransformation.net> References: <7345737E-DA15-47C8-AE3B-2AC70BC2005B@AlchemyTransformation.net> Message-ID: This is a mailing list, so the best way to get help is to send a request with code examples and most importantly what you have already tried. That being said, there are some smart people on here, much more experienced than I am on Python so I would say you can get help with any problem you may have. I have taken the MIT course you are talking about and it is quite challenging (perhaps a little more than it should be) and my oldest son at one point took the Microsoft MVA course on the subject of python and he said it was very good (surprisingly enough)..he is a Java programmer primarily and said that for someone with even average skill and ability, the MS course is actually great and easy to follow on. Good luck, and welcome. Mike On Mon, Oct 30, 2017, 7:24 PM Alchemy wrote: > Hello, > > I am in the midst of a career change from renovations to computers. > I?m used to working conceptually and understand coding is that. > I am taking an online opencourseare classfrom MIT ?Introduction to > computer science and programming?. > The class uses python 2.7, has video lectures and homework assignments > that are coding exercises. > Python seems like the root to everything in the computers possabilities > and I want to internalize it. > I?m stuck on the homework ,butmore importantly stuck on the concepts > behind the homework. > > Can your orginazation help? > > gratefully, > > Ian > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From pcpanchal123 at gmail.com Tue Oct 31 15:30:54 2017 From: pcpanchal123 at gmail.com (Pareshkumar Panchal) Date: Tue, 31 Oct 2017 15:30:54 -0400 Subject: [Tutor] How to schedule the SFTP folder sync using python Message-ID: Hi, can you help me about scheduling the folder sync between sftp and local directory? pysftp library is good or you recommend different library? thank you From shelzmike at gmail.com Mon Oct 30 19:48:51 2017 From: shelzmike at gmail.com (Mike Miller) Date: Mon, 30 Oct 2017 23:48:51 +0000 Subject: [Tutor] request.post in If condition In-Reply-To: References: Message-ID: What do you mean when you say it is not hitting? Is there a specific error, or are you saying it simply isn't posting to your site? Mike On Mon, Oct 30, 2017, 8:21 AM sourav voip wrote: > Hi All, > > I'm trying to hit request.post with condition using if-else as below... > I;m posting the full script here...as I've tried declaring post url details > tested with multiple places.. > > If condition for disk utiliztion is working perfect ,however request.post > is not hitting. > Please suggest any hint/clue as I'm a new learner in python. > > Regards, > Sourav > > ------------------------------------------------------------ > ------------------------- > #!/usr/bin/python > import re,sys,commands > import requests > # Set the request parameters > url = 'https://devxxxx.service-now.com/api/now/table/incident' > > # Eg. User name="admin", Password="admin" for this code sample. > user = 'admin' > pwd = 'xxxx' > > # Set proper headers > headers = {"Content-Type":"application/json","Accept":"application/json"} > > > ################# > #Set variables > command = "df /" > critical = 50.0 > warning = 40.0 > ################# > > #build regex > dfPattern = re.compile('[0-9]+') > > #get disk utilization > diskUtil = commands.getstatusoutput(command) > > #split out the util % > diskUtil = diskUtil[1].split()[11] > > #look for a match. If no match exit and return an > #UNKNOWN (3) state to Nagios > > matchobj = dfPattern.match(diskUtil) > if (matchobj): > diskUtil = eval(matchobj.group(0)) > else: > print "STATE UNKNOWN" > sys.exit(3) > > #Determine state to pass to Nagios > #CRITICAL = 2 > #WARNING = 1 > #OK = 0 > if diskUtil >= critical: > url = 'https://devxxxx.service-now.com/api/now/table/incident' > user = 'admin' > pwd = 'xxxx' > headers = {"Content-Type":"application/json","Accept":"application/ > json"} > requests.post(url, auth=(user, pwd), headers=headers > ,data="{\"assignment_group\":\Hardware\",\"short_description\":\"Threshold > critical\"}") > print "FREE SPACE CRITICAL: '/' is %.2f%% full" % (float(diskUtil)) > sys.exit(2) > elif diskUtil >= warning: > requests.post(url, auth=(user, pwd), headers=headers > ,data="{\"assignment_group\":\Hardware\",\"short_description\":\"Threshold > Warning\"}") > print "FREE SPACE WARNING: '/' is %.2f%% full" % (float(diskUtil)) > sys.exit(1) > else: > print "FREE SPACE OK: '/' is %.2f%% full" % (float(diskUtil)) > sys.exit(0) > > > On Mon, Oct 30, 2017 at 4:04 PM, sourav voip > wrote: > > > Hi All, > > > > I'm trying to hit request.post with condition using if-else as below... > > I;m posting the full script here...as I've tried declaring post url > > details tested with multiple places. > > > > > > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor >