|
unixbeard posted:sometimes i use a global variable for a database connection Theres a difference in python between using global variables like in your db connection example, and use of the global keyword. The use case for the second one is when you not only want to access a variable from outside the local scope (which happens automatically when you try to access a variable which doesn't happen to already be declared in local scope), but for when you want to change it and have those changes visible to things outside of the scope of what you're dealing with. There are of course a few valid reasons I can think of for why you may want to use the global keyword, just like there are plenty of circumstances under which its obviously sensible to use a GOTO statement. However, a lot of people come to depend on it for sloppy hacks, and as program complexity grows over time the result can get tangled up and incomprehensible. I think that sometimes this is just a lazy habit people get into, but relying on that style out of convenience or familiarity kind of curdles your brain and makes your code very unpleasant for others to follow.
|
# ? Jun 22, 2010 23:49 |
|
|
# ? Jun 13, 2024 04:29 |
|
i agree you should minimize their use where feasible, and it can be a sign of sloppiness. But sometimes it's just not realistic to go through and refactor everything. For instance I have a large program that was reading its inputs from textfiles, then it grew and I needed it to read from a database as well. Yes in an ideal world it should've been refactored, but that would take a few weeks vs a few days to get it to support databases. It can be hard to justify that extra time if someone is paying and they needed it done yesterday. It's a feature of the language, and people should be aware of arguments surrounding them and use them responsibly. Another time I've used them is for the mutex on an output stream in a threaded program. I also hate when functions are passing around arguments they dont use, ie. if a() calls b(), which calls c() which calls d() and d() needs the database connection, you need to pass it all the way down even though a, b, and c aren't using it. You could argue that if it gets to that stage it's time to redesign, but again you need to look at what that will really get you vs the time spent on it. I am also disappointed at my inability to get the phrase "willy nilly" into this post.
|
# ? Jun 23, 2010 10:33 |
|
This is the sort of thing that I always do in 15 lines and someone shows me how to do in 2... Say I have a list of strings that are sorted the way I want them and that this isn't alphabetical...completely arbitrary sort order. Now I'm building another list of strings and I want to use the first list as the specification to sort the second list. Bonus points for sorting strings in the second list that aren't in the first list to the end. code:
code:
|
# ? Jun 23, 2010 20:48 |
|
I was completely unaware you could do this in an interactive session until now (requires a unixy platform where python was built with readline)code:
|
# ? Jun 23, 2010 21:06 |
|
For the first part,code:
code:
Edit: code:
code:
Carthag Tuek fucked around with this message at 22:06 on Jun 23, 2010 |
# ? Jun 23, 2010 21:19 |
|
tripwire posted:I was completely unaware you could do this in an interactive session until now (requires a unixy platform where python was built with readline) or just use ipython
|
# ? Jun 24, 2010 03:32 |
|
nbv4 posted:or just use ipython ipython seems to replicate some of my favorite features of irb, actually, but I'm kind of creeped out by the strange frankensteinian hybrid that it seems to be. Having 'rm', 'rmdir', and such alongside python is just...weird to me.
|
# ? Jun 24, 2010 06:15 |
|
Fayk posted:ipython seems to replicate some of my favorite features of irb, actually, but I'm kind of creeped out by the strange frankensteinian hybrid that it seems to be. Having 'rm', 'rmdir', and such alongside python is just...weird to me. Yeah, my first instinct to that was , but then I realized it tab completed directories
|
# ? Jun 24, 2010 14:42 |
|
Has anyone every used pysage? http://code.google.com/p/pysage/
|
# ? Jun 24, 2010 16:08 |
|
king_kilr posted:Yeah, my first instinct to that was , but then I realized it tab completed directories I think I'd be more comfortable if I understood the metaphors and methods used - like some commands don't work - it seems all special-cased, but I can't see how and where. I should look at it more, though...
|
# ? Jun 24, 2010 22:55 |
|
Fayk posted:I think I'd be more comfortable if I understood the metaphors and methods used - like some commands don't work - it seems all special-cased, but I can't see how and where. I should look at it more, though... Yeah, this is why I installed ipython and then only used it once or twice.
|
# ? Jun 24, 2010 22:56 |
|
Autocomplete and "?" for help is enough to make ipython awesome for me, I don't see why you'd want to switch back to the normal shell.
|
# ? Jun 24, 2010 23:31 |
|
Nevermind ipython's cross-session command history (in which you can actually type the first few letters of a command and then hit up to see previous commands with the same first letters), deep reloading, input and output caches, and the pdb integration! I get angsty without it.
|
# ? Jun 24, 2010 23:54 |
|
How the heck do you use virtualenv with ipython?
|
# ? Jun 25, 2010 00:23 |
|
devilmouse posted:Nevermind ipython's cross-session command history (in which you can actually type the first few letters of a command and then hit up to see previous commands with the same first letters), deep reloading, input and output caches, and the pdb integration! I get angsty without it. Oh yeah, the completion is amazing. But wait, deep reloading? How do you do that?
|
# ? Jun 25, 2010 00:29 |
|
Thermopyle posted:How the heck do you use virtualenv with ipython? souce bin/activate easy_install ipython
|
# ? Jun 25, 2010 00:42 |
|
Jonnty posted:Autocomplete and "?" for help is enough to make ipython awesome for me, I don't see why you'd want to switch back to the normal shell. ?? for source code is better. As for ipython in a virtualenv, I install it fresh into every venv (actually I have a postcreate hook with virtualenvwrapper that does it for me). Also, wtf is easy_install, that's like a lovely dead pip right?
|
# ? Jun 25, 2010 00:49 |
|
king_kilr posted:?? for source code is better. Whoa. I never knew it did this, I must have only ever tried in on builtins where it doesn't work. Thanks, this'll save me a lot of effort!
|
# ? Jun 25, 2010 00:54 |
|
Jonnty posted:Oh yeah, the completion is amazing. But wait, deep reloading? How do you do that? dreload(<modulename>)
|
# ? Jun 25, 2010 01:21 |
|
devilmouse posted:dreload(<modulename>) Sweet, thanks. Any more magical features I'm missing?! e: pretty sure I asked about deep reloading in ipython in #python once and they scoffed and told me that I shouldn't need it. twats.
|
# ? Jun 25, 2010 01:46 |
|
Jonnty posted:Sweet, thanks. Any more magical features I'm missing?! Take a look at this http://ipython.scipy.org/doc/manual/html/interactive/tutorial.html - It looks like it covers the high points.
|
# ? Jun 25, 2010 03:33 |
|
I had to transpose a list of lists (AKA a 2D array) or python objects, and a bit of googling turned up this function, which works perfectly:code:
|
# ? Jun 25, 2010 20:09 |
|
Stabby McDamage posted:I've used * in parameter lists before, but how is it being used in this context? It unpacks the list or tuple into positional arguments. See http://docs.python.org/tutorial/controlflow.html#tut-unpacking-arguments
|
# ? Jun 25, 2010 20:23 |
|
Haystack posted:It unpacks the list or tuple into positional arguments. See http://docs.python.org/tutorial/controlflow.html#tut-unpacking-arguments Okay, that got me started on the right direction. The big thing to me is some magic in map() that I was previously unaware of: http://docs.python.org/library/functions.html posted:map(function, iterable, ...)¶ So we unpack each list of lists, and the lambda walks each element of each list one at a time, packing them into the lambda's row. So the two nested loops are both hidden inside the call to map. That's awesome.
|
# ? Jun 25, 2010 20:54 |
|
Is there an easy standard way to do without the "do ... while" syntax in Python? This is what I have: code:
code:
|
# ? Jun 25, 2010 23:06 |
|
code:
|
# ? Jun 25, 2010 23:24 |
|
hlfrk414 posted:
|
# ? Jun 26, 2010 01:39 |
|
hlfrk414 posted:
I guess this would work. Cheers
|
# ? Jun 26, 2010 11:26 |
|
nbv4 posted:or just use ipython oh my why did I never get around to this
|
# ? Jun 26, 2010 12:53 |
|
aside, I was looking up descriptors today and decided to make this horrorcode:
code:
|
# ? Jun 26, 2010 14:50 |
|
Forgive me, I'm new to Python. I want to do this:code:
code:
|
# ? Jun 26, 2010 18:14 |
|
code:
|
# ? Jun 26, 2010 18:25 |
|
king_kilr posted:
Nifty, thanks!
|
# ? Jun 26, 2010 18:39 |
|
I can't figure out how to solve this intermittent error, and I've failed to find a solution. I just ignored it; thinking I could solve it later, but now I've created another object that uses vertex arrays and so this error is occurring more often.code:
Here is heavily omitted code showing how I use Vertex Arrays. I'll post the vertex array lists if necessary. code:
Chris Awful fucked around with this message at 03:00 on Jun 29, 2010 |
# ? Jun 28, 2010 23:23 |
|
Jonnty posted:e: pretty sure I asked about deep reloading in ipython in #python once and they scoffed and told me that I shouldn't need it. twats. Typically when people want this, they should be writing unit tests instead.
|
# ? Jun 29, 2010 00:09 |
|
Habnabit posted:Typically when people want this, they should be writing unit tests instead. Hello again! Seriously though, I've not really needed to use it since, so he was probably right. I guess it's just my gut instinct to get angry at the line you see too often in open source forums and IRC channels: "it's not our fault for not having that feature, it's your fault for wanting it".
|
# ? Jun 29, 2010 00:34 |
|
I have a question about pyparsing... I'm parsing the output of save_object() from an LP style MUD which is a space separated key/value pair system where each kv is on a single line. It supports double quote delimited strings, numbers in either int or float format, a dictionary delimited by ([ ]) or a list delimited by ({ }) Based on the pyparsing JSON demo, I created:code:
code:
code:
|
# ? Jun 29, 2010 03:54 |
|
Jonnty posted:Seriously though, I've not really needed to use it since, so he was probably right. I guess it's just my gut instinct to get angry at the line you see too often in open source forums and IRC channels: "it's not our fault for not having that feature, it's your fault for wanting it". Hughlander posted:
|
# ? Jun 29, 2010 07:49 |
|
Habnabit posted:Sometimes there's a reason features don't exist. It tends to be more when the feature is "linux sound actually working" though or something like that.
|
# ? Jun 29, 2010 11:35 |
|
|
# ? Jun 13, 2024 04:29 |
|
Habnabit posted:Sometimes there's a reason features don't exist. With a data format that simple, it may be easier just to read line-by-line and use split and/or a regex. If I was writing it, I never would even have considered using a parsing library for space-based key-value pairs.
|
# ? Jun 29, 2010 15:41 |