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
Nippashish
Nov 2, 2005

Let me see you dance!

Ghost of Reagan Past posted:

a fancy way to do it with a generator or something

Well, you can do this

code:
def a_while(dt):
    end = datetime.datetime.now() + datetime.timedelta(days=1)
    for i in itertools.count(0):
        yield i
        if datetime.datetime.now() > end:
            break

for i in a_while(1):
    print(i)

Adbot
ADBOT LOVES YOU

Curdy Lemonstan
Jan 25, 2012

by zen death robot
I'm trying my hand at python now for the first time by making a game of life clone in pygame. I can't for the life of me figure out whats wrong with my code,

Most parts works and the games logic is fine when there is no updating required, like basically when the patterns are 'still life',
oscillators and everything else behaves very strangely and also behaves differently on the rows and columns.

It feels like I'm just within grasp of a functioning game but I can't wrap my head around what's wrong with it.

Here is my code:
http://pastebin.com/RxeuBx89

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Curdy Lemonstan posted:

I'm trying my hand at python now for the first time by making a game of life clone in pygame. I can't for the life of me figure out whats wrong with my code,

Most parts works and the games logic is fine when there is no updating required, like basically when the patterns are 'still life',
oscillators and everything else behaves very strangely and also behaves differently on the rows and columns.

It feels like I'm just within grasp of a functioning game but I can't wrap my head around what's wrong with it.

Here is my code:
http://pastebin.com/RxeuBx89

code:
if gameActive:
        newBoard = Board
        tempBoard = Board
I don't think these lines achieve what you wanted them to.

As an exercise, try typing this in the Python interactive interpreter:

code:
L = [1, 2, 3]
M = L
M.append(4)
L; M
Do you see what's happening here? Now try this:

code:
L = [1, 2, 3]
M = L.copy()
M.append(4)
L; M
You could add a copy() method to your Board that explicitly makes an independent copy.

Curdy Lemonstan
Jan 25, 2012

by zen death robot

Hammerite posted:

code:
if gameActive:
        newBoard = Board
        tempBoard = Board
I don't think these lines achieve what you wanted them to.

As an exercise, try typing this in the Python interactive interpreter:

code:
L = [1, 2, 3]
M = L
M.append(4)
L; M
Do you see what's happening here? Now try this:

code:
L = [1, 2, 3]
M = L.copy()
M.append(4)
L; M
You could add a copy() method to your Board that explicitly makes an independent copy.

Wow thanks! I see now the error of my ways. thank you so much!

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Other thoughts - in getCellList() you explicitly enumerate the eight squares surrounding the cell under consideration (taking care not to query cells that are outside the edge of the board), and then you return a list of them. But all you care about is how many of them are "alive". I would be tempted to reduce the amount of code by doing something like this:

Python code:
def get_adjacent_alive_cells(board, x, y):
    alive_cells_count = 0
    for x2 in range(x - 1, x + 2):
        for y2 in range(y - 1, y + 2):
            if x2 != x or y2 != y:
                try:
                    alive_cells_count += int(board[x2][y2])
                except IndexError:
                    pass # Ignore errors resulting from querying cells out of range
    return alive_cells_count
Here we have avoided having to laboriously check whether the coordinates (x2, y2) are in range by using try-except and making clear we just don't care when we query out of bounds.

This could be even further condensed if you wanted, but I resisted the urge to do that because I only want to illustrate the general approach I am suggesting.

Also, you have a global Board object which is just a list of lists. The object oriented approach would be to have a class Board and have most of your free-standing functions be methods on that class.

Curdy Lemonstan
Jan 25, 2012

by zen death robot
Awesome! *cracks knuckles* Lets code!

BigRedDot
Mar 6, 2008

Welp. https://news.ycombinator.com/item?id=9936295

If there's things you want to see in Anaconda, let us know, we are moving ahead full bore :)

Dominoes
Sep 20, 2007

BigRedDot posted:

Welp. https://news.ycombinator.com/item?id=9936295

If there's things you want to see in Anaconda, let us know, we are moving ahead full bore :)
Outstanding.

Qt5 support.

The Fool
Oct 16, 2003


Is there a not terrible/easy ocr library for python?

I'm thinking about trying to pull serial numbers from a batch of photos.

QuarkJets
Sep 8, 2008

BigRedDot posted:

Welp. https://news.ycombinator.com/item?id=9936295

If there's things you want to see in Anaconda, let us know, we are moving ahead full bore :)

Easier/more user friendly offline package management. Some of us don't have luxuries like internet access on our work machines, and updating/installing new packages in an Anaconda environment without internet access can be confusing if you've never done it before.

Cingulate
Oct 23, 2012

by Fluffdaddy

BigRedDot posted:

Welp. https://news.ycombinator.com/item?id=9936295

If there's things you want to see in Anaconda, let us know, we are moving ahead full bore :)
Does that mean Travis Oliphant is rich now

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

BigRedDot posted:

Welp. https://news.ycombinator.com/item?id=9936295

If there's things you want to see in Anaconda, let us know, we are moving ahead full bore :)
I used to use a normal Python install until I saw the light of Anaconda (also because numpy on Windows seems like a pain in the rear end to install). Congrats.

Actual Python question: I've been wanting to build a UI for this data tool I've been building, but I'm torn on whether I should just use Tkinter, Qt, something else, or just build a web interface with Django and learn how to smash JSON files into an SQL database. If you were going to build a UI for a tool to collect and sift through hundreds of thousands of tweets, what would you use, Python thread?

Ghost of Reagan Past fucked around with this message at 02:19 on Jul 24, 2015

Dominoes
Sep 20, 2007

If you want it to be easily accessible by other people, use Django or Flask for the UI. It sounds like Flask would be more appropriate. Qt's nice if you want to run it on your own computer as a desktop app.

Keep in mind, learning Qt and web development are both a pain in the nuts if you're not familiar. Qt's challenge is lack of tutorials and non-userfriendly documentation. Web development's challenge is learning how each component (database, html, css, js, servers, front-end-frameworks, deployment etc) works before trying to put them together.

Dominoes fucked around with this message at 03:13 on Jul 24, 2015

pmchem
Jan 22, 2010


BigRedDot posted:

Welp. https://news.ycombinator.com/item?id=9936295

If there's things you want to see in Anaconda, let us know, we are moving ahead full bore :)

pylint (for spyder integration) and additional neural network tools in the default distribution, please!

also, congrats

Luigi Thirty
Apr 30, 2006

Emergency confection port.

Dominoes posted:

If you want it to be easily accessible by other people, use Django or Flask for the UI. It sounds like Flask would be more appropriate. Qt's nice if you want to run it on your own computer as a desktop app.

Keep in mind, learning Qt and web development are both a pain in the nuts if you're not familiar. Qt's challenge is lack of tutorials and non-userfriendly documentation. Web development's challenge is learning how each component (database, html, css, js, servers, front-end-frameworks, deployment etc) works before trying to put them together.

I'd rather read the Django or Flask docs than try to figure out how to make Qt bend to your will if you've never done UI development before.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

If you're not familiar, both native and web UI development will seem daunting. However, I think web development knowledge will be more widely useful going forward so if you're just going to learn one do that.

salisbury shake
Dec 27, 2011
Having been there and going down both routes, I'll echo the Flask/Django sentiment. With Qt you'll be using a wrapper over the C++ library and the documentation is for C++. You'll have to learn a lot that is only applicable to that library or building desktop GUIs. It isn't very portable. Then there is the whole QML layer which is a language unto itself.

I'd say go with Flask. You won't have everything Django provides out of the box, but in this case it might be enough. Figure out how to write a function that can inject and render what you need into you view, display it in your Jinja template, run the Flask server and you're set.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
Alright, Flask it is.

BigRedDot
Mar 6, 2008

Dominoes posted:

Qt5 support.
Planned, probably Q1 2016.

pmchem posted:

pylint (for spyder integration) and additional neural network tools in the default distribution, please!
Do you mean in the installer? It's always a tradeoff what goes in the installer, it would be easy to balloon it to 1GB download. :) However conda install pylint should grab it from the repo super fast. But, you also just gave me an interesting idea, which is that perhaps the installers could offer additional optional packages that user's could check/uncheck at install (they would have to be downloaded separately from the installer, but the installer could do it for you, right at install time). I will toss the idea to the team and see what they think.

QuarkJets posted:

Easier/more user friendly offline package management. Some of us don't have luxuries like internet access on our work machines, and updating/installing new packages in an Anaconda environment without internet access can be confusing if you've never done it before.
This is definitely an important use-case. Conda should work offline now, if you have a package cache, or your own repo directory. Do you mean tools to help set those things up more easily?

Cingulate posted:

Does that mean Travis Oliphant is rich now
It means we get to bring on more help! But I'm totally going to make him buy me lunch, like at least once. :)

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
You can also sort of make desktop apps using web development stuff now that chromium and node etc. all exist. Atom.io is built using HTML/CSS/JS, as is Brackets.

Dominoes
Sep 20, 2007

BigRedDot posted:

Planned, probably Q1 2016.
Sweet!

Cingulate
Oct 23, 2012

by Fluffdaddy
I really tried to think of some way to improve, but I really couldn't think of anything but for "just do what you're doing now, but harder". Which is lame.
I'm really happy with conda.

I have something extra lame though. Make installing packages from binstar easier. Right now, you have to manually copy lines and poo poo, multiple lines. Just give me another flag for conda install to automatically check binstar if there is nothing in the main repo/my channels, and if something is found, prompt me asking if I want to install it.

Captain Capacitor
Jan 21, 2008

The code you say?
I'm hoping at least one other person here is at this PyData conference.

BigRedDot
Mar 6, 2008

Captain Capacitor posted:

I'm hoping at least one other person here is at this PyData conference.

I'm on the conference organizing committee, around the registration table most of the time.

Captain Capacitor
Jan 21, 2008

The code you say?

BigRedDot posted:

I'm on the conference organizing committee, around the registration table most of the time.

Well dang here I was killing time around the continuum table thinking I could find someone else. At least I didn't completely embarrass myself by bringing up some obscure reference.

The continuum shirts are hella comfy, as it happens.

pmchem
Jan 22, 2010


BigRedDot posted:

Do you mean in the installer? It's always a tradeoff what goes in the installer, it would be easy to balloon it to 1GB download. :) However conda install pylint should grab it from the repo super fast. But, you also just gave me an interesting idea, which is that perhaps the installers could offer additional optional packages that user's could check/uncheck at install (they would have to be downloaded separately from the installer, but the installer could do it for you, right at install time). I will toss the idea to the team and see what they think.

yup, I meant the installer, with some of the same motivation as quarkjets' post. I'd like to be able to get all the packages I desire at once, since the combination of administrator+network access is rare and painful.

BigRedDot
Mar 6, 2008

Captain Capacitor posted:

Well dang here I was killing time around the continuum table thinking I could find someone else. At least I didn't completely embarrass myself by bringing up some obscure reference.
Well I'm still around, stop by and say hi. :)

quote:

The continuum shirts are hella comfy, as it happens.
Glad you like them!

Cingulate
Oct 23, 2012

by Fluffdaddy
iPython question: I have a self-installed module available, but I have no idea how I am making it available. It's not in my PYTHONPATH, it's not in my anaconda directory, it's not in the directory I'm calling Python from. Can I somehow check what other paths Python checks?

KICK BAMA KICK
Mar 2, 2009

Cingulate posted:

iPython question: I have a self-installed module available, but I have no idea how I am making it available. It's not in my PYTHONPATH, it's not in my anaconda directory, it's not in the directory I'm calling Python from. Can I somehow check what other paths Python checks?
This might be way too simple but have you looked at sys.path?

Cingulate
Oct 23, 2012

by Fluffdaddy

KICK BAMA KICK posted:

This might be way too simple but have you looked at sys.path?
It is in sys.path, thanks! So how did it get in there ..?

Michaellaneous
Oct 30, 2013

I have a bunch of values stored in seperate objects. Is there a quick way to put them all in a neat JSON string?

Dominoes
Sep 20, 2007

Michaellaneous posted:

I have a bunch of values stored in seperate objects. Is there a quick way to put them all in a neat JSON string?

Python code:
json = json.dumps([[obj.attr1, obj.attr2, obj.attr3] for obj in objects])

Amberskin
Dec 22, 2013

We come in peace! Legit!
Hello,

I'm taking a MOOC about machine learning which has python based lab exercises. My knowledge of python is quite basic, but I'm not having big trouble with the language. Actually, I like what I've seen...

The labs are done using iPython notebooks, against a configured virtual machine, so it is a "black box" for me: I just open the browser, point to the VM and start coding. My question is if there is some way to debug the code using iPhyton without resorting to the old and ugly practice of spamming the code with "print" statements. I've done some googling, but found nothing (I admit I haven't gone beyond the basic searchs). Could anyone point me to any doc/tutorial/example to teach myself how to do it?

vikingstrike
Sep 23, 2007

whats happening, captain

Amberskin posted:

Hello,

I'm taking a MOOC about machine learning which has python based lab exercises. My knowledge of python is quite basic, but I'm not having big trouble with the language. Actually, I like what I've seen...

The labs are done using iPython notebooks, against a configured virtual machine, so it is a "black box" for me: I just open the browser, point to the VM and start coding. My question is if there is some way to debug the code using iPhyton without resorting to the old and ugly practice of spamming the code with "print" statements. I've done some googling, but found nothing (I admit I haven't gone beyond the basic searchs). Could anyone point me to any doc/tutorial/example to teach myself how to do it?

I'd recommend starting by reading about the python debugger: https://docs.python.org/2/library/pdb.html It works just fine in iPython notebooks.

KICK BAMA KICK
Mar 2, 2009

Also if you just want a more sensible version of spamming prints, check out the logging module. Use logger.debug statements in place of those prints; overhead consists of logging.basicConfig() near the start of your code and a logger = logging.getLogger(__name__) at the top of each module that wants to log anything. That'll automatically give you timestamps and the name of module that the entry came from. Set the level argument to basicConfig to logging.DEBUG when you want to see the logs, logging.INFO when you don't, no need to comment out all your logging statements. (Off the top of my head, may have gotten a detail wrong.)

Amberskin
Dec 22, 2013

We come in peace! Legit!

vikingstrike posted:

I'd recommend starting by reading about the python debugger: https://docs.python.org/2/library/pdb.html It works just fine in iPython notebooks.


KICK BAMA KICK posted:

Also if you just want a more sensible version of spamming prints, check out the logging module. Use logger.debug statements in place of those prints; overhead consists of logging.basicConfig() near the start of your code and a logger = logging.getLogger(__name__) at the top of each module that wants to log anything. That'll automatically give you timestamps and the name of module that the entry came from. Set the level argument to basicConfig to logging.DEBUG when you want to see the logs, logging.INFO when you don't, no need to comment out all your logging statements. (Off the top of my head, may have gotten a detail wrong.)

Thanks! I'll try both things.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I tried to install the latest version of django-after-response and got:

code:
$ pip install django-after-response==0.2.2
Collecting django-after-response==0.2.2
  Could not find a version that satisfies the requirement django-after-response==0.2.2 (from versions: 0.1.0, 0.1.1, 0.2.0, 0.2.1)
  No distributions matching the version for django-after-response==0.2.2
How come it's not able to find the version that PyPi shows on the web? If I omit the version number from the command then I get 0.2.1.

Dominoes
Sep 20, 2007

I don't have a solution, but it's doing the same when I try.

oxidation
May 22, 2011

KICK BAMA KICK posted:

Also if you just want a more sensible version of spamming prints, check out the logging module. Use logger.debug statements in place of those prints; overhead consists of logging.basicConfig() near the start of your code and a logger = logging.getLogger(__name__) at the top of each module that wants to log anything. That'll automatically give you timestamps and the name of module that the entry came from. Set the level argument to basicConfig to logging.DEBUG when you want to see the logs, logging.INFO when you don't, no need to comment out all your logging statements. (Off the top of my head, may have gotten a detail wrong.)

I'm slowly weening myself off of relying on prints (by using PyCharm's debugger), and this is amazing, thank you!

Adbot
ADBOT LOVES YOU

KICK BAMA KICK
Mar 2, 2009

oxidation posted:

I'm slowly weening myself off of relying on prints (by using PyCharm's debugger), and this is amazing, thank you!
argparse is a good module to check out after logging, to add command-line arguments to set the logging level. It's less intuitive but not too bad to just implement a simple --verbosity n argument, bonus points for the -vvv form as well. Can specify the arguments in PyCharm's run configuration even if this isn't something you'd normally launch from the command line.

  • Locked thread