Re: [Tutor] combining c and python
another option that no one has mentioned yet is the use of 'ctypes' with existing C libraries: http://docs.python.org/library/ctypes cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "A computer never does what you want... only what you tell it." +wesley chun : wescpy at gmail : @wescpy Python training & consulting : http://CyberwebConsulting.com "Core Python" books : http://CorePython.com Python blog: http://wescpy.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
Thanks Stephen. That looks like nice clean code too! Cheers Pete On Tue, Sep 18, 2012 at 9:59 AM, Stephen Haywood < step...@averagesecurityguy.info> wrote: > Someone has already tried. https://github.com/sebleier/RDP > > > On Mon, Sep 17, 2012 at 5:50 PM, Pete O'Connell > wrote: > >> When I have a bit more time I am going to try to implement the >> Ramer–Douglas–Peucker algorithm to improve the accuracy of my curve >> simplification: >> >> http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm >> >> >> > -- > Stephen Haywood > Information Security Consultant > CISSP, GPEN, OSCP > T: @averagesecguy > W: averagesecurityguy.info > > -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Input handling?
On Sep 18, 2012 7:14 AM, "Steven D'Aprano" wrote: > > On Tue, Sep 18, 2012 at 12:04:22AM -0400, Dave Angel wrote: > > Somehow you managed to put your other message in its own thread, instead > > of adding to this one. > > Not all mail clients support threading, either when receiving or > sending. > > But my mail client, mutt, shows Scott's emails threaded correctly. > Perhaps your mail client is broken? What are you using? Mutt goes above and beyond a correct implementation of threading by using heuristics to fix broken threads. I know it parses the subject line but I'm not sure what else it uses. The upshot is that if you use mutt you won't see the same thing that would be shown in a client that has a strict interpretation of threading (unless you disable the heuristics in your muttrc). Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Input handling?
On Sep 18, 2012 10:02 AM, "Oscar Benjamin" wrote: > > > On Sep 18, 2012 7:14 AM, "Steven D'Aprano" wrote: > > Apologies for gmail screwing up your name. I wish I could use mutt on this machine. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple random string generator
On Tue, Sep 18, 2012 at 1:24 AM, Dave Angel wrote: > On 09/18/2012 01:15 AM, Mark Lawrence wrote: >> @Matthew Dalrymple I don't know what is happening but that's at least >> two messages from you that show precisely nothing, just original >> comments from others. Anyone else seeing the same thing? I'm reading >> through gmane on Windows Vista with Thunderbird. > > I had already sent Matthew a message advising him to hit enter (to get a > blank line), so that his remarks aren't just mixed in with the ones he's > replying to. If you look closely, you can find his remarks beginning > with the phrase "so what should be done..." You can recognize it the > lack of capitalization or paragraph boundaries. It's HTML: =3B so what should be done then would be to make sure that the start and end time are like this? =3B for n in range(10=2C 101=2C 5): =3B =3B =3B word =3D mkword(n) =3B =3B =3B start =3D time.time() =3B =3B =3B for i in range(1): =3B =3B =3B =3B =3B =3B =3B anagramSolutionX(word=2Cword=2C =3B =3B =3B end1 =3D time.time() =3B =3B =3B solu2 =3D end1 - start =3B is that right? sorry if im making this harder than it should be =3B to me that would make sense because you would be asking it to do it 1 times before it would move on to the end time? =3B thanks again for all the help guys it means a lot ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Storing information as attributes or in a dictionary
Dear all, Suppose I have a parser that parses information stored in e.g. an XML file. I would like to design a Python class to store the information contained in this XML file. One option is to create a class like this: class Record(object): pass and store the information in the XML file as attributes of objects of this class, as in >>> handle = open("myxmlfile.xml") >>> record = parse(handle) # returns a Record object >>> record.name "John Doe" >>> record.birthday "February 1, 1920" Alternatively I could subclass the dictionary class: class Record(dict): pass and have something like >>> handle = open("myxmlfile.xml") >>> record = parse(handle) # returns a Record object >>> record['name'] "John Doe" >>> record['birthday'] "February 1, 1920" I can see some advantage to using a dictionary, because it allows me to use the same strings as keys in the dictionary as in used in the XML file itself. But are there some general guidelines for when to use a dictionary-like class, and when to use attributes to store information? In particular, are there any situations where there is some advantage in using attributes? Thanks, -Michiel. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple random string generator
I wanna apologize about the emails...idk y some people can't see what I was saying...I'm just using my hotmail account...I did try to bold what I was saying but I guess that converts it to HTML I appreciate u guys being patient with me and I'm sorry for any trouble that had occurred due to this Thanks for all of your help Matt > To: tutor@python.org > From: breamore...@yahoo.co.uk > Date: Tue, 18 Sep 2012 06:15:49 +0100 > Subject: Re: [Tutor] simple random string generator > > @Matthew Dalrymple I don't know what is happening but that's at least > two messages from you that show precisely nothing, just original > comments from others. Anyone else seeing the same thing? I'm reading > through gmane on Windows Vista with Thunderbird. > > -- > Cheers. > > Mark Lawrence. > > ___ > Tutor maillist - Tutor@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] Storing information as attributes or in a dictionary
On 18 September 2012 15:14, Michiel de Hoon wrote: > Dear all, > > Suppose I have a parser that parses information stored in e.g. an XML > file. I would like to design a Python class to store the information > contained in this XML file. > > One option is to create a class like this: > > class Record(object): > pass > > and store the information in the XML file as attributes of objects of this > class, as in > > >>> handle = open("myxmlfile.xml") > >>> record = parse(handle) # returns a Record object > >>> record.name > "John Doe" > >>> record.birthday > "February 1, 1920" > > Alternatively I could subclass the dictionary class: > > class Record(dict): > pass > > and have something like > > >>> handle = open("myxmlfile.xml") > >>> record = parse(handle) # returns a Record object > >>> record['name'] > "John Doe" > >>> record['birthday'] > "February 1, 1920" > > I can see some advantage to using a dictionary, because it allows me to > use the same strings as keys in the dictionary as in used in the XML file > itself. But are there some general guidelines for when to use a > dictionary-like class, and when to use attributes to store information? In > particular, are there any situations where there is some advantage in using > attributes? > Some people find attribute access a bit "nicer" when they are using an object. Attributes have the disadvantage that they're a bit awkward if they aren't valid identifiers, where as any string is ok for a dictionary key. A valid identifier is any string... 1) consisting only of alphanumeric characters (a-z, A-Z, 0-9) and the underscore (_) 2) that does not begin with a numeric character (0-9). 3) and that is not a Python keyword (is, not, and, def, class, ...). To see what happens otherwise: >>> class A(object): pass ... >>> a = A() >>> a.class = 'some value' File "", line 1 a.class = 'some value' ^ SyntaxError: invalid syntax Also if your objects are instances of a class that has any methods you'll need to ensure that the method names don't conflict with the XML keys as well. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Input handling?
0) using mobile yahoo mail 1) expect: raw_input to assign the variable to "" 2)No output to stderr; has a loading symbol after hitting enter without input (tried both input and raw_input) Doesn't affect active interpreter. Only affects the screen with the run option. 3) def simulation(): import random from random import randrange from time import sleep print "Welcome to The World." sleep(1) print "Now Loading..." sleep(1.5) #Intro page print gender = raw_input("Gender: male/female \n") if gender != "male" or "female": gender = "male" Mlist = ["Jacob", "Mason", "William", "Jayden", "Noah", "Michael", "Ethan", "Alexander", "Aiden", "Daniel", "Anthony", "Matthew", "Elijah", "Joshua", "Liam", "Andrew", "James", "David", "Benjamin", "Logan"] Flist = ["Sophia", "Isabella", "Emma", "Olivia", "Ava", "Emily", "Abigail", "Madison", "Mia", "Chloe", "Elizabeth", "Ella", "Addison", "Natalie", "Lily", "Grace", "Samantha", "Avery", "Sofia", "Aubrey"] Llist = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Pavis", "Miller", "Wilson", "Moore", "Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez", "Robinson"] username = raw_input("Input your full name: \n") if username == "": if gender == "male": username = random.choice(Mlist) + " " + random.choice(Llist) else: username = random.choice(Flist) + " " + random.choice(Llist) print print "Username: %s. Gender: %s." % (username,gender) simulation()___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Input handling?
On Tue, Sep 18, 2012 at 1:23 PM, Scott Yamamoto wrote: > > 1) expect: raw_input to assign the variable to "" > 2)No output to stderr; has a loading symbol after hitting enter without input > (tried both input and raw_input) > Doesn't affect active interpreter. Only affects the screen with the run > option. > 3) Except for a bug your code worked for me in Python 2.7.3. > def simulation(): > import random > from random import randrange > from time import sleep You're not using randrange, plus you should move the imports to the top of your module. > gender = raw_input("Gender: male/female \n") > if gender != "male" or "female": > gender = "male" This is the bug. You have the test expression "expr1 or expr2" where expr1 is "gender != male" and expr2 is "female". A non-empty string is always True by definition, so your test amounts to "expr1 or True", which means the "if" block always executes. > Mlist = ["Jacob", "Mason", "William", ...] > Flist = ["Sophia", "Isabella", "Emma", ...] > Llist = ["Smith", "Johnson", "Williams", ...] Consider moving these lists out of the function. They can all go nicely in a dict, which you can set as a default argument. > username = raw_input("Input your full name: \n") > if username == "": > if gender == "male": > username = random.choice(Mlist) + " " + random.choice(Llist) > else: > username = random.choice(Flist) + " " + random.choice(Llist) Using a dict for the names eliminates the need to test gender here. For example: import random import time names = { "male": [ "Jacob", "Mason", "William", "Jayden", "Noah", "Michael", "Ethan", "Alexander", "Aiden", "Daniel", "Anthony", "Matthew", "Elijah", "Joshua", "Liam", "Andrew", "James", "David", "Benjamin", "Logan", ], "female": [ "Sophia", "Isabella", "Emma", "Olivia", "Ava", "Emily", "Abigail", "Madison", "Mia", "Chloe", "Elizabeth", "Ella", "Addison", "Natalie", "Lily", "Grace", "Samantha", "Avery", "Sofia", "Aubrey", ], "last": [ "Smith", "Johnson", "Williams", "Jones", "Brown", "Pavis", "Miller", "Wilson", "Moore", "Taylor", "Anderson", "Thomas", "Jackson", "White", "Harris", "Martin", "Thompson", "Garcia", "Martinez", "Robinson", ], } def simulation(names=names): print "Welcome to The World." time.sleep(1) print "Now Loading..." time.sleep(1.5) # Intro page gender = raw_input("\nGender [male|female]:\n").strip() if gender not in ("male", "female"): gender = "male" username = raw_input("Input your full name:\n") if not username: username = " ".join( random.choice(n) for n in (names[gender], names["last"])) print "\nUsername: %s. Gender: %s." % (username, gender) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Input handling?
Thanks for everything. I'll keep this in mind as I continue coding.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.
I teach at a college and I'm trying to use Python (2.6 because I'm running my tool in ArcGIS) to unzip a .zip file that contains one folder full of student folders, each with 1 or more submissions (zipfiles) that represent student submissions to weekly lab assignments. It all starts with an originalzip.zip (for example) that has a single folder (originalfolder) Within the 'originalfolder' folder there are anywhere from 1 - 40 folders (that are not zipped). (these are the students userid folders) Within each of the (1-40) student userid folders is anywhere from 1-10 zipfiles and perhaps a .pdf or .docx (if for example they submitted more than one revision of the assignment, there are more than 1) Folder Structure originalzip.zip --originalfolder --folder1 (w/ two zip folders) --internalzip1_a.zip --internalfolder1_a --files --folders --internalzip1_b.zip --internalfolder1_b --files --folders --folder2 (w/1 zip folder) --internalzip2.zip --internalfolder2 --files --folders --etc My goal is to: a) Unzip the 'originalzip.zip' b) go to the 'originalfolder' (the unzipped version of the originalzip.zip) c) go into the first folder (folder1) in the original folder and unzip any and all zipfiles within it d) go to the second folder (folder2) in the original folder and unzip any and all zipfiles within it e) continue until all folders within originalfolders have been checked for internalzips ### Note, I am a beginner both with this tutor environment and in python. I apologize in advance if my code below is 'not up to par' but I am trying to keep it simple in nature and use ample comments to keep track of what I am doing. I also don't know if I should post sample data (zipfile of a folder of folders with zipfiles), and if so, where? I have some code that works to extract the 'originalzip.zip', to an 'originalfolder' but it won't go to the folders (folder1, folder2, etc.) to unzip the zipfiles within them. It gets hung up on access to the first student folder and won't unzip it. I think it's a simple fix, but I've been messing with it for quite a while and can't figure it out. Code below: #1 Required imports. import os, os.path, zipfile, arcpy #2 I'm Utilizing 'GetParameterAsText' so that this code can be run as a tool in ArcGIS #2.1 in_zip is a variable for "What zipfile (LAB) do you want to extract?" in_Zip = arcpy.GetParameterAsText(0) cZ = in_Zip #2.2 outDir is a variable for "What is your output Directory?" outDir = os.getcwd() #3 Extracting the initial zipfolder: #3.1 Opening the original zipfile z = zipfile.ZipFile(cZ) #4 Extracting the cZ (original zipfile)into the output directory. z.extractall(outDir) #5 Getting a list of contents of the original zipfile zipContents = z.namelist() #6 Unzipping the Inner Zips: #6.1 Looping through the items that were in the original zipfile, and now in a folder... # ...For each item in the zipContents for item in zipContents: #6.2 Get the location (note the location is the 'outDir' plus what namelist() gave me) #(have never used 'os.sep', had to look it up, when someone suggested it) itemLoc = outDir + os.sep + item #6.3 Opens the first (2nd, 3rd, etc files) of the internal zip file (*.zip) z = zipfile.ZipFile(itemLoc) #6.4 Extract all files in *.zip in the same folder as the zipfile z.extractall(os.path.split(itemLoc)[0]) #6.5 determining the list of items in each students' folder student_lab = z.namelist() #7 THE END. Thank you for any and all suggestions/ fixes, constructive criticism and assistance with my beginner code! If you'd like to email me directly it's gwl...@uw.edu Regards, Greg Lund ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Border width of Canvas widget in tkinter
I am using Python 3.x I am unable to remove the border in a Canvas widget with bd=0 or borderwidth=0. Can someone please explain how one can do this? I give below my program class Avatar(Frame): def __init__(self,parent=None,width=100,height=100,ovalness=1,bgFrameColor='Blue',bgCanvasColor='Black'): Frame.__init__(self,parent,width=width,height=height,bg=bgFrameColor) self.grid() #self.grid_propagate(0) self.width=width self.height=height self.ovalness=ovalness self.bgFrameColor=bgFrameColor self.bgCanvasColor=bgCanvasColor self.canvas1=Canvas(self,width=width/2,height=height/2,bg=bgCanvasColor,borderwidth=0) self.canvas1.grid(row=0,column=0,ipadx=0,ipady=0,padx=0,pady=0) self.canvas1.grid_propagate(0) self.canvas2=Canvas(self,width=width/2,height=height/2,bg=bgCanvasColor,borderwidth=0) self.canvas2.grid(row=1,column=1,ipadx=0,ipady=0,padx=0,pady=0) self.canvas2.grid_propagate(0) self.draw() def draw(self): pass if __name__=='__main__': root=Tk() x=Avatar(parent=root) x.mainloop() when I run this program I can see a gray border on the two canvas objects. Alok___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stored variable
On Tue, Sep 18, 2012 at 08:21:41PM -0500, Lamar iVisionary wrote: > I cant figure out to get the stored variable / (Name) to appear when I > respond Hi , nice to meet you. name = raw_input("Good day stranger, to whom do I have the honour of addressing? ") print "Greetings and salutations %s, I hope you are well!" % name -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stored variable :p:
On 09/19/2012 09:21 AM, Lamar iVisionary wrote: Trying to write the following in Python. "Hi, my name is Bob!", .What is your name? print " Hi," + "my name is Bob!, strname= raw_input ("What is your name?") Hi ###, nice to meet you! I'm 18. How old are you? I cant figure out to get the stored variable / (Name) to appear when I respond Hi , nice to meet you. Any help with this would be great. Just a beginner here myself but I think you could just use a string substitution to accomplish what you want. For example: sayWord = "Ni!" print "The knights who say %s" % sayWord Will produce: The knights who say Ni! You can do it directly too but usually replacing with a variable gives more flexibility. thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Storing information as attributes or in a dictionary
On Tue, Sep 18, 2012 at 07:14:26AM -0700, Michiel de Hoon wrote: > Dear all, > > Suppose I have a parser that parses information stored in e.g. an XML file. You mean like the XML parsers that already come with Python? http://docs.python.org/library/markup.html http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/ Or powerful third-party libraries that already exist? http://lxml.de/index.html Please don't waste your time re-inventing the wheel :) > I would like to design a Python class to store the information > contained in this XML file. > > One option is to create a class like this: > > class Record(object): > pass > > and store the information in the XML file as attributes of objects of > this class That is perfectly fine if you have a known set of attribute names, and none of them clash with Python reserved words (like "class", "del", etc.) or are otherwise illegal identifiers (e.g. "2or3"). In general, I prefer to use a record-like object if and only if I have a pre-defined set of field names, in which case I prefer to use namedtuple: py> from collections import namedtuple as nt py> Record = nt("Record", "north south east west") py> x = Record(1, 2, 3, 4) py> print x Record(north=1, south=2, east=3, west=4) py> x.east 3 > Alternatively I could subclass the dictionary class: > > class Record(dict): > pass Why bother subclassing it? You don't add any functionality. Just return a dict, it will be lighter-weight and faster. > I can see some advantage to using a dictionary, because it allows me > to use the same strings as keys in the dictionary as in used in the > XML file itself. But are there some general guidelines for when to use > a dictionary-like class, Yes. You should prefer a dictionary when you have one or more of these: - your field names could be illegal as identifiers (e.g. "field name", "foo!", etc.) - you have an unknown and potentially unlimited number of field names - each record could have a different set of field names - or some fields may be missing - you expect to be programmatically inspecting field names that aren't known until runtime, e.g.: name = get_name_of_field() value = record[name] # is cleaner than getattr(record, name) - you expect to iterate over all field names You might prefer to use attributes of a class if you have one or more of these: - all field names are guaranteed to be legal identifiers - you have a fixed set of field names, known ahead of time - you value the convenience of writing record.field instead of record['field'] > and when to use attributes to store > information? In particular, are there any situations where there is > some advantage in using attributes? Not so much. Attributes are convenient, because you save three characters: obj.spam obj['spam'] but otherwise attributes are just a more limited version of dict keys. Anything that can be done with attributes can be done with a dict, since attributes are usually implemented with a dict. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor