[Tutor] hashlib problems
Im wanting to use the builtin hashlib functions to encrypt passwords before storing them in a database. According to documentation on the python.org site it should be as simple as import hashlib hashname = hashlib.sha234 (or whatever other hash method you want like md5 or whatever) hashname.update(b"test") the b is for byte since the documentation states that hashlib does not accept strings hashname.hexdigest() or digest() will give you the hash encrypted value. (hexdigest will encode the output in hex for use in email and string db storage) My problem is that hashname.update results in a: AttributeError: 'built_in_function_or_method' object has no attribute 'update' I get this error in python 2 and python 3 I get it on multiple operating systems, so I dont understand whats going wrong. Either there is a bug in the module hashlib, or the module changed and the docs don't keep up. Either way I can not encrypt the password of my project for new users till I figure out whats going on. Your help much appreciated. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hashlib problems
On Fri, Sep 10, 2010 at 3:12 PM, Luke Paireepinart wrote: > In general, you shouldn't even hint at the possibility of there being a bug > in the code unless you have test cases and a patch handy. Especially > something as widely used as hashlib. It gives An air of arrogance about your > post, whether you intend it or not. > I think your issue got resolved already, just thought it might help to have a > bit of an explication about hacker culture. > See Eric raymond's how to ask questions the smart way for much more > information. Sad as it is, your initial impressions will actually govern how > likely you are to get replies. > > Food for thought! Luke and the rest of the list: I know it can be perceived as arrogant to suggest a bug and not have at least some effort behind the statement. Given the response of the missing parentheses I can certainly understand why you would think that about some new comer to the list. What you didn't know (and what I didn't say because it wasn't relative to the question) was what I had done on my own to figure it out. You need to know that I'm also mildly dyslexic and sometimes miss or transpose things when I read them. Going back and re-reading the documentation again but this time knowing what to look for, I can indeed see that I missed the "()"s. It turns out I missed them in several places. But reading and re-reading that same document over the course of two days did not reveal the problem. I was as confident as I could be that I was reading the documentation correctly and was following proper syntax. I will apologize for the tone and using the word "bug" without sufficient evidence, and I will be more thorough in the future. Once I get past a few basic questions I hope that my contributions to the list can overcome this little hiccup. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] changing list index start
I'm using the following function style I found on the net to create menus for a command line python script: def mainmenu(): # the main menu todolist() mainmenuoptions = ['Clients','Jobs','Billing','Quotes','To Do Items','Employee','Exit'] mainmenucalls = [clientsmenu, jobsmenu, billingmenu, quotesmenu, todomenu, empmenu, quit] for i,option in enumerate(mainmenuoptions): print('%s. %s' % (i, option)) mainchoice = int(input('\nYour Choice? ')) clearscreen(osname) mainmenucalls[mainchoice]() return It works well, but the first item is the list is item 0. This is normal in most computing situations, but because this index is part of the output It would be nice if the first item in the list is item 1. php provided a way to change this, but I can find no documentation that says python can do this as well. I'm sure that python can do this, but I'm unable to find a definitive answer with google. Would someone please enlighten me? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] changing list index start
On Fri, Sep 10, 2010 at 6:14 PM, Lie Ryan wrote: > On 09/11/10 07:36, Rance Hall wrote: > In most cases in Python, you would almost never need to reference the > list's index directly since python makes it easy to use iterators; > however in your particular case, which is a valid exception, enumerate() > takes an optional second argument `start` which defines the number that > enumerate start to count with, i.e. you can do: > > for i, option in enumerate(mainmenuoptions, 1): > print('%s. %s' % (i, option)) > >> php provided a way to change this, but I can find no documentation >> that says python can do this as well. > Thanks everyone for responding, Because this menu structure is repeated many times in my code, the ideal solution would have been to "set index start = 1" in the beginning of the script. something like sysctl variables in Linux perhaps but in this case only valid for this program. Its clear from the responses that this solution is not available in python, I wish it were, it would make my life much easier for this project. I like the approach that Lie suggested, as it seems more "natural python" to me as opposed to a workaround. However this is also only half a solution since it applies to the printed menus only and not to the response afterward. It seems that Luke is right looks like we have to do math with the indexes. Lie also referred to my particular case as a valid exception, are there enough other such valid exceptions that requesting a feature enhancement would gain some traction? If this is but one of a few special cases, I doubt it would be worth the time or trouble to formally make the request. Maybe I should ask if there is a better way to do what I want to do here. Is there? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] changing list index start
On Sat, Sep 11, 2010 at 10:40 AM, Joel Goldstick wrote: > > I think the first message in the original post is instructive: > > "I'm using the following function style I found on the net to create > menus for a command line python script:" > > I (sometimes!) love looking at other peoples code to learn. However taking > some code and > using it in your application demands that you understand it, or it will bite > you later. In this case, > the code might have been appropriate to the original author, but not for its > new use. > I agree completely and I did understand what this code snippet was doing, and I also understood why the first menu item was labeled 0 My question revolves around the theme of "this is the idea I want to use, it does this, I like what it does, but I'd like to alter it slightly, how do I turn this into what I want? > Using index starting with 1 sounds to me idiomatic of BASIC programming. > While I'm too inexperienced > with Python to consider myself good at it, I think the key to using python > is to 'get it' as to the fundamental > data types in python and how they make things easier to solve problems. > One of the earliest programming > books I read was 'Algorithms + Data Structures = Programs' by Niklaus > Wirth. The book used Pascal, which I think > the author wrote. But no matter the language, learn what data structures it > offers, and why and then your algorithms > will become simpler and more elegant. If you find yourself doing weird > things with your code, see if you can rethink > how you organize your data > Yea, it does a little bit, doesn't it. (at least reminding of BASIC) I can't disagree with you on the rest of your comments either I can't say that I thought my original idea was weird at all, BASIC, PHP and probably other languages allow you to approach the problem in the same way I thought of. But this is python and not any other language, so for me I'm finding that the biggest issue on my plate now is not "What is this?" but more of "Why is this better or worse than that?" Steven reply in an example of what I'm talking about, Steven said "You *think* its a good idea" In some ways python is a very flexible flowing language. In other ways its very restrictive. For example: I *like* how whitespace matters in python, it forces you to structure your code.correctly. Other languages don't care about the whitespace, you dont have to indent your code (which is a disaster to read I know) This is the stuff I need to learn, where is python rigid and where is it flexible. This gives me a framework to use for evaluating tasks. I think my original question has been answered and I'm glad I wasn't barking up the wrong tree. As to the philosophical stuff maybe its time to start a new thread. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] py-postgressql v1.0.1 question
I'm not sure if this is the right forum for this or not, if there is a better place to ask this question please let me know and I'll re-post elsewhere. I'm using python v3.1 and the py-postgresql v1.0.1 module located at http://python.projects.postgresql.org/docs/1.0/ I'm using prepared sql statements like: insertnote = db.prepare("INSERT INTO business.to_do_list (note) VALUES ($1)") delete_note = db.prepare("DELETE FROM business.to_do_list WHERE item = $1") so far so good, everything is working well and I understand whats going on. But I have a situation where I want to count the number of notes in the database, and if 0 do something, and if 1 do something else. I have another occasion where I only want the first few records to be returned. So for testing the idea I did this: get_todo = db.prepare("SELECT note FROM business.to_do_list ORDER BY item") get_todo_limit = db.prepare("SELECT note FROM business.to_do_list ORDER BY item LIMIT 10") get_todo_count = db.prepare("SELECT COUNT(note) AS notecount FROM business.to_do_list") I *think* there is a better way to do this, but I'm not seeing it in the documentation, or its there and I'm not understanding it correctly. I suspect that the get_todo_count statement is not required at all. I have a hunch, although I can't prove yet that the result set returned by the SQL SELECT statement will have some way to access the record count directly Something like this: m = get_todo_limit() if m.count == 0: do stuff else: do other stuff I can't quite narrow this down. I'm sure its possible, It likely depends on what python variable type is used by the py-postgresql module, but I'm not seeing this in the docs. Second question is more of a performance question: I don't suspect a "large" # of items in the to_do list, so I *think* that it would be better to just have one SQL statement and then loop through the results 10 times to get the first few records rather than having a seperate sql statement as I have shown here. I'm too new at python to have a feel for the *right* way to go about this part Could someone point me in the right direction please? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] forcing hashlib to has string variables
Everybody knows you don't store plain text passwords in a database, you store hashes instead consider: userpass = getpass.getpass("User password? ") encuserpass = hashlib.md5() encuserpass.update(userpass) del userpass Now the documentation clearly states that if you are hashing a string you need to covert it to bytes first with a line like this: encuserpass.update(b"text string here") The "b" in this syntax is a shortcut to converting the string to bytes for hasing purposes. which means that the first code snippet fails, since I didnt convert the variable contents to bytes instead of text. I didn't see an example that addresses hashing the string contents of a variable. Whats missing in the above example that makes hashing the contents of a string variable work? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] forcing hashlib to has string variables
Luke: On python3.1 I get the following error using your (untested) two line snippet: TypeError: Unicode-objects must be encoded before hashing If I add the b back into the mix, I get a hash with no error messages. But I still can't quite figure out how to get the variable contents into the hashing function. On Sun, Sep 12, 2010 at 1:47 PM, Luke Paireepinart wrote: > This is how I use it (untested) > Import hashlib > Print hashlib.md5("somestr").hexdigest() > > Works fine without using binary string. > > > On Sep 12, 2010, at 1:19 PM, Rance Hall wrote: > >> Everybody knows you don't store plain text passwords in a database, >> you store hashes instead >> >> consider: >> >> userpass = getpass.getpass("User password? ") >> >> encuserpass = hashlib.md5() >> >> encuserpass.update(userpass) >> >> del userpass >> >> >> Now the documentation clearly states that if you are hashing a string >> you need to covert it to bytes first with a line like this: >> >> encuserpass.update(b"text string here") >> >> The "b" in this syntax is a shortcut to converting the string to bytes >> for hasing purposes. >> >> which means that the first code snippet fails, since I didnt convert >> the variable contents to bytes instead of text. >> >> I didn't see an example that addresses hashing the string contents of >> a variable. >> >> Whats missing in the above example that makes hashing the contents of >> a string variable work? >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] forcing hashlib to has string variables
On Sun, Sep 12, 2010 at 2:29 PM, Dave Angel wrote: > On 2:59 PM, Rance Hall wrote: > Convert the string to bytes. If the string is ASCII, you can simply use the > bytes() function. If not, you may need to specify an encoding. Thanks Dave, this is what I needed, I looked in the function reference and didn't see it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] py-postgressql v1.0.1 question
On Sun, Sep 12, 2010 at 3:53 PM, Bill Allen wrote: > Rance, > > I was doing something similar, except I was querying an Oracle database, > using the cx_Oracle module. I wanted the non-duplicated count of parts in > my database that met certain criteria. All the output that met the criteria > of the select statements is loaded into the cursor object. I then loop > through the cursor object appending the contents into a list. Then I > converted the list to a set to blow out the duplicates and then back to a > list and took the len of the list for my final count. > > #Fetch the list of parts > cursor.execute("select pobj_name from pfmc_part where pmodel = :arg_1 and > pstatus = :arg_2", arg_1 = "PN-DWG", arg_2 = "RELEASED") > for pobj_name in cursor: > Parts_List.append(pobj_name) > > print("size of Parts_List before set operation =", len(Parts_List)) > Parts_List = list(set(Parts_List)) > print("size of Parts_List after set operation =", len(Parts_List)) > > Perhaps you could loop though your get_todo object and load into a list and > do similar or just take a len of it directly if it is already a list. > Thanks for the tip. I'll do some more research but this sounds promising. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] py-postgressql v1.0.1 question
On Sun, Sep 12, 2010 at 4:54 PM, Luke Paireepinart wrote: >> >> Thanks for the tip. I'll do some more research but this sounds promising. >> >> Rance >> > Just be aware that some methods of list building will iterate over the list > and evaluate it. So if you only want to retrieve the first 10 results but you > do something like > Results = [I.fetch() for I in cursor] > print Results[:10] > > You will actually be retrieving all records. > Obviously this is a contrived example and it will be more subtle in practice. > Just be cautious that you're not fetching all your data initially and then > paging it. You could maybe check your database for the number of results it's > returning for each query maybe. I'm not very knowledgeable about database > monitoring and optimization but this strikes me as something you could > probably do. Thanks for the warning here, and I had already suspected as much. I am making a calculated guess that there will only be at most 15-20 items at a time in the result set. Getting all 20 and only choosing the first 10 during the list iteration process seems to be the best route. Especially since sometimes I will want all of them. If I just play games with the iterator then I can reuse my get_list code other places. If my calculated guess turns out to be wrong, and there are a significant # of items in the list, then I can revisit this issue. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how best to implement paginated data in CLI
I'm using python 3.1 with py-postgresql (located at http://python.projects.postgresql.org/ I need to work through how best to paginate larger sql result sets. my SELECT statement returns a list, I can get its len() to find out that X records were returned What I want to do is print the first N records and include a Previous/Next Choice at the bottom. I know I'll be making some use of the start and stop optional parameters in the for loop that processes the list. I think that I'll need variables for the page size (#of records per screen) and a start position for where to begin in the list. I want to make this a function so that its reusable code Here is a first draft of the idea: (untested) def paginate_stuff(list,start) pagesize = 10 for row in list[start:start+pagesize] print(row["column"]) print ("\n") message = "" prompt = "" if not start == 0: message = message + "Previous" prompt = prompt + "P" if not start ==0 and not start+pagesize >= len(list): message = message + " or " prompt = prompt + " or " if not start+pagesize >= len(list): message = message + "Next" prompt = prompt + "N" print((message+"\n") choice = input(prompt+"? ") if choice == "n' or "N": paginate_stuff(list,start + pagesize) if choice == "p" or "P": paginate_stuff(list,start - pagesize) return I'm not certain at this writing of the proper syntax of the if statement conditions, I will look that up to be sure the syntax is right. What I want to ask about is the logic flow, does this make sense? Am I thinking about the problem the right way? Is there a better way to pull this off? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how best to implement paginated data in CLI
On Tue, Sep 14, 2010 at 7:15 PM, Alan Gauld wrote: > > "Rance Hall" wrote > >> I'm using python 3.1 with py-postgresql (located at >> http://python.projects.postgresql.org/ >> >> I need to work through how best to paginate larger sql result sets. > > Its usually best to do that at the SQL level by controlling the cursor. > I don't know PostGres but most SQL dialects allow you to control > the number of result rows returned to the cursor then fetch the next > group and the next etc. This will also potentially save a lot of network > bandwidth (you only fetch the results you need which may not be > all of them) and get your results back quicker for a big result set. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ Alan: I think I need to refine my definition of large in my original question. You are correct, but I think some more context might be in order. A standard windows terminal window opens to run my app and it has anywhere between 15 and 20 lines of text available for output. (that can be altered I know, but that has limits) Some of those lines are taken up with formatting and the question and input prompt at the bottom, so you have only room for 10 data elements from your list, In my case large is any recordset larger than the ten there is room for on a single screen. So the real question is how large is large? I'm operating under the assumption that the size of the datasets Im talking about are "small" in terms of what I usually think of when I think of databases and networking, bandwidth and the like. But large in terms of the delivery mechanism (the windows terminal window) I suspect that the extra network activity required to manage cursors on a remote database is in my case meaningful. I hope this sort of helps explain why I went where I went with my OP. In reviewing the app I'm hoping to replace I found the size of the client list is under 100 records. Given this new information, do you still think that db cursors is the way to go? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] working with empty lists
Im working on a little piece of test code to test an idea for a larger script. Here is what I've got so far: l = [] for i in range(0,10) l.append(i) for i in range(0,10) print('%s. %s' % (i, l[i]) This gives me: 0. 0 1. 1 2. 2 etc which is what I expect, but what I want is to get something like 0. 1 1. 2 2. 3 . so that the output is clearly different on either side of the "." I tried changing the append to l.append(i+1) which almost worked but the output started with 1. 2 I was curious what happend to the 0. 1 line I know this means that I'm not understanding exactly what append actually does. I know that my ide shows that the list as other functions like insert, etc. Can someone explain to me whats the best way to add a value to a list that is not long enough to accept it, without playing with the indexes, it appears I currently am playing with them. I know generally that we aren't supposed to care about the indexes but this is eventually going to be part of a menuing system that displays the index, so I do have a legit need to care about what is happening to the list index. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] working with empty lists
On Wed, Sep 15, 2010 at 11:33 PM, bob gailer wrote: > On 9/16/2010 12:05 AM, Rance Hall wrote: >> Thanks guys for replying, looks like I do have a bug in my code, but its not where I thought it was. Must have been up too late last night. The code I provided in my OP does work (with typos corrected) but this code with one layer of complexity does not. def paginate_stuff(list,start): pagesize = 2 for i in list[start:start+pagesize]: print('%s. %s' % (i,list[i])) return l = [] for i in range(0,10): l.append(i+1) paginate_stuff(l,0) Idea here is to be able to take N things Y at a time sequentially from anywhere in the middle of the list. every time I run this code I start the output one list item in the sequence ahead of where I want to. I can change the functional call to paginate_stuff(l,3) and it will start where the above one leaves off But I keep losing list[0] Im reasonably sure that it has something to do with my for loop, but I don't understand how ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] working with empty lists
On Thu, Sep 16, 2010 at 9:24 AM, Joel Goldstick wrote: > I typed in this: > > > 3 l = [] > 4 > 5 for i in range(0,10): > 6 l.append(i+1) > 7 > 8 for i in range(0,10): > 9 print ('%s. %s' % (i, l[i])) > 10 > 11 def paginate_stuff(list, start): > 12 pagesize = 2 > 13 for i in list[start:start+pagesize]: > 14 print ('%s. %s' % (i,list[i])) > 15 return > 16 > 17 paginate_stuff(l,0) > > and i get this: > 0. 1 > 1. 2 > 2. 3 > 3. 4 > 4. 5 > 5. 6 > 6. 7 > 7. 8 > 8. 9 > 9. 10 > 1. 2 > 2. 3 > > > What are you expecting? > > you are getting what I am getting, so good news there, its not my code (its my understanding instead) In the above output where the you go from 9. 10 and the next item is 1. 2 I'm expecting the next item to be 0. 1 again. It appears as if the for loop iterator is iterating BEFORE it executes stuff as opposed to after like I'm used to. if I change the print line inside the for loop to: print('%s. %s) % (i-1,list[i-1])) I get what I think I should have gotten orginally Is this the correct understanding, is the for loop iterator iterating before any of the stuff executes the first time? This seems odd to me somehow. I appear to have fixed it, now I just wish I understood it. R ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] working with empty lists
On Thu, Sep 16, 2010 at 11:21 AM, Joel Goldstick wrote: > > > On Thu, Sep 16, 2010 at 11:46 AM, Rance Hall wrote: >> >> On Thu, Sep 16, 2010 at 9:24 AM, Joel Goldstick >> wrote: > This line is illustrative: > >> 13 for i in list[start:start+pagesize]: > > start is 0, pagesize is 2 so we get list[0:2] which means i gets the value > of what is in list[0], then next time list[1] then it stops > > list[0] IS 1, list[1] is 2 > > so: > 14 print ('%s. %s' % (i,list[i])) > > will print 1. list[1] which is 2 > > then print 2. list[2] which is 3 > > maybe what you want is this: > for i, value enumerate(list[start:start+pagesize], start): > print i, value > For some reason I wasn't seeing that I was iterating over the list contents not the list indexes, and because in my sample data both were numbers it worked but not how I wanted it too, Damn that dyslexia, oh well. Thanks for helping me through it. By the way, this also works as I expect: for i in range(start, start+pagesize): also gives me what I want to see. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Windows printing
I'm using this page as a reference: http://timgolden.me.uk/python/win32_how_do_i/print.html I'm able to print to the default printer ok, but I can't seem to find out how to get to pick the printer I want to use. This is from a CLI app, so there is no gui. win32print seems like it has everything I need, I just can't quite see how to put it together I'd like to have my python app ask which printer to use as opposed to just using the default printer. win32print.EnumPrinters() does give me a list, but its not just a list of names. the list also includes other items like network paths depending on the level of detail you specify in EnumPrinters. Tim has two possible ways to print. Tim shows how to use the win32api to pass a ShellExecute command that prints. The catch to this command is you have to print a file type and it has to be the default type for an app to open. He also shows a way to skip the ShellExecute and use the print api directly to gain some more control. Based on my needs, I'm pretty sure that I'll need to use win32print, Tim's how-to is likely not for my version of python (mine is 3.1) since some of his command fail on my system because mine wants options or parameters that Tim doesn't mention. I'm going to be printing automatically generated check-in tickets. I can make the ticket, save it as a temp file, but I can not print to the default printer, I must be able to select the destination printer. My plan is to ask the user what printer to use for the session, and save that printer name in a variable and direct all automated prints to that printer. As we move the laptop from place to place the default printer is not always available. And the available printer changes depending on location. Ideally there would be a variation of the ShellExecute command that would let me specify a printer name. Windows 7 is the predominate windows platform we are using. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Windows printing
On Thu, Sep 23, 2010 at 3:40 AM, Tim Golden wrote: > On 23/09/2010 07:30, Rance Hall wrote: >> >> >> Tim's how-to is likely not for my version of python (mine is 3.1) >> since some of his command fail on my system because mine wants options >> or parameters that Tim doesn't mention. > > I've fixed one issue: the win32print example now passes bytes for Py3.x > and str for 2.x; that example now works for me. I haven't worked through > the other examples yet but thanks for the heads-up. > Thanks for the rapid turn around on this one. > > OK. Thanks for the fairly clear explanation of the situation. I agree > that a ShellExecute variant which allowed printer selection would be > good, but I don't believe there is one. That's basically because > ShellExecute is really what the user indirectly actions when they > double-click or right-click on a file and choose one of the actions: > there's no scope for additional info within the ShellExecute mechanism. > Obviously a program called in this way is free to do whatever it wishes > in the way of selection dialogs and the like. > > One thing which isn't entirely clear to me is whether your ticket-saved- > as-a-temp-file is printer-ready, eg plain text, PS or PCL, or whether > you'd really want to format it further before printing. On the basis > that it's ready to go direct, the following very slight variation should > do what I think you want: (tested only on XP; I'll try to get hold of > a W7 machine to double-check) > For the first roll-out and testing I figured plaintext was good enough. Future revisions will probably use the PDF library you also referred to on your page. Either way that is as printer ready as I expect I will be able to get it. > > import os, sys > import win32print > > printer_info = win32print.EnumPrinters ( > win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS > ) > printer_names = [name for (flags, description, name, comment) in > printer_info] > for i, name in enumerate (printer_names): > print ("%d) %s" % (i + 1, name)) > > n_printer = int (input ("Which printer: ")) > printer_name = printer_names[n_printer+1] > print ("Using", printer_name) > > hPrinter = win32print.OpenPrinter (printer_name) > # > # ... and so on, per the original example > # > > > > Is this what you're after? Or have I missed the point? > > TJG This has the logic of what I want, I'll test it later today on Win7 and see if I have any issues I can't resolve, I tried some of this from the python console and dont get any errors, so it lools promising. Thanks again for the rapid response. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Windows Printing, Round 2
Again I'm referencing Tim Golden from http://timgolden.me.uk/python/win32_how_do_i/print.html This code block is relevant: import os, sys import win32print printer_name = win32print.GetDefaultPrinter () # # raw_data could equally be raw PCL/PS read from # some print-to-file operation # if sys.version_info >= (3,): raw_data = bytes ("This is a test", "utf-8") else: raw_data = "This is a test" hPrinter = win32print.OpenPrinter (printer_name) try: hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data", None, "RAW")) try: win32print.WritePrinter (hPrinter, raw_data) finally: win32print.EndDocPrinter (hPrinter) finally: win32print.ClosePrinter (hPrinter) Things are progressing along and I'm really more and more excited about python, every time I try to do something, It just seems to work and be straightforward. Notes: AFAICS win32pring.StartDocPrinter(hPrinter, 1, (jobname, None, None)) might be better as the last "None" tells the system to process through the print driver, Raw Data bypasses the print driver. Due to the variety of printers involved, I think bypassing the print driver with "RAW" will come back and bite me later. I also added a variable to catch the output from win32print.WritePrinter() so it would not display on the screen. I called it hSize. Questions: Under python 3 I need to send a byte string to the printer. But I wont have a byte string, I'll have a filename What is the pythonic way to convert a file to a bytestream? I can open the file for reading, and loop line by line through the file appending bytes(line, "utf-8") to variable but this doesn't seem right to me somehow. Is there a better way to do this? My HP laserjet 1100 does not print the job automatically. It accepts the job, processes the job, and then lights up the lights on the front of the printer and waits. When I hit the button, then the document prints. I have only seen this behavior before when printing envelopes. When an envelope print job goes to the printer it behaves the same way my python print jobs are. I suspect that this has to do with the fact that the page size of the printjob is either not specified or different from the standard 8.5 in wide x 11 in long it can handle. win32print documentation mentions DocumentProperties and DeviceCapabilities that might help, but I don't see how to use them to implement a solution for my problem. I further suspect that there are other printers out there that will behave similarly if they don't have specified what they needed. How do you deal with this problem? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] generating formatted output
My app will be printing a series of documents that are the same each time the doc is printed with the exception of the variables. Sort of a MailMerge if you will. It seems to me that the easiest approach is to create a series of text files with the layout and placeholders I need (again much like MailMerge) And then do something like this: file = open(fileName, "r") #Opens the file in read-mode text = file.read() #Reads the file and assigns the value to a variable file.close() #Closes the file (read session) text = text.replace(sourceText, replaceText) #there will be a series of these. file = open(fileName2, "w") #Opens a new file in write-mode. file.write(text) file.close() #Closes the file (write session) Then you can print the file or email it or whatever you need to do. There wont be too many of these replacements (think invoice template with substitutes for customer information and a billing detail section.) So my question is about the approach. Is this reasonable? Is there a better way to do this? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Operating in Place
On Tue, Sep 28, 2010 at 3:03 PM, Corey Richardson wrote: > Hello tutors. > > I hate doing this: > string = string.lower() > > Is there a way to do it without the "string =" part? Thanks. > I suppose the best answer is it depends on what you are doing with string after you do string.lower() you can use the string.lower() directly in IF statements and such without worry a = "TEST" if a.lower() == "test": do stuff works for me.even on the new 3.1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] validating user input for cli app
I have the following scenario, during a cli app a function is called whose purpose is to get enough info from user to create a database record. Clearly the input values need to be validated. How do I handle the situation where validation fails, but the answer to the question is required. so far I have something like this def moduser(clientid): if not len(clientid) == 0: client = getclient(clientid) # this is a database call to get the existing record for modification, entering a zero length string assumes its a new record instead of a mod. if len(client) == 0: print("client not found") # more code here to wait a second and then go back to the menu selection that started this function. clienttype = input("Enter client type, there are two choices, Commercial or Individual\n choose C or I ") *** My question is about what goes here *** .. return This is where life gets confusing, if clienttype is not a c or an I (case insensitive) then I have to ask the question again or offer a way out of my function There are several questions being asked here, about name, address, etc., each element has its own validation rules, and each question needs and answer I know how to write the if statements to decide if the data entered is valid or not, but I need a way to deal with what happens when it is NOT valid. I'd like to be able to just ask the question again, and re-validate the new input or offer a "press q to quit" option q to quit is no big deal, but it doesn't make sense to ME to clutter up the code with a whole set of functions that each just ask one question and return a value. Especially since when my app is done there will likely be over a hundred of these type of one or two line functions. the names will be impossible to keep up with. Whats more, debugging and future upgrades will be a major PITA. The old GOTO syntax that everybody hates was ideal for this type of thing, if validation fails, just goto the question again and re-ask it. Based on my Internet research it looks like the only way you get GOTO capability is with an April Fools joke in 2004. So how do you do this? Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] if syntax expression help
I have a user entered variable that I need to check to see if they entered one of the two legal values. But I only need to check this if one other fact is true. we have a variable called "mode" whose value is either "add" or "edit" based on how we where called. we have a userentry variable tied to an imput function. My current if statement looks like this: if ((userentry.lower != "c" or userentry.lower != "i") and mode == "add"): do stuff else: do other stuff My problem is that my other stuff always executes even when I think my stuff should run. so what I want is two conditions tested simultaneously I think I have a syntax problem with the parenthesis because I can't find documentation where this is valid, but it doesn't error out so python doesn't choke on this. I know about nested ifs but I was trying to avoid them for elegance and easier reading. if mode is "add" then my userentry field needs good data, but if my mode is NOT "add" then I don't care about this data cause I'm not going to be using it. Ive tried to construct the logic such that when the if is true, its the stuff that runs, and when the if is false, its the other stuff that runs. Because of the NOT equal tos in the if a false is actually a passed test, and true is actually a failed test. Could someone please help me figure out the best way to say this in python. BTW its python31 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] for loops when there is only one row in the result - is there an alternative?
Im using the py-postgresql module (docs here: http://python.projects.postgresql.org/docs/1.0/) in a python 3.1 environment to connect to my database. so far everything is working, but I'm having trouble understanding the structure of the variable returned by a select statement Generally you have something like this: clientlist = get_clients() # where get_clients is a prepared sql statement. normally you would get the individual rows like this: for row in clientlist: do stuff which is great for a long list of results. But I'm running into issues when there are only 0 or 1 results in the set. if there are zero rows then I can do something like: if len(clientlist) == 0: do stuff I'm looking for a better way to access the row when there is just one row in the result. Say from a user login attempt, or a request to edit an existing client record. Is there a decent way to get direct access to the single row in the result set without having to go through the for loop for just one item? It likely helps to know exactly what variable type clientlist would be. I have no idea. What I can say is that once you do get the row result, you can refer to values in the row with syntax like row["columnname"], but I'm honestly not sure if this is helpful information. Ive read the module docs looking for something interesting, but I can't seem to find this particular tidbit. If I have to do the for loop fine, but I just thought it looked a little ugly. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] string case manipulation in python 3.x
I need to do some case manipulation that I don't see in the documented string functions. I want to make sure that user input meets a certain capitalization scheme, for example, if user input is a name, then the first letter of each word in the name is upper case, and the rest are lower. I know how to force the upper and lower cases with string.lower() and friends. and I could even do a string.split() on the spaces in the names to break the name into pieces. I don't see an obvious way to do this. what I've come up with so far is to do something like this. break the name into pieces force each piece to be lower case replace the first letter in each word with a uppercase version of whats there already. Problems with this approach as I see them: The built in split function will create undesirable results for names that contain suffixes like Jr. etc. I'm not entirely sure how to replace the string with an uppercase first letter on a per word basis. Whats the "right" way to do something like this? Thanks Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyserial and invalid handle
On Mon, Nov 29, 2010 at 8:21 PM, John Smith wrote: > > On 11/29/2010 5:56 PM, Emile van Sebille wrote: > (snip) >> >> Hmmm... any chance you don't have administrative rights on the account >> performing this? I never got to Win7 (having stopped at XP) but I know >> it's got a reputation for excessive permission asking. > > You're right about that. It's like Win7 is paranoid. But, it tells me when I > need to supply administrative permission. Some day I'll find the button that > tells the system that nobody else uses this computer and to shut up and get > on with it. > Just so you know, Its called User Account Control or UAC. Google for "disabling UAC on Windows 7" and you can find a tutorial or two on how to make your computer "shut up and get on with it." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] printing in python 3.x
I've been working on this cli based python 3.x app for a friends shop. So far, everything is working well. We are now ready to start development on a new module of my code. The shop is a repair shop, and I've already done the code for client management and employee management and all the framework and supporting stuff, but now its time to check in broken items for repair and print a reciept for the customer and a shop ticket for the item. shop tickets are easy to do *we think*, we think all we need to do is create a "template" text file, and open that text file, sub markers for real data, and create a temp file then send that to the printer with the functions of the underlying OS. In my tests on windows this worked fine. But shop owner wants to do something nicer with the customer receipts. He is talking shop logos and pdf files. I have several issues. Google keeps bringing up ReportLib and the python imaging library, but neither have a 3.x compatible version yet. Shop owner is talking about eventually being able to save customer check in tickets and check out tickets on a web site where clients can log in and check on their work. My thought is that this is perfect for PDF files, but they aren't available on 3.x yet. What other options do I have? I looked at word integration and openoffice integration and they dont look ready for 3.x yet either. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newline
Ava: On an old typewriter when you slapped that mechanical arm two separate but related things happened. 1) the page advanced to the next line of text. 2) the carriage was returnedt to the beginning of the line of text. If you were careful about the way you slapped that mechanical arm, you could get the page to advance without moving the carriage to the beginning of the line. You would get stair stepped output like this Now in the computing world, these two separate but related things can sometimes still be dealt with individually. strictly speaking a newline character is the command to move down to the next line. and a return character is the command to go to the beginning of the line. On Windows, by convention the newline command does both. So you likely won't see the return command much. But there is one, and it is used on most non-windows systems. as to your specific question, I've never actually tried to specify a new line character like you have demonstrated. I just use the "\n" sequence to make my new lines happen where I want. On Fri, Dec 3, 2010 at 7:47 PM, Ashley Blackwell wrote: > Hello everybody, I'm new to the mailing list so I'm pretty sure I'll have > lots of questions:) > > It's a very basic question I have and everybody might look at this > question and say, "Wow, she reallly doesn't get it?" But oh well. Here's my > question: I'm in Chapter 2 of my Python Programming Third Editition book. > I've gotten to the section of the chapter where it talks about a "newline > character. Exactly what is a newline character? I keep reading the same > section of the book and I'm just not really understanding. Here's the code > that the book uses to explain the newline character: > > print("Here", end=" ") > print("it is...") > > The book says that I can specify that a space be the final character printed > instead of a newline. In the first print() statement I specify that a space > be the final string printed. So the text "Here" appears but no newline > character. The next print() statement prints the text "it is"..." right > after the space following the final "e" in the "Here" text. You do it by > passing a space to the end parameter of the print() function with the code > end" ". > > My Operating System is Windows 7 and the Python version is 3.1.3. That's it. > Thanks to anybody who responds to my question. > > -- > Ava Durand > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] updating databases with null values
I have a set of questions that ask about a customers name, address, email, etc. some of these values are allowed to be null, and others aren't. Some are required to have specific formats when they aren't null. I'm happy with the code Ive written and its question asking routine, but I need help understanding how best to deal with null values. I'm connecting to a postgres database with the python postgres module described here: http://python.projects.postgresql.org/ I'm also using python 3.x if it matters. The sql is different if you want to allow null values update table set value = "string" where condition with null value: update table set value = NULL where condition It would seem I need several iterations of the prepared db statement for various combinations of null values or I don't allow null values at all and put empty strings in the database instead of the null value. Could someone help me think through this idea and help me decide on a good method. Right now I'm thinking that I should skip null values and just store zero length strings in the db. I don't like it, but its certainly much easier (at least to me so far) Your thoughts? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] gui coding
When I learned FORTRAN years ago they didn't teach us OOP or what I like to call Class based programming. since then I've sort of always fallen back to be a procedural programmer with lots of functions. Python and the tkinter (Tkinter on Versions < 3) seem like a great way to write cross platform GUI apps for small light duty apps. I'm intrigued and want to learn but all the samples seem to be OOP or class based. I've tried to wrap my head around class based programming before and it didn't take. But it appears I'm going to have to try again as I can not find any tkinter samples that use a procedural approach. I'm finding it very difficult to understand what I need to get from tkinter because the classes are getting in the way and I'm not seeing what I need to see. Is there something about class based programming that GUI apps prefer to work better in? Does anyone have or know of a good tutorial or explanation of class based coding that I could have a run at? Thanks Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework and the Tutor list
On Wed, Feb 2, 2011 at 10:53 AM, Jerry Hill wrote: > > I don't think that's true at all. I think people here are happy to > help, including by posting working, efficient, code. What we try to > avoid is having students come here with their assignments and have us > do their schoolwork for them. I'm not sure how long you've been > subscribed to the list, but this happens on a regular basis. > -- > Jerry I can't agree with Jerry's comments more, and I have been the benefactor of both sides of this unwritten rule. As a student, I came to discussion forums like this one with homework questions hoping for an easy answer, and didn't get one. Now that I am a college professor I'm quite glad I didn't get an easy answer, and I appreciate even more now the fact that forums like this one know a homework problem from a real one, and work to teach more than normal when it is needed. I haven't been involved with python long, and I had an abrupt start on this list as my first question question was not written with the protocol I normally use, I had not taken time off and gotten away from my frustrations and taken the time to write a clear concise non-judgmental question. The rest of this list chastised me, and still helped me. I deserved the critical words, and did not deserve the help. I got both. Thanks go to the long time readers and contributors of this list who have made it something valuable, and I hope that some day soon those of us who are still learning, will be able to begin to meaningfully contribute. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SMS to URL
On Wed, Feb 9, 2011 at 4:08 AM, Dipo Elegbede wrote: > Hi peeps, > I am trying to write a code such that i can send an sms to a specific url > from my phone and get a reply back from the url. > I want the reply to be the content of the url I send to; what modules would > you advice. > I am testing with the url: http://www.dipoelegbede.com/msg.txt. > I have succeeded in writing a code that fetches the content of the page > using urllib and urllib2 but i still want a way to have my sms speak to the > url. > I will appreciate any help in this direction. Advices or links to > documentations would be fine. > Thank You. > I'm not convinced that what you say you want to do is really what you want to do, so I may be off base here, but as far as I know, you can not send an SMS message to anything other than a phone number. Many cell providers offer email-sms-gateway services so that you can send an email to the email address that matches your phone, and the phone company turns the email into an SMS and sends it to your phone for you. If your carrier has an emaiil-to-sms gateway then its reasonably trivial to use urllib and capture a web page and send it to you. Its the remote control aspect that you have requested that has me stumped. A friend of mine runs a phone message service and he has a device hooked to his network that has an antenna on it that sends sms message directly on the carrier network. If you had one of these things (which I doubt you do) I'm sure you could reverse it and send an sms message directly to your network. But the work that would be required to set this up (not to mention the expense) seems like more trouble than it is worth. Sorry I wasn't more help. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] script conversion windows -> linux error
I wrote a script on a windows box for python 3.1 after a meeting with the client for this project we agreed to port it to Linux so that I could make use of enscript and ps2pdf to email customers documents right from the script. Client uses a Centos server so I did some reading and installed python 3.2 in its own directory with a shortcut link named python3. Parallel instances of python seem to be fine. I fixed the standard line feed issues and am getting interpreter errors that I don't understand. for example: I am making use of the config module to store some database configuration details. I have this line in my code: config_version = config.get('versions','configver',0) This line fails under 3.2 Linux with the error message: TypeError: get() takes exactly 3 positional arguments (4 given) What could the 4th argument be? I only see three. This same code worked on python 3.1 under windows. What am I missing? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] script conversion windows -> linux error
On Fri, Mar 25, 2011 at 1:54 PM, Walter Prins wrote: > > > On 25 March 2011 18:26, Rance Hall wrote: >> >> config_version = config.get('versions','configver',0) >> >> This line fails under 3.2 Linux with the error message: >> >> TypeError: get() takes exactly 3 positional arguments (4 given) >> >> What could the 4th argument be? I only see three. >> >> This same code worked on python 3.1 under windows. >> >> What am I missing? > > OK, off the top of my head and not having checked anything, here's me > thinking out loud: > > Recall that all methods get one extra parameter, namely the instance > reference of the object that the method applies to/is being called on. > Thanks for this, I could not have recalled this, because I don't think I knew it prior to you mentioning it. > Regardless, the message is telling you that you're passing one more > parameter than expected. This makes me think that the version of config > module that you're using is older than the one you used on Windows, and that > perhaps this older version had one less parameter, which is why it's > complaining about too many parameters. > The version of config on the linux box should be NEWER than the one I used in windows. Recall in my OP that Windows was python 3.1 and Linux is now 3.2 > As I say, that's just educated guesswork/inference, I've not checked whether > the above hypothesis holds water under examination... Likely explanation. But I don't think it holds water when compared against the configparser documentation. Interactive help documentation seems to suggest that my syntax is ok. But the compiler clearly doesnt like it. Rance PS, I just took the original zero third argument out, and now it works. I think that confirms your theory. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing python3.2 from source on Centos 5.5
I thought I had successfully installed python3.2 parallel to the python2.x that is required by Centos. Im getting error messages from python scripts indicating that the _ssl module is not installed. I thought that this would be installed by default, but apparently you need to have -with-ssl in the configure line? Is there anything else I should be looking for since Im going to have to reinstall python3 anyway? Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] finding directory of self
I had been doing this under windows: import os import sys osname = os.name pathtocfg = os.path.dirname(sys.argv[0]) configfileloc = os.path.abspath(pathtocfg) os.chdir(configfileloc) to set the directory of all subsequent file lookups in a script. It worked underwindows because the shortcuts have a "Start In" directory listing. I need something for Linux use that does not care about a symlink that would be used to start the script. The above code finds the directory the symlink is in. I'm actually not suprised by this, it is what I expected, I just don't understand how to fix it. How can I do something similar to this, but find the "real" dir? Perhaps one of the other sys.argv arguments? Thanks for your help Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] data validation logic
Hey gang: I need some help trying to pythonize (sp?, if this is even a word?) an idea. I'd like to define a datavalidation function that returns true if data is valid, and false if it isn't. Here is the clincher. the logic of the data validator needs to be able to handle different types of data testing on request. So here is my idea: Define a function that can accept multiple inputs. One being the data element to validate, and the additional arguments, which can be null would define the validation needing to be done. For example, if a match value, or set of match values is defined, check the data element to ensure it matches one of the match values. I'd also like to be able to feed this data validator a list of names of tests to check (numeric, alpha-numeric, maxlength=10, etc.) each of these names would be the names of the various sub tests the validator can perform eventually I'll probably convert this to a class, but I really don't have a knack for OOP yet. (I'm still learning that.) Anyway so here is the problem I see with this idea and can't quite figure out how to handle. I need a way to store the pass fail values of each of the individual tests, and then return a pass if the data passes ALL tests no matter how many individual tests might be executed. In my mind an associative array is the best way to track this. It can be dynamically created with each call of the function and the array elements can be named for each test ran. The problem is the final test of pass/fail based on the pass/fail results of individual tests. It might be as simple as looping through the array elements and if any value is false break out of the loop and return false. else if all are true return true. Would someone be kind enough to help me work out the finer details of such an approach. Perhaps lists might be better for this sort of thing?, perhaps I have screwed up my logic somewhere? Thanks for any hints you can provide. PS this is on python 3.x. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] working with strings in python3
Ok so I know what I am doing is deprecated (or at least poor form) but the replacement must be awkward cause I'm not getting it. so this is in a cli based program designed to print status messages to the terminal on a linux box. pseudo code: message = "Bah." if test: message = message + " Humbug!" print(message) end pseudo code I'm sure this is not the way we are supposed to augment strings like this. maybe there is string.append() method or something I should be using instead? In my case the optional extra parts are always at the end of the current value of the string. Thanks for clearing this up for me. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] working with strings in python3
On Mon, Apr 18, 2011 at 8:10 PM, Marc Tompkins wrote: > On Mon, Apr 18, 2011 at 5:17 PM, Rance Hall wrote: >> >> Ok so I know what I am doing is deprecated (or at least poor form) but >> the replacement must be awkward cause I'm not getting it. >> >> >> so this is in a cli based program designed to print status messages to >> the terminal on a linux box. >> >> pseudo code: >> >> >> message = "Bah." >> >> if test: >> message = message + " Humbug!" >> >> print(message) >> >> end pseudo code >> >> >> I'm sure this is not the way we are supposed to augment strings like this. >> >> maybe there is string.append() method or something I should be using >> instead? >> >> In my case the optional extra parts are always at the end of the >>> >>> current value of the string. >> > > The reason it's deprecated is because it's SLOW - since strings are > immutable, there's a lot of extra copying going on behind the scenes. If > this code were inside a loop, being run lots and lots of times, this would > quickly become one of the bottlenecks of your program. However, in the > example you've given, I'm not sure it would make an important difference - > as slow as string concatenation is, it's still a heck of a lot faster than > the human operator! > So you have a CLI based app that asks a question of the user and you are trying to feed the prompt for the input function. I have a mode variable that stores how a function was called so I know the difference between a new entry, and an entry update. An entry update has default values for all the questions. so you might have an example like: message = "Please type the location address line 1." and if the mode was edit you can add message = message + "\nPress Enter to accept the default of " + dbrecord["address1"] then later your code doesn't care it just prints the message variable no matter what it contains. I'm going to go ahead and use this format even though it is deprecated and then later when we upgrade it I can fix it. > If you just want to have a status update for your user - like a progress bar > - you could simply print one string at a time and not store the result. > (i.e. if you just want to display a line of dots to indicate activity, you > don't need to store the full number of dots - just print one at a time - > without newlines - and forget about it.) > > If, on the other hand, you always want to have access to the full string AND > you want to print it along the way, then try a list. Lists are awesome! > >> message = ['Bah'] # message is a list, which currently contains one item: >> 'Bah' >> for x in range(10): >> message.append(str(x)) # append a string ('0', '1', etc.) to the list >> print " ".join(message) # print the contents of the list, separated >> by spaces > > That's if you want to do this in a loop. If you have a set number of items > to display, then string formatting is your friend: > >> message = 'The %s in %s stays mainly in the %s' % ('rain', 'Spain', >> 'plain') > > String formatting doesn't work for me in this case as the message is sort of dynamic. Way too much logic around printing a statement. A list might make sense, but printing a message one word at a time doesn't seem to me like much of a time saver. I totally get the immutable string thing. I can't say that I care one way or the other, mutable or not, each has consequences. But are CLI apps so rare that this sort of thing just doesn't happen anymore? This seems like such a basic thing and deprecating it seems rather harsh. I'm not really trying to start a flame fest, I'm just trying to understand the thought process. I don't see what I would call a legitimate alternative to this type of string usage. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] working with strings in python3
On Mon, Apr 18, 2011 at 9:50 PM, Marc Tompkins wrote: > On Mon, Apr 18, 2011 at 6:53 PM, Rance Hall wrote: > >> >> I'm going to go ahead and use this format even though it is deprecated >> and then later when we upgrade it I can fix it. >> > And there you have your answer. > >> A list might make sense, but printing a message one word at a time >> doesn't seem to me like much of a time saver. > > > Did you try my example code? It doesn't "print a message one word at a > time"; any time you print " ".join(message), you get the whole thing. Put a > \n between the quotes, and you get the whole thing on separate lines. > I think you misunderstood me, I simply meant that the print " ".join(message) has to parse through each word in order to get any output, I didn't mean to suggest that you got output one word at a time. Sorry for the confusion. > It's just that it's not stored as a single string, but assembled on-demand. > And you're right - doing this once or twice, or even ten times as in my > example code, doesn't make a huge difference. But at some point, if you > intend to continue programming, you will run into a situation where your > code needs to do something over and over and over and over... and getting > out of bad habits like string concatenation can make a very noticeable > difference in speed. > >> >> But are CLI apps so rare that this sort of thing just doesn't happen >> anymore? > > No, they're not all that rare, and CLI-ness has nothing to do with it. > >> >> This seems like such a basic thing and deprecating it seems >> rather harsh. >> > Bottom line: Python is not BASIC. In BASIC, strings aren't immutable, so > in-place string concatenation doesn't carry (much of) a performance > penalty. In Python, it will make your program unnecessarily slow. I think > you're under the impression that "deprecation" is a value judgment, or that > "message = message + foo" is deprecated because it looks like BASIC syntax. > Neither is true. > Again a little misunderstanding. I didn't mean BASIC the programming language. I forgot all I knew about that language long ago. I mean basic in the fundamental concept sense. Variables are variable, that's why we call them variable. Constants are constant, and that's why we call them constant. The idea of immutable strings variables blurs the line between these two mathematical concepts which are part of my thinking about teaching math (which I do at a local community college) as well as my thinking of computer programming. > From the Python wiki: > http://wiki.python.org/moin/PythonSpeed/PerformanceTips#String_Concatenation > Thanks for this pointer, it was interesting reading. > We may ask ourselves "why did Guido decide to make strings immutable in the > first place?"; probably the best reason is "so that strings can be used as > keys in a dictionary", but since I'm not Guido - not even Dutch! - I really > can't speak for him. > I'm not sure I buy this explanation as strings have been used as keys in dictionaries or "associative arrays" for a long time, a string variable is variable or mutable, where a keyname can not be. Anyway, thanks everyone for taking the time to help me get my head wrapped around this, I'm not sure I managed to do it yet, but the seeds were planted. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] validating user input
Ok, so I have this code that is working, but does little to validate user input and I don't quite yet understand that process yet. so given the following function: def buildmenu(menuchoices): for i, option in enumerate(menuchoices, 1): print('%s. %s' % (i, option)) menuchoice = int(input('\nYour Choice? ')) return menuchoice-1 Ideally before returning the menuchoice-1 there should be a check to make sure that the choice is a valid one. I know what I want to happen if the test fails, and if it passes we can just do the return and exit the function. I need help understanding how exactly we validate the input since in this case I have no idea how many menu entries there would be. Thanks for your time. Rance PS Happy Easter. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] validating user input
On Sat, Apr 23, 2011 at 6:38 PM, Alan Gauld wrote: > > "Rance Hall" wrote > >> Ok, so I have this code that is working, but does little to validate >> user input and I don't quite yet understand that process yet. >> >> so given the following function: >> >> def buildmenu(menuchoices): >> for i, option in enumerate(menuchoices, 1): > > Interesting, I didn't know enumerate could take a second > argument, and help() doesn't mention it eioither You > learn something new ... > >> print('%s. %s' % (i, option)) >> menuchoice = int(input('\nYour Choice? ')) >> return menuchoice-1 >> >> I need help understanding how exactly we validate the input since in >> this case I have no idea how many menu entries there would be. > > But you have just finished enumerating them and I holds > the index of the last item, or in your case the count of > the last item. But even without that you can always > use len() to find out how many menuchoices there are... > I knew about len, but somehow didnt think of it in this context. I didn't know that the last iteration of i and option would still be around. Some loops like that in some languages sort of clean up after themselves. So at the present time I have this: def buildmenu(menuchoices): for i, option in enumerate(menuchoices, 1): print('%s. %s' % (i, option)) validdata = False while not validdata: menuchoice = int(input('\nYour Choice? ')) if menuchoice >= 1 and menuchoice <= i: return menuchoice-1 else: print("Entry not valid") Maybe the while not loop could have been avoided, but it made sense to me, and was consitent with a pattern I used in another section of code. The issue I have now is that the input might not be an integer at all. If it isnt, then the int conversion that is part of the input line will die with a fatal error. So I *think* that I should strip out the int() from the input line and let the input stand on its own. Then as part of the if logic block at a test to see IF a int type conversion would succeed. If so, do it and make sure it is within the range. If no type conversion can happen, then reask the question just as we do if the number is not in the desired range. This was the part I was stuck on. I just didnt know it till I finished this much and still wasn't happy with the results for me to put a finger on exactly what my problem was. Thanks again all. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Graphic interface
On Tue, May 10, 2011 at 7:41 AM, louis leichtnam wrote: > Hello, > I'm trying to build a graphic interface, with button, radiobutton, enter > text area etc. > Does anyone have an idea or a source code for this? > Thank you, > Louis How you do this exactly depends on a number of things you don't specify. When you jump into the world of graphical programming there is such a thing as a window manager. GTK, QT, KDE, TK, and Windows all have different ways of addressing these same issues. Python even has its own builtin thing called Tkinter that uses the TK widget set (the widgets are the buttons, sliderbars, etc) You can use Tkinter, or you can bind python to the GTK or KDE widget set and ask python to create those widgets and explain how to arrange them on the screen and the window manager will handle the heavy lifting for you. Source code won't help you if you don't understand the above and limitations/options available in the widget set you have chosen. Most of the code samples are Object Oriented. If you are not familiar with coding objects then you will likely find the code samples rather daunting -- I know I did. I think I'd recommend that you start with Tkinter since it is cross platform compatible that way it doesn't matter what OS you use your code will still work. May I suggest that you go read about TK and Tkinter and decide if they are appropriate for your project. Then you can ask a specific question that can be answered. HTH Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] OT: Drag and Drop GUI IDE ideas
I know this is OT and I am sorry, but the readers of the list are some of the best to judge my problem. And it is about learning to code, just not python specifically. The MIS department at UNK is looking to create a course in business app development. The course will be about the app life cycle and discuss problem statement, testing, maintenance and disposal/decommissioning, etc. We want the students to develop a small app in the process, It could be a firefox extension, mobile phone app, or any other type simple structure. The catch is that the class is for non-programmers to try to get them introduced to the field. We would like to use a gui ide that can essentially hide the code from the programmer and connect widgets with the gui. We want them to be able to look at the code and analyze it. We would prefer open source (read: free as in beer) tools. Does anyone have any ideas of tools that can do this? we are open to any idea, android sdk and gui, ipad sdk and some gui tool, etc. Thanks for your time. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Progress Bar
On Tue, Nov 8, 2011 at 9:42 AM, Nikunj Badjatya wrote: > Hi All, > I am writing an installer in Python and Powershell which will be used to > install Virtual Machines at specific locations. > The main script is in Python and main.py inturn calls Powershell scripts > (.PS1). > It is complete console based and no gui. > I am using Python 2.7 > I was thinking to have a progress bar in my installer. similar to the way > when we install something in linux via console and a progress bar comes as > "[# ] 40% " > etc. > I also saw and ran couple of examples based in Python on the net. > http://code.google.com/p/python-progressbar/source/browse/examples.py > > My question is, How do I integrate it with Powershell scripts.? > I am using popen() to run powershell scripts. > The easiest way to do this is manually advance your progress bar with each powershell script completion. for example if there are four *ps1 scripts, then completing each represents an additional 25% completion. popen(ps script 1) advance progress meter to 25% popen(ps script 2) advance progress meter to 50% . to get anything more sophisticated you have to have the powershell scripts report something back to the main python script but I don't have a feel for how easy/hard that would be. I doubt that the popen call allows much more than waiting for the called script to exit. You could have the power shell scripts keep track of their own progress with a log file or some such and then a thread in your python installer could watch those log files and manually advance your progress meter based on log file entries. HTH Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: sifting through a long program
I accidentally replied just to the OP, so I'm forwarding my comments to the list for the record. -- Forwarded message -- From: Rance Hall Date: Thu, Nov 10, 2011 at 1:36 PM Subject: Re: [Tutor] sifting through a long program To: Nathaniel Trujillo On Thu, Nov 10, 2011 at 12:58 PM, Nathaniel Trujillo wrote: > How do I get to line 362 of a program without counting each line ? Thanks > for the help. Use an editor with line numbers visible? I've been a silent lurker on this list for awhile learning python as I read what other people were doing with it. The teacher in me wants you to get your questions answered, but the volunteer in me wants you to not waste my time and the time of other people on this list. This is clearly not a python question. It is a question on what tools are needed to write effective code regardless of language. Its starting to appear as if you are too lazy to even try to address your own problems on your own, and with Google at your fingertips there is no excuse for this. Perhaps you are trying, and it just appears as if you are not trying. Perhaps though, you really aren't trying. A word of warning, most of us have email clients that can silently delete messages from specific email addresses without having to read them. You don't want to be on the ignore list for people who can help you but don't want to spoon feed you in the process. Good luck learning python. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] going big (in terms of code base)
Ive just finished a major project with python and database connectivity. It was cool, and the friend I wrote it for was happy. It was strictly a terminal based app. To GUI of any sort. Because I like to write cross platform apps as much as possible, tools like python and os common widget sets like tk/tcl are my first choice. So Ive started studying tkinter (from python 3.x) I'm pleased with the flexibility. So far Ive been a very procedural based coder. Ive not cared much for Object Oriented stuff because I didn't learn to think/program that way. (think Fortran/Pascal, etc) GUIs add a lot of code bloat and lots of chances for bugs that have nothing to do with the logic of your program. I'm looking for a way to manage large projects (large in terms of code size) There are lots of new concepts to grasp here: From basic GUI stuff like event handlers to understanding class layout and what things go in this class and what things go in that one. Before proceeding I need to understand how best to break up a large file into several smaller files and not break anything. I assume that "import" will be part of the solution but it only "imports" complete python modules. Much different than a php include for short code snippets. I'm guessing that means I have to learn how to create my own modules as well. And then how to manage them. The hard part I'm struggling with at this point is understanding what python calls the things I need to understand. This makes sense in terms of being able to go back and fix something later and know where it is to find it. What I would like to do is create a basic app framework that I can use/reuse on future projects. What else to I need to know about to make my idea a reality? In what order would you suggest I learn these things so I can interconnect them properly. For example. OOP before tkinter? Thanks for your time. Rance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] interesting behaviour with postional output
On Mon, Nov 14, 2011 at 3:16 PM, Joel Goldstick wrote: > So, as was stated, best to use mono space if you are writing to a terminal. > If you are writing web pages, of course you have lots of formatting options > > -- > Joel Goldstick One option I've used in the past for issues such as this is enscript. You can have python call enscript with some formatting options, including font, and pipe the output of enscript into ps2pdf and you have a pdf file that you can email/upload to a web server, etc. The OP didn't say what the intended output was supposed to do, but just that it was different between screen and other output modes. The OP now knows that because of the difference between fixed width and proportional fonts, but my suspicion is that that piece of information while it fully explains and answers the OP, there is more forthcoming. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] going big (in terms of code base)
On Mon, Nov 14, 2011 at 5:01 PM, Wayne Werner wrote: > On Mon, Nov 14, 2011 at 2:49 PM, Rance Hall wrote: >> >> GUIs add a lot of code bloat and lots of chances for bugs that have >> nothing to do with the logic of your program. > > I'm not sure that I would entirely agree with this statement. I think that > GUIs definitely increase the likelihood of code bloat, they don't > necessarily create it. Bloat is unnecessary cruft that could (and should) be > removed from your program. If you want a graphical interface it's very > difficult to do that without a nice framework ;) > Yea, you are right. I didn't say that well. I think we were thinking the same thing, but you said it better than I did. >> >> I'm looking for a way to manage large projects (large in terms of code >> size) >> >> There are lots of new concepts to grasp here: From basic GUI stuff >> like event handlers to understanding class layout and what things go >> in this class and what things go in that one. >> >> Before proceeding I need to understand how best to break up a large >> file into several smaller files and not break anything. >> >> I assume that "import" will be part of the solution but it only >> "imports" complete python modules. Much different than a php include >> for short code snippets. > > Modules are definitely what you're looking for - but you can use modules in > a strictly procedural sense. Heck, if you wanted to make some pretty > un-Pythonic code you could throw imports all over your program in a very > strictly procedural fashion. But don't do that. > >> >> I'm guessing that means I have to learn how to create my own modules >> as well. And then how to manage them. >> > > Yes indeed. If you're planning to do a lot of things with modules, and > trying to deploy them to other people, I'd highly recommend learning > virtualenv - it will save you a lot of headaches in the long run. > Thanks for this, I looked it up, and it at least looks like something I could make use of. >> >> This makes sense in terms of being able to go back and fix something >> later and know where it is to find it. >> >> What I would like to do is create a basic app framework that I can >> use/reuse on future projects. >> > > Depending on what you need, this may be premature optimization because there > already exists basic frameworks (like Tkinter). > If you find that there are a lot of duplication/customization things that > you're doing, then it makes sense to automate that and create some type of > framework... but in the several Tkinter programs that I've created I've > never said "Huh. This is just clunky, I wish I had X". Well, except for the > H/VBoxes from GTK - but I created my own wrappers around the Frame class and > got exactly what I wanted. I think perhaps we are talking past each other here. I'm thinking a sort of skeleton directory that already has the basic code to create a basic main window, with a menubar and status message box at the bottom. That includes basic functions of db connectivity. Sort of like a template in a larger IDE. >> >> What else to I need to know about to make my idea a reality? In what >> order would you suggest I learn these things so I can interconnect >> them properly. For example. OOP before tkinter? > > I'm not sure that OOP or Tkinter would make learning one or the other any > easier, but I suspect that if there is a best order that OOP might be on the > better side. But then again, it might not be necessary. Even though I've > written classes for my programs it's hardly required, but I do like to think > that it makes some things easier. Relearning the way I think in terms of being a coder certainly does not qualify under "easier," but if I'm to write code that's maintainable for the long term then I need to switch to OOP > Actually, that's pretty much my criteria for using classes or anything else. > "Will it make this problem easier?" is the question that I ask myself. In > the case of some program hitting a database, maybe I have a table of > "people" and it stores the people that I know. So maybe I'll make a People > class that has the following attributes: > firstname > lastname > phone > email > address > And since in my mind, real people already have those things, it's easier for > me to think of myself as: > wayne = People() > wayne.firstname = "Wayne" > wayne.lastname = "Werner" > wayne.phone = "555-555-" > wayne.email = "wa
[Tutor] threading in python2.7
I bought myself a pcduino 3 nano development board for Christmas and started picking up python again after a long forced hiatus. The board runs Ubuntu Precise Working with development boards has me navigating the inner working of python threading and I find myself at a loss. My current project is a simulated emergency vehicle warning system (otherwise known as a police siren) I started my project by writing the lighting and the sound portions of the program separately and tweaking them till I got each part working as desired. Now to get them to start simultaneously and exit cleanly. I started with some basic python like this: import needed stuff global variable declarations def setup_hardware(): do stuff here to setup hardware and gpio sytem def lights(): control the red and blue LEDs to simulate warning lights def sound(): control the sound to simulate a warning siren def cleanup(): exit cleanly and cleanup def main(): setup() try: # individually starting either lights or sound works as expected except: # catch keyboard interrupt and call cleanup I've read many threading tutorials from the old thread module to the new threading module and I've found several different ways of starting both lights and sound in their own threads. The issue I have is exiting the program. All of the tutorials suggest having a global variable to indicate when to exit the threads. so I used a global variable called exitFlag and begin the program with it set to 0 Each of the lights and sound functions are placed in a "while not exitFlag:" loop I think I'm running into some variable scoping issues related to threads, but I'm not sure exactly. Based on the debugging messages I've added to my code, the changes to the exitFlag variable made in the either the main loop or the cleanup loop are never seen by the lights and sound loop threads. The net effect of this is that once the threads are started, they are uncontrollable and I have to quit the terminal to get the threads to stop. Could someone please point me in the direction of a good tutorial or sample code that works that I could use as study tool? I've not bothered posting the code since its custom to this board and will fail on another python instance unless it has the the same hardware control modules my board does. Thanks all for your help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading in python2.7
First, thanks Joseph for taking the time to reply. My comments interspersed below: On Fri, Jan 2, 2015 at 2:39 PM, Joseph Lee wrote: > Hi, > Answers are below. > > -Original Message- > From: Tutor [mailto:tutor-bounces+joseph.lee22590=gmail@python.org] On > Behalf Of Rance Hall > Sent: Friday, January 2, 2015 12:17 PM > To: tutor > Subject: [Tutor] threading in python2.7 > > Each of the lights and sound functions are placed in a "while not > exitFlag:" > loop > > I think I'm running into some variable scoping issues related to threads, > but I'm not sure exactly. > > JL: Each thread has its own context and local variables unless you want to > bring in the exit flag from the module itself. A good place to do it is > right before you enter the while loop. > Exactly what I suspected was happening, but was unsure how to address the issue. > > Original: > Based on the debugging messages I've added to my code, the changes to the > exitFlag variable made in the either the main loop or the cleanup loop are > never seen by the lights and sound loop threads. > > The net effect of this is that once the threads are started, they are > uncontrollable and I have to quit the terminal to get the threads to stop. > > JL: > > Could someone please point me in the direction of a good tutorial or sample > code that works that I could use as study tool? > > JL: There are plenty of projects online which uses threads. A toy project > might be to implement a long running counter that stops when you set some > variable to False, like: > > Pseudocode: > import threading > import time > > stop = False > > def count(upto): > global stop > for i in xrange(upto): > if stop: return > print i > time.sleep(0.5) > > def main(): > t = threading.Thread(count, args=(someNumber)) > t.run() > > This failed the first time I tried it, but see below: > Essentially, this guy runs two threads: the main thread, and the counter > thread. If the stop value is set, the counter thread will stop running. As > for placing the value check at the top of the loop, it was done that way to > make sure the thread dies when it is true and to do it as early as possible > for performance. > > Threads introduce interesting issues. For instance, due to Python's global > interpreter lock (GIL), only one thread can run at a given time. You'll > also > need to make sure that values modified by one thread is not misinterpreted > in other threads, and that two or more threads doesn't write to same > location at the same time and cause problems (the way to mitigate this is > using what's called "critical region" where only one thread at a time can > mess with an important variable or do some critical activity, quite beyond > the scope I think). > > Original: > I've not bothered posting the code since its custom to this board and will > fail on another python instance unless it has the the same hardware control > modules my board does. > > JL: If you are writing for an embedded board and want to really make sure > your code will work as intended, try starting from your desktop - write a > module that "simulates" the board (that is, write a console script) and > test > it on your computer before porting to your board. > > Really good advice here. I'll craft something new that has the flow I want without respect to the board specifics and see where that goes. > Good luck. Please let us know if you need more help. > Cheers, > Joseph > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading in python2.7
On Fri, Jan 2, 2015 at 3:18 PM, Alan Gauld wrote: > On 02/01/15 20:17, Rance Hall wrote: > >> I bought myself a pcduino 3 nano development board for Christmas and >> started picking up python again after a long forced hiatus. The board >> runs >> Ubuntu Precise >> > > Snap!(ish), I got an arduino Uno and RaspberryPi. > As I understand it the pcDuino is just a PC and > duino on a single board, right? > Yes, you are correct. Arduino Uno headers on the board, and a full pc on chip. For $40 (US) its was a no brainer for me. The OS is on an onboard 4GB Nand, and there is a mini sd card slot and a place to plug in a conventional sata hd. Not as many gpio controls on the board as a Pi, but with the easy addition of some arduino uno hardware that can be resolved. > > Working with development boards has me navigating the inner working of >> python threading and I find myself at a loss. >> > > You realise the 'duino processor is strictly single threaded? > Yes, I did, but it is a dual core cpu. > So you need a stateless event handler and keep the threading all within > Python. > > My current project is a simulated emergency vehicle warning system >> (otherwise known as a police siren) >> >> I started my project by writing the lighting and the sound portions of the >> program separately and tweaking them till I got each part working as >> desired. >> > > How are you writing the code? > Are you writing 'duino stuff in C and control stuff in Python? > How does the pcduino communicate between the controller and PC sections? > Do you still use the 'duino IDE? > > So far all the code is python. The good folks at LinkSprite have python modules that are board specific so I can get all the gpio controls by installing the board modules for python and doing a "import gpio" > In my set up I'd write the light/sound controller stuff and have > the event loop read signals from the Python code via the USB link. > Is it similar on the pcduino? > Mine isn't that fancy yet. I have three gpio pins one attached to a red led, and a second attached to a blue one. The third is controlling a buzzer. > > >> > > def setup_hardware(): >> do stuff here to setup hardware and gpio sytem >> >> def lights(): >> control the red and blue LEDs to simulate warning lights >> >> def sound(): >> control the sound to simulate a warning siren >> >> def cleanup(): >> exit cleanly and cleanup >> >> def main(): >> setup() >> try: >> # individually starting either lights or sound works as expected >> except: >> # catch keyboard interrupt and call cleanup >> >> >> > > Where is the event loop? > You need something to manage the events. > This is where my mistake happened. Sorry I wasn't clear here. The skeleton code I posted was from the working but not threaded starter attempt. My main looks like this def main(): setup() #thread management stuff here #start lights thread #start sound thread The idea here being that the lights and sounds can pulse on different frequencies/patterns independent of each other, but both loops running at what the user feels is "the same time. > > Each of the lights and sound functions are placed in a "while not >> exitFlag:" loop >> > > So the thread control is in two separate loops. > And each loop can set the flag? > > exitFlag variable made in the either the main loop or the cleanup loop are >> never seen by the lights and sound loop threads. > > > How many loops do you have? You have a light loop, a sound loop, a setup > lop and a cleanup loop? Sounds too many. > Got the word loop on the brain. Sorry about this. The main function and cleanup function are not loops just procedures. The loops are the light and sound loops and either the main or cleanup function should be able to set the criteria by which both the light and sound loops exit. > > The net effect of this is that once the threads are started, they are >> uncontrollable and I have to quit the terminal to get the threads to stop. >> >> Could someone please point me in the direction of a good tutorial or >> sample >> code that works that I could use as study tool? >> > > You should probably try the Ardiuino/PCduino forums > > I've not bothered posting the code since its custom to this board and will >> fail on another python instance >> > > But your skeleton is too skeletal for us to suggest anything. You don't > even show us where the threading
[Tutor] threading in python 2.7 - 2nd version
Thanks to the advice from Joseph and Alan, I hacked a quick python script which demonstrates my problem more accurately. Its not board specific as was my last code. This sample works the same on my pcduino as it does on my desktop. #threading problem example import threading import sys import time threads = [] exitFlag = 0 def delay(ms): time.sleep(1.0*ms/1000) def threadloop(): while not exitFlag: print "exitFlag value: ", exitFlag delay(2000) def cleanup(): exitFlag = 1 print "Exit flag value: ", exitFlag for t in threads: t.join() sys.exit() def main(): try: my_thread = threading.Thread( target = threadloop, args = () ) my_thread.start() threads.append(my_thread) delay(20) keypress = raw_input("Press a key and hit Enter to exit\n") cleanup() except KeyboardInterrupt: cleanup() main() the thread driven loop doesn't ever see the fact that the exitFlag as changed, based on the output to screen. Be warned, this code gives you an infinite loop, so be sure to run it in a terminal you can kill without impacting other work you are doing. There are many ways to work with theads. Class definitions, etc. The thread and threading modules. I've tried several and get the same results. What do I need to do to get the thread to stop based on the value of exitFlag? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading in python 2.7 - 2nd version
Cameron: This was the exact issue, and I had initially suspected as much, but had no idea how to fix it. I had tried to use the global variable directive, but as it turned out I had misused it. Anyway, everything is working as it should. Thanks Rance On Mon, Jan 5, 2015 at 12:39 AM, Cameron Simpson wrote: > On 04Jan2015 23:19, Rance Hall wrote: > >> Thanks to the advice from Joseph and Alan, I hacked a quick python script >> which demonstrates my problem more accurately. >> Its not board specific as was my last code. This sample works the same on >> my pcduino as it does on my desktop. [...] >> >> [...] >> exitFlag = 0 >> > > Ok. > > def threadloop(): >>while not exitFlag: >>print "exitFlag value: ", exitFlag >>delay(2000) >> >> def cleanup(): >>exitFlag = 1 >>print "Exit flag value: ", exitFlag >>for t in threads: >>t.join() >>sys.exit() >> > > These two hold your issue. > > threadloop() _does_ consult the global "exitFlag". But only because you > never assign to it in this function. So when you consult the name > "exitFlag", it looks for a local variable and does not find one, so it > looks in the global scope and finds it there. > > cleanup() uses exitFlag as a _local_ variable (like most variables in > python). This is because it assigns to it. Python will always use a local > variable for something you assign to unless you tell it not to; that (by > default) keeps the effects of a function within the function. Because of > this, cleanup does not affect the global variable. > > Here, it would be best to add the line: > >global exitFlag > > at the top of _both_ functions, to be clear that you are accessing the > global in both cases. So: > > def threadloop(): > global exitFlag > while not exitFlag: > print "exitFlag value: ", exitFlag > delay(2000) > def cleanup(): > global exitFlag > exitFlag = 1 > print "Exit flag value: ", exitFlag > for t in threads: > t.join() > sys.exit() > > Cheers, > Cameron Simpson > > Out of memory. > We wish to hold the whole sky, > But we never will. > - Haiku Error Messages http://www.salonmagazine.com/ > 21st/chal/1998/02/10chal2.html > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor