|
I'm trying to use the sqlite module but I'm having a problem with the double column type. Some of the data has decimal places, but when those cells are read from the database and turned into machine floating points, errors are introduced, so I thought to try to make it return strings since I don't want to manipulate the numbers, anyway. I tried registering a data converter, but it doesn't seem to be used. Can anyone tell me what I'm doing wrong here:code:
code:
I also tried registering the converter on the name of the column: code:
Munkeymon fucked around with this message at 18:50 on Jul 3, 2010 |
# ¿ Jul 3, 2010 06:07 |
|
|
# ¿ Apr 27, 2024 12:58 |
|
Yet another 'critique my code' post. I needed to read a file backwards for some reason and mashed some horrible script out of Google snippets, so I figured it would be a nice exercise to make it look nice(r). http://pastebin.com/npK2YzDc The main questions are asked in inline comments, but I'm looking at overall Pythonicity, as well.
|
# ¿ Dec 13, 2010 23:17 |
|
Lurchington posted:Closing the file with either a with statement or a try-finally is usually better. Yeah, don't think I didn't check itertools first. Sailor_Spoon posted:stuff It started life as a class before I realized I could just write a generator and skip all the manual state management bullshit, thus TheName. Also, I should have mentioned it was supposed to be used like a module, so that's why I did my importing inside the function - I guess I'll move that to an __init__ block? Edit: hmm, looks like I was misunderstanding the format requirements of a module - this is easier than I thought Sailor_Spoon posted:Type checking is usually the wrong way to do things. Here, I would probably do something like ... and let either file mode errors or IOErrors bubble up Wouldn't that be a lot less useful to an end user? Consider what happens when the user (hopefully accidentally) passes in a file that happens to not be seekable: "Why is that stupid module trying to call open on the file I sent in?!" Then they have to read and understand the code to know why something is failing. Munkeymon fucked around with this message at 17:57 on Dec 14, 2010 |
# ¿ Dec 14, 2010 16:26 |
|
Sailor_Spoon posted:Depends on your user, I guess. I'd much rather get an error then have the function return None, which could mean a lot of things. Not having a seek method would be an AttributeError. I caused an IOError when I was loving around testing seek on different things I didn't think would be seekable, but, now that you mention it, it might not be universal across implementations/types of unseekable files. tef posted:code:
|
# ¿ Dec 14, 2010 21:06 |
|
What is code:
code:
At least I assume it's syntactic sugar for something string-specific because it's not working on other sequence types, but I can't find anything with Google, probably because the stupid keyword is 'in'
|
# ¿ Mar 31, 2011 17:34 |
|
Thermopyle posted:
Yep, I was thinking code:
|
# ¿ Mar 31, 2011 19:55 |
|
Space Prostitute posted:I believe that in general x in y is syntactic sugar for y.__contains__(x). This page has some details. I guess it's just defined differently on strings and lists.
|
# ¿ Mar 31, 2011 22:56 |
|
Is there a reason that this:code:
Can someone more in tune with Python culture tell me if that's a wart or an intentional feature?
|
# ¿ Nov 10, 2011 17:38 |
|
tef posted:wart, no longer a feature in python 3 Excellent - thanks
|
# ¿ Nov 10, 2011 22:27 |
|
Has anyone in the history of the universe convinced pyodbc to compile on CygWin?Rocko Bonaparte posted:I'm trying to tie together .NET and native environments as much as possible. I am more familiar with IronPython, but I understand there's an alternative Python for .NET. IronPython puts the engine in the .NET runtime, whereas I believe Python for .NET is the CPython engine with hooks into .NET. Unfortunately I need both .NET and native stuff, and some COM just to really screw things up. I'm trying to figure out what options I have for making this as painless as possible. This claims that the PyWin32 project adds COM interop to the CPython windows build, so you might just be able to use 'normal' Python if that works.
|
# ¿ Jan 18, 2012 18:58 |
|
If you really are running notepad, subprocess.call('notepad.exe') should just work. Windows has a path environment variable, too.
|
# ¿ Jan 27, 2012 19:00 |
|
spengler posted:I'm struggling with something pretty basic. I have an XML file that looks like this: "dcst:siteaddress" is also a tag name, fyi. The minidom DOM is, by Python standards pretty obtuse. Try this stuff: code:
And if that's not quite what you want, hopefully you can figure out how to modify it.
|
# ¿ Apr 3, 2012 18:40 |
|
Am I nuts or shouldn't this work:Python code:
pre:105 if not state: break 106 else: --> 107 stack.append((input[:i+1], state, None)) UnboundLocalError: local variable 'i' referenced before assignment Python code:
|
# ¿ Jun 8, 2012 20:13 |
|
leterip posted:If your input has no elements, i is never created. Didn't think it was getting called with empty input. Stupid assumption on my part. yaoi prophet the for-else was intentional.
|
# ¿ Jun 8, 2012 20:35 |
|
I'm using Iron Python to make a lot of calls to a web service with urllib2's urlopen function.Python code:
Edit: Switched to httplib and it's fine now Munkeymon fucked around with this message at 20:33 on Jun 15, 2012 |
# ¿ Jun 15, 2012 16:30 |
|
http://sourceware.org/cygwinports/ helps if you're using CygWin.
|
# ¿ Jun 25, 2012 15:59 |
|
duck monster posted:Aaaaaand apple continues its tradition of wiping users python directorys instead of migrating them during operating system upgrades. They keep hurting you - why do you keep coming back?
|
# ¿ Jul 31, 2012 15:18 |
|
pliable posted:What in the mother gently caress? That guy was on a slower Core 2. Took 10 minutes in regular Python and 3 minutes on IronPython on my i5.
|
# ¿ Aug 27, 2012 16:03 |
|
bc87 posted:Coming from a c++ background, serialization is so much easier using the pickle module. Don't use it for data a malicious user could access, though http://nadiana.com/python-pickle-insecure
|
# ¿ Sep 7, 2012 14:53 |
|
For the more modern .docx format, if you're using it https://github.com/mikemaccana/python-docx (I haven't personally tried this library) e: there's always IronPython and .Net's word interop library Microsoft.Office.Interop.Word. Using regular .Net libraries in Python is kind of annoying, but not nearly as annoying as trying to write VBscript in Office Munkeymon fucked around with this message at 15:10 on Oct 19, 2012 |
# ¿ Oct 19, 2012 14:51 |
|
Suspicious Dish posted:"I'm helping all these people do their jobs, and without me, they couldn't work!" Sure they could
|
# ¿ Dec 11, 2012 23:37 |
|
BeefofAges posted:Configure your editor to output four spaces when you hit tab. My only problem with using spaces like that is that a depressing number of editors can't be configured to delete back to a tab stop, even when they have an option to insert spaces when you hit tab. Having to remove a single keystroke with four just annoys the poo poo out of me (because I'm a big whiny baby, obviously, but that's not going to change).
|
# ¿ Feb 14, 2013 22:00 |
|
yaoi prophet posted:What's the idiomatic way for initializing a bunch of variables to the same value without that kind of sharing? Python code:
Edit 2: Python code:
Munkeymon fucked around with this message at 22:27 on Mar 14, 2013 |
# ¿ Mar 14, 2013 20:02 |
|
dedian posted:I wrote a little script this weekend to calculate and store md5 hashes of files in a list of directories I point the script to, for the purpose of finding duplicates. I fully realize there's lots of tools to do this already, and do it better, but I wanted to write something myself as I get more familiar with Python (I keep meaning to go through Learn python the hard way or other tutorials ). Does any of this look like the completely wrong way of doing things? It seems like the for loops don't need to be as nested... somehow? List comprehensions, or.. something? Anyway, just something dumb to poke holes in on a Monday morning (I'm happy at least that it works ) Consider using argparse to make your command line interface both more useful and easier to maintain: http://pymotw.com/2/argparse/index.html You can use glob to help build DIR_LIST based on user input: http://pymotw.com/2/glob/index.html
|
# ¿ Apr 15, 2013 16:03 |
|
Hammerlite misspoke. The .py is implied when you import something, so you can leave it off. What might be a lot easier for you, especially considering that it takes some effort to reload a module that's been imported, is to run python through the command interpreter. You can just hit Start and type cmd to get it to come up in the search list. Then, I think you can just run you files by typing python <file path with .py>, assuming it's on the PATH, which you evidently already know how to edit. If you CHDIR to the directory your scripts live (chdir C:\Users\Dan\desktop) you can just type python hello.py to run your scripts.
|
# ¿ May 1, 2013 21:52 |
|
Dominoes posted:I got my program to work on Ubuntu running from source - everything works. (although the GUI fonts are too big for their buttons - easy fix) I looked up making a .deb file, and the instructions were complex, and seemed to require being on Linux to it. Ubuntu is Linux and, what's more, a Debian (.deb(!)) based distribution, so you're about as close to the target of that format as you can get if you're actively using Ubuntu.
|
# ¿ Jun 12, 2013 22:08 |
|
Mr. Wynand posted:I still don't quite get how input() is running at all during the retrieval unless you are already using a multithreaded or at least async lib. If you are then there is usually a way to control tasks and make them dependent on one of them finishing. I mean you said something about it being in a loop, so... stop the loop? stdin is a pipe. Pipes are buffered. Input is being buffered during the long-running operation and handed to raw_input after it gets done. This is easy to demonstrate: Python code:
code:
E: oh, ⏬ ⏬ well there you go ⏬ ⏬. Munkeymon fucked around with this message at 18:04 on Jul 19, 2013 |
# ¿ Jul 19, 2013 17:54 |
|
evensevenone posted:Have you tried sys.stdin.flush()? I can't test it right now and terminals seem to defy all common sense but feel like that might work if you put it immediately before the input command. Tried it. Doesn't do anything.
|
# ¿ Jul 19, 2013 20:29 |
|
Thermopyle posted:Secondly, and I'm not as sure about myself here, I think the fact that a == True and a is True both give you the same result is implementation-specific. In other words, I think, that the next version of CPython could change that, or another implementation, like IronPython for example, might return False when you ask it to evaluate a is True, because conceptually a with the value True is a different object from True. IronPython does have single global True and False objects which surprises me a bit because it doesn't do that with strings and numbers like CPython will. I do think you're right about it being platform specific, though, because I can easily imagine an interpreter with a braindead-simple bool class that doesn't memoize itself.
|
# ¿ Aug 8, 2013 20:44 |
|
I'm trying to be all fancy and clever and make a one-liner that takescode:
code:
I came up with this: Python code:
Python code:
I know how to do it with a couple of regular loops, but I'm annoyed that I can't get it to work this way, too.
|
# ¿ Aug 28, 2013 19:50 |
|
Hammerite posted:I take it you are aware that "for row in inpt" is the inner loop here? Welp, that's the problem - I got the order of evaluation mixed up. Probably why I don't use nested (or even conditional) comprehensions more often and a reason I should: I don't parse them correctly at first glance. In my defense, nested comprehensions read rather awkwardly, unlike basically everything else in Python. quote:On the other hand, I do not understand how this code can be giving you the result you expect. I would expect it to give you "Product 1" for the first item, then "Product 2" for the second item, and so on until it runs out of product numbers. It works as I expected it to work for one row, which probably should have tipped me off.
|
# ¿ Aug 28, 2013 21:06 |
|
sharktamer posted:Has anyone used comments withing a ConfigParser config file? I can set up comments within the file like so: Write your changes to a different file and have ConfigParser merge them with the base options?
|
# ¿ Sep 25, 2013 16:58 |
|
Use a defaultdict with keys that are tuples holding coordinates and values of True/False or 1/0 to represent the board. Yeah, that runs out of memory or overflows (built-in wrapping!) at some point in reality, but on magic infinite hardware it works. E: to limit memory you could, say decrement every empty cell on a tick and delete the ones under some threshold, but you can probably make a initial board that grows to consume all memory if you try. You need magical hardware, basically. Munkeymon fucked around with this message at 20:28 on Oct 1, 2013 |
# ¿ Oct 1, 2013 20:09 |
|
lostleaf posted:Does anyone have any recommendations on python libraries that handle multitouch events from the trackpad or touchscreen? I'm looking to write a program like bettertouchtool(mac) or touchegg(linux) but for windows to customize multitouch events. Was "kiva" supposed to be Kivy?
|
# ¿ Oct 11, 2013 16:30 |
|
Thermopyle posted:I wish one of you smart, experienced motherfuckers would get off your rear end and make a good python game framework already. I hate having to tell newbies that their options aren't that great. http://pyopengl.sourceforge.net/ what more do you need * *I don't know because (without getting into my relative intelligence or lack thereof) I'm not experienced enough
|
# ¿ Nov 7, 2013 17:40 |
|
ahmeni posted:Either someone will have to build Python bindings for Unity via some sort of IronPython wizardry or someone will have to build a cross-platform compatible game library that runs a Python engine on mobile platforms. Either way that's a lot of work. I'm not sure it'd be so hard to minimally get running, provided you have Unity and the IronPython assemblies handy. Setting up a DLR environment and pointing it at a Python script is actually ~5-10 lines in C# which my 30 seconds of googling suggests would be totally doable from a default Unity install since it apparently just uses Mono under the hood. I might try it this weekend if I have time, but it's looking like I won't :\
|
# ¿ Nov 8, 2013 19:00 |
|
Pollyanna posted:...right I forgot to say, "define a function that has a name equal to page". I don't know if there's a way to do that, though... Why do you care about the name of a throwaway wrapper function? Which, BTW you can set with the __name__ property if you really want Anyway, you can probably do the same thing like this: Python code:
E: I get it now - code updated Munkeymon fucked around with this message at 23:04 on Nov 8, 2013 |
# ¿ Nov 8, 2013 22:41 |
|
Thermopyle posted:One of you is saying on the new OP document to not suggest Windows users download the python.org Windows version but to instead download a Windows-specific distribution like...well like, I don't know what. But anyway... I'm a non-scientific user and the last two times I've set up Python on a Windows machine I used Anaconda because basically. I didn't even know it had an IDE involved - I just didn't want to gently caress with getting all those packages individually, which is annoying on Windows because you can't just type sudo apt-get install some-library like you can on *nix.
|
# ¿ Nov 22, 2013 17:12 |
|
Pivotal Lever posted:I mistook CoC for GBS 2.1, sorry! This thread could use more missiles, tbh. We need some sort of global lock to make sure output can only happen on one thread at a time.
|
# ¿ Dec 20, 2013 15:52 |
|
|
# ¿ Apr 27, 2024 12:58 |
|
Dren posted:Lookaheads are cheating How about (\d{3})\s{2}(\w+(?:\s{1,2}\w+)+?) ?
|
# ¿ Jan 24, 2014 17:17 |