Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Hughlander
May 11, 2005

Stabby McDamage posted:

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.

Even when the values could be a dict of dicts of dicts of lists of dicts of lists containing floats?

IE: One k/v pair in my normal test is:
code:
replay ({({"tterhius's warhorse",1271525142,0,1,0,10,1,({1,0,}),}),({"tterhius",1271525132,0,1,0,10,3,({1,1,}),}),
({"tterhius",1271525132,0,2,1,10,2,({3,1,}),}),({"tterhius",1271525124,0,3,2,10,8,({3,1,}),}),
({"tterhius",1271525122,0,5,5,10,5,({6,1,}),}),({"Hastur the Unspeakable",1271522226,0,5,0,10,5,({5,0,}),}),
({"Hastur the Unspeakable",1271522218,0,5,5,10,5,({5,0,}),}),({"Hastur the Unspeakable",1271521050,0,6,4,10,6,({6,0,}),}),
({"Hastur the Unspeakable",1271520978,0,4,4,10,4,({4,0,}),}),({"Hastur the Unspeakable",1271520910,0,1,1,10,1,({1,0,}),}),
({"Hastur the Unspeakable",1271520898,0,9,1,10,9,({9,0,}),}),({"large inner demon",1271357344,0,3,0,10,3,({3,2,}),}),
({"large inner demon",1271357336,0,1,3,10,1,({1,2,}),}),({"large inner demon",1271357334,0,7,3,10,7,({7,2,}),}),
({"large inner demon",1271357284,0,2,6,10,2,({2,12,}),}),({"large inner demon",1271357278,0,2,8,10,2,({2,12,}),}),
({"bloodguard",1267999881,0,10,0,10,10,({10,0,}),}),({"uzziel",1258168171,0,7,3,10,7,}),({"traveller",1256943054,0,5,5,10,5,}),})
or
code:
tags (["default":(["end":"</#CHAN#>","start":"<#CHAN#>",]),])
IE: It's basically JSON with an outer k/v pair, I'd think you're being disingenuous to say that you would just use a regex to parse JSON.

Hughlander fucked around with this message at 16:46 on Jun 29, 2010

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->
The reason it is so slow is that pyparsing is a parsing framework, not a parser itself. Hand written parsers will normally go much faster than a parser-interpreter written in python.


Personally: i'd write a simple tokenizer and a top down parser myself, as it has embedded values in lists or pairs.


alternatively, i'd tokenize it into key-value and use an existing library like simplejson to parse the value.

edit: if you're using py2.6 : http://docs.python.org/library/json.html

clearly not a horse
May 8, 2009

undue butt brutality is not a criminal offense
EDIT: In hindsight, ignore this post and treat it like poo poo. The problem was just division by zero. :ughh:


I am having a small problem with floating point numbers here, and I know I am doing something terribly wrong - I just need help to point out the error.

Below is a small function for generating a crude approximation of eulers number, where a fraction of (1/n) is added for each iteration. I may be even wrong with my approach, but the point is the error I get during interpretation.

code:
from math import *

def e (depth):
    base = 1.0
    for n in range(depth):
        base += 1.0/n
        print base

When I run the function, I get this error:
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 6, in e
ZeroDivisionError: float division

clearly not a horse fucked around with this message at 23:28 on Jun 29, 2010

spankweasel
Jan 4, 2006

range(N) returns a list starting at 0 with N elements in it.

Try: for n in range(1, depth+1)

Stabby McDamage
Dec 11, 2005

Doctor Rope

Hughlander posted:

Even when the values could be a dict of dicts of dicts of lists of dicts of lists containing floats?

IE: One k/v pair in my normal test is:
code:
replay ({({"tterhius's warhorse",1271525142,0,1,0,10,1,({1,0,}),}),({"tterhius",1271525132,0,1,0,10,3,({1,1,}),}),
({"tterhius",1271525132,0,2,1,10,2,({3,1,}),}),({"tterhius",1271525124,0,3,2,10,8,({3,1,}),}),
({"tterhius",1271525122,0,5,5,10,5,({6,1,}),}),({"Hastur the Unspeakable",1271522226,0,5,0,10,5,({5,0,}),}),
({"Hastur the Unspeakable",1271522218,0,5,5,10,5,({5,0,}),}),({"Hastur the Unspeakable",1271521050,0,6,4,10,6,({6,0,}),}),
({"Hastur the Unspeakable",1271520978,0,4,4,10,4,({4,0,}),}),({"Hastur the Unspeakable",1271520910,0,1,1,10,1,({1,0,}),}),
({"Hastur the Unspeakable",1271520898,0,9,1,10,9,({9,0,}),}),({"large inner demon",1271357344,0,3,0,10,3,({3,2,}),}),
({"large inner demon",1271357336,0,1,3,10,1,({1,2,}),}),({"large inner demon",1271357334,0,7,3,10,7,({7,2,}),}),
({"large inner demon",1271357284,0,2,6,10,2,({2,12,}),}),({"large inner demon",1271357278,0,2,8,10,2,({2,12,}),}),
({"bloodguard",1267999881,0,10,0,10,10,({10,0,}),}),({"uzziel",1258168171,0,7,3,10,7,}),({"traveller",1256943054,0,5,5,10,5,}),})
or
code:
tags (["default":(["end":"</#CHAN#>","start":"<#CHAN#>",]),])
IE: It's basically JSON with an outer k/v pair, I'd think you're being disingenuous to say that you would just use a regex to parse JSON.

I'm sorry, I didn't read closely enough. I didn't see that lists and dicts were among the data types. Nevermind.

fourwood
Sep 9, 2001

Damn I'll bring them to their knees.

clearly not a horse posted:

EDIT: In hindsight, ignore this post and treat it like poo poo. The problem was just division by zero. :ughh:


I am having a small problem with floating point numbers here, and I know I am doing something terribly wrong - I just need help to point out the error.

Below is a small function for generating a crude approximation of eulers number, where a fraction of (1/n) is added for each iteration. I may be even wrong with my approach, but the point is the error I get during interpretation.

code:
from math import *

def e (depth):
    base = 1.0
    for n in range(depth):
        base += 1.0/n
        print base

When I run the function, I get this error:
File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 6, in e
ZeroDivisionError: float division

You may want to double check your math. Summing 1/n doesn't give you what you think it gives you. You shouldn't get division by zero even when n = 0.

Mr. Clark2
Sep 17, 2003

Rocco sez: Oh man, what a bummer. Woof.

Could someone please point me in the direction of a good dictionary tutorial in python 3? Something that would be geared more towards a beginner if possible. Thanks.

Lurchington
Jan 2, 2003

Forums Dragoon

Mr. Clark2 posted:

Could someone please point me in the direction of a good dictionary tutorial in python 3? Something that would be geared more towards a beginner if possible. Thanks.

it's not loading on my network for some reason, but this may be helpful: http://diveintopython3.org/native-datatypes.html

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Mr. Clark2 posted:

Could someone please point me in the direction of a good dictionary tutorial in python 3? Something that would be geared more towards a beginner if possible. Thanks.

Dive into Python is pretty good:

http://diveintopython3.org/native-datatypes.html#dictionaries

It's not a horrendously hard concept, really, but if there's a particular problem you're facing do ask.

e: welp. I guess we have consensus then.

Mr. Clark2
Sep 17, 2003

Rocco sez: Oh man, what a bummer. Woof.

Thanks guys. I'm trying to learn python (I have 0 prior programming experience), I've bought 2 separate books designed for beginners, but in each one as soon as the book starts to delve into dictionaries, I come to a dead end and find it impossible to do the assignments and activities at the ends of the chapters. I'm hoping that by reading about dictionaries from as many different sources as possible, it will begin to 'click' in my head.

b0lt
Apr 29, 2005

Mr. Clark2 posted:

Thanks guys. I'm trying to learn python (I have 0 prior programming experience), I've bought 2 separate books designed for beginners, but in each one as soon as the book starts to delve into dictionaries, I come to a dead end and find it impossible to do the assignments and activities at the ends of the chapters. I'm hoping that by reading about dictionaries from as many different sources as possible, it will begin to 'click' in my head.

What do you think dictionaries are? If you don't have a clear mental model of what they represent, you're bound to have trouble doing stuff with them.

wins32767
Mar 16, 2007

Mr. Clark2 posted:

Thanks guys. I'm trying to learn python (I have 0 prior programming experience), I've bought 2 separate books designed for beginners, but in each one as soon as the book starts to delve into dictionaries, I come to a dead end and find it impossible to do the assignments and activities at the ends of the chapters. I'm hoping that by reading about dictionaries from as many different sources as possible, it will begin to 'click' in my head.

You get lists, right? A rough and tumble way of looking at dictionaries is as though they were lists that can be indexed by objects (like strings) instead of just integers. Yes, slicing et al doesn't really apply, but it's a reasonable mental model for them until you get into the nitty gritty.

LuckySevens
Feb 16, 2004

fear not failure, fear only the limitations of our dreams

Dicionaries are just lists that are not ordered and accessible by a key, ala { key : value }. Just play with them, they're fairly simple to use and you can do some python tricks to extract the data into lists or get values using a string name etc etc.

Lurchington
Jan 2, 2003

Forums Dragoon

LuckySevens posted:

Dicionaries are just lists that are not ordered and accessible by a key, ala { key : value }. Just play with them, they're fairly simple to use and you can do some python tricks to extract the data into lists or get values using a string name etc etc.

And hey, once he's comfortable with Dictionaries, there's a bonus in 2.7/3.1 :)

http://docs.python.org/dev/py3k/whatsnew/3.1.html#pep-372-ordered-dictionaries
http://docs.python.org/dev/whatsnew/2.7.html#pep-372-adding-an-ordered-dictionary-to-collections

Stabby McDamage
Dec 11, 2005

Doctor Rope

fourwood posted:

You may want to double check your math. Summing 1/n doesn't give you what you think it gives you. You shouldn't get division by zero even when n = 0.

I think he's looking for sum([1.0/math.factorial(n) for n in range(N)]).

(If using Python < 2.6, then math.factorial isn't available, but def factorial(x): return x*factorial(x-1) if x>1 else 1 will do.)

Chris Awful
Oct 2, 2005

Tell your friends they don't have to be scared or hungry anymore comrades.

Chris Awful posted:

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:
Traceback (most recent call last):
  File "Z:\Goon\Goon.py", line 589, in <module>
    main()
  File "Z:\Goon\Goon.py", line 581, in main
    DrawGLScene()
  File "Z:\Goon\Goon.py", line 321, in DrawGLScene
    glDrawArrays(GL_QUADS, 4, 8)
WindowsError: exception: access violation reading 0x01AC5000

I think I resolved the issue. While searching Google for the symptoms, I came along this blog and found useful information in the comment section. There was talk about a flashing/flickering issue which was resolved by decreasing the count argument of glDrawArrays. This led me to reexamine glDrawArrays. Apparently, I misunderstood the index and count argument to mean range. So I changed glDrawArrays(GL_QUADS, 4, 8) to glDrawArrays(GL_QUADS, 4, 4) and the flickering and blending garbage has ceased.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Has anyone watched the MIT OpenCourseWare link in the OP? Can anyone attest to the quality and if its worth the time for a complete beginner?

Boblonious
Jan 14, 2007

I HATE PYTHON IT'S THE SHITTIEST LANGUAGE EVER PHP IS WAY BETTER BECAUSE IT IS MADE FOR THE WEB AND IT USES MYSQL WHICH IS ALSO USED BY GOOGLE AND THATS WHY ITS SO MUCH BETTER THAN PYTHON EVEN HTML IS A BETTER PROGRAMMING LANGUAGE THAN PYTHON

Hughmoris posted:

Has anyone watched the MIT OpenCourseWare link in the OP? Can anyone attest to the quality and if its worth the time for a complete beginner?

If it's the one I'm thinking of, I watched the first few a while back to gain some ideas (I recently had to TA for a similar course at a different institution) and remember thinking they were pretty good.

VirtuaSpy
Jul 26, 2002

OMG WTF FIREEEE!!
Regarding the discussion of ipython going on, I'm currently at the SciPy conference in Austin, and I noticed that everyone uses ipython instead of the regular default Python shell (in this part of the community at least), and I've learned a few tricks in it as well that I'll share:

Besides the already-mentioned ls, dir, rm, etc. magic commands and the ? and ?? to show docstrings and source, it has other magic commands such as (which can be used with or without the leading %):

%run - for running a script in the current dir, e.g., %run myscript.py; the variables/modules from that script can then be used interactively

%logstart - logs the previous and future inputs to a file, which I've been using in the tutorials to record the history

%timeit - used to benchmark a command or script some number of times (100, 1000, etc.; this is determined by how long the function takes to run) and returns the best of 3 loops:

code:
In [12]: timeit 2**32
10000000 loops, best of 3: 33.9 ns per loop
You can check out the other magic functions by typing % and hitting Tab.

Also, this shorthand "_" for the previous output (can be a value or an instance or whatever is returned):

code:
In [15]: 2**8
Out[15]: 256
In [16]: x = _
In [17]: x
Out[17]: 256
or this shorthand, which I thought was really cool:

code:
In [18]: 2**10
Out[18]: 1024
In [19]: x = Out[18]
In [20]: x
Out[20]: 1024
Also, tab-completion when importing modules and their classes/methods. Those are the tips/tricks that I remember off the top of my head.

The developers of ipython mentioned that they have overhauled most of ipython in the last 9 months, and that they plan to release a big update "soon". Overall, it seems to be a really powerful tool, and GUIs such as Spyder (http://packages.python.org/spyder/) are currently implementing IPython as their Python shell instead of the standard one, which is apparently very difficult to do. I'll definitely be using ipython more in my own work, and I can't wait for it to be implemented in Spyder.

king_kilr
May 25, 2007
Ipython is amazing :) For all you debug whores ipdb brings some of that awesome to debugging, just replace your import pdb with import ipdb and you're good to go. How was SciPy, by the way? Was David Beazley's keynote any good?

VirtuaSpy
Jul 26, 2002

OMG WTF FIREEEE!!
SciPy has been very cool so far, although this was my first time at SciPy, the talks have been awesome. There is one more day of talks tomorrow (including mine!, which details my use of Python in my research). There were about 190 attendees this year, up from 120 last year. And Beazley's keynote was good stuff - funny, informative, and good demos (SimpleXMLRPCserver, multiprocessing, and logging - he ran a demo logging script that tweeted its status when the job was "complete"). Travis' keynote is in the morning and should be good as well.

I know they are going to post the slides soon, the proceedings come out in August, and I think they are going to post videos as well, so I'll post those links here whenever they make those available in case anyone is interested.

Captain Capacitor
Jan 21, 2008

The code you say?
I've been experimenting with ipython recently, however neither the autoindent nor the tab key works for me. The tab key just displays the "Do you really want to display all 291 possibilities?".

For the record, this is on OSX 10.6, if anyone has any advice.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Captain Capacitor posted:

I've been experimenting with ipython recently, however neither the autoindent nor the tab key works for me. The tab key just displays the "Do you really want to display all 291 possibilities?".

For the record, this is on OSX 10.6, if anyone has any advice.

Press tab again or enter or something. Anyway, autocomplete is only good if you type part of the word first. Have you never used a shell before?

Captain Capacitor
Jan 21, 2008

The code you say?

Jonnty posted:

Press tab again or enter or something. Anyway, autocomplete is only good if you type part of the word first. Have you never used a shell before?

Perhaps I didn't make myself clear. When trying to tab over as part of normal indentation, and having not typed anything on the currently line, it attempts the autocomplete behavior.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Captain Capacitor posted:

Perhaps I didn't make myself clear. When trying to tab over as part of normal indentation, and having not typed anything on the currently line, it attempts the autocomplete behavior.

Oh, I see. Tab is used for autocomplete, so you can't indent with it. Are you sure autoindent isn't working? Have you tried typing something like "if True: <newline> print "a"" and seeing if it works?

Also remember that to unident, you have to press enter twice.

Captain Capacitor
Jan 21, 2008

The code you say?

Jonnty posted:

Oh, I see. Tab is used for autocomplete, so you can't indent with it. Are you sure autoindent isn't working? Have you tried typing something like "if True: <newline> print "a"" and seeing if it works?

Also remember that to unident, you have to press enter twice.

I think that autoindent is working, but it doesn't display. I say this because when I scroll back through my input history the lines are displayed with the tabs. The Control-I/Control-O for indent/unindent don't seem to work either.

devilmouse
Mar 26, 2004

It's just like real life.
I don't know if this still matters in 10.6/the current version of ipython, but at least a year ago, the default Apple install of readline in 10.5 didn't work with ipython, so you had to download the GNU version and compile it yourself. Maybe google around and see if that's still a problem?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



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:
import sqlite3
#register the type I want to NOT mangle
sqlite3.register_converter('DOUBLE', str)
connection = sqlite3.connect('etc...')
I tried using this as the converter:
code:
def dont_convert(this_thing):
   print this_thing
   return this_thing
And nothing was emitted

I also tried registering the converter on the name of the column:
code:
sqlite3.register_converter('amount', str)
But I'm still getting floats with rounding errors out of it.

Munkeymon fucked around with this message at 18:50 on Jul 3, 2010

Haystack
Jan 23, 2005





You know what would be nice?

Having,
code:
>>>[1,2,3] is a list
True
>>>[1,2,3] is not a list
False
as sugar for:
code:
>>>isinstance([1,2,3], list)
True
>>>not isinstance([1,2,3], list)
False
Granted, having "a" as a keyword would be trouble. Maybe "isa" and "isa not?" Has the added benefit of making your code sound like it has a bad Italian accent.

ATLbeer
Sep 26, 2004
Über nerd

Haystack posted:

You know what would be nice?

Having,
code:
>>>[1,2,3] is a list
True
>>>[1,2,3] is not a list
False
as sugar for:
code:
>>>isinstance([1,2,3], list)
True
>>>not isinstance([1,2,3], list)
False
Granted, having "a" as a keyword would be trouble. Maybe "isa" and "isa not?" Has the added benefit of making your code sound like it has a bad Italian accent.

>>> type([1,2,3]) is list
True
>>> type({1:2}) is dict
True

Haystack
Jan 23, 2005





ATLbeer posted:

>>> type([1,2,3]) is list
True
>>> type({1:2}) is dict
True

Yes, but,
code:
>>>class Balls:
    pass
>>>sack = Balls()
>>>type(sack) is Balls
False

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Haystack posted:

Yes, but,
code:
>>>class Balls:
    pass
>>>sack = Balls()
>>>type(sack) is Balls
False

code:
>>> class Balls(object):
...     pass
>>> sack = Balls()
>>> type(sack) is Balls
True
New-style classes, fool!

ATLbeer
Sep 26, 2004
Über nerd
Yes but,

Haystack posted:

Yes, but,
code:
>>>class Balls:
    pass
>>>sack = Balls()
>>>type(sack) is Balls
False

code:

>>> class Balls(object):
...     pass
... 
>>> sack = Balls()
>>> type(sack) is Balls
True

Haystack
Jan 23, 2005





Jonnty posted:

code:
>>> class Balls(object):
...     pass
>>> sack = Balls()
>>> type(sack) is Balls
True
New-style classes, fool!

:doh:

That'll teach me to be lazy with the interactive interpreter.

Boblonious
Jan 14, 2007

I HATE PYTHON IT'S THE SHITTIEST LANGUAGE EVER PHP IS WAY BETTER BECAUSE IT IS MADE FOR THE WEB AND IT USES MYSQL WHICH IS ALSO USED BY GOOGLE AND THATS WHY ITS SO MUCH BETTER THAN PYTHON EVEN HTML IS A BETTER PROGRAMMING LANGUAGE THAN PYTHON
Yeah, but it still wouldn't work for subclasses. Having some kind of shorthand for isinstance would still be nice.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
isa = isinstance

king_kilr
May 25, 2007
This was discussed on comp.lang.python a while ago, no one was really enthusiastic about it.

ATLbeer
Sep 26, 2004
Über nerd
http://www.python.org/download/releases/2.7/

quote:

  • An ordered dictionary type
  • New unittest features including test skipping and new assert methods
  • A much faster io module
  • Automatic numbering of fields in the str.format() method
  • Float repr improvements backported from 3.x
  • Tile support for Tkinter
  • A backport of the memoryview object from 3.x
  • Set literals
  • Set and dictionary comprehensions
  • Dictionary views
  • New syntax for nested with statements
  • The sysconfig module

http://docs.python.org/dev/whatsnew/2.7.html

Now, if I can only get our OPs team to upgrade production from 2.4...

qntm
Jun 17, 2009

ATLbeer posted:

Set literals

Ooooh! I've been doing lots of work with sets lately.

Actually, it says here it was backported from v3, which is weird, because I'm using v3 and it doesn't appear in the documentation.

Adbot
ADBOT LOVES YOU

Shaocaholica
Oct 29, 2002

Fig. 5E
How do I use non scrolling output? For instance, if I want to draw a progress bar in the terminal? Is there something I should be using other than print and sys.stdout.write()?

  • Locked thread