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
m0nk3yz
Mar 13, 2002

Behold the power of cheese!

deimos posted:

I am gonna ask the boss for PyCon, and I am pretty sure I am going to go regardless of job paying for it, at the very least for the conference.

That being said, this is discouraging.

m0nk3yz do you think it'll be better this year?

Yes, I do - I think a lot of us, myself included, largely agreed with Bruce. I think things will be changed quite a bit, and I know that the pycon organizers are watching out for this pitfall this year.

Also, I have two talks. You should go. I did a post this morning which may be the first of many

Adbot
ADBOT LOVES YOU

deimos
Nov 30, 2006

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

m0nk3yz posted:

Yes, I do - I think a lot of us, myself included, largely agreed with Bruce. I think things will be changed quite a bit, and I know that the pycon organizers are watching out for this pitfall this year.

Also, I have two talks. You should go. I did a post this morning which may be the first of many

Already read it, you're on my RSS.

king_kilr
May 25, 2007

deimos posted:

I am gonna ask the boss for PyCon, and I am pretty sure I am going to go regardless of job paying for it, at the very least for the conference.

That being said, this is discouraging.

m0nk3yz do you think it'll be better this year?

I attended last year and I really enjoyed it. This year I have the privilege of speaking and am once again really looking forward to it. One of the big things Bruce mentions, sponsored keynotes, I don't think we're having this year.

Stephen
Feb 6, 2004

Stoned
This is my first time using Python and I'm trying to design a web app. For now, I just want to create a simple "Hello World" type deal just to learn how Python works.

In PHP it's as simple as writing a hello.php file, echoing text and then uploading it to the server. Since I do not have command line access, just FTP access to our web server, is it even possible for me to create a site in Python? All the Python tutorials refer me to Django to build a site, and all the Django tutorials start with "run this on the command line". Are there any good online tutorials for degenerate PHP losers such as myself that are trying to learn this for the first time?

deimos
Nov 30, 2006

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

Stephen posted:

This is my first time using Python and I'm trying to design a web app. For now, I just want to create a simple "Hello World" type deal just to learn how Python works.

In PHP it's as simple as writing a hello.php file, echoing text and then uploading it to the server. Since I do not have command line access, just FTP access to our web server, is it even possible for me to create a site in Python? All the Python tutorials refer me to Django to build a site, and all the Django tutorials start with "run this on the command line". Are there any good online tutorials for degenerate PHP losers such as myself that are trying to learn this for the first time?

For learning probably get a 10MB account at http://www.alwaysdata.com/ it's free.

Basically click "Inscription" then:
on the first line enter the subdomain you want
on the second pick any of the options
on the third input pick "Pack Gratuit (10Mo)
(click button)
Adresse email = Email Address
Mot de passe = password
Confirmez le mot de passe = confirm password
Pays = Country (pick États-Unis)
(click button)
(click button)

last window tells you:
username is gonna be your emailprosper.spurius@gmail.com ;
ftp server address is: ftp.alwaysdata.com and your login is <subdomain name>
ssh address is: ssh.alwaysdata.com and your login is <subdomain name>. You have to turn it on on the administration console.
your web address is http://<subdomain>.alwaysdata.net/


The admin is also in french, but you can probably figure it out. (I am not actually a french speaker but I speak spanish natively and can speak catalan so french is relatively easy to read to me.)


edit: of course the alternative is getting one of the $10-30 a year packages that host python/django in english.

deimos fucked around with this message at 17:35 on Jan 30, 2009

Stephen
Feb 6, 2004

Stoned

deimos posted:

For learning probably get a 10MB account at http://www.alwaysdata.com/ it's free.
Yeah this won't really help. I already have the hosting and server, I just don't have command line access. Basically I've been given a project to do at my job that is easy to build and has a distant deadline and I thought it would be good to branch out my skills a bit by learning Python. Unfortunately I won't be able to use Python if I can't just build a site and then upload it a la PHP.

I guess I'm just a little confused on the whole process of how a web app is created using Python.

deimos
Nov 30, 2006

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

Stephen posted:

Yeah this won't really help. I already have the hosting and server, I just don't have command line access. Basically I've been given a project to do at my job that is easy to build and has a distant deadline and I thought it would be good to branch out my skills a bit by learning Python. Unfortunately I won't be able to use Python if I can't just build a site and then upload it a la PHP.

I guess I'm just a little confused on the whole process of how a web app is created using Python.

Your company is using a host to which you don't have shell access? :psyduck:

Stephen
Feb 6, 2004

Stoned

deimos posted:

Your company is using a host to which you don't have shell access? :psyduck:

Correct. I'm just a lowly Web Developer. I can't be trusted with such toys. Up until now, we've simply used PHP to build all our pages so it hasn't been needed anyways.

geera
May 20, 2003

Stephen posted:

Correct. I'm just a lowly Web Developer. I can't be trusted with such toys. Up until now, we've simply used PHP to build all our pages so it hasn't been needed anyways.
I'm relatively new to Python and using it to develop for the web also, but from what I can gather, you basically need some kind of web framework in order to make developing websites in Python not be a total kludge. I come from a similar PHP background and am also used to LAMP being available everywhere, so there is a new learning curve to web development with Python.

Anyway, you could try something like web.py as your web framework, which is much smaller than Django and is likely easier to install. Haven't tried it yet myself.

WickedMetalHead
Mar 9, 2007
/dev/null
If you only have FTP access getting python to work is likely going to be beyond what you want to do unless someone who has shell access is willing to do the intial setup.

Kire
Aug 25, 2006
Is there an easy way to randomize the retrieval of key, value pairs from a dict?

I have a paired list of {English : German} words, and I want to randomly pull an English word and its German translation. I know about d.keys(), but I'm not sure how to randomly select one from the result.

wrok
Mar 24, 2006

by angerbotSD

Kire posted:

Is there an easy way to randomize the retrieval of key, value pairs from a dict?

I have a paired list of {English : German} words, and I want to randomly pull an English word and its German translation. I know about d.keys(), but I'm not sure how to randomly select one from the result.

code:
import random

random_key = dictionary.keys()[random.randint(0,len(dictionary.keys())-1)]
right?

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Kire posted:

Is there an easy way to randomize the retrieval of key, value pairs from a dict?

I have a paired list of {English : German} words, and I want to randomly pull an English word and its German translation. I know about d.keys(), but I'm not sure how to randomly select one from the result.

why not choice = random.choice(d.keys())?

code:
>>> import random
>>> x = {1:2,3:4}
>>> random.choice(x.keys())
1

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
I think for his purposes something more like
code:
english, german = random.choice[english2german.items()]
But otherwise I'm with you, m0nk3yz

deimos
Nov 30, 2006

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

Stephen posted:

Correct. I'm just a lowly Web Developer. I can't be trusted with such toys. Up until now, we've simply used PHP to build all our pages so it hasn't been needed anyways.

Tell your admins to set up django or mod_python for you, once that is done you can just drop files in a directory. mod_python can be set up to work just like php.

For testing you can run django localy on the local server. We'd be more than happy to help you in the django thread if you go that way.

Habnabit
Dec 30, 2007

lift your skinny fists like
antennas in germany.

deimos posted:

Tell your admins to set up django or mod_python for you, once that is done you can just drop files in a directory. mod_python can be set up to work just like php.

For testing you can run django localy on the local server. We'd be more than happy to help you in the django thread if you go that way.
Bleh mod_python. There's more problems with it that aren't even listed on that page, too. It would be better to use mod_wsgi, given the option. Its configuration is simpler, and it also supports adding actions to file types.

Kire posted:

Is there an easy way to randomize the retrieval of key, value pairs from a dict?

I have a paired list of {English : German} words, and I want to randomly pull an English word and its German translation. I know about d.keys(), but I'm not sure how to randomly select one from the result.
random.sample(d, N)

deimos
Nov 30, 2006

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

Habnabit posted:

Bleh mod_python. There's more problems with it that aren't even listed on that page, too. It would be better to use mod_wsgi, given the option. Its configuration is simpler, and it also supports adding actions to file types.

configuration is simpler depending on the distribution, I had a nightmare of a time getting it to work on centos. mod_python is old dependable, I don't use it but it's what's easiest to install.

duck monster
Dec 15, 2004

deimos posted:

I am gonna ask the boss for PyCon, and I am pretty sure I am going to go regardless of job paying for it, at the very least for the conference.

That being said, this is discouraging.

m0nk3yz do you think it'll be better this year?

I know a couple of developers who have decided to give 09 a miss due to the poo poo that went on with the last pycon. Spending $5K+ to fly from australia to go to a conference on the other side of the earth only to have it wrecked by blatant spamming by vendors doesn't endear folks to the organisation of it all.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

duck monster posted:

I know a couple of developers who have decided to give 09 a miss due to the poo poo that went on with the last pycon. Spending $5K+ to fly from australia to go to a conference on the other side of the earth only to have it wrecked by blatant spamming by vendors doesn't endear folks to the organisation of it all.

And they're shortsighted if they don't think the organizers didn't listen to the feedback or attempt to change things. Everyone involved in 08 admitted to the mistakes, and steps were taken to rectify it.

sd6
Jan 14, 2008

This has all been posted before, and it will all be posted again
EDIT: Never mind, I'm retarded.

sd6 fucked around with this message at 02:10 on Feb 2, 2009

m0nk3yz
Mar 13, 2002

Behold the power of cheese!
Anyone looking for a giant read, I just reprinted an old article of mine on Python threads the and global interpreter lock. I'm going to be publishing all of my stuff through 2008 soonish. :frogsiren:

deimos
Nov 30, 2006

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

m0nk3yz posted:

Anyone looking for a giant read, I just reprinted an old article of mine on Python threads the and global interpreter lock. I'm going to be publishing all of my stuff through 2008 soonish. :frogsiren:

:swoon:

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

deimos posted:

:swoon:

I realize now that single article is like 6500 words. I should make a cliff notes version.

Nigel Tufnel
Jan 4, 2005
You can't really dust for vomit.
Very very new to Python (learnt BASIC way back in the day). I've followed what I can of some tutorials but need answers to the following:

1. How can I embed a string within an outputted line?
Example:

code:
x='yes'
y='no'

print 'there is' X 'or' Y
Desired output: there is yes or there is no

What code do I need to use to get the strings x and y into my output?


2. How can I get outputs to split between lines?

Desired output:

This is line 1 with variable1
This is line 2 with variable2

something like:

code:
print 'this is line 1 with' variable1 \n\
print 'this is line 1 with' variable2
But that doesn't work because I cannae code for poo poo.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
1. Use the + operator to append variables to strings.
code:
print 'there is  ' + X + ' or ' + Y
2. Print automatically puts a new line at the end, so you just need this:
code:
print 'this is line 1 with ' + variable1
print 'this is line 2 with ' + variable2

duck monster
Dec 15, 2004

Nigel Tufnel posted:

Very very new to Python (learnt BASIC way back in the day). I've followed what I can of some tutorials but need answers to the following:

1. How can I embed a string within an outputted line?
Example:

code:
x='yes'
y='no'

print 'there is' X 'or' Y
Desired output: there is yes or there is no

Optimally you might use some sort of string interpolation (google it), but
perhaps a little more understandably

print 'there is' + X + ' or ' + Y

Note, you can have type errors with this, so be careful.

quote:

What code do I need to use to get the strings x and y into my output?


2. How can I get outputs to split between lines?

Desired output:

This is line 1 with variable1
This is line 2 with variable2

something like:

code:
print 'this is line 1 with' variable1 \n\
print 'this is line 1 with' variable2
But that doesn't work because I cannae code for poo poo.
print 'this is line 1 with '+variable1
print 'this is line 1 with '+variable2

Dont worry about the '\n' with print, because it does it automatically.

Theres also the option with print to use commas to separate stuff ie

print 'this is line 1 with' , variable1
print 'this is line 1 with' , variable2

and infact that might even be better because its less likely to poo poo itself if you have to mix string and int, although I still think string interpolation is the way to go, however I dont have the time to explain it, so google is your friend.

mwarkentin
Oct 26, 2004
The other way to do it.. I prefer this way..

code:
>>> x = 'yes'
>>> y = 'no'
>>> print 'There is %s or there is %s' % (x, y,)
There is yes or there is no
code:
>>> variable1 = 'var1'
>>> variable2 = 'var2'
>>> print 'This is line 1 with %s' % (variable1,)
This is line 1 with var1
>>> print 'This is line 2 with %s' % (variable2,)
This is line 2 with var2

m0nk3yz
Mar 13, 2002

Behold the power of cheese!
I know there was some discussion about contextmanagers earlier in the thread, as an FYI I just put the article from pymag up which gives a lot more information. It's only 2000 words or so, so I shouldn't get yelled at for it being too long. Here.

bitprophet
Jul 22, 2004
Taco Defender

m0nk3yz posted:

I know there was some discussion about contextmanagers earlier in the thread, as an FYI I just put the article from pymag up which gives a lot more information. It's only 2000 words or so, so I shouldn't get yelled at for it being too long. Here.

tl;dr


Seriously though, that was a great article :) makes me wonder if getting the Python Mag is worth it after all. (I assume you get a subscription for free for being a contributor? How is it?)

I also need to work through your other, longer recent reposts sometime; skipped them for now, not really due to the length but due to not caring as much about threading (yet).

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

bitprophet posted:

tl;dr


Seriously though, that was a great article :) makes me wonder if getting the Python Mag is worth it after all. (I assume you get a subscription for free for being a contributor? How is it?)

I also need to work through your other, longer recent reposts sometime; skipped them for now, not really due to the length but due to not caring as much about threading (yet).

Nope, no free subscription - they pay you to write articles though. I'm an associate editor too, so I get to do technical reviews. I'll be pushing a few more up (one on paramiko) as the week goes on, given I've braincramped trying to grok twisted.

RjY
Apr 24, 2003

unpack("8s","MAP01\0\0\0") gave me MAP01\0\0\0: it seems 8s is not handled as a zero-terminated string, but as a fixed-length blob. The best I could come up with was a wrapper around unpack:
code:
def _z(s):
  if type(s) == str:
    i = s.find('\0')
    if i != -1:
      s = s[:i]
  return s

def unpackz(format, data):
  return tuple(_z(s) for s in unpack(format, data))
So how come unpack doesn't support C-style zero-terminated strings, given that the whole point of its parent module seems to be to unpack C structs?

deimos
Nov 30, 2006

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

RjY posted:

unpack("8s","MAP01\0\0\0") gave me MAP01\0\0\0: it seems 8s is not handled as a zero-terminated string, but as a fixed-length blob. The best I could come up with was a wrapper around unpack:
code:
def _z(s):
  if type(s) == str:
    i = s.find('\0')
    if i != -1:
      s = s[:i]
  return s

def unpackz(format, data):
  return tuple(_z(s) for s in unpack(format, data))
So how come unpack doesn't support C-style zero-terminated strings, given that the whole point of its parent module seems to be to unpack C structs?

It does support them, unpack determines the size of the resulting container. MAP01\0\0\0 is the correct output, try printing it and you'll see it prints correctly.

Also to clean it:
code:
def _z(s):
    return s[:s.index('\x00')]
is a lot quicker than what you have you could even do the mundane s.rstrip("\x00")

deimos fucked around with this message at 23:51 on Feb 3, 2009

RjY
Apr 24, 2003

deimos posted:

It does support them, unpack determines the size of the resulting container. MAP01\0\0\0 is the correct output, try printing it and you'll see it prints correctly.
Maybe it's correct from the point of view of python, which can obviously handle zero bytes in strings, and so can treat them as arbitrary binary blobs - but in C (which is where this data is coming from) strings are terminated with a zero so the correct output should just be MAP01. In C it's extremely common to put a variable-length zero-terminated string into a fixed-size character array and I am surprised unpack won't do this for me. (Especially when it handles Pascal-style length-prefixed strings - compared to C, how common are they?)

deimos posted:

code:
def _z(s):
    return s[:s.index('\x00')]
is a lot quicker than what you have you could even do the mundane s.rstrip("\x00")
All right but if I have more in the format string than 8s, or if the string I'm unpacking is 8 characters long and so doesn't have a \0 in it, it's just going to throw exceptions. (I am actually trying to unpack a struct { int offset, length; char name[8]; } using a format string of ii8s, but opted not to complicate the initial post with that)

tripwire
Nov 19, 2004

        ghost flow

RjY posted:

Maybe it's correct from the point of view of python, which can obviously handle zero bytes in strings, and so can treat them as arbitrary binary blobs - but in C (which is where this data is coming from) strings are terminated with a zero so the correct output should just be MAP01. In C it's extremely common to put a variable-length zero-terminated string into a fixed-size character array and I am surprised unpack won't do this for me. (Especially when it handles Pascal-style length-prefixed strings - compared to C, how common are they?)
All right but if I have more in the format string than 8s, or if the string I'm unpacking is 8 characters long and so doesn't have a \0 in it, it's just going to throw exceptions. (I am actually trying to unpack a struct { int offset, length; char name[8]; } using a format string of ii8s, but opted not to complicate the initial post with that)

Its doing the correct thing, it sounds like you are just confused about the way it works.

>>> teapot = unpack('8s', 'MAP01\0\0\0')
>>> teapot
('MAP01\x00\x00\x00',)

The value of teapot is a tuple because unpack always returns a tuple, even with just one value. The size of teapot is determined by the size of the string you gave it- notice that its preserving the extra null characters.

>>> print teapot[0]
MAP01
>>> print ''.join(teapot)
MAP01

Its printing the correct value. When you just call unpack from a prompt interactively it will show you null characters as well if they exist. What are you intending to do, toss out the trailing nulls?

deimos
Nov 30, 2006

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

tripwire posted:

Its printing the correct value. When you just call unpack from a prompt interactively it will show you null characters as well if they exist. What are you intending to do, toss out the trailing nulls?

The interactive prompt always prints out the __repr__ which is the cause of this.

RjY posted:

All right but if I have more in the format string than 8s, or if the string I'm unpacking is 8 characters long and so doesn't have a \0 in it, it's just going to throw exceptions. (I am actually trying to unpack a struct { int offset, length; char name[8]; } using a format string of ii8s, but opted not to complicate the initial post with that)

code:
>>> unpack('8si8s', 'blah\0\0\0\0\x04\x03\x02\x01blah\0\0\0\0')
('blah\x00\x00\x00\x00', 16909060, 'blah\x00\x00\x00\x00')
Your point?

deimos fucked around with this message at 15:20 on Feb 4, 2009

Inverness
Feb 4, 2009

Fully configurable personal assistant.

mwarkentin posted:

The other way to do it.. I prefer this way..
The new standard for Python 3.0 (this is possible in 2.6 also) is the format function for string objects.
code:
>>> x = 'yes'
>>> y = 'no'
>>> print('There is {0} or there is {1}'.format(x, y))
There is yes or there is no
More information here

Print has also been changed to a function. You can enable the print function syntax in 2.6 using from __future__ import print_function.

I recommend any new to Python to start learning with version 2.6, as it is bridging the gap between Python 2 and 3.

Inverness fucked around with this message at 18:43 on Feb 4, 2009

Allie
Jan 17, 2004

tripwire posted:

Its printing the correct value. When you just call unpack from a prompt interactively it will show you null characters as well if they exist. What are you intending to do, toss out the trailing nulls?

The fact that you can't see the null bytes when you print the string is irrelevant. He just needs to get over it and do s.rstrip('\x00').

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Inverness posted:

I recommend any new to Python to start learning with version 2.6, as it is bridging the gap between Python 2 and 3.

I'll add something about this to the OP sooner or later.

79
Dec 25, 2005

Melancholy.

m0nk3yz posted:

Anyone looking for a giant read, I just reprinted an old article of mine on Python threads the and global interpreter lock. I'm going to be publishing all of my stuff through 2008 soonish. :frogsiren:

Thank you for posting "A (BRIEF) INTRODUCTION TO PYTHON-CORE DEVELOPMENT", I've been meaning to mess around with the runtime for a while, this will be helpful when I get around to it in the spring :)

Adbot
ADBOT LOVES YOU

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

So even though I posted in this tread about getting into stackless months ago, I got busy and am now trying to get back into it. What I have is a simple program to generate a sine wave in an array and send it through an audio stream to the sound card without the intermediate step of writing a wave file. What I want is a way to play two different waves concurrently.

code:
import math, wave, array, stackless

class Sine:
         
    def __init__(self, seconds, frequency, amplitude, channel, sampleRate=44100):
        cyclesPerSample = float(frequency)/sampleRate
        numberOfSamples = int(seconds*sampleRate)
        maxAmplitude = (amplitude/100.)*32767
        sampleArray = array.array('h')

        for nSamples in xrange(numberOfSamples):
            cycles = nSamples*cyclesPerSample
            cycles -= int(cycles) # makes 0.0<=cycles<1.0
            sampleValue = int(round(maxAmplitude*math.sin(2.0*math.pi*cycles))) # round 
            sampleArray.append(sampleValue)
        self.data = sampleArray        
        stackless.tasklet(self.send_array)(channel)
        
    def send_array(self, channel):
        channel.send(self.data)
code:
import pyaudio, stackless

class OutStream:
    
    def __init__(self, Format=pyaudio.paInt16, Channels=1, sampleRate=44100):
        self.channel = stackless.channel()
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format = Format, channels = Channels, rate = sampleRate, output = True)
        stackless.tasklet(self.listen)()
        
    def listen(self):
        while 1:
            data = self.channel.receive()
            stackless.tasklet(self.play)(data)
        
    def play(self, data):
        self.stream.write(data)
        
    def __del__(self):
        self.stream.close()
        self.p.terminate()
code:
import stackless
from Sine import *
from Output import *

output = OutStream()
output2 = OutStream()

x = Sine(1,650,75,channel=output.channel)
x = Sine(1,450,75,channel=output2.channel)

stackless.run()
I've been playing around with executing the code in different orders, like making the channels a property of Sine instead of Output and generating all the Sine data before sending any of it, but the second wave always plays after the first. There is a little bit of overlap between them, but if I lengthen the first wave the second wave still doesn't play until the first one is just about done, no matter the length. I don't need them to line up at a specific point or anything, but I would like to get them to output concurrently. Is this possible using anything along similar lines to what I've tried so far?

fart simpson fucked around with this message at 11:09 on Feb 5, 2009

  • Locked thread