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
namaste friends
Sep 18, 2004

by Smythe

diddy kongs feet posted:

Total programming rookie, working on an assignment in jython and I'm trying to implement input validation for two things at once, kinda stumped. Basically I'm drawing a 'room' on an image and then requesting input location to place a light inside the room. Between taking inputs and placing the light I want to check that the x/y coords are within the image and that the light is actually placed inside the room I've drawn. I can validate both of these independently just fine but can't work out a good economy for doing both appropriately, since my logic so far lets the user pass one check and then fail the first check when passing the second yet continue. I know how to wing it but I'd be repeating a lot of code and that doesn't seem right to me.
Could anyone hook me up with some reading material or just general advice on input validation economy? Struggling with this got me really interested in best practices for things like this.

Are you familiar with finite state machines? This abstraction might help you deal with validating input through states.

Adbot
ADBOT LOVES YOU

diddy kongs feet
Dec 11, 2012

wanna lick the dirt out between ur chimp toes
drat thanks loads, great reading. I probably should've gone back and edited in that I managed to work out a rough logic for my validation so it's working for now, I guess my post was more a cry for help re: background reading, 'best practice' reading etc.

What's the consensus on jython anyway? Some grads I spoke to were talking trash on this intro programming class for using JES but as a beginner it's been good stuff. How well does it lead into vanilla python? Where's a good place to start if I want to get a head start on it? Went through all the OP links but further reading is always appreciated.

Thermopyle
Jul 1, 2003

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

diddy kongs feet posted:

drat thanks loads, great reading. I probably should've gone back and edited in that I managed to work out a rough logic for my validation so it's working for now, I guess my post was more a cry for help re: background reading, 'best practice' reading etc.

What's the consensus on jython anyway? Some grads I spoke to were talking trash on this intro programming class for using JES but as a beginner it's been good stuff. How well does it lead into vanilla python? Where's a good place to start if I want to get a head start on it? Went through all the OP links but further reading is always appreciated.

I'm not familiar with jython at all, but from briefly perusing the google results for "difference jython python", it seems like you could drop right into using CPython right now. In other words, the answer to "How well does it lead into vanilla python?" seems like "Almost perfectly".

Nippashish
Nov 2, 2005

Let me see you dance!
As long as you don't care about using new language features (last time I used jython it implemented python 2.6), and don't care about C extensions then cPython and jython are basically identical from a programmer perspective.

Dren
Jan 5, 2001

Pillbug
Anyone have anything I can read about anydbm and how it works? If I care about how it works is that a sign that I should actually pick a db engine?

OnceIWasAnOstrich
Jul 22, 2006

Its just an interface to whatever random dbm implementation it decides to grab. I tried using it the other day on OSX and it combined with whatever ancient BSD dbm it comes with caused my computer to hard reboot whenever I ran my script. I started using Kyoto Cabinet and never looked back.

edit: For a little more detail. You can think of anydbm and all of the specific varieties as on-disk dictionaries. They all utilize various versions/implementations of dbm which is a non-relational database/key-value store. You would use anydbm if you need to use something kind of like a dictionary without keeping it all in memory, and you need this something to be in the standard library.

OnceIWasAnOstrich fucked around with this message at 18:53 on May 16, 2014

Dren
Jan 5, 2001

Pillbug

OnceIWasAnOstrich posted:

Its just an interface to whatever random dbm implementation it decides to grab. I tried using it the other day on OSX and it combined with whatever ancient BSD dbm it comes with caused my computer to hard reboot whenever I ran my script. I started using Kyoto Cabinet and never looked back.

edit: For a little more detail. You can think of anydbm and all of the specific varieties as on-disk dictionaries. They all utilize various versions/implementations of dbm which is a non-relational database/key-value store. You would use anydbm if you need to use something kind of like a dictionary without keeping it all in memory, and you need this something to be in the standard library.

ok thanks, that's enough information to know it's not what I want.

Thermopyle
Jul 1, 2003

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

Have you looked at the standard library shelve module?

the
Jul 18, 2004

by Cowcaster
Trying to clean up some data again. I'm getting output like this from a website:

Python code:
u'\r\n\t\t\t\t\t\t\xa0\r\n\t\t\t\t\t\tApalachee Correctional 
Institution East\r\n\t\t\t\t\t\t\t(Male) \r\n\t\t\t\t\t\t\t35 Apalachee Drive
\r\n\t\t\t\t\t\t\tSneads, Florida  32460-4166\r\n\t\t\t\t\t\t\t(850) 718-0688\
r\n\t\t\t\t\t\t\tEast Unit Fax: (850) 593-6445\r\n\t\t\t\t\t\tApalachee Correct
ional Institution West\r\n\t\t\t\t\t\t\t(Male) \r\n\t\t\t\t\t\t\t52 West Unit 
Drive\r\n\t\t\t\t\t\t\tSneads Florida 32460-4165\r\n\t\t\t\t\t\t\t(850) 718-0577
\r\n\t\t\t\t\t\t\tFax: (850) 593-6445\r\n\t\t\t\t\t'
And obviously I want it looking something like this:
Python code:
Apalachee Correctional Institution East
Sneads, Florida 32460-4166
(850) 718-0688
I've gone through and done a str.replace() for the \r, \n, \t elements, etc. But that still leaves me with figuring out how to tell the program to know when it reaches the end of a name and the beginning of a city, state, etc.

Because doing a str.replace() leaves me with:

Python code:
'Apalachee Correctional Institution East(Male) 35 Apalachee DriveSneads,
 Florida  32460-4166(850) 718-0688East Unit Fax: (850) 593-6445Apalachee Correctional
 Institution West(Male) 52 West Unit DriveSneads Florida 32460-4165(850) 718-0577Fax: 
 (850) 593-6445'
edit: I done broke the tables with that

the fucked around with this message at 19:18 on May 16, 2014

QuarkJets
Sep 8, 2008

It sounds like you just want to keep newline returns, so don't replace those. Just replace \t and see what it looks like. If you need to grab specific pieces, then you could use string.split('\r\n')

the
Jul 18, 2004

by Cowcaster

QuarkJets posted:

It sounds like you just want to keep newline returns, so don't replace those. Just replace \t and see what it looks like. If you need to grab specific pieces, then you could use string.split('\r\n')

So the \r\n is a newline return? How come it's not displaying that way?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

the posted:

So the \r\n is a newline return? How come it's not displaying that way?

It's platform dependent: http://en.wikipedia.org/wiki/Newline

namaste friends
Sep 18, 2004

by Smythe
Can you try a regex filter like this:

code:
crap = u'\r\n\t\t\t\t\t\t\xa0\r\n\t\t\t\t\t\tApalachee Correctional 
Institution East\r\n\t\t\t\t\t\t\t(Male) \r\n\t\t\t\t\t\t\t35 Apalachee Drive
\r\n\t\t\t\t\t\t\tSneads, Florida  32460-4166\r\n\t\t\t\t\t\t\t(850) 718-0688\
r\n\t\t\t\t\t\t\tEast Unit Fax: (850) 593-6445\r\n\t\t\t\t\t\tApalachee Correct
ional Institution West\r\n\t\t\t\t\t\t\t(Male) \r\n\t\t\t\t\t\t\t52 West Unit 
Drive\r\n\t\t\t\t\t\t\tSneads Florida 32460-4165\r\n\t\t\t\t\t\t\t(850) 718-0577
\r\n\t\t\t\t\t\t\tFax: (850) 593-6445\r\n\t\t\t\t\t'


new_string = re.sub(r"\\\D", '', crap) 

Check out this regex tester for python: https://pythex.org/

edit:

The regex \\\D
means match if any non-digit character has a \ in front of it.

The first slash in '\\' means escape. So \\ means match a '\'.

\D means match any non-digit.

namaste friends fucked around with this message at 20:11 on May 16, 2014

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Cultural Imperial posted:

Can you try a regex filter like this:

code:
crap = u'\r\n\t\t\t\t\t\t\xa0\r\n\t\t\t\t\t\tApalachee Correctional 
Institution East\r\n\t\t\t\t\t\t\t(Male) \r\n\t\t\t\t\t\t\t35 Apalachee Drive
\r\n\t\t\t\t\t\t\tSneads, Florida  32460-4166\r\n\t\t\t\t\t\t\t(850) 718-0688\
r\n\t\t\t\t\t\t\tEast Unit Fax: (850) 593-6445\r\n\t\t\t\t\t\tApalachee Correct
ional Institution West\r\n\t\t\t\t\t\t\t(Male) \r\n\t\t\t\t\t\t\t52 West Unit 
Drive\r\n\t\t\t\t\t\t\tSneads Florida 32460-4165\r\n\t\t\t\t\t\t\t(850) 718-0577
\r\n\t\t\t\t\t\t\tFax: (850) 593-6445\r\n\t\t\t\t\t'


new_string = re.sub(r"\\\D", '', crap) 

Check out this regex tester for python: https://pythex.org/

edit:

The regex \\\D
means match if any non-digit character has a \ in front of it.

The first slash in '\\' means escape. So \\ means match a '\'.

\D means match any non-digit.

Did you try this? Those backslashes aren't actually in the string. They're only in the output of str.__repr__ -- known control characters are dumped as backslash escapes.

namaste friends
Sep 18, 2004

by Smythe

Lysidas posted:

Did you try this? Those backslashes aren't actually in the string. They're only in the output of str.__repr__ -- known control characters are dumped as backslash escapes.

I tried it in the tester and in code, although I admit I didn't spend a lot of time on it. Apologies since I'm completely wrong.

Lichtenstein
May 31, 2012

It'll make sense, eventually.
I wanted to take cocos2d for a spin, since the last version is supposed to be compatible with Python 3. So far it refuses to recognise it as a package.

Is there some trick to installing it, or am I just being dumb about it?

accipter
Sep 12, 2003

Lichtenstein posted:

I wanted to take cocos2d for a spin, since the last version is supposed to be compatible with Python 3. So far it refuses to recognise it as a package.

Is there some trick to installing it, or am I just being dumb about it?

What are you doing now? I would assume that you are doing "pip3 install cocos2d", or perhaps "python3 setup.py install".

Lichtenstein
May 31, 2012

It'll make sense, eventually.
Yep, tried both of those. I've installed both pyglet 1.2 and six, so it shouldn't be related to dependency shenanigans.

Comrade Gritty
Sep 19, 2011

This Machine Kills Fascists

Lichtenstein posted:

Yep, tried both of those. I've installed both pyglet 1.2 and six, so it shouldn't be related to dependency shenanigans.

Can you show a traceback?

Lichtenstein
May 31, 2012

It'll make sense, eventually.
When I tried to do the hello world straight from the docs it throws

code:
Traceback (most recent call last):
  File "C:\helloworld\cocos.py", line 1, in <module>
    import cocos, pyglet
  File "C:\helloworld\cocos.py", line 3, in <module>
    class HelloWorld(cocos.layer.Layer):
AttributeError: 'module' object has no attribute 'layer'
I tried shuffling things around in case it's something dumb like director module not being initialised beforehand, but no game, it just protests the first module it encounters. Now, when I import something with a from statement (in this case, from cocos.director import director) it throws

code:
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2195, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\helloworld\cocos.py", line 1, in <module>
    import cocos, pyglet
  File "C:\helloworld\cocos.py", line 2, in <module>
    from cocos.director import director
ImportError: No module named 'cocos.director'; 'cocos' is not a package

BigRedDot
Mar 6, 2008

the posted:

Trying to clean up some data again. I'm getting output like this from a website:

Regex is way too much work. Try io.StringIO with newline=None for universal newline mode.
code:
In [38]: str = u'[your ugly string here]'

In [39]: lines = [line.strip() for line in io.StringIO(str , newline=None).readlines()]

In [40]: lines = [x for x in lines if x] # discard empty strings

In [41]: lines
Out[41]: 
[u'Apalachee Correctional Institution East',
 u'(Male)',
 u'35 Apalachee Drive',
 u'Sneads, Florida  32460-4166',
 u'(850) 718-0688',
 u'East Unit Fax: (850) 593-6445',
 u'Apalachee Correctional Institution West',
 u'(Male)',
 u'52 West Unit Drive',
 u'Sneads Florida 32460-4165',
 u'(850) 718-0577',
 u'Fax: (850) 593-6445']

BigRedDot fucked around with this message at 01:40 on May 17, 2014

Comrade Gritty
Sep 19, 2011

This Machine Kills Fascists

Lichtenstein posted:

When I tried to do the hello world straight from the docs it throws

code:
Traceback (most recent call last):
  File "C:\helloworld\cocos.py", line 1, in <module>
    import cocos, pyglet
  File "C:\helloworld\cocos.py", line 3, in <module>
    class HelloWorld(cocos.layer.Layer):
AttributeError: 'module' object has no attribute 'layer'
I tried shuffling things around in case it's something dumb like director module not being initialised beforehand, but no game, it just protests the first module it encounters. Now, when I import something with a from statement (in this case, from cocos.director import director) it throws

code:
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2195, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\helloworld\cocos.py", line 1, in <module>
    import cocos, pyglet
  File "C:\helloworld\cocos.py", line 2, in <module>
    from cocos.director import director
ImportError: No module named 'cocos.director'; 'cocos' is not a package

What version of setuptools do you have? Run pip list to find out.

the
Jul 18, 2004

by Cowcaster
Do you know a way I could combine/append two lxml.html.HtmlElement objects?

Like say I read from a page and I wanted to grab/work with tables[5] and tables[7]

I tried:

Python code:
table = tables[5]+tables[7]
but that doesn't work. Any ideas?

edit: this worked:

Python code:
tables = root.cssselect('table')
table = tables[4]
table.append(tables[5])
table.append(tables[6])
table.append(tables[10])

the fucked around with this message at 20:40 on May 19, 2014

andyf
May 18, 2008

happy car is happy

I'm failing at figuring out how to display a line of text that includes values from two lists in a certain way (two values, then a separator, then two more values etc)

code:
users = ["adam", "ben", "bob"]
ages = ["(20)", "(30)", "(40)"]
And I'm aiming for output like:
The users are Adam (20), Ben (30), Bob (40)

I'm trying to get this to work using zip but the closest I've come so far is:

code:
print "The users are %s" % (", ".join([j for i in zip(users, ages) for j in i]))
-> The users are adam, (20), ben, (30), bob, (40)
I can't figure out how to make it only do the ", " join after a value from each list has been used, rather than after each single value. Any ideas?

OnceIWasAnOstrich
Jul 22, 2006

andyf posted:

I can't figure out how to make it only do the ", " join after a value from each list has been used, rather than after each single value. Any ideas?

You are asking it to do a single type of join (', ') on a single list when you want two different types of joins (', ' and ' '). Instead of flattening your list of tuples you get from zip() in that awful nested comprehension you need to do a ' '.join() on each name/value tuple in the list.

Python code:
print 'The users are %s' % (', '.join(' '.join(i) for i in zip(users, ages)))
#The users are adam (20), ben (30), bob (40)

OnceIWasAnOstrich fucked around with this message at 18:42 on May 21, 2014

andyf
May 18, 2008

happy car is happy

Ahhhhh great, thank you!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Is there a better way to extract the number out of a string like 'file[0]' ?

Python code:
import re
index = int(re.search(r'\d+', 'file[0]').group())

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Why do you want to?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Suspicious Dish posted:

Why do you want to?

Why do I want to find a better way? Just wanted to make sure the way I'm doing it isn't totally stupid

or Why do I want to get the number out of that string? Django's request.FILES has 'file[0]' as the key, and I need to grab the corresponding caption from request.DATA.getlist('caption')

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

fletcher posted:

Is there a better way to extract the number out of a string like 'file[0]' ?

Python code:
import re
index = int(re.search(r'\d+', 'file[0]').group())

Wel if you want to get more specific with the regex:

Python code:
import re
index = int(re.search(r'\w+\[(\d+)\]', 'file34[8]').group(1))
This gets 8 instead of 34.

