Re: [Tutor] PHP
On Sat, Dec 4, 2010 at 2:27 PM, Kirk Bailey wrote: > and continue in html. What a BLOODY shame we can't do that in python, > which is otherwise awesome and superior in every way i can thus far > evaluate. As someone mentioned, you CAN do that in Python. However, you'll find that mixing content and processing logic isn't considered a best practice. Embedding simple display logic into your presentational templates is fine (looping, if conditional then display this section, etc), but you'll want to avoid mixing complicated logic in with presentation - it makes maintenance/testing/reuse that much harder, even moreso when working on a team with different skill specialties. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which non SQL Database ?
On Sat, Dec 4, 2010 at 5:42 PM, Jorge Biquez wrote: > Newbie question. Sorry. If it isn't you're on the wrong list :) > training so no pressure on performance). One application will run as a > desktop one,under Windows, Linux, Macintosh, being able to update data, not > much, not complex, not many records. The important details here are: simple data, low-volume. I'm assuming this is single-user (as in, each instance of your application has it's own DB) > The second application, running behind > web pages, will do the same, Is this multiple users, each accessing the same DB? That really changes what you are looking for. If you are dealing with single-user, or only a few users, I'd say look into SQLite - It uses SQL syntax but doesn't run as a server and stores the database as a single file. It's great to use in small projects because the syntax is the same as larger projects, and you can replace with a full-blown multi-user SQL DB if you ever need to without having to rework everything. It's also very simple to use. I believe SQLite (sqlite3) is part of the core library in recent Python versions, or available as a package for older pythons. Berkeley DB is pretty much interchangeable with SQLite in terms of functionality. I much prefer SQLite. If your web application intends to have multiple users interacting with the same data, neither is probably a good fit. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python
On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano wrote: >> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. > > I sympathize with you. I wonder who thought that building a 1GB XML file was > a good thing. XML is like violence: if it isn't working, try more. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trying to parse a HUGE(1gb) xml file in python
On Mon, Dec 20, 2010 at 5:32 PM, Steven D'Aprano wrote: >> XML is like violence: if it isn't working, try more. > > I love it -- may I quote you? I can't claim credit for it, I saw originally saw it on some sigs on Slashdot a few years ago. It certainly matches the majority of XML usage I've encountered. As to the original post: Yes, as others have suggested you're going to want an event-based parser along the lines of SAX. Sadly (for you) this means a mental shift in how you address your code, but it's not terrible - just different. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] vim as a python editor
On Wed, Dec 15, 2010 at 7:30 PM, Alan Gauld wrote: > I also use split screen view in vim so that within vim I often have > two or three buffers open at once all displayed in a split screen. I know you prefer "default" settings, but one mapping I tend to stick in my .vimrcs whereever I go: nmap w_ Which moves to the next screen and maximizes it. = will return to the equally distributed split screens. When coding an AJAX app, for example, I'll often have the HTML, the JS, the CSS, and the Python service, and the tests all up at once. I also use "screen" a lot because I'm often working on remote machines without X involved, plus it lets me bounce between shell (docs, git, etc) and editing easily with the added benefit of not having to worry about communication disruption (local power outage, etc) killing my work in progress. Often once I get an environment perfectly tailored (yay for virtualenv) I'll have a screen session manage the entire thing until the project is done, just reconnecting to it when I'm working on it. I highly recommend both screen and virtualenv to anyone that is unfamiliar with them. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Choice of Python
On Tue, Dec 28, 2010 at 7:38 AM, Abdulhakim Haliru wrote: > Unlearn my php ways (as that must certainly happen J) and learn python and > web development woth python, Jquery etal ? ... > Is python really worth the pain or should I just skip it ? If you accept that you'll continue to learn new syntaxes throughout your career, then the "pain" of python isn't notably different than any other language (though I have to constantly reteach myself not to use semicolons :) ). You aren't "unlearning" so much as "meta-learning" That covers the "pain" half of your equation. As for the Python half --- Python is pretty widely used in a variety of ways. It's probably the most common plugin language for outside utilities (not that anything is particularly standard in that landscape). I've noticed that new language development often compares against Python. Scientific fields work more and more with Python. Python won't be your last stop, but it's a valuable one. As to HOW valuable, that depends on a bunch of life details we don't know. If you want one language to focus on and use exclusively for several years, Python may or may not be the best choice depending on your field. If you want a useful tool that will improve your understanding of other tools in addition to being useful in its own right, Python is absolutely a good choice. All that said, I doubt you'll find many Python Nay-sayers on the Python Tutors mailing list :) -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Choice of Python
On Tue, Dec 28, 2010 at 3:06 PM, Jeff Johnson wrote: > Webfaction supports long processes and that is why they are the largest > Django hosting site. They support a ton of software, too. SVN, Trac are > two I use. I didn't see git hosting among their software. Is it available without hoop-jumping? -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] syntax error that i cant spot!
On Sat, Jan 1, 2011 at 9:10 PM, Alan Gauld wrote: > Why avoidCamelCase? I actually prefer it to using_non_camel_case The readability is an often argued topic - I myself find the space of names_in_underscores to be more readable (words are distinct), but I try to follow the common conventions of the languages I'm using, and Python appears to use camelCase more than not. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regex question
On Tue, Jan 4, 2011 at 10:37 AM, Richard D. Moores wrote: > regex = ".*" + search + ".*" > p = re.compile(regex, re.I) > > in finding lines in a text file that contain search, a string entered > at a prompt. That's an inefficient regex (though the compiler may be smart enough to prune the unneeded ".*"). Just having "search" as your regex is fine (it will search for the pattern _in_ the string, no need to specify the other parts of the string), but if you're not using any special regex characters you're probably better off not using a regex and just using a string operation. Regexes are great for trying to do powerful and complicated things - and as such may be too complicated if you're trying to do a simple thing. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regex question
On Tue, Jan 4, 2011 at 11:07 AM, Richard D. Moores wrote: > A file has these 2 lines: > > alksdhjf ksjhdf kjshf dex akjdhf jkdshf jsdhf > alkdshf jkashd flkjdsf index alkdjshf alkdjshf > > And I want the only line that contains the word "dex" Ah! Then you want a slightly different Regex pattern. .*dex.* would match both lines. You'll need to be more specific when you say "the word dex" to determine your regex. \sdex\s will match dex surrounded by whitespace, which might be what you want. \bdex\b will match dex surrounded by word boundaries, which is probably the best pick. -- Brett Ritter / SwiftOne US200300 swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginning Python and other resources (was Re: SlicingTuples)
On Tue, Jan 4, 2011 at 4:50 PM, Patty wrote: > Hi David - I was looking for the book you recomended below - "Python 3 > Object Oriented Programming" by Dusty Phillips - and found it on Amazon for > $43 new on up and $70 for used but maybe that was hardback? Do you happen > to know of some other way to obtain it for less than $45? Half.com is always my stop of choice for programming books. I see a copy there for $16. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ideas for a simple program
On Tue, Jan 25, 2011 at 12:25 PM, walter weston wrote: > can I have some good ideas for simple programs, I have come to realize the > hardest part of programming is not having a project to do and having to > think of one haha There are two ways to approach this: 1) Think of something that you'd like to have, personally, and start work on it. Here you'll have a real passion and benefit. 2) Tackle something that everyone does. Here you'll have a lot of examples and people will likely quickly understand your goals. For #1 I can't help, though I recommend the attempt, but for #2 I recommend small games. A blackjack game or a computer that can play tic-tac-toe with you are great ways to work on a well-defined problem that won't take forever. Once you feel sufficiently confident, helping on an open source project as others suggest is a great way. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ideas for a simple program
On Tue, Jan 25, 2011 at 10:18 PM, Steven D'Aprano wrote: > Or a program to find anagrams. A great exercise in learning how different string operations perform is to search a text file for the largest palindrome string. I recall running one on the Constitution where a good program could do it in seconds and a bad program would take hours (if it didn't run out of memory first). -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python packaging systems
On Thu, Feb 10, 2011 at 11:14 AM, Alan Gauld wrote: > Personally I don't like them and prefer to install a version of Python > and then install my modules separately. > > If you want to hide your code(a little bit) you can ship only the pyc ... Don't forget that the motivation need not be obfuscation. I myself like to package standalone programs for non-techie friends and family to use. For them, "Run this" is a good first instruction versus "Install Python..." (regardless of how much their lives might be improved by the latter :) ) -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python3 Tutorial question about a[:] = []
On Wed, Feb 16, 2011 at 12:39 PM, Richard D. Moores wrote: > from <http://docs.python.org/py3k/tutorial/introduction.html#lists> : > >>>> # Clear the list: replace all items with an empty list >>>> a[:] = [] >>>> a > [] > > I've been using >>>> a = [] >>>> a > [] > > What's the difference? a = [] This sets the variable "a" to refer to a new list a[:] = [] This replaces the existing content of a with empty contents. Check out this example in interactive mode >>> a = [1,2,3] >>> b = a >>> a = [] >>> print a [] >>> print b [1, 2, 3] Versus: >>> a = [1,2,3] >>> b = a >>> a[:] = [] >>> print a [] >>> print b [] -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replying
On Mon, Mar 28, 2011 at 1:04 PM, wrote: > When replying to the mailing list, does everyone just hit the reply button in > your email program. Because that sends the email directly to your email. Also > everyone is cc'ng the mailing list; is that the exceptable way to reply so > everyone in the list gets the replies? Fully functional email clients (generally only found on Linux/BSD) have a "reply to Group" function that works as intended. Most email clients lack such ability, so the process on this list is to use "Reply to All", which CC's the list as you describe. How email lists function is an oft-debated topic. Many (most?) lists will make the "reply-to" header in the email reflect the list address (instead of the original sender), so a simple "Reply" will go to the list. However, the "Reply To" header is not intended by the email RFC to function this way. It ends up in a battle of "follow the spec as intended" vs "follow the generally expected results". You can read much on this debate by googling for "Reply To munging harmful" and "Reply To munging useful", but further discussion is definitely outside the scope of this list. Technical mailing lists and/or long-existing mailing lists will often take stances on subjects such as reply to, top-quoting, quote trimming, signatures, plain text vs HTML, etc because many of those standards came about from a time when communicating detailed and in-depth topics over email to a large group was pretty much the only way to communicate if not in person. Today's web-forum lol outlook-trained populace is generally ignorant of such reasonings, but trust me, the rules have a reason beyond techie elitism for existing. (*steps off soapbox*). You can google "Eternal September" for more on that general topic. TL;DR Version: Yes, that's perfectly acceptable. *bites tongue to resist going off on a rant about TL;DR* Hope that was helpful! -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learnpython.org - Free Interactive Python Tutorial
On Thu, Apr 21, 2011 at 10:23 AM, Greg Werner wrote: > Hi Ron may you share the link? I have been using another fun one: > www.pyschools.com, although I would love to have another option. On 20/04/2011, at 20:20, tee chwee liong wrote: > this is great for beginners like me. Could you pls provide the link. tq To all: The link is in the subject line: http://learnpython.org -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python assignment
On Mon, May 9, 2011 at 6:42 PM, Matthew Rezaei wrote: > I have a python assignment and I need help regarding it, is there anyone > that can help me out with it? The answer you will inevitably get is something like: Hi, we're happy to help you understand the problems you're encountering as you work on your assignment. In order to help with that: * Tell us the specific problem you are trying to solve * Show us the code that is giving you a problem (succinct!), including a cut-and-paste of the exact error message you get (if it's an error as opposed to unexpected results) * Give a clear question to answer. If you haven't gotten this from someone else, then consider this that response :) -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing output from Python program to HTML
On Tue, May 10, 2011 at 8:16 AM, Spyros Charonis wrote: > newline = line.replace(item, " item ... > The Python compiler complains on the line I try to change the font color, > saying "invalid syntax". Your issue here is not importing libraries, but your quotations. When you get to "red", it takes that first double quote as ENDING the string of HTML that starts with ". It doesn't know that you're putting quotes inside your string, as the only way it knows when a string ends is when it reaches the quoting character that matches the beginning. The easiest solution is to use 'red' (using single quotes) as HTML accepts both. newline = line.replace(item, " item ") As alternative you could use a different quoting for your string (note the single quotes on the outside): newline = line.replace(item, ' item ') Alternatively you could escape your string. This tells Python that the quotes are NOT ending the string, and the backslashes will not appear in the resulting output: newline = line.replace(item, " item ") Note that the first option works because HTML accepts single or double quotes for it's attribute values. The second option works because single and double quotes for strings work the saem in Python (this is not true of all languages). The third option is pretty standard across languages, but can be annoying because it becomes harder to cut-and-paste strings to/from other places (for example, template files, raw HTML files, etc). Hope that helps! -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] folder and file list
On Tue, May 10, 2011 at 8:17 AM, Tommy Bell wrote: > scandir('c:\tmp') > this doesnt work, I know why - but i dont know how to fix it. > The reason it doesnt work is because isfile requires a file, but current > contains a path. Not quite. Stick a "print path" as the first line in scandir. Notice that it doesn't print out c:\tmp The issue is that Windows (Well, DOS, back in the day) decided to use backslashes as the path separator, where the rest of the world (mostly) used slashes. This meant that most programming languages use backslashes to "escape" characters to have special meaning. Putting "\n" in a string puts in not an "n" but a newline character. \t is a tab. This causes you (and many other windows programmers) a little bit of distress today, in many programming languages. To have your string recognize that your backslash is an actual real backslash you can escape it: scandir('c:\\tmp') After that your code should work fine (it runs on my system, but I'm not on windows). This filepath issue has many details you can look up or ask about, but this should get you started. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ 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 8:41 AM, louis leichtnam wrote: > 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? Assuming that you're talking about a desktop application rather than a web-based application, I'd start by googling TkInter and going from there. There are alternatives, but that's well-supported and widely used. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python program with multiple answers
On Wed, May 11, 2011 at 6:49 AM, Johnson Tran wrote: > I've been working on a Python program where I create an 8 ball that will > allow you to ask questions and will reply back with 20 possible answers. ... > if anyone could point me in the right direction it'd be really helpful Answer cloudy, try again later [couldn't resist] Patrick gave a decent summary. I'd suggest for learning purposes take each step at a time: 1) Save the questions, then once that works: 2) Save the corresponding answers, then 3) Print them, then 4) Sort them That way if you encounter any problem it's limited in scope rather than trying to take it all in at once. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python file upload
On Friday, May 13, 2011, Rayon wrote: > Hi all, I need a python script to upload a xml file to a web page using > python. I have tried using pycurl but I am still getting Fyi, the XML spec allows for no characters or whitespace before the XML declaration, that could be your error, -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] upload xml file via pycurl
On Mon, May 16, 2011 at 2:00 PM, Rayon wrote: > I am trying to upload a xml file via pycurl, I really need some help here. What is the error your are receiving? Did you fix the fact that you had starting whitespace as I mentioned to your last email? -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Interview Questions..
On Fri, May 27, 2011 at 12:51 PM, Marc Tompkins wrote: > That was the point that Joel and I were making. The CS programs that have > become Java schools now make the curriculum as easy as possible because they The concept that knowledge/ability to use a language doesn't indicate quality is one I agree with. however: > enough, was: Professional programmers - I really supported the effort, years > back, to protect the term "software engineer" - should be familiar with the > ins and outs of computers, not just with the quirks of the language they are > employed to use. To use my dishwasher analogy from a moment ago, I'm sure Here I disagree. A certain level of base knowledge beyond the requirements of your language is required, true, but a) I think that can be taken too far. I suspect a ton of truly great programmers have never have to memalloc() and they are still good. b) I think this is placing the cart before the horse. To expand on that second point, I see a good programmer as someone that thinks abstractly, that can bounce between big picture and details, that considers concepts like reuse and flexibility without extra effort. They are lazy enough to want to take advantage of existing libraries and diligent enough to fix things the first time. They have curiosity and insight. A person like that will, in time, learn enough about the environment and foundations of their tools to reap all possible benefit from it. Studying those foundations will not make you one of those people, nor will testing for knowledge of those foundations necessarily find you one of those people. And, frankly, I suspect a great many of those people will never wrestle with when exactly their compiler performs tail call elimination. But that's just my suspicion. > There were a couple of other points I wanted to answer, but I'm out of > time. It does seem that we mostly agree - certainly we agree that Java > stinks! One issue I've not seen discussed is some of the specific habits the language encourages. I've never been one to trash a language, believing it's a poor workman that blames his tools, and that almost all tools have their strengths, but having worked with Java (and Java developers) for a while now I've really come to dislike some of the practices that are becoming common: Stacked patterns without understanding the purpose, premature and excessive abstraction, elimination of verbs, and horrendous naming habits. I'm curious to see if any of these habits change if/when Java adds functions as first-class objects. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strategy to read a redirecting html page
On Thu, Jun 2, 2011 at 4:06 PM, Robert Sjoblom wrote: > Python already has the urllib/urllib2 package that automatically > follow redirects, so I don't see why you'd need a 3rd-party module to > deal with it? When it encounters a 301 status code from the server, Ah, but I believe the issue is that the server isn't giving him a 301, it's giving him a 200 with a page explaining that "this page has moved, please update your bookmarks" etc, with a META HTTP-EQUIV tag to redirect the page after N seconds. (Based on the part where the OP said "I get the first warning/error message page and not the redirection one.") -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Q
On Wed, Jun 8, 2011 at 3:25 PM, Vincent Balmori wrote: > In the "Loop of Game" section of my code for some reason it gives me five > more chances than I wanted it to. When I put two as the chance limit, it > allowed seven. Also, the program will always say "yes" to any letter I > enter, even if it's wrong. Let's take a look. I'll comment on a few parts that aren't the cause of your problems. > WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") > # pick one word randomly from the sequence > word = random.choice(WORDS) > # create a variable to use later to see if the guess is correct > correct = word I'm unclear on the purpose of this step. You never again use word, so why have correct and not just use word? > LETTERS = None > if correct == "python": > LETTERS = "python" LETTERS always ends up == correct (and thus == word). Why not just use correct (or word)? > chances = 0 > while chances < 5: > guess = input("Does the word have a: ") > for letter in correct: You are looping over every letter in correct. Why? Don't you just want to loop over my 5 chances? > if letter.lower() in LETTERS: Here's your other issue. What is letter? Based on your for loop, it's one of the letters in correct. What test do you actual intend to do here? Say it in english and the python code should be fairly obvious. > print("\nYes") > chances += 1 > guess = input("\nDoes the word have a: ") > else: > print("\nNo") > chances += 1 > guess = input("\nDoes the word have a: ") Just as a tip you've got a lot of repetition here. If you move the "chances" line and "guess" line outside of the if/else, your program will be shorter and cleaner. > print("\nYour chances are up!") You want this to occur when your chances are up, right? So why is it _inside_ the while loop? Your basic approach is fine, you just have some extra stuff going on unnecessarily and I'm guessing it's confused you as to your logic. Give it another shot and let us know how it goes. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating Point Craziness
> dictionary = {3.1014: value, 2.1005: value, > 1.0999: value} > Why is this happening? The output is telling me 3.1, but the value isn't It's a quirk of how computers store floating point numbers. While humans mentally tend to treat everything as characters (and thus "3.1" is three character: a "3", a ".", and a "1") the computer internally stores everything as bytes (which are basically numbers), and we have a character set that says that such-and-such number can represent "A" or "B", or even "3". For the purposes of efficiency, actual numbers can be STORED as numbers. This is the difference between an "integer" value and a "character" value - not what is stored, but the stored number is interpreted. Internally it's all represented as binary numbers = sums of bits that represent powers of two. So 111 = 64+32+8+4+2+1 which is 110 (assuming the math I just did in my head is correct, but you get the idea) (Note that the Python Virtual machine is another layer of translation above this, but that's irrelevant to the basic point) Okay fine, so "1024" stored as a number only requires 10 bits (binary digits) to store, while "1024" as a string is 4 characters, requiring (at least, depending on your character set) 4 bytes to store. None of this explains what you're seeing. So how is a floating number stored? Say, 0.5? The short version (you can google for the longer and more accurate version) is that the decimal part is stored as a denominator. So 0.5 would be 2 (because 1/2 = .5) and 0.125 = 8 (because /18 = 0.125) and .75 would be the 2 bit and the 4 bit (because 1/2 + 1/4 = 0.75) That works great for powers of two, but how do you represent something like 0.1? 1/10 isn't easily represented in binary fractions. Answer: you don't. The computer instead gets the best approximation it can. When you deal with most common representations, you never notice the difference, but it's still there. Floating point math is an issue for all programs that require high precision, and there are additional libraries (including in Python) to deal with it, but they aren't the default (again, both in and out of Python) for various reasons. In your case I suspect you'll just want to use a format to output the number and you'll see exactly what you expect. It only becomes a problem in high-precision areas, which 0.1 increments don't tend to be. Hope that helps! -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using python to write web page
On Wed, Jun 15, 2011 at 8:46 PM, naheed arafat wrote: > Or there is any python module to get this job done? The normal way this sort of thing is handled is that the HTML is in some form of template, where the tags you are talking about editing are variables. A common example is a web-based form where you want to present either a blank form or the same form pre-populated. So rather than generating the HTML, then editing it to be different you instead simple set the variables and generate the HTML and the result reflects whatever the variables were set to. There are multiple modules for such templates. This is assuming that you generate the HTML in the first place instead of trying to edit someone elses existing HTML. In that case you'd proceed as you described. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nitinchandra rubbish on list
On Sun, Jun 19, 2011 at 11:33 AM, Alexandre Conrad wrote: > 2011/6/19 Steven D'Aprano : >> Stop replying to the sender, and the problem will probably go away. > I always "reply to all", which replies to the sender and CC the list > (just like this email) and I can't recall of such behavior. All works > fine with me. I've had this as well, and as Steven says, it only occurs with a partiuclar recipient. If you send to the list only (or in fact anyone other than that person), you'll not receive this. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using a dict value in the same dict
On Tue, Jul 5, 2011 at 3:40 PM, Walter Prins wrote: >> So the trick is to define the dictionary in separate sessions, not at >> once. > > What do you mean, "seperate sessions, not at once"? He means you can't say: d = {a: "1", b: d["a"]} Which is correct. To set one value based on another they must be set separately. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List methods inside a dictionary of list
On Mon, Jul 11, 2011 at 9:26 AM, Rafael Turner wrote: > I am playing lists and dictionaries and I came across this > counter-intuitive result. > >>>> d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) ... >>>> d['a'].__setitem__(0,4) ... > > I was not expecting all the keys to be updated. Is there any > documentation I could read on how different datatypes' methods and > operators interact differently when inside a dictionary? I would also > like to find a way of being able to use list methods in side a > dictionary so that As has been mentioned, this isn't the dictionary doing anything weird, this is that "8*[[0]]" gives you a list of 8 references to the same list. You can play with just that part and see that that's the source of your issue. To achieve what you are trying, try this instead: d = dict([(x,[0]) for x in ['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j']]) Can you understand how this behaves differently than 8*[[0]] ? Check the Python docs for array multiplication if you're confused, but the basic idea is that "[0]" isn't getting evaluated freshly for every piece in the array for 8*[[0]], but in a list comprehension it is. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mailing list documentation
On Sun, Jul 31, 2011 at 8:42 PM, Christopher King wrote: > Wow wow, way is a ser...@might.co.za person doing a tutor response. Assuming you mean _why_ (not _way_) , Sergey was simply quoting the introductory email we all get when we join, as it answered the original posters question. There is nothing of concern here. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When to use def __init__ when making a class?
On Tue, Aug 2, 2011 at 8:47 PM, brandon w wrote: > 1) When should I use "def __init__(self):" when I create a class? When you have any initialization to do. (in other words, when you want class instantiation to do more than simply give you an instance of the class. > 2) Would these two classes have the same effect? Neither of these two classes compile. You should try what you are asking before asking, it will lead you to better questions. Your examples show some misunderstanding about Python classes. 1) an __init__ method shouldn't return anything. The Class construction call will already return an instance of the class, __init__ is just about initialization. 2) A class definition likewise has no return value. 3) In your examples, Marcus and Jasmine are either intended to be strings (and should be quoted), or are variables you didn't provide. Here, try running these examples and see what you can figure out. Experiment with them a little, then come back with questions on what you observe. class Name: def __init__(self): self.man = "Marcus" self.woman = "Jasmine" instance = Name() print instance.man # Python 3 will use a different print syntax class Name: pass instance = Name() instance.man = "Fred" print instance.man -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Standard way to create debian packages for python programs?
On Thu, Aug 18, 2011 at 11:39 AM, Mac Ryan wrote: > In particular, `distutils` is part of the standard python > distribution and it is in no way specific to Debian, so I don't feel > this is the wrong place to ask, although it's totally ok if tutors feel > they aren't competent enough on the subject to give proper feedback. :) Are you looking for distutils advice, or .deb advice? They are pretty different. How are you expecting your users to install your package? Via easy_install? Apt-get? Something else? -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Intro
On Mon, Aug 29, 2011 at 11:26 AM, Cranky Frankie wrote: > Hi Python tutor listers, as an intro I'll repost what I sent to my > Linux groups, since one of their members turned me on to you: Glad you're so happy with this book...did you have a question for this list? -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Strange zip syntax
I ran into this article ( http://blog.adku.com/2011/09/hodgepodge-of-python.html ) and found myself temporarily stymied by one line it in: zip(*[iter(a)]*2) Used like this: >>> a = ['a','1','b','2','c','3'] >>> zip(*[iter(a)]*2) [('a', '1'), ('b', '2'), ('c', '3')] While I'm unlikely to use such a construct (if I can't easily follow it now, I or my successor likely won't follow it should it need to be debugged sometime in the future), I found the education I got in deciphering it was worth the effort. I'm sharing it here so others can benefit from my puzzlement. iter(a) returns a list iterator for a. See help(iter) for more. [iter(a)] is a list containing one element, an iterator. This is created only so we can do the below: [iter(a)]*2 is a list containing two elements, each the SAME list iterator. For simplicity, let's break this out for further analysis: >>> b = iter(a) >>> c = [b,b] *[iter(a)]*2 flattens the list when passed into a function call. Using our more verbose but simple syntax: *c. This only works when passed to a function. zip() creates tuples each holding the Nth elements from a number of sequences. See help(zip) for more. Thus, zip(a) or zip(a,a) would return: >>> zip(a) [('a',), ('1',), ('b',), ('2',), ('c',), ('3',)] >>> zip(a,a) [('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')] What happens when we pass an iterator to zip? That's not mentioned in the docstring blurb. >>> zip(iter(a)) [('a',), ('1',), ('b',), ('2',), ('c',), ('3',)] Answer: It works as intended. Now we come to the magic of this little snippet. zip(iter(a),iter(a)) wouldn't work, because each call to iter(a) returns a DIFFERENT iterator. >>> zip(iter(a), iter(a)) [('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')] But by creating the list of two elements each of which is the SAME iterator, as each is asked to iterate it advances the common element indicator: >>> zip(*c) [('a', '1'), ('b', '2'), ('c', '3')] Notice that the flattening is required, because zip needs to get multiple arguments: >>> b = iter(a) #our original iterator is spent, so we're assigning a new one >>> c = [b,b] >>> zip(c) #Not flattened, is just a single list, like a. [(,), (,)] >>> zip(b,b) # here it is two iterators sent to zip() (though they happen to >>> be the SAME iterator) [('a', '1'), ('b', '2'), ('c', '3')] I hope some of you enjoy playing with this, and hopefully someone learned something useful! While I'm not likely to use the listed form, I can very well see myself saying: >>> a = ['a','1','b','2','c','3'] #well, I can see myself using this with >>> meaningful variable names >>> b = iter(a) >>> zip(b,b) # Group in sets of 2 elements -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Logging to different directories
I commonly prefer to have my log files in //MM/DD/ directories (it allows for lots of fun command-line actions). I've not yet sussed out a nice way to do this with the logging module. Before I ask the main list, is there a fundamental feature I've missed? The TimedRotatingFileHandler will let me write to filenames based on date, but it won't alter the path (in fact, the entire logging module seems to be built on the assumption of no subdirectories). Have I missed something? -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Return T/F vs print T/F
On Sat, Feb 4, 2012 at 11:42 AM, Sivaram Neelakantan wrote: > On Sat, Feb 04 2012,bob gailer wrote: > >> On 2/4/2012 9:38 AM, Sivaram Neelakantan wrote: >>> While trying out code, I have trouble following the difference between >>> >>> return True vs print True and the same with False. If I use return >>> for the True/False statements, nothing gets printed. Why? >> >> Why did you expect something to be printed? > > err...return sends something back as in True or False? Yes, it returns it to the caller. It doesn't print it. When using the interactive mode, the program that creates that mode prints the return. When you are using it your program, the caller decides what to do. The flip side is that printing a value does NOT return it. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What the difference between the two RE?
On Thu, Feb 9, 2012 at 1:15 AM, daedae11 wrote: > import re > re.match("^hello", "hello") > re.match("hello", "hello") > > Please give a string that matches RE "^hello" but does not match RE "hello", > or matches RE "hello" but does not match RE "^hello". In addition to the other answers, it's important to note that nothing in these REs require it to be a "word". So both of the above will match the "hello" in "hellonwheels" for example. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] specific recommendation for a Python book, to move from baby-level to intermediate-level
On Wed, Feb 15, 2012 at 7:46 AM, Joel Goldstick wrote: > Programming is all about doing it -- over and over. I think Malcolm > Gladwell proposed that it takes 10,000 hours to get good at anything. > Its great to be smitten, but there is no shortcut. Jumping in because this is a favorite topic of mine. The 10,000 hours has been followed-up on a lot, and as a I recall (I'm no expert) it varies considerably. Experts (meaning that they have an intuitive approach to problems) can arise in much less time, while others (with a rule-based understanding only) can put in far more than 10,000 hours and still not become experts. What I've found works for me personally is to combine _doing_ (which is essential and unavoidable) with _variety_ in the tasks and also to google the heck out of "best practices", "anti-patterns", and "tips" on the topic. Make sure to get a feel for the reliability of the source of these tips, and don't trust them blindly, instead try to understand them. For example, there are three levels of progression in handling how Python uses "self" in objects: 1) This is stupid, other languages are smarter than this. 2) I do it without worrying about it because I'm used to it, but I don't really get why it works that way. 3) I understand how "obvious" alternatives won't work as Python is modeled and have come to appreciate the way it works. You can't jump straight to #3, but if you know it's there you can regularly poke at the issue as you're otherwise learning (via doing) and get closer to a higher understanding more quickly than writing 10,000 hours of "To Do" lists will get you there. Other common enlightenments to strive for: * I grok list comprehensions * I understand generators * I understand decorators (where understand is more than "can use") -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] breeds of Python .....
On Sat, Mar 31, 2012 at 5:37 PM, Barry Drake wrote: > concentrate on Python3 or stay with Python2 and get into bad habits when it > comes to change eventually? Apart from the print and input functions, I > haven't so far got a lot to re-learn. My recommendation is to go with Python2 - most major projects haven't made the switch and I'd expect another year or two before they do so. Many tutorials and examples are Python 2-based and there are not that many differences to unlearn in terms of habits. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finally without try or except
On Mon, Jul 30, 2012 at 12:20 PM, Tino Dai wrote: > Yes, but that would involve surrounding the entire method with a try except > finally block. I was > told by the Python-Guru-In Residence that shouldn't emulate Java code in > Python, and that was > generally bad programming practice (no flame war intended) It is true that you should use the idioms of each language in their domain. That doesn't mean you should ignore language features entirely. Wrapping everything in a single try/finally to handle this exception is as intended and not emulating the Java style of wrapping everything everywhere (be that good or bad). -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simon says
On Mon, Sep 10, 2012 at 12:24 PM, Matthew Ngaha wrote: > thanks for your input. i understand Simon Says. I'm just struggling to > see the connection with the assignment. a sequence of colours and > sounds using the keyboard. I 'm trying to understand how this relates > to the game This seems less about "simon says" the social game, and more about "simon", the 1980s electronic toy. That toy had four colored buttons that would light up in a sequence, playing a tone for each color. The player would then have to press the buttons in the same sequence. The game would then replay the sequence and add an additional step. Player would have to enter the longer sequence in. Continue until player loses (and game made an angry buzz). It was quite addictive. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question
On Thu, Sep 27, 2012 at 10:18 PM, jh wrote: > The subtotal of your items is: 26010.8502 > > My question here is, why does my subtotal have so many decimals when I never > went above 2 in my input? This is not actually a Python thing, it's a computer thing. Computers represent numbers (everything) in binary, as you doubtless have heard. The issue is that while 1 or 12 or 4562 are easily represented in binary, a number like "0.1" is less obvious. Floating point numbers are stored as binary approximations that dont' work out to exactly the same thing. (For _most_ purposes they are close enough, but if you are every dealing with highly precise math, there are libraries to help be more accurate) This is similar to how 1/3 can't be represented easily in decimal form. (3 is hard number to use as a divisor in base 10. It turns out that most digits are painful to use as divisors in base 2 (binary) except for 1,2,4, 8, or other powers of 2.) -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lotka-Volterra Model Simulation Questions
On Sat, Sep 29, 2012 at 2:16 AM, Alan Gauld wrote: > As to using short names to keep things on a single line, there is a huge > body of research in Comp Science that shows that meaningful names outweigh > single line expressions every time in terms of reliability, comprehension, > ease of maintenance etc. With the nod to what exactly is meaningful vs noise, I'm in subjective agreement. Can you point to any of the research you mention? I'd like to read into to see how my personal experience equates with the overall study - I might learn something! One point of curiousity for me: in Perl there was a attempt a decade ago to promote a change in how hashes (dicts) were named to better match their usage (that is, rather than having a hash named for the collection, e.g. %addresses, have your hash named to match the singular usage: %address_of, which leads to $address_of{$student} ). No idea if that caught on or not, as I spent a few years trapped in Java, where the trend is to disguise everything in a mass of Verbed Nouns. Googling coughed up this link ( http://infoscience.epfl.ch/record/138586?ln=en&of=HD ), but I'm awash in results about general discussions of variables in research rather than studies about programming variable names (my google-fu is weak) -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on dic creation
{} > with open(filename) as FileObject: > for lines in FileObject: > linelist=lines.split('\t') > Key=linelist[0] > ValCity=(linelist[12]).strip() > ValState=linelist[13] > ValOne=linelist[2] > ValTwo=linelist[6] > ValThree=boolean(linelist[7]) > D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)} > return D > print fieldict("DOT500.txt") > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on dic creation
FYI - Gmail's new "compose" feature makes it WAY too easy to miss trimming the quotes. :( On Mon, Nov 5, 2012 at 3:37 PM, Brett Ritter wrote: (way too much) -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner's question
On Thu, Nov 22, 2012 at 10:39 PM, Peter O'Doherty wrote: > Judging by the volume of replies, is it fair to say that this problem was > much too advanced for page 16 of an introductory text? That is a matter of taste. There are two things you need to learn: 1) the syntax of the language 2) how to approach programming problems #1 can be done gradually, #2 necessarily has a "hard" moment. For that, you need a problem that requires you think in programming terms: How can I break this down? What are the different conditions that could break my logic? How do I avoid missing situations? Once you cross that hump, it becomes "easy". Future problems will be hard again, but never to the same degree - once you've internalized the process, it's just a matter of refinement. Myself, I'd prefer to get that hard part over quickly. Up to page 16 obviously taught you enough basic syntax for your initial stab, so I think it was well timed. The important thing is for you to realize that you're learning a thought pattern, like algebra - once you "get it", the rest is merely learning applications and additional rules, but that initial insight requires some mental wrestling that can't really be reduced. If you're worried about the rest of the text (which I'm unfamiliar with) being more hard than enjoyable, I'd recommend continuing to play with this problem. Create variants (lowest POSITIVE even number), learn the typical situations that make it complicated. When someone can give you a problem in that vein and you can write a solution without referring to your previous attempts (or anyone elses), you've crossed the line and are forever changed. From that point forward the best the book (or any programming text) can do is challenge you, it can no longer be "hard". (Disclaimer: certain _concepts_ can once again be hard, such as pointers (not in python), lambdas, closures, etc. These should still be easier than this initial learning step) The bad part is that once you get it, you'll look back and wonder how you considered it hard. The good part is that if you are a programmer at heart, you'll find you love challenges. I think that's part of why you've gotten so many replies - this is an easy problem to understand, quick to provide a solution (right or wrong) for, and the constraints placed on it (just to keep you from getting lost in unfamiliar syntax) make it a mental challenge for us because our first approach is disallowed. Don't be disheartened at the difficulty, and don't fear it being this hard all along. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions question
On Wed, Dec 5, 2012 at 4:13 PM, Ed Owens wrote: > >>> str(string) > '[\nUpdated: Dec 5, 2012, 5:08pm EST\n]' > >>> m = re.search('":\b(\w+\s+\d+,\s+\**d+,\s+\d+:\d+.m\s+\w+)<', > str(string)) > >>> print m > None > It starts with ": which doesn't appear in your string. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions question
On Wed, Dec 5, 2012 at 5:04 PM, Ed Owens wrote: > >>> m = re.search(':\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', str(string)) > >>> print m > None > >>> > Okay, without the double-quote (it wasn't the colon that I was worried about, it was the double-quote), I believe the issue now is that you have a space in the string (after the colon), but \b is a zero-width match. -- Brett Ritter / SwiftOne swift...@swiftone.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor