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
Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Centipeed posted:

I tried Project Euler when I was first getting into a programming language, but the problem is that most of the thinking you have to do (Or I had to do, at least) was mathematical rather than "How do I do this with the language I've got in front of me?". Did anyone else find this?

I can see where you're coming from, but a lot of languages have different constructs that can help you solve the problem easier than if you had used another language. The hard part is figuring out how to apply those constructs to the problem at hand.

Also remember that even if you found a solution to the problem, that depending on the paradigm your language uses, you might not be able to implement it the way you had solved it.

Adbot
ADBOT LOVES YOU

Pooball
Sep 21, 2005
Warm and squishy.

MEAT TREAT posted:

I can see where you're coming from, but a lot of languages have different constructs that can help you solve the problem easier than if you had used another language. The hard part is figuring out how to apply those constructs to the problem at hand.

Also remember that even if you found a solution to the problem, that depending on the paradigm your language uses, you might not be able to implement it the way you had solved it.

Can you give an example? I've done twenty Project Euler problems but haven't noticed any use for any traditional language features like object orientation, higher-order functions, code as data. The Python, Java and Haskell solutions are more or less the same as the C.

The nicer solutions use a language with support for abstract algebra such as Mathematica or PARI/GP.

hey mom its 420
May 12, 2007

The Haskell solutions use higher-order functions all the time and they're very much different from the C or Java solutions.
Project Euler is good for getting used to the syntax and some basic idioms of a language.
I used Project Euler when learning Haskell and I think it's especially good for learning it because it makes you use infinite lists, higher-order functions, laziness and forces you to think in a functional instead of an imperative manner.
Anyway, I think it's good for getting warmed up with a language. But once you've solved 80 or so problems, it's not about whether you can implement the solution in your programming language of choice, it's about whether you know a mathematical trick or method behind some problem, so that's why I stopped solving them around then.

ahobday
Apr 19, 2007

Someone needs to put together a Project Euler type website which doesn't have a focus on maths. I don't know what the possibilities would be, though. Just some simple programming problems that don't aim to take advantage of ANY particular language's abilities. Hello World 2.0 type challenges.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Centipeed posted:

Someone needs to put together a Project Euler type website which doesn't have a focus on maths. I don't know what the possibilities would be, though. Just some simple programming problems that don't aim to take advantage of ANY particular language's abilities. Hello World 2.0 type challenges.

But... that's what makes the problems work. Sure, you could make up elaborate word problems that the solution is "implement this algorithm", but that's exactly what the math problems allow you to do without all of the extra fluff.

It's not like you need a particularly rich math background. Just look up the solutions to the problems on wiki or in a math text, and then implement the solution in your language of choice.

Ultimately, if you can't do the math described in the problems, then you're missing a prerequisite to good software engineering. Here's an opportunity to get better at both!

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

tbradshaw posted:

But... that's what makes the problems work. Sure, you could make up elaborate word problems that the solution is "implement this algorithm", but that's exactly what the math problems allow you to do without all of the extra fluff.

It's not like you need a particularly rich math background. Just look up the solutions to the problems on wiki or in a math text, and then implement the solution in your language of choice.

Ultimately, if you can't do the math described in the problems, then you're missing a prerequisite to good software engineering. Here's an opportunity to get better at both!

Not every programming problem involves implementing algorithms. It could be something like "design a system that does this:"

ndrz
Oct 31, 2003

Here you go:

UVa Online Judge

It only supports Java, C, C++, and PASCAL (yeah I know it's a python thread, but if you just want problems to solve, you can do them without submitting) but there are TONS of great problems here. We had to do several for a Data Structures and Algorithms class, and they're pretty fun.

Not only do you get to solve problems, it analyzes how fast your program is and ranks your against other people that have solved the problems. My instructor (Glen Downing) is #1 for a bunch of problems (or was last year anyways).

ndrz fucked around with this message at 04:05 on Apr 25, 2009

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

BeefofAges posted:

Not every programming problem involves implementing algorithms. It could be something like "design a system that does this:"

You might want to revisit what "algorithm" means. :D

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

tbradshaw posted:

You might want to revisit what "algorithm" means. :D

shhh, you know what I meant!

Luigi Thirty
Apr 30, 2006

Emergency confection port.

e: I am dumb, nevermind.

Luigi Thirty fucked around with this message at 16:27 on Apr 25, 2009

LE
Oct 19, 2002

Okay, here is my first stupid newbie python question. Let there be no mistake about it, I am very green with python.

Here's the deal: I am trying to run through this project that shows how python can be used with curses to make some spiffy apps. It looks straightforward enough, and so I make a go at it. In the middle of the page, there is this bit of code:

code:
import curses

myscreen = curses.initscr()

myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()

curses.endwin()
The tutorial is all "YEAH JUST GO AHEAD AND RUN IT'LL WORK DO IT DO IT DO IT" and so I did! And it bombs out with this:

code:
Traceback (most recent call last):
  File "curses.py", line 1, in <module>
    import curses
  File "~/Python/curses.py", line 3, in <module>
    myscreen = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I've tried running that little thing on three different platforms (Mac OS X, CentOS, & Debian), and they all generate the same error.

I'm thinking that it is something not too difficult to take care of, but I don't understand what is causing it. I can say with total certainty that curses is installed on OS X.

he;lp

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

LE posted:

Okay, here is my first stupid newbie python question. Let there be no mistake about it, I am very green with python.

Here's the deal: I am trying to run through this project that shows how python can be used with curses to make some spiffy apps. It looks straightforward enough, and so I make a go at it. In the middle of the page, there is this bit of code:

code:
import curses

myscreen = curses.initscr()

myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()

curses.endwin()
The tutorial is all "YEAH JUST GO AHEAD AND RUN IT'LL WORK DO IT DO IT DO IT" and so I did! And it bombs out with this:

code:
Traceback (most recent call last):
  File "curses.py", line 1, in <module>
    import curses
  File "~/Python/curses.py", line 3, in <module>
    myscreen = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I've tried running that little thing on three different platforms (Mac OS X, CentOS, & Debian), and they all generate the same error.

I'm thinking that it is something not too difficult to take care of, but I don't understand what is causing it. I can say with total certainty that curses is installed on OS X.

he;lp

Works on my machine, stock python 2.5.1 from Apple (OS/X) what version (python --version) are you running?

tripwire
Nov 19, 2004

        ghost flow

LE posted:

Okay, here is my first stupid newbie python question. Let there be no mistake about it, I am very green with python.

Here's the deal: I am trying to run through this project that shows how python can be used with curses to make some spiffy apps. It looks straightforward enough, and so I make a go at it. In the middle of the page, there is this bit of code:

code:
import curses

myscreen = curses.initscr()

myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()

curses.endwin()
The tutorial is all "YEAH JUST GO AHEAD AND RUN IT'LL WORK DO IT DO IT DO IT" and so I did! And it bombs out with this:

code:
Traceback (most recent call last):
  File "curses.py", line 1, in <module>
    import curses
  File "~/Python/curses.py", line 3, in <module>
    myscreen = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I've tried running that little thing on three different platforms (Mac OS X, CentOS, & Debian), and they all generate the same error.

I'm thinking that it is something not too difficult to take care of, but I don't understand what is causing it. I can say with total certainty that curses is installed on OS X.

he;lp
I haven't used this library myself but idly speculating, maybe that method is old and deprecated. Look through the documentation for a similar sounding method or some information on what to do in the new version. To see which things are available from the curses module once you've imported it, you can do this in an interactive session:
>>>import curses
>>>dir(curses)

It should print a list of every class or function contained in curses. You can apply dir() to any of the elements of this list if they contain new classes or functions as well.

No Safe Word
Feb 26, 2005

LE posted:

Okay, here is my first stupid newbie python question. Let there be no mistake about it, I am very green with python.

Here's the deal: I am trying to run through this project that shows how python can be used with curses to make some spiffy apps. It looks straightforward enough, and so I make a go at it. In the middle of the page, there is this bit of code:

code:
import curses

myscreen = curses.initscr()

myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()

curses.endwin()
The tutorial is all "YEAH JUST GO AHEAD AND RUN IT'LL WORK DO IT DO IT DO IT" and so I did! And it bombs out with this:

code:
Traceback (most recent call last):
  File "curses.py", line 1, in <module>
    import curses
  File "~/Python/curses.py", line 3, in <module>
    myscreen = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I've tried running that little thing on three different platforms (Mac OS X, CentOS, & Debian), and they all generate the same error.

I'm thinking that it is something not too difficult to take care of, but I don't understand what is causing it. I can say with total certainty that curses is installed on OS X.

he;lp


Ho ho, I'm going to catch this one first yet again.

:siren: Don't name your file curses.py :siren:

When you do import curses it looks in the current directory first for a file named curses.py and in your case it's finding itself (yes, it can do this) and loading that as the curses module. If you rename your file it won't find something named curses.py in the current directory and it will go ahead and look on the PYTHONPATH for a curses.py to import and it should find the appropriate one.

edit: put in the quote and sirens to hope LE didn't miss this right below me...

No Safe Word fucked around with this message at 02:03 on Apr 28, 2009

LE
Oct 19, 2002

m0nk3yz posted:

Works on my machine, stock python 2.5.1 from Apple (OS/X) what version (python --version) are you running?

code:
lappy:Python LE$ python --version
Python 2.5.4

lappy:Python LE$ uname -a
Darwin <SNIP> 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386

LE
Oct 19, 2002

No Safe Word posted:

Ho ho, I'm going to catch this one first yet again.

Don't name your file curses.py

When you do import curses it looks in the current directory first for a file named curses.py and in your case it's finding itself (yes, it can do this) and loading that as the curses module. If you rename your file it won't find something named curses.py in the current directory and it will go ahead and look on the PYTHONPATH for a curses.py to import and it should find the appropriate one.

Yep, you got it. Thank you! :neckbeard:

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

No Safe Word posted:

Ho ho, I'm going to catch this one first yet again.

:siren: Don't name your file curses.py :siren:

When you do import curses it looks in the current directory first for a file named curses.py and in your case it's finding itself (yes, it can do this) and loading that as the curses module. If you rename your file it won't find something named curses.py in the current directory and it will go ahead and look on the PYTHONPATH for a curses.py to import and it should find the appropriate one.

edit: put in the quote and sirens to hope LE didn't miss this right below me...

D'oh. So painfully obvious.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

outlier posted:

The situation is that I have some 3rd party code calling a commandline program, waiting for the results, then munging them in various ways before returning them to a webservice. And when the user gets bored and terminates the request, the called commandline process seems to hang around. Curious.

Following myself up again: after perusing the code for subprocess, it seems that if a subprocess is not completed by the time the owning object is disposed of, it gets stashed into a list in subprocess called "_active", where it sits until checked by the next call to subprocess. Weird. Anyone have any insight on this?

No Safe Word
Feb 26, 2005

m0nk3yz posted:

D'oh. So painfully obvious.

Yeah, but obviously not uncommon since the same thing happened a little over a week ago :)

It used to bite me pretty badly when I'd name my C files test.c and just run make test and try and run it.

test is a shell builtin that will get precedence and make test compiles test.c into a binary named test

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"
Is there any reasonable way to release a library for both Python 2 and 3? From what I can tell from the documentation and mailing list, while some code might work unmodified on 2.6 and 3.0, getting 2.4 and 2.5 code to work is unlikely even with 2to3. PyPI doesn't seem to use the version for source uploads, so I can't upload a 2.x and 3.x tarball. I'm playing with the idea of only releasing for 3.x from now on, but there's probably still many people stuck with version 2.

I know there are a few people here that maintain large Python libraries, what do you suggest?

yippee cahier
Mar 28, 2005

I want to use a C++ library (libraw) in my program. Is there any way to import a .h file for use with ctypes or do I have to recreate every struct by hand? I have a very very small amount of cython experience and this page looks so promising: http://docs.cython.org/docs/external_C_code.html#external-declarations

Am I on the right track?

Scaevolus
Apr 16, 2007

sund posted:

Is there any way to import a .h file for use with ctypes or do I have to recreate every struct by hand?

http://docs.cython.org/docs/external_C_code.html?highlight=extern#referencing-c-header-files ?

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Janin posted:

Is there any reasonable way to release a library for both Python 2 and 3? From what I can tell from the documentation and mailing list, while some code might work unmodified on 2.6 and 3.0, getting 2.4 and 2.5 code to work is unlikely even with 2to3. PyPI doesn't seem to use the version for source uploads, so I can't upload a 2.x and 3.x tarball. I'm playing with the idea of only releasing for 3.x from now on, but there's probably still many people stuck with version 2.

I know there are a few people here that maintain large Python libraries, what do you suggest?

This really depends on your goal - if you goal is 2.4/2.5 compatibility, you're not going to be able to support 3.x without significant changes. If you can stick to 2.6 support only, 3.0 support in the same code base is pretty simple (minus the bytes/string stuff). You can upload to PyPI and mark it with the trove classifiers Programming Language :: Python :: 3/Programming Language :: Python :: 2.6 and people should be able to find it.

I've seen people actually upload two packages - python2-pkg and python3-pkg, but that's inefficient. If I were you, I would probably only support 2.6 and 3.x in the same package. YMMV - I would not however, just upload a 3k version.

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"

m0nk3yz posted:

This really depends on your goal - if you goal is 2.4/2.5 compatibility, you're not going to be able to support 3.x without significant changes. If you can stick to 2.6 support only, 3.0 support in the same code base is pretty simple (minus the bytes/string stuff). You can upload to PyPI and mark it with the trove classifiers Programming Language :: Python :: 3/Programming Language :: Python :: 2.6 and people should be able to find it.

I've seen people actually upload two packages - python2-pkg and python3-pkg, but that's inefficient. If I were you, I would probably only support 2.6 and 3.x in the same package. YMMV - I would not however, just upload a 3k version.

It has to support 2.4 and 2.5. Supporting 2.6 is very easy, since it's mostly backwards-compatible. I want to have a version for 3.0, since apparently the 3.0 standard library's implementation of this format has some issues, but that's not possible if it involves sacrificing support for 2.4.

Splitting it into two separate packages sounds like a good idea -- do you think it'd be better to have 2.x `libname` and `python3-libname`, or vice-versa?

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Janin posted:

Splitting it into two separate packages sounds like a good idea -- do you think it'd be better to have 2.x `libname` and `python3-libname`, or vice-versa?

this sounds fine for now, but remember to install as libname, even if the name on pypi is python3-libname

I swear there was discussion about enhancing pypi to support this stuff, but I can't recall

king_kilr
May 25, 2007

m0nk3yz posted:

this sounds fine for now, but remember to install as libname, even if the name on pypi is python3-libname

I swear there was discussion about enhancing pypi to support this stuff, but I can't recall

PyPi needs about a dozen such enhancements, such as this, or recognizing that a package is a beta and thus installing the previous stable release by default(figuring that one out during the Django 1.1 alpha release was fun...)

ahobday
Apr 19, 2007

Can someone explain decorators to me as if I were a child? I went through the first couple pages of the Pyglet tutorial, and they make use of them, but I've looked around at explanations and all of them cause me to start zoning out half way through.

So, if it's at all possible, an explanation of Decorators that a five year old would understand.

Allie
Jan 17, 2004

Centipeed posted:

Can someone explain decorators to me as if I were a child? I went through the first couple pages of the Pyglet tutorial, and they make use of them, but I've looked around at explanations and all of them cause me to start zoning out half way through.

So, if it's at all possible, an explanation of Decorators that a five year old would understand.

A function is a list of instructions. A decorator changes that list - sometimes it adds extra instructions that happen first, or instructions to do last. Sometimes a decorator takes that first list of instructions and takes notes about them. Other times decorators don't do any of that - sometimes they change what happens when it's finished doing the list.

Or in less subtle terms: a decorator is a function that takes a function as its first argument, and returns a function. This allows you to change the original function. You can do whatever you want inside the decorator. Creating wrappers is common. Another usage is for marking the function in a registry. You could also change the return value using a wrapper.

For example, the Django web framework has the decorator @login_required. If you use this with a view function (which displays pages), if Django sees that the user isn't logged in, it doesn't execute the original function at all. Instead, it sends them to a login page, and then back to the original page. The decorator does this by wrapping the original function.

A more complex example: You can use decorators to memoize a function. What this means is that it caches return values from the function. If you call the function with the same arguments ten times, it'll execute the function only the first time, and give you back the cached value for the following nine times. This also makes use of a wrapper.

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"

Centipeed posted:

Can someone explain decorators to me as if I were a child? I went through the first couple pages of the Pyglet tutorial, and they make use of them, but I've looked around at explanations and all of them cause me to start zoning out half way through.

So, if it's at all possible, an explanation of Decorators that a five year old would understand.

code:
@my_decorator
def my_func(): return "balls"

# is the same as
def my_func(): return "balls"

my_func = my_decorator (my_func)

Milde posted:

