|
Hey guys, very much a noobie here struggling through some python programming. I have excel files. Oh boy do I have excel files. I'm trying to write a program that (for a start) searches through a specific column and finds non-empty cells and identifies them. To that end, I'm using XLRD, slicing the column, and then attempting to use index to return the position in the list. But it isn't working, no matter how I try it. This is what I've cobbled together: code:
Is my approach completely flawed?
|
# ? Jul 8, 2013 12:36 |
|
|
# ? May 23, 2024 07:15 |
|
http://www.slideshare.net/r1chardj0n3s/dont-do-this-24000445 i haven't even got halfway this is amazingly disgusting
|
# ? Jul 8, 2013 13:55 |
|
Obdicut posted:excel stuff I'm not sure if I understood what you really want to do but I noticed this with a hasty look: Python code:
Python code:
Python code:
edit: Python code:
Symbolic Butt fucked around with this message at 14:48 on Jul 8, 2013 |
# ? Jul 8, 2013 14:19 |
|
Symbolic Butt posted:I'm not sure if I understood what you really want to do but I noticed this with a hasty look: Thank you that, the stupid mistake of searching string vs. integer is what was messing up my index-ing. The code you included looks a lot more elegant than mine, and since I need to both grab the index and the value of the cell it'll be super-useful. Sorry, that was a mistake I should have caught. Thank you for the help.
|
# ? Jul 8, 2013 15:41 |
|
tef posted:http://www.slideshare.net/r1chardj0n3s/dont-do-this-24000445 i haven't even got halfway this is amazingly disgusting Python code:
|
# ? Jul 8, 2013 18:00 |
|
Is it bad practice to put a lot of the "prep work" for a class into its __init__? I'm writing a throwaway script that parses a recipe from a URL to get more familiar with lxml, writing classes, unit tests and exception-handling. In my first cut at the script, "recipe = Recipe(url)" fetches the HTML from the URL, parses it, then populates a bunch of class attributes. Should I instead be calling a method to do that on the object after initializing it? Something like "recipe = Recipe()" then "recipe.getfromurl(url)"?
|
# ? Jul 9, 2013 01:25 |
|
onionradish posted:Is it bad practice to put a lot of the "prep work" for a class into its __init__? Yeah, generally speaking, you should try to avoid doing "real work" in a constructor. Here's a good read from Google that mentions it, with some other rules of thumb: http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-decided-to.html.
|
# ? Jul 9, 2013 02:57 |
|
technically speaking __init__ isn't a constructor it is an initializer run automatically on a new instance after it is constructed. There is an actual constructor, __new__, but generally you don't need to use it. what you are doing actually sounds like a pretty reasonable use case for initialization, assuming a Recipe isn't useful without that information. it would be silly to create an object, then add all the instance information. It wouldn't hurt to separate out some of that into a different method and call that from inside __init__ though, easier to unit test and more flexible. I'd probably have __init__ take an optional kwarg of url, and initialize itself using another method, say get_from_url, if a url was provided. something like code:
coaxmetal fucked around with this message at 04:59 on Jul 9, 2013 |
# ? Jul 9, 2013 04:56 |
|
Yes, __new__ is technically the constructor, but you almost never need to use __new__, so for all intents and purposes when someone says "my constructor in PYTHON" everyone knows what they really mean is __init__ (which, while technically is not a constructor, serves many of the same purposes) Still, it's a cool thing to be aware of
|
# ? Jul 9, 2013 05:12 |
|
Symbolic Butt posted:Every time I use lambda I feel like I'm compensating some shortcoming in the language, like for example this nested defaultdict thing: Can you explain what this does? I understand how defaultdict works, I use it a lot, but I've only ever used it with Python's basic types, e.g. dd=defaultdict(list). This looks like it makes a dictionary where by default every value is a function - am I reading this correctly? Why is there another defaultdict inside?
|
# ? Jul 9, 2013 05:18 |
|
FoiledAgain posted:Can you explain what this does? I understand how defaultdict works, I use it a lot, but I've only ever used it with Python's basic types, e.g. dd=defaultdict(list). This looks like it makes a dictionary where by default every value is a function - am I reading this correctly? Why is there another defaultdict inside? The argument to defaultdict isn't a type, it's a function; defaultdict(f) will call f() to create the 'default' value; for lists this is the empty list, for ints it's 0, etc. The reason it does that and doesn't just take a value is because if it did, defaultdict([]) would use the same list object, which is probably not what you want. So, for example, you can do Python code:
|
# ? Jul 9, 2013 05:23 |
|
yaoi prophet posted:The argument to defaultdict isn't a type, it's a function; defaultdict(f) will call f() to create the 'default' value; for lists this is the empty list, for ints it's 0, etc. The reason it does that and doesn't just take a value is because if it did, defaultdict([]) would use the same list object, which is probably not what you want. So, for example, you can do Ah that makes sense, thanks. I learned it this way: I tried defaultdict(list()) and it didn't work then I tried defaultdict(list) and it did, so I got it in my head that you pass a type.
|
# ? Jul 9, 2013 05:39 |
|
Ronald Raiden posted:technically speaking __init__ isn't a constructor it is an initializer run automatically on a new instance after it is constructed. Now explain how Java and C++ constructors aren't actually constructors either.
|
# ? Jul 9, 2013 08:02 |
|
Thanks for the __init__ feedback. The googletesting link Chosen posted is great timing because I'll be trying to set up tests next so I can refactor now where needed. I'd actually done all of the "work" in the __init__ through methods as Ronald Raiden suggested, but the idea of making the parameter optional (even though it'd always be provided in practice) seems like it'd be better for testing, allowing creation of a "plain" instance and then assertions against the methods.
|
# ? Jul 9, 2013 14:01 |
|
Thought I would post this here asking for a little help. I'm trying to return a list from a function but when I print the list out outside the function it comes back with "None". Here's the code: Python code:
Python code:
|
# ? Jul 9, 2013 16:11 |
|
yaoi prophet posted:The argument to defaultdict isn't a type, it's a function; defaultdict(f) will call f() to create the 'default' value; for lists this is the empty list, for ints it's 0, etc. The reason it does that and doesn't just take a value is because if it did, defaultdict([]) would use the same list object, which is probably not what you want. So, for example, you can do This allows perhaps the neatest encoding of trees (maybe a rose tree?) in python: code:
|
# ? Jul 9, 2013 16:25 |
|
DSA_Key posted:Thought I would post this here asking for a little help. I'm trying to return a list from a function but when I print the list out outside the function it comes back with "None". Python functions return "None" by default. Even if you don't write a return line, when the function is done it returns None. That's what happening inside of failed when it doesn't pass the if check.
|
# ? Jul 9, 2013 16:59 |
|
What FoiledAgain said. Here's a cleaner way, using list comprehensions. It replaces the entire block of code you posted. Python code:
|
# ? Jul 9, 2013 17:21 |
|
^^^^^ Just FYI, that list comprehension isn't exactly the same, since it won't filter out duplicate ips.DSA_Key posted:Thought I would post this here asking for a little help. I'm trying to return a list from a function but when I print the list out outside the function it comes back with "None". You've got two reasons that badip is turning out None: First, failed will only return a list if it's a bad IP. Second, the way you've written it, badip will only ever either be None or a list with one item with it, since you're overwriting it with a newly returned value every iteration of the for loop. Also, I suspect that you're expecting the badip list created in the failed function will carry over between calls. That's not the case; the list will get recreated each and every time the function gets called. Realistically speaking, you should structure your approach differently. The failed function should only return a bad ip or None. The for loop should be the one responsible for filling and filtering the list of bad ips. For instance: Python code:
|
# ? Jul 9, 2013 17:38 |
|
There's only one hard and fast rule I apply to __init__. If an error is encountered in __init__ throw the exception (or capture it and throw your own exception). It doesn't make much sense to swallow the error and allow the constructor to return a no good object that will blow up later when someone tries to use it. Stuff like how much real work to put into __init__ is a matter of use-case. I try to go with as little as possible.
|
# ? Jul 9, 2013 18:49 |
|
Haystack posted:
This is very helpful, I arrived at this conclusion shortly after I posed the question, I guess my brain wasn't working this morning. Thanks again!
|
# ? Jul 9, 2013 20:38 |
|
What is going on here? Why is seg changing types?code:
edit2: changed to show Tkinter FoiledAgain fucked around with this message at 04:24 on Jul 10, 2013 |
# ? Jul 9, 2013 23:40 |
|
Maybe show_segment_info() is altering it in place. Check seg's type before and after the 'seg_button =' line.
Dominoes fucked around with this message at 23:56 on Jul 9, 2013 |
# ? Jul 9, 2013 23:45 |
|
Dominoes posted:Maybe show_segment_info() is altering it in place. Check seg's type before and after the 'seg_button =' line. It's definitely a string going in, because I cast it to one the line before creating the Button. I didn't cut out any code there. And that concatenation is actually the first line of show_segment_info() so I'm sure nothing has happened to seg yet. I figured it had something to do with lambdas(?)
|
# ? Jul 9, 2013 23:56 |
|
I think self.show_segment_info(seg) isnt' evaluated until that lambda is executed, so that's probably related
|
# ? Jul 9, 2013 23:59 |
|
Something like this works for me: https://gist.github.com/anonymous/5962369, maybe it has something do with with what's going on in Button as this works for me: Python code:
Python code:
code:
|
# ? Jul 10, 2013 00:56 |
|
I realize that I didn't say that's the Button class from Tkinter, not my own object. Sorry for the confusion.
|
# ? Jul 10, 2013 03:03 |
|
yaoi prophet posted:The argument to defaultdict isn't a type, it's a function; defaultdict(f) will call f() to create the 'default' value; for lists this is the empty list, for ints it's 0, etc. The reason it does that and doesn't just take a value is because if it did, defaultdict([]) would use the same list object, which is probably not what you want. So, for example, you can do I don't really see why it would necessarily use the same list object, but I guess it would break the possibility of a tree like Malcolm XML showed. My beef is how misleading the examples of defaultdicts are when they use int, float or list as the argument. Maybe using just lambdas/building the constant functions would be better? Python code:
|
# ? Jul 10, 2013 13:42 |
|
Are there any Python modules that I can use to make requests to a webpage and show what resources have been requested and hopefully the amount of time the requests take? I want to make something that does what this web page analyzer does: http://tools.pingdom.com/fpt/ I can request a webpage easily enough in Python but trying to find examples or modules that would help me break it down further like Chrome/firefox developer tools and that pingdom tool do has not been easy.
|
# ? Jul 11, 2013 09:36 |
|
Sylink posted:Are there any Python modules that I can use to make requests to a webpage and show what resources have been requested and hopefully the amount of time the requests take? You will probably want to look into the HAR format. That is the format standard for gathering all those different web requests. It is basically JSON. http://element34.ca/blog/harpy This link may set you down the right path.
|
# ? Jul 11, 2013 12:33 |
|
Symbolic Butt posted:I don't really see why it would necessarily use the same list object, but I guess it would break the possibility of a tree like Malcolm XML showed. Lists are a bad example to see why it would use the same object — consider a more complex (i.e. user-defined) type: Python code:
Python code:
Another way to look at this: most Python type names refer to the constructor for the type. As I mentioned above, [] is really just sugar for list(), as {} is for dict() and () is for tuple(). Since you instantiate your user-defined classes by invoking the class name, why should builtin types be any different? For posterity, here's how to make an arbitrarily deep nested dictionary structure (with autovivification of intermediate dictionaries like Perl hashes): Python code:
|
# ? Jul 11, 2013 15:53 |
|
FoiledAgain posted:What is going on here? Why is seg changing types? Do you do anything else with seg later in some_method? Lambda will use its last value from that call of the function: code:
|
# ? Jul 11, 2013 17:41 |
|
Hey guys, so I teach Python one-on-one at a private high school and it's lame to keep telling my students "it's a hassle" when they ask me if they can deploy their little choose your own adventure and conversation programs to their friends and family. Sure for us it seems simple to just make sure python is installed, run a program in IDLE or whatever, etc. And my students obviously can do it too. But what if they want to show it to their grandma? Grandma just wants a double-click, and even that's already asking a lot. Things would also be easier if we were dealing with GUIs, but right now we're talking about really simple console text-based programs. So I'm wondering, of all the random and partially-supported deployment methods out there, what you guys think is the smoothest and most reliable across a broad range of given systems. Hopefully resulting in a double-click affair. I realize this is asking kind of a lot especially with a text-based program but I'm wondering if I'm missing something really simple. Thanks! Also, any of you teachers out there, do you prefer to teach in 2.7 or 3.3? mellowjournalism fucked around with this message at 19:58 on Jul 11, 2013 |
# ? Jul 11, 2013 19:56 |
|
i don't know how good it is, but py2exe exists.
|
# ? Jul 11, 2013 21:41 |
|
Both cx_freeze and PyInstaller have worked for me and are both cross-platform in the sense that you can use it on Windows/OSX/Linux to build a double-click executable for that system, although PyInstaller seemed to have more problems with some libraries.
|
# ? Jul 11, 2013 22:09 |
|
yellowjournalism posted:Hey guys, so I teach Python one-on-one at a private high school and it's lame to keep telling my students "it's a hassle" when they ask me if they can deploy their little choose your own adventure and conversation programs to their friends and family. Sure for us it seems simple to just make sure python is installed, run a program in IDLE or whatever, etc. And my students obviously can do it too. But what if they want to show it to their grandma? Grandma just wants a double-click, and even that's already asking a lot. Things would also be easier if we were dealing with GUIs, but right now we're talking about really simple console text-based programs. Create a setup.py file that looks like this, and put it in your program's directory. Fill in the blanks. Python code:
Python code:
Python code:
Dominoes fucked around with this message at 02:08 on Jul 12, 2013 |
# ? Jul 11, 2013 23:26 |
|
Ssss yeah I run a macbook at school, as do most of my students, along with our school computers running various versions of OS X. I can do it on my home PC but that still leaves out students who only have access to OS X. I tried to use wineskin, and maybe I'll try again, but the fact that it even started to give me a headache says to me that it's not really appropriate for the level I teach at (mostly middle school-age kids; I forgot to mention that even though we're technically a high school, we do one-on-one instruction and most of my python students are like 12) I was already planning on pretty much just going through all the ones listed here http://wiki.python.org/moin/deployment but figured I'd ask you guys for preferred methods or anything simple I'm missing. I might have to also just target Python 2.7 since there's generally less support for 3.3. Thanks though!
|
# ? Jul 11, 2013 23:52 |
|
Discovering the same hassle clued me in that Python is more of a programmer's programming language. It's interesting how the language and modules are designed to be easy to understand and use, but the results for users are not.
Dominoes fucked around with this message at 01:04 on Jul 12, 2013 |
# ? Jul 11, 2013 23:59 |
|
I actually hate teaching Python and transition to Java as soon as possible (for a variety of reasons). As you said, Python feels more like a really convenient language for prototyping, getting quick little things done, and whatever web/scripting/etc stuff that I don't know about but am aware is quite useful for others. Half the time Python honestly builds in a lot of arbitrary and confusing training scars for kids that can be annoying to deal with when they learn the more popular industry languages.
|
# ? Jul 12, 2013 01:02 |
|
|
# ? May 23, 2024 07:15 |
|
yellowjournalism posted:I actually hate teaching Python and transition to Java as soon as possible (for a variety of reasons). As you said, Python feels more like a really convenient language for prototyping, getting quick little things done, and whatever web/scripting/etc stuff that I don't know about but am aware is quite useful for others.
|
# ? Jul 12, 2013 01:33 |