|
You made me worry there
|
# ? May 22, 2012 20:18 |
|
|
# ? May 31, 2024 00:31 |
|
well how about that.
|
# ? May 22, 2012 20:35 |
|
ShoulderDaemon posted:If you're running a sh script, then the script starts its own shell. It doesn't borrow the shell it's invoked through. Ok, just for my own edification, I don't start a python shell, when I say I am running scripts from the shell, I mean from the bash prompt. Is that first shell the shell that python is running in order to execute the script? So I guess my question then is what's the point of invoking shell=True then? I could have sworn that I had trouble running bash commands from within an interactive shell in the past, but now it seems to be ok. If stuff like subprocess.Popen will work with or without shell=True, what benefit do you get using it?
|
# ? May 22, 2012 20:37 |
|
For that merge sort example, isn't that an awful lot of calls to .append()?
|
# ? May 22, 2012 20:53 |
|
fritz posted:For that merge sort example, isn't that an awful lot of calls to .append()? I believe append will only resize the list if the pre-allocated space is filled, and bigger allocations occur as powers of 2, so the list gets copied less than log2(n) times. http://stackoverflow.com/questions/311775/python-create-a-list-with-initial-capacity edit: and of course the entries in the list itself consist of references, so there's no larger penalty for appending larger objects etc.
|
# ? May 22, 2012 21:02 |
|
Ridgely_Fan posted:I believe append will only resize the list if the pre-allocated space is filled, and bigger allocations occur as powers of 2, so the list gets copied less than log2(n) times. Oh, cool, maybe I was thinking of a different language.
|
# ? May 22, 2012 21:05 |
|
JetsGuy posted:So I guess my question then is what's the point of invoking shell=True then? To allow morons to write code like this: code:
|
# ? May 22, 2012 21:33 |
|
tef posted:Despite this, people will tell you its performance without profiling it. Don't listen to these people, they are bad people. (also they do not know how python's sort works) Oh right, I forgot Timsort is amazing and uses ranges of already sorted data to run quicker. Thanks for pointing that out.
|
# ? May 22, 2012 21:51 |
|
Tim Peters
|
# ? May 22, 2012 22:03 |
Thanks everyone! I just began learning so I wasn't sure what was meant by "linear time".
|
|
# ? May 22, 2012 22:59 |
|
Suspicious Dish posted:To allow morons to write code like this: I'm gonna have to admit that I'm an idiot and I don't see what that command would do... fritz posted:For that merge sort example, isn't that an awful lot of calls to .append()? I used to use append a LOT, but I found numpy.vstack and numpy.hstack to be even better (and often times clearer to use).
|
# ? May 22, 2012 23:06 |
|
JetsGuy posted:I'm gonna have to admit that I'm an idiot and I don't see what that command would do... It's a standard UNIX thing to get the number of unique lines in a file.
|
# ? May 22, 2012 23:39 |
|
Suspicious Dish posted:It's a standard UNIX thing to get the number of unique lines in a file. Neglecting the fact that sort has a flag -u that makes sort | uniq unnecessary
|
# ? May 23, 2012 01:17 |
|
creeping featurism http://www.in-ulm.de/~mascheck/various/uuoc/kp.pdf
|
# ? May 23, 2012 01:23 |
|
Crossposting this from the sci computing thread. Since a few of y'all use matplotlib, I figure you may want to know.Jetsguy posted:
|
# ? May 23, 2012 16:51 |
code:
code:
I tried unicode, decode and encode but I'm pretty sure I'm doing something wrong because nothing works. vvvvvv The real page displays it as "1492: La conquista del paraíso / 1492 The Conquest of Paradise" so I think scrapy is encoding it weird. Never mind, the json exporter escapes unicode characters by default. lunar detritus fucked around with this message at 20:43 on May 23, 2012 |
|
# ? May 23, 2012 19:14 |
|
gmq posted:I managed to make a spider that gets the info I needed with scrapy but I'm getting strings like this in the json file: Is this the data you get back from the server? You're hosed.
|
# ? May 23, 2012 19:34 |
This is driving me nuts and Google isn't helping.code:
|
|
# ? May 23, 2012 21:29 |
|
Jo posted:This is driving me nuts and Google isn't helping. I don't know about Tk stuff, but if the input and ctrl-c have locked, it's usually I think because thread has stolen the GIL and your main thread can't get it back to process the input. Not that the knowledge will help in any way.
|
# ? May 23, 2012 21:48 |
|
There's a reason no real applications are built on Tkinter.
|
# ? May 23, 2012 23:38 |
|
By the way, you don't use semicolons to end lines in Python
|
# ? May 24, 2012 02:12 |
|
I want to find specific sequence patterns in a list of tuples, based on the second element in the tuple. For example, given a list like: code:
I can think of ways to do this by manually iterating through the list and testing the second tuple element against a bunch of IF statements, but suspect there's a smarter, more Python-like way to do it. I've just started with the language, and am already blown away by how much code Python's constructors eliminate.
|
# ? May 24, 2012 17:01 |
|
onionradish posted:I want to find specific sequence patterns in a list of tuples, based on the second element in the tuple. Use a dictionary: d = {'animal': [dog, cow, cat], 'vegetable': [corn, carrot], 'mineral': [granite]} Then you can access the dictionary by category: d['animal'] gives you a list of your animals.
|
# ? May 24, 2012 18:08 |
|
Will that give me the sequence, though? The order will matter, and that's the part I'm not sure how do do "elegantly." For example, "animal, vegetable" vs. "vegetable, animal" should return [cow, corn] and [carrot, cat] respectively. Myabe a Regular Expression? However, the dictionary form would be helpful elsewhere in code as a list of items in a category. Is there an easy way to convert the list example (assumed assigned to a variable) to that dictionary format? I'm doing it currently by iterating the list with separate constructors with custom IF statements. The categories (second tuple parameter) are hard-wired, but the entries (first tuple parameter) will vary. onionradish fucked around with this message at 18:35 on May 24, 2012 |
# ? May 24, 2012 18:23 |
|
onionradish posted:I want to find specific sequence patterns in a list of tuples, based on the second element in the tuple. code:
|
# ? May 24, 2012 18:43 |
|
It actually took a minute to figure out what you were doing. So you have a list of things and a list of categories, and order in the list matters, and you want to return examples where the categories go in a particular sequence? For this I'd probably switch over to numpy. Use integer arrays, and say let 'animal' = 0, 'vegetable' = 2, 'mineral' = 3 Then you can do something like: Python code:
If performance wasn't a big deal within some constant value, you could use lists of integers instead of numpy arrays but it would be somewhat slower.
|
# ? May 24, 2012 19:06 |
|
onionradish posted:However, the dictionary form would be helpful elsewhere in code as a list of items in a category. Is there an easy way to convert the list example (assumed assigned to a variable) to that dictionary format? I'm doing it currently by iterating the list with separate constructors with custom IF statements. The categories (second tuple parameter) are hard-wired, but the entries (first tuple parameter) will vary. I would do it this way: Python code:
You can do the same thing with a try...except block: Python code:
FoiledAgain fucked around with this message at 20:36 on May 24, 2012 |
# ? May 24, 2012 20:32 |
|
You don't have to use default_dict or exceptions to get default values in Python dictionaries:Python code:
|
# ? May 24, 2012 23:39 |
|
Ridgely_Fan posted:You don't have to use default_dict or exceptions to get default values in Python dictionaries: Your code doesn't work. code:
|
# ? May 24, 2012 23:47 |
|
setdefaultPython code:
Python code:
|
# ? May 25, 2012 00:01 |
|
Lysidas posted:Your code doesn't work. Huh, that's odd. I guess you can't use the "get" method to default return empty lists. Oops. Suspicious Dish posted:Except this is a bit silly, as I don't think this is what he wants to do, as he wants consecutive pattern matches. Here: Well I think he wanted both. He wants to get the items for pairs of categories as well as the items in the individual categories themselves (something like getting word counts, word bigrams, and word trigrams by categories I guess). Emacs Headroom fucked around with this message at 01:10 on May 25, 2012 |
# ? May 25, 2012 01:08 |
|
Ridgely_Fan posted:Huh, that's odd. I guess you can't use the "get" method to default return empty lists. Oops. No, it's that list.append returns None.
|
# ? May 25, 2012 01:11 |
|
Ah, so I should have done:Python code:
|
# ? May 25, 2012 01:14 |
I'm having a small problem writing a JSON file. "TypeError: <Cast 'Josh Hartnett' as 'Eben Oleson'> is not JSON serializable" Python code:
quote:>>> print cast I'm not sure what the problem is, any ideas?
|
|
# ? May 25, 2012 02:56 |
|
It's a custom Cast object, which isn't directly serializable to JSON? What kind of JSON data do you want?
|
# ? May 25, 2012 03:03 |
I have a JSON file exported by scrapy with movie names. I'm writing a script that takes those movie names and queries them against The Movie DB and saves the info (of all the movies) in a new JSON file. It worked fine until I added 'Cast', 'Genres' and 'Crew' which are lists. It works if I use str(mq.cast) but they stop being lists. Maybe I can process them later into lists again by using commas as delimiters? I don't really care about the format of the final file as long I can easily process it later to get it into Django. vvvvvvvvv EDIT: No, I want to get them into Django later, I just want a file right now. They are objects produced by the pytmdb3 module. lunar detritus fucked around with this message at 04:03 on May 25, 2012 |
|
# ? May 25, 2012 03:24 |
|
gmq posted:I have a JSON file exported by scrapy with movie names. I'm writing a script that takes those movie names and queries them against The Movie DB and saves the info (of all the movies) in a new JSON file. Right, so they're Django models. Use the Django serialization stuff to serialize your model to JSON.
|
# ? May 25, 2012 03:57 |
|
gmq posted:EDIT: No, I want to get them into Django later, I just want a file right now. Sorry, I missed this, as you didn't reply. Seems there's no easy way to export the data. You can try cast._data, but that may not be filled in properly.
|
# ? May 25, 2012 05:02 |
Python code:
Thanks though!
|
|
# ? May 25, 2012 16:35 |
|
|
# ? May 31, 2024 00:31 |
|
Thanks for the ideas on pattern matching! A big help, especially learning about zip(); I'll play around with code this weekend.
|
# ? May 25, 2012 17:34 |