Or in less subtle terms: a decorator is a function that takes a function as its first argument, and returns a function. This allows you to change the original function. You can do whatever you want inside the decorator. Creating wrappers is common. Another usage is for marking the function in a registry. You could also change the return value using a wrapper.
Decorators don't have to return functions, they can return anything.

Allie
Jan 17, 2004

Janin posted:

Decorators don't have to return functions, they can return anything.

Yes, you might use it to return some other kind of callable object. You could return any other type of object too, I suppose, though I'm not sure I've ever seen that done.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
The only time I've seen a decorator return a non-callable is to return a property type in a class body.

The Evan
Nov 29, 2004

Apple ][ Cash Money
In interactive mode, when I reload a module that imports other modules, how do I reload those imported modules? Should I just reload all of them?

tayl0r
Oct 3, 2002
This post brought to you by SOE
Ok I'm having an interesting problem trying to get some code to run on Google App Engine (Python). It works fine in the SDK locally, just not when I upload it.

I downloaded itunes-connect-scraper and got it to work locally in the google app engine SDK.
http://code.google.com/p/itunes-connect-scraper/

Then I hacked it up some and made it write to the GAE data store. I've uploaded it to my real GAE application but unfortunately it doesn't work there.

I've put in a bunch of logging and debugging and I know that it downloads the tar.gz files (or at least gets the http headers like it is), the error is when it tries to un-gzip the file.

code:
Exception: <type 'exceptions.IOError'>, Not a gzipped file
Here are the http headers from GAE:
code:
X-Google-Cache-Control: remote-fetch
content-disposition: filename=S_D_80082691_999998_20090430_2009050218173.txt.gz
set-cookie: PianoAppSID=sbZwkyr8w; version="1"; expires=Sat, 02-May-2009 04:02:18 GMT;
path=/; secure, PianoAppResourceAccess=c2Jad2VWcnZRys2NjE3K2c=;
version="1"; expires=Sat, 02-May-2009 04:02:18 GMT; path=/; secure
expires: Sat, 02 May 2009 03:03:18 GMT,Fri, 01-May-2009 14:53:20 GMT
filename: S_D_80082691_999998_20090430_20030218173.txt.gz
connection: close
Via: HTTP/1.1 GWA
Cache-Control: max-age=60
Date: Sat, 02 May 2009 03:02:18 GMT
Server: Apache/1.3.33 (Darwin) mod_ssl/2.8.24 OpenSSL/0.9.7i
Content-Type: application/x-gzip
And here are the http headers when I run locally:
code:
content-length: 651
content-disposition: filename=S_D_80082691_999998_20090426_200905708447.txt
.gz
content-encoding: gzip
set-cookie: PianoAppSID=Ai7rXNp7IAHM; version="1"; expires=Sat, 02-May
-2009 04:07:08 GMT; path=/; secure, PianoAppResourceAccess=QWk3cYN
Gg3SUFI0K2c=; version="1"; expires=Sat, 02-May-2009 04:07:08
GMT; path=/; secure
expires: Sat, 02 May 2009 03:08:08 GMT, Fri, 01-May-2009 14:53:20 GMT
server: Apache/1.3.33 (Darwin) mod_ssl/2.8.24 OpenSSL/0.9.7i
connection: close
cache-control: max-age=60
date: Sat, 02 May 2009 03:07:08 GMT
filename: S_D_80082691_999998_20090426_2009050708447.txt.gz
content-type: application/x-gzip
Here is the block of code that downloads the file:
code:
opener = urllib2.build_opener()
h = opener.open(reportURL, urllib.urlencode(reportData))
logging.error("reportURL: " + reportURL)
logging.error(h.info())
s = StringIO.StringIO(h.read())
f = gzip.GzipFile(fileobj=s)
for line in f.read().split('\n'):
reportURL and reportData both look good.

I'm wondering if anyone knows if GAE has some problems with txt.gz files or something stupid like that. I can't think of any other reason why it would work locally but not on the GAE server.

Got any ideas for more debugging I could do?

One weird thing is the HTTP headers on GAE dont tell me the content length. How can I print out the size of the tar.gz file I just downloaded?

tayl0r fucked around with this message at 04:32 on May 2, 2009

tef
May 30, 2004

-> some l-system crap ->
Use the urlfetch api for google app engine:

http://code.google.com/appengine/docs/python/urlfetch/overview.html

urllib2 is awful.

checkeredshawn
Jul 16, 2007

I wanted to write a quick game in Python and use classes so I could learn and write something fun. It's like the word game Boggle, except that I can tell the letter grid class to use any number for the n by n matrix of letters, so for example if I give it 4, it'll return something like:

code:
N   T   Y   H   

N   G   W   N   

P   P   R   R   

U   T   O   U   
The purpose of the game is to form words out of the grid of letters with the restrictions that you can only use each letter once, and you can only go from one letter to another directly next to it in any direction. So an example valid word for the above grid would be "TOUR" starting with the T in the bottom row.

Everything is written except for the word validating stuff. I don't mean checking words against a dictionary to see if they're valid English words, I mean checking if user input is valid in the grid. I tried writing out a function that would generate all 2-letter words hoping that it would help me visualize what it should look like, but I tried writing it recursively and Python didn't like it. Here's what I wrote to generate all valid 2-letter combinations in a grid:

code:
def word_combinations(matrix, num_letters):
  list_of_words = []
  visited = []
  for row in matrix:
    for first in row:
      if isinstance(first, EmptyLetter):
        continue
      elif (first in visited):
        continue
      else:
        visited.append(first) 
        for second in first.neighbors:
          if isinstance(second, EmptyLetter):
            continue
          else:
            visited.append(second)
            list_of_words.append(first.letter + second.letter)
      visited = []
  print list_of_words, len(list_of_words)
A friend suggested that instead of figuring out all possible combinations, try to check to see if the first letter is in the matrix, then for each of those matches continue to see if the second letter is in their neighbors, etc. but I couldn't visualize how to write that out. If anyone wants to help and has any questions about the above code or the rest of the code I'd really appreciate any suggestions and I can explain how I implemented stuff. I realize it might have been easier to not use classes but I wrote it this way to learn how to use classes in Python, not necessarily to write it the most efficient and minimal way possible.

such a nice boy
Mar 22, 2002

checkeredshawn posted:

A friend suggested that instead of figuring out all possible combinations, try to check to see if the first letter is in the matrix, then for each of those matches continue to see if the second letter is in their neighbors, etc. but I couldn't visualize how to write that out. If anyone wants to help and has any questions about the above code or the rest of the code I'd really appreciate any suggestions and I can explain how I implemented stuff. I realize it might have been easier to not use classes but I wrote it this way to learn how to use classes in Python, not necessarily to write it the most efficient and minimal way possible.

Your friend's suggestion is good. The number of possible combinations is huge, even if you're limiting words to 4 letters.

I'd use a general strategy of first finding all the coordinates on the board that have the first letter, and then finding all the points adjacent to that that have the next letter, and so on, until you find a path that gets to the end of the word. You'll want to make a utility function like "findAdjacentCoordinatesWithLetter" that takes in a coordinate and a letter.

tayl0r
Oct 3, 2002
This post brought to you by SOE

tef posted:

Use the urlfetch api for google app engine:

http://code.google.com/appengine/docs/python/urlfetch/overview.html

urllib2 is awful.

Do you think what I'm trying to do would even work since GAE wont let you modify the referer?

GAE posted:

Request Headers
An app can set HTTP headers for the outgoing request.
When sending an HTTP POST request, if a Content-Type header is not set explicitly, the header is set to x-www-form-urlencoded. This is the content type used by web forms.

For security reasons, the following headers cannot be modified by the application:
Content-Length
Host
Referer
Vary
Via
X-Forwarded-For

tef
May 30, 2004

-> some l-system crap ->
Well if it is disabled in the api, I imagine what you're trying to do won't work on google app engine at all.

The local api will just be wrappers around the standard library, but the implementations at google have been modified for the sandbox.


Edit: What you are doing is trying to circumvent the security policy by using an unsupported api. Which is why it is not working, and will never work.

tayl0r
Oct 3, 2002
This post brought to you by SOE
HOLY poo poo! Just when I was about to give up, it works!

I switched it over to use urlfetch (was pretty easy) and it still wasn't working. Got the same error- bad gzip data.

I printed out what it was downloading and there was my data, already un-gzipped! I wonder why GAE automatically ungzips .txt.gz files if you download them? that seems broken.

Adbot
ADBOT LOVES YOU

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

The Evan posted:

In interactive mode, when I reload a module that imports other modules, how do I reload those imported modules? Should I just reload all of them?

ipython has dreload (deep reload) but with normal python, I think you have to reload all.

  • Locked thread