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
hey mom its 420
May 12, 2007

But he was saving them in the first example, sooo

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->
Some europython materials are up:

http://wiki.europython.eu/TalkMaterials

Waiting on the eckel videos myself, his talks look to be interesting.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

Bonus posted:

But he was saving them in the first example, sooo
Read the code again -- he's only saving the label so he can call grid() upon it. In the second example, which presumably works, they're not saved at all.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

tef posted:

Some europython materials are up:

http://wiki.europython.eu/TalkMaterials

Waiting on the eckel videos myself, his talks look to be interesting.

Where are the videos being posted?

tef
May 30, 2004

-> some l-system crap ->
I assume they will appear here shortly:
http://wiki.europython.eu/RecordedTalks

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

supster posted:

Where are the videos being posted?

They'll probably be up on blip.tv

awesomepanda
Dec 26, 2005

The good life, as i concieve it, is a happy life.
what is the difference between a statement and an expression?

how do eval and exec differ? why is assert needed? why not just use an if statement?

Scaevolus
Apr 16, 2007

CrazyPanda posted:

what is the difference between a statement and an expression?
It has to do with how the parser works. You can use expressions most anywhere, but statements have to be on their own line (logical line, not physical I know, semicolons). You can embed expressions in each other, but you can't embed statements in each other.

These are a few statements:
code:
a = 5
if foo:
return "okay"
these are expressions:
code:
1
5 + 3
range(100)[30]
"what".find("a")
You might want to read Python's grammar.

quote:

how do eval and exec differ?
exec starts a statement, eval is a function (part of an expression).

quote:

why is assert needed? why not just use an if statement?
For debugging. It's helpful to have it in the language instead of as a macro or function. if not should_always_be_true: throw_some_error is less helpful than assert(should_always_be_true).

Scaevolus fucked around with this message at 05:38 on Jul 5, 2009

awesomepanda
Dec 26, 2005

The good life, as i concieve it, is a happy life.

Scaevolus posted:


You might want to read Python's grammar.


Hi Scaevolus thank you for your response. im really new to programming so i dont understand the grammar page. is there something like grammar lite for beginners?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

CrazyPanda posted:

Hi Scaevolus thank you for your response. im really new to programming so i dont understand the grammar page. is there something like grammar lite for beginners?

Yes, precisely one section up: http://docs.python.org/reference/index.html

Scaevolus
Apr 16, 2007

CrazyPanda posted:

Hi Scaevolus thank you for your response. im really new to programming so i dont understand the grammar page. is there something like grammar lite for beginners?

It's in pseudo-BNF format, but it's not that important for you to understand right now.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

CrazyPanda posted:

what is the difference between a statement and an expression?
An expression has a value, such as 1, "hello", or None. A statement has no value, it's only purpose is to perform some side effect.

CrazyPanda posted:

how do eval and exec differ?
eval will execute an expression, and return its value. exec will execute a series of statements, and either has no result (2.x statement) or returns None (3.x function)

CrazyPanda posted:

why is assert needed? why not just use an if statement?
It's not needed, but is merely a small shortcut. These two blocks are equivalent:

code:
assert a == "ok", "a isn't OK!"

if __debug__:
    if a != "ok":
      raise AssertionError("a isn't OK!")
where __debug__ is a special flag, enabled by default, disabled by the -O command-line option.

PrinceofLowLight
Mar 31, 2006
Newbie with python. I'm trying to automate a repetitive task, analyzing some data that's too big to put in Excel. The text files range from 6-30 megs. It usually crashes notepad too, but opens with wordpad. Will Python be able to handle this?

The lines are arranged like so:

8.79669 5 5 5 5


I want to make it that so that python associates each column with its row. Then, if the third column is 5, to take the next 20 values of the third column and then average them, then spit that out into an output file with a small header. I'm not sure how to go about this.

Thanks!

deedee megadoodoo
Sep 28, 2000
Two roads diverged in a wood, and I, I took the one to Flavortown, and that has made all the difference.


PrinceofLowLight posted:

Newbie with python. I'm trying to automate a repetitive task, analyzing some data that's too big to put in Excel. The text files range from 6-30 megs. It usually crashes notepad too, but opens with wordpad. Will Python be able to handle this?

The lines are arranged like so:

8.79669 5 5 5 5


I want to make it that so that python associates each column with its row. Then, if the third column is 5, to take the next 20 values of the third column and then average them, then spit that out into an output file with a small header. I'm not sure how to go about this.

Thanks!

Python isn't a front end like notepad. You'll be able to open any file as long as your machine has the memory for it and you've coded the operations correctly.

PS - notepad can't open the file because it has a limit on file size. Wordpad has a much higher limitation, if it has one at all.

edit: also, for what you're describing you really only need to read one row at a time, which is fairly trivial.

deedee megadoodoo fucked around with this message at 21:59 on Jul 5, 2009

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

HatfulOfHollow posted:

Python isn't a front end like notepad. You'll be able to open any file as long as your machine has the memory for it and you've coded the operations correctly.

PS - notepad can't open the file because it has a limit on file size. Wordpad has a much higher limitation, if it has one at all.

Notepad++

But if he's having trouble opening it on a modern Excel (I think 2003 was the first to get rid of the 65k row limit) then the files are huge.

tripwire
Nov 19, 2004

        ghost flow

PrinceofLowLight posted:

Newbie with python. I'm trying to automate a repetitive task, analyzing some data that's too big to put in Excel. The text files range from 6-30 megs. It usually crashes notepad too, but opens with wordpad. Will Python be able to handle this?

The lines are arranged like so:

8.79669 5 5 5 5


I want to make it that so that python associates each column with its row. Then, if the third column is 5, to take the next 20 values of the third column and then average them, then spit that out into an output file with a small header. I'm not sure how to go about this.

Thanks!
You could do this regular expressions incredibly easily. If you are set on doing this with python just check the re module documentation.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
Regular expressions? Seriously?
http://docs.python.org/library/csv.html#module-csv is probably the easiest.

tripwire
Nov 19, 2004

        ghost flow

Lonely Wolf posted:

Regular expressions? Seriously?
http://docs.python.org/library/csv.html#module-csv is probably the easiest.

Well I was going to suggest a shell script. I'd just use "cut" or "sed" but it sounds like hes on windows. I didn't realize there was an actual CSV module (obviously he should use that if it exists), but when I did something like this before for parsing logs I ended up comparing pyparsing (obviously way overkill) and pythons re module and the re module wasn't too far behind doing it on the command line with cat and sed.

Lord Purple
Mar 7, 2006

Remove your eyes...
I am having a bit of a hard time with python's threading functionality. I am trying to trying to build an object inside of gui class and the constructor takes a while to create the object. Without threading, the entire gui freezes up while waiting for the object to be created. Is there any way I can assign the constructor to a thread and then set the created object from the thread to an object inside of my gui class when it has completed?

Balam
Dec 12, 2006
code:
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> y
[]
>>> for object in x:
	y.append(object)
	x.remove(object)
>>> y
[1, 3, 5, 7, 9]
>>> x
[2, 4, 6, 8, 10]
I can't figure out why not every object is being added and removed to and from the respective list? Is there a better way to move and delete items across list?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
code:
x,y = y,x

hlfrk414
Dec 31, 2008
Isn't obvious? Each time you remove something from x, it skips the next item in x. You're not supposed to modify something you're currently iterating over. Why not just get a copy and then clear x? Or just point y to it? Why must you do it the manual way?

Switch the references:
x, y = y, x

Switch in place, changing the lists, not the references:
y.extend(x)
del x[:]

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Balam posted:

code:
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> y
[]
>>> for object in x:
	y.append(object)
	x.remove(object)
>>> y
[1, 3, 5, 7, 9]
>>> x
[2, 4, 6, 8, 10]
I can't figure out why not every object is being added and removed to and from the respective list? Is there a better way to move and delete items across list?

I'm guessing when you remove the object from the list, you are altering the size of the list but the internal iterator doesn't adjust and it stops once it reaches the end of the list.
code:
i = 0
object = 1
remove
listsize = 9

i = 1
object = 3
remove
listsize = 8

i = 2
object = 5
remove
listsize = 7

i = 3
object = 7
remove
listsize = 6

i = 4
object = 9
remove
listsize = 5

i = 5
stop

Balam
Dec 12, 2006

hlfrk414 posted:

Why must you do it the manual way?

Actually, my goal is to simulate a deck of cards and handing them out to two empty lists representing the hands for the two players. I suppose I could just random.shuffle the deck and put the first half in one list and the second half in another list, then redefine the deck as an empty list, unless there is a good way to simulate passing a card out one at time until the deck list is empty. Other than that I was just curious why it wasn't working. Thanks for the help guys.

Balam
Dec 12, 2006

MEAT TREAT posted:

I'm guessing when you remove the object from the list, you are altering the size of the list but the internal iterator doesn't adjust and it stops once it reaches the end of the list.

Ok, that makes senses, assuming that's the way it works. Thanks for explaining it.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...
For general public information, these are some notes about Titanium. Titanium Appcelerator is an architecture, set of libraries and tools for making rich web applications, be they normal web apps or standalone desktop / mobile phone apps.

What's the Python link? Python is one of the languages they support. In theory, you can have this very impressive rich visual apps driven by a Python backend. Since working with Plone is just one big headache (that you manage to get cured just before the next big headache comes along), I've been spending some time playing with it.

- A variety of back ends are supported, for a variety of languages
- For Python that means Google App Engine and Pylons. Django is mentioned, but the necessary code is missing.
- And while "normal" web applications are supported, most of the focus is obviously on desktop and phone development
- And actually the Pylons examples don't work as written. It's missing the necessary library for Appcelerator.
- Install the necessary library for Appcelerator via easy_install and you get a different error - the library version is too new.
- Argh
- The documentation for Appcelerator is patchy, incomplete and outdated. Which makes it a perfect match for Pylons ...
- And despite advice, writing your own service broker is far from straightforward

In summary - not today. I'm going back to Django and Dojo, where documentation and examples aren't afterthoughts.

Benji the Blade
Jun 22, 2004
Plate of shrimp.

Avenging Dentist posted:

code:
x,y = y,x

Or if you need to change the pointed-to lists and not just the names x and y

code:
x[:], y[:] = y[:], x[:]
AD's suggestion is better if just swapping the names does the trick, since it doesn't involve copying.

Kire
Aug 25, 2006
I'm having a strange issue with pygame. I copied an example straight out of the book, and it works just fine on my home computer, but when I use my work machine (a very locked down xp box), I get:
code:
Traceback (most recent call last):
  File "C:\Python26\hello pygame.py", line 10, in <module>
    screen = pygame.display.set_mode((640, 480), 0, 32)
error: No available video device
For a while i was also able to do some simple drawings of aalines and polygons using some other pygame tutorials, and they worked on my work machine, but then one day they just started giving me errors. I assume the admins changed something with the firewall or virus protection? Is there something I can do to get around this?

tripwire
Nov 19, 2004

        ghost flow

Kire posted:

I'm having a strange issue with pygame. I copied an example straight out of the book, and it works just fine on my home computer, but when I use my work machine (a very locked down xp box), I get:
code:
Traceback (most recent call last):
  File "C:\Python26\hello pygame.py", line 10, in <module>
    screen = pygame.display.set_mode((640, 480), 0, 32)
error: No available video device
For a while i was also able to do some simple drawings of aalines and polygons using some other pygame tutorials, and they worked on my work machine, but then one day they just started giving me errors. I assume the admins changed something with the firewall or virus protection? Is there something I can do to get around this?
Sounds like an issue with pygame. If they have a forum or mailing list try to see if other people are experiencing a similar issue.. failing I would skim the pygame source for whatever is throwing that error to see what conditions could cause it to fail.

Scaevolus
Apr 16, 2007

Is this fullscreen? If so, try making it windowed.

My only guess is that a policy was added to prevent resizing the screen, and pygame fails when it tries to do it.

Habnabit
Dec 30, 2007

lift your skinny fists like
antennas in germany.

scrabbleship posted:

I am having a bit of a hard time with python's threading functionality. I am trying to trying to build an object inside of gui class and the constructor takes a while to create the object. Without threading, the entire gui freezes up while waiting for the object to be created. Is there any way I can assign the constructor to a thread and then set the created object from the thread to an object inside of my gui class when it has completed?

What is it that takes so long? It seems pretty odd that just instantiating an object would necessitate spawning a whole new thread. In any case, GUI frameworks really don't work well with threading. If it doesn't totally blow up, there's plenty of other things that can go wrong. My guess is you're using GTK, and it has things in place to prevent people from doing stupid things like that.

Zedlic
Mar 10, 2005

Ask me about being too much of a sperging idiot to understand what resisting arrest means.
Here's a question that may or may not be silly.

Let's say I'm writing a little card game in Python. I have my Card class that takes two integers that denote rank and suit. The __init__ then assigns the passed parameters to variables.

My question is: Should I be validating the parameters passed when creating an object of this class, and if yes then how can I do that? I want something to happen if somewhere later in the code a Card object with rank 243 and suit -5 is created, but as I understand __init__ always returns None so I can't really be making any checks there. Right now I just get a Card object with rank 243 and suit -5.

How do I check if the parameters passed when objects are created are valid?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Zedlic posted:

How do I check if the parameters passed when objects are created are valid?

Exceptions, dude. (Or assertions.)

Zedlic
Mar 10, 2005

Ask me about being too much of a sperging idiot to understand what resisting arrest means.

Avenging Dentist posted:

Exceptions, dude. (Or assertions.)

Well don't I feel silly now. My mind was set on __init__ having to return something to signify an error. Thanks.

I told you it was a silly question.

Kire
Aug 25, 2006

Scaevolus posted:

Is this fullscreen? If so, try making it windowed.

My only guess is that a policy was added to prevent resizing the screen, and pygame fails when it tries to do it.

It's windowed, so that's not it.


Another question: I've never been able to get the event QUIT to work. All my pygame windows just hang when I click to close them, and I have to force-quit. I'm using the code right out of the book:
code:
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
In other examples, even when I clearly make the while loop False I still have the same problem of frozen windows. Here's the response from IDLE when it happens:
code:
Traceback (most recent call last):
  File "C:\Python26\hello pygame.py", line 20, in <module>
    exit()
SystemExit

Alcohol Kills
Feb 29, 2008

by Peatpot

Kire posted:

It's windowed, so that's not it.


Another question: I've never been able to get the event QUIT to work. All my pygame windows just hang when I click to close them, and I have to force-quit. I'm using the code right out of the book:
code:
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
In other examples, even when I clearly make the while loop False I still have the same problem of frozen windows. Here's the response from IDLE when it happens:
code:
Traceback (most recent call last):
  File "C:\Python26\hello pygame.py", line 20, in <module>
    exit()
SystemExit

I think this is related to Tkinter, because I have the same "hanging window" problem any time I "update" an element of the window before actually packing the window up.

Maybe somebody else could be more helpful here.

ATLbeer
Sep 26, 2004
Über nerd
Has anyone seen a pure Python implementation of a round robin archive type system anywhere?

I have a project that RRD would work well for except the amount of data is going to cause disk I/O issues if I used the standard RRD package as nice as it is.

Gork da Ork
Jul 26, 2008
I've been using some spare time to practice python by trying to find better, more elegant ways to solve a problem. Really, it's quite a lot of fun. This is such a breeze compared to C!


Challenge: packet parsing. What's the cleanest, most elegant way to parse binary packet formats?

Real-world motivation: you have to interface with an existing binary format, be it a network protocol or file. The thing is vast: implementing all of it in Python will take effort. Better make sure you're doing it right from the start.

Goals, in order of difficulty:
1. Start with static-length, byte-aligned packets.
2. Move on to deal with dynamic length, embedded null-terminated strings, and occasional variations.
3. Move on to really dynamic/polymorphic packets with ever-changing encoding based on what came before (eg, an enum byte specifying how to decode the data that comes afterwards... many times in the same packet).
4. Just to make it worse, remove the byte-alignment assumption. The protocol is ancient, and bit-packed to save space!

As the goals get harder, the problem of storing the data becomes more important. Using a function that returns a tuple of values is sufficient for small packets, but dictionaries must be used for the big polymorphic ones. There may be some middle ground for packet-classes in between: I wonder what the overhead is compared to dictionaries.


So far, this is what I've learned:


1 is covered completely by the struct module! Fast (I assume it's implemented in C) cool syntactic sugar (a,b,c,d = unpack("4I", data)) and great support for padding/byte order.

2 raises the fun question, 'what is the neatest way to chop a chunk of data off a list?' (Writing a function that does it doesn't count.)
So far, my best solution is "chunk,data = data[:n],data[n:]" where n is the size of your chunk. It looks a little ugly, but is reasonably error-proof and succinct.

Chopping off null-terminated strings has a far more satisfying solution: "chunk,data = data.split('\0', 1)". The 1 is the maximum number of times to split - necessary since len(result) must be 2. It provides a great way to cut off several nullstrings at once - "firstname,lastname,data = data.split('\0', 2)

3 is where it gets interesting. One approach I like the feel of is parsing via a generator that takes the data as input, and yields (key,value) pairs for dictionary insertion. Parsing a packet is then "for (k,v) in parseGen(data): packet[k]=v". This seems to provide a clean way to split the parsing into modules if need be (by invoking "for pair in parseGen2(data): yield pair" inside parseGen) and lets you parse a packet partially (if you're only interested up to a certain point) if you want.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

ATLbeer posted:

Has anyone seen a pure Python implementation of a round robin archive type system anywhere?

I have a project that RRD would work well for except the amount of data is going to cause disk I/O issues if I used the standard RRD package as nice as it is.

All I know of are:

http://code.google.com/p/pyrrd/
http://code.google.com/p/rrdpy/

The latter is from Corey Goldberg, who is wicked smart - he maintains/wrote PyLot http://www.pylot.org/

Adbot
ADBOT LOVES YOU

Scaevolus
Apr 16, 2007

Both of these just wrap rrdtool.

  • Locked thread