![]() |
|
Re: ** Python Support Thread **
Being able to use functions as arguments and return values is like in the top three reasons if not the #1 reason I use Python
|
Re: ** Python Support Thread **
Quote:
I also didn't know this property was so unusual that it is a great reason to like Python. I mean, Javascript, Ruby, Haskell, et. al. ftw? Since I'm not sure if you were referring to my post, I wanted to clarify that I was talking specifically about closures, which are a massive mind-**** at first. I feel like my single biggest break-through in programming to date was identifying a situation where a closure was reasonable, and then actually getting the thing to work correctly. Of course, this doesn't mean I can explain it to a six year old, but it means I can identify what it does. Basically, a closure function holds a value so you can call it again later with other arguments. Instead of a mutable variable, you get a sort of mutable state of a function. http://stackoverflow.com/questions/2...to-use-closure I think the MDN docs give the best explanation I have seen, though it is in javascript. https://developer.mozilla.org/en/Jav...Guide/Closures |
Re: ** Python Support Thread **
Well then maybe I'm unlucky in that the languages I've worked with the most in any "official" capacity happen to be the ones that don't allow it (like C). ;) Lisp-like languages are interesting but run afoul of some of my other necessities like having good libraries (I'm not sure I would have picked up Python if it weren't for numpy).
|
Re: ** Python Support Thread **
a buddy of mind spoke highly of Clojure since you get all the power of lisp plus it's super easy to integrate with any Java library you like (and, as you may be aware, there are a few of those in the wild).
|
Re: ** Python Support Thread **
I've got some ugly (working!) code that I want to improve.
First, an explanation of what's going on here. This is an email copy-pasted from what I sent to friend of mine: Quote:
Code:
# My attempt to solve a basic sudoku puzzle |
Re: ** Python Support Thread **
Will edit this post as I find stuff
Ugly ans matrix made less ugly: Code:
ans = [[[i for i in range(1,10)] for j in range(9)] for k in range(9)]Code:
if numb in answ:Code:
answ = [x for x in answ if x not in chk] |
Re: ** Python Support Thread **
Thanks Xhad!
Quote:
Quote:
|
Re: ** Python Support Thread **
This is hard to google for obvious reasons, but I'm pretty sure "in" just calls "__contains__" (except for dictionaries and custom classes where __contains__ isn't defined and it has to call something else instead). So it's just as fast but more readable and slightly more flexible.
That construct is called a "comprehension". |
Re: ** Python Support Thread **
So, running the thing on my machine now and playing with it. This caught my eye in defineSquare:
Code:
if r < 3: rows = [0,1,2]Code:
r0, c0 = r//3*3, c//3*3Code:
def defineSquare(r,c):I also changed building "otherans" into a one-liner by importing copy.deepcopy, but now I'm looking for a way to dispense with it completely. I really want to believe there is a way to keep track of whether ans has changed without using "==" on two multidimensional lists. EDIT: My reaction to this if condition was "Is this on purpose?" Code:
for point2 in ans: |
Re: ** Python Support Thread **
Quote:
Code:
r0, c0 = r//3*3, c//3*3Quote:
Quote:
|
Re: ** Python Support Thread **
Quote:
Code:
r0, c0 = r/3*3, c/3*3Code:
r0, c0 = r - (r%3), c-(c%3) |
Re: ** Python Support Thread **
any of you guys do any web scraping with python? it seems like the most popular packages for scraping (beautifulsoup) aren't so great or just don't exist for python 3. I tried the beta for beautifulsoup and got errors when trying to import the module :(
Why haven't they kept the libraries up to date with python 3? Can I have both python 2 and 3 on my computer at the same time? One of my main motivations for learning to program in the first place was so I could scrape sites. |
Re: ** Python Support Thread **
Quote:
Quote:
|
Re: ** Python Support Thread **
Also, utilities exist to convert python2 code to python3 and any decent library developer certainly knows this, so whatever python2-only libraries are still around are most likely difficult or at least nontrivial to port.
|
Re: ** Python Support Thread **
So on the Sudoku thing, I think I've gotten to the point where any more playing with it will be for my benefit rather than mmbt0ne's. I do want to mention a couple more things though:
I finally realized an easy way to get rid of that "otherans" thing that's bothering me: make an AnswerMatrix class like so: Code:
class AnswerMatrix:Second thing, still don't know enough to know if this is easily possible (or if it would add that much in performance even in larger puzzles), but I really suspect ans could use sets instead of lists. They're often faster (since they're hash tables rather than arrays) and allow operations that make certain things easier. For instance, that answ thing from earlier? If they were sets and not lists this actually works: Code:
answ -= chk |
Re: ** Python Support Thread **
Code:
[i for i in range(1,10)]Code:
range(1,10)and yeah using isinstance or type to do type checking is a bit of a code smell and usually frowned upon. |
Re: ** Python Support Thread **
So, yet another IDE I just found out about:
http://idlex.****************/ Essentially, it's a Python script that runs IDLE but with a lot of the suck patched out of it. I've been running it for "desktop calculator mode" for the last few days and I've definitely noticed a distinct lack of things like crashing and spawning processes for no reason. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Thanks! I just got scrapy installed yesterday and am getting acquainted with it. It does seem like a lot of work but there are some examples and open source code for it.
https://github.com/scrapy/dirbot http://snippets.scrapy.org/ bwaha i'll be working for google in no time :D |
Re: ** Python Support Thread **
I'm trying to get 3to2 to work on a windows machine, with zero success. Is there a simple step-by-step instruction set somewhere?
I made the choice to start with Python3 a few months ago, and am now there are some packages I'd like to use that haven't been ported yet. It's mostly little things that I'll need to change (printing, division, csv module parameters, etc.) but I assume 3to2 will do all of that by magic once I have it working rather than me, more tediously, trying to make these changes by hand. So I've downloaded 3to2 but if I run "python3.2 setup.py install" it generates a bunch of syntax errors, so I must be missing something...any ideas? |
Re: ** Python Support Thread **
3to2 should be run with python2.x rather than python 3.2 iirc.
|
Re: ** Python Support Thread **
You're right, thanks! Not sure how I missed that bit of info...
|
Quote:
|
Re: ** Python Support Thread **
Yeah, I'll continue things in 2.7, but the problem is that I've already written a bunch of stuff in 3.2 over the past few months, and now need to convert that. Basically just a lack of foresight on my part that I'd want to use some libraries which weren't supporting 3.x yet.
(And 3to2 sadly is not magical...it looks like a lot of the details will need to be fixed by hand anyway.) Edit: The "UnicodeEncodeError"s are just rubbing salt into the wound here :-) |
Re: ** Python Support Thread **
Eh, I haven't tried to use 3to2 yet, but I could definitely see where many bad things could happen. I like using 3 better than 2, but oh well.
I use 3 to do database stuff and 2 to do web scraping atm. Otherwise, I do Project Eulers with 3. I guess I'm not much of a programmer with Python these days. |
| All times are GMT -4. The time now is 06:18 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive