|
Check herequote:CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. You want to either sort after getting the keys sorted(dictionary.keys()) or find some sort of SortedDict implementation that fits your needs. VVVV Thermopyle posted:Dictionaries have no order. I can honestly say I haven't found a use for OrderedDict outside of serialisation. deimos fucked around with this message at 21:15 on Sep 12, 2013 |
# ? Sep 12, 2013 21:12 |
|
|
# ? May 9, 2024 16:14 |
|
Dictionaries have no order. If you need them to look at collections.OrderedDict. edit: oh yeah ^^, you could find a sorteddict as well
|
# ? Sep 12, 2013 21:13 |
|
deimos posted:You want to either sort after getting the keys sorted(dictionary.keys()) or find some sort of SortedDict implementation that fits your needs. I can't count the number of times I've put this in a script I'm working on code:
And before anyone gives me poo poo about my code I'll just leave this here... -bash-3.2$ cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.5 (Tikanga) -bash-3.2$ rpm -q python python-2.4.3-27.el5 You guys and your fancy new python versions. I'm jealous. deedee megadoodoo fucked around with this message at 21:32 on Sep 12, 2013 |
# ? Sep 12, 2013 21:27 |
|
Last time I needed a sorteddict I got one from blist. I recommend it.code:
You can probably get by with sorting your keys though.
|
# ? Sep 12, 2013 21:38 |
|
HatfulOfHollow, mind explaining what's going on in that tibit there? I sorta get it, I know what classes are, but I'm not sure what's going on around line 3. Also, thanks guys, I managed to get it all sorted out. Now I have two lists, and I'm wondering if the best way to put them together is in a CSV file. I made a 2d array of values in NumPy, but I'm not sure how to change that into a CSV. savetxt gives me an error when I try to use it. edit: Like, say I had: code:
code:
Dren posted:http://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file I think what happened for me is that I never managed to make it into a 2D array, it became some...weird monstrosity instead. I got an "expected Float, got something else" error when I tried. edit: FIGURED IT OUT Pollyanna fucked around with this message at 22:15 on Sep 12, 2013 |
# ? Sep 12, 2013 21:42 |
|
http://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file If you happen to be in pandas, pandas has a dataframe -> csv method you could use too.
|
# ? Sep 12, 2013 21:48 |
|
Pollyanna posted:HatfulOfHollow, mind explaining what's going on in that tibit there? I sorta get it, I know what classes are, but I'm not sure what's going on around line 3. super can be hard to wrap your mind around if you're new to OOP. Basically, that lines is calling the keys method of the super class of SortedDictionary (namely, dict). The reason you do this: Say you inherit from a a class with a method called butts and that method prepends the word "butt" to any string passed to the method like this: code:
code:
Python code:
|
# ? Sep 12, 2013 21:55 |
|
I think I get it, yeah. What's "self", though?
|
# ? Sep 12, 2013 23:43 |
|
super is used to keep code working through the chain of inheritance. If you just have a single level of inheritance you don't need to use super, but things can get wonky if you have multiple levels. self is a reference to the object you instantiate. It's like "this" in java. It's basically a way to access members of the class from within it, while also making them accessible externally. deedee megadoodoo fucked around with this message at 00:14 on Sep 13, 2013 |
# ? Sep 13, 2013 00:10 |
|
Thermopyle posted:
You are missing self in the butts method, which I have fixed for you. Pollyanna posted:I think I get it, yeah. What's "self", though? self is instance of the class. You might want to start reading here for more information on classes: http://docs.python.org/2/tutorial/classes.html
|
# ? Sep 13, 2013 00:14 |
|
I'm actually realizing I don't have a very good handle on OOP in general. I'll have to read up on it. I also thought of something. You know how compiled languages need an executable? Is there something like that for Python? I know RenPy has some sort of executable, but I'm not very familiar with it. Pollyanna fucked around with this message at 00:52 on Sep 13, 2013 |
# ? Sep 13, 2013 00:49 |
|
Pollyanna posted:I'm actually realizing I don't have a very good handle on OOP in general. I'll have to read up on it. Need or want? If you are importing packages into your script say main.py with the statement 'import foobar', then foobar.py will be compiled into foobar.pyc, which means that text was translated into byte-code file. However, main.py won't get compiled. For most cases, you never need to worry about compiling code. If you want to share your code with someone that has no idea what they are doing you might consider compiling (or finding new friends/associates). There are a number of options for this case such as cx_freeze (http://cx-freeze.sourceforge.net/).
|
# ? Sep 13, 2013 01:00 |
|
Does anyone know of any module I can use to export generated strings into a html template? I don't mean something as sophisticated as Django, just something where I can define a html like:code:
|
# ? Sep 13, 2013 10:20 |
|
sharktamer posted:Does anyone know of any module I can use to export generated strings into a html template? I don't mean something as sophisticated as Django, just something where I can define a html like: Jinja is pretty cool.
|
# ? Sep 13, 2013 10:21 |
|
Nippashish posted:Jinja is pretty cool. That looks great. Something included in python would have been better and would make deployment easier, but this is about as good. I'll give this a go later. Thanks a lot. e: I haven't checked the full spec yet, but all I really need to do is generate a html file for use in an email. There's not going to be any hosting or web stuff needed, it should just eat an html file, put in the strings and spit it back out as a new string. Looks like it should be able to do just this, but maybe there's something lighter? sharktamer fucked around with this message at 11:07 on Sep 13, 2013 |
# ? Sep 13, 2013 10:50 |
|
Seriously, just use the templating engine (Mako is probably lightest, but it's just as featureful as Jinja2). It'll protect you from your own stupidity now and someone else's malice later.
|
# ? Sep 13, 2013 11:20 |
|
sharktamer posted:That looks great. Something included in python would have been better and would make deployment easier, but this is about as good. I use mako for templating, it's really nice and simple. Alternatively you could just store a HTML file with named replacement strings in it like: code:
And template with: code:
|
# ? Sep 13, 2013 11:26 |
|
Will do. I just thought maybe using one of these modules would be like smashing a nut with a sledgehammer. I'll give one of these a go. They look easy enough to get into. Thanks both of you.
|
# ? Sep 13, 2013 11:27 |
|
ahmeni posted:I use mako for templating, it's really nice and simple. Alternatively you could just store a HTML file with named replacement strings in it like: It is true that it's very simple to whip up a class that does that, but Crosscontaminant is right to point out that that does nothing to protect you from injected scripts and such.
|
# ? Sep 13, 2013 12:00 |
|
So, I'm writing a thing that takes as much info as you can give it and tries to determine what canonical movie the information refers to. So, you can provide it with as little as one piece of information (such as a title) or somewhere around a dozen pieces of information (title, director, date, cast, etc) and it figures out which movie you're talking about. As a user of this library would you prefer a class that takes a ton of optional arguments on instantiation, or setting a bunch of attributes on an object, or a bunch of methods like add_release_date, add_director, or what?
|
# ? Sep 13, 2013 17:45 |
|
Thermopyle posted:So, I'm writing a thing that takes as much info as you can give it and tries to determine what canonical movie the information refers to. Documented **kwargs and let the dev sort it out?
|
# ? Sep 13, 2013 18:28 |
|
I would prefer to pass you a giant string and have you do the hard part. Have you looked at elastic search?
|
# ? Sep 13, 2013 19:00 |
|
Dren posted:I would prefer to pass you a giant string and have you do the hard part. Have you looked at elastic search? I don't have the backing data, I basically act as a smarter front end to other third party APIs, and intended users already have structured data, its' just not completely normalized. So they'll maybe have the title but capitalization might be wrong or maybe its missing "The". Or maybe they'll have a release date and a director (with first initial instead of whole name), and a few cast members names. But they'll at least have each piece of data and know whether a string is a director's name or a list of cast members or whatever. Right now I'm thinking about taking a class and using attributes on that class, instead of taking a kwargs or whatever. That way users can provide their own classes with the attributes I'm expecting.
|
# ? Sep 13, 2013 19:16 |
|
Please just use kwargs instead of rolling your own snowflake way of passing named parameters.
|
# ? Sep 13, 2013 19:22 |
|
Either a class with defined properties or kwargs. Would passing you a giant jumbled string and having you try all the combinations and figure out the answer that is most likely correct be out of the question?
|
# ? Sep 13, 2013 19:25 |
|
Dren posted:Either a class with defined properties or kwargs. Out of the question. That will result in a huge increase in hits on third party APIs, and isn't really necessary since potential users will already know what the various strings are. more like dICK posted:Please just use kwargs instead of rolling your own snowflake way of passing named parameters. Yeah, I'm back to thinking this is the right answer.
|
# ? Sep 13, 2013 19:35 |
|
I've written a daemon and a GUI utility to manage it, but finding out how to package it all up in a distributed way is turning up lots of different advice, so whats the reccommended way of packaging it all up into a .deb thing that other people can use? It needs to require some other non-python packages and mess around with their configuration files but I guess it can do at run time instead of install time if needed. Should I even be trying to do this with Python or just tell users to download the dependencies and go through the 'download tarball, extract, run python3 setup.py install in terminal' thing themselves? https://pypi.python.org/pypi/stdeb is the most promising I've seen so far but if it literally only works on the exact same debian/ubuntu version as it was compiled on then there isn't much point. e: Come to think of it where do I even put the files on a system? daemonscript and can go in /usr/bin I presume but what about daemonHelperFunctions.py? Would that go in dist-packages or is this an acceptable situation to shove everything in a single py file? Ireland Sucks fucked around with this message at 08:00 on Sep 16, 2013 |
# ? Sep 16, 2013 06:49 |
|
Okay, I have a question. I want to plot two x-y graphs on a single plot, such that I can see where they intersect. This is what I've written:Python code:
Pollyanna fucked around with this message at 04:02 on Sep 17, 2013 |
# ? Sep 17, 2013 03:30 |
|
Our final project for class is to make a text adventure game in python. I want to do a roguelike. I know there's a specific module for that, but using that probably won't cut it. Is there a good module for working with the windows console? Curses apparently only works in UNIX (or doesn't, I can't really figure out what the deal is with it) and most of other relevant modules I've found were last updated in 2000 and don't really work well with 2.7 or modern Windows. Am I out of luck? I could try to do it all from scratch with nested lists or something, but having cursor functionality and easier printing by coordinates would really expand what I could accomplish. It also sucks having to use os.system('cls') to refresh the screen, as it is slow and so causes annoying flickering. Is there a better way to dynamically print?
|
# ? Sep 17, 2013 04:51 |
|
hardycore posted:Our final project for class is to make a text adventure game in python. I want to do a roguelike. I know there's a specific module for that, but using that probably won't cut it. Is there a good module for working with the windows console? Curses apparently only works in UNIX (or doesn't, I can't really figure out what the deal is with it) and most of other relevant modules I've found were last updated in 2000 and don't really work well with 2.7 or modern Windows. Am I out of luck? I could try to do it all from scratch with nested lists or something, but having cursor functionality and easier printing by coordinates would really expand what I could accomplish. It also sucks having to use os.system('cls') to refresh the screen, as it is slow and so causes annoying flickering. Is there a better way to dynamically print? Libtcodpy is extremely good. Get it and run the sample it provides and you'll see how much potential there is. Edit: Also a neat sample of what it can do if you want: http://www.youtube.com/watch?v=B6yBR4C8YsM
|
# ? Sep 17, 2013 05:09 |
|
Pollyanna posted:Okay, I have a question. I want to plot two x-y graphs on a single plot, such that I can see where they intersect. This is what I've written: You need to access the next subplot (212) before plotting the second set of data. Try something like this: Python code:
|
# ? Sep 17, 2013 09:32 |
|
I notice that when a file is run as a program (as opposed to being imported), importing it (directly or indirectly) causes its statements to be run a second time. This is in contrast to importing it two or more times, in which case its statements only run the first time it is imported. Evidently this is not necessary (Python could regard the program-level execution of the file as the first import of that file, and make a corresponding entry (besides __main__) in the sys.modules module table). What is the rationale behind Python's behaviour? importtest1.py: Python code:
Python code:
code:
|
# ? Sep 17, 2013 13:44 |
|
When you run python importtest1.py, the module executing isn't importtest1. It's __main__. You can see this by importing __main__ and inspecting __main__.__file__. When you import importtest1 again, it runs a separate execution of the code. Always split out your main program scripts from importable library modules.
|
# ? Sep 17, 2013 13:46 |
|
Suspicious Dish posted:When you run python importtest1.py, the module executing isn't importtest1. It's __main__. You can see this by importing __main__ and inspecting __main__.__file__. When you import importtest1 again, it runs a separate execution of the code. You are describing the what, not the why. I am aware of the what, indeed I described it in my post and demonstrated awareness of __main__. I would like someone to describe the why.
|
# ? Sep 17, 2013 13:49 |
|
Oh. Well, there's multiple reasons I can think of that would make what you're suggesting hard to do. I'm not sure there's any rationale other than "that's the way it always worked, and it probably added this __main__ thing when modules were first implemented". First, how would it know what the actual module was named? If you have a module that you access with a.b.c, how would it know about the parent packages? Heuristics? Take a guess? Require people to say a/b/c.py on the command line? Second, let's say it figures out your script is a packaged module. Should it run all the __init__.py files of a package, and basically attempt to do the equivalent of __import__("a.b.c")? Third, if they changed it, the if __name__ == "__main__" trick would stop working, so you'd have to find a new way to ask "was this the main executable"? ... Not that I'd have a problem with that, but we still get people in #python asking "hey guys this tutorial is broken" for the print statement change in Python 3, and this obtuse change where the old syntax doesn't break, it just always evaluates to False would be incredibly confusing to new learners.
|
# ? Sep 17, 2013 13:57 |
|
QuarkJets posted:You need to access the next subplot (212) before plotting the second set of data. Try something like this: The problem with that is, I'm using 212 for a different set of data, and I'm trying to overlay two sets of data onto a single subplot. This is the full bit of code I'm talking about : Python code:
edit: Never mind, I'm retarded. I'm supposed to use plt.plot_date(y = sma, fmt = 'b-') instead of plt.plot(y = sma, fmt = 'b-'). Pollyanna fucked around with this message at 14:51 on Sep 17, 2013 |
# ? Sep 17, 2013 14:38 |
|
Graphing question where I am using matplotlib. The graphs I produce are as follows: The first two graphs are the same but with different linewidths, whereas the third graph demonstrates a graph with 3 instead of 4 subplots. I would like linewidth to be as thick as possible so that the segments do not overlap, regardless of the number of subplots on the graph. Is there a way to automatically calculate what linewidth should be so that the line segments do not overlap?
|
# ? Sep 17, 2013 15:00 |
|
I have another problem. When I try to make three subplots instead of two, i.e. subplot(213), I get this error:code:
|
# ? Sep 17, 2013 15:16 |
|
Pollyanna posted:I have another problem. When I try to make three subplots instead of two, i.e. subplot(213), I get this error:
|
# ? Sep 17, 2013 15:46 |
|
|
# ? May 9, 2024 16:14 |
|
Okay, so now I have another problem (sigh). I want to make a graph that has both a scatter plot and a histogram. However, I can't seem to just say plot.hist() onto a plot that already has a plot.plot_date(). How do you overlay two different types of plots onto a graph? edit: nvm, figured it out! Pollyanna fucked around with this message at 16:43 on Sep 17, 2013 |
# ? Sep 17, 2013 16:32 |