(I didn't actually test the code, typing on a tablet sorry)

the
Jul 18, 2004

by Cowcaster
How would I do this?

"for row in b:

but skip the first row"

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

the posted:

How would I do this?

"for row in b:

but skip the first row"

Python code:
first = True
for row in b:
    if first:
        first = False
        continue
    # do stuff

Gmaz
Apr 3, 2011

New DLC for Aoe2 is out: Dynasties of India
for row in b[1:]

etc...?

No Safe Word
Feb 26, 2005

Alternatively, if b is an iterator, just call
code:
b.next()
once before executing the loop.

the
Jul 18, 2004

by Cowcaster
In case anyone was curious, I'll post the problem I was having. I am trying to grab the information from this page. My code looked like this:

Python code:
import requests
import lxml.html
import cssselect
import csv

req = requests.get('http://en.wikipedia.org/wiki/List_of_AZA_member_zoos_and_aquaria')
root = lxml.html.fromstring(req.text)
b = root.cssselect('table:first-of-type tr')
c = []

urls = []

for row in b[1:]:
	cells = row.cssselect('tr')
	c.append(cells[0].text_content().strip())
        links = cells[0].cssselect('a')
        urls.append(links[3].get('href'))
I was getting an out of range error on urls, and I thought it was because I was grabbing the header on that page (which doesn't have any urls). Which is why I was asking what I was asking. But that wasn't fixing it.

However, I printed out the last entry before it crashed, and it was the entry of Jenkins Aquarium. If you notice, that one doesn't have it's own url entry, meaning that the links list was only 2 entries long for that one (while the rest were 3). So I fixed it by changing links[3] to links[-1].

SurgicalOntologist
Jun 17, 2004

Crossposting from the scientific computing thread...

SurgicalOntologist posted:

I decided to use software-carpentry's setup instructions for my scientific computing workshop that's starting next week. This uses Anaconda, and msysgit on Windows.

Well, I'm a bit late but I'm testing the Windows setup. It appears I can't activate conda virtualenvs from within msys. Whether I do activate or source activate, I get the "Activating environment ..." message but nothing actually changes, not the prompt, not which python, etc. Also I get the proper "No environment named..." message if the environment doesn't exist.

Any ideas?

E: looking at activate.bat... it must be something to do with the difference between Windows and Bash/msys environment variables. The set command in the script doesn't actually do anything in msys... ugh.

OnceIWasAnOstrich
Jul 22, 2006

Don't use Windows?

Alternatively, just call all your environment binaries directly using their path instead of relying on the activate scripts to try to change all of your paths in ways designed for a different type of shell. I don't know of anything that requires you to use the activation scripts, and I hardly ever use them with my environments. The conda command handles environments with a switch, and I call pip/interpreters directly from the environments bin directory.

Or I guess you can write your own activate script that does whatever activate scripts use with msys syntax.

BigRedDot
Mar 6, 2008

SurgicalOntologist posted:

Crossposting from the scientific computing thread...

activate.bat is a windows batch file, it will only work from cmd.exe or PowerShell. You can use conda and activate.bat perfectly well from these windows native command line tools and that would be my immediate suggestion.

SurgicalOntologist
Jun 17, 2004

OnceIWasAnOstrich posted:

Don't use Windows?

Alternatively, just call all your environment binaries directly using their path instead of relying on the activate scripts to try to change all of your paths in ways designed for a different type of shell. I don't know of anything that requires you to use the activation scripts, and I hardly ever use them with my environments. The conda command handles environments with a switch, and I call pip/interpreters directly from the environments bin directory.

Or I guess you can write your own activate script that does whatever activate scripts use with msys syntax.

If I could convince everyone not to use Windows I would (maybe by the end of the workshop...). Of course I could do a VM or something but I want to leave people with a native solution or they won't ever fire up Python again.

The reason I'm using msys is so I can have the same instructions for everyone, once everything is setup. Not a bad idea to just not bother with activating at all, I hadn't really considered that.

BigRedDot posted:

activate.bat is a windows batch file, it will only work from cmd.exe or PowerShell. You can use conda and activate.bat perfectly well from these windows native command line tools and that would be my immediate suggestion.

Yeah the backup plan was to just have Windows users open up both bash and cmd but if I'm going to teach bash basics it would be nice to have everything in one place. Maybe I'll write a bash wrapper around activate.bat.

SurgicalOntologist fucked around with this message at 21:30 on May 23, 2014

Adbot
ADBOT LOVES YOU

OnceIWasAnOstrich
Jul 22, 2006

I taught a learning Python class once with Anaconda and I found everything to be too much work in Windows and unfortunately I never found a good way to make Windows act enough like Unix so I just skipped it and taught the entire class in the Spyder IDE that comes with Anaconda. Not my finest moment, but I wasn't getting paid and didn't want to do any more work than necessary.

Why do you need environments at all?

  • Locked